Skip to content

Commit 627a50e

Browse files
authored
Merge pull request #44384 from frappe/mergify/bp/version-14-hotfix/pr-44323
fix: update gross profit for returned invoices (backport #44323)
2 parents 0dfb9dd + 7eafe7f commit 627a50e

File tree

2 files changed

+41
-37
lines changed

2 files changed

+41
-37
lines changed

erpnext/accounts/report/gross_profit/gross_profit.py

+37-33
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ def process(self):
421421

422422
if grouped_by_invoice:
423423
buying_amount = 0
424+
base_amount = 0
424425

425426
for row in reversed(self.si_list):
426427
if self.filters.get("group_by") == "Monthly":
@@ -461,12 +462,11 @@ def process(self):
461462
else:
462463
row.buying_amount = flt(self.get_buying_amount(row, row.item_code), self.currency_precision)
463464

464-
if grouped_by_invoice:
465-
if row.indent == 1.0:
466-
buying_amount += row.buying_amount
467-
elif row.indent == 0.0:
468-
row.buying_amount = buying_amount
469-
buying_amount = 0
465+
if grouped_by_invoice and row.indent == 0.0:
466+
row.buying_amount = buying_amount
467+
row.base_amount = base_amount
468+
buying_amount = 0
469+
base_amount = 0
470470

471471
# get buying rate
472472
if flt(row.qty):
@@ -476,11 +476,19 @@ def process(self):
476476
if self.is_not_invoice_row(row):
477477
row.buying_rate, row.base_rate = 0.0, 0.0
478478

479+
if self.is_not_invoice_row(row):
480+
self.update_return_invoices(row)
481+
482+
if grouped_by_invoice and row.indent == 1.0:
483+
buying_amount += row.buying_amount
484+
base_amount += row.base_amount
485+
479486
# calculate gross profit
480487
row.gross_profit = flt(row.base_amount - row.buying_amount, self.currency_precision)
481488
if row.base_amount:
482489
row.gross_profit_percent = flt(
483-
(row.gross_profit / row.base_amount) * 100.0, self.currency_precision
490+
(row.gross_profit / row.base_amount) * 100.0,
491+
self.currency_precision,
484492
)
485493
else:
486494
row.gross_profit_percent = 0.0
@@ -491,33 +499,29 @@ def process(self):
491499
if self.grouped:
492500
self.get_average_rate_based_on_group_by()
493501

502+
def update_return_invoices(self, row):
503+
if row.parent in self.returned_invoices and row.item_code in self.returned_invoices[row.parent]:
504+
returned_item_rows = self.returned_invoices[row.parent][row.item_code]
505+
for returned_item_row in returned_item_rows:
506+
# returned_items 'qty' should be stateful
507+
if returned_item_row.qty != 0:
508+
if row.qty >= abs(returned_item_row.qty):
509+
row.qty += returned_item_row.qty
510+
row.base_amount += flt(returned_item_row.base_amount, self.currency_precision)
511+
returned_item_row.qty = 0
512+
returned_item_row.base_amount = 0
513+
514+
else:
515+
row.qty = 0
516+
row.base_amount = 0
517+
returned_item_row.qty += row.qty
518+
returned_item_row.base_amount += row.base_amount
519+
520+
row.buying_amount = flt(flt(row.qty) * flt(row.buying_rate), self.currency_precision)
521+
494522
def get_average_rate_based_on_group_by(self):
495523
for key in list(self.grouped):
496-
if self.filters.get("group_by") == "Invoice":
497-
for row in self.grouped[key]:
498-
if row.indent == 1.0:
499-
if (
500-
row.parent in self.returned_invoices
501-
and row.item_code in self.returned_invoices[row.parent]
502-
):
503-
returned_item_rows = self.returned_invoices[row.parent][row.item_code]
504-
for returned_item_row in returned_item_rows:
505-
# returned_items 'qty' should be stateful
506-
if returned_item_row.qty != 0:
507-
if row.qty >= abs(returned_item_row.qty):
508-
row.qty += returned_item_row.qty
509-
returned_item_row.qty = 0
510-
else:
511-
row.qty = 0
512-
returned_item_row.qty += row.qty
513-
row.base_amount += flt(returned_item_row.base_amount, self.currency_precision)
514-
row.buying_amount = flt(
515-
flt(row.qty) * flt(row.buying_rate), self.currency_precision
516-
)
517-
if flt(row.qty) or row.base_amount:
518-
row = self.set_average_rate(row)
519-
self.grouped_data.append(row)
520-
elif self.filters.get("group_by") == "Payment Term":
524+
if self.filters.get("group_by") == "Payment Term":
521525
for i, row in enumerate(self.grouped[key]):
522526
invoice_portion = 0
523527

@@ -537,7 +541,7 @@ def get_average_rate_based_on_group_by(self):
537541

538542
new_row = self.set_average_rate(new_row)
539543
self.grouped_data.append(new_row)
540-
else:
544+
elif self.filters.get("group_by") != "Invoice":
541545
for i, row in enumerate(self.grouped[key]):
542546
if i == 0:
543547
new_row = row

erpnext/accounts/report/gross_profit/test_gross_profit.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -418,12 +418,12 @@ def test_crnote_against_invoice_with_multiple_instances_of_same_item(self):
418418
"item_name": self.item,
419419
"warehouse": "Stores - _GP",
420420
"qty": 0.0,
421-
"avg._selling_rate": 0.0,
421+
"avg._selling_rate": 100,
422422
"valuation_rate": 0.0,
423-
"selling_amount": -100.0,
423+
"selling_amount": 0.0,
424424
"buying_amount": 0.0,
425-
"gross_profit": -100.0,
426-
"gross_profit_%": 100.0,
425+
"gross_profit": 0.0,
426+
"gross_profit_%": 0.0,
427427
}
428428
gp_entry = [x for x in data if x.parent_invoice == sinv.name]
429429
# Both items of Invoice should have '0' qty

0 commit comments

Comments
 (0)