blob: 952d70a602c1434eb4c1ddfda5f5a1d26885e8db [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
54 if out.warehouse:
55 out.projected_qty = webnotes.conn.get_value("Bin", {"item_code": item.name,
56 "warehouse": out.warehouse}, "projected_qty")
57
58 if args.transaction_date and item.lead_time_days:
59 out.schedule_date = out.lead_time_date = add_days(args.transaction_date,
60 item.lead_time_days)
61
Anand Doshif3096132013-05-21 19:35:06 +053062 meta = webnotes.get_doctype(args.doctype)
Anand Doshi756dca72013-01-15 18:39:21 +053063
Anand Doshif3096132013-05-21 19:35:06 +053064 if meta.get_field("currency"):
65 out.purchase_ref_rate = out.discount_rate = out.purchase_rate = \
66 out.import_ref_rate = out.import_rate = 0.0
67 out.update(_get_price_list_rate(args, item_bean, meta))
Anand Doshi756dca72013-01-15 18:39:21 +053068
69 return out
Anand Doshi1dde46a2013-05-15 21:15:57 +053070
71def _get_basic_details(args, item_bean):
72 item = item_bean.doc
73
74 out = webnotes._dict({
75 "description": item.description_html or item.description,
76 "qty": 0.0,
77 "uom": item.stock_uom,
78 "conversion_factor": 1.0,
79 "warehouse": args.warehouse or item.default_warehouse,
80 "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in
81 item_bean.doclist.get({"parentfield": "item_tax"})))),
82 "batch_no": None,
83 "expense_head": item.purchase_account,
84 "cost_center": item.cost_center
85 })
86
87 for fieldname in ("item_name", "item_group", "brand", "stock_uom"):
88 out[fieldname] = item.fields.get(fieldname)
89
90 return out
91
Anand Doshif3096132013-05-21 19:35:06 +053092def _get_price_list_rate(args, item_bean, meta=None):
93 from utilities.transaction_base import validate_currency
94 item = item_bean.doc
95 out = webnotes._dict()
96
97 # try fetching from price list
98 if args.price_list_name and args.price_list_currency:
99 price_list_rate = item_bean.doclist.get({
100 "parentfield": "ref_rate_details",
101 "price_list_name": args.price_list_name,
102 "ref_currency": args.price_list_currency,
103 "buying": 1})
104 if price_list_rate:
105 out.purchase_ref_rate = flt(price_list_rate[0].ref_rate) * flt(args.plc_conversion_rate)
106
107 # if not found, fetch from last purchase transaction
108 if not out.purchase_ref_rate:
109 last_purchase = get_last_purchase_details(item.name, args.docname, args.conversion_rate)
110 if last_purchase:
111 out.update(last_purchase)
112
113 if out.purchase_ref_rate or out.purchase_rate or out.rate:
114 validate_currency(args, item, meta)
115
116 return out
117
Anand Doshi1dde46a2013-05-15 21:15:57 +0530118def _get_supplier_part_no(args, item_bean):
119 item_supplier = item_bean.doclist.get({"parentfield": "item_supplier_details",
120 "supplier": args.supplier})
121
122 return item_supplier and item_supplier[0].supplier_part_no or None
Anand Doshi756dca72013-01-15 18:39:21 +0530123
Anand Doshi1dde46a2013-05-15 21:15:57 +0530124def _validate_item_details(args, item):
125 from utilities.transaction_base import validate_item_fetch
126 validate_item_fetch(args, item)
127
128 # validate if purchase item or subcontracted item
129 if item.is_purchase_item != "Yes":
130 msgprint(_("Item") + (" %s: " % item.name) + _("not a purchase item"),
131 raise_exception=True)
132
133 if args.is_subcontracted == "Yes" and item.is_sub_contracted_item != "Yes":
134 msgprint(_("Item") + (" %s: " % item.name) +
135 _("not a sub-contracted item.") +
136 _("Please select a sub-contracted item or do not sub-contract the transaction."),
137 raise_exception=True)
Anand Doshi756dca72013-01-15 18:39:21 +0530138
139def get_last_purchase_details(item_code, doc_name, conversion_rate=1.0):
140 """returns last purchase details in stock uom"""
141 # get last purchase order item details
142 last_purchase_order = webnotes.conn.sql("""\
143 select po.name, po.transaction_date, po.conversion_rate,
144 po_item.conversion_factor, po_item.purchase_ref_rate,
145 po_item.discount_rate, po_item.purchase_rate
146 from `tabPurchase Order` po, `tabPurchase Order Item` po_item
147 where po.docstatus = 1 and po_item.item_code = %s and po.name != %s and
148 po.name = po_item.parent
149 order by po.transaction_date desc, po.name desc
150 limit 1""", (item_code, doc_name), as_dict=1)
151
152 # get last purchase receipt item details
153 last_purchase_receipt = webnotes.conn.sql("""\
154 select pr.name, pr.posting_date, pr.posting_time, pr.conversion_rate,
155 pr_item.conversion_factor, pr_item.purchase_ref_rate, pr_item.discount_rate,
156 pr_item.purchase_rate
157 from `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pr_item
158 where pr.docstatus = 1 and pr_item.item_code = %s and pr.name != %s and
159 pr.name = pr_item.parent
160 order by pr.posting_date desc, pr.posting_time desc, pr.name desc
161 limit 1""", (item_code, doc_name), as_dict=1)
162
163 purchase_order_date = getdate(last_purchase_order and last_purchase_order[0].transaction_date \
164 or "1900-01-01")
165 purchase_receipt_date = getdate(last_purchase_receipt and \
166 last_purchase_receipt[0].posting_date or "1900-01-01")
167
168 if (purchase_order_date > purchase_receipt_date) or \
169 (last_purchase_order and not last_purchase_receipt):
170 # use purchase order
171 last_purchase = last_purchase_order[0]
172 purchase_date = purchase_order_date
173
174 elif (purchase_receipt_date > purchase_order_date) or \
175 (last_purchase_receipt and not last_purchase_order):
176 # use purchase receipt
177 last_purchase = last_purchase_receipt[0]
178 purchase_date = purchase_receipt_date
179
180 else:
181 return webnotes._dict()
182
183 conversion_factor = flt(last_purchase.conversion_factor)
184 out = webnotes._dict({
185 "purchase_ref_rate": flt(last_purchase.purchase_ref_rate) / conversion_factor,
186 "purchase_rate": flt(last_purchase.purchase_rate) / conversion_factor,
187 "discount_rate": flt(last_purchase.discount_rate),
188 "purchase_date": purchase_date
189 })
190
191 conversion_rate = flt(conversion_rate) or 1.0
192 out.update({
193 "import_ref_rate": out.purchase_ref_rate / conversion_rate,
194 "import_rate": out.purchase_rate / conversion_rate,
195 "rate": out.purchase_rate
196 })
197
198 return out