blob: b299c5ecace44c1837c6c4aa59d3f616fef00a9b [file] [log] [blame]
Anand Doshi756dca72013-01-15 18:39:21 +05301# ERPNext - web based ERP (http://erpnext.com)
2# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17from __future__ import unicode_literals
18import webnotes
19from webnotes.utils import getdate, flt, add_days
20import json
21
22@webnotes.whitelist()
23def get_item_details(args):
24 """
25 args = {
26 "doctype": "",
27 "docname": "",
28 "item_code": "",
29 "warehouse": None,
30 "supplier": None,
31 "transaction_date": None,
32 "conversion_rate": 1.0
33 }
34 """
35 if isinstance(args, basestring):
36 args = json.loads(args)
37
38 args = webnotes._dict(args)
39
40 item_wrapper = webnotes.model_wrapper("Item", args.item_code)
41 item = item_wrapper.doc
42
43 from stock.utils import validate_end_of_life
44 validate_end_of_life(item.name, item.end_of_life)
45
46 # fetch basic values
47 out = webnotes._dict()
48 out.update({
49 "item_name": item.item_name,
50 "item_group": item.item_group,
51 "brand": item.brand,
52 "description": item.description,
53 "qty": 0,
54 "stock_uom": item.stock_uom,
55 "uom": item.stock_uom,
56 "conversion_factor": 1,
57 "warehouse": args.warehouse or item.default_warehouse,
58 "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in
59 item_wrapper.doclist.get({"parentfield": "ref_rate_details"})))),
60 "batch_no": None,
61 "expense_head": item.purchase_account,
62 "cost_center": item.cost_center
63 })
64
65 if args.supplier:
66 item_supplier = item_wrapper.doclist.get({"parentfield": "item_supplier_details",
67 "supplier": args.supplier})
68 if item_supplier:
69 out["supplier_part_no"] = item_supplier[0].supplier_part_no
70
71 if out.warehouse:
72 out.projected_qty = webnotes.conn.get_value("Bin", {"item_code": item.name,
73 "warehouse": out.warehouse}, "projected_qty")
74
75 if args.transaction_date and item.lead_time_days:
76 out.schedule_date = out.lead_time_date = add_days(args.transaction_date,
77 item.lead_time_days)
78
79 # set zero
80 out.purchase_ref_rate = out.discount_rate = out.purchase_rate = \
81 out.import_ref_rate = out.import_rate = 0.0
82
83 if args.doctype in ["Purchase Order", "Purchase Invoice", "Purchase Receipt"]:
84 # try fetching from price list
Anand Doshif8f0c0d2013-01-17 20:20:56 +053085 if args.price_list_name and args.price_list_currency:
Anand Doshi756dca72013-01-15 18:39:21 +053086 rates_as_per_price_list = get_rates_as_per_price_list(args, item_wrapper.doclist)
87 if rates_as_per_price_list:
88 out.update(rates_as_per_price_list)
89
90 # if not found, fetch from last purchase transaction
91 if not out.purchase_rate:
92 last_purchase = get_last_purchase_details(item.name, args.docname, args.conversion_rate)
93 if last_purchase:
94 out.update(last_purchase)
95
96 return out
97
98def get_rates_as_per_price_list(args, item_doclist=None):
99 if not item_doclist:
100 item_doclist = webnotes.model_wrapper("Item", args.item_code).doclist
101
102 result = item_doclist.get({"parentfield": "ref_rate_details",
103 "price_list_name": args.price_list_name, "ref_currency": args.price_list_currency})
104
105 if result:
106 purchase_ref_rate = flt(result[0].ref_rate) * flt(args.plc_conversion_rate)
107 conversion_rate = flt(args.conversion_rate) or 1.0
108 return webnotes._dict({
109 "purchase_ref_rate": purchase_ref_rate,
110 "purchase_rate": purchase_ref_rate,
111 "rate": purchase_ref_rate,
112 "discount_rate": 0,
113 "import_ref_rate": purchase_ref_rate / conversion_rate,
114 "import_rate": purchase_ref_rate / conversion_rate
115 })
116 else:
117 return webnotes._dict()
118
119def get_last_purchase_details(item_code, doc_name, conversion_rate=1.0):
120 """returns last purchase details in stock uom"""
121 # get last purchase order item details
122 last_purchase_order = webnotes.conn.sql("""\
123 select po.name, po.transaction_date, po.conversion_rate,
124 po_item.conversion_factor, po_item.purchase_ref_rate,
125 po_item.discount_rate, po_item.purchase_rate
126 from `tabPurchase Order` po, `tabPurchase Order Item` po_item
127 where po.docstatus = 1 and po_item.item_code = %s and po.name != %s and
128 po.name = po_item.parent
129 order by po.transaction_date desc, po.name desc
130 limit 1""", (item_code, doc_name), as_dict=1)
131
132 # get last purchase receipt item details
133 last_purchase_receipt = webnotes.conn.sql("""\
134 select pr.name, pr.posting_date, pr.posting_time, pr.conversion_rate,
135 pr_item.conversion_factor, pr_item.purchase_ref_rate, pr_item.discount_rate,
136 pr_item.purchase_rate
137 from `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pr_item
138 where pr.docstatus = 1 and pr_item.item_code = %s and pr.name != %s and
139 pr.name = pr_item.parent
140 order by pr.posting_date desc, pr.posting_time desc, pr.name desc
141 limit 1""", (item_code, doc_name), as_dict=1)
142
143 purchase_order_date = getdate(last_purchase_order and last_purchase_order[0].transaction_date \
144 or "1900-01-01")
145 purchase_receipt_date = getdate(last_purchase_receipt and \
146 last_purchase_receipt[0].posting_date or "1900-01-01")
147
148 if (purchase_order_date > purchase_receipt_date) or \
149 (last_purchase_order and not last_purchase_receipt):
150 # use purchase order
151 last_purchase = last_purchase_order[0]
152 purchase_date = purchase_order_date
153
154 elif (purchase_receipt_date > purchase_order_date) or \
155 (last_purchase_receipt and not last_purchase_order):
156 # use purchase receipt
157 last_purchase = last_purchase_receipt[0]
158 purchase_date = purchase_receipt_date
159
160 else:
161 return webnotes._dict()
162
163 conversion_factor = flt(last_purchase.conversion_factor)
164 out = webnotes._dict({
165 "purchase_ref_rate": flt(last_purchase.purchase_ref_rate) / conversion_factor,
166 "purchase_rate": flt(last_purchase.purchase_rate) / conversion_factor,
167 "discount_rate": flt(last_purchase.discount_rate),
168 "purchase_date": purchase_date
169 })
170
171 conversion_rate = flt(conversion_rate) or 1.0
172 out.update({
173 "import_ref_rate": out.purchase_ref_rate / conversion_rate,
174 "import_rate": out.purchase_rate / conversion_rate,
175 "rate": out.purchase_rate
176 })
177
178 return out