summaryrefslogtreecommitdiff
path: root/indoteknik_custom/controllers
diff options
context:
space:
mode:
authorIT Fixcomart <it@fixcomart.co.id>2022-09-24 13:13:23 +0700
committerIT Fixcomart <it@fixcomart.co.id>2022-09-24 13:13:23 +0700
commitd057901e1b30dc24978ec345e4daec1d16abf117 (patch)
treeb049b832447031bb198f3dcb35f4420028568f37 /indoteknik_custom/controllers
parent66f43a6152ccd63a1d2e2e1f80b0c3825bf39e7f (diff)
Rest API Odoo
Diffstat (limited to 'indoteknik_custom/controllers')
-rw-r--r--indoteknik_custom/controllers/__init__.py2
-rw-r--r--indoteknik_custom/controllers/api/__init__.py2
-rw-r--r--indoteknik_custom/controllers/api/product.py27
-rw-r--r--indoteknik_custom/controllers/api/sale_order.py119
-rw-r--r--indoteknik_custom/controllers/api_controller.py32
5 files changed, 182 insertions, 0 deletions
diff --git a/indoteknik_custom/controllers/__init__.py b/indoteknik_custom/controllers/__init__.py
new file mode 100644
index 00000000..2fd318df
--- /dev/null
+++ b/indoteknik_custom/controllers/__init__.py
@@ -0,0 +1,2 @@
+from . import api_controller
+from . import api
diff --git a/indoteknik_custom/controllers/api/__init__.py b/indoteknik_custom/controllers/api/__init__.py
new file mode 100644
index 00000000..bd901bd4
--- /dev/null
+++ b/indoteknik_custom/controllers/api/__init__.py
@@ -0,0 +1,2 @@
+from . import sale_order
+from . import product
diff --git a/indoteknik_custom/controllers/api/product.py b/indoteknik_custom/controllers/api/product.py
new file mode 100644
index 00000000..5c5a6c1a
--- /dev/null
+++ b/indoteknik_custom/controllers/api/product.py
@@ -0,0 +1,27 @@
+from .. import api_controller
+from odoo import http
+from odoo.http import request
+import json
+
+
+class ProductApi(api_controller.ApiController):
+ @http.route('/api/product/search', auth='public', methods=['GET'])
+ def search_product(self, **kw):
+ self.authenticate(kw)
+ limit = kw.get('limit', 0)
+ offset = kw.get('offset', 0)
+ order = kw.get('order', '')
+ domain = kw.get('domain', [])
+ if domain:
+ domain = json.loads(domain)
+
+ product_variants = request.env['product.product'].search(domain)
+ product_variant_ids = [v['id'] for v in product_variants]
+ domain = [('product_variant_ids', 'in', product_variant_ids)]
+ products = request.env['product.template'].search(domain, limit=int(limit), offset=int(offset), order=order)
+ response = []
+ for product in products:
+ response.append({
+ 'name': product.name
+ })
+ return json.dumps(response)
diff --git a/indoteknik_custom/controllers/api/sale_order.py b/indoteknik_custom/controllers/api/sale_order.py
new file mode 100644
index 00000000..4194bcb6
--- /dev/null
+++ b/indoteknik_custom/controllers/api/sale_order.py
@@ -0,0 +1,119 @@
+from .. import api_controller
+from odoo import http
+from odoo.http import request
+import json
+
+
+class SaleOrderApi(api_controller.ApiController):
+ @http.route('/api/sale_order/invoiced', auth='public', methods=['GET'])
+ def get_sale_order_invoiced_by_partner_id(self, partner_id=None, **kw):
+ self.authenticate(kw)
+ response = []
+ if partner_id:
+ # Get company member by partner_id
+ parent_partner_id = request.env['res.partner'].search([('id', '=', int(partner_id))], limit=1).parent_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] + [int(partner_id)]
+
+ # Get sale order by company member and invoiced
+ default_domain = [
+ ('partner_id', 'in', partner_child_ids),
+ '|',
+ ('invoice_status', '=', 'invoiced'),
+ ('invoice_status', '=', 'to_invoice')
+ ]
+ sale_orders = self.search_with_api_params('sale.order', kw, default_domain)
+ for sale_order in sale_orders:
+ pickings = []
+ for picking in sale_order.picking_ids:
+ if picking.state in ['confirmed', 'assigned', 'done']:
+ pickings.append({
+ 'id': picking.id,
+ 'name': picking.name,
+ 'delivery_address': picking.partner_id.street,
+ 'delivery_tracking_no': picking.delivery_tracking_no,
+ 'delivery_status': picking.delivery_status
+ })
+
+ response.append({
+ 'id': sale_order.id,
+ 'name': sale_order.name,
+ 'amount_total': sale_order.amount_total,
+ 'salesperson': sale_order.user_id.name,
+ 'date_order': self.time_to_str(sale_order.date_order, '%d/%m/%Y'),
+ 'pickings': pickings,
+ 'access_token': sale_order.access_token
+ })
+ return json.dumps(response)
+
+ # @http.route('/api/test', auth='public', methods=['GET'])
+ # def test(self, **kw):
+ # self.authenticate(kw)
+ # response = []
+ # products = self.search_with_api_params('product.template', kw)
+ # for product in products:
+ # response.append({
+ # 'id': product.id,
+ # 'name': product.name
+ # })
+ # return json.dumps(response)
+
+ @http.route('/api/sale_order/invoiced/detail', auth='public', methods=['GET'])
+ def get_sale_order_invoiced_detail(self, id=None, partner_id=None, **kw):
+ self.authenticate(kw)
+ response = {}
+ if id:
+ default_domain = [
+ ('id', '=', id),
+ '|',
+ ('invoice_status', '=', 'invoiced'),
+ ('invoice_status', '=', 'to_invoice')
+ ]
+ if partner_id:
+ # Get company member by partner_id
+ parent_partner_id = request.env['res.partner'].search([('id', '=', int(partner_id))], limit=1).parent_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] + [int(partner_id)]
+ default_domain.append(('partner_id', 'in', partner_child_ids))
+ sale_order = self.search_with_api_params('sale.order', kw, default_domain)
+
+ if sale_order:
+ orders = []
+ for order in sale_order.order_line:
+ orders.append({
+ 'name': order.name,
+ 'product_qty': order.product_qty,
+ 'price_unit': order.price_unit,
+ 'price_tax': order.price_tax,
+ 'price_total': order.price_total,
+ 'price_subtotal': order.price_subtotal,
+ 'tax': order.tax_id.name,
+ 'discount': order.discount,
+ })
+
+ response.update({
+ 'id': sale_order.id,
+ 'name': sale_order.name,
+ 'carrier': sale_order.carrier_id.name,
+ 'partner': {
+ 'id': sale_order.partner_id.id,
+ 'name': sale_order.partner_id.name,
+ 'mobile': sale_order.partner_id.mobile,
+ 'email': sale_order.partner_id.email
+ },
+ 'delivery_address': sale_order.partner_shipping_id.street,
+ 'delivery_method': sale_order.carrier_id.name,
+ 'payment_term': sale_order.payment_term_id.name,
+ 'salesperson': sale_order.user_id.name,
+ 'date_order': self.time_to_str(sale_order.date_order, '%d/%m/%Y %H:%M:%S'),
+ 'note': sale_order.note,
+ 'amount_untaxed': sale_order.amount_untaxed,
+ 'amount_tax': sale_order.amount_tax,
+ 'amount_total': sale_order.amount_total,
+ 'orders': orders
+ })
+ return json.dumps(response)
+
+ @http.route('/api/sale_order', auth='public', methods=['POST'], csrf=False)
+ def post_sale_order(self, **kw):
+ return json.dumps(['a'])
diff --git a/indoteknik_custom/controllers/api_controller.py b/indoteknik_custom/controllers/api_controller.py
new file mode 100644
index 00000000..cb5fae60
--- /dev/null
+++ b/indoteknik_custom/controllers/api_controller.py
@@ -0,0 +1,32 @@
+import datetime
+
+from odoo import http
+from odoo.http import request
+import json
+from pytz import timezone
+
+
+class ApiController(http.Controller):
+ def authenticate(self, kw):
+ db = kw.get('db')
+ username = kw.get('username')
+ password = kw.get('password')
+ request.session.authenticate(db, username, password)
+
+ def time_to_str(self, object, format):
+ time = ''
+ if isinstance(object, datetime.datetime):
+ time = object.astimezone(timezone('Asia/Jakarta')).strftime(format)
+ return time
+
+
+ def search_with_api_params(self, model: str, kw, default_domain=[]):
+ limit = kw.get('limit', 0)
+ offset = kw.get('offset', 0)
+ order = kw.get('order', '')
+ domain = kw.get('domain', [])
+ if domain:
+ domain = json.loads(domain)
+ domain += default_domain
+
+ return request.env[model].search(domain, limit=int(limit), offset=int(offset), order=order)