blob: 23d55938a2856125dae17a5329574ee691dd1a09 [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
Anand Doshi1dde46a2013-05-15 21:15:57 +053019from webnotes import msgprint, _
Anand Doshi756dca72013-01-15 18:39:21 +053020from webnotes.utils import getdate, flt, add_days
21import json
22
23@webnotes.whitelist()
24def get_item_details(args):
25 """
26 args = {
27 "doctype": "",
28 "docname": "",
29 "item_code": "",
30 "warehouse": None,
31 "supplier": None,
32 "transaction_date": None,
Anand Doshi1dde46a2013-05-15 21:15:57 +053033 "conversion_rate": 1.0,
34 "price_list_name": None,
35 "price_list_currency": None,
36 "plc_conversion_rate": 1.0,
37 "is_subcontracted": "Yes" / "No"
Anand Doshi756dca72013-01-15 18:39:21 +053038 }
39 """
40 if isinstance(args, basestring):
41 args = json.loads(args)
42
43 args = webnotes._dict(args)
44
Anand Doshi1dde46a2013-05-15 21:15:57 +053045 item_bean = webnotes.bean("Item", args.item_code)
46 item = item_bean.doc
Anand Doshi756dca72013-01-15 18:39:21 +053047
Anand Doshi1dde46a2013-05-15 21:15:57 +053048 _validate_item_details(args, item)
Anand Doshi756dca72013-01-15 18:39:21 +053049
Anand Doshi1dde46a2013-05-15 21:15:57 +053050 out = _get_basic_details(args, item_bean)
Anand Doshi756dca72013-01-15 18:39:21 +053051
Anand Doshi1dde46a2013-05-15 21:15:57 +053052 out.supplier_part_no = _get_supplier_part_no(args, item_bean)
Anand Doshi756dca72013-01-15 18:39:21 +053053
Rushabh Mehta1bcc19e2013-07-01 11:46:07 +053054 if not out.warehouse:
55 out.warehouse = item_bean.doc.default_warehouse
56
Anand Doshi756dca72013-01-15 18:39:21 +053057 if out.warehouse:
Anand Doshifc777182013-05-27 19:29:07 +053058 out.projected_qty = get_projected_qty(item.name, out.warehouse)
Anand Doshi756dca72013-01-15 18:39:21 +053059
60 if args.transaction_date and item.lead_time_days:
61 out.schedule_date = out.lead_time_date = add_days(args.transaction_date,
62 item.lead_time_days)
63
Anand Doshif3096132013-05-21 19:35:06 +053064 meta = webnotes.get_doctype(args.doctype)
Anand Doshi756dca72013-01-15 18:39:21 +053065
Anand Doshif3096132013-05-21 19:35:06 +053066 if meta.get_field("currency"):
67 out.purchase_ref_rate = out.discount_rate = out.purchase_rate = \
68 out.import_ref_rate = out.import_rate = 0.0
69 out.update(_get_price_list_rate(args, item_bean, meta))
Anand Doshi3543f302013-05-24 19:25:01 +053070
71 if args.doctype == "Material Request":
72 out.min_order_qty = flt(item.min_order_qty)
73
Anand Doshi756dca72013-01-15 18:39:21 +053074 return out
Anand Doshi1dde46a2013-05-15 21:15:57 +053075
76def _get_basic_details(args, item_bean):
77 item = item_bean.doc
78
79 out = webnotes._dict({
80 "description": item.description_html or item.description,
81 "qty": 0.0,
82 "uom": item.stock_uom,
83 "conversion_factor": 1.0,
84 "warehouse": args.warehouse or item.default_warehouse,
85 "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in
86 item_bean.doclist.get({"parentfield": "item_tax"})))),
87 "batch_no": None,
88 "expense_head": item.purchase_account,
89 "cost_center": item.cost_center
90 })
91
92 for fieldname in ("item_name", "item_group", "brand", "stock_uom"):
93 out[fieldname] = item.fields.get(fieldname)
94
95 return out
96
Anand Doshifc777182013-05-27 19:29:07 +053097def _get_price_list_rate(args, item_bean, meta):
Anand Doshif3096132013-05-21 19:35:06 +053098 from utilities.transaction_base import validate_currency
99 item = item_bean.doc
100 out = webnotes._dict()
101
102 # try fetching from price list
103 if args.price_list_name and args.price_list_currency:
104 price_list_rate = item_bean.doclist.get({
105 "parentfield": "ref_rate_details",
106 "price_list_name": args.price_list_name,
107 "ref_currency": args.price_list_currency,
Anand Doshi060d9242013-06-12 17:40:36 +0530108 "buying_or_selling": "Buying"})
Anand Doshif3096132013-05-21 19:35:06 +0530109 if price_list_rate:
Anand Doshifc777182013-05-27 19:29:07 +0530110 out.import_ref_rate = \
111 flt(price_list_rate[0].ref_rate * args.plc_conversion_rate / args.conversion_rate)
Anand Doshif3096132013-05-21 19:35:06 +0530112
113 # if not found, fetch from last purchase transaction
Anand Doshifc777182013-05-27 19:29:07 +0530114 if not out.import_ref_rate:
Anand Doshif3096132013-05-21 19:35:06 +0530115 last_purchase = get_last_purchase_details(item.name, args.docname, args.conversion_rate)
116 if last_purchase:
117 out.update(last_purchase)
118
Anand Doshifc777182013-05-27 19:29:07 +0530119 if out.import_ref_rate or out.import_rate:
Anand Doshif3096132013-05-21 19:35:06 +0530120 validate_currency(args, item, meta)
121
122 return out
123
Anand Doshi1dde46a2013-05-15 21:15:57 +0530124def _get_supplier_part_no(args, item_bean):
125 item_supplier = item_bean.doclist.get({"parentfield": "item_supplier_details",
126 "supplier": args.supplier})
127
128 return item_supplier and item_supplier[0].supplier_part_no or None
Anand Doshi756dca72013-01-15 18:39:21 +0530129
Anand Doshi1dde46a2013-05-15 21:15:57 +0530130def _validate_item_details(args, item):
131 from utilities.transaction_base import validate_item_fetch
132 validate_item_fetch(args, item)
133
134 # validate if purchase item or subcontracted item
135 if item.is_purchase_item != "Yes":
136 msgprint(_("Item") + (" %s: " % item.name) + _("not a purchase item"),
137 raise_exception=True)
138
139 if args.is_subcontracted == "Yes" and item.is_sub_contracted_item != "Yes":
140 msgprint(_("Item") + (" %s: " % item.name) +
141 _("not a sub-contracted item.") +
142 _("Please select a sub-contracted item or do not sub-contract the transaction."),
143 raise_exception=True)
Anand Doshi756dca72013-01-15 18:39:21 +0530144
145def get_last_purchase_details(item_code, doc_name, conversion_rate=1.0):
146 """returns last purchase details in stock uom"""
147 # get last purchase order item details
148 last_purchase_order = webnotes.conn.sql("""\
149 select po.name, po.transaction_date, po.conversion_rate,
150 po_item.conversion_factor, po_item.purchase_ref_rate,
151 po_item.discount_rate, po_item.purchase_rate
152 from `tabPurchase Order` po, `tabPurchase Order Item` po_item
153 where po.docstatus = 1 and po_item.item_code = %s and po.name != %s and
154 po.name = po_item.parent
155 order by po.transaction_date desc, po.name desc
156 limit 1""", (item_code, doc_name), as_dict=1)
157
158 # get last purchase receipt item details
159 last_purchase_receipt = webnotes.conn.sql("""\
160 select pr.name, pr.posting_date, pr.posting_time, pr.conversion_rate,
161 pr_item.conversion_factor, pr_item.purchase_ref_rate, pr_item.discount_rate,
162 pr_item.purchase_rate
163 from `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pr_item
164 where pr.docstatus = 1 and pr_item.item_code = %s and pr.name != %s and
165 pr.name = pr_item.parent
166 order by pr.posting_date desc, pr.posting_time desc, pr.name desc
167 limit 1""", (item_code, doc_name), as_dict=1)
168
169 purchase_order_date = getdate(last_purchase_order and last_purchase_order[0].transaction_date \
170 or "1900-01-01")
171 purchase_receipt_date = getdate(last_purchase_receipt and \
172 last_purchase_receipt[0].posting_date or "1900-01-01")
173
174 if (purchase_order_date > purchase_receipt_date) or \
175 (last_purchase_order and not last_purchase_receipt):
176 # use purchase order
177 last_purchase = last_purchase_order[0]
178 purchase_date = purchase_order_date
179
180 elif (purchase_receipt_date > purchase_order_date) or \
181 (last_purchase_receipt and not last_purchase_order):
182 # use purchase receipt
183 last_purchase = last_purchase_receipt[0]
184 purchase_date = purchase_receipt_date
185
186 else:
187 return webnotes._dict()
188
189 conversion_factor = flt(last_purchase.conversion_factor)
190 out = webnotes._dict({
191 "purchase_ref_rate": flt(last_purchase.purchase_ref_rate) / conversion_factor,
192 "purchase_rate": flt(last_purchase.purchase_rate) / conversion_factor,
193 "discount_rate": flt(last_purchase.discount_rate),
194 "purchase_date": purchase_date
195 })
196
197 conversion_rate = flt(conversion_rate) or 1.0
198 out.update({
199 "import_ref_rate": out.purchase_ref_rate / conversion_rate,
200 "import_rate": out.purchase_rate / conversion_rate,
201 "rate": out.purchase_rate
202 })
203
Anand Doshifc777182013-05-27 19:29:07 +0530204 return out
205
206@webnotes.whitelist()
207def get_conversion_factor(item_code, uom):
208 return {"conversion_factor": webnotes.conn.get_value("UOM Conversion Detail",
209 {"parent": item_code, "uom": uom})}
210
211@webnotes.whitelist()
212def get_projected_qty(item_code, warehouse):
213 return webnotes.conn.get_value("Bin", {"item_code": item_code,
214 "warehouse": warehouse}, "projected_qty")