summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers/api_v1/user.py
diff options
context:
space:
mode:
authorIT Fixcomart <it@fixcomart.co.id>2025-01-15 10:01:14 +0000
committerIT Fixcomart <it@fixcomart.co.id>2025-01-15 10:01:14 +0000
commite7c181dbc0cf45a7ffcead50851c0b498c93e15e (patch)
tree7b656969cf2657d0f1d0123d1d7c7c7eeb429bd0 /indoteknik_api/controllers/api_v1/user.py
parent26688e4143d28f6a03eaf3f860ab25142ea7ca23 (diff)
parent5f0026f595b28f937b1ee06828555586333892b3 (diff)
Merged in iman/switch-account (pull request #267)
Iman/switch account
Diffstat (limited to 'indoteknik_api/controllers/api_v1/user.py')
-rw-r--r--indoteknik_api/controllers/api_v1/user.py330
1 files changed, 280 insertions, 50 deletions
diff --git a/indoteknik_api/controllers/api_v1/user.py b/indoteknik_api/controllers/api_v1/user.py
index addf2c9f..f651d9fa 100644
--- a/indoteknik_api/controllers/api_v1/user.py
+++ b/indoteknik_api/controllers/api_v1/user.py
@@ -437,8 +437,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,
@@ -461,7 +461,14 @@ class User(controller.Controller):
nama_wajib_pajak = kw.get('nama_wajib_pajak', False)
is_pkp = kw.get('is_pkp')
type_acc = kw.get('type_acc', False)
-
+ parent_id = kw.get('parent_id', False)
+ if parent_id:
+ try:
+ parent_id = int(parent_id)
+ except ValueError:
+ parent_id = False
+ else:
+ parent_id = False
response = {
'switch': False,
@@ -473,55 +480,23 @@ class User(controller.Controller):
('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_input': business_name
- })
- else:
- if not nama_wajib_pajak and is_pkp == 'false':
- nama_wajib_pajak = business_name
-
- if not alamat_wajib_pajak and is_pkp == 'false':
- alamat_wajib_pajak = alamat_bisnis
-
- new_company_data = {
+ if parent_id:
+ match_company = request.env['res.partner'].search([('id', '=', parent_id)], limit=1)
+ new_data = {
+ 'email': email_partner,
+ 'customer_type': 'pkp' if is_pkp == 'true' else 'nonpkp',
'name': business_name if business_name else business_name,
'company_type_id': company_type_id if company_type_id else False,
'industry_id': industry_id,
- 'customer_type': 'pkp' if is_pkp == 'true' else 'nonpkp',
'npwp': npwp if is_pkp == 'true' else (npwp if npwp else '00.000.000.0-000.000'),
'sppkp': sppkp if is_pkp == 'true' else (sppkp if sppkp else '-'),
'nama_wajib_pajak': nama_wajib_pajak,
'alamat_lengkap_text': alamat_wajib_pajak,
- 'email': email_partner,
'street': alamat_bisnis,
- 'company_type': 'company',
- 'user_id': 3222,
- 'property_account_receivable_id': 395,
- 'property_account_payable_id': 438,
- 'active': False,
}
- new_company = request.env['res.partner'].create(new_company_data)
- request.env['user.company.request'].create({
- 'user_id': user.partner_id.id,
- 'user_company_id': new_company.id,
- 'user_input': business_name
- })
- # user.partner_id.parent_id = new_company.id
- # user.partner_id.customer_type = new_company.customer_type
- # user.partner_id.npwp = new_company.npwp
- # user.partner_id.sppkp = new_company.sppkp
- # user.partner_id.nama_wajib_pajak = new_company.nama_wajib_pajak
- # user.partner_id.alamat_lengkap_text = new_company.alamat_lengkap_text
+ match_company.write(new_data)
- 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)
@@ -530,12 +505,12 @@ class User(controller.Controller):
'type': 'binary',
'datas': base64.b64encode(pdf_data),
'res_model': 'res.partner',
- 'res_id': new_company.id,
+ 'res_id': match_company.id,
'mimetype': npwp_mimetype
})
- new_company.message_post(body="NPWP Uploaded", attachment_ids=[npwp_attachment.id])
+ match_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)
@@ -544,11 +519,149 @@ class User(controller.Controller):
'type': 'binary',
'datas': base64.b64encode(pdf_data),
'res_model': 'res.partner',
- 'res_id': new_company.id,
+ 'res_id': match_company.id,
'mimetype': sppkp_mimetype
})
- new_company.message_post(body="SPPKP Uploaded", attachment_ids=[sppkp_attachment.id])
-
+ match_company.message_post(body="SPPKP Uploaded", attachment_ids=[sppkp_attachment.id])
+ request.env['user.company.request'].create({
+ 'user_id': user.id,
+ 'user_company_id': match_company.id,
+ 'user_input': business_name,
+ 'is_switch_account': True
+ })
+ else:
+ 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:
+ if user.parent_id :
+ new_data = {
+ 'email': email_partner,
+ 'customer_type': 'pkp' if is_pkp == 'true' else 'nonpkp',
+ 'name': business_name if business_name else business_name,
+ 'company_type_id': company_type_id if company_type_id else False,
+ 'industry_id': industry_id,
+ 'npwp': npwp if is_pkp == 'true' else (npwp if npwp else '00.000.000.0-000.000'),
+ 'sppkp': sppkp if is_pkp == 'true' else (sppkp if sppkp else '-'),
+ 'nama_wajib_pajak': nama_wajib_pajak,
+ 'alamat_lengkap_text': alamat_wajib_pajak,
+ 'street': alamat_bisnis,
+ }
+ match_company.write(new_data)
+
+ if npwp_document != ' ':
+ npwp_mimetype, _ = mimetypes.guess_type(npwp_filename)
+ npwp_mimetype = npwp_mimetype or 'application/octet-stream'
+ pdf_data = base64.b64decode(npwp_document)
+ npwp_attachment = request.env['ir.attachment'].create({
+ 'name': 'NPWP Document',
+ 'type': 'binary',
+ 'datas': base64.b64encode(pdf_data),
+ 'res_model': 'res.partner',
+ 'res_id': match_company.id,
+ 'mimetype': npwp_mimetype
+ })
+ match_company.message_post(body="NPWP Uploaded", attachment_ids=[npwp_attachment.id])
+
+ if sppkp_document != ' ':
+ sppkp_mimetype, _ = mimetypes.guess_type(sppkp_filename)
+ sppkp_mimetype = sppkp_mimetype or 'application/octet-stream'
+ pdf_data = base64.b64decode(sppkp_document)
+ sppkp_attachment = request.env['ir.attachment'].create({
+ 'name': 'SPPKP Document',
+ 'type': 'binary',
+ 'datas': base64.b64encode(pdf_data),
+ 'res_model': 'res.partner',
+ 'res_id': match_company.id,
+ 'mimetype': sppkp_mimetype
+ })
+ match_company.message_post(body="SPPKP Uploaded", attachment_ids=[sppkp_attachment.id])
+ else:
+ # Create a user company request
+ request.env['user.company.request'].create({
+ 'user_id': user.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
+
+ if not alamat_wajib_pajak and is_pkp == 'false':
+ alamat_wajib_pajak = alamat_bisnis
+
+ new_company_data = {
+ 'name': business_name if business_name else business_name,
+ 'company_type_id': company_type_id if company_type_id else False,
+ 'industry_id': industry_id,
+ 'customer_type': 'pkp' if is_pkp == 'true' else 'nonpkp',
+ 'npwp': npwp if is_pkp == 'true' else (npwp if npwp else '00.000.000.0-000.000'),
+ 'sppkp': sppkp if is_pkp == 'true' else (sppkp if sppkp else '-'),
+ 'nama_wajib_pajak': nama_wajib_pajak,
+ 'alamat_lengkap_text': alamat_wajib_pajak,
+ 'email': email_partner,
+ 'street': alamat_bisnis,
+ 'company_type': 'company',
+ 'user_id': 3222,
+ 'property_account_receivable_id': 395,
+ 'property_account_payable_id': 438,
+ 'active': False,
+ }
+ new_company = request.env['res.partner'].create(new_company_data)
+ request.env['user.company.request'].create({
+ 'user_id': user.id,
+ 'user_company_id': new_company.id,
+ 'user_input': business_name
+ })
+ # user.partner_id.parent_id = new_company.id
+ # user.partner_id.customer_type = new_company.customer_type
+ # user.partner_id.npwp = new_company.npwp
+ # user.partner_id.sppkp = new_company.sppkp
+ # 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:
+ npwp_mimetype, _ = mimetypes.guess_type(npwp_filename)
+ npwp_mimetype = npwp_mimetype or 'application/octet-stream'
+ pdf_data = base64.b64decode(npwp_document)
+ npwp_attachment = request.env['ir.attachment'].create({
+ 'name': 'NPWP Document',
+ 'type': 'binary',
+ 'datas': base64.b64encode(pdf_data),
+ 'res_model': 'res.partner',
+ 'res_id': new_company.id,
+ 'mimetype': npwp_mimetype
+ })
+ 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:
+ sppkp_mimetype, _ = mimetypes.guess_type(sppkp_filename)
+ sppkp_mimetype = sppkp_mimetype or 'application/octet-stream'
+ pdf_data = base64.b64decode(sppkp_document)
+ sppkp_attachment = request.env['ir.attachment'].create({
+ 'name': 'SPPKP Document',
+ 'type': 'binary',
+ 'datas': base64.b64encode(pdf_data),
+ 'res_model': 'res.partner',
+ 'res_id': new_company.id,
+ 'mimetype': sppkp_mimetype
+ })
+ 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
+ # if user.parent_id:
+ # request_company.send_company_switch_approve_mail()
+ # else:
+ # request_company.send_company_request_mail_switch()
+
+ request_company.send_company_request_mail_switch()
response['switch'] = 'Pending'
return self.response(response)
@@ -556,7 +669,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': ''
}
@@ -572,5 +685,122 @@ class User(controller.Controller):
else:
response['status'] = new_company_request.is_approve
else:
- response['status'] = 'unknown'
+ 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', 'OPTIONS'])
+ @controller.Controller.must_authorized()
+ 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', 'OPTIONS'])
+ @controller.Controller.must_authorized()
+ 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) \ No newline at end of file