feat: automatically create quotation for down payment orders and remove user confirmation prompt.
This commit is contained in:
parent
f938be25f2
commit
db67645436
@ -31,9 +31,9 @@ patch(Order.prototype, {
|
|||||||
|
|
||||||
const standardLines = lines.filter(line => line.product.id !== downPaymentProductId);
|
const standardLines = lines.filter(line => line.product.id !== downPaymentProductId);
|
||||||
|
|
||||||
// If there is a down payment AND standard products that haven't been zeroed out yet
|
// If there is a down payment, we require a customer and automatically
|
||||||
// we prompt the user to create a quotation.
|
// create a quotation without prompting.
|
||||||
const needsQuotation = downPaymentLines.length > 0 && standardLines.some(line => line.price > 0 || line.get_unit_price() > 0);
|
const needsQuotation = downPaymentLines.length > 0;
|
||||||
|
|
||||||
if (needsQuotation && !this.is_quotation_line_converted) {
|
if (needsQuotation && !this.is_quotation_line_converted) {
|
||||||
if (!this.get_partner()) {
|
if (!this.get_partner()) {
|
||||||
@ -44,64 +44,53 @@ patch(Order.prototype, {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { confirmed } = await this.env.services.popup.add(ConfirmPopup, {
|
try {
|
||||||
title: _t("Create Quotation?"),
|
// Prepare data for backend
|
||||||
body: _t("This cart contains a down payment and standard products. Do you want to create a backend Quotation for the standard products and proceed to pay only the down payment?"),
|
const linesData = standardLines.map(line => ({
|
||||||
confirmText: _t("Yes"),
|
product_id: line.product.id,
|
||||||
cancelText: _t("No")
|
qty: line.get_quantity(),
|
||||||
});
|
price_unit: line.get_unit_price(),
|
||||||
|
discount: line.get_discount(),
|
||||||
|
tax_ids: line.tax_ids || line.get_product().taxes_id || [],
|
||||||
|
}));
|
||||||
|
|
||||||
if (confirmed) {
|
// Call backend to create sale.order (removed the first empty array for @api.model)
|
||||||
try {
|
const saleOrderResult = await this.env.services.orm.call(
|
||||||
// Prepare data for backend
|
"pos.order",
|
||||||
const linesData = standardLines.map(line => ({
|
"create_quotation_from_pos_lines",
|
||||||
product_id: line.product.id,
|
[this.get_partner().id, linesData, this.pos.config.id]
|
||||||
qty: line.get_quantity(),
|
);
|
||||||
price_unit: line.get_unit_price(),
|
|
||||||
discount: line.get_discount(),
|
|
||||||
tax_ids: line.tax_ids || line.get_product().taxes_id || [],
|
|
||||||
}));
|
|
||||||
|
|
||||||
// Call backend to create sale.order (removed the first empty array for @api.model)
|
if (saleOrderResult) {
|
||||||
const saleOrderResult = await this.env.services.orm.call(
|
// Link the down payment line to the newly created sale order
|
||||||
"pos.order",
|
const dpLine = lines.find(line => line.product.id === downPaymentProductId);
|
||||||
"create_quotation_from_pos_lines",
|
if (dpLine) {
|
||||||
[this.get_partner().id, linesData, this.pos.config.id]
|
dpLine.sale_order_origin_id = {
|
||||||
);
|
id: saleOrderResult.id,
|
||||||
|
name: saleOrderResult.name,
|
||||||
if (saleOrderResult) {
|
};
|
||||||
// Link the down payment line to the newly created sale order
|
|
||||||
const dpLine = lines.find(line => line.product.id === downPaymentProductId);
|
|
||||||
if (dpLine) {
|
|
||||||
dpLine.sale_order_origin_id = {
|
|
||||||
id: saleOrderResult.id,
|
|
||||||
name: saleOrderResult.name,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Zero out the standard lines to not charge the customer twice
|
|
||||||
for (const line of standardLines) {
|
|
||||||
line.set_unit_price(0);
|
|
||||||
// Set a flag to easily skip stock delivery later if needed
|
|
||||||
line.is_quotation_line = true;
|
|
||||||
}
|
|
||||||
this.is_quotation_line_converted = true;
|
|
||||||
} else {
|
|
||||||
await this.env.services.popup.add(ErrorPopup, {
|
|
||||||
title: _t("Error"),
|
|
||||||
body: _t("Failed to create the quotation."),
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
|
||||||
|
// Zero out the standard lines to not charge the customer twice
|
||||||
|
for (const line of standardLines) {
|
||||||
|
line.set_unit_price(0);
|
||||||
|
// Set a flag to easily skip stock delivery later if needed
|
||||||
|
line.is_quotation_line = true;
|
||||||
|
}
|
||||||
|
this.is_quotation_line_converted = true;
|
||||||
|
} else {
|
||||||
await this.env.services.popup.add(ErrorPopup, {
|
await this.env.services.popup.add(ErrorPopup, {
|
||||||
title: _t("Error"),
|
title: _t("Error"),
|
||||||
body: _t("An error occurred while creating the quotation: ") + error.message,
|
body: _t("Failed to create the quotation."),
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} catch (error) {
|
||||||
return; // User cancelled
|
await this.env.services.popup.add(ErrorPopup, {
|
||||||
|
title: _t("Error"),
|
||||||
|
body: _t("An error occurred while creating the quotation: ") + error.message,
|
||||||
|
});
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user