brotherton-erpnext/erpnext/selling/search_criteria/gross_profit/gross_profit.py

86 lines
2.8 KiB
Python
Raw Normal View History

2012-02-23 07:05:32 +00:00
# ERPNext - web based ERP (http://erpnext.com)
# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2012-02-23 07:05:32 +00:00
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2012-02-23 07:05:32 +00:00
# Add Columns
# ------------
from __future__ import unicode_literals
2012-09-24 10:15:02 +00:00
from webnotes.utils import flt
colnames[colnames.index('Rate*')] = 'Rate'
col_idx['Rate'] = col_idx['Rate*']
col_idx.pop('Rate*')
colnames[colnames.index('Amount*')] = 'Amount'
col_idx['Amount'] = col_idx['Amount*']
col_idx.pop('Amount*')
columns = [
['Purchase Cost','Currency','150px',''],
2012-09-24 10:15:02 +00:00
['Gross Profit','Currency','150px',''],
['Gross Profit (%)','Currrency','150px','']
]
for c in columns:
colnames.append(c[0])
coltypes.append(c[1])
colwidths.append(c[2])
coloptions.append(c[3])
col_idx[c[0]] = len(colnames)-1
sle = sql("""
select
2012-09-24 10:15:02 +00:00
actual_qty, incoming_rate, voucher_no, item_code, warehouse
from
`tabStock Ledger Entry`
where
2012-09-24 10:15:02 +00:00
voucher_type = 'Delivery Note'
and ifnull(is_cancelled, 'No') = 'No'
order by posting_date desc, posting_time desc, name desc
""", as_dict=1)
2012-09-24 10:15:02 +00:00
def get_purchase_cost(dn, item, wh, qty):
from webnotes.utils import flt
global sle
purchase_cost = 0
for d in sle:
2012-09-24 10:15:02 +00:00
if d['voucher_no'] == dn and d['item_code'] == item \
and d['warehouse'] == wh and abs(d['actual_qty']) == qty:
purchase_cost += flt(d['incoming_rate'])*flt(abs(d['actual_qty']))
return purchase_cost
2012-09-24 10:15:02 +00:00
out, tot_amount, tot_pur_cost = [], 0, 0
for r in res:
2012-09-24 10:15:02 +00:00
purchase_cost = get_purchase_cost(r[col_idx['ID']], r[col_idx['Item Code']], \
r[col_idx['Warehouse']], r[col_idx['Quantity']])
r.append(purchase_cost)
gp = flt(r[col_idx['Amount']]) - flt(purchase_cost)
2012-09-24 10:29:26 +00:00
gp_percent = purchase_cost and round((gp*100/purchase_cost), 2) or 0
2012-09-24 10:15:02 +00:00
r.append(fmt_money(gp))
r.append(fmt_money(gp_percent))
out.append(r)
tot_amount += flt(r[col_idx['Amount']])
tot_pur_cost += flt(purchase_cost)
# Add Total Row
l_row = ['' for i in range(len(colnames))]
l_row[col_idx['Project Name']] = '<b>TOTALS</b>'
l_row[col_idx['Amount']] = fmt_money(tot_amount)
2012-09-24 10:15:02 +00:00
l_row[col_idx['Purchase Cost']] = fmt_money(tot_pur_cost)
l_row[col_idx['Gross Profit']] = fmt_money(flt(tot_amount) - flt(tot_pur_cost))
l_row[col_idx['Gross Profit (%)']] = round((flt(tot_amount) - flt(tot_pur_cost))*100/ \
flt(tot_pur_cost), 2)
out.append(l_row)