blob: f211caec5a7d299f7f3e53cd58ba32b97c819f19 [file] [log] [blame]
Nabin Hait436f5262014-02-10 14:47:54 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
2# License: GNU General Public License v3. See license.txt
3
4from __future__ import unicode_literals
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05305import frappe
6from frappe import _, throw
7from frappe.utils import flt, cint, add_days
Nabin Hait436f5262014-02-10 14:47:54 +05308import json
9
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053010@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +053011def get_item_details(args):
12 """
13 args = {
14 "item_code": "",
15 "warehouse": None,
16 "customer": "",
17 "conversion_rate": 1.0,
18 "selling_price_list": None,
19 "price_list_currency": None,
20 "plc_conversion_rate": 1.0
21 "doctype": "",
22 "docname": "",
23 "supplier": None,
24 "transaction_date": None,
25 "conversion_rate": 1.0,
26 "buying_price_list": None,
27 "is_subcontracted": "Yes" / "No",
28 "transaction_type": "selling"
29 }
30 """
31
32 if isinstance(args, basestring):
33 args = json.loads(args)
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053034 args = frappe._dict(args)
Nabin Hait436f5262014-02-10 14:47:54 +053035
Nabin Hait0aa71a52014-02-11 16:14:52 +053036 if not args.get("transaction_type"):
Nabin Hait9d1f0772014-02-19 17:43:24 +053037 args.transaction_type = "buying" if args.get("supplier") else "selling"
Nabin Hait139dc7b2014-02-12 14:53:18 +053038
39 if not args.get("price_list"):
40 args.price_list = args.get("selling_price_list") or args.get("buying_price_list")
Nabin Hait0aa71a52014-02-11 16:14:52 +053041
Nabin Hait436f5262014-02-10 14:47:54 +053042 if args.barcode:
43 args.item_code = get_item_code(barcode=args.barcode)
44 elif not args.item_code and args.serial_no:
45 args.item_code = get_item_code(serial_no=args.serial_no)
46
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053047 item_bean = frappe.bean("Item", args.item_code)
Nabin Hait436f5262014-02-10 14:47:54 +053048 item = item_bean.doc
49
50 validate_item_details(args, item)
51
52 out = get_basic_details(args, item_bean)
53
54 get_party_item_code(args, item_bean, out)
55
56 if out.get("warehouse"):
57 out.update(get_available_qty(args.item_code, out.warehouse))
58 out.update(get_projected_qty(item.name, out.warehouse))
59
Nabin Hait436f5262014-02-10 14:47:54 +053060 get_price_list_rate(args, item_bean, out)
61
Nabin Hait139dc7b2014-02-12 14:53:18 +053062 out.update(get_item_discount(out.item_group, args.customer))
Nabin Hait436f5262014-02-10 14:47:54 +053063
64 if args.transaction_type == "selling" and cint(args.is_pos):
65 out.update(get_pos_settings_item_details(args.company, args))
66
Nabin Hait139dc7b2014-02-12 14:53:18 +053067 if args.get("doctype") in ("Sales Invoice", "Delivery Note"):
Nabin Hait436f5262014-02-10 14:47:54 +053068 if item_bean.doc.has_serial_no == "Yes" and not args.serial_no:
Nabin Hait0aa71a52014-02-11 16:14:52 +053069 out.serial_no = get_serial_nos_by_fifo(args, item_bean)
Nabin Hait139dc7b2014-02-12 14:53:18 +053070
71 if args.transaction_date and item.lead_time_days:
72 out.schedule_date = out.lead_time_date = add_days(args.transaction_date,
73 item.lead_time_days)
Nabin Hait436f5262014-02-10 14:47:54 +053074
75 return out
76
Nabin Hait436f5262014-02-10 14:47:54 +053077def get_item_code(barcode=None, serial_no=None):
78 if barcode:
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053079 item_code = frappe.conn.get_value("Item", {"barcode": barcode})
Nabin Hait436f5262014-02-10 14:47:54 +053080 elif serial_no:
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053081 item_code = frappe.conn.get_value("Serial No", serial_no, "item_code")
Nabin Hait436f5262014-02-10 14:47:54 +053082
83 if not item_code:
84 throw(_("No Item found with ") + _("Barcode") if barcode else _("Serial No") +
85 ": %s" % (barcode or serial_no))
86
87 return item_code
88
89def validate_item_details(args, item):
90 if not args.company:
91 throw(_("Please specify Company"))
92
93 from erpnext.stock.doctype.item.item import validate_end_of_life
94 validate_end_of_life(item.name, item.end_of_life)
95
96 if args.transaction_type == "selling":
97 # validate if sales item or service item
Nabin Hait139dc7b2014-02-12 14:53:18 +053098 if args.get("order_type") == "Maintenance":
Nabin Hait436f5262014-02-10 14:47:54 +053099 if item.is_service_item != "Yes":
100 throw(_("Item") + (" %s: " % item.name) +
101 _("not a service item.") +
102 _("Please select a service item or change the order type to Sales."))
103
104 elif item.is_sales_item != "Yes":
105 throw(_("Item") + (" %s: " % item.name) + _("not a sales item"))
106
107 elif args.transaction_type == "buying":
108 # validate if purchase item or subcontracted item
109 if item.is_purchase_item != "Yes":
110 throw(_("Item") + (" %s: " % item.name) + _("not a purchase item"))
111
Nabin Hait139dc7b2014-02-12 14:53:18 +0530112 if args.get("is_subcontracted") == "Yes" and item.is_sub_contracted_item != "Yes":
Nabin Hait436f5262014-02-10 14:47:54 +0530113 throw(_("Item") + (" %s: " % item.name) +
114 _("not a sub-contracted item.") +
115 _("Please select a sub-contracted item or do not sub-contract the transaction."))
116
117def get_basic_details(args, item_bean):
118 item = item_bean.doc
119
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530120 from frappe.defaults import get_user_default_as_list
Nabin Hait436f5262014-02-10 14:47:54 +0530121 user_default_warehouse_list = get_user_default_as_list('warehouse')
122 user_default_warehouse = user_default_warehouse_list[0] \
123 if len(user_default_warehouse_list)==1 else ""
Nabin Hait36028bf2014-02-12 19:09:28 +0530124
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530125 out = frappe._dict({
Nabin Hait139dc7b2014-02-12 14:53:18 +0530126 "item_code": item.name,
127 "item_name": item.item_name,
128 "description": item.description_html or item.description,
129 "warehouse": user_default_warehouse or args.warehouse or item.default_warehouse,
Nabin Haiteba1bdb2014-02-12 16:04:17 +0530130 "income_account": item.income_account or args.income_account \
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530131 or frappe.conn.get_value("Company", args.company, "default_income_account"),
Nabin Hait139dc7b2014-02-12 14:53:18 +0530132 "expense_account": item.expense_account or args.expense_account \
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530133 or frappe.conn.get_value("Company", args.company, "default_expense_account"),
Nabin Hait139dc7b2014-02-12 14:53:18 +0530134 "cost_center": item.selling_cost_center \
Nabin Hait36028bf2014-02-12 19:09:28 +0530135 if args.transaction_type == "selling" else item.buying_cost_center,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530136 "batch_no": None,
137 "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in
138 item_bean.doclist.get({"parentfield": "item_tax"})))),
139 "uom": item.stock_uom,
140 "min_order_qty": flt(item.min_order_qty) if args.doctype == "Material Request" else "",
141 "conversion_factor": 1.0,
142 "qty": 1.0,
143 "price_list_rate": 0.0,
144 "base_price_list_rate": 0.0,
145 "rate": 0.0,
146 "base_rate": 0.0,
147 "amount": 0.0,
148 "base_amount": 0.0,
149 "discount_percentage": 0.0
150 })
Nabin Hait436f5262014-02-10 14:47:54 +0530151
152 for fieldname in ("item_name", "item_group", "barcode", "brand", "stock_uom"):
153 out[fieldname] = item.fields.get(fieldname)
154
155 return out
156
157def get_price_list_rate(args, item_bean, out):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530158 meta = frappe.get_doctype(args.doctype)
Nabin Hait436f5262014-02-10 14:47:54 +0530159
160 if meta.get_field("currency"):
161 validate_price_list(args)
162 validate_conversion_rate(args, meta)
163
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530164 price_list_rate = frappe.conn.get_value("Item Price",
Nabin Haita7f757a2014-02-10 17:54:04 +0530165 {"price_list": args.price_list, "item_code": args.item_code}, "price_list_rate")
Nabin Hait436f5262014-02-10 14:47:54 +0530166
167 if not price_list_rate: return {}
168
169 out.price_list_rate = flt(price_list_rate) * flt(args.plc_conversion_rate) \
170 / flt(args.conversion_rate)
171
172 if not out.price_list_rate and args.transaction_type == "buying":
173 from erpnext.stock.doctype.item.item import get_last_purchase_details
174 out.update(get_last_purchase_details(item_bean.doc.name,
175 args.docname, args.conversion_rate))
176
177def validate_price_list(args):
178 if args.get("price_list"):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530179 if not frappe.conn.get_value("Price List",
Nabin Hait436f5262014-02-10 14:47:54 +0530180 {"name": args.price_list, args.transaction_type: 1, "enabled": 1}):
181 throw(_("Price List is either disabled or for not ") + _(args.transaction_type))
182 else:
183 throw(_("Price List not selected"))
184
185def validate_conversion_rate(args, meta):
186 from erpnext.setup.doctype.currency.currency import validate_conversion_rate
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530187 from frappe.model.meta import get_field_precision
Nabin Hait436f5262014-02-10 14:47:54 +0530188
189 # validate currency conversion rate
190 validate_conversion_rate(args.currency, args.conversion_rate,
191 meta.get_label("conversion_rate"), args.company)
192
193 args.conversion_rate = flt(args.conversion_rate,
194 get_field_precision(meta.get_field("conversion_rate"),
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530195 frappe._dict({"fields": args})))
Nabin Hait436f5262014-02-10 14:47:54 +0530196
197 # validate price list currency conversion rate
198 if not args.get("price_list_currency"):
199 throw(_("Price List Currency not selected"))
200 else:
201 validate_conversion_rate(args.price_list_currency, args.plc_conversion_rate,
202 meta.get_label("plc_conversion_rate"), args.company)
203
204 args.plc_conversion_rate = flt(args.plc_conversion_rate,
205 get_field_precision(meta.get_field("plc_conversion_rate"),
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530206 frappe._dict({"fields": args})))
Nabin Hait436f5262014-02-10 14:47:54 +0530207
Nabin Hait139dc7b2014-02-12 14:53:18 +0530208def get_item_discount(item_group, customer):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530209 parent_item_groups = [x[0] for x in frappe.conn.sql("""SELECT parent.name
Nabin Hait139dc7b2014-02-12 14:53:18 +0530210 FROM `tabItem Group` AS node, `tabItem Group` AS parent
211 WHERE parent.lft <= node.lft and parent.rgt >= node.rgt and node.name = %s
212 GROUP BY parent.name
213 ORDER BY parent.lft desc""", (item_group,))]
214
215 discount = 0
216 for d in parent_item_groups:
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530217 res = frappe.conn.sql("""select discount, name from `tabCustomer Discount`
Nabin Hait139dc7b2014-02-12 14:53:18 +0530218 where parent = %s and item_group = %s""", (customer, d))
219 if res:
220 discount = flt(res[0][0])
221 break
222
223 return {"discount_percentage": discount}
Nabin Hait436f5262014-02-10 14:47:54 +0530224
225def get_party_item_code(args, item_bean, out):
226 if args.transaction_type == "selling":
227 customer_item_code = item_bean.doclist.get({"parentfield": "item_customer_details",
228 "customer_name": args.customer})
229 out.customer_item_code = customer_item_code[0].ref_code if customer_item_code else None
230 else:
231 item_supplier = item_bean.doclist.get({"parentfield": "item_supplier_details",
232 "supplier": args.supplier})
233 out.supplier_part_no = item_supplier[0].supplier_part_no if item_supplier else None
234
235
236def get_pos_settings_item_details(company, args, pos_settings=None):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530237 res = frappe._dict()
Nabin Hait436f5262014-02-10 14:47:54 +0530238
239 if not pos_settings:
240 pos_settings = get_pos_settings(company)
241
242 if pos_settings:
243 for fieldname in ("income_account", "cost_center", "warehouse", "expense_account"):
244 if not args.get(fieldname):
245 res[fieldname] = pos_settings.get(fieldname)
246
247 if res.get("warehouse"):
248 res.actual_qty = get_available_qty(args.item_code,
249 res.warehouse).get("actual_qty")
250
251 return res
252
253def get_pos_settings(company):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530254 pos_settings = frappe.conn.sql("""select * from `tabPOS Setting` where user = %s
255 and company = %s""", (frappe.session['user'], company), as_dict=1)
Nabin Hait436f5262014-02-10 14:47:54 +0530256
257 if not pos_settings:
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530258 pos_settings = frappe.conn.sql("""select * from `tabPOS Setting`
Nabin Hait436f5262014-02-10 14:47:54 +0530259 where ifnull(user,'') = '' and company = %s""", company, as_dict=1)
260
261 return pos_settings and pos_settings[0] or None
Nabin Hait139dc7b2014-02-12 14:53:18 +0530262
263def get_serial_nos_by_fifo(args, item_bean):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530264 return "\n".join(frappe.conn.sql_list("""select name from `tabSerial No`
Nabin Hait139dc7b2014-02-12 14:53:18 +0530265 where item_code=%(item_code)s and warehouse=%(warehouse)s and status='Available'
266 order by timestamp(purchase_date, purchase_time) asc limit %(qty)s""", {
267 "item_code": args.item_code,
268 "warehouse": args.warehouse,
269 "qty": cint(args.qty)
270 }))
Nabin Hait436f5262014-02-10 14:47:54 +0530271
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530272@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530273def get_conversion_factor(item_code, uom):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530274 return {"conversion_factor": frappe.conn.get_value("UOM Conversion Detail",
Nabin Hait436f5262014-02-10 14:47:54 +0530275 {"parent": item_code, "uom": uom}, "conversion_factor")}
276
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530277@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530278def get_projected_qty(item_code, warehouse):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530279 return {"projected_qty": frappe.conn.get_value("Bin",
Nabin Hait436f5262014-02-10 14:47:54 +0530280 {"item_code": item_code, "warehouse": warehouse}, "projected_qty")}
281
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530282@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530283def get_available_qty(item_code, warehouse):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530284 return frappe.conn.get_value("Bin", {"item_code": item_code, "warehouse": warehouse},
Nabin Hait436f5262014-02-10 14:47:54 +0530285 ["projected_qty", "actual_qty"], as_dict=True) or {}