fix(asset): handle same asset being sold in multiple line items in sales invoice

(cherry picked from commit 23b094f151)
This commit is contained in:
Navin-S-R
2025-12-30 14:47:28 +05:30
committed by Mergify
parent 8ba42b2efb
commit ddd367c6de
2 changed files with 39 additions and 17 deletions

View File

@@ -1205,6 +1205,13 @@ class SalesInvoice(SellingController):
def split_asset_based_on_sale_qty(self):
asset_qty_map = self.get_asset_qty()
for asset, qty in asset_qty_map.items():
if qty["actual_qty"] < qty["sale_qty"]:
frappe.throw(
_(
"Sell quantity cannot exceed the asset quantity. Asset {0} has only {1} item(s)."
).format(asset, qty["actual_qty"])
)
remaining_qty = qty["actual_qty"] - qty["sale_qty"]
if remaining_qty > 0:
split_asset(asset, remaining_qty)
@@ -1227,13 +1234,16 @@ class SalesInvoice(SellingController):
for row in self.items:
if row.is_fixed_asset and row.asset:
actual_qty = asset_actual_qty.get(row.asset)
asset_qty_map.setdefault(
row.asset,
{
"sale_qty": flt(row.qty),
"actual_qty": flt(actual_qty),
},
)
if row.asset in asset_qty_map.keys():
asset_qty_map[row.asset]["sale_qty"] += flt(row.qty)
else:
asset_qty_map.setdefault(
row.asset,
{
"sale_qty": flt(row.qty),
"actual_qty": flt(actual_qty),
},
)
return asset_qty_map

View File

@@ -591,7 +591,7 @@ frappe.ui.form.on("Asset", {
});
};
const dialog = new frappe.ui.Dialog({
let dialog = new frappe.ui.Dialog({
title: __("Sell Asset"),
fields: [
{
@@ -606,18 +606,30 @@ frappe.ui.form.on("Asset", {
dialog.set_primary_action(__("Sell"), function () {
const dialog_data = dialog.get_values();
const sell_qty = cint(dialog_data.sell_qty);
const asset_qty = cint(frm.doc.asset_quantity);
if (sell_qty < cint(frm.doc.asset_quantity)) {
frappe.confirm(
__(
"The sell quantity is less than the total asset quantity. The remaining quantity will be split into a new asset. This action cannot be undone. <br><b>Do you want to continue?<b>"
),
() => make_sales_invoice(sell_qty)
);
} else {
make_sales_invoice(sell_qty);
if (sell_qty <= 0) {
frappe.throw(__("Sell quantity must be greater than zero"));
}
if (sell_qty > asset_qty) {
frappe.throw(__("Sell quantity cannot exceed the asset quantity"));
}
if (sell_qty < asset_qty) {
frappe.confirm(
__(
"The sell quantity is less than the total asset quantity. The remaining quantity will be split into a new asset. This action cannot be undone. <br><br><b>Do you want to continue?</b>"
),
() => {
make_sales_invoice(sell_qty);
dialog.hide();
}
);
return;
}
make_sales_invoice(sell_qty);
dialog.hide();
});