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