blob: 9c26a18f82162760228343ca7305fd1c3cf9ab42 [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
tundebabzy6e90f492018-02-12 10:48:57 +010013from erpnext import get_company_currency
Nabin Hait98c2a052014-05-05 18:41:41 +053014
tundebabzyacccdb32017-11-23 08:35:15 +010015
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053016@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +053017def get_item_details(args):
18 """
19 args = {
20 "item_code": "",
21 "warehouse": None,
22 "customer": "",
23 "conversion_rate": 1.0,
24 "selling_price_list": None,
25 "price_list_currency": None,
Nabin Hait615d3422014-02-25 19:01:20 +053026 "plc_conversion_rate": 1.0,
Rushabh Mehtab46069d2016-01-15 16:59:26 +053027 "doctype": "",
28 "name": "",
Nabin Hait436f5262014-02-10 14:47:54 +053029 "supplier": None,
30 "transaction_date": None,
31 "conversion_rate": 1.0,
32 "buying_price_list": None,
33 "is_subcontracted": "Yes" / "No",
Nabin Hait444f9562014-06-20 15:59:49 +053034 "ignore_pricing_rule": 0/1
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +053035 "project": ""
Nabin Hait436f5262014-02-10 14:47:54 +053036 }
37 """
Anand Doshidffec8f2014-07-01 17:45:15 +053038 args = process_args(args)
Rushabh Mehtaf14b8092014-04-03 14:30:42 +053039 item_doc = frappe.get_doc("Item", args.item_code)
40 item = item_doc
Nabin Hait436f5262014-02-10 14:47:54 +053041
42 validate_item_details(args, item)
Anand Doshibd67e872014-04-11 16:51:27 +053043
Rushabh Mehta1a763742014-10-03 16:36:43 +053044 out = get_basic_details(args, item)
Anand Doshibd67e872014-04-11 16:51:27 +053045
Rushabh Mehtaf14b8092014-04-03 14:30:42 +053046 get_party_item_code(args, item_doc, out)
Nabin Hait436f5262014-02-10 14:47:54 +053047
Saurabh3fbf3ce2016-03-09 15:31:04 +053048 if frappe.db.exists("Product Bundle", args.item_code):
Saurabhbd01a812016-03-07 14:32:23 +053049 valuation_rate = 0.0
Saurabh3fbf3ce2016-03-09 15:31:04 +053050 bundled_items = frappe.get_doc("Product Bundle", args.item_code)
Rushabh Mehta0394aec2016-04-22 17:22:22 +053051
Saurabh3fbf3ce2016-03-09 15:31:04 +053052 for bundle_item in bundled_items.items:
53 valuation_rate += \
54 flt(get_valuation_rate(bundle_item.item_code, out.get("warehouse")).get("valuation_rate") \
55 * bundle_item.qty)
Saurabhbd01a812016-03-07 14:32:23 +053056
57 out.update({
58 "valuation_rate": valuation_rate
59 })
Saurabh3fbf3ce2016-03-09 15:31:04 +053060
Saurabh5ada14b2016-02-26 18:02:55 +053061 else:
Saurabh3fbf3ce2016-03-09 15:31:04 +053062 out.update(get_valuation_rate(args.item_code, out.get("warehouse")))
Anand Doshibd67e872014-04-11 16:51:27 +053063
Rushabh Mehtaf14b8092014-04-03 14:30:42 +053064 get_price_list_rate(args, item_doc, out)
Nabin Hait436f5262014-02-10 14:47:54 +053065
Rushabh Mehtab46069d2016-01-15 16:59:26 +053066 if args.customer and cint(args.is_pos):
Rushabh Mehtaad9156a2015-08-18 15:29:42 +053067 out.update(get_pos_profile_item_details(args.company, args))
Rushabh Mehta6b537922017-03-14 16:59:11 +053068
Nabin Hait0287d312017-01-11 10:31:33 +053069 if out.get("warehouse"):
70 out.update(get_bin_details(args.item_code, out.warehouse))
Anand Doshibd67e872014-04-11 16:51:27 +053071
Nabin Haita3dd72a2014-05-28 12:49:20 +053072 # update args with out, if key or value not exists
73 for key, value in out.iteritems():
74 if args.get(key) is None:
75 args[key] = value
76
Nabin Hait444f9562014-06-20 15:59:49 +053077 out.update(get_pricing_rule_for_item(args))
Anand Doshibd67e872014-04-11 16:51:27 +053078
pratu16x7577e4c42017-06-23 11:39:03 +053079 if (args.get("doctype") == "Delivery Note" or
Nabin Hait3ce41d62017-05-03 18:22:24 +053080 (args.get("doctype") == "Sales Invoice" and args.get('update_stock'))) \
81 and out.warehouse and out.stock_qty > 0:
pratu16x7577e4c42017-06-23 11:39:03 +053082
Rushabh Mehta551406a2017-04-21 12:40:19 +053083 if out.has_serial_no:
Rohit Waghchaureba3f0e62017-08-28 17:19:28 +053084 out.serial_no = get_serial_no(out, args.serial_no)
Rushabh Mehta551406a2017-04-21 12:40:19 +053085
Nabin Hait8fac2ad2017-05-18 11:54:24 +053086 if out.has_batch_no and not args.get("batch_no"):
Rushabh Mehta551406a2017-04-21 12:40:19 +053087 out.batch_no = get_batch_no(out.item_code, out.warehouse, out.qty)
rohitwaghchaure2fb8cc52017-11-29 13:50:04 +053088 actual_batch_qty = get_batch_qty(out.batch_no, out.warehouse, out.item_code)
89 if actual_batch_qty:
90 out.update(actual_batch_qty)
Rushabh Mehta551406a2017-04-21 12:40:19 +053091
Nabin Hait139dc7b2014-02-12 14:53:18 +053092 if args.transaction_date and item.lead_time_days:
93 out.schedule_date = out.lead_time_date = add_days(args.transaction_date,
94 item.lead_time_days)
Anand Doshibd67e872014-04-11 16:51:27 +053095
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +053096 if args.get("is_subcontracted") == "Yes":
Saurabh590d4012017-09-18 15:49:19 +053097 out.bom = args.get('bom') or get_default_bom(args.item_code)
Rushabh Mehta0394aec2016-04-22 17:22:22 +053098
Saurabh2d7af632016-02-26 11:09:20 +053099 get_gross_profit(out)
Anand Doshi70f57eb2015-11-27 14:37:40 +0530100
Nabin Hait436f5262014-02-10 14:47:54 +0530101 return out
102
Anand Doshidffec8f2014-07-01 17:45:15 +0530103def process_args(args):
104 if isinstance(args, basestring):
105 args = json.loads(args)
106
107 args = frappe._dict(args)
108
Anand Doshidffec8f2014-07-01 17:45:15 +0530109 if not args.get("price_list"):
110 args.price_list = args.get("selling_price_list") or args.get("buying_price_list")
111
112 if args.barcode:
113 args.item_code = get_item_code(barcode=args.barcode)
114 elif not args.item_code and args.serial_no:
115 args.item_code = get_item_code(serial_no=args.serial_no)
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530116
mbauskar6f603392016-01-21 16:10:22 +0530117 set_transaction_type(args)
Anand Doshidffec8f2014-07-01 17:45:15 +0530118 return args
119
tundebabzyacccdb32017-11-23 08:35:15 +0100120
Neil Trini Lasrado4abda672015-02-25 17:34:27 +0530121@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530122def get_item_code(barcode=None, serial_no=None):
123 if barcode:
Anand Doshie9baaa62014-02-26 12:35:33 +0530124 item_code = frappe.db.get_value("Item", {"barcode": barcode})
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530125 if not item_code:
126 frappe.throw(_("No Item with Barcode {0}").format(barcode))
Nabin Hait436f5262014-02-10 14:47:54 +0530127 elif serial_no:
Anand Doshie9baaa62014-02-26 12:35:33 +0530128 item_code = frappe.db.get_value("Serial No", serial_no, "item_code")
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530129 if not item_code:
130 frappe.throw(_("No Item with Serial No {0}").format(serial_no))
Anand Doshibd67e872014-04-11 16:51:27 +0530131
Nabin Hait436f5262014-02-10 14:47:54 +0530132 return item_code
Anand Doshibd67e872014-04-11 16:51:27 +0530133
tundebabzyacccdb32017-11-23 08:35:15 +0100134
Nabin Hait436f5262014-02-10 14:47:54 +0530135def validate_item_details(args, item):
136 if not args.company:
137 throw(_("Please specify Company"))
Anand Doshibd67e872014-04-11 16:51:27 +0530138
Nabin Hait436f5262014-02-10 14:47:54 +0530139 from erpnext.stock.doctype.item.item import validate_end_of_life
Anand Doshi21e09a22015-10-29 12:21:41 +0530140 validate_end_of_life(item.name, item.end_of_life, item.disabled)
Anand Doshibd67e872014-04-11 16:51:27 +0530141
tundebabzyacccdb32017-11-23 08:35:15 +0100142 if args.transaction_type == "selling" and cint(item.has_variants):
Umair Sayyed72534de2016-04-15 12:52:12 +0530143 throw(_("Item {0} is a template, please select one of its variants").format(item.name))
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530144
tundebabzyacccdb32017-11-23 08:35:15 +0100145 elif args.transaction_type == "buying" and args.doctype != "Material Request":
Rushabh Mehta1e8025b2015-07-24 15:16:25 +0530146 if args.get("is_subcontracted") == "Yes" and item.is_sub_contracted_item != 1:
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530147 throw(_("Item {0} must be a Sub-contracted Item").format(item.name))
Anand Doshibd67e872014-04-11 16:51:27 +0530148
tundebabzyacccdb32017-11-23 08:35:15 +0100149
Rushabh Mehta1a763742014-10-03 16:36:43 +0530150def get_basic_details(args, item):
tundebabzyacccdb32017-11-23 08:35:15 +0100151 """
152 :param args: {
153 "item_code": "",
154 "warehouse": None,
155 "customer": "",
156 "conversion_rate": 1.0,
157 "selling_price_list": None,
158 "price_list_currency": None,
Shreya Shahb13a54a2017-12-06 19:17:04 +0530159 "price_list_uom_dependant": None,
tundebabzyacccdb32017-11-23 08:35:15 +0100160 "plc_conversion_rate": 1.0,
161 "doctype": "",
162 "name": "",
163 "supplier": None,
164 "transaction_date": None,
165 "conversion_rate": 1.0,
166 "buying_price_list": None,
167 "is_subcontracted": "Yes" / "No",
168 "ignore_pricing_rule": 0/1
169 "project": "",
170 barcode: "",
171 serial_no: "",
172 warehouse: "",
173 currency: "",
174 update_stock: "",
175 price_list: "",
176 company: "",
177 order_type: "",
178 is_pos: "",
179 ignore_pricing_rule: "",
180 project: "",
181 qty: "",
182 stock_qty: "",
183 conversion_factor: ""
184 }
185 :param item: `item_code` of Item object
186 :return: frappe._dict
187 """
188
Rushabh Mehta1a763742014-10-03 16:36:43 +0530189 if not item:
190 item = frappe.get_doc("Item", args.get("item_code"))
Anand Doshibd67e872014-04-11 16:51:27 +0530191
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530192 if item.variant_of:
193 item.update_template_tables()
194
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530195 from frappe.defaults import get_user_default_as_list
Nabin Haitb6b56452015-12-23 16:37:00 +0530196 user_default_warehouse_list = get_user_default_as_list('Warehouse')
Nabin Hait436f5262014-02-10 14:47:54 +0530197 user_default_warehouse = user_default_warehouse_list[0] \
tundebabzyacccdb32017-11-23 08:35:15 +0100198 if len(user_default_warehouse_list) == 1 else ""
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530199
Rohit Waghchaure6734c4a2017-01-06 14:39:40 +0530200 warehouse = user_default_warehouse or item.default_warehouse or args.warehouse
Nabin Hait36028bf2014-02-12 19:09:28 +0530201
rohitwaghchauree8ccc0e2017-11-21 16:17:22 +0530202 material_request_type = ''
203 if args.get('doctype') == "Material Request":
204 material_request_type = frappe.db.get_value('Material Request',
205 args.get('name'), 'material_request_type')
206
Nabin Hait79a1d2a2017-09-29 18:14:10 +0530207 #Set the UOM to the Default Sales UOM or Default Purchase UOM if configured in the Item Master
208 if not args.uom:
209 if args.get('doctype') in ['Quotation', 'Sales Order', 'Delivery Note', 'Sales Invoice']:
210 args.uom = item.sales_uom if item.sales_uom else item.stock_uom
rohitwaghchauree8ccc0e2017-11-21 16:17:22 +0530211 elif (args.get('doctype') in ['Purchase Order', 'Purchase Receipt', 'Purchase Invoice']) or \
212 (args.get('doctype') == 'Material Request' and material_request_type == 'Purchase'):
Nabin Hait79a1d2a2017-09-29 18:14:10 +0530213 args.uom = item.purchase_uom if item.purchase_uom else item.stock_uom
214 else:
215 args.uom = item.stock_uom
216
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530217 out = frappe._dict({
Nabin Hait139dc7b2014-02-12 14:53:18 +0530218 "item_code": item.name,
219 "item_name": item.item_name,
Neil Trini Lasradoc48f06c2015-02-10 16:49:16 +0530220 "description": cstr(item.description).strip(),
221 "image": cstr(item.image).strip(),
Saurabhd3135532016-02-25 18:59:20 +0530222 "warehouse": warehouse,
Rushabh Mehta1a763742014-10-03 16:36:43 +0530223 "income_account": get_default_income_account(args, item),
224 "expense_account": get_default_expense_account(args, item),
225 "cost_center": get_default_cost_center(args, item),
Rushabh Mehta551406a2017-04-21 12:40:19 +0530226 'has_serial_no': item.has_serial_no,
227 'has_batch_no': item.has_batch_no,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530228 "batch_no": None,
Anand Doshibd67e872014-04-11 16:51:27 +0530229 "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in
Nabin Haite7d15362014-12-25 16:01:55 +0530230 item.get("taxes")))),
Nabin Hait79a1d2a2017-09-29 18:14:10 +0530231 "uom": args.uom,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530232 "min_order_qty": flt(item.min_order_qty) if args.doctype == "Material Request" else "",
Nabin Hait4a6c1782015-05-29 17:31:31 +0530233 "qty": args.qty or 1.0,
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530234 "stock_qty": args.qty or 1.0,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530235 "price_list_rate": 0.0,
236 "base_price_list_rate": 0.0,
237 "rate": 0.0,
238 "base_rate": 0.0,
239 "amount": 0.0,
240 "base_amount": 0.0,
Nabin Hait82e3e252015-02-23 16:58:30 +0530241 "net_rate": 0.0,
242 "net_amount": 0.0,
Saurabhc6dbe702015-10-19 14:17:52 +0530243 "discount_percentage": 0.0,
244 "supplier": item.default_supplier,
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530245 "update_stock": args.get("update_stock") if args.get('doctype') in ['Sales Invoice', 'Purchase Invoice'] else 0,
Rohit Waghchaurec858d522016-12-12 13:01:43 +0530246 "delivered_by_supplier": item.delivered_by_supplier if args.get("doctype") in ["Sales Order", "Sales Invoice"] else 0,
Vishal Dhayaguded42242d2017-11-29 16:09:59 +0530247 "is_fixed_asset": item.is_fixed_asset,
248 "weight_per_unit":item.weight_per_unit,
249 "weight_uom":item.weight_uom,
Shreya Shah44fa9a62018-01-08 14:58:20 +0530250 "last_purchase_rate": item.last_purchase_rate if args.get("doctype") in ["Purchase Order"] else 0
Nabin Hait139dc7b2014-02-12 14:53:18 +0530251 })
Anand Doshibd67e872014-04-11 16:51:27 +0530252
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530253 # calculate conversion factor
mbauskar287fe812017-04-10 19:15:57 +0530254 if item.stock_uom == args.uom:
255 out.conversion_factor = 1.0
256 else:
257 out.conversion_factor = args.conversion_factor or \
tundebabzyacccdb32017-11-23 08:35:15 +0100258 get_conversion_factor(item.item_code, args.uom).get("conversion_factor") or 1.0
mbauskar287fe812017-04-10 19:15:57 +0530259
260 args.conversion_factor = out.conversion_factor
261 out.stock_qty = out.qty * out.conversion_factor
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530262
Shreya Shah44fa9a62018-01-08 14:58:20 +0530263 # calculate last purchase rate
264 from erpnext.buying.doctype.purchase_order.purchase_order import item_last_purchase_rate
265 out.last_purchase_rate = item_last_purchase_rate(args.name, args.conversion_rate, item.item_code, out.conversion_factor)
266
Nabin Haitb09ed412015-02-17 10:33:58 +0530267 # if default specified in item is for another company, fetch from company
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530268 for d in [
269 ["Account", "income_account", "default_income_account"],
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530270 ["Account", "expense_account", "default_expense_account"],
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530271 ["Cost Center", "cost_center", "cost_center"],
272 ["Warehouse", "warehouse", ""]]:
Nabin Hait98a8fae2015-02-26 11:37:07 +0530273 company = frappe.db.get_value(d[0], out.get(d[1]), "company")
274 if not out[d[1]] or (company and args.company != company):
Nabin Haitb09ed412015-02-17 10:33:58 +0530275 out[d[1]] = frappe.db.get_value("Company", args.company, d[2]) if d[2] else None
276
Nabin Hait436f5262014-02-10 14:47:54 +0530277 for fieldname in ("item_name", "item_group", "barcode", "brand", "stock_uom"):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530278 out[fieldname] = item.get(fieldname)
Anand Doshibd67e872014-04-11 16:51:27 +0530279
Nabin Hait436f5262014-02-10 14:47:54 +0530280 return out
Anand Doshibd67e872014-04-11 16:51:27 +0530281
tundebabzyacccdb32017-11-23 08:35:15 +0100282
Rushabh Mehta1a763742014-10-03 16:36:43 +0530283def get_default_income_account(args, item):
284 return (item.income_account
285 or args.income_account
Anand Doshid35354c2015-02-24 18:12:17 +0530286 or frappe.db.get_value("Item Group", item.item_group, "default_income_account"))
Rushabh Mehta1a763742014-10-03 16:36:43 +0530287
288def get_default_expense_account(args, item):
289 return (item.expense_account
290 or args.expense_account
Anand Doshid35354c2015-02-24 18:12:17 +0530291 or frappe.db.get_value("Item Group", item.item_group, "default_expense_account"))
Rushabh Mehta1a763742014-10-03 16:36:43 +0530292
293def get_default_cost_center(args, item):
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +0530294 return (frappe.db.get_value("Project", args.get("project"), "cost_center")
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530295 or (item.selling_cost_center if args.get("customer") else item.buying_cost_center)
gmarkee5a31462015-09-27 09:38:01 +0200296 or frappe.db.get_value("Item Group", item.item_group, "default_cost_center")
297 or args.get("cost_center"))
Rushabh Mehta1a763742014-10-03 16:36:43 +0530298
Rushabh Mehtaf14b8092014-04-03 14:30:42 +0530299def get_price_list_rate(args, item_doc, out):
Anand Doshi39a28912016-02-15 11:42:18 +0530300 meta = frappe.get_meta(args.parenttype or args.doctype)
Nabin Hait436f5262014-02-10 14:47:54 +0530301
302 if meta.get_field("currency"):
303 validate_price_list(args)
rohitwaghchaure4cccdbd2017-07-25 14:05:01 +0530304 if args.price_list:
305 validate_conversion_rate(args, meta)
Nabin Hait436f5262014-02-10 14:47:54 +0530306
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530307 price_list_rate = get_price_list_rate_for(args.price_list, item_doc.name)
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530308
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530309 # variant
310 if not price_list_rate and item_doc.variant_of:
311 price_list_rate = get_price_list_rate_for(args.price_list, item_doc.variant_of)
312
313 # insert in database
Rushabh Mehta1a763742014-10-03 16:36:43 +0530314 if not price_list_rate:
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530315 if args.price_list and args.rate:
316 insert_item_price(args)
Rushabh Mehta1a763742014-10-03 16:36:43 +0530317 return {}
Anand Doshibd67e872014-04-11 16:51:27 +0530318
Nabin Hait436f5262014-02-10 14:47:54 +0530319 out.price_list_rate = flt(price_list_rate) * flt(args.plc_conversion_rate) \
320 / flt(args.conversion_rate)
Zarrar17fd6612017-12-08 14:57:38 +0530321 if not args.price_list_uom_dependant:
Shreya Shahb13a54a2017-12-06 19:17:04 +0530322 out.price_list_rate = flt(out.price_list_rate * (args.conversion_factor or 1.0))
mbauskar287fe812017-04-10 19:15:57 +0530323
Nabin Hait34d28222016-01-19 15:45:49 +0530324 if not out.price_list_rate and args.transaction_type=="buying":
Nabin Hait436f5262014-02-10 14:47:54 +0530325 from erpnext.stock.doctype.item.item import get_last_purchase_details
Anand Doshibd67e872014-04-11 16:51:27 +0530326 out.update(get_last_purchase_details(item_doc.name,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530327 args.name, args.conversion_rate))
Anand Doshibd67e872014-04-11 16:51:27 +0530328
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530329def insert_item_price(args):
330 """Insert Item Price if Price List and Price List Rate are specified and currency is the same"""
331 if frappe.db.get_value("Price List", args.price_list, "currency") == args.currency \
332 and cint(frappe.db.get_single_value("Stock Settings", "auto_insert_price_list_rate_if_missing")):
333 if frappe.has_permission("Item Price", "write"):
Nabin Hait906bf642015-09-02 11:32:07 +0530334 price_list_rate = args.rate / args.conversion_factor \
335 if args.get("conversion_factor") else args.rate
Rushabh Mehta985cb822016-12-30 15:06:54 +0530336
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530337 item_price = frappe.get_doc({
338 "doctype": "Item Price",
339 "price_list": args.price_list,
340 "item_code": args.item_code,
341 "currency": args.currency,
Nabin Hait906bf642015-09-02 11:32:07 +0530342 "price_list_rate": price_list_rate
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530343 })
Rushabh Mehta985cb822016-12-30 15:06:54 +0530344
Kanchan Chauhan2ff7d2a2016-06-25 17:54:25 +0530345 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 +0530346
Kanchan Chauhan5ee8fcc2016-06-24 17:11:15 +0530347 if name:
348 item_price = frappe.get_doc('Item Price', name)
349 item_price.price_list_rate = price_list_rate
Rushabh Mehta985cb822016-12-30 15:06:54 +0530350 item_price.save()
Kanchan Chauhan5ee8fcc2016-06-24 17:11:15 +0530351 frappe.msgprint(_("Item Price updated for {0} in Price List {1}").format(args.item_code,
352 args.price_list))
Rushabh Mehta985cb822016-12-30 15:06:54 +0530353 else:
Kanchan Chauhan5ee8fcc2016-06-24 17:11:15 +0530354 item_price.insert()
355 frappe.msgprint(_("Item Price added for {0} in Price List {1}").format(args.item_code,
356 args.price_list))
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530357
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530358def get_price_list_rate_for(price_list, item_code):
Rushabh Mehta1a763742014-10-03 16:36:43 +0530359 return frappe.db.get_value("Item Price",
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530360 {"price_list": price_list, "item_code": item_code}, "price_list_rate")
Rushabh Mehta1a763742014-10-03 16:36:43 +0530361
Nabin Hait436f5262014-02-10 14:47:54 +0530362def validate_price_list(args):
363 if args.get("price_list"):
Anand Doshibd67e872014-04-11 16:51:27 +0530364 if not frappe.db.get_value("Price List",
Nabin Hait34d28222016-01-19 15:45:49 +0530365 {"name": args.price_list, args.transaction_type: 1, "enabled": 1}):
Rushabh Mehta985cb822016-12-30 15:06:54 +0530366 throw(_("Price List {0} is disabled or does not exist").format(args.price_list))
mbauskar0b665ac2017-04-11 02:49:24 -0400367 elif not args.get("supplier"):
Nabin Hait436f5262014-02-10 14:47:54 +0530368 throw(_("Price List not selected"))
Anand Doshibd67e872014-04-11 16:51:27 +0530369
Nabin Hait436f5262014-02-10 14:47:54 +0530370def validate_conversion_rate(args, meta):
Rushabh Mehta66e08e32014-09-29 12:17:03 +0530371 from erpnext.controllers.accounts_controller import validate_conversion_rate
Anand Doshibd67e872014-04-11 16:51:27 +0530372
Anand Doshife13bfe2015-08-25 12:49:40 +0530373 if (not args.conversion_rate
374 and args.currency==frappe.db.get_value("Company", args.company, "default_currency")):
375 args.conversion_rate = 1.0
376
Nabin Hait436f5262014-02-10 14:47:54 +0530377 # validate currency conversion rate
Anand Doshibd67e872014-04-11 16:51:27 +0530378 validate_conversion_rate(args.currency, args.conversion_rate,
Nabin Hait436f5262014-02-10 14:47:54 +0530379 meta.get_label("conversion_rate"), args.company)
Anand Doshibd67e872014-04-11 16:51:27 +0530380
381 args.conversion_rate = flt(args.conversion_rate,
382 get_field_precision(meta.get_field("conversion_rate"),
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530383 frappe._dict({"fields": args})))
Anand Doshibd67e872014-04-11 16:51:27 +0530384
Nabin Hait436f5262014-02-10 14:47:54 +0530385 # validate price list currency conversion rate
386 if not args.get("price_list_currency"):
387 throw(_("Price List Currency not selected"))
388 else:
Anand Doshibd67e872014-04-11 16:51:27 +0530389 validate_conversion_rate(args.price_list_currency, args.plc_conversion_rate,
Nabin Hait436f5262014-02-10 14:47:54 +0530390 meta.get_label("plc_conversion_rate"), args.company)
Anand Doshibd67e872014-04-11 16:51:27 +0530391
392 args.plc_conversion_rate = flt(args.plc_conversion_rate,
393 get_field_precision(meta.get_field("plc_conversion_rate"),
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530394 frappe._dict({"fields": args})))
Anand Doshibd67e872014-04-11 16:51:27 +0530395
Rushabh Mehtaf14b8092014-04-03 14:30:42 +0530396def get_party_item_code(args, item_doc, out):
Nabin Hait34d28222016-01-19 15:45:49 +0530397 if args.transaction_type=="selling" and args.customer:
Nabin Hait79f091e2014-12-26 11:41:15 +0530398 customer_item_code = item_doc.get("customer_items", {"customer_name": args.customer})
Nabin Hait436f5262014-02-10 14:47:54 +0530399 out.customer_item_code = customer_item_code[0].ref_code if customer_item_code else None
Anand Doshi54abf022016-01-21 22:28:47 +0530400
Nabin Hait34d28222016-01-19 15:45:49 +0530401 if args.transaction_type=="buying" and args.supplier:
Nabin Hait79f091e2014-12-26 11:41:15 +0530402 item_supplier = item_doc.get("supplier_items", {"supplier": args.supplier})
Nabin Hait436f5262014-02-10 14:47:54 +0530403 out.supplier_part_no = item_supplier[0].supplier_part_no if item_supplier else None
Anand Doshibd67e872014-04-11 16:51:27 +0530404
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530405def get_pos_profile_item_details(company, args, pos_profile=None):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530406 res = frappe._dict()
Anand Doshibd67e872014-04-11 16:51:27 +0530407
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530408 if not pos_profile:
rohitwaghchaurec037dc72017-11-28 16:11:15 +0530409 pos_profile = get_pos_profile(company, args.get('pos_profile'))
Anand Doshibd67e872014-04-11 16:51:27 +0530410
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530411 if pos_profile:
Nabin Hait436f5262014-02-10 14:47:54 +0530412 for fieldname in ("income_account", "cost_center", "warehouse", "expense_account"):
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530413 if not args.get(fieldname) and pos_profile.get(fieldname):
414 res[fieldname] = pos_profile.get(fieldname)
Anand Doshibd67e872014-04-11 16:51:27 +0530415
Nabin Hait436f5262014-02-10 14:47:54 +0530416 if res.get("warehouse"):
Saurabhd3135532016-02-25 18:59:20 +0530417 res.actual_qty = get_bin_details(args.item_code,
Nabin Hait436f5262014-02-10 14:47:54 +0530418 res.warehouse).get("actual_qty")
Anand Doshibd67e872014-04-11 16:51:27 +0530419
Nabin Hait436f5262014-02-10 14:47:54 +0530420 return res
421
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530422@frappe.whitelist()
rohitwaghchaurec037dc72017-11-28 16:11:15 +0530423def get_pos_profile(company, pos_profile=None, user=None):
424 if pos_profile:
425 return frappe.get_doc('POS Profile', pos_profile)
426
427 if not user:
428 user = frappe.session['user']
429
430 pos_profile = frappe.db.sql("""select pf.*
431 from
432 `tabPOS Profile` pf, `tabPOS Profile User` pfu
433 where
434 pfu.parent = pf.name and pfu.user = %s and pf.company = %s
435 and pf.disabled = 0 and pfu.default=1""", (user, company), as_dict=1)
Anand Doshibd67e872014-04-11 16:51:27 +0530436
Nabin Haitc469f2c2017-04-03 13:01:11 +0530437 if not pos_profile:
rohitwaghchaurec037dc72017-11-28 16:11:15 +0530438 pos_profile = frappe.db.sql("""select pf.*
439 from
440 `tabPOS Profile` pf left join `tabPOS Profile User` pfu
441 on
442 pf.name = pfu.parent
443 where
444 ifnull(pfu.user, '') = '' and pf.company = %s
445 and pf.disabled = 0""", (company), as_dict=1)
Anand Doshibd67e872014-04-11 16:51:27 +0530446
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530447 return pos_profile and pos_profile[0] or None
Anand Doshibd67e872014-04-11 16:51:27 +0530448
Kanchan Chauhan0a3f2c82016-10-25 23:15:39 +0530449def get_serial_nos_by_fifo(args):
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530450 if frappe.db.get_single_value("Stock Settings", "automatically_set_serial_nos_based_on_fifo"):
451 return "\n".join(frappe.db.sql_list("""select name from `tabSerial No`
Nabin Hait398c83a2015-10-22 15:11:44 +0530452 where item_code=%(item_code)s and warehouse=%(warehouse)s
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530453 order by timestamp(purchase_date, purchase_time) asc limit %(qty)s""", {
454 "item_code": args.item_code,
455 "warehouse": args.warehouse,
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530456 "qty": abs(cint(args.stock_qty))
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530457 }))
Nabin Hait08222152014-02-28 11:30:47 +0530458
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530459@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530460def get_conversion_factor(item_code, uom):
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530461 variant_of = frappe.db.get_value("Item", item_code, "variant_of")
462 filters = {"parent": item_code, "uom": uom}
463 if variant_of:
Nabin Hait314086d2015-10-07 11:55:01 +0530464 filters["parent"] = ("in", (item_code, variant_of))
Anand Doshie9baaa62014-02-26 12:35:33 +0530465 return {"conversion_factor": frappe.db.get_value("UOM Conversion Detail",
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530466 filters, "conversion_factor")}
Nabin Hait08222152014-02-28 11:30:47 +0530467
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530468@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530469def get_projected_qty(item_code, warehouse):
Anand Doshibd67e872014-04-11 16:51:27 +0530470 return {"projected_qty": frappe.db.get_value("Bin",
Nabin Hait436f5262014-02-10 14:47:54 +0530471 {"item_code": item_code, "warehouse": warehouse}, "projected_qty")}
Nabin Hait08222152014-02-28 11:30:47 +0530472
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530473@frappe.whitelist()
Saurabhd3135532016-02-25 18:59:20 +0530474def get_bin_details(item_code, warehouse):
Anand Doshibd67e872014-04-11 16:51:27 +0530475 return frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse},
Manas Solankia884bd92018-02-09 13:12:07 +0530476 ["projected_qty", "actual_qty"], as_dict=True) \
477 or {"projected_qty": 0, "actual_qty": 0}
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530478
479@frappe.whitelist()
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530480def get_serial_no_details(item_code, warehouse, stock_qty, serial_no):
481 args = frappe._dict({"item_code":item_code, "warehouse":warehouse, "stock_qty":stock_qty, "serial_no":serial_no})
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530482 serial_no = get_serial_no(args)
483 return {'serial_no': serial_no}
Rushabh Mehta985cb822016-12-30 15:06:54 +0530484
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530485@frappe.whitelist()
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530486def get_bin_details_and_serial_nos(item_code, warehouse, stock_qty=None, serial_no=None):
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530487 bin_details_and_serial_nos = {}
488 bin_details_and_serial_nos.update(get_bin_details(item_code, warehouse))
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530489 if stock_qty > 0:
490 bin_details_and_serial_nos.update(get_serial_no_details(item_code, warehouse, stock_qty, serial_no))
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530491 return bin_details_and_serial_nos
Anand Doshidffec8f2014-07-01 17:45:15 +0530492
493@frappe.whitelist()
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530494def get_batch_qty(batch_no, warehouse, item_code):
sburanaw66951e52017-04-25 15:28:57 +0700495 from erpnext.stock.doctype.batch import batch
Sambhaji Kolate98dbccd2015-03-10 15:04:28 +0530496 if batch_no:
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530497 return {'actual_batch_qty': batch.get_batch_qty(batch_no, warehouse)}
Rushabh Mehta1e8025b2015-07-24 15:16:25 +0530498
Anand Doshidffec8f2014-07-01 17:45:15 +0530499@frappe.whitelist()
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530500def apply_price_list(args, as_doc=False):
501 """Apply pricelist on a document-like dict object and return as
502 {'parent': dict, 'children': list}
503
504 :param args: See below
505 :param as_doc: Updates value in the passed dict
506
Anand Doshidffec8f2014-07-01 17:45:15 +0530507 args = {
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530508 "doctype": "",
509 "name": "",
510 "items": [{"doctype": "", "name": "", "item_code": "", "brand": "", "item_group": ""}, ...],
Anand Doshidffec8f2014-07-01 17:45:15 +0530511 "conversion_rate": 1.0,
512 "selling_price_list": None,
513 "price_list_currency": None,
Shreya Shahb13a54a2017-12-06 19:17:04 +0530514 "price_list_uom_dependant": None,
Anand Doshidffec8f2014-07-01 17:45:15 +0530515 "plc_conversion_rate": 1.0,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530516 "doctype": "",
517 "name": "",
Anand Doshidffec8f2014-07-01 17:45:15 +0530518 "supplier": None,
519 "transaction_date": None,
520 "conversion_rate": 1.0,
521 "buying_price_list": None,
Anand Doshidffec8f2014-07-01 17:45:15 +0530522 "ignore_pricing_rule": 0/1
523 }
524 """
525 args = process_args(args)
526
527 parent = get_price_list_currency_and_exchange_rate(args)
528 children = []
529
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530530 if "items" in args:
531 item_list = args.get("items")
Anand Doshidffec8f2014-07-01 17:45:15 +0530532 args.update(parent)
533
534 for item in item_list:
535 args_copy = frappe._dict(args.copy())
536 args_copy.update(item)
537 item_details = apply_price_list_on_item(args_copy)
538 children.append(item_details)
539
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530540 if as_doc:
Shreya Shahb13a54a2017-12-06 19:17:04 +0530541 args.price_list_currency = parent.price_list_currency,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530542 args.plc_conversion_rate = parent.plc_conversion_rate
543 if args.get('items'):
544 for i, item in enumerate(args.get('items')):
545 for fieldname in children[i]:
546 # if the field exists in the original doc
547 # update the value
548 if fieldname in item and fieldname not in ("name", "doctype"):
549 item[fieldname] = children[i][fieldname]
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530550 return args
551 else:
552 return {
553 "parent": parent,
554 "children": children
555 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530556
557def apply_price_list_on_item(args):
558 item_details = frappe._dict()
559 item_doc = frappe.get_doc("Item", args.item_code)
560 get_price_list_rate(args, item_doc, item_details)
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530561
Anand Doshidffec8f2014-07-01 17:45:15 +0530562 item_details.update(get_pricing_rule_for_item(args))
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530563
Anand Doshidffec8f2014-07-01 17:45:15 +0530564 return item_details
565
566def get_price_list_currency(price_list):
Neil Trini Lasradoadb1b2c2015-03-18 11:40:39 +0530567 if price_list:
568 result = frappe.db.get_value("Price List", {"name": price_list,
569 "enabled": 1}, ["name", "currency"], as_dict=True)
Anand Doshidffec8f2014-07-01 17:45:15 +0530570
Neil Trini Lasradoadb1b2c2015-03-18 11:40:39 +0530571 if not result:
Rushabh Mehta985cb822016-12-30 15:06:54 +0530572 throw(_("Price List {0} is disabled or does not exist").format(price_list))
Anand Doshidffec8f2014-07-01 17:45:15 +0530573
Neil Trini Lasradoadb1b2c2015-03-18 11:40:39 +0530574 return result.currency
Anand Doshidffec8f2014-07-01 17:45:15 +0530575
Shreya Shahb13a54a2017-12-06 19:17:04 +0530576def get_price_list_uom_dependant(price_list):
577 if price_list:
578 result = frappe.db.get_value("Price List", {"name": price_list,
579 "enabled": 1}, ["name", "price_not_uom_dependant"], as_dict=True)
580
581 if not result:
582 throw(_("Price List {0} is disabled or does not exist").format(price_list))
583
584 return result.price_not_uom_dependant
585
586
Anand Doshidffec8f2014-07-01 17:45:15 +0530587def get_price_list_currency_and_exchange_rate(args):
Anand Doshi70f57eb2015-11-27 14:37:40 +0530588 if not args.price_list:
589 return {}
590
Anand Doshidffec8f2014-07-01 17:45:15 +0530591 price_list_currency = get_price_list_currency(args.price_list)
Shreya Shahb13a54a2017-12-06 19:17:04 +0530592 price_list_uom_dependant = get_price_list_uom_dependant(args.price_list)
Anand Doshidffec8f2014-07-01 17:45:15 +0530593 plc_conversion_rate = args.plc_conversion_rate
tundebabzy6e90f492018-02-12 10:48:57 +0100594 company_currency = get_company_currency(args.company)
Anand Doshidffec8f2014-07-01 17:45:15 +0530595
Nabin Haitdd82bf22015-05-11 16:49:17 +0530596 if (not plc_conversion_rate) or (price_list_currency and args.price_list_currency \
597 and price_list_currency != args.price_list_currency):
Rushabh Mehta6b537922017-03-14 16:59:11 +0530598 # cksgb 19/09/2016: added args.transaction_date as posting_date argument for get_exchange_rate
tundebabzy6e90f492018-02-12 10:48:57 +0100599 plc_conversion_rate = get_exchange_rate(price_list_currency, company_currency,
Nabin Haitad4f1c72016-12-09 12:14:47 +0530600 args.transaction_date) or plc_conversion_rate
Anand Doshidffec8f2014-07-01 17:45:15 +0530601
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530602 return frappe._dict({
Anand Doshidffec8f2014-07-01 17:45:15 +0530603 "price_list_currency": price_list_currency,
Shreya Shahb13a54a2017-12-06 19:17:04 +0530604 "price_list_uom_dependant": price_list_uom_dependant,
Anand Doshidffec8f2014-07-01 17:45:15 +0530605 "plc_conversion_rate": plc_conversion_rate
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530606 })
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530607
608@frappe.whitelist()
609def get_default_bom(item_code=None):
610 if item_code:
611 bom = frappe.db.get_value("BOM", {"docstatus": 1, "is_default": 1, "is_active": 1, "item": item_code})
612 if bom:
613 return bom
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530614
Saurabh3fbf3ce2016-03-09 15:31:04 +0530615def get_valuation_rate(item_code, warehouse=None):
Saurabhbd01a812016-03-07 14:32:23 +0530616 item = frappe.get_doc("Item", item_code)
617 if item.is_stock_item:
Saurabhbd01a812016-03-07 14:32:23 +0530618 if not warehouse:
619 warehouse = item.default_warehouse
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530620
621 return frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse},
Saurabhbd01a812016-03-07 14:32:23 +0530622 ["valuation_rate"], as_dict=True) or {"valuation_rate": 0}
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530623
Saurabhbd01a812016-03-07 14:32:23 +0530624 elif not item.is_stock_item:
Nabin Haitfe876c02017-03-06 16:32:57 +0530625 valuation_rate =frappe.db.sql("""select sum(base_net_amount) / sum(qty*conversion_factor)
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530626 from `tabPurchase Invoice Item`
Saurabhbd01a812016-03-07 14:32:23 +0530627 where item_code = %s and docstatus=1""", item_code)
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530628
Saurabhbd01a812016-03-07 14:32:23 +0530629 if valuation_rate:
630 return {"valuation_rate": valuation_rate[0][0] or 0.0}
631 else:
632 return {"valuation_rate": 0.0}
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530633
Saurabh2d7af632016-02-26 11:09:20 +0530634def get_gross_profit(out):
Saurabh5ada14b2016-02-26 18:02:55 +0530635 if out.valuation_rate:
636 out.update({
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530637 "gross_profit": ((out.base_rate - out.valuation_rate) * out.stock_qty)
Saurabh5ada14b2016-02-26 18:02:55 +0530638 })
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530639
Saurabhd3135532016-02-25 18:59:20 +0530640 return out
mbauskar6f603392016-01-21 16:10:22 +0530641
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530642@frappe.whitelist()
Rohit Waghchaureba3f0e62017-08-28 17:19:28 +0530643def get_serial_no(args, serial_nos=None):
644 serial_no = None
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530645 if isinstance(args, basestring):
646 args = json.loads(args)
647 args = frappe._dict(args)
648
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530649 if args.get('doctype') == 'Sales Invoice' and not args.get('update_stock'):
650 return ""
651
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530652 if args.get('warehouse') and args.get('stock_qty') and args.get('item_code'):
Rushabh Mehta985cb822016-12-30 15:06:54 +0530653
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530654 if frappe.get_value('Item', {'item_code': args.item_code}, "has_serial_no") == 1:
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530655 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 +0530656 args = process_args(args)
657 serial_no = get_serial_nos_by_fifo(args)
Rohit Waghchaureba3f0e62017-08-28 17:19:28 +0530658
659 if not serial_no and serial_nos:
660 # For POS
661 serial_no = serial_nos
662
663 return serial_no