Skip to content

Commit

Permalink
fix: incorrect warehouse and selling amount on bundled products (#33549)
Browse files Browse the repository at this point in the history
fix: incorrect warehouse and selling amount on bundled products (#33549)

(cherry picked from commit bbe5e5d)

Co-authored-by: ruthra kumar <ruthra@erpnext.com>
  • Loading branch information
mergify[bot] and ruthra-kumar authored Jan 9, 2023
1 parent fb59c5b commit c6c3ac3
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions erpnext/accounts/report/gross_profit/gross_profit.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,18 @@ def process(self):
row.delivery_note, frappe._dict()
)
row.item_row = row.dn_detail
# Update warehouse and base_amount from 'Packed Item' List
if product_bundles and not row.parent:
# For Packed Items, row.parent_invoice will be the Bundle name
product_bundle = product_bundles.get(row.parent_invoice)
if product_bundle:
for packed_item in product_bundle:
if (
packed_item.get("item_code") == row.item_code
and packed_item.get("parent_detail_docname") == row.item_row
):
row.warehouse = packed_item.warehouse
row.base_amount = packed_item.base_amount

# get buying amount
if row.item_code in product_bundles:
Expand Down Expand Up @@ -589,7 +601,9 @@ def get_buying_amount_from_product_bundle(self, row, product_bundle):
buying_amount = 0.0
for packed_item in product_bundle:
if packed_item.get("parent_detail_docname") == row.item_row:
buying_amount += self.get_buying_amount(row, packed_item.item_code)
packed_item_row = row.copy()
packed_item_row.warehouse = packed_item.warehouse
buying_amount += self.get_buying_amount(packed_item_row, packed_item.item_code)

return flt(buying_amount, self.currency_precision)

Expand Down Expand Up @@ -922,12 +936,25 @@ def load_stock_ledger_entries(self):
def load_product_bundle(self):
self.product_bundles = {}

for d in frappe.db.sql(
"""select parenttype, parent, parent_item,
item_code, warehouse, -1*qty as total_qty, parent_detail_docname
from `tabPacked Item` where docstatus=1""",
as_dict=True,
):
pki = qb.DocType("Packed Item")

pki_query = (
frappe.qb.from_(pki)
.select(
pki.parenttype,
pki.parent,
pki.parent_item,
pki.item_code,
pki.warehouse,
(-1 * pki.qty).as_("total_qty"),
pki.rate,
(pki.rate * pki.qty).as_("base_amount"),
pki.parent_detail_docname,
)
.where(pki.docstatus == 1)
)

for d in pki_query.run(as_dict=True):
self.product_bundles.setdefault(d.parenttype, frappe._dict()).setdefault(
d.parent, frappe._dict()
).setdefault(d.parent_item, []).append(d)
Expand Down

0 comments on commit c6c3ac3

Please sign in to comment.