blob: 385a8c7b51693079b894eb9099387024c9351391 [file] [log] [blame]
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
2# License: GNU General Public License v3. See license.txt
Anand Doshi756dca72013-01-15 18:39:21 +05303
4from __future__ import unicode_literals
5import webnotes
Anand Doshi1dde46a2013-05-15 21:15:57 +05306from webnotes import msgprint, _
Anand Doshi756dca72013-01-15 18:39:21 +05307from webnotes.utils import getdate, flt, add_days
8import json
9
10@webnotes.whitelist()
11def get_item_details(args):
12 """
13 args = {
14 "doctype": "",
15 "docname": "",
16 "item_code": "",
17 "warehouse": None,
18 "supplier": None,
19 "transaction_date": None,
Anand Doshi1dde46a2013-05-15 21:15:57 +053020 "conversion_rate": 1.0,
Rushabh Mehta4a404e92013-08-09 18:11:35 +053021 "buying_price_list": None,
Anand Doshi1dde46a2013-05-15 21:15:57 +053022 "price_list_currency": None,
23 "plc_conversion_rate": 1.0,
24 "is_subcontracted": "Yes" / "No"
Anand Doshi756dca72013-01-15 18:39:21 +053025 }
26 """
27 if isinstance(args, basestring):
28 args = json.loads(args)
29
30 args = webnotes._dict(args)
31
Anand Doshi1dde46a2013-05-15 21:15:57 +053032 item_bean = webnotes.bean("Item", args.item_code)
33 item = item_bean.doc
Anand Doshi756dca72013-01-15 18:39:21 +053034
Anand Doshi1dde46a2013-05-15 21:15:57 +053035 _validate_item_details(args, item)
Anand Doshi756dca72013-01-15 18:39:21 +053036
Anand Doshi1dde46a2013-05-15 21:15:57 +053037 out = _get_basic_details(args, item_bean)
Anand Doshi756dca72013-01-15 18:39:21 +053038
Anand Doshi1dde46a2013-05-15 21:15:57 +053039 out.supplier_part_no = _get_supplier_part_no(args, item_bean)
Anand Doshi756dca72013-01-15 18:39:21 +053040
Rushabh Mehta1bcc19e2013-07-01 11:46:07 +053041 if not out.warehouse:
42 out.warehouse = item_bean.doc.default_warehouse
43
Anand Doshi756dca72013-01-15 18:39:21 +053044 if out.warehouse:
Anand Doshifc777182013-05-27 19:29:07 +053045 out.projected_qty = get_projected_qty(item.name, out.warehouse)
Anand Doshi756dca72013-01-15 18:39:21 +053046
47 if args.transaction_date and item.lead_time_days:
48 out.schedule_date = out.lead_time_date = add_days(args.transaction_date,
49 item.lead_time_days)
50
Anand Doshif3096132013-05-21 19:35:06 +053051 meta = webnotes.get_doctype(args.doctype)
Anand Doshi756dca72013-01-15 18:39:21 +053052
Anand Doshif3096132013-05-21 19:35:06 +053053 if meta.get_field("currency"):
54 out.purchase_ref_rate = out.discount_rate = out.purchase_rate = \
55 out.import_ref_rate = out.import_rate = 0.0
56 out.update(_get_price_list_rate(args, item_bean, meta))
Anand Doshi3543f302013-05-24 19:25:01 +053057
58 if args.doctype == "Material Request":
59 out.min_order_qty = flt(item.min_order_qty)
60
Anand Doshi756dca72013-01-15 18:39:21 +053061 return out
Anand Doshi1dde46a2013-05-15 21:15:57 +053062
63def _get_basic_details(args, item_bean):
64 item = item_bean.doc
65
66 out = webnotes._dict({
67 "description": item.description_html or item.description,
Akhilesh Darjeee9470812013-09-19 19:09:15 +053068 "qty": 1.0,
Anand Doshi1dde46a2013-05-15 21:15:57 +053069 "uom": item.stock_uom,
70 "conversion_factor": 1.0,
71 "warehouse": args.warehouse or item.default_warehouse,
72 "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in
73 item_bean.doclist.get({"parentfield": "item_tax"})))),
74 "batch_no": None,
Rushabh Mehtad9e70702013-07-08 18:01:46 +053075 "expense_head": item.purchase_account \
76 or webnotes.conn.get_value("Company", args.company, "default_expense_account"),
Anand Doshi1dde46a2013-05-15 21:15:57 +053077 "cost_center": item.cost_center
78 })
79
80 for fieldname in ("item_name", "item_group", "brand", "stock_uom"):
81 out[fieldname] = item.fields.get(fieldname)
82
83 return out
84
Anand Doshifc777182013-05-27 19:29:07 +053085def _get_price_list_rate(args, item_bean, meta):
Anand Doshif3096132013-05-21 19:35:06 +053086 from utilities.transaction_base import validate_currency
87 item = item_bean.doc
88 out = webnotes._dict()
89
90 # try fetching from price list
Rushabh Mehta4a404e92013-08-09 18:11:35 +053091 if args.buying_price_list and args.price_list_currency:
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +053092 price_list_rate = webnotes.conn.sql("""select ref_rate from `tabItem Price`
93 where price_list=%s and item_code=%s and buying_or_selling='Buying'""",
94 (args.buying_price_list, args.item_code), as_dict=1)
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +053095
Anand Doshif3096132013-05-21 19:35:06 +053096 if price_list_rate:
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +053097 from utilities.transaction_base import validate_currency
98 validate_currency(args, item_bean.doc, meta)
99
Nabin Haitdd266faf2013-09-23 13:00:59 +0530100 out.import_ref_rate = flt(price_list_rate[0].ref_rate) * \
101 flt(args.plc_conversion_rate) / flt(args.conversion_rate)
Anand Doshif3096132013-05-21 19:35:06 +0530102
103 # if not found, fetch from last purchase transaction
Anand Doshifc777182013-05-27 19:29:07 +0530104 if not out.import_ref_rate:
Anand Doshif3096132013-05-21 19:35:06 +0530105 last_purchase = get_last_purchase_details(item.name, args.docname, args.conversion_rate)
106 if last_purchase:
107 out.update(last_purchase)
108
Anand Doshifc777182013-05-27 19:29:07 +0530109 if out.import_ref_rate or out.import_rate:
Anand Doshif3096132013-05-21 19:35:06 +0530110 validate_currency(args, item, meta)
111
112 return out
113
Anand Doshi1dde46a2013-05-15 21:15:57 +0530114def _get_supplier_part_no(args, item_bean):
115 item_supplier = item_bean.doclist.get({"parentfield": "item_supplier_details",
116 "supplier": args.supplier})
117
118 return item_supplier and item_supplier[0].supplier_part_no or None
Anand Doshi756dca72013-01-15 18:39:21 +0530119
Anand Doshi1dde46a2013-05-15 21:15:57 +0530120def _validate_item_details(args, item):
121 from utilities.transaction_base import validate_item_fetch
122 validate_item_fetch(args, item)
123
124 # validate if purchase item or subcontracted item
125 if item.is_purchase_item != "Yes":
126 msgprint(_("Item") + (" %s: " % item.name) + _("not a purchase item"),
127 raise_exception=True)
128
129 if args.is_subcontracted == "Yes" and item.is_sub_contracted_item != "Yes":
130 msgprint(_("Item") + (" %s: " % item.name) +
131 _("not a sub-contracted item.") +
132 _("Please select a sub-contracted item or do not sub-contract the transaction."),
133 raise_exception=True)
Anand Doshi756dca72013-01-15 18:39:21 +0530134
135def get_last_purchase_details(item_code, doc_name, conversion_rate=1.0):
136 """returns last purchase details in stock uom"""
137 # get last purchase order item details
138 last_purchase_order = webnotes.conn.sql("""\
139 select po.name, po.transaction_date, po.conversion_rate,
140 po_item.conversion_factor, po_item.purchase_ref_rate,
141 po_item.discount_rate, po_item.purchase_rate
142 from `tabPurchase Order` po, `tabPurchase Order Item` po_item
143 where po.docstatus = 1 and po_item.item_code = %s and po.name != %s and
144 po.name = po_item.parent
145 order by po.transaction_date desc, po.name desc
146 limit 1""", (item_code, doc_name), as_dict=1)
147
148 # get last purchase receipt item details
149 last_purchase_receipt = webnotes.conn.sql("""\
150 select pr.name, pr.posting_date, pr.posting_time, pr.conversion_rate,
151 pr_item.conversion_factor, pr_item.purchase_ref_rate, pr_item.discount_rate,
152 pr_item.purchase_rate
153 from `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pr_item
154 where pr.docstatus = 1 and pr_item.item_code = %s and pr.name != %s and
155 pr.name = pr_item.parent
156 order by pr.posting_date desc, pr.posting_time desc, pr.name desc
157 limit 1""", (item_code, doc_name), as_dict=1)
158
159 purchase_order_date = getdate(last_purchase_order and last_purchase_order[0].transaction_date \
160 or "1900-01-01")
161 purchase_receipt_date = getdate(last_purchase_receipt and \
162 last_purchase_receipt[0].posting_date or "1900-01-01")
163
164 if (purchase_order_date > purchase_receipt_date) or \
165 (last_purchase_order and not last_purchase_receipt):
166 # use purchase order
167 last_purchase = last_purchase_order[0]
168 purchase_date = purchase_order_date
169
170 elif (purchase_receipt_date > purchase_order_date) or \
171 (last_purchase_receipt and not last_purchase_order):
172 # use purchase receipt
173 last_purchase = last_purchase_receipt[0]
174 purchase_date = purchase_receipt_date
175
176 else:
177 return webnotes._dict()
178
179 conversion_factor = flt(last_purchase.conversion_factor)
180 out = webnotes._dict({
181 "purchase_ref_rate": flt(last_purchase.purchase_ref_rate) / conversion_factor,
182 "purchase_rate": flt(last_purchase.purchase_rate) / conversion_factor,
183 "discount_rate": flt(last_purchase.discount_rate),
184 "purchase_date": purchase_date
185 })
186
187 conversion_rate = flt(conversion_rate) or 1.0
188 out.update({
189 "import_ref_rate": out.purchase_ref_rate / conversion_rate,
190 "import_rate": out.purchase_rate / conversion_rate,
191 "rate": out.purchase_rate
192 })
193
Anand Doshifc777182013-05-27 19:29:07 +0530194 return out
195
196@webnotes.whitelist()
197def get_conversion_factor(item_code, uom):
198 return {"conversion_factor": webnotes.conn.get_value("UOM Conversion Detail",
Nabin Hait9481e032013-07-22 17:28:11 +0530199 {"parent": item_code, "uom": uom}, "conversion_factor")}
Anand Doshifc777182013-05-27 19:29:07 +0530200
201@webnotes.whitelist()
202def get_projected_qty(item_code, warehouse):
203 return webnotes.conn.get_value("Bin", {"item_code": item_code,
204 "warehouse": warehouse}, "projected_qty")