blob: dfe4cc36be89696faac2b123888c978166519675 [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 Hait0f231062014-02-20 11:27:47 +05308from frappe.model.meta import has_field
Nabin Hait436f5262014-02-10 14:47:54 +05309import json
10
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053011@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +053012def get_item_details(args):
13 """
14 args = {
15 "item_code": "",
16 "warehouse": None,
17 "customer": "",
18 "conversion_rate": 1.0,
19 "selling_price_list": None,
20 "price_list_currency": None,
21 "plc_conversion_rate": 1.0
22 "doctype": "",
23 "docname": "",
24 "supplier": None,
25 "transaction_date": None,
26 "conversion_rate": 1.0,
27 "buying_price_list": None,
28 "is_subcontracted": "Yes" / "No",
29 "transaction_type": "selling"
30 }
31 """
32
33 if isinstance(args, basestring):
34 args = json.loads(args)
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053035 args = frappe._dict(args)
Nabin Hait0f231062014-02-20 11:27:47 +053036
Nabin Hait0aa71a52014-02-11 16:14:52 +053037 if not args.get("transaction_type"):
Nabin Hait0f231062014-02-20 11:27:47 +053038 args.transaction_type = "buying" if has_field(args.get("doctype"), "supplier") \
39 else "selling"
Nabin Hait139dc7b2014-02-12 14:53:18 +053040
41 if not args.get("price_list"):
42 args.price_list = args.get("selling_price_list") or args.get("buying_price_list")
Nabin Hait0aa71a52014-02-11 16:14:52 +053043
Nabin Hait436f5262014-02-10 14:47:54 +053044 if args.barcode:
45 args.item_code = get_item_code(barcode=args.barcode)
46 elif not args.item_code and args.serial_no:
47 args.item_code = get_item_code(serial_no=args.serial_no)
48
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053049 item_bean = frappe.bean("Item", args.item_code)
Nabin Hait436f5262014-02-10 14:47:54 +053050 item = item_bean.doc
51
52 validate_item_details(args, item)
53
54 out = get_basic_details(args, item_bean)
55
56 get_party_item_code(args, item_bean, out)
57
58 if out.get("warehouse"):
59 out.update(get_available_qty(args.item_code, out.warehouse))
60 out.update(get_projected_qty(item.name, out.warehouse))
61
Nabin Hait436f5262014-02-10 14:47:54 +053062 get_price_list_rate(args, item_bean, out)
63
Nabin Hait139dc7b2014-02-12 14:53:18 +053064 out.update(get_item_discount(out.item_group, args.customer))
Nabin Hait436f5262014-02-10 14:47:54 +053065
66 if args.transaction_type == "selling" and cint(args.is_pos):
67 out.update(get_pos_settings_item_details(args.company, args))
68
Nabin Hait139dc7b2014-02-12 14:53:18 +053069 if args.get("doctype") in ("Sales Invoice", "Delivery Note"):
Nabin Hait436f5262014-02-10 14:47:54 +053070 if item_bean.doc.has_serial_no == "Yes" and not args.serial_no:
Nabin Hait0aa71a52014-02-11 16:14:52 +053071 out.serial_no = get_serial_nos_by_fifo(args, item_bean)
Nabin Hait139dc7b2014-02-12 14:53:18 +053072
73 if args.transaction_date and item.lead_time_days:
74 out.schedule_date = out.lead_time_date = add_days(args.transaction_date,
75 item.lead_time_days)
Nabin Hait436f5262014-02-10 14:47:54 +053076
77 return out
78
Nabin Hait436f5262014-02-10 14:47:54 +053079def get_item_code(barcode=None, serial_no=None):
80 if barcode:
Anand Doshie9baaa62014-02-26 12:35:33 +053081 item_code = frappe.db.get_value("Item", {"barcode": barcode})
Nabin Hait436f5262014-02-10 14:47:54 +053082 elif serial_no:
Anand Doshie9baaa62014-02-26 12:35:33 +053083 item_code = frappe.db.get_value("Serial No", serial_no, "item_code")
Nabin Hait436f5262014-02-10 14:47:54 +053084
85 if not item_code:
86 throw(_("No Item found with ") + _("Barcode") if barcode else _("Serial No") +
87 ": %s" % (barcode or serial_no))
88
89 return item_code
90
91def validate_item_details(args, item):
92 if not args.company:
93 throw(_("Please specify Company"))
94
95 from erpnext.stock.doctype.item.item import validate_end_of_life
96 validate_end_of_life(item.name, item.end_of_life)
97
98 if args.transaction_type == "selling":
99 # validate if sales item or service item
Nabin Hait139dc7b2014-02-12 14:53:18 +0530100 if args.get("order_type") == "Maintenance":
Nabin Hait436f5262014-02-10 14:47:54 +0530101 if item.is_service_item != "Yes":
102 throw(_("Item") + (" %s: " % item.name) +
103 _("not a service item.") +
104 _("Please select a service item or change the order type to Sales."))
105
106 elif item.is_sales_item != "Yes":
107 throw(_("Item") + (" %s: " % item.name) + _("not a sales item"))
108
109 elif args.transaction_type == "buying":
110 # validate if purchase item or subcontracted item
111 if item.is_purchase_item != "Yes":
112 throw(_("Item") + (" %s: " % item.name) + _("not a purchase item"))
113
Nabin Hait139dc7b2014-02-12 14:53:18 +0530114 if args.get("is_subcontracted") == "Yes" and item.is_sub_contracted_item != "Yes":
Nabin Hait436f5262014-02-10 14:47:54 +0530115 throw(_("Item") + (" %s: " % item.name) +
116 _("not a sub-contracted item.") +
117 _("Please select a sub-contracted item or do not sub-contract the transaction."))
118
119def get_basic_details(args, item_bean):
120 item = item_bean.doc
121
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530122 from frappe.defaults import get_user_default_as_list
Nabin Hait436f5262014-02-10 14:47:54 +0530123 user_default_warehouse_list = get_user_default_as_list('warehouse')
124 user_default_warehouse = user_default_warehouse_list[0] \
125 if len(user_default_warehouse_list)==1 else ""
Nabin Hait36028bf2014-02-12 19:09:28 +0530126
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530127 out = frappe._dict({
Nabin Hait139dc7b2014-02-12 14:53:18 +0530128 "item_code": item.name,
129 "item_name": item.item_name,
130 "description": item.description_html or item.description,
131 "warehouse": user_default_warehouse or args.warehouse or item.default_warehouse,
Nabin Haiteba1bdb2014-02-12 16:04:17 +0530132 "income_account": item.income_account or args.income_account \
Anand Doshie9baaa62014-02-26 12:35:33 +0530133 or frappe.db.get_value("Company", args.company, "default_income_account"),
Nabin Hait139dc7b2014-02-12 14:53:18 +0530134 "expense_account": item.expense_account or args.expense_account \
Anand Doshie9baaa62014-02-26 12:35:33 +0530135 or frappe.db.get_value("Company", args.company, "default_expense_account"),
Nabin Hait139dc7b2014-02-12 14:53:18 +0530136 "cost_center": item.selling_cost_center \
Nabin Hait36028bf2014-02-12 19:09:28 +0530137 if args.transaction_type == "selling" else item.buying_cost_center,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530138 "batch_no": None,
139 "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in
140 item_bean.doclist.get({"parentfield": "item_tax"})))),
141 "uom": item.stock_uom,
142 "min_order_qty": flt(item.min_order_qty) if args.doctype == "Material Request" else "",
143 "conversion_factor": 1.0,
144 "qty": 1.0,
145 "price_list_rate": 0.0,
146 "base_price_list_rate": 0.0,
147 "rate": 0.0,
148 "base_rate": 0.0,
149 "amount": 0.0,
150 "base_amount": 0.0,
151 "discount_percentage": 0.0
152 })
Nabin Hait436f5262014-02-10 14:47:54 +0530153
154 for fieldname in ("item_name", "item_group", "barcode", "brand", "stock_uom"):
155 out[fieldname] = item.fields.get(fieldname)
156
157 return out
158
159def get_price_list_rate(args, item_bean, out):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530160 meta = frappe.get_doctype(args.doctype)
Nabin Hait436f5262014-02-10 14:47:54 +0530161
162 if meta.get_field("currency"):
163 validate_price_list(args)
164 validate_conversion_rate(args, meta)
165
Anand Doshie9baaa62014-02-26 12:35:33 +0530166 price_list_rate = frappe.db.get_value("Item Price",
Nabin Haita7f757a2014-02-10 17:54:04 +0530167 {"price_list": args.price_list, "item_code": args.item_code}, "price_list_rate")
Nabin Hait436f5262014-02-10 14:47:54 +0530168
169 if not price_list_rate: return {}
170
171 out.price_list_rate = flt(price_list_rate) * flt(args.plc_conversion_rate) \
172 / flt(args.conversion_rate)
173
174 if not out.price_list_rate and args.transaction_type == "buying":
175 from erpnext.stock.doctype.item.item import get_last_purchase_details
176 out.update(get_last_purchase_details(item_bean.doc.name,
177 args.docname, args.conversion_rate))
178
179def validate_price_list(args):
180 if args.get("price_list"):
Anand Doshie9baaa62014-02-26 12:35:33 +0530181 if not frappe.db.get_value("Price List",
Nabin Hait436f5262014-02-10 14:47:54 +0530182 {"name": args.price_list, args.transaction_type: 1, "enabled": 1}):
183 throw(_("Price List is either disabled or for not ") + _(args.transaction_type))
184 else:
185 throw(_("Price List not selected"))
186
187def validate_conversion_rate(args, meta):
188 from erpnext.setup.doctype.currency.currency import validate_conversion_rate
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530189 from frappe.model.meta import get_field_precision
Nabin Hait436f5262014-02-10 14:47:54 +0530190
191 # validate currency conversion rate
192 validate_conversion_rate(args.currency, args.conversion_rate,
193 meta.get_label("conversion_rate"), args.company)
194
195 args.conversion_rate = flt(args.conversion_rate,
196 get_field_precision(meta.get_field("conversion_rate"),
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530197 frappe._dict({"fields": args})))
Nabin Hait436f5262014-02-10 14:47:54 +0530198
199 # validate price list currency conversion rate
200 if not args.get("price_list_currency"):
201 throw(_("Price List Currency not selected"))
202 else:
203 validate_conversion_rate(args.price_list_currency, args.plc_conversion_rate,
204 meta.get_label("plc_conversion_rate"), args.company)
205
206 args.plc_conversion_rate = flt(args.plc_conversion_rate,
207 get_field_precision(meta.get_field("plc_conversion_rate"),
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530208 frappe._dict({"fields": args})))
Nabin Hait436f5262014-02-10 14:47:54 +0530209
Nabin Hait139dc7b2014-02-12 14:53:18 +0530210def get_item_discount(item_group, customer):
Anand Doshie9baaa62014-02-26 12:35:33 +0530211 parent_item_groups = [x[0] for x in frappe.db.sql("""SELECT parent.name
Nabin Hait139dc7b2014-02-12 14:53:18 +0530212 FROM `tabItem Group` AS node, `tabItem Group` AS parent
213 WHERE parent.lft <= node.lft and parent.rgt >= node.rgt and node.name = %s
214 GROUP BY parent.name
215 ORDER BY parent.lft desc""", (item_group,))]
216
217 discount = 0
218 for d in parent_item_groups:
Anand Doshie9baaa62014-02-26 12:35:33 +0530219 res = frappe.db.sql("""select discount, name from `tabCustomer Discount`
Nabin Hait139dc7b2014-02-12 14:53:18 +0530220 where parent = %s and item_group = %s""", (customer, d))
221 if res:
222 discount = flt(res[0][0])
223 break
224
225 return {"discount_percentage": discount}
Nabin Hait436f5262014-02-10 14:47:54 +0530226
227def get_party_item_code(args, item_bean, out):
228 if args.transaction_type == "selling":
229 customer_item_code = item_bean.doclist.get({"parentfield": "item_customer_details",
230 "customer_name": args.customer})
231 out.customer_item_code = customer_item_code[0].ref_code if customer_item_code else None
232 else:
233 item_supplier = item_bean.doclist.get({"parentfield": "item_supplier_details",
234 "supplier": args.supplier})
235 out.supplier_part_no = item_supplier[0].supplier_part_no if item_supplier else None
236
237
238def get_pos_settings_item_details(company, args, pos_settings=None):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530239 res = frappe._dict()
Nabin Hait436f5262014-02-10 14:47:54 +0530240
241 if not pos_settings:
242 pos_settings = get_pos_settings(company)
243
244 if pos_settings:
245 for fieldname in ("income_account", "cost_center", "warehouse", "expense_account"):
246 if not args.get(fieldname):
247 res[fieldname] = pos_settings.get(fieldname)
248
249 if res.get("warehouse"):
250 res.actual_qty = get_available_qty(args.item_code,
251 res.warehouse).get("actual_qty")
252
253 return res
254
255def get_pos_settings(company):
Anand Doshie9baaa62014-02-26 12:35:33 +0530256 pos_settings = frappe.db.sql("""select * from `tabPOS Setting` where user = %s
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530257 and company = %s""", (frappe.session['user'], company), as_dict=1)
Nabin Hait436f5262014-02-10 14:47:54 +0530258
259 if not pos_settings:
Anand Doshie9baaa62014-02-26 12:35:33 +0530260 pos_settings = frappe.db.sql("""select * from `tabPOS Setting`
Nabin Hait436f5262014-02-10 14:47:54 +0530261 where ifnull(user,'') = '' and company = %s""", company, as_dict=1)
262
263 return pos_settings and pos_settings[0] or None
Nabin Hait139dc7b2014-02-12 14:53:18 +0530264
265def get_serial_nos_by_fifo(args, item_bean):
Anand Doshie9baaa62014-02-26 12:35:33 +0530266 return "\n".join(frappe.db.sql_list("""select name from `tabSerial No`
Nabin Hait139dc7b2014-02-12 14:53:18 +0530267 where item_code=%(item_code)s and warehouse=%(warehouse)s and status='Available'
268 order by timestamp(purchase_date, purchase_time) asc limit %(qty)s""", {
269 "item_code": args.item_code,
270 "warehouse": args.warehouse,
271 "qty": cint(args.qty)
272 }))
Nabin Hait436f5262014-02-10 14:47:54 +0530273
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530274@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530275def get_conversion_factor(item_code, uom):
Anand Doshie9baaa62014-02-26 12:35:33 +0530276 return {"conversion_factor": frappe.db.get_value("UOM Conversion Detail",
Nabin Hait436f5262014-02-10 14:47:54 +0530277 {"parent": item_code, "uom": uom}, "conversion_factor")}
278
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530279@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530280def get_projected_qty(item_code, warehouse):
Anand Doshie9baaa62014-02-26 12:35:33 +0530281 return {"projected_qty": frappe.db.get_value("Bin",
Nabin Hait436f5262014-02-10 14:47:54 +0530282 {"item_code": item_code, "warehouse": warehouse}, "projected_qty")}
283
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530284@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530285def get_available_qty(item_code, warehouse):
Anand Doshie9baaa62014-02-26 12:35:33 +0530286 return frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse},
Nabin Hait436f5262014-02-10 14:47:54 +0530287 ["projected_qty", "actual_qty"], as_dict=True) or {}