blob: 539e8a566762a9ab4db84a960c34a6d82c45e793 [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Nabin Hait436f5262014-02-10 14:47:54 +05302# 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
Nabin Hait2244ac42015-01-12 17:35:14 +05307from frappe.utils import flt, cint, add_days, cstr
Nabin Hait436f5262014-02-10 14:47:54 +05308import json
Nabin Hait34d28222016-01-19 15:45:49 +05309from erpnext.accounts.doctype.pricing_rule.pricing_rule import get_pricing_rule_for_item, set_transaction_type
Anand Doshidffec8f2014-07-01 17:45:15 +053010from erpnext.setup.utils import get_exchange_rate
Rushabh Mehta66e08e32014-09-29 12:17:03 +053011from frappe.model.meta import get_field_precision
Rushabh Mehta551406a2017-04-21 12:40:19 +053012from erpnext.stock.doctype.batch.batch import get_batch_no
Nabin Hait98c2a052014-05-05 18:41:41 +053013
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053014@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +053015def get_item_details(args):
16 """
17 args = {
18 "item_code": "",
19 "warehouse": None,
20 "customer": "",
21 "conversion_rate": 1.0,
22 "selling_price_list": None,
23 "price_list_currency": None,
Nabin Hait615d3422014-02-25 19:01:20 +053024 "plc_conversion_rate": 1.0,
Rushabh Mehtab46069d2016-01-15 16:59:26 +053025 "doctype": "",
26 "name": "",
Nabin Hait436f5262014-02-10 14:47:54 +053027 "supplier": None,
28 "transaction_date": None,
29 "conversion_rate": 1.0,
30 "buying_price_list": None,
31 "is_subcontracted": "Yes" / "No",
Nabin Hait444f9562014-06-20 15:59:49 +053032 "ignore_pricing_rule": 0/1
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +053033 "project": ""
Nabin Hait436f5262014-02-10 14:47:54 +053034 }
35 """
Anand Doshidffec8f2014-07-01 17:45:15 +053036 args = process_args(args)
Rushabh Mehtaf14b8092014-04-03 14:30:42 +053037 item_doc = frappe.get_doc("Item", args.item_code)
38 item = item_doc
Nabin Hait436f5262014-02-10 14:47:54 +053039
40 validate_item_details(args, item)
Anand Doshibd67e872014-04-11 16:51:27 +053041
Rushabh Mehta1a763742014-10-03 16:36:43 +053042 out = get_basic_details(args, item)
Anand Doshibd67e872014-04-11 16:51:27 +053043
Rushabh Mehtaf14b8092014-04-03 14:30:42 +053044 get_party_item_code(args, item_doc, out)
Nabin Hait436f5262014-02-10 14:47:54 +053045
Saurabh3fbf3ce2016-03-09 15:31:04 +053046 if frappe.db.exists("Product Bundle", args.item_code):
Saurabhbd01a812016-03-07 14:32:23 +053047 valuation_rate = 0.0
Saurabh3fbf3ce2016-03-09 15:31:04 +053048 bundled_items = frappe.get_doc("Product Bundle", args.item_code)
Rushabh Mehta0394aec2016-04-22 17:22:22 +053049
Saurabh3fbf3ce2016-03-09 15:31:04 +053050 for bundle_item in bundled_items.items:
51 valuation_rate += \
52 flt(get_valuation_rate(bundle_item.item_code, out.get("warehouse")).get("valuation_rate") \
53 * bundle_item.qty)
Saurabhbd01a812016-03-07 14:32:23 +053054
55 out.update({
56 "valuation_rate": valuation_rate
57 })
Saurabh3fbf3ce2016-03-09 15:31:04 +053058
Saurabh5ada14b2016-02-26 18:02:55 +053059 else:
Saurabh3fbf3ce2016-03-09 15:31:04 +053060 out.update(get_valuation_rate(args.item_code, out.get("warehouse")))
Anand Doshibd67e872014-04-11 16:51:27 +053061
Rushabh Mehtaf14b8092014-04-03 14:30:42 +053062 get_price_list_rate(args, item_doc, out)
Nabin Hait436f5262014-02-10 14:47:54 +053063
Rushabh Mehtab46069d2016-01-15 16:59:26 +053064 if args.customer and cint(args.is_pos):
Rushabh Mehtaad9156a2015-08-18 15:29:42 +053065 out.update(get_pos_profile_item_details(args.company, args))
Rushabh Mehta6b537922017-03-14 16:59:11 +053066
Nabin Hait0287d312017-01-11 10:31:33 +053067 if out.get("warehouse"):
68 out.update(get_bin_details(args.item_code, out.warehouse))
Anand Doshibd67e872014-04-11 16:51:27 +053069
Nabin Haita3dd72a2014-05-28 12:49:20 +053070 # update args with out, if key or value not exists
71 for key, value in out.iteritems():
72 if args.get(key) is None:
73 args[key] = value
74
Nabin Hait444f9562014-06-20 15:59:49 +053075 out.update(get_pricing_rule_for_item(args))
Anand Doshibd67e872014-04-11 16:51:27 +053076
pratu16x7577e4c42017-06-23 11:39:03 +053077 if (args.get("doctype") == "Delivery Note" or
Nabin Hait3ce41d62017-05-03 18:22:24 +053078 (args.get("doctype") == "Sales Invoice" and args.get('update_stock'))) \
79 and out.warehouse and out.stock_qty > 0:
pratu16x7577e4c42017-06-23 11:39:03 +053080
Rushabh Mehta551406a2017-04-21 12:40:19 +053081 if out.has_serial_no:
Rohit Waghchaureba3f0e62017-08-28 17:19:28 +053082 out.serial_no = get_serial_no(out, args.serial_no)
Rushabh Mehta551406a2017-04-21 12:40:19 +053083
Nabin Hait8fac2ad2017-05-18 11:54:24 +053084 if out.has_batch_no and not args.get("batch_no"):
Rushabh Mehta551406a2017-04-21 12:40:19 +053085 out.batch_no = get_batch_no(out.item_code, out.warehouse, out.qty)
86
Anand Doshibd67e872014-04-11 16:51:27 +053087
Nabin Hait139dc7b2014-02-12 14:53:18 +053088 if args.transaction_date and item.lead_time_days:
89 out.schedule_date = out.lead_time_date = add_days(args.transaction_date,
90 item.lead_time_days)
Anand Doshibd67e872014-04-11 16:51:27 +053091
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +053092 if args.get("is_subcontracted") == "Yes":
Saurabh590d4012017-09-18 15:49:19 +053093 out.bom = args.get('bom') or get_default_bom(args.item_code)
Rushabh Mehta0394aec2016-04-22 17:22:22 +053094
Saurabh2d7af632016-02-26 11:09:20 +053095 get_gross_profit(out)
Anand Doshi70f57eb2015-11-27 14:37:40 +053096
Nabin Hait436f5262014-02-10 14:47:54 +053097 return out
98
pratu16x7577e4c42017-06-23 11:39:03 +053099 # print(frappe._dict({
100 # 'has_serial_no' : out.has_serial_no,
101 # 'has_batch_no' : out.has_batch_no
102 # }))
103
104 # return frappe._dict({
105 # 'has_serial_no' : out.has_serial_no,
106 # 'has_batch_no' : out.has_batch_no
107 # })
108
Anand Doshidffec8f2014-07-01 17:45:15 +0530109def process_args(args):
110 if isinstance(args, basestring):
111 args = json.loads(args)
112
113 args = frappe._dict(args)
114
Anand Doshidffec8f2014-07-01 17:45:15 +0530115 if not args.get("price_list"):
116 args.price_list = args.get("selling_price_list") or args.get("buying_price_list")
117
118 if args.barcode:
119 args.item_code = get_item_code(barcode=args.barcode)
120 elif not args.item_code and args.serial_no:
121 args.item_code = get_item_code(serial_no=args.serial_no)
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530122
mbauskar6f603392016-01-21 16:10:22 +0530123 set_transaction_type(args)
Anand Doshidffec8f2014-07-01 17:45:15 +0530124 return args
125
Neil Trini Lasrado4abda672015-02-25 17:34:27 +0530126@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530127def get_item_code(barcode=None, serial_no=None):
128 if barcode:
Anand Doshie9baaa62014-02-26 12:35:33 +0530129 item_code = frappe.db.get_value("Item", {"barcode": barcode})
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530130 if not item_code:
131 frappe.throw(_("No Item with Barcode {0}").format(barcode))
Nabin Hait436f5262014-02-10 14:47:54 +0530132 elif serial_no:
Anand Doshie9baaa62014-02-26 12:35:33 +0530133 item_code = frappe.db.get_value("Serial No", serial_no, "item_code")
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530134 if not item_code:
135 frappe.throw(_("No Item with Serial No {0}").format(serial_no))
Anand Doshibd67e872014-04-11 16:51:27 +0530136
Nabin Hait436f5262014-02-10 14:47:54 +0530137 return item_code
Anand Doshibd67e872014-04-11 16:51:27 +0530138
Nabin Hait436f5262014-02-10 14:47:54 +0530139def validate_item_details(args, item):
140 if not args.company:
141 throw(_("Please specify Company"))
Anand Doshibd67e872014-04-11 16:51:27 +0530142
Nabin Hait436f5262014-02-10 14:47:54 +0530143 from erpnext.stock.doctype.item.item import validate_end_of_life
Anand Doshi21e09a22015-10-29 12:21:41 +0530144 validate_end_of_life(item.name, item.end_of_life, item.disabled)
Anand Doshibd67e872014-04-11 16:51:27 +0530145
Umair Sayyed72534de2016-04-15 12:52:12 +0530146 if args.transaction_type=="selling" and cint(item.has_variants):
147 throw(_("Item {0} is a template, please select one of its variants").format(item.name))
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530148
Nabin Hait34d28222016-01-19 15:45:49 +0530149 elif args.transaction_type=="buying" and args.doctype != "Material Request":
Rushabh Mehta1e8025b2015-07-24 15:16:25 +0530150 if args.get("is_subcontracted") == "Yes" and item.is_sub_contracted_item != 1:
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530151 throw(_("Item {0} must be a Sub-contracted Item").format(item.name))
Anand Doshibd67e872014-04-11 16:51:27 +0530152
Rushabh Mehta1a763742014-10-03 16:36:43 +0530153def get_basic_details(args, item):
154 if not item:
155 item = frappe.get_doc("Item", args.get("item_code"))
Anand Doshibd67e872014-04-11 16:51:27 +0530156
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530157 if item.variant_of:
158 item.update_template_tables()
159
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530160 from frappe.defaults import get_user_default_as_list
Nabin Haitb6b56452015-12-23 16:37:00 +0530161 user_default_warehouse_list = get_user_default_as_list('Warehouse')
Nabin Hait436f5262014-02-10 14:47:54 +0530162 user_default_warehouse = user_default_warehouse_list[0] \
163 if len(user_default_warehouse_list)==1 else ""
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530164
Rohit Waghchaure6734c4a2017-01-06 14:39:40 +0530165 warehouse = user_default_warehouse or item.default_warehouse or args.warehouse
Nabin Hait36028bf2014-02-12 19:09:28 +0530166
Nabin Hait79a1d2a2017-09-29 18:14:10 +0530167 #Set the UOM to the Default Sales UOM or Default Purchase UOM if configured in the Item Master
168 if not args.uom:
169 if args.get('doctype') in ['Quotation', 'Sales Order', 'Delivery Note', 'Sales Invoice']:
170 args.uom = item.sales_uom if item.sales_uom else item.stock_uom
171 elif args.get('doctype') in ['Purchase Order', 'Purchase Receipt', 'Purchase Invoice']:
172 args.uom = item.purchase_uom if item.purchase_uom else item.stock_uom
173 else:
174 args.uom = item.stock_uom
175
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530176 out = frappe._dict({
Nabin Hait139dc7b2014-02-12 14:53:18 +0530177 "item_code": item.name,
178 "item_name": item.item_name,
Neil Trini Lasradoc48f06c2015-02-10 16:49:16 +0530179 "description": cstr(item.description).strip(),
180 "image": cstr(item.image).strip(),
Saurabhd3135532016-02-25 18:59:20 +0530181 "warehouse": warehouse,
Rushabh Mehta1a763742014-10-03 16:36:43 +0530182 "income_account": get_default_income_account(args, item),
183 "expense_account": get_default_expense_account(args, item),
184 "cost_center": get_default_cost_center(args, item),
Rushabh Mehta551406a2017-04-21 12:40:19 +0530185 'has_serial_no': item.has_serial_no,
186 'has_batch_no': item.has_batch_no,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530187 "batch_no": None,
Anand Doshibd67e872014-04-11 16:51:27 +0530188 "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in
Nabin Haite7d15362014-12-25 16:01:55 +0530189 item.get("taxes")))),
Nabin Hait79a1d2a2017-09-29 18:14:10 +0530190 "uom": args.uom,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530191 "min_order_qty": flt(item.min_order_qty) if args.doctype == "Material Request" else "",
Nabin Hait4a6c1782015-05-29 17:31:31 +0530192 "qty": args.qty or 1.0,
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530193 "stock_qty": args.qty or 1.0,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530194 "price_list_rate": 0.0,
195 "base_price_list_rate": 0.0,
196 "rate": 0.0,
197 "base_rate": 0.0,
198 "amount": 0.0,
199 "base_amount": 0.0,
Nabin Hait82e3e252015-02-23 16:58:30 +0530200 "net_rate": 0.0,
201 "net_amount": 0.0,
Saurabhc6dbe702015-10-19 14:17:52 +0530202 "discount_percentage": 0.0,
203 "supplier": item.default_supplier,
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530204 "update_stock": args.get("update_stock") if args.get('doctype') in ['Sales Invoice', 'Purchase Invoice'] else 0,
Rohit Waghchaurec858d522016-12-12 13:01:43 +0530205 "delivered_by_supplier": item.delivered_by_supplier if args.get("doctype") in ["Sales Order", "Sales Invoice"] else 0,
Nabin Haite4170002016-07-05 15:37:09 +0530206 "is_fixed_asset": item.is_fixed_asset
Nabin Hait139dc7b2014-02-12 14:53:18 +0530207 })
Anand Doshibd67e872014-04-11 16:51:27 +0530208
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530209 # calculate conversion factor
mbauskar287fe812017-04-10 19:15:57 +0530210 if item.stock_uom == args.uom:
211 out.conversion_factor = 1.0
212 else:
213 out.conversion_factor = args.conversion_factor or \
214 get_conversion_factor(item.item_code, args.uom).get("conversion_factor") or 1.0
215
216 args.conversion_factor = out.conversion_factor
217 out.stock_qty = out.qty * out.conversion_factor
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530218
Nabin Haitb09ed412015-02-17 10:33:58 +0530219 # if default specified in item is for another company, fetch from company
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530220 for d in [
221 ["Account", "income_account", "default_income_account"],
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530222 ["Account", "expense_account", "default_expense_account"],
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530223 ["Cost Center", "cost_center", "cost_center"],
224 ["Warehouse", "warehouse", ""]]:
Nabin Hait98a8fae2015-02-26 11:37:07 +0530225 company = frappe.db.get_value(d[0], out.get(d[1]), "company")
226 if not out[d[1]] or (company and args.company != company):
Nabin Haitb09ed412015-02-17 10:33:58 +0530227 out[d[1]] = frappe.db.get_value("Company", args.company, d[2]) if d[2] else None
228
Nabin Hait436f5262014-02-10 14:47:54 +0530229 for fieldname in ("item_name", "item_group", "barcode", "brand", "stock_uom"):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530230 out[fieldname] = item.get(fieldname)
Anand Doshibd67e872014-04-11 16:51:27 +0530231
Nabin Hait436f5262014-02-10 14:47:54 +0530232 return out
Anand Doshibd67e872014-04-11 16:51:27 +0530233
Rushabh Mehta1a763742014-10-03 16:36:43 +0530234def get_default_income_account(args, item):
235 return (item.income_account
236 or args.income_account
Anand Doshid35354c2015-02-24 18:12:17 +0530237 or frappe.db.get_value("Item Group", item.item_group, "default_income_account"))
Rushabh Mehta1a763742014-10-03 16:36:43 +0530238
239def get_default_expense_account(args, item):
240 return (item.expense_account
241 or args.expense_account
Anand Doshid35354c2015-02-24 18:12:17 +0530242 or frappe.db.get_value("Item Group", item.item_group, "default_expense_account"))
Rushabh Mehta1a763742014-10-03 16:36:43 +0530243
244def get_default_cost_center(args, item):
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +0530245 return (frappe.db.get_value("Project", args.get("project"), "cost_center")
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530246 or (item.selling_cost_center if args.get("customer") else item.buying_cost_center)
gmarkee5a31462015-09-27 09:38:01 +0200247 or frappe.db.get_value("Item Group", item.item_group, "default_cost_center")
248 or args.get("cost_center"))
Rushabh Mehta1a763742014-10-03 16:36:43 +0530249
Rushabh Mehtaf14b8092014-04-03 14:30:42 +0530250def get_price_list_rate(args, item_doc, out):
Anand Doshi39a28912016-02-15 11:42:18 +0530251 meta = frappe.get_meta(args.parenttype or args.doctype)
Nabin Hait436f5262014-02-10 14:47:54 +0530252
253 if meta.get_field("currency"):
254 validate_price_list(args)
rohitwaghchaure4cccdbd2017-07-25 14:05:01 +0530255 if args.price_list:
256 validate_conversion_rate(args, meta)
Nabin Hait436f5262014-02-10 14:47:54 +0530257
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530258 price_list_rate = get_price_list_rate_for(args.price_list, item_doc.name)
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530259
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530260 # variant
261 if not price_list_rate and item_doc.variant_of:
262 price_list_rate = get_price_list_rate_for(args.price_list, item_doc.variant_of)
263
264 # insert in database
Rushabh Mehta1a763742014-10-03 16:36:43 +0530265 if not price_list_rate:
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530266 if args.price_list and args.rate:
267 insert_item_price(args)
Rushabh Mehta1a763742014-10-03 16:36:43 +0530268 return {}
Anand Doshibd67e872014-04-11 16:51:27 +0530269
Nabin Hait436f5262014-02-10 14:47:54 +0530270 out.price_list_rate = flt(price_list_rate) * flt(args.plc_conversion_rate) \
271 / flt(args.conversion_rate)
Anand Doshibd67e872014-04-11 16:51:27 +0530272
mbauskar287fe812017-04-10 19:15:57 +0530273 out.price_list_rate = flt(out.price_list_rate * (args.conversion_factor or 1.0))
274
Nabin Hait34d28222016-01-19 15:45:49 +0530275 if not out.price_list_rate and args.transaction_type=="buying":
Nabin Hait436f5262014-02-10 14:47:54 +0530276 from erpnext.stock.doctype.item.item import get_last_purchase_details
Anand Doshibd67e872014-04-11 16:51:27 +0530277 out.update(get_last_purchase_details(item_doc.name,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530278 args.name, args.conversion_rate))
Anand Doshibd67e872014-04-11 16:51:27 +0530279
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530280def insert_item_price(args):
281 """Insert Item Price if Price List and Price List Rate are specified and currency is the same"""
282 if frappe.db.get_value("Price List", args.price_list, "currency") == args.currency \
283 and cint(frappe.db.get_single_value("Stock Settings", "auto_insert_price_list_rate_if_missing")):
284 if frappe.has_permission("Item Price", "write"):
Nabin Hait906bf642015-09-02 11:32:07 +0530285 price_list_rate = args.rate / args.conversion_factor \
286 if args.get("conversion_factor") else args.rate
Rushabh Mehta985cb822016-12-30 15:06:54 +0530287
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530288 item_price = frappe.get_doc({
289 "doctype": "Item Price",
290 "price_list": args.price_list,
291 "item_code": args.item_code,
292 "currency": args.currency,
Nabin Hait906bf642015-09-02 11:32:07 +0530293 "price_list_rate": price_list_rate
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530294 })
Rushabh Mehta985cb822016-12-30 15:06:54 +0530295
Kanchan Chauhan2ff7d2a2016-06-25 17:54:25 +0530296 name = frappe.db.get_value('Item Price', {'item_code': args.item_code, 'price_list': args.price_list, 'currency': args.currency}, 'name')
Rushabh Mehta985cb822016-12-30 15:06:54 +0530297
Kanchan Chauhan5ee8fcc2016-06-24 17:11:15 +0530298 if name:
299 item_price = frappe.get_doc('Item Price', name)
300 item_price.price_list_rate = price_list_rate
Rushabh Mehta985cb822016-12-30 15:06:54 +0530301 item_price.save()
Kanchan Chauhan5ee8fcc2016-06-24 17:11:15 +0530302 frappe.msgprint(_("Item Price updated for {0} in Price List {1}").format(args.item_code,
303 args.price_list))
Rushabh Mehta985cb822016-12-30 15:06:54 +0530304 else:
Kanchan Chauhan5ee8fcc2016-06-24 17:11:15 +0530305 item_price.insert()
306 frappe.msgprint(_("Item Price added for {0} in Price List {1}").format(args.item_code,
307 args.price_list))
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530308
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530309def get_price_list_rate_for(price_list, item_code):
Rushabh Mehta1a763742014-10-03 16:36:43 +0530310 return frappe.db.get_value("Item Price",
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530311 {"price_list": price_list, "item_code": item_code}, "price_list_rate")
Rushabh Mehta1a763742014-10-03 16:36:43 +0530312
Nabin Hait436f5262014-02-10 14:47:54 +0530313def validate_price_list(args):
314 if args.get("price_list"):
Anand Doshibd67e872014-04-11 16:51:27 +0530315 if not frappe.db.get_value("Price List",
Nabin Hait34d28222016-01-19 15:45:49 +0530316 {"name": args.price_list, args.transaction_type: 1, "enabled": 1}):
Rushabh Mehta985cb822016-12-30 15:06:54 +0530317 throw(_("Price List {0} is disabled or does not exist").format(args.price_list))
mbauskar0b665ac2017-04-11 02:49:24 -0400318 elif not args.get("supplier"):
Nabin Hait436f5262014-02-10 14:47:54 +0530319 throw(_("Price List not selected"))
Anand Doshibd67e872014-04-11 16:51:27 +0530320
Nabin Hait436f5262014-02-10 14:47:54 +0530321def validate_conversion_rate(args, meta):
Rushabh Mehta66e08e32014-09-29 12:17:03 +0530322 from erpnext.controllers.accounts_controller import validate_conversion_rate
Anand Doshibd67e872014-04-11 16:51:27 +0530323
Anand Doshife13bfe2015-08-25 12:49:40 +0530324 if (not args.conversion_rate
325 and args.currency==frappe.db.get_value("Company", args.company, "default_currency")):
326 args.conversion_rate = 1.0
327
Nabin Hait436f5262014-02-10 14:47:54 +0530328 # validate currency conversion rate
Anand Doshibd67e872014-04-11 16:51:27 +0530329 validate_conversion_rate(args.currency, args.conversion_rate,
Nabin Hait436f5262014-02-10 14:47:54 +0530330 meta.get_label("conversion_rate"), args.company)
Anand Doshibd67e872014-04-11 16:51:27 +0530331
332 args.conversion_rate = flt(args.conversion_rate,
333 get_field_precision(meta.get_field("conversion_rate"),
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530334 frappe._dict({"fields": args})))
Anand Doshibd67e872014-04-11 16:51:27 +0530335
Nabin Hait436f5262014-02-10 14:47:54 +0530336 # validate price list currency conversion rate
337 if not args.get("price_list_currency"):
338 throw(_("Price List Currency not selected"))
339 else:
Anand Doshibd67e872014-04-11 16:51:27 +0530340 validate_conversion_rate(args.price_list_currency, args.plc_conversion_rate,
Nabin Hait436f5262014-02-10 14:47:54 +0530341 meta.get_label("plc_conversion_rate"), args.company)
Anand Doshibd67e872014-04-11 16:51:27 +0530342
343 args.plc_conversion_rate = flt(args.plc_conversion_rate,
344 get_field_precision(meta.get_field("plc_conversion_rate"),
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530345 frappe._dict({"fields": args})))
Anand Doshibd67e872014-04-11 16:51:27 +0530346
Rushabh Mehtaf14b8092014-04-03 14:30:42 +0530347def get_party_item_code(args, item_doc, out):
Nabin Hait34d28222016-01-19 15:45:49 +0530348 if args.transaction_type=="selling" and args.customer:
Nabin Hait79f091e2014-12-26 11:41:15 +0530349 customer_item_code = item_doc.get("customer_items", {"customer_name": args.customer})
Nabin Hait436f5262014-02-10 14:47:54 +0530350 out.customer_item_code = customer_item_code[0].ref_code if customer_item_code else None
Anand Doshi54abf022016-01-21 22:28:47 +0530351
Nabin Hait34d28222016-01-19 15:45:49 +0530352 if args.transaction_type=="buying" and args.supplier:
Nabin Hait79f091e2014-12-26 11:41:15 +0530353 item_supplier = item_doc.get("supplier_items", {"supplier": args.supplier})
Nabin Hait436f5262014-02-10 14:47:54 +0530354 out.supplier_part_no = item_supplier[0].supplier_part_no if item_supplier else None
Anand Doshibd67e872014-04-11 16:51:27 +0530355
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530356def get_pos_profile_item_details(company, args, pos_profile=None):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530357 res = frappe._dict()
Anand Doshibd67e872014-04-11 16:51:27 +0530358
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530359 if not pos_profile:
360 pos_profile = get_pos_profile(company)
Anand Doshibd67e872014-04-11 16:51:27 +0530361
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530362 if pos_profile:
Nabin Hait436f5262014-02-10 14:47:54 +0530363 for fieldname in ("income_account", "cost_center", "warehouse", "expense_account"):
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530364 if not args.get(fieldname) and pos_profile.get(fieldname):
365 res[fieldname] = pos_profile.get(fieldname)
Anand Doshibd67e872014-04-11 16:51:27 +0530366
Nabin Hait436f5262014-02-10 14:47:54 +0530367 if res.get("warehouse"):
Saurabhd3135532016-02-25 18:59:20 +0530368 res.actual_qty = get_bin_details(args.item_code,
Nabin Hait436f5262014-02-10 14:47:54 +0530369 res.warehouse).get("actual_qty")
Anand Doshibd67e872014-04-11 16:51:27 +0530370
Nabin Hait436f5262014-02-10 14:47:54 +0530371 return res
372
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530373@frappe.whitelist()
374def get_pos_profile(company):
375 pos_profile = frappe.db.sql("""select * from `tabPOS Profile` where user = %s
Nabin Haitc469f2c2017-04-03 13:01:11 +0530376 and company = %s""", (frappe.session['user'], company), as_dict=1)
Anand Doshibd67e872014-04-11 16:51:27 +0530377
Nabin Haitc469f2c2017-04-03 13:01:11 +0530378 if not pos_profile:
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530379 pos_profile = frappe.db.sql("""select * from `tabPOS Profile`
Nabin Hait436f5262014-02-10 14:47:54 +0530380 where ifnull(user,'') = '' and company = %s""", company, as_dict=1)
Anand Doshibd67e872014-04-11 16:51:27 +0530381
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530382 return pos_profile and pos_profile[0] or None
Anand Doshibd67e872014-04-11 16:51:27 +0530383
Nabin Hait08222152014-02-28 11:30:47 +0530384
Kanchan Chauhan0a3f2c82016-10-25 23:15:39 +0530385def get_serial_nos_by_fifo(args):
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530386 if frappe.db.get_single_value("Stock Settings", "automatically_set_serial_nos_based_on_fifo"):
387 return "\n".join(frappe.db.sql_list("""select name from `tabSerial No`
Nabin Hait398c83a2015-10-22 15:11:44 +0530388 where item_code=%(item_code)s and warehouse=%(warehouse)s
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530389 order by timestamp(purchase_date, purchase_time) asc limit %(qty)s""", {
390 "item_code": args.item_code,
391 "warehouse": args.warehouse,
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530392 "qty": abs(cint(args.stock_qty))
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530393 }))
Nabin Hait08222152014-02-28 11:30:47 +0530394
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530395@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530396def get_conversion_factor(item_code, uom):
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530397 variant_of = frappe.db.get_value("Item", item_code, "variant_of")
398 filters = {"parent": item_code, "uom": uom}
399 if variant_of:
Nabin Hait314086d2015-10-07 11:55:01 +0530400 filters["parent"] = ("in", (item_code, variant_of))
Anand Doshie9baaa62014-02-26 12:35:33 +0530401 return {"conversion_factor": frappe.db.get_value("UOM Conversion Detail",
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530402 filters, "conversion_factor")}
Nabin Hait08222152014-02-28 11:30:47 +0530403
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530404@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530405def get_projected_qty(item_code, warehouse):
Anand Doshibd67e872014-04-11 16:51:27 +0530406 return {"projected_qty": frappe.db.get_value("Bin",
Nabin Hait436f5262014-02-10 14:47:54 +0530407 {"item_code": item_code, "warehouse": warehouse}, "projected_qty")}
Nabin Hait08222152014-02-28 11:30:47 +0530408
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530409@frappe.whitelist()
Saurabhd3135532016-02-25 18:59:20 +0530410def get_bin_details(item_code, warehouse):
Anand Doshibd67e872014-04-11 16:51:27 +0530411 return frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse},
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530412 ["projected_qty", "actual_qty"], as_dict=True) \
413 or {"projected_qty": 0, "actual_qty": 0}
414
415@frappe.whitelist()
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530416def get_serial_no_details(item_code, warehouse, stock_qty, serial_no):
417 args = frappe._dict({"item_code":item_code, "warehouse":warehouse, "stock_qty":stock_qty, "serial_no":serial_no})
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530418 serial_no = get_serial_no(args)
419 return {'serial_no': serial_no}
Rushabh Mehta985cb822016-12-30 15:06:54 +0530420
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530421@frappe.whitelist()
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530422def get_bin_details_and_serial_nos(item_code, warehouse, stock_qty=None, serial_no=None):
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530423 bin_details_and_serial_nos = {}
424 bin_details_and_serial_nos.update(get_bin_details(item_code, warehouse))
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530425 if stock_qty > 0:
426 bin_details_and_serial_nos.update(get_serial_no_details(item_code, warehouse, stock_qty, serial_no))
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530427 return bin_details_and_serial_nos
Anand Doshidffec8f2014-07-01 17:45:15 +0530428
429@frappe.whitelist()
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530430def get_batch_qty(batch_no, warehouse, item_code):
sburanaw66951e52017-04-25 15:28:57 +0700431 from erpnext.stock.doctype.batch import batch
Sambhaji Kolate98dbccd2015-03-10 15:04:28 +0530432 if batch_no:
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530433 return {'actual_batch_qty': batch.get_batch_qty(batch_no, warehouse)}
Rushabh Mehta1e8025b2015-07-24 15:16:25 +0530434
Anand Doshidffec8f2014-07-01 17:45:15 +0530435@frappe.whitelist()
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530436def apply_price_list(args, as_doc=False):
437 """Apply pricelist on a document-like dict object and return as
438 {'parent': dict, 'children': list}
439
440 :param args: See below
441 :param as_doc: Updates value in the passed dict
442
Anand Doshidffec8f2014-07-01 17:45:15 +0530443 args = {
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530444 "doctype": "",
445 "name": "",
446 "items": [{"doctype": "", "name": "", "item_code": "", "brand": "", "item_group": ""}, ...],
Anand Doshidffec8f2014-07-01 17:45:15 +0530447 "conversion_rate": 1.0,
448 "selling_price_list": None,
449 "price_list_currency": None,
450 "plc_conversion_rate": 1.0,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530451 "doctype": "",
452 "name": "",
Anand Doshidffec8f2014-07-01 17:45:15 +0530453 "supplier": None,
454 "transaction_date": None,
455 "conversion_rate": 1.0,
456 "buying_price_list": None,
Anand Doshidffec8f2014-07-01 17:45:15 +0530457 "ignore_pricing_rule": 0/1
458 }
459 """
460 args = process_args(args)
461
462 parent = get_price_list_currency_and_exchange_rate(args)
463 children = []
464
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530465 if "items" in args:
466 item_list = args.get("items")
Anand Doshidffec8f2014-07-01 17:45:15 +0530467 args.update(parent)
468
469 for item in item_list:
470 args_copy = frappe._dict(args.copy())
471 args_copy.update(item)
472 item_details = apply_price_list_on_item(args_copy)
473 children.append(item_details)
474
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530475 if as_doc:
476 args.price_list_currency = parent.price_list_currency
477 args.plc_conversion_rate = parent.plc_conversion_rate
478 if args.get('items'):
479 for i, item in enumerate(args.get('items')):
480 for fieldname in children[i]:
481 # if the field exists in the original doc
482 # update the value
483 if fieldname in item and fieldname not in ("name", "doctype"):
484 item[fieldname] = children[i][fieldname]
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530485 return args
486 else:
487 return {
488 "parent": parent,
489 "children": children
490 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530491
492def apply_price_list_on_item(args):
493 item_details = frappe._dict()
494 item_doc = frappe.get_doc("Item", args.item_code)
495 get_price_list_rate(args, item_doc, item_details)
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530496
Anand Doshidffec8f2014-07-01 17:45:15 +0530497 item_details.update(get_pricing_rule_for_item(args))
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530498
Anand Doshidffec8f2014-07-01 17:45:15 +0530499 return item_details
500
501def get_price_list_currency(price_list):
Neil Trini Lasradoadb1b2c2015-03-18 11:40:39 +0530502 if price_list:
503 result = frappe.db.get_value("Price List", {"name": price_list,
504 "enabled": 1}, ["name", "currency"], as_dict=True)
Anand Doshidffec8f2014-07-01 17:45:15 +0530505
Neil Trini Lasradoadb1b2c2015-03-18 11:40:39 +0530506 if not result:
Rushabh Mehta985cb822016-12-30 15:06:54 +0530507 throw(_("Price List {0} is disabled or does not exist").format(price_list))
Anand Doshidffec8f2014-07-01 17:45:15 +0530508
Neil Trini Lasradoadb1b2c2015-03-18 11:40:39 +0530509 return result.currency
Anand Doshidffec8f2014-07-01 17:45:15 +0530510
511def get_price_list_currency_and_exchange_rate(args):
Anand Doshi70f57eb2015-11-27 14:37:40 +0530512 if not args.price_list:
513 return {}
514
Anand Doshidffec8f2014-07-01 17:45:15 +0530515 price_list_currency = get_price_list_currency(args.price_list)
516 plc_conversion_rate = args.plc_conversion_rate
517
Nabin Haitdd82bf22015-05-11 16:49:17 +0530518 if (not plc_conversion_rate) or (price_list_currency and args.price_list_currency \
519 and price_list_currency != args.price_list_currency):
Rushabh Mehta6b537922017-03-14 16:59:11 +0530520 # cksgb 19/09/2016: added args.transaction_date as posting_date argument for get_exchange_rate
Rushabh Mehta985cb822016-12-30 15:06:54 +0530521 plc_conversion_rate = get_exchange_rate(price_list_currency, args.currency,
Nabin Haitad4f1c72016-12-09 12:14:47 +0530522 args.transaction_date) or plc_conversion_rate
Anand Doshidffec8f2014-07-01 17:45:15 +0530523
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530524 return frappe._dict({
Anand Doshidffec8f2014-07-01 17:45:15 +0530525 "price_list_currency": price_list_currency,
526 "plc_conversion_rate": plc_conversion_rate
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530527 })
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530528
529@frappe.whitelist()
530def get_default_bom(item_code=None):
531 if item_code:
532 bom = frappe.db.get_value("BOM", {"docstatus": 1, "is_default": 1, "is_active": 1, "item": item_code})
533 if bom:
534 return bom
535 else:
536 frappe.throw(_("No default BOM exists for Item {0}").format(item_code))
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530537
Saurabh3fbf3ce2016-03-09 15:31:04 +0530538def get_valuation_rate(item_code, warehouse=None):
Saurabhbd01a812016-03-07 14:32:23 +0530539 item = frappe.get_doc("Item", item_code)
540 if item.is_stock_item:
Saurabhbd01a812016-03-07 14:32:23 +0530541 if not warehouse:
542 warehouse = item.default_warehouse
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530543
544 return frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse},
Saurabhbd01a812016-03-07 14:32:23 +0530545 ["valuation_rate"], as_dict=True) or {"valuation_rate": 0}
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530546
Saurabhbd01a812016-03-07 14:32:23 +0530547 elif not item.is_stock_item:
Nabin Haitfe876c02017-03-06 16:32:57 +0530548 valuation_rate =frappe.db.sql("""select sum(base_net_amount) / sum(qty*conversion_factor)
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530549 from `tabPurchase Invoice Item`
Saurabhbd01a812016-03-07 14:32:23 +0530550 where item_code = %s and docstatus=1""", item_code)
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530551
Saurabhbd01a812016-03-07 14:32:23 +0530552 if valuation_rate:
553 return {"valuation_rate": valuation_rate[0][0] or 0.0}
554 else:
555 return {"valuation_rate": 0.0}
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530556
Saurabh2d7af632016-02-26 11:09:20 +0530557def get_gross_profit(out):
Saurabh5ada14b2016-02-26 18:02:55 +0530558 if out.valuation_rate:
559 out.update({
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530560 "gross_profit": ((out.base_rate - out.valuation_rate) * out.stock_qty)
Saurabh5ada14b2016-02-26 18:02:55 +0530561 })
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530562
Saurabhd3135532016-02-25 18:59:20 +0530563 return out
mbauskar6f603392016-01-21 16:10:22 +0530564
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530565@frappe.whitelist()
Rohit Waghchaureba3f0e62017-08-28 17:19:28 +0530566def get_serial_no(args, serial_nos=None):
567 serial_no = None
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530568 if isinstance(args, basestring):
569 args = json.loads(args)
570 args = frappe._dict(args)
571
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530572 if args.get('doctype') == 'Sales Invoice' and not args.get('update_stock'):
573 return ""
574
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530575 if args.get('warehouse') and args.get('stock_qty') and args.get('item_code'):
Rushabh Mehta985cb822016-12-30 15:06:54 +0530576
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530577 if frappe.get_value('Item', {'item_code': args.item_code}, "has_serial_no") == 1:
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530578 args = json.dumps({"item_code": args.get('item_code'),"warehouse": args.get('warehouse'),"stock_qty": args.get('stock_qty')})
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530579 args = process_args(args)
580 serial_no = get_serial_nos_by_fifo(args)
Rohit Waghchaureba3f0e62017-08-28 17:19:28 +0530581
582 if not serial_no and serial_nos:
583 # For POS
584 serial_no = serial_nos
585
586 return serial_no