refactor: set actual and projected qty in Packed Item and Sales Invoice Item

This commit is contained in:
barredterra
2024-02-16 23:50:32 +01:00
parent f99f7fd2cf
commit 75230ece9a
3 changed files with 23 additions and 17 deletions

View File

@@ -1025,23 +1025,11 @@ class SalesInvoice(SellingController):
frappe.throw(_("Could not update stock, invoice contains drop shipping item."))
def update_current_stock(self):
for d in self.get("items"):
if d.item_code and d.warehouse:
bin = frappe.db.sql(
"select actual_qty from `tabBin` where item_code = %s and warehouse = %s",
(d.item_code, d.warehouse),
as_dict=1,
)
d.actual_qty = bin and flt(bin[0]["actual_qty"]) or 0
for item in self.items:
item.set_actual_qty()
for d in self.get("packed_items"):
bin = frappe.db.sql(
"select actual_qty, projected_qty from `tabBin` where item_code = %s and warehouse = %s",
(d.item_code, d.warehouse),
as_dict=1,
)
d.actual_qty = bin and flt(bin[0]["actual_qty"]) or 0
d.projected_qty = bin and flt(bin[0]["projected_qty"]) or 0
for packed_item in self.packed_items:
packed_item.set_actual_and_projected_qty()
def update_packing_list(self):
if cint(self.update_stock) == 1:

View File

@@ -102,3 +102,12 @@ class SalesInvoiceItem(Document):
frappe.bold(self.idx), frappe.bold(self.cost_center), frappe.bold(company)
)
)
def set_actual_qty(self):
if self.item_code and self.warehouse:
self.actual_qty = (
frappe.db.get_value(
"Bin", {"item_code": self.item_code, "warehouse": self.warehouse}, "actual_qty"
)
or 0
)

View File

@@ -51,7 +51,16 @@ class PackedItem(Document):
warehouse: DF.Link | None
# end: auto-generated types
pass
def set_actual_and_projected_qty(self):
"Set actual and projected qty based on warehouse and item_code"
_bin = frappe.db.get_value(
"Bin",
{"item_code": self.item_code, "warehouse": self.warehouse},
["actual_qty", "projected_qty"],
as_dict=True,
)
self.actual_qty = _bin.actual_qty if _bin else 0
self.projected_qty = _bin.projected_qty if _bin else 0
def make_packing_list(doc):