diff options
Diffstat (limited to 'indoteknik_api/controllers/api_v1')
| -rw-r--r-- | indoteknik_api/controllers/api_v1/partner.py | 19 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v1/user.py | 198 |
2 files changed, 176 insertions, 41 deletions
diff --git a/indoteknik_api/controllers/api_v1/partner.py b/indoteknik_api/controllers/api_v1/partner.py index 369f2125..d1a41739 100644 --- a/indoteknik_api/controllers/api_v1/partner.py +++ b/indoteknik_api/controllers/api_v1/partner.py @@ -127,13 +127,9 @@ class Partner(controller.Controller): 'alamat_lengkap_text': [], 'street': [], }) - - # Mengambil id_user dari request id_user = self.get_request_params(kw, { 'id_user': ['number'] }) - - # Mengambil parameter user dari request params_user = self.get_request_params(kw, { 'company_type_id': ['number'], 'industry_id': ['number'], @@ -142,17 +138,26 @@ class Partner(controller.Controller): 'alamat_lengkap_text': [], }) - # Cek validitas parameter if not params['valid']: return self.response(code=400, description=params) - # Mencari partner dan user berdasarkan ID partner = request.env[self._name].search([('id', '=', params['value']['id'])], limit=1) user = request.env[self._name].search([('id', '=', id_user['value']['id_user'])], limit=1) - if not partner: return self.response(code=404, description='Partner not found') + if not params['value'].get('tax_name'): + params['value']['nama_wajib_pajak'] = params['value'].get('name') + params_user['value']['nama_wajib_pajak'] = params_user['value'].get('name') + + if not params['value'].get('alamat_lengkap_text'): + params['value']['alamat_lengkap_text'] = params['value'].get('street') + params_user['value']['alamat_lengkap_text'] = params_user['value'].get('street') + + if not params['value'].get('npwp'): + params['value']['npwp'] = "00.000.000.0-000.000" + params_user['value']['npwp'] = "00.000.000.0-000.000" + # Filter parameter yang memiliki nilai saja untuk partner params_filtered = {k: v for k, v in params['value'].items() if v} diff --git a/indoteknik_api/controllers/api_v1/user.py b/indoteknik_api/controllers/api_v1/user.py index a3eb5459..b66e0f3c 100644 --- a/indoteknik_api/controllers/api_v1/user.py +++ b/indoteknik_api/controllers/api_v1/user.py @@ -157,26 +157,20 @@ class User(controller.Controller): user.partner_id.email = email user.partner_id.mobile = phone - if type_acc == 'business' and business_name: - # Eksekusi query SQL menggunakan Levenshtein distance - query = """ - SELECT name, levenshtein(name::text, %s) AS distance - FROM res_partner - WHERE levenshtein(name::text, %s) < 3 - ORDER BY distance ASC - """ - params = (business_name, business_name) - request.env.cr.execute(query, params) - result = request.env.cr.fetchone() - - if result: - match_company_name = result[0] - match_company_id = result[2] - + if type_acc == 'business': + parameter = [ + ('company_type', '=', 'company'), + ('name', 'ilike', business_name) + ] + match_company = request.env['res.partner'].search(parameter, limit=1) + match_ratio = 0 + if match_company: + match_ratio = SequenceMatcher(None, match_company.name, business_name).ratio() + if match_ratio > 0.8: # Create a user company request request.env['user.company.request'].create({ 'user_id': user.partner_id.id, - 'user_company_id': match_company_id, + 'user_company_id': match_company.id, 'user_input': business_name }) else: @@ -260,7 +254,7 @@ class User(controller.Controller): 'activation_request': False, 'reason': None } - + user = self.get_user_by_email(email) if not user: response['reason'] = 'NOT_FOUND' @@ -415,8 +409,8 @@ class User(controller.Controller): @http.route(prefix + 'user/<id>/switch', auth='public', methods=['PUT', 'OPTIONS'], csrf=False) @controller.Controller.must_authorized() def switch_account(self, **kw): - id = kw.get('id') - user = request.env['res.users'].search([('id', '=', id)], limit=1) + id = int(kw.get('id')) + user = request.env['res.partner'].search([('id', '=', id)], limit=1) response = { 'switch': False, @@ -456,12 +450,24 @@ class User(controller.Controller): if match_company: match_ratio = SequenceMatcher(None, match_company.name, business_name).ratio() if match_ratio > 0.8: - # Create a user company request - request.env['user.company.request'].create({ - 'user_id': user.partner_id.id, - 'user_company_id': match_company.id, - 'user_input': business_name - }) + if user.parent_id : + match_company.email = email_partner, + match_company.customer_type = 'pkp' if is_pkp == 'true' else 'nonpkp', + match_company.name = business_name if business_name else business_name, + match_company.company_type_id.id = company_type_id if company_type_id else False, + match_company.industry_id.id = industry_id, + match_company.npwp = npwp if is_pkp == 'true' else (npwp if npwp else '00.000.000.0-000.000'), + match_company.sppkp = sppkp if is_pkp == 'true' else (sppkp if sppkp else '-'), + match_company.nama_wajib_pajak = nama_wajib_pajak, + match_company.alamat_lengkap_text = alamat_wajib_pajak, + match_company.street = alamat_bisnis, + else: + # Create a user company request + request.env['user.company.request'].create({ + 'user_id': user.partner_id.id, + 'user_company_id': match_company.id, + 'user_input': business_name + }) else: if not nama_wajib_pajak and is_pkp == 'false': nama_wajib_pajak = business_name @@ -488,7 +494,7 @@ class User(controller.Controller): } new_company = request.env['res.partner'].create(new_company_data) request.env['user.company.request'].create({ - 'user_id': user.partner_id.id, + 'user_id': user.id, 'user_company_id': new_company.id, 'user_input': business_name }) @@ -499,7 +505,7 @@ class User(controller.Controller): # user.partner_id.nama_wajib_pajak = new_company.nama_wajib_pajak # user.partner_id.alamat_lengkap_text = new_company.alamat_lengkap_text - if npwp_document: + if npwp_document != ' ': npwp_mimetype, _ = mimetypes.guess_type(npwp_filename) npwp_mimetype = npwp_mimetype or 'application/octet-stream' pdf_data = base64.b64decode(npwp_document) @@ -511,9 +517,12 @@ class User(controller.Controller): 'res_id': new_company.id, 'mimetype': npwp_mimetype }) - new_company.message_post(body="NPWP Uploaded", attachment_ids=[npwp_attachment.id]) + if user.parent_id: + match_company.message_post(body="NPWP Uploaded", attachment_ids=[npwp_attachment.id]) + else: + new_company.message_post(body="NPWP Uploaded", attachment_ids=[npwp_attachment.id]) - if sppkp_document: + if sppkp_document != ' ': sppkp_mimetype, _ = mimetypes.guess_type(sppkp_filename) sppkp_mimetype = sppkp_mimetype or 'application/octet-stream' pdf_data = base64.b64decode(sppkp_document) @@ -525,7 +534,13 @@ class User(controller.Controller): 'res_id': new_company.id, 'mimetype': sppkp_mimetype }) - new_company.message_post(body="SPPKP Uploaded", attachment_ids=[sppkp_attachment.id]) + if user.parent_id: + match_company.message_post(body="SPPKP Uploaded", attachment_ids=[sppkp_attachment.id]) + else: + new_company.message_post(body="SPPKP Uploaded", attachment_ids=[sppkp_attachment.id]) + request_company = self.get_user_by_email(user.email) + request_company.parent_name = business_name + request_company.send_company_request_mail() response['switch'] = 'Pending' return self.response(response) @@ -534,7 +549,7 @@ class User(controller.Controller): # @controller.Controller.must_authorized() def switch_account_progres(self, **kw): id = int(kw.get('id')) - user = request.env['res.users'].search([('id', '=', id)], limit=1) + # user = request.env['res.partner'].search([('id', '=', id)], limit=1) response = { 'status': '' } @@ -550,5 +565,120 @@ class User(controller.Controller): else: response['status'] = new_company_request.is_approve else: - response['status'] = 'unknown' - return self.response(response)
\ No newline at end of file + response['status'] = False + return self.response(response) + + def get_user_by_email(self, email): + return request.env['res.users'].search([ + ('login', '=', email), + ('active', 'in', [True, False]) + ]) + + @http.route(prefix + 'user/download/npwp/<id>', auth='public', methods=['GET']) + def download_npwp(self, **kw): + id = int(kw.get('id')) + """Download NPWP file for the given company""" + # Search for the NPWP attachment associated with the company + attachment = request.env['ir.attachment'].search([ + ('res_model', '=', 'res.partner'), + ('res_id', '=', id), + ('name', '=', 'NPWP Document') + ], limit=1) + + if not attachment: + return request.not_found() + + # Get the file data and content type + npwp_mimetype = attachment.mimetype # This should be directly from the attachment + npwp_mimetype = npwp_mimetype or 'application/octet-stream' + + # Create a filename with the correct extension + filename = f"{attachment.name}" + + # Manually set the Content-Disposition header to inline + content_disposition = f'inline; filename="{filename}"' + + # Send the file as a response + return request.make_response( + base64.b64decode(attachment.datas), + headers=[ + ('Content-Type', npwp_mimetype), + ('Content-Disposition', content_disposition) + ] + ) + + @http.route(prefix + 'user/chek/npwp/<id>', auth='public', methods=['GET']) + def chek_npwp(self, **kw): + id = int(kw.get('id')) + """Download NPWP file for the given company""" + # Search for the NPWP attachment associated with the company + attachment = request.env['ir.attachment'].search([ + ('res_model', '=', 'res.partner'), + ('res_id', '=', id), + ('name', '=', 'NPWP Document') + ], limit=1) + response = { + 'status': '' + } + if attachment: + response['status'] = True + else: + response['status'] = False + + # If no attachment is found, return status False + return self.response(response) + + @http.route(prefix + 'user/download/sppkp/<id>', auth='public', methods=['GET']) + def download_sppkp(self, **kw): + id = int(kw.get('id')) + """Download SPPKP file for the given company""" + # Search for the SPPKP attachment associated with the company + attachment = request.env['ir.attachment'].search([ + ('res_model', '=', 'res.partner'), + ('res_id', '=', id), + ('name', '=', 'SPPKP Document') + ], limit=1) + + if not attachment: + return request.not_found() + + # Get the file data and content type + sppkp_mimetype = attachment.mimetype # Use the MIME type directly from the attachment + sppkp_mimetype = sppkp_mimetype or 'application/octet-stream' + + # Create a filename with the correct extension + filename = attachment.name # No need to append extension since attachment should already have it + + # Manually set the Content-Disposition header to inline + content_disposition = f'inline; filename="{filename}"' + + # Send the file as a response + return request.make_response( + base64.b64decode(attachment.datas), + headers=[ + ('Content-Type', sppkp_mimetype), + ('Content-Disposition', content_disposition) # Ensure inline display + ] + ) + + @http.route(prefix + 'user/chek/sppkp/<id>', auth='public', methods=['GET']) + def chek_sppkp(self, **kw): + id = int(kw.get('id')) + """Download SPPKP file for the given company""" + # Search for the SPPKP attachment associated with the company + attachment = request.env['ir.attachment'].search([ + ('res_model', '=', 'res.partner'), + ('res_id', '=', id), + ('name', '=', 'SPPKP Document') + ], limit=1) + + response = { + 'status': '' + } + if attachment: + response['status'] = True + else: + response['status'] = False + + # If no attachment is found, return status False + return self.response(response) |
