From 463101a734c8b64cfca4b7cbff5d060295e841ef Mon Sep 17 00:00:00 2001 From: Miqdad Date: Mon, 18 Aug 2025 14:08:33 +0700 Subject: accept context from quotation website --- indoteknik_api/controllers/api_v1/sale_order.py | 68 ++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 2 deletions(-) (limited to 'indoteknik_api') diff --git a/indoteknik_api/controllers/api_v1/sale_order.py b/indoteknik_api/controllers/api_v1/sale_order.py index d199cd84..374b49a2 100644 --- a/indoteknik_api/controllers/api_v1/sale_order.py +++ b/indoteknik_api/controllers/api_v1/sale_order.py @@ -544,13 +544,76 @@ class SaleOrder(controller.Controller): main_partner = partner_invoice.get_main_parent() _logger.info( f"Partner Info - Sales: {sales_partner.id}, Invoice: {partner_invoice.id}, Main: {main_partner.id}") - + + def _get_request_context(params, kw): + # 1) kw (querystring di route) + ctx = kw.get('context') + if ctx: + return str(ctx).strip().lower() + + # 2) querystring langsung + ctx = request.httprequest.args.get('context') + if ctx: + return str(ctx).strip().lower() + + # 3) form-encoded body + ctx = request.httprequest.form.get('context') + if ctx: + return str(ctx).strip().lower() + + # 4) JSON body (kalau ada) + try: + js = request.httprequest.get_json(force=False, silent=True) or {} + ctx = js.get('context') + if ctx: + return str(ctx).strip().lower() + except Exception: + pass + + # 5) fallback: dari hasil get_request_params kalau kamu tambahkan 'context' di mapping + try: + return str((params.get('value', {}) or {}).get('context') or '').strip().lower() + except Exception: + return '' + + ctx = _get_request_context(params, kw) + _logger.info(f"[checkout] context sources -> kw={repr(kw.get('context'))}, " + f"args={repr(request.httprequest.args.get('context'))}, " + f"form={repr(request.httprequest.form.get('context'))}, " + f"json={(request.httprequest.get_json(force=False, silent=True) or {}).get('context') if hasattr(request.httprequest,'get_json') else None}, " + f"normalized={ctx}") + + payment_term_id_value = 26 + + ctx = str((kw.get('context') or '')) + if ctx == 'quotation': + try: + creator_user_id = params['value'].get('user_id') or request.env.user.id + user = request.env['res.users'].sudo().browse(int(creator_user_id)) + + term_name = None + if user and user.exists() and user.partner_id: + prop = getattr(user.partner_id, 'property_payment_term_id', False) + # Kalau Many2one -> pakai .name, kalau string/selection -> cast ke str + if prop: + term_name = getattr(prop, 'name', False) or str(prop).strip() + + if term_name: + term = request.env['account.payment.term'].sudo().search( + [('name', 'ilike', term_name)], + limit=1 + ) + if term: + payment_term_id_value = term.id + except Exception as e: + _logger.warning(f"Gagal resolve payment term dari user: {e}") + parameters = { 'warehouse_id': 8, 'carrier_id': 1, 'sales_tax_id': 23, 'pricelist_id': user_pricelist or product_pricelist_default_discount_id, - 'payment_term_id': 26, + 'payment_term_id':payment_term_id_value, 'team_id': 2, 'company_id': 1, 'currency_id': 12, @@ -633,6 +696,7 @@ class SaleOrder(controller.Controller): _logger.info(f"Final is_has_disc: {is_has_disc}") + order_line = request.env['sale.order.line'].create({ 'company_id': 1, 'order_id': sale_order.id, -- cgit v1.2.3