blob: 54197b49fb309081e9b4532ff19ed391742bf158 [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
62 # set zero
63 out.purchase_ref_rate = out.discount_rate = out.purchase_rate = \
64 out.import_ref_rate = out.import_rate = 0.0
65
Nabin Hait396f47d2013-01-18 16:29:30 +053066 if args.doctype in ["Purchase Order", "Purchase Invoice", "Purchase Receipt",
67 "Supplier Quotation"]:
Anand Doshi756dca72013-01-15 18:39:21 +053068 # try fetching from price list
Anand Doshif8f0c0d2013-01-17 20:20:56 +053069 if args.price_list_name and args.price_list_currency:
Anand Doshi1dde46a2013-05-15 21:15:57 +053070 rates_as_per_price_list = get_rates_as_per_price_list(args, item_bean.doclist)
Anand Doshi756dca72013-01-15 18:39:21 +053071 if rates_as_per_price_list:
72 out.update(rates_as_per_price_list)
73
74 # if not found, fetch from last purchase transaction
75 if not out.purchase_rate:
76 last_purchase = get_last_purchase_details(item.name, args.docname, args.conversion_rate)
77 if last_purchase:
78 out.update(last_purchase)
79
80 return out
Anand Doshi1dde46a2013-05-15 21:15:57 +053081
82def _get_basic_details(args, item_bean):
83 item = item_bean.doc
84
85 out = webnotes._dict({
86 "description": item.description_html or item.description,
87 "qty": 0.0,
88 "uom": item.stock_uom,
89 "conversion_factor": 1.0,
90 "warehouse": args.warehouse or item.default_warehouse,
91 "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in
92 item_bean.doclist.get({"parentfield": "item_tax"})))),
93 "batch_no": None,
94 "expense_head": item.purchase_account,
95 "cost_center": item.cost_center
96 })
97
98 for fieldname in ("item_name", "item_group", "brand", "stock_uom"):
99 out[fieldname] = item.fields.get(fieldname)
100
101 return out
102
103def _get_supplier_part_no(args, item_bean):
104 item_supplier = item_bean.doclist.get({"parentfield": "item_supplier_details",
105 "supplier": args.supplier})
106
107 return item_supplier and item_supplier[0].supplier_part_no or None
Anand Doshi756dca72013-01-15 18:39:21 +0530108
109def get_rates_as_per_price_list(args, item_doclist=None):
110 if not item_doclist:
Rushabh Mehtac53231a2013-02-18 18:24:28 +0530111 item_doclist = webnotes.bean("Item", args.item_code).doclist
Anand Doshi756dca72013-01-15 18:39:21 +0530112
113 result = item_doclist.get({"parentfield": "ref_rate_details",
Nabin Hait396f47d2013-01-18 16:29:30 +0530114 "price_list_name": args.price_list_name, "ref_currency": args.price_list_currency,
115 "buying": 1})
Anand Doshi756dca72013-01-15 18:39:21 +0530116
117 if result:
118 purchase_ref_rate = flt(result[0].ref_rate) * flt(args.plc_conversion_rate)
119 conversion_rate = flt(args.conversion_rate) or 1.0
120 return webnotes._dict({
121 "purchase_ref_rate": purchase_ref_rate,
122 "purchase_rate": purchase_ref_rate,
123 "rate": purchase_ref_rate,
124 "discount_rate": 0,
125 "import_ref_rate": purchase_ref_rate / conversion_rate,
126 "import_rate": purchase_ref_rate / conversion_rate
127 })
128 else:
129 return webnotes._dict()
Anand Doshi1dde46a2013-05-15 21:15:57 +0530130
131def _validate_item_details(args, item):
132 from utilities.transaction_base import validate_item_fetch
133 validate_item_fetch(args, item)
134
135 # validate if purchase item or subcontracted item
136 if item.is_purchase_item != "Yes":
137 msgprint(_("Item") + (" %s: " % item.name) + _("not a purchase item"),
138 raise_exception=True)
139
140 if args.is_subcontracted == "Yes" and item.is_sub_contracted_item != "Yes":
141 msgprint(_("Item") + (" %s: " % item.name) +
142 _("not a sub-contracted item.") +
143 _("Please select a sub-contracted item or do not sub-contract the transaction."),
144 raise_exception=True)
Anand Doshi756dca72013-01-15 18:39:21 +0530145
146def get_last_purchase_details(item_code, doc_name, conversion_rate=1.0):
147 """returns last purchase details in stock uom"""
148 # get last purchase order item details
149 last_purchase_order = webnotes.conn.sql("""\
150 select po.name, po.transaction_date, po.conversion_rate,
151 po_item.conversion_factor, po_item.purchase_ref_rate,
152 po_item.discount_rate, po_item.purchase_rate
153 from `tabPurchase Order` po, `tabPurchase Order Item` po_item
154 where po.docstatus = 1 and po_item.item_code = %s and po.name != %s and
155 po.name = po_item.parent
156 order by po.transaction_date desc, po.name desc
157 limit 1""", (item_code, doc_name), as_dict=1)
158
159 # get last purchase receipt item details
160 last_purchase_receipt = webnotes.conn.sql("""\
161 select pr.name, pr.posting_date, pr.posting_time, pr.conversion_rate,
162 pr_item.conversion_factor, pr_item.purchase_ref_rate, pr_item.discount_rate,
163 pr_item.purchase_rate
164 from `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pr_item
165 where pr.docstatus = 1 and pr_item.item_code = %s and pr.name != %s and
166 pr.name = pr_item.parent
167 order by pr.posting_date desc, pr.posting_time desc, pr.name desc
168 limit 1""", (item_code, doc_name), as_dict=1)
169
170 purchase_order_date = getdate(last_purchase_order and last_purchase_order[0].transaction_date \
171 or "1900-01-01")
172 purchase_receipt_date = getdate(last_purchase_receipt and \
173 last_purchase_receipt[0].posting_date or "1900-01-01")
174
175 if (purchase_order_date > purchase_receipt_date) or \
176 (last_purchase_order and not last_purchase_receipt):
177 # use purchase order
178 last_purchase = last_purchase_order[0]
179 purchase_date = purchase_order_date
180
181 elif (purchase_receipt_date > purchase_order_date) or \
182 (last_purchase_receipt and not last_purchase_order):
183 # use purchase receipt
184 last_purchase = last_purchase_receipt[0]
185 purchase_date = purchase_receipt_date
186
187 else:
188 return webnotes._dict()
189
190 conversion_factor = flt(last_purchase.conversion_factor)
191 out = webnotes._dict({
192 "purchase_ref_rate": flt(last_purchase.purchase_ref_rate) / conversion_factor,
193 "purchase_rate": flt(last_purchase.purchase_rate) / conversion_factor,
194 "discount_rate": flt(last_purchase.discount_rate),
195 "purchase_date": purchase_date
196 })
197
198 conversion_rate = flt(conversion_rate) or 1.0
199 out.update({
200 "import_ref_rate": out.purchase_ref_rate / conversion_rate,
201 "import_rate": out.purchase_rate / conversion_rate,
202 "rate": out.purchase_rate
203 })
204
205 return out