Skip to content

Commit 23b094f

Browse files
committed
fix(asset): handle same asset being sold in multiple line items in sales invoice
1 parent e7e6567 commit 23b094f

2 files changed

Lines changed: 35 additions & 13 deletions

File tree

erpnext/accounts/doctype/sales_invoice/sales_invoice.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,13 @@ def check_prev_docstatus(self):
13641364
def split_asset_based_on_sale_qty(self):
13651365
asset_qty_map = self.get_asset_qty()
13661366
for asset, qty in asset_qty_map.items():
1367+
if qty["actual_qty"] < qty["sale_qty"]:
1368+
frappe.throw(
1369+
_(
1370+
"Sell quantity cannot exceed the asset quantity. Asset {0} has only {1} item(s)."
1371+
).format(asset, qty["actual_qty"])
1372+
)
1373+
13671374
remaining_qty = qty["actual_qty"] - qty["sale_qty"]
13681375
if remaining_qty > 0:
13691376
split_asset(asset, remaining_qty)
@@ -1386,13 +1393,16 @@ def get_asset_qty(self):
13861393
for row in self.items:
13871394
if row.is_fixed_asset and row.asset:
13881395
actual_qty = asset_actual_qty.get(row.asset)
1389-
asset_qty_map.setdefault(
1390-
row.asset,
1391-
{
1392-
"sale_qty": flt(row.qty),
1393-
"actual_qty": flt(actual_qty),
1394-
},
1395-
)
1396+
if row.asset in asset_qty_map.keys():
1397+
asset_qty_map[row.asset]["sale_qty"] += flt(row.qty)
1398+
else:
1399+
asset_qty_map.setdefault(
1400+
row.asset,
1401+
{
1402+
"sale_qty": flt(row.qty),
1403+
"actual_qty": flt(actual_qty),
1404+
},
1405+
)
13961406

13971407
return asset_qty_map
13981408

erpnext/assets/doctype/asset/asset.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ frappe.ui.form.on("Asset", {
550550
});
551551
};
552552

553-
const dialog = new frappe.ui.Dialog({
553+
let dialog = new frappe.ui.Dialog({
554554
title: __("Sell Asset"),
555555
fields: [
556556
{
@@ -565,18 +565,30 @@ frappe.ui.form.on("Asset", {
565565
dialog.set_primary_action(__("Sell"), function () {
566566
const dialog_data = dialog.get_values();
567567
const sell_qty = cint(dialog_data.sell_qty);
568+
const asset_qty = cint(frm.doc.asset_quantity);
569+
570+
if (sell_qty <= 0) {
571+
frappe.throw(__("Sell quantity must be greater than zero"));
572+
}
568573

569-
if (sell_qty < cint(frm.doc.asset_quantity)) {
574+
if (sell_qty > asset_qty) {
575+
frappe.throw(__("Sell quantity cannot exceed the asset quantity"));
576+
}
577+
578+
if (sell_qty < asset_qty) {
570579
frappe.confirm(
571580
__(
572-
"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>"
581+
"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>"
573582
),
574-
() => make_sales_invoice(sell_qty)
583+
() => {
584+
make_sales_invoice(sell_qty);
585+
dialog.hide();
586+
}
575587
);
576-
} else {
577-
make_sales_invoice(sell_qty);
588+
return;
578589
}
579590

591+
make_sales_invoice(sell_qty);
580592
dialog.hide();
581593
});
582594

0 commit comments

Comments
 (0)