From 39e27d0187d352dfea7db1bc1c9aece42e348caa Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 27 Jan 2023 11:08:02 +0700 Subject: sale order and invoice api --- indoteknik_api/models/__init__.py | 1 + indoteknik_api/models/account_move.py | 23 ++++++++++++++++++++++- indoteknik_api/models/rest_api.py | 13 +++++++++++++ indoteknik_api/models/sale_order.py | 25 +++++++++++++++++++++++-- 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 indoteknik_api/models/rest_api.py (limited to 'indoteknik_api/models') diff --git a/indoteknik_api/models/__init__.py b/indoteknik_api/models/__init__.py index 98d84a80..a4f7363a 100644 --- a/indoteknik_api/models/__init__.py +++ b/indoteknik_api/models/__init__.py @@ -4,6 +4,7 @@ from . import product_pricelist from . import product_product from . import product_template from . import res_users +from . import rest_api from . import sale_order from . import x_manufactures from . import website_content diff --git a/indoteknik_api/models/account_move.py b/indoteknik_api/models/account_move.py index 9fd6fb18..3f85a447 100644 --- a/indoteknik_api/models/account_move.py +++ b/indoteknik_api/models/account_move.py @@ -6,7 +6,7 @@ from pytz import timezone class AccountMove(models.Model): _inherit = 'account.move' - def api_v1_single_response(self, account_move): + def api_v1_single_response(self, account_move, context=False): data = { 'id': account_move.id, 'name': account_move.name, @@ -17,4 +17,25 @@ class AccountMove(models.Model): 'amount_residual': account_move.amount_residual, 'invoice_date': account_move.invoice_date.strftime('%d/%m/%Y') or '' } + if context: + if context == 'with_detail': + res_users = self.env['res.users'] + data_with_detail = { + 'id': account_move.id, + 'name': account_move.name, + 'purchase_order_name': account_move.ref or '', + 'payment_term': account_move.invoice_payment_term_id.name or '', + 'sales': account_move.invoice_user_id.name, + 'amount_total': account_move.amount_total, + 'amount_residual': account_move.amount_residual, + 'invoice_date': account_move.invoice_date.strftime('%d/%m/%Y') or '', + 'invoice_date_due': account_move.invoice_date_due.strftime('%d/%m/%Y') or '', + 'customer': res_users.api_address_response(account_move.partner_id), + 'products': [], + } + for line in account_move.invoice_line_ids: + product = self.env['product.product'].api_single_response(line.product_id) + product['quantity'] = line.quantity + data_with_detail['products'].append(product) + data.update(data_with_detail) return data diff --git a/indoteknik_api/models/rest_api.py b/indoteknik_api/models/rest_api.py new file mode 100644 index 00000000..35cce201 --- /dev/null +++ b/indoteknik_api/models/rest_api.py @@ -0,0 +1,13 @@ +from odoo import models +import datetime +from pytz import timezone + + +class RestApi(models.TransientModel): + _name = 'rest.api' + + def datetime_to_str(self, object, format): + time = '' + if isinstance(object, datetime.datetime): + time = object.astimezone(timezone('Asia/Jakarta')).strftime(format) + return time \ No newline at end of file diff --git a/indoteknik_api/models/sale_order.py b/indoteknik_api/models/sale_order.py index 3359ee6a..aa20ccdb 100644 --- a/indoteknik_api/models/sale_order.py +++ b/indoteknik_api/models/sale_order.py @@ -4,11 +4,32 @@ from odoo import models class SaleOrder(models.Model): _inherit = 'sale.order' - def api_v1_single_response(self, sale_order): + def api_v1_single_response(self, sale_order, context=False): data = { 'id': sale_order.id, 'name': sale_order.name, 'sales': sale_order.user_id.name, - 'amount_total': sale_order.amount_total + 'amount_total': sale_order.amount_total, + 'purchase_order_name': sale_order.partner_purchase_order_name, + 'invoice_count': sale_order.invoice_count } + if context: + if context == 'with_detail': + res_users = self.env['res.users'] + data_with_detail = { + 'payment_term': sale_order.payment_term_id.name or '', + 'date_order': self.env['rest.api'].datetime_to_str(sale_order.date_order, '%d/%m/%Y %H:%M:%S'), + 'products': [], + 'address': { + 'customer': res_users.api_address_response(sale_order.partner_id), + 'invoice': res_users.api_address_response(sale_order.partner_invoice_id), + 'shipping': res_users.api_address_response(sale_order.partner_shipping_id) + }, + 'invoices': [self.env['account.move'].api_v1_single_response(x) for x in sale_order.invoice_ids] + } + for line in sale_order.order_line: + product = self.env['product.product'].api_single_response(line.product_id) + product['quantity'] = line.product_uom_qty + data_with_detail['products'].append(product) + data.update(data_with_detail) return data -- cgit v1.2.3 From 27a1a3d68f728455ae196d2aa5736858ba623d09 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Tue, 31 Jan 2023 13:57:05 +0700 Subject: api category tree --- indoteknik_api/models/sale_order.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'indoteknik_api/models') diff --git a/indoteknik_api/models/sale_order.py b/indoteknik_api/models/sale_order.py index aa20ccdb..826f2a14 100644 --- a/indoteknik_api/models/sale_order.py +++ b/indoteknik_api/models/sale_order.py @@ -11,8 +11,23 @@ class SaleOrder(models.Model): 'sales': sale_order.user_id.name, 'amount_total': sale_order.amount_total, 'purchase_order_name': sale_order.partner_purchase_order_name, - 'invoice_count': sale_order.invoice_count + 'invoice_count': sale_order.invoice_count, + 'status': 'draft', } + if sale_order.state == 'cancel': + data['status'] = 'cancel' + if sale_order.state in ['draft', 'sent']: + data['status'] = 'draft' + if sale_order.approval_status in ['pengajuan1', 'pengajuan2']: + data['status'] = 'waiting' + if sale_order.state == 'sale': + data['status'] = 'sale' + for picking in sale_order.picking_ids: + if picking.state == 'assigned': + data['status'] = 'shipping' + if sale_order.state == 'done': + data['status'] = 'done' + if context: if context == 'with_detail': res_users = self.env['res.users'] -- cgit v1.2.3 From 06534703452ee4c3afbabd21e872d20b085badba Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 2 Feb 2023 14:20:17 +0700 Subject: fix error parse time to str on account_move --- indoteknik_api/models/account_move.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'indoteknik_api/models') diff --git a/indoteknik_api/models/account_move.py b/indoteknik_api/models/account_move.py index 3f85a447..c70c21a5 100644 --- a/indoteknik_api/models/account_move.py +++ b/indoteknik_api/models/account_move.py @@ -15,8 +15,10 @@ class AccountMove(models.Model): 'sales': account_move.invoice_user_id.name, 'amount_total': account_move.amount_total, 'amount_residual': account_move.amount_residual, - 'invoice_date': account_move.invoice_date.strftime('%d/%m/%Y') or '' + 'invoice_date': '' } + if isinstance(object, datetime.date): + data['invoice_date'] = account_move.invoice_date.strftime('%d/%m/%Y') if context: if context == 'with_detail': res_users = self.env['res.users'] -- cgit v1.2.3 From 6b97d72c91f9a36462d08027660dddab945db01c Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 2 Feb 2023 14:26:42 +0700 Subject: show invoice only in posted status --- indoteknik_api/models/sale_order.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'indoteknik_api/models') diff --git a/indoteknik_api/models/sale_order.py b/indoteknik_api/models/sale_order.py index 826f2a14..c7d488be 100644 --- a/indoteknik_api/models/sale_order.py +++ b/indoteknik_api/models/sale_order.py @@ -40,11 +40,14 @@ class SaleOrder(models.Model): 'invoice': res_users.api_address_response(sale_order.partner_invoice_id), 'shipping': res_users.api_address_response(sale_order.partner_shipping_id) }, - 'invoices': [self.env['account.move'].api_v1_single_response(x) for x in sale_order.invoice_ids] + 'invoices': [] } for line in sale_order.order_line: product = self.env['product.product'].api_single_response(line.product_id) product['quantity'] = line.product_uom_qty data_with_detail['products'].append(product) + for invoice in sale_order.invoice_ids: + if invoice.state == 'posted': + data_with_detail['invoices'].append(self.env['account.move'].api_v1_single_response(invoice)) data.update(data_with_detail) return data -- cgit v1.2.3 From 3d402f5a12d50d2264dde918d4b893b9c8943389 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 2 Feb 2023 17:12:04 +0700 Subject: download tax invoice --- indoteknik_api/models/account_move.py | 5 +++-- indoteknik_api/models/rest_api.py | 6 +++++- 2 files changed, 8 insertions(+), 3 deletions(-) (limited to 'indoteknik_api/models') diff --git a/indoteknik_api/models/account_move.py b/indoteknik_api/models/account_move.py index c70c21a5..5589b5fc 100644 --- a/indoteknik_api/models/account_move.py +++ b/indoteknik_api/models/account_move.py @@ -1,6 +1,6 @@ import datetime from odoo import models -from pytz import timezone +import hashlib class AccountMove(models.Model): @@ -15,7 +15,8 @@ class AccountMove(models.Model): 'sales': account_move.invoice_user_id.name, 'amount_total': account_move.amount_total, 'amount_residual': account_move.amount_residual, - 'invoice_date': '' + 'invoice_date': '', + 'efaktur_token': self.env['rest.api'].md5_salt(account_move.id, 'account.move$') if account_move.efaktur_document else '', } if isinstance(object, datetime.date): data['invoice_date'] = account_move.invoice_date.strftime('%d/%m/%Y') diff --git a/indoteknik_api/models/rest_api.py b/indoteknik_api/models/rest_api.py index 35cce201..2c54769e 100644 --- a/indoteknik_api/models/rest_api.py +++ b/indoteknik_api/models/rest_api.py @@ -1,6 +1,7 @@ from odoo import models import datetime from pytz import timezone +import hashlib class RestApi(models.TransientModel): @@ -10,4 +11,7 @@ class RestApi(models.TransientModel): time = '' if isinstance(object, datetime.datetime): time = object.astimezone(timezone('Asia/Jakarta')).strftime(format) - return time \ No newline at end of file + return time + + def md5_salt(self, value, salt): + return hashlib.md5((salt + str(value)).encode()).hexdigest() \ No newline at end of file -- cgit v1.2.3 From 9eb80e0aad8966c42fa721738986737b4040e0e4 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 3 Feb 2023 10:10:16 +0700 Subject: [ADD] feature download invoice --- indoteknik_api/models/account_move.py | 2 +- indoteknik_api/models/rest_api.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'indoteknik_api/models') diff --git a/indoteknik_api/models/account_move.py b/indoteknik_api/models/account_move.py index 5589b5fc..f77ded16 100644 --- a/indoteknik_api/models/account_move.py +++ b/indoteknik_api/models/account_move.py @@ -16,7 +16,7 @@ class AccountMove(models.Model): 'amount_total': account_move.amount_total, 'amount_residual': account_move.amount_residual, 'invoice_date': '', - 'efaktur_token': self.env['rest.api'].md5_salt(account_move.id, 'account.move$') if account_move.efaktur_document else '', + 'efaktur_token': self.env['rest.api'].md5_salt(account_move.id, 'account.move') if account_move.efaktur_document else '', } if isinstance(object, datetime.date): data['invoice_date'] = account_move.invoice_date.strftime('%d/%m/%Y') diff --git a/indoteknik_api/models/rest_api.py b/indoteknik_api/models/rest_api.py index 2c54769e..052800b7 100644 --- a/indoteknik_api/models/rest_api.py +++ b/indoteknik_api/models/rest_api.py @@ -14,4 +14,4 @@ class RestApi(models.TransientModel): return time def md5_salt(self, value, salt): - return hashlib.md5((salt + str(value)).encode()).hexdigest() \ No newline at end of file + return hashlib.md5((salt + '$' + str(value)).encode()).hexdigest() \ No newline at end of file -- cgit v1.2.3 From 6b5f3041727d84db4d24215062940b8f2fca6d1c Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 3 Feb 2023 14:43:58 +0700 Subject: [FIX] feature download invoice --- indoteknik_api/models/account_move.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'indoteknik_api/models') diff --git a/indoteknik_api/models/account_move.py b/indoteknik_api/models/account_move.py index f77ded16..3c8fd655 100644 --- a/indoteknik_api/models/account_move.py +++ b/indoteknik_api/models/account_move.py @@ -8,6 +8,7 @@ class AccountMove(models.Model): def api_v1_single_response(self, account_move, context=False): data = { + 'token': self.env['rest.api'].md5_salt(account_move.id, 'account.move'), 'id': account_move.id, 'name': account_move.name, 'purchase_order_name': account_move.ref or '', @@ -16,7 +17,7 @@ class AccountMove(models.Model): 'amount_total': account_move.amount_total, 'amount_residual': account_move.amount_residual, 'invoice_date': '', - 'efaktur_token': self.env['rest.api'].md5_salt(account_move.id, 'account.move') if account_move.efaktur_document else '', + 'efaktur': True if account_move.efaktur_document else False, } if isinstance(object, datetime.date): data['invoice_date'] = account_move.invoice_date.strftime('%d/%m/%Y') -- cgit v1.2.3 From 446e3be759d72b7a06b4e4671b91c6f9c8bfa903 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Mon, 6 Feb 2023 12:02:42 +0700 Subject: add api cancel SO and upload PO in SO --- indoteknik_api/models/account_move.py | 1 - indoteknik_api/models/rest_api.py | 5 ++++- indoteknik_api/models/sale_order.py | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) (limited to 'indoteknik_api/models') diff --git a/indoteknik_api/models/account_move.py b/indoteknik_api/models/account_move.py index 3c8fd655..5c31f010 100644 --- a/indoteknik_api/models/account_move.py +++ b/indoteknik_api/models/account_move.py @@ -1,6 +1,5 @@ import datetime from odoo import models -import hashlib class AccountMove(models.Model): diff --git a/indoteknik_api/models/rest_api.py b/indoteknik_api/models/rest_api.py index 052800b7..0a15aad1 100644 --- a/indoteknik_api/models/rest_api.py +++ b/indoteknik_api/models/rest_api.py @@ -14,4 +14,7 @@ class RestApi(models.TransientModel): return time def md5_salt(self, value, salt): - return hashlib.md5((salt + '$' + str(value)).encode()).hexdigest() \ No newline at end of file + return hashlib.md5((salt + '$' + str(value)).encode()).hexdigest() + + def md5_salt_valid(self, value, salt, token): + return hashlib.md5((salt + '$' + str(value)).encode()).hexdigest() == token \ No newline at end of file diff --git a/indoteknik_api/models/sale_order.py b/indoteknik_api/models/sale_order.py index c7d488be..cc2f9586 100644 --- a/indoteknik_api/models/sale_order.py +++ b/indoteknik_api/models/sale_order.py @@ -32,6 +32,7 @@ class SaleOrder(models.Model): if context == 'with_detail': res_users = self.env['res.users'] data_with_detail = { + 'purchase_order_file': True if sale_order.partner_purchase_order_file else False, 'payment_term': sale_order.payment_term_id.name or '', 'date_order': self.env['rest.api'].datetime_to_str(sale_order.date_order, '%d/%m/%Y %H:%M:%S'), 'products': [], -- cgit v1.2.3 From ef2f9fefe4df844f5a676d2a166dcd4dfdaa249b Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Mon, 6 Feb 2023 16:14:43 +0700 Subject: fix feature --- indoteknik_api/models/rest_api.py | 29 ++++++++++++++++++++++++++++- indoteknik_api/models/sale_order.py | 3 ++- 2 files changed, 30 insertions(+), 2 deletions(-) (limited to 'indoteknik_api/models') diff --git a/indoteknik_api/models/rest_api.py b/indoteknik_api/models/rest_api.py index 0a15aad1..65119b52 100644 --- a/indoteknik_api/models/rest_api.py +++ b/indoteknik_api/models/rest_api.py @@ -1,7 +1,9 @@ from odoo import models +from odoo.http import request import datetime from pytz import timezone import hashlib +import base64 class RestApi(models.TransientModel): @@ -17,4 +19,29 @@ class RestApi(models.TransientModel): return hashlib.md5((salt + '$' + str(value)).encode()).hexdigest() def md5_salt_valid(self, value, salt, token): - return hashlib.md5((salt + '$' + str(value)).encode()).hexdigest() == token \ No newline at end of file + return hashlib.md5((salt + '$' + str(value)).encode()).hexdigest() == token + + def get_single_attachment(self, model, field, id): + domain = [ + ('res_model', '=', model), + ('res_field', '=', field), + ('res_id', '=', id), + ] + fields = ['datas', 'mimetype'] + result = self.env['ir.attachment'].sudo().search_read(domain, fields) + return result[0] if len(result) > 0 else None + + def response_attachment(self, data = {}): + decode_content = data.get('decode_content', False) + if decode_content: + data['content'] = base64.b64decode(data['content']) + + return request.make_response( + data['content'], + [ + ('Content-Type', data['mimetype']), + ('Content-Disposition', 'attachment; filename=%s' % data['filename']), + ('Content-Length', len(data['content'])) + ] + ) + \ No newline at end of file diff --git a/indoteknik_api/models/sale_order.py b/indoteknik_api/models/sale_order.py index cc2f9586..c3f3dccb 100644 --- a/indoteknik_api/models/sale_order.py +++ b/indoteknik_api/models/sale_order.py @@ -6,11 +6,13 @@ class SaleOrder(models.Model): def api_v1_single_response(self, sale_order, context=False): data = { + 'token': self.env['rest.api'].md5_salt(sale_order.id, 'sale.order'), 'id': sale_order.id, 'name': sale_order.name, 'sales': sale_order.user_id.name, 'amount_total': sale_order.amount_total, 'purchase_order_name': sale_order.partner_purchase_order_name, + 'purchase_order_file': True if sale_order.partner_purchase_order_file else False, 'invoice_count': sale_order.invoice_count, 'status': 'draft', } @@ -32,7 +34,6 @@ class SaleOrder(models.Model): if context == 'with_detail': res_users = self.env['res.users'] data_with_detail = { - 'purchase_order_file': True if sale_order.partner_purchase_order_file else False, 'payment_term': sale_order.payment_term_id.name or '', 'date_order': self.env['rest.api'].datetime_to_str(sale_order.date_order, '%d/%m/%Y %H:%M:%S'), 'products': [], -- cgit v1.2.3