Skip to content

Commit

Permalink
fix: billed qty and received amount in PO analysis report (frappe#44349)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitwaghchaure authored Nov 26, 2024
1 parent 6c5f57b commit 2ab7ec5
Showing 1 changed file with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def execute(filters=None):

columns = get_columns(filters)
data = get_data(filters)
update_received_amount(data)

if not data:
return [], [], None, []
Expand All @@ -40,16 +41,13 @@ def get_data(filters):
po = frappe.qb.DocType("Purchase Order")
po_item = frappe.qb.DocType("Purchase Order Item")
pi_item = frappe.qb.DocType("Purchase Invoice Item")
pr_item = frappe.qb.DocType("Purchase Receipt Item")

query = (
frappe.qb.from_(po)
.inner_join(po_item)
.on(po_item.parent == po.name)
.left_join(pi_item)
.on((pi_item.po_detail == po_item.name) & (pi_item.docstatus == 1))
.left_join(pr_item)
.on((pr_item.purchase_order_item == po_item.name) & (pr_item.docstatus == 1))
.select(
po.transaction_date.as_("date"),
po_item.schedule_date.as_("required_date"),
Expand All @@ -63,7 +61,6 @@ def get_data(filters):
(po_item.qty - po_item.received_qty).as_("pending_qty"),
Sum(IfNull(pi_item.qty, 0)).as_("billed_qty"),
po_item.base_amount.as_("amount"),
(pr_item.base_amount).as_("received_qty_amount"),
(po_item.billed_amt * IfNull(po.conversion_rate, 1)).as_("billed_amount"),
(po_item.base_amount - (po_item.billed_amt * IfNull(po.conversion_rate, 1))).as_(
"pending_amount"
Expand Down Expand Up @@ -95,6 +92,39 @@ def get_data(filters):
return data


def update_received_amount(data):
pr_data = get_received_amount_data(data)

for row in data:
row.received_qty_amount = flt(pr_data.get(row.name))


def get_received_amount_data(data):
pr = frappe.qb.DocType("Purchase Receipt")
pr_item = frappe.qb.DocType("Purchase Receipt Item")

query = (
frappe.qb.from_(pr)
.inner_join(pr_item)
.on(pr_item.parent == pr.name)
.select(
pr_item.purchase_order_item,
Sum(pr_item.base_amount).as_("received_qty_amount"),
)
.where((pr_item.parent == pr.name) & (pr.docstatus == 1))
.groupby(pr_item.purchase_order_item)
)

query = query.where(pr_item.purchase_order_item.isin([row.name for row in data]))

data = query.run()

if not data:
return frappe._dict()

return frappe._dict(data)


def prepare_data(data, filters):
completed, pending = 0, 0
pending_field = "pending_amount"
Expand Down

0 comments on commit 2ab7ec5

Please sign in to comment.