Skip to content

Commit

Permalink
[FIX] sale_financial_risk_pos_compatibility: Code refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
geomer198 authored and ivs-cetmix committed Dec 2, 2023
1 parent 59a9693 commit 94b6ba3
Showing 1 changed file with 45 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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"),
Expand All @@ -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);

0 comments on commit 94b6ba3

Please sign in to comment.