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);
|
||||
|
||||
// If there is a down payment AND standard products that haven't been zeroed out yet
|
||||
// we prompt the user to create a quotation.
|
||||
const needsQuotation = downPaymentLines.length > 0 && standardLines.some(line => line.price > 0 || line.get_unit_price() > 0);
|
||||
// If there is a down payment, we require a customer and automatically
|
||||
// create a quotation without prompting.
|
||||
const needsQuotation = downPaymentLines.length > 0;
|
||||
|
||||
if (needsQuotation && !this.is_quotation_line_converted) {
|
||||
if (!this.get_partner()) {
|
||||
@ -44,64 +44,53 @@ patch(Order.prototype, {
|
||||
return;
|
||||
}
|
||||
|
||||
const { confirmed } = await this.env.services.popup.add(ConfirmPopup, {
|
||||
title: _t("Create Quotation?"),
|
||||
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?"),
|
||||
confirmText: _t("Yes"),
|
||||
cancelText: _t("No")
|
||||
});
|
||||
try {
|
||||
// Prepare data for backend
|
||||
const linesData = standardLines.map(line => ({
|
||||
product_id: line.product.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 || [],
|
||||
}));
|
||||
|
||||
if (confirmed) {
|
||||
try {
|
||||
// Prepare data for backend
|
||||
const linesData = standardLines.map(line => ({
|
||||
product_id: line.product.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)
|
||||
const saleOrderResult = await this.env.services.orm.call(
|
||||
"pos.order",
|
||||
"create_quotation_from_pos_lines",
|
||||
[this.get_partner().id, linesData, this.pos.config.id]
|
||||
);
|
||||
|
||||
// Call backend to create sale.order (removed the first empty array for @api.model)
|
||||
const saleOrderResult = await this.env.services.orm.call(
|
||||
"pos.order",
|
||||
"create_quotation_from_pos_lines",
|
||||
[this.get_partner().id, linesData, this.pos.config.id]
|
||||
);
|
||||
|
||||
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;
|
||||
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,
|
||||
};
|
||||
}
|
||||
} 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, {
|
||||
title: _t("Error"),
|
||||
body: _t("An error occurred while creating the quotation: ") + error.message,
|
||||
body: _t("Failed to create the quotation."),
|
||||
});
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
return; // User cancelled
|
||||
} catch (error) {
|
||||
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