blob: 0d9c8fa8385472de96da86a95deb5b17f84f47b8 [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302# 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, _
Nabin Haitca471f42013-11-20 13:14:12 +05307from webnotes.utils import getdate, flt, add_days, cstr
Anand Doshi756dca72013-01-15 18:39:21 +05308import 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 validate_currency(args, item_bean.doc, meta)
98
Nabin Haitdd266faf2013-09-23 13:00:59 +053099 out.import_ref_rate = flt(price_list_rate[0].ref_rate) * \
100 flt(args.plc_conversion_rate) / flt(args.conversion_rate)
Anand Doshif3096132013-05-21 19:35:06 +0530101
102 # if not found, fetch from last purchase transaction
Anand Doshifc777182013-05-27 19:29:07 +0530103 if not out.import_ref_rate:
Anand Doshif3096132013-05-21 19:35:06 +0530104 last_purchase = get_last_purchase_details(item.name, args.docname, args.conversion_rate)
105 if last_purchase:
106 out.update(last_purchase)
107
Anand Doshifc777182013-05-27 19:29:07 +0530108 if out.import_ref_rate or out.import_rate:
Anand Doshif3096132013-05-21 19:35:06 +0530109 validate_currency(args, item, meta)
110
111 return out
112
Anand Doshi1dde46a2013-05-15 21:15:57 +0530113def _get_supplier_part_no(args, item_bean):
114 item_supplier = item_bean.doclist.get({"parentfield": "item_supplier_details",
115 "supplier": args.supplier})
116
117 return item_supplier and item_supplier[0].supplier_part_no or None
Anand Doshi756dca72013-01-15 18:39:21 +0530118
Anand Doshi1dde46a2013-05-15 21:15:57 +0530119def _validate_item_details(args, item):
120 from utilities.transaction_base import validate_item_fetch
121 validate_item_fetch(args, item)
122
123 # validate if purchase item or subcontracted item
124 if item.is_purchase_item != "Yes":
125 msgprint(_("Item") + (" %s: " % item.name) + _("not a purchase item"),
126 raise_exception=True)
127
128 if args.is_subcontracted == "Yes" and item.is_sub_contracted_item != "Yes":
129 msgprint(_("Item") + (" %s: " % item.name) +
130 _("not a sub-contracted item.") +
131 _("Please select a sub-contracted item or do not sub-contract the transaction."),
132 raise_exception=True)
Anand Doshi756dca72013-01-15 18:39:21 +0530133
Nabin Haitca471f42013-11-20 13:14:12 +0530134def get_last_purchase_details(item_code, doc_name=None, conversion_rate=1.0):
Anand Doshi756dca72013-01-15 18:39:21 +0530135 """returns last purchase details in stock uom"""
136 # get last purchase order item details
137 last_purchase_order = webnotes.conn.sql("""\
138 select po.name, po.transaction_date, po.conversion_rate,
139 po_item.conversion_factor, po_item.purchase_ref_rate,
140 po_item.discount_rate, po_item.purchase_rate
141 from `tabPurchase Order` po, `tabPurchase Order Item` po_item
142 where po.docstatus = 1 and po_item.item_code = %s and po.name != %s and
143 po.name = po_item.parent
144 order by po.transaction_date desc, po.name desc
Nabin Haitca471f42013-11-20 13:14:12 +0530145 limit 1""", (item_code, cstr(doc_name)), as_dict=1)
Anand Doshi756dca72013-01-15 18:39:21 +0530146
147 # get last purchase receipt item details
148 last_purchase_receipt = webnotes.conn.sql("""\
149 select pr.name, pr.posting_date, pr.posting_time, pr.conversion_rate,
150 pr_item.conversion_factor, pr_item.purchase_ref_rate, pr_item.discount_rate,
151 pr_item.purchase_rate
152 from `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pr_item
153 where pr.docstatus = 1 and pr_item.item_code = %s and pr.name != %s and
154 pr.name = pr_item.parent
155 order by pr.posting_date desc, pr.posting_time desc, pr.name desc
Nabin Haitca471f42013-11-20 13:14:12 +0530156 limit 1""", (item_code, cstr(doc_name)), as_dict=1)
Anand Doshi756dca72013-01-15 18:39:21 +0530157
158 purchase_order_date = getdate(last_purchase_order and last_purchase_order[0].transaction_date \
159 or "1900-01-01")
160 purchase_receipt_date = getdate(last_purchase_receipt and \
161 last_purchase_receipt[0].posting_date or "1900-01-01")
162
163 if (purchase_order_date > purchase_receipt_date) or \
164 (last_purchase_order and not last_purchase_receipt):
165 # use purchase order
166 last_purchase = last_purchase_order[0]
167 purchase_date = purchase_order_date
168
169 elif (purchase_receipt_date > purchase_order_date) or \
170 (last_purchase_receipt and not last_purchase_order):
171 # use purchase receipt
172 last_purchase = last_purchase_receipt[0]
173 purchase_date = purchase_receipt_date
174
175 else:
176 return webnotes._dict()
177
178 conversion_factor = flt(last_purchase.conversion_factor)
179 out = webnotes._dict({
180 "purchase_ref_rate": flt(last_purchase.purchase_ref_rate) / conversion_factor,
181 "purchase_rate": flt(last_purchase.purchase_rate) / conversion_factor,
182 "discount_rate": flt(last_purchase.discount_rate),
183 "purchase_date": purchase_date
184 })
185
186 conversion_rate = flt(conversion_rate) or 1.0
187 out.update({
188 "import_ref_rate": out.purchase_ref_rate / conversion_rate,
189 "import_rate": out.purchase_rate / conversion_rate,
190 "rate": out.purchase_rate
191 })
192
Anand Doshifc777182013-05-27 19:29:07 +0530193 return out
194
195@webnotes.whitelist()
196def get_conversion_factor(item_code, uom):
197 return {"conversion_factor": webnotes.conn.get_value("UOM Conversion Detail",
Nabin Hait9481e032013-07-22 17:28:11 +0530198 {"parent": item_code, "uom": uom}, "conversion_factor")}
Anand Doshifc777182013-05-27 19:29:07 +0530199
200@webnotes.whitelist()
201def get_projected_qty(item_code, warehouse):
202 return webnotes.conn.get_value("Bin", {"item_code": item_code,
203 "warehouse": warehouse}, "projected_qty")