Gross profit report considering returned items (#10734)
This commit is contained in:
parent
e859671eb3
commit
cf36534eed
@ -17,7 +17,6 @@ def execute(filters=None):
|
|||||||
gross_profit_data = GrossProfitGenerator(filters)
|
gross_profit_data = GrossProfitGenerator(filters)
|
||||||
|
|
||||||
data = []
|
data = []
|
||||||
source = gross_profit_data.grouped_data if filters.get("group_by") != "Invoice" else gross_profit_data.data
|
|
||||||
|
|
||||||
group_wise_columns = frappe._dict({
|
group_wise_columns = frappe._dict({
|
||||||
"invoice": ["parent", "customer", "customer_group", "posting_date","item_code", "item_name","item_group", "brand", "description", \
|
"invoice": ["parent", "customer", "customer_group", "posting_date","item_code", "item_name","item_group", "brand", "description", \
|
||||||
@ -45,7 +44,7 @@ def execute(filters=None):
|
|||||||
|
|
||||||
columns = get_columns(group_wise_columns, filters)
|
columns = get_columns(group_wise_columns, filters)
|
||||||
|
|
||||||
for src in source:
|
for src in gross_profit_data.grouped_data:
|
||||||
row = []
|
row = []
|
||||||
for col in group_wise_columns.get(scrub(filters.group_by)):
|
for col in group_wise_columns.get(scrub(filters.group_by)):
|
||||||
row.append(src.get(col))
|
row.append(src.get(col))
|
||||||
@ -103,6 +102,7 @@ class GrossProfitGenerator(object):
|
|||||||
self.load_stock_ledger_entries()
|
self.load_stock_ledger_entries()
|
||||||
self.load_product_bundle()
|
self.load_product_bundle()
|
||||||
self.load_non_stock_items()
|
self.load_non_stock_items()
|
||||||
|
self.get_returned_invoice_items()
|
||||||
self.process()
|
self.process()
|
||||||
|
|
||||||
def process(self):
|
def process(self):
|
||||||
@ -143,40 +143,68 @@ class GrossProfitGenerator(object):
|
|||||||
row.gross_profit_percent = 0.0
|
row.gross_profit_percent = 0.0
|
||||||
|
|
||||||
# add to grouped
|
# add to grouped
|
||||||
if self.filters.group_by != "Invoice":
|
self.grouped.setdefault(row.get(scrub(self.filters.group_by)), []).append(row)
|
||||||
self.grouped.setdefault(row.get(scrub(self.filters.group_by)), []).append(row)
|
|
||||||
|
|
||||||
self.data.append(row)
|
|
||||||
|
|
||||||
if self.grouped:
|
if self.grouped:
|
||||||
self.get_average_rate_based_on_group_by()
|
self.get_average_rate_based_on_group_by()
|
||||||
else:
|
|
||||||
self.grouped_data = []
|
|
||||||
|
|
||||||
def get_average_rate_based_on_group_by(self):
|
def get_average_rate_based_on_group_by(self):
|
||||||
# sum buying / selling totals for group
|
# sum buying / selling totals for group
|
||||||
self.grouped_data = []
|
self.grouped_data = []
|
||||||
for key in self.grouped.keys():
|
for key in self.grouped.keys():
|
||||||
for i, row in enumerate(self.grouped[key]):
|
if self.filters.get("group_by") != "Invoice":
|
||||||
if i==0:
|
for i, row in enumerate(self.grouped[key]):
|
||||||
new_row = row
|
if i==0:
|
||||||
else:
|
new_row = row
|
||||||
new_row.qty += row.qty
|
else:
|
||||||
new_row.buying_amount += row.buying_amount
|
new_row.qty += row.qty
|
||||||
new_row.base_amount += row.base_amount
|
new_row.buying_amount += row.buying_amount
|
||||||
|
new_row.base_amount += row.base_amount
|
||||||
|
new_row = self.set_average_rate(new_row)
|
||||||
|
self.grouped_data.append(new_row)
|
||||||
|
else:
|
||||||
|
for i, row in enumerate(self.grouped[key]):
|
||||||
|
if row.parent in self.returned_invoices \
|
||||||
|
and row.item_code in self.returned_invoices[row.parent]:
|
||||||
|
returned_item_rows = self.returned_invoices[row.parent][row.item_code]
|
||||||
|
for returned_item_row in returned_item_rows:
|
||||||
|
row.qty += returned_item_row.qty
|
||||||
|
row.base_amount += returned_item_row.base_amount
|
||||||
|
row.buying_amount = row.qty * row.buying_rate
|
||||||
|
if row.qty:
|
||||||
|
row = self.set_average_rate(row)
|
||||||
|
self.grouped_data.append(row)
|
||||||
|
|
||||||
new_row.gross_profit = new_row.base_amount - new_row.buying_amount
|
def set_average_rate(self, new_row):
|
||||||
new_row.gross_profit_percent = ((new_row.gross_profit / new_row.base_amount) * 100.0) \
|
new_row.gross_profit = new_row.base_amount - new_row.buying_amount
|
||||||
if new_row.base_amount else 0
|
new_row.gross_profit_percent = ((new_row.gross_profit / new_row.base_amount) * 100.0) \
|
||||||
new_row.buying_rate = (new_row.buying_amount / new_row.qty) \
|
if new_row.base_amount else 0
|
||||||
if new_row.qty else 0
|
new_row.buying_rate = (new_row.buying_amount / new_row.qty) if new_row.qty else 0
|
||||||
new_row.base_rate = (new_row.base_amount / new_row.qty) \
|
new_row.base_rate = (new_row.base_amount / new_row.qty) if new_row.qty else 0
|
||||||
if new_row.qty else 0
|
return new_row
|
||||||
|
|
||||||
self.grouped_data.append(new_row)
|
def get_returned_invoice_items(self):
|
||||||
|
returned_invoices = frappe.db.sql("""
|
||||||
|
select
|
||||||
|
si.name, si_item.item_code, si_item.qty, si_item.base_amount, si.return_against
|
||||||
|
from
|
||||||
|
`tabSales Invoice` si, `tabSales Invoice Item` si_item
|
||||||
|
where
|
||||||
|
si.name = si_item.parent
|
||||||
|
and si.docstatus = 1
|
||||||
|
and si.is_return = 1
|
||||||
|
""", as_dict=1)
|
||||||
|
|
||||||
|
self.returned_invoices = frappe._dict()
|
||||||
|
for inv in returned_invoices:
|
||||||
|
self.returned_invoices.setdefault(inv.return_against, frappe._dict())\
|
||||||
|
.setdefault(inv.item_code, []).append(inv)
|
||||||
|
|
||||||
def skip_row(self, row, product_bundles):
|
def skip_row(self, row, product_bundles):
|
||||||
if self.filters.get("group_by") != "Invoice" and not row.get(scrub(self.filters.get("group_by"))):
|
if self.filters.get("group_by") != "Invoice":
|
||||||
|
if not row.get(scrub(self.filters.get("group_by"))):
|
||||||
|
return True
|
||||||
|
elif row.get("is_return") == 1:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def get_buying_amount_from_product_bundle(self, row, product_bundle):
|
def get_buying_amount_from_product_bundle(self, row, product_bundle):
|
||||||
@ -268,20 +296,26 @@ class GrossProfitGenerator(object):
|
|||||||
sales_person_cols = ""
|
sales_person_cols = ""
|
||||||
sales_team_table = ""
|
sales_team_table = ""
|
||||||
|
|
||||||
self.si_list = frappe.db.sql("""select `tabSales Invoice Item`.parenttype, `tabSales Invoice Item`.parent,
|
self.si_list = frappe.db.sql("""
|
||||||
`tabSales Invoice`.posting_date, `tabSales Invoice`.posting_time, `tabSales Invoice`.project, `tabSales Invoice`.update_stock,
|
select
|
||||||
`tabSales Invoice`.customer, `tabSales Invoice`.customer_group, `tabSales Invoice`.territory,
|
`tabSales Invoice Item`.parenttype, `tabSales Invoice Item`.parent,
|
||||||
`tabSales Invoice Item`.item_code, `tabSales Invoice Item`.item_name, `tabSales Invoice Item`.description,
|
`tabSales Invoice`.posting_date, `tabSales Invoice`.posting_time,
|
||||||
`tabSales Invoice Item`.warehouse, `tabSales Invoice Item`.item_group, `tabSales Invoice Item`.brand,
|
`tabSales Invoice`.project, `tabSales Invoice`.update_stock,
|
||||||
`tabSales Invoice Item`.dn_detail, `tabSales Invoice Item`.delivery_note, `tabSales Invoice Item`.stock_qty as qty,
|
`tabSales Invoice`.customer, `tabSales Invoice`.customer_group,
|
||||||
`tabSales Invoice Item`.base_net_rate, `tabSales Invoice Item`.base_net_amount, `tabSales Invoice Item`.name as "item_row"
|
`tabSales Invoice`.territory, `tabSales Invoice Item`.item_code,
|
||||||
|
`tabSales Invoice Item`.item_name, `tabSales Invoice Item`.description,
|
||||||
|
`tabSales Invoice Item`.warehouse, `tabSales Invoice Item`.item_group,
|
||||||
|
`tabSales Invoice Item`.brand, `tabSales Invoice Item`.dn_detail,
|
||||||
|
`tabSales Invoice Item`.delivery_note, `tabSales Invoice Item`.stock_qty as qty,
|
||||||
|
`tabSales Invoice Item`.base_net_rate, `tabSales Invoice Item`.base_net_amount,
|
||||||
|
`tabSales Invoice Item`.name as "item_row", `tabSales Invoice`.is_return
|
||||||
{sales_person_cols}
|
{sales_person_cols}
|
||||||
from
|
from
|
||||||
`tabSales Invoice`
|
`tabSales Invoice` inner join `tabSales Invoice Item`
|
||||||
inner join `tabSales Invoice Item` on `tabSales Invoice Item`.parent = `tabSales Invoice`.name
|
on `tabSales Invoice Item`.parent = `tabSales Invoice`.name
|
||||||
{sales_team_table}
|
{sales_team_table}
|
||||||
where
|
where
|
||||||
`tabSales Invoice`.docstatus = 1 {conditions} {match_cond}
|
`tabSales Invoice`.docstatus=1 {conditions} {match_cond}
|
||||||
order by
|
order by
|
||||||
`tabSales Invoice`.posting_date desc, `tabSales Invoice`.posting_time desc"""
|
`tabSales Invoice`.posting_date desc, `tabSales Invoice`.posting_time desc"""
|
||||||
.format(conditions=conditions, sales_person_cols=sales_person_cols,
|
.format(conditions=conditions, sales_person_cols=sales_person_cols,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user