diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-01-27 11:08:02 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-01-27 11:08:02 +0700 |
| commit | 39e27d0187d352dfea7db1bc1c9aece42e348caa (patch) | |
| tree | 97aa953bbc865669f7f813e50024866e4d6b826d | |
| parent | e48e4626b94f7e09b3bab95937cf4b7a5ca07e40 (diff) | |
sale order and invoice api
| -rw-r--r-- | indoteknik_api/controllers/api_v1/invoice.py | 25 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v1/sale_order.py | 27 | ||||
| -rw-r--r-- | indoteknik_api/controllers/controller.py | 3 | ||||
| -rw-r--r-- | indoteknik_api/models/__init__.py | 1 | ||||
| -rw-r--r-- | indoteknik_api/models/account_move.py | 23 | ||||
| -rw-r--r-- | indoteknik_api/models/rest_api.py | 13 | ||||
| -rw-r--r-- | indoteknik_api/models/sale_order.py | 25 |
7 files changed, 71 insertions, 46 deletions
diff --git a/indoteknik_api/controllers/api_v1/invoice.py b/indoteknik_api/controllers/api_v1/invoice.py index 5a6e8316..59cacfc4 100644 --- a/indoteknik_api/controllers/api_v1/invoice.py +++ b/indoteknik_api/controllers/api_v1/invoice.py @@ -34,7 +34,11 @@ class Invoice(controller.Controller): ] if params['value']['name']: name = params['value']['name'].replace(' ', '%') - domain.append(('name', 'ilike', '%'+ name +'%')) + domain += [ + '|', + ('name', 'ilike', '%'+ name +'%'), + ('ref', 'ilike', '%'+ name +'%') + ] invoices = request.env['account.move'].search(domain, offset=offset, limit=limit) data = { 'invoice_total': request.env['account.move'].search_count(domain), @@ -67,23 +71,6 @@ class Invoice(controller.Controller): data = {} account_move = request.env['account.move'].search(domain) if account_move: - res_users = request.env['res.users'] - data = { - '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 = request.env['product.product'].api_single_response(line.product_id) - product['quantity'] = line.quantity - data['products'].append(product) + data = request.env['account.move'].api_v1_single_response(account_move, context='with_detail') return self.response(data) diff --git a/indoteknik_api/controllers/api_v1/sale_order.py b/indoteknik_api/controllers/api_v1/sale_order.py index 90dee56c..073f6301 100644 --- a/indoteknik_api/controllers/api_v1/sale_order.py +++ b/indoteknik_api/controllers/api_v1/sale_order.py @@ -30,7 +30,11 @@ class SaleOrder(controller.Controller): domain = [('partner_id', 'in', partner_child_ids)] if params['value']['name']: name = params['value']['name'].replace(' ', '%') - domain.append(('name', 'ilike', '%'+ name +'%')) + domain += [ + '|', + ('name', 'ilike', '%'+ name +'%'), + ('partner_purchase_order_name', 'ilike', '%'+ name +'%') + ] sale_orders = request.env['sale.order'].search(domain, offset=offset, limit=limit) data = { 'sale_order_total': request.env['sale.order'].search_count(domain), @@ -61,26 +65,7 @@ class SaleOrder(controller.Controller): data = {} sale_order = request.env['sale.order'].search(domain) if sale_order: - res_users = request.env['res.users'] - data = { - 'id': sale_order.id, - 'name': sale_order.name, - 'payment_term': sale_order.payment_term_id.name or '', - 'sales': sale_order.user_id.name or '', - 'date_order': self.time_to_str(sale_order.date_order, '%d/%m/%Y %H:%M:%S'), - 'purchase_order_name': sale_order.partner_purchase_order_name, - 'products': [], - 'amount_total': sale_order.amount_total, - '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) - } - } - for line in sale_order.order_line: - product = request.env['product.product'].api_single_response(line.product_id) - product['quantity'] = line.product_uom_qty - data['products'].append(product) + data = request.env['sale.order'].api_v1_single_response(sale_order, context='with_detail') return self.response(data) diff --git a/indoteknik_api/controllers/controller.py b/indoteknik_api/controllers/controller.py index a1a81e37..0b08a58f 100644 --- a/indoteknik_api/controllers/controller.py +++ b/indoteknik_api/controllers/controller.py @@ -7,11 +7,8 @@ from odoo import http from odoo.http import request from odoo.tools.config import config from pytz import timezone -import logging import jwt -_logger = logging.getLogger(__name__) - class Controller(http.Controller): jwt_secret_key = "NTNv7j0TuYARvmNMmWXo6fKvM4o6nvaUi9ryX38ZHL1bkrnD1ObOQ8JAUmHCBq7Iy7otZcyAagBLHVKvvYaIpmMuxmARQ97jUVG16Jkpkp1wXOPsrF9zwew6TpczyHkHgX5EuLg2MeBuiTqJACs1J0apruOOJCggOtkjB4c" 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 |
