From 94b6ba34fbeb546da07c574bdbec26cdb05ed0c9 Mon Sep 17 00:00:00 2001 From: geomer198 Date: Thu, 23 Nov 2023 22:40:42 +0400 Subject: [PATCH] [FIX] sale_financial_risk_pos_compatibility: Code refactoring. --- .../static/src/js/CreateOrderPopup.esm.js | 73 ++++++++++++------- 1 file changed, 45 insertions(+), 28 deletions(-) diff --git a/sale_financial_risk_pos_compatibility/static/src/js/CreateOrderPopup.esm.js b/sale_financial_risk_pos_compatibility/static/src/js/CreateOrderPopup.esm.js index 200ff42c85..20d0c565ab 100644 --- a/sale_financial_risk_pos_compatibility/static/src/js/CreateOrderPopup.esm.js +++ b/sale_financial_risk_pos_compatibility/static/src/js/CreateOrderPopup.esm.js @@ -5,16 +5,33 @@ import Registries from "point_of_sale.Registries"; export const CreateOrderPopupRisk = (CreateOrderPopup) => class CreateOrderPopupRisk extends CreateOrderPopup { - async _actionCreateSaleOrder(order_state) { - if (order_state === "draft") { - // Default behavior - return await super._actionCreateSaleOrder(order_state); + async _calculateRiskAmount() { + const order = this.env.pos.get_order(); + return order.get_total_with_tax() + order.get_rounding_applied(); + } + + _getRiskExceptionMessage(partner, riskAmount) { + if (partner.risk_exception) { + return this.env._t("Financial risk exceeded.\n"); } - this.extraContext = false; + if ( + partner.risk_sale_order_limit && + partner.risk_sale_order + riskAmount > partner.risk_sale_order_limit + ) { + return this.env._t("This sale order exceeds the sales orders risk.\n"); + } + if ( + partner.risk_sale_order_include && + partner.risk_total + riskAmount > partner.credit_limit + ) { + return this.env._t("This sale order exceeds the financial risk.\n"); + } + return ""; + } + + async _getOrderPartnerRiskValue() { const order = this.env.pos.get_order(); - const risk_amount = - order.get_total_with_tax() + order.get_rounding_applied(); - const [partner] = await this.rpc({ + const [partnerRiskValues] = await this.rpc({ model: "res.partner", method: "read", args: [ @@ -29,26 +46,11 @@ export const CreateOrderPopupRisk = (CreateOrderPopup) => ], ], }); - let exception_msg = ""; - if (partner.risk_exception) { - exception_msg = this.env._t("Financial risk exceeded.\n"); - } else if ( - partner.risk_sale_order_limit && - partner.risk_sale_order + risk_amount > partner.risk_sale_order_limit - ) { - exception_msg = this.env._t( - "This sale order exceeds the sales orders risk.\n" - ); - } else if ( - partner.risk_sale_order_include && - partner.risk_total + risk_amount > partner.credit_limit - ) { - exception_msg = this.env._t( - "This sale order exceeds the financial risk.\n" - ); - } else { - return await super._actionCreateSaleOrder(order_state); - } + return partnerRiskValues; + } + + async _handleRiskException(exception_msg, order_state) { + const order = this.env.pos.get_order(); if (this.env.pos.user.has_role_risk_manager) { const {confirmed} = await this.showPopup("ConfirmPopup", { title: this.env._t("Partner risk exceeded"), @@ -68,6 +70,21 @@ export const CreateOrderPopupRisk = (CreateOrderPopup) => } return await this.cancel(); } + + async _actionCreateSaleOrder(order_state) { + if (order_state === "draft") { + // Default behavior + return await super._actionCreateSaleOrder(order_state); + } + this.extraContext = false; + const riskAmount = this._calculateRiskAmount(); + const partner = this._getOrderPartnerRiskValue(); + const exception_msg = this._getRiskExceptionMessage(partner, riskAmount); + if (!exception_msg) { + return await super._actionCreateSaleOrder(order_state); + } + return await this._handleRiskException(exception_msg, order_state); + } }; Registries.Component.extend(CreateOrderPopup, CreateOrderPopupRisk);