summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-01-26 17:00:30 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-01-26 17:00:30 +0700
commit9cb38fa42adaf5943db84682ab66ec7f81ee86d8 (patch)
tree854166c57232e1c74b8a33a245c024a388fe5ea1
parent36f67ca3cd1140a6e8d4cf537a1ce58d5b5ecdc7 (diff)
parente48e4626b94f7e09b3bab95937cf4b7a5ca07e40 (diff)
Merge branch 'feature/rest-api' into staging
-rw-r--r--indoteknik_api/controllers/api_v1/__init__.py1
-rw-r--r--indoteknik_api/controllers/api_v1/invoice.py89
-rw-r--r--indoteknik_api/controllers/controller.py3
-rw-r--r--indoteknik_api/models/__init__.py1
-rw-r--r--indoteknik_api/models/account_move.py20
5 files changed, 113 insertions, 1 deletions
diff --git a/indoteknik_api/controllers/api_v1/__init__.py b/indoteknik_api/controllers/api_v1/__init__.py
index e09b8f7b..a4776503 100644
--- a/indoteknik_api/controllers/api_v1/__init__.py
+++ b/indoteknik_api/controllers/api_v1/__init__.py
@@ -5,6 +5,7 @@ from . import category
from . import city
from . import district
from . import flash_sale
+from . import invoice
from . import manufacture
from . import partner
from . import product_variant
diff --git a/indoteknik_api/controllers/api_v1/invoice.py b/indoteknik_api/controllers/api_v1/invoice.py
new file mode 100644
index 00000000..5a6e8316
--- /dev/null
+++ b/indoteknik_api/controllers/api_v1/invoice.py
@@ -0,0 +1,89 @@
+from .. import controller
+from odoo import http
+from odoo.http import request
+
+
+class Invoice(controller.Controller):
+ PREFIX = '/api/v1/'
+ PREFIX_PARTNER = PREFIX + 'partner/<partner_id>/'
+
+ @http.route(PREFIX_PARTNER + 'invoice', auth='public', method=['GET', 'OPTIONS'])
+ def get_partner_invoice(self, **kw):
+ user_token = self.authenticate()
+ if not user_token:
+ return self.unauthorized_response()
+
+ params = self.get_request_params(kw, {
+ 'partner_id': ['number'],
+ 'name': [],
+ 'limit': ['default:0', 'number'],
+ 'offset': ['default:0', 'number'],
+ })
+ limit = params['value']['limit']
+ offset = params['value']['offset']
+ if not user_token['partner_id'] == params['value']['partner_id']:
+ return self.unauthorized_response()
+ if not params['valid']:
+ return self.response(code=400, description=params)
+
+ partner_child_ids = self.get_partner_child_ids(params['value']['partner_id'])
+ domain = [
+ ('move_type', '=', 'out_invoice'),
+ ('state', '=', 'posted'),
+ ('partner_id', 'in', partner_child_ids)
+ ]
+ if params['value']['name']:
+ name = params['value']['name'].replace(' ', '%')
+ domain.append(('name', 'ilike', '%'+ name +'%'))
+ invoices = request.env['account.move'].search(domain, offset=offset, limit=limit)
+ data = {
+ 'invoice_total': request.env['account.move'].search_count(domain),
+ 'invoices': [request.env['account.move'].api_v1_single_response(x) for x in invoices]
+ }
+ return self.response(data)
+
+ @http.route(PREFIX_PARTNER + 'invoice/<id>', auth='public', method=['GET', 'OPTIONS'])
+ def get_partner_invoice_by_id(self, **kw):
+ user_token = self.authenticate()
+ if not user_token:
+ return self.unauthorized_response()
+
+ params = self.get_request_params(kw, {
+ 'partner_id': ['number'],
+ 'id': ['number']
+ })
+ if not user_token['partner_id'] == params['value']['partner_id']:
+ return self.unauthorized_response()
+ if not params['valid']:
+ return self.response(code=400, description=params)
+
+ partner_child_ids = self.get_partner_child_ids(params['value']['partner_id'])
+ domain = [
+ ('move_type', '=', 'out_invoice'),
+ ('state', '=', 'posted'),
+ ('id', '=', params['value']['id']),
+ ('partner_id', 'in', partner_child_ids)
+ ]
+ 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)
+
+ return self.response(data)
diff --git a/indoteknik_api/controllers/controller.py b/indoteknik_api/controllers/controller.py
index 4a7a8fb6..a1a81e37 100644
--- a/indoteknik_api/controllers/controller.py
+++ b/indoteknik_api/controllers/controller.py
@@ -138,7 +138,8 @@ class Controller(http.Controller):
return False
def get_partner_child_ids(self, partner_id):
- parent_partner_id = request.env['res.partner'].search([('id', '=', partner_id)], limit=1).parent_id.id
+ parent_partner_id = request.env['res.partner'].search([('id', '=', partner_id)], limit=1)
+ parent_partner_id = parent_partner_id.parent_id.id or parent_partner_id.id
partner_childs = request.env['res.partner'].search([('parent_id', '=', int(parent_partner_id))])
partner_child_ids = [v['id'] for v in partner_childs] + [partner_id]
return partner_child_ids
diff --git a/indoteknik_api/models/__init__.py b/indoteknik_api/models/__init__.py
index d484500a..9af9f36e 100644
--- a/indoteknik_api/models/__init__.py
+++ b/indoteknik_api/models/__init__.py
@@ -1,3 +1,4 @@
+from . import account_move
from . import blog_post
from . import product_pricelist
from . import product_product
diff --git a/indoteknik_api/models/account_move.py b/indoteknik_api/models/account_move.py
new file mode 100644
index 00000000..9fd6fb18
--- /dev/null
+++ b/indoteknik_api/models/account_move.py
@@ -0,0 +1,20 @@
+import datetime
+from odoo import models
+from pytz import timezone
+
+
+class AccountMove(models.Model):
+ _inherit = 'account.move'
+
+ def api_v1_single_response(self, account_move):
+ 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 ''
+ }
+ return data