blob: 33326d9e59c89a48750dfe189d8a648fbb7b0907 [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,
68 "qty": 0.0,
69 "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:
Anand Doshif3096132013-05-21 19:35:06 +053092 price_list_rate = item_bean.doclist.get({
93 "parentfield": "ref_rate_details",
Rushabh Mehta4a404e92013-08-09 18:11:35 +053094 "price_list": args.buying_price_list,
Anand Doshif3096132013-05-21 19:35:06 +053095 "ref_currency": args.price_list_currency,
Anand Doshi060d9242013-06-12 17:40:36 +053096 "buying_or_selling": "Buying"})
Anand Doshif3096132013-05-21 19:35:06 +053097 if price_list_rate:
Anand Doshifc777182013-05-27 19:29:07 +053098 out.import_ref_rate = \
99 flt(price_list_rate[0].ref_rate * args.plc_conversion_rate / args.conversion_rate)
Anand Doshif3096132013-05-21 19:35:06 +0530100
101 # if not found, fetch from last purchase transaction
Anand Doshifc777182013-05-27 19:29:07 +0530102 if not out.import_ref_rate:
Anand Doshif3096132013-05-21 19:35:06 +0530103 last_purchase = get_last_purchase_details(item.name, args.docname, args.conversion_rate)
104 if last_purchase:
105 out.update(last_purchase)
106
Anand Doshifc777182013-05-27 19:29:07 +0530107 if out.import_ref_rate or out.import_rate:
Anand Doshif3096132013-05-21 19:35:06 +0530108 validate_currency(args, item, meta)
109
110 return out
111
Anand Doshi1dde46a2013-05-15 21:15:57 +0530112def _get_supplier_part_no(args, item_bean):
113 item_supplier = item_bean.doclist.get({"parentfield": "item_supplier_details",
114 "supplier": args.supplier})
115
116 return item_supplier and item_supplier[0].supplier_part_no or None
Anand Doshi756dca72013-01-15 18:39:21 +0530117
Anand Doshi1dde46a2013-05-15 21:15:57 +0530118def _validate_item_details(args, item):
119 from utilities.transaction_base import validate_item_fetch
120 validate_item_fetch(args, item)
121
122 # validate if purchase item or subcontracted item
123 if item.is_purchase_item != "Yes":
124 msgprint(_("Item") + (" %s: " % item.name) + _("not a purchase item"),
125 raise_exception=True)
126
127 if args.is_subcontracted == "Yes" and item.is_sub_contracted_item != "Yes":
128 msgprint(_("Item") + (" %s: " % item.name) +
129 _("not a sub-contracted item.") +
130 _("Please select a sub-contracted item or do not sub-contract the transaction."),
131 raise_exception=True)
Anand Doshi756dca72013-01-15 18:39:21 +0530132
133def get_last_purchase_details(item_code, doc_name, conversion_rate=1.0):
134 """returns last purchase details in stock uom"""
135 # get last purchase order item details
136 last_purchase_order = webnotes.conn.sql("""\
137 select po.name, po.transaction_date, po.conversion_rate,
138 po_item.conversion_factor, po_item.purchase_ref_rate,
139 po_item.discount_rate, po_item.purchase_rate
140 from `tabPurchase Order` po, `tabPurchase Order Item` po_item
141 where po.docstatus = 1 and po_item.item_code = %s and po.name != %s and
142 po.name = po_item.parent
143 order by po.transaction_date desc, po.name desc
144 limit 1""", (item_code, doc_name), as_dict=1)
145
146 # get last purchase receipt item details
147 last_purchase_receipt = webnotes.conn.sql("""\
148 select pr.name, pr.posting_date, pr.posting_time, pr.conversion_rate,
149 pr_item.conversion_factor, pr_item.purchase_ref_rate, pr_item.discount_rate,
150 pr_item.purchase_rate
151 from `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pr_item
152 where pr.docstatus = 1 and pr_item.item_code = %s and pr.name != %s and
153 pr.name = pr_item.parent
154 order by pr.posting_date desc, pr.posting_time desc, pr.name desc
155 limit 1""", (item_code, doc_name), as_dict=1)
156
157 purchase_order_date = getdate(last_purchase_order and last_purchase_order[0].transaction_date \
158 or "1900-01-01")
159 purchase_receipt_date = getdate(last_purchase_receipt and \
160 last_purchase_receipt[0].posting_date or "1900-01-01")
161
162 if (purchase_order_date > purchase_receipt_date) or \
163 (last_purchase_order and not last_purchase_receipt):
164 # use purchase order
165 last_purchase = last_purchase_order[0]
166 purchase_date = purchase_order_date
167
168 elif (purchase_receipt_date > purchase_order_date) or \
169 (last_purchase_receipt and not last_purchase_order):
170 # use purchase receipt
171 last_purchase = last_purchase_receipt[0]
172 purchase_date = purchase_receipt_date
173
174 else:
175 return webnotes._dict()
176
177 conversion_factor = flt(last_purchase.conversion_factor)
178 out = webnotes._dict({
179 "purchase_ref_rate": flt(last_purchase.purchase_ref_rate) / conversion_factor,
180 "purchase_rate": flt(last_purchase.purchase_rate) / conversion_factor,
181 "discount_rate": flt(last_purchase.discount_rate),
182 "purchase_date": purchase_date
183 })
184
185 conversion_rate = flt(conversion_rate) or 1.0
186 out.update({
187 "import_ref_rate": out.purchase_ref_rate / conversion_rate,
188 "import_rate": out.purchase_rate / conversion_rate,
189 "rate": out.purchase_rate
190 })
191
Anand Doshifc777182013-05-27 19:29:07 +0530192 return out
193
194@webnotes.whitelist()
195def get_conversion_factor(item_code, uom):
196 return {"conversion_factor": webnotes.conn.get_value("UOM Conversion Detail",
Nabin Hait9481e032013-07-22 17:28:11 +0530197 {"parent": item_code, "uom": uom}, "conversion_factor")}
Anand Doshifc777182013-05-27 19:29:07 +0530198
199@webnotes.whitelist()
200def get_projected_qty(item_code, warehouse):
201 return webnotes.conn.get_value("Bin", {"item_code": item_code,
202 "warehouse": warehouse}, "projected_qty")