2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2013-08-05 09:29:54 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
2013-02-25 12:47:51 +00:00
|
|
|
from __future__ import unicode_literals
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
2014-11-24 09:42:37 +00:00
|
|
|
from frappe import _, scrub
|
2017-02-01 08:04:06 +00:00
|
|
|
from erpnext.stock.utils import get_incoming_rate
|
2017-03-14 09:26:24 +00:00
|
|
|
from erpnext.controllers.queries import get_match_cond
|
2017-03-28 06:50:31 +00:00
|
|
|
from erpnext.stock.stock_ledger import get_valuation_rate
|
2015-04-13 10:01:24 +00:00
|
|
|
from frappe.utils import flt
|
2013-02-25 12:47:51 +00:00
|
|
|
|
2016-08-22 08:44:52 +00:00
|
|
|
|
2013-02-25 12:47:51 +00:00
|
|
|
def execute(filters=None):
|
2016-01-19 11:57:06 +00:00
|
|
|
if not filters: filters = frappe._dict()
|
2017-03-31 07:14:29 +00:00
|
|
|
filters.currency = frappe.db.get_value("Company", filters.company, "default_currency")
|
2016-03-09 07:45:38 +00:00
|
|
|
|
2014-11-24 09:42:37 +00:00
|
|
|
gross_profit_data = GrossProfitGenerator(filters)
|
|
|
|
|
|
|
|
data = []
|
|
|
|
source = gross_profit_data.grouped_data if filters.get("group_by") != "Invoice" else gross_profit_data.data
|
|
|
|
|
|
|
|
group_wise_columns = frappe._dict({
|
2016-10-18 10:57:07 +00:00
|
|
|
"invoice": ["parent", "customer", "customer_group", "posting_date","item_code", "item_name","item_group", "brand", "description", \
|
2014-11-24 09:42:37 +00:00
|
|
|
"warehouse", "qty", "base_rate", "buying_rate", "base_amount",
|
2016-03-09 11:32:59 +00:00
|
|
|
"buying_amount", "gross_profit", "gross_profit_percent", "project"],
|
2016-03-09 07:45:38 +00:00
|
|
|
"item_code": ["item_code", "item_name", "brand", "description", "qty", "base_rate",
|
2014-11-24 09:42:37 +00:00
|
|
|
"buying_rate", "base_amount", "buying_amount", "gross_profit", "gross_profit_percent"],
|
|
|
|
"warehouse": ["warehouse", "qty", "base_rate", "buying_rate", "base_amount", "buying_amount",
|
|
|
|
"gross_profit", "gross_profit_percent"],
|
|
|
|
"territory": ["territory", "qty", "base_rate", "buying_rate", "base_amount", "buying_amount",
|
|
|
|
"gross_profit", "gross_profit_percent"],
|
|
|
|
"brand": ["brand", "qty", "base_rate", "buying_rate", "base_amount", "buying_amount",
|
|
|
|
"gross_profit", "gross_profit_percent"],
|
|
|
|
"item_group": ["item_group", "qty", "base_rate", "buying_rate", "base_amount", "buying_amount",
|
|
|
|
"gross_profit", "gross_profit_percent"],
|
|
|
|
"customer": ["customer", "customer_group", "qty", "base_rate", "buying_rate", "base_amount", "buying_amount",
|
|
|
|
"gross_profit", "gross_profit_percent"],
|
|
|
|
"customer_group": ["customer_group", "qty", "base_rate", "buying_rate", "base_amount", "buying_amount",
|
|
|
|
"gross_profit", "gross_profit_percent"],
|
|
|
|
"sales_person": ["sales_person", "allocated_amount", "qty", "base_rate", "buying_rate", "base_amount", "buying_amount",
|
|
|
|
"gross_profit", "gross_profit_percent"],
|
2016-03-09 11:32:59 +00:00
|
|
|
"project": ["project", "base_amount", "buying_amount", "gross_profit", "gross_profit_percent"],
|
2014-11-24 09:42:37 +00:00
|
|
|
"territory": ["territory", "base_amount", "buying_amount", "gross_profit", "gross_profit_percent"]
|
|
|
|
})
|
|
|
|
|
|
|
|
columns = get_columns(group_wise_columns, filters)
|
|
|
|
|
|
|
|
for src in source:
|
|
|
|
row = []
|
|
|
|
for col in group_wise_columns.get(scrub(filters.group_by)):
|
|
|
|
row.append(src.get(col))
|
2016-03-09 07:45:38 +00:00
|
|
|
|
2017-03-31 07:14:29 +00:00
|
|
|
row.append(filters.currency)
|
2014-11-24 09:42:37 +00:00
|
|
|
data.append(row)
|
|
|
|
|
|
|
|
return columns, data
|
|
|
|
|
|
|
|
def get_columns(group_wise_columns, filters):
|
|
|
|
columns = []
|
|
|
|
column_map = frappe._dict({
|
2015-05-28 08:02:51 +00:00
|
|
|
"parent": _("Sales Invoice") + ":Link/Sales Invoice:120",
|
2014-11-24 09:42:37 +00:00
|
|
|
"posting_date": _("Posting Date") + ":Date",
|
|
|
|
"posting_time": _("Posting Time"),
|
|
|
|
"item_code": _("Item Code") + ":Link/Item",
|
|
|
|
"item_name": _("Item Name"),
|
2016-02-04 03:45:00 +00:00
|
|
|
"item_group": _("Item Group") + ":Link/Item Group",
|
2014-11-24 09:42:37 +00:00
|
|
|
"brand": _("Brand"),
|
|
|
|
"description": _("Description"),
|
|
|
|
"warehouse": _("Warehouse") + ":Link/Warehouse",
|
|
|
|
"qty": _("Qty") + ":Float",
|
2016-01-19 11:57:06 +00:00
|
|
|
"base_rate": _("Avg. Selling Rate") + ":Currency/currency",
|
|
|
|
"buying_rate": _("Avg. Buying Rate") + ":Currency/currency",
|
|
|
|
"base_amount": _("Selling Amount") + ":Currency/currency",
|
|
|
|
"buying_amount": _("Buying Amount") + ":Currency/currency",
|
|
|
|
"gross_profit": _("Gross Profit") + ":Currency/currency",
|
2014-11-24 09:42:37 +00:00
|
|
|
"gross_profit_percent": _("Gross Profit %") + ":Percent",
|
2016-03-09 11:32:59 +00:00
|
|
|
"project": _("Project") + ":Link/Project",
|
2014-11-24 09:42:37 +00:00
|
|
|
"sales_person": _("Sales person"),
|
2016-01-19 11:57:06 +00:00
|
|
|
"allocated_amount": _("Allocated Amount") + ":Currency/currency",
|
2014-11-24 09:42:37 +00:00
|
|
|
"customer": _("Customer") + ":Link/Customer",
|
|
|
|
"customer_group": _("Customer Group") + ":Link/Customer Group",
|
|
|
|
"territory": _("Territory") + ":Link/Territory"
|
|
|
|
})
|
|
|
|
|
|
|
|
for col in group_wise_columns.get(scrub(filters.group_by)):
|
|
|
|
columns.append(column_map.get(col))
|
2016-03-09 07:45:38 +00:00
|
|
|
|
2016-01-19 11:57:06 +00:00
|
|
|
columns.append({
|
|
|
|
"fieldname": "currency",
|
|
|
|
"label" : _("Currency"),
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Currency"
|
|
|
|
})
|
2014-11-24 09:42:37 +00:00
|
|
|
|
|
|
|
return columns
|
|
|
|
|
|
|
|
class GrossProfitGenerator(object):
|
|
|
|
def __init__(self, filters=None):
|
2016-08-22 12:17:13 +00:00
|
|
|
self.data = []
|
|
|
|
self.average_buying_rate = {}
|
|
|
|
self.filters = frappe._dict(filters)
|
|
|
|
self.load_invoice_items()
|
|
|
|
self.load_stock_ledger_entries()
|
|
|
|
self.load_product_bundle()
|
|
|
|
self.load_non_stock_items()
|
|
|
|
self.process()
|
2014-11-24 09:42:37 +00:00
|
|
|
|
|
|
|
def process(self):
|
|
|
|
self.grouped = {}
|
|
|
|
for row in self.si_list:
|
2015-07-07 08:29:23 +00:00
|
|
|
if self.skip_row(row, self.product_bundles):
|
2014-11-24 09:42:37 +00:00
|
|
|
continue
|
|
|
|
|
2015-04-13 10:01:24 +00:00
|
|
|
row.base_amount = flt(row.base_net_amount)
|
2014-11-24 09:42:37 +00:00
|
|
|
|
2015-11-12 05:37:32 +00:00
|
|
|
product_bundles = []
|
2015-11-05 07:43:02 +00:00
|
|
|
if row.update_stock:
|
|
|
|
product_bundles = self.product_bundles.get(row.parenttype, {}).get(row.parent, frappe._dict())
|
|
|
|
elif row.dn_detail:
|
|
|
|
product_bundles = self.product_bundles.get("Delivery Note", {})\
|
|
|
|
.get(row.delivery_note, frappe._dict())
|
|
|
|
row.item_row = row.dn_detail
|
2015-11-12 05:37:32 +00:00
|
|
|
|
2014-11-24 09:42:37 +00:00
|
|
|
# get buying amount
|
2015-07-07 08:29:23 +00:00
|
|
|
if row.item_code in product_bundles:
|
2015-11-12 05:37:32 +00:00
|
|
|
row.buying_amount = self.get_buying_amount_from_product_bundle(row,
|
2015-11-05 07:43:02 +00:00
|
|
|
product_bundles[row.item_code])
|
2014-11-24 09:42:37 +00:00
|
|
|
else:
|
|
|
|
row.buying_amount = self.get_buying_amount(row, row.item_code)
|
|
|
|
|
|
|
|
# get buying rate
|
|
|
|
if row.qty:
|
2015-04-13 10:01:24 +00:00
|
|
|
row.buying_rate = row.buying_amount / row.qty
|
|
|
|
row.base_rate = row.base_amount / row.qty
|
2014-11-24 09:42:37 +00:00
|
|
|
else:
|
2015-04-13 10:01:24 +00:00
|
|
|
row.buying_rate, row.base_rate = 0.0, 0.0
|
2014-11-24 09:42:37 +00:00
|
|
|
|
|
|
|
# calculate gross profit
|
2015-04-13 10:01:24 +00:00
|
|
|
row.gross_profit = row.base_amount - row.buying_amount
|
|
|
|
if row.base_amount:
|
|
|
|
row.gross_profit_percent = (row.gross_profit / row.base_amount) * 100.0
|
2014-11-24 09:42:37 +00:00
|
|
|
else:
|
|
|
|
row.gross_profit_percent = 0.0
|
|
|
|
|
|
|
|
# add to grouped
|
|
|
|
if self.filters.group_by != "Invoice":
|
|
|
|
self.grouped.setdefault(row.get(scrub(self.filters.group_by)), []).append(row)
|
|
|
|
|
|
|
|
self.data.append(row)
|
|
|
|
|
|
|
|
if self.grouped:
|
2016-04-12 11:31:39 +00:00
|
|
|
self.get_average_rate_based_on_group_by()
|
2014-11-24 09:42:37 +00:00
|
|
|
else:
|
|
|
|
self.grouped_data = []
|
|
|
|
|
2016-04-12 11:31:39 +00:00
|
|
|
def get_average_rate_based_on_group_by(self):
|
2014-11-24 09:42:37 +00:00
|
|
|
# sum buying / selling totals for group
|
|
|
|
self.grouped_data = []
|
|
|
|
for key in self.grouped.keys():
|
|
|
|
for i, row in enumerate(self.grouped[key]):
|
|
|
|
if i==0:
|
|
|
|
new_row = row
|
|
|
|
else:
|
|
|
|
new_row.qty += row.qty
|
|
|
|
new_row.buying_amount += row.buying_amount
|
2015-04-13 10:01:24 +00:00
|
|
|
new_row.base_amount += row.base_amount
|
2014-11-24 09:42:37 +00:00
|
|
|
|
2015-04-13 10:01:24 +00:00
|
|
|
new_row.gross_profit = new_row.base_amount - new_row.buying_amount
|
|
|
|
new_row.gross_profit_percent = ((new_row.gross_profit / new_row.base_amount) * 100.0) \
|
|
|
|
if new_row.base_amount else 0
|
|
|
|
new_row.buying_rate = (new_row.buying_amount / new_row.qty) \
|
2014-11-24 09:42:37 +00:00
|
|
|
if new_row.qty else 0
|
2016-04-12 11:31:39 +00:00
|
|
|
new_row.base_rate = (new_row.base_amount / new_row.qty) \
|
|
|
|
if new_row.qty else 0
|
2014-11-24 09:42:37 +00:00
|
|
|
|
|
|
|
self.grouped_data.append(new_row)
|
|
|
|
|
2015-07-07 08:29:23 +00:00
|
|
|
def skip_row(self, row, product_bundles):
|
2015-04-13 10:01:24 +00:00
|
|
|
if self.filters.get("group_by") != "Invoice" and not row.get(scrub(self.filters.get("group_by"))):
|
2014-11-24 09:42:37 +00:00
|
|
|
return True
|
|
|
|
|
2015-07-07 08:29:23 +00:00
|
|
|
def get_buying_amount_from_product_bundle(self, row, product_bundle):
|
2014-11-24 09:42:37 +00:00
|
|
|
buying_amount = 0.0
|
2015-11-05 07:43:02 +00:00
|
|
|
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)
|
2014-11-24 09:42:37 +00:00
|
|
|
|
|
|
|
return buying_amount
|
|
|
|
|
|
|
|
def get_buying_amount(self, row, item_code):
|
|
|
|
# IMP NOTE
|
|
|
|
# stock_ledger_entries should already be filtered by item_code and warehouse and
|
|
|
|
# sorted by posting_date desc, posting_time desc
|
|
|
|
if item_code in self.non_stock_items:
|
2016-08-22 11:57:22 +00:00
|
|
|
#Issue 6089-Get last purchasing rate for non-stock item
|
2016-08-22 08:44:52 +00:00
|
|
|
item_rate = self.get_last_purchase_rate(item_code)
|
2015-04-13 10:01:24 +00:00
|
|
|
return flt(row.qty) * item_rate
|
2014-11-24 09:42:37 +00:00
|
|
|
|
|
|
|
else:
|
2015-07-23 11:38:44 +00:00
|
|
|
my_sle = self.sle.get((item_code, row.warehouse))
|
|
|
|
if (row.update_stock or row.dn_detail) and my_sle:
|
2015-11-05 07:43:02 +00:00
|
|
|
parenttype, parent = row.parenttype, row.parent
|
2015-05-25 06:53:03 +00:00
|
|
|
if row.dn_detail:
|
2015-11-05 07:43:02 +00:00
|
|
|
parenttype, parent = "Delivery Note", row.delivery_note
|
2015-11-12 05:37:32 +00:00
|
|
|
|
2015-04-13 10:01:24 +00:00
|
|
|
for i, sle in enumerate(my_sle):
|
|
|
|
# find the stock valution rate from stock ledger entry
|
2015-07-01 11:18:03 +00:00
|
|
|
if sle.voucher_type == parenttype and parent == sle.voucher_no and \
|
2015-11-05 07:43:02 +00:00
|
|
|
sle.voucher_detail_no == row.item_row:
|
2015-04-13 10:01:24 +00:00
|
|
|
previous_stock_value = len(my_sle) > i+1 and \
|
|
|
|
flt(my_sle[i+1].stock_value) or 0.0
|
|
|
|
return previous_stock_value - flt(sle.stock_value)
|
|
|
|
else:
|
2017-02-01 08:04:06 +00:00
|
|
|
return flt(row.qty) * self.get_average_buying_rate(row, item_code)
|
2014-11-24 09:42:37 +00:00
|
|
|
|
|
|
|
return 0.0
|
|
|
|
|
2017-02-01 08:04:06 +00:00
|
|
|
def get_average_buying_rate(self, row, item_code):
|
2015-04-13 10:01:24 +00:00
|
|
|
if not item_code in self.average_buying_rate:
|
|
|
|
if item_code in self.non_stock_items:
|
2017-04-03 06:41:36 +00:00
|
|
|
self.average_buying_rate[item_code] = flt(frappe.db.sql("""
|
|
|
|
select sum(base_net_amount) / sum(qty * conversion_factor)
|
2015-04-13 10:01:24 +00:00
|
|
|
from `tabPurchase Invoice Item`
|
|
|
|
where item_code = %s and docstatus=1""", item_code)[0][0])
|
|
|
|
else:
|
2017-03-28 06:50:31 +00:00
|
|
|
average_buying_rate = get_incoming_rate(row)
|
|
|
|
if not average_buying_rate:
|
2017-03-31 07:14:29 +00:00
|
|
|
average_buying_rate = get_valuation_rate(item_code, row.warehouse,
|
2017-04-03 06:35:35 +00:00
|
|
|
row.parenttype, row.parent, allow_zero_rate=True,
|
|
|
|
currency=self.filters.currency)
|
2017-03-31 12:09:45 +00:00
|
|
|
|
2017-04-03 06:41:36 +00:00
|
|
|
self.average_buying_rate[item_code] = flt(average_buying_rate)
|
2015-04-13 10:01:24 +00:00
|
|
|
|
|
|
|
return self.average_buying_rate[item_code]
|
|
|
|
|
2016-08-22 08:44:52 +00:00
|
|
|
def get_last_purchase_rate(self, item_code):
|
|
|
|
if self.filters.to_date:
|
|
|
|
last_purchase_rate = frappe.db.sql("""
|
|
|
|
select (a.base_rate / a.conversion_factor)
|
|
|
|
from `tabPurchase Invoice Item` a
|
|
|
|
where a.item_code = %s and a.docstatus=1
|
2017-03-31 07:14:29 +00:00
|
|
|
and modified <= %s
|
2016-08-22 08:44:52 +00:00
|
|
|
order by a.modified desc limit 1""", (item_code,self.filters.to_date))
|
|
|
|
else:
|
|
|
|
last_purchase_rate = frappe.db.sql("""
|
|
|
|
select (a.base_rate / a.conversion_factor)
|
|
|
|
from `tabPurchase Invoice Item` a
|
|
|
|
where a.item_code = %s and a.docstatus=1
|
|
|
|
order by a.modified desc limit 1""", item_code)
|
2016-08-22 12:17:13 +00:00
|
|
|
return flt(last_purchase_rate[0][0]) if last_purchase_rate else 0
|
2016-08-22 08:44:52 +00:00
|
|
|
|
2014-11-24 09:42:37 +00:00
|
|
|
def load_invoice_items(self):
|
|
|
|
conditions = ""
|
|
|
|
if self.filters.company:
|
|
|
|
conditions += " and company = %(company)s"
|
|
|
|
if self.filters.from_date:
|
|
|
|
conditions += " and posting_date >= %(from_date)s"
|
|
|
|
if self.filters.to_date:
|
|
|
|
conditions += " and posting_date <= %(to_date)s"
|
2017-03-31 07:14:29 +00:00
|
|
|
|
2017-03-12 11:40:41 +00:00
|
|
|
if self.filters.group_by=="Sales Person":
|
|
|
|
sales_person_cols = ", sales.sales_person, sales.allocated_amount, sales.incentives"
|
2017-03-14 09:26:24 +00:00
|
|
|
sales_team_table = "left join `tabSales Team` sales on sales.parent = `tabSales Invoice`.name"
|
2017-03-12 11:40:41 +00:00
|
|
|
else:
|
|
|
|
sales_person_cols = ""
|
|
|
|
sales_team_table = ""
|
2014-11-24 09:42:37 +00:00
|
|
|
|
2017-03-14 09:26:24 +00:00
|
|
|
self.si_list = frappe.db.sql("""select `tabSales Invoice Item`.parenttype, `tabSales Invoice Item`.parent,
|
|
|
|
`tabSales Invoice`.posting_date, `tabSales Invoice`.posting_time, `tabSales Invoice`.project, `tabSales Invoice`.update_stock,
|
|
|
|
`tabSales Invoice`.customer, `tabSales Invoice`.customer_group, `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"
|
2017-03-12 11:40:41 +00:00
|
|
|
{sales_person_cols}
|
2017-03-31 07:14:29 +00:00
|
|
|
from
|
2017-03-14 09:26:24 +00:00
|
|
|
`tabSales Invoice`
|
|
|
|
inner join `tabSales Invoice Item` on `tabSales Invoice Item`.parent = `tabSales Invoice`.name
|
2017-03-12 11:40:41 +00:00
|
|
|
{sales_team_table}
|
2014-11-24 09:42:37 +00:00
|
|
|
where
|
2017-03-14 09:26:24 +00:00
|
|
|
`tabSales Invoice`.docstatus = 1 and `tabSales Invoice`.is_return != 1 {conditions} {match_cond}
|
2014-11-24 09:42:37 +00:00
|
|
|
order by
|
2017-03-14 09:26:24 +00:00
|
|
|
`tabSales Invoice`.posting_date desc, `tabSales Invoice`.posting_time desc"""
|
2017-03-31 07:14:29 +00:00
|
|
|
.format(conditions=conditions, sales_person_cols=sales_person_cols,
|
2017-03-14 09:26:24 +00:00
|
|
|
sales_team_table=sales_team_table, match_cond = get_match_cond('Sales Invoice')), self.filters, as_dict=1)
|
2014-11-24 09:42:37 +00:00
|
|
|
|
|
|
|
def load_stock_ledger_entries(self):
|
|
|
|
res = frappe.db.sql("""select item_code, voucher_type, voucher_no,
|
|
|
|
voucher_detail_no, stock_value, warehouse, actual_qty as qty
|
|
|
|
from `tabStock Ledger Entry`
|
|
|
|
where company=%(company)s
|
|
|
|
order by
|
|
|
|
item_code desc, warehouse desc, posting_date desc,
|
|
|
|
posting_time desc, name desc""", self.filters, as_dict=True)
|
|
|
|
self.sle = {}
|
|
|
|
for r in res:
|
|
|
|
if (r.item_code, r.warehouse) not in self.sle:
|
|
|
|
self.sle[(r.item_code, r.warehouse)] = []
|
|
|
|
|
|
|
|
self.sle[(r.item_code, r.warehouse)].append(r)
|
|
|
|
|
2015-07-07 08:29:23 +00:00
|
|
|
def load_product_bundle(self):
|
|
|
|
self.product_bundles = {}
|
2014-11-24 09:42:37 +00:00
|
|
|
|
|
|
|
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):
|
2015-07-07 08:29:23 +00:00
|
|
|
self.product_bundles.setdefault(d.parenttype, frappe._dict()).setdefault(d.parent,
|
2014-11-24 09:42:37 +00:00
|
|
|
frappe._dict()).setdefault(d.parent_item, []).append(d)
|
|
|
|
|
|
|
|
def load_non_stock_items(self):
|
|
|
|
self.non_stock_items = frappe.db.sql_list("""select name from tabItem
|
2015-07-24 09:46:25 +00:00
|
|
|
where is_stock_item=0""")
|