blob: bcef47278593d500b69ebf81cff90be3aaab6fb5 [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
Achilles Rasquinha714c7462018-02-15 11:28:55 +053015from six import string_types, iteritems
tundebabzyacccdb32017-11-23 08:35:15 +010016
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053017@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +053018def get_item_details(args):
19 """
20 args = {
21 "item_code": "",
22 "warehouse": None,
23 "customer": "",
24 "conversion_rate": 1.0,
25 "selling_price_list": None,
26 "price_list_currency": None,
Nabin Hait615d3422014-02-25 19:01:20 +053027 "plc_conversion_rate": 1.0,
Rushabh Mehtab46069d2016-01-15 16:59:26 +053028 "doctype": "",
29 "name": "",
Nabin Hait436f5262014-02-10 14:47:54 +053030 "supplier": None,
31 "transaction_date": None,
32 "conversion_rate": 1.0,
33 "buying_price_list": None,
34 "is_subcontracted": "Yes" / "No",
Nabin Hait444f9562014-06-20 15:59:49 +053035 "ignore_pricing_rule": 0/1
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +053036 "project": ""
Nabin Hait436f5262014-02-10 14:47:54 +053037 }
38 """
Anand Doshidffec8f2014-07-01 17:45:15 +053039 args = process_args(args)
Rushabh Mehtaf14b8092014-04-03 14:30:42 +053040 item_doc = frappe.get_doc("Item", args.item_code)
41 item = item_doc
Nabin Hait436f5262014-02-10 14:47:54 +053042
43 validate_item_details(args, item)
Anand Doshibd67e872014-04-11 16:51:27 +053044
Rushabh Mehta1a763742014-10-03 16:36:43 +053045 out = get_basic_details(args, item)
Anand Doshibd67e872014-04-11 16:51:27 +053046
Rushabh Mehtaf14b8092014-04-03 14:30:42 +053047 get_party_item_code(args, item_doc, out)
Nabin Hait436f5262014-02-10 14:47:54 +053048
Saurabh3fbf3ce2016-03-09 15:31:04 +053049 if frappe.db.exists("Product Bundle", args.item_code):
Saurabhbd01a812016-03-07 14:32:23 +053050 valuation_rate = 0.0
Saurabh3fbf3ce2016-03-09 15:31:04 +053051 bundled_items = frappe.get_doc("Product Bundle", args.item_code)
Rushabh Mehta0394aec2016-04-22 17:22:22 +053052
Saurabh3fbf3ce2016-03-09 15:31:04 +053053 for bundle_item in bundled_items.items:
54 valuation_rate += \
55 flt(get_valuation_rate(bundle_item.item_code, out.get("warehouse")).get("valuation_rate") \
56 * bundle_item.qty)
Saurabhbd01a812016-03-07 14:32:23 +053057
58 out.update({
59 "valuation_rate": valuation_rate
60 })
Saurabh3fbf3ce2016-03-09 15:31:04 +053061
Saurabh5ada14b2016-02-26 18:02:55 +053062 else:
Saurabh3fbf3ce2016-03-09 15:31:04 +053063 out.update(get_valuation_rate(args.item_code, out.get("warehouse")))
Anand Doshibd67e872014-04-11 16:51:27 +053064
Rushabh Mehtaf14b8092014-04-03 14:30:42 +053065 get_price_list_rate(args, item_doc, out)
Nabin Hait436f5262014-02-10 14:47:54 +053066
Rushabh Mehtab46069d2016-01-15 16:59:26 +053067 if args.customer and cint(args.is_pos):
Rushabh Mehtaad9156a2015-08-18 15:29:42 +053068 out.update(get_pos_profile_item_details(args.company, args))
Rushabh Mehta6b537922017-03-14 16:59:11 +053069
Nabin Hait0287d312017-01-11 10:31:33 +053070 if out.get("warehouse"):
71 out.update(get_bin_details(args.item_code, out.warehouse))
Anand Doshibd67e872014-04-11 16:51:27 +053072
Nabin Haita3dd72a2014-05-28 12:49:20 +053073 # update args with out, if key or value not exists
Achilles Rasquinha714c7462018-02-15 11:28:55 +053074 for key, value in iteritems(out):
Nabin Haita3dd72a2014-05-28 12:49:20 +053075 if args.get(key) is None:
76 args[key] = value
77
Nabin Hait444f9562014-06-20 15:59:49 +053078 out.update(get_pricing_rule_for_item(args))
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
Nabin Hait8fac2ad2017-05-18 11:54:24 +053083 if out.has_batch_no and not args.get("batch_no"):
Rushabh Mehta551406a2017-04-21 12:40:19 +053084 out.batch_no = get_batch_no(out.item_code, out.warehouse, out.qty)
rohitwaghchaure2fb8cc52017-11-29 13:50:04 +053085 actual_batch_qty = get_batch_qty(out.batch_no, out.warehouse, out.item_code)
86 if actual_batch_qty:
87 out.update(actual_batch_qty)
Shreya14bd43d2018-04-27 16:24:05 +053088
89 if out.has_serial_no and args.get('batch_no'):
90 out.batch_no = args.get('batch_no')
91 out.serial_no = get_serial_no(out, args.serial_no)
92
93 elif out.has_serial_no:
Shreyaa20157a2018-04-13 12:03:42 +053094 out.serial_no = get_serial_no(out, args.serial_no)
Rushabh Mehta551406a2017-04-21 12:40:19 +053095
Nabin Hait139dc7b2014-02-12 14:53:18 +053096 if args.transaction_date and item.lead_time_days:
97 out.schedule_date = out.lead_time_date = add_days(args.transaction_date,
98 item.lead_time_days)
Anand Doshibd67e872014-04-11 16:51:27 +053099
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530100 if args.get("is_subcontracted") == "Yes":
Saurabh590d4012017-09-18 15:49:19 +0530101 out.bom = args.get('bom') or get_default_bom(args.item_code)
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530102
Saurabh2d7af632016-02-26 11:09:20 +0530103 get_gross_profit(out)
Anand Doshi70f57eb2015-11-27 14:37:40 +0530104
Nabin Hait436f5262014-02-10 14:47:54 +0530105 return out
106
Anand Doshidffec8f2014-07-01 17:45:15 +0530107def process_args(args):
Achilles Rasquinha56b2e122018-02-13 14:42:40 +0530108 if isinstance(args, string_types):
Anand Doshidffec8f2014-07-01 17:45:15 +0530109 args = json.loads(args)
110
111 args = frappe._dict(args)
112
Anand Doshidffec8f2014-07-01 17:45:15 +0530113 if not args.get("price_list"):
114 args.price_list = args.get("selling_price_list") or args.get("buying_price_list")
115
116 if args.barcode:
117 args.item_code = get_item_code(barcode=args.barcode)
118 elif not args.item_code and args.serial_no:
119 args.item_code = get_item_code(serial_no=args.serial_no)
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530120
mbauskar6f603392016-01-21 16:10:22 +0530121 set_transaction_type(args)
Anand Doshidffec8f2014-07-01 17:45:15 +0530122 return args
123
tundebabzyacccdb32017-11-23 08:35:15 +0100124
Neil Trini Lasrado4abda672015-02-25 17:34:27 +0530125@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530126def get_item_code(barcode=None, serial_no=None):
127 if barcode:
Giovanni2144e022017-12-10 17:27:09 +0100128 item_code = frappe.db.get_value("Item Barcode", {"barcode": barcode}, fieldname=["parent"])
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530129 if not item_code:
130 frappe.throw(_("No Item with Barcode {0}").format(barcode))
Nabin Hait436f5262014-02-10 14:47:54 +0530131 elif serial_no:
Anand Doshie9baaa62014-02-26 12:35:33 +0530132 item_code = frappe.db.get_value("Serial No", serial_no, "item_code")
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530133 if not item_code:
134 frappe.throw(_("No Item with Serial No {0}").format(serial_no))
Anand Doshibd67e872014-04-11 16:51:27 +0530135
Nabin Hait436f5262014-02-10 14:47:54 +0530136 return item_code
Anand Doshibd67e872014-04-11 16:51:27 +0530137
tundebabzyacccdb32017-11-23 08:35:15 +0100138
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
tundebabzyacccdb32017-11-23 08:35:15 +0100146 if args.transaction_type == "selling" and cint(item.has_variants):
Umair Sayyed72534de2016-04-15 12:52:12 +0530147 throw(_("Item {0} is a template, please select one of its variants").format(item.name))
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530148
tundebabzyacccdb32017-11-23 08:35:15 +0100149 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
tundebabzyacccdb32017-11-23 08:35:15 +0100153
Rushabh Mehta1a763742014-10-03 16:36:43 +0530154def get_basic_details(args, item):
tundebabzyacccdb32017-11-23 08:35:15 +0100155 """
156 :param args: {
157 "item_code": "",
158 "warehouse": None,
159 "customer": "",
160 "conversion_rate": 1.0,
161 "selling_price_list": None,
162 "price_list_currency": None,
Shreya Shahb13a54a2017-12-06 19:17:04 +0530163 "price_list_uom_dependant": None,
tundebabzyacccdb32017-11-23 08:35:15 +0100164 "plc_conversion_rate": 1.0,
165 "doctype": "",
166 "name": "",
167 "supplier": None,
168 "transaction_date": None,
169 "conversion_rate": 1.0,
170 "buying_price_list": None,
171 "is_subcontracted": "Yes" / "No",
172 "ignore_pricing_rule": 0/1
173 "project": "",
174 barcode: "",
175 serial_no: "",
176 warehouse: "",
177 currency: "",
178 update_stock: "",
179 price_list: "",
180 company: "",
181 order_type: "",
182 is_pos: "",
183 ignore_pricing_rule: "",
184 project: "",
185 qty: "",
186 stock_qty: "",
187 conversion_factor: ""
188 }
189 :param item: `item_code` of Item object
190 :return: frappe._dict
191 """
192
Rushabh Mehta1a763742014-10-03 16:36:43 +0530193 if not item:
194 item = frappe.get_doc("Item", args.get("item_code"))
Anand Doshibd67e872014-04-11 16:51:27 +0530195
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530196 if item.variant_of:
197 item.update_template_tables()
198
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530199 from frappe.defaults import get_user_default_as_list
Nabin Haitb6b56452015-12-23 16:37:00 +0530200 user_default_warehouse_list = get_user_default_as_list('Warehouse')
Nabin Hait436f5262014-02-10 14:47:54 +0530201 user_default_warehouse = user_default_warehouse_list[0] \
tundebabzyacccdb32017-11-23 08:35:15 +0100202 if len(user_default_warehouse_list) == 1 else ""
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530203
Rohit Waghchaure6734c4a2017-01-06 14:39:40 +0530204 warehouse = user_default_warehouse or item.default_warehouse or args.warehouse
Nabin Hait36028bf2014-02-12 19:09:28 +0530205
rohitwaghchauree8ccc0e2017-11-21 16:17:22 +0530206 material_request_type = ''
207 if args.get('doctype') == "Material Request":
208 material_request_type = frappe.db.get_value('Material Request',
209 args.get('name'), 'material_request_type')
210
Nabin Hait79a1d2a2017-09-29 18:14:10 +0530211 #Set the UOM to the Default Sales UOM or Default Purchase UOM if configured in the Item Master
212 if not args.uom:
213 if args.get('doctype') in ['Quotation', 'Sales Order', 'Delivery Note', 'Sales Invoice']:
214 args.uom = item.sales_uom if item.sales_uom else item.stock_uom
rohitwaghchauree8ccc0e2017-11-21 16:17:22 +0530215 elif (args.get('doctype') in ['Purchase Order', 'Purchase Receipt', 'Purchase Invoice']) or \
216 (args.get('doctype') == 'Material Request' and material_request_type == 'Purchase'):
Nabin Hait79a1d2a2017-09-29 18:14:10 +0530217 args.uom = item.purchase_uom if item.purchase_uom else item.stock_uom
218 else:
219 args.uom = item.stock_uom
220
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530221 out = frappe._dict({
Nabin Hait139dc7b2014-02-12 14:53:18 +0530222 "item_code": item.name,
223 "item_name": item.item_name,
Neil Trini Lasradoc48f06c2015-02-10 16:49:16 +0530224 "description": cstr(item.description).strip(),
225 "image": cstr(item.image).strip(),
Saurabhd3135532016-02-25 18:59:20 +0530226 "warehouse": warehouse,
Rushabh Mehta1a763742014-10-03 16:36:43 +0530227 "income_account": get_default_income_account(args, item),
228 "expense_account": get_default_expense_account(args, item),
229 "cost_center": get_default_cost_center(args, item),
Rushabh Mehta551406a2017-04-21 12:40:19 +0530230 'has_serial_no': item.has_serial_no,
231 'has_batch_no': item.has_batch_no,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530232 "batch_no": None,
Anand Doshibd67e872014-04-11 16:51:27 +0530233 "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in
Nabin Haite7d15362014-12-25 16:01:55 +0530234 item.get("taxes")))),
Nabin Hait79a1d2a2017-09-29 18:14:10 +0530235 "uom": args.uom,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530236 "min_order_qty": flt(item.min_order_qty) if args.doctype == "Material Request" else "",
Nabin Hait4a6c1782015-05-29 17:31:31 +0530237 "qty": args.qty or 1.0,
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530238 "stock_qty": args.qty or 1.0,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530239 "price_list_rate": 0.0,
240 "base_price_list_rate": 0.0,
241 "rate": 0.0,
242 "base_rate": 0.0,
243 "amount": 0.0,
244 "base_amount": 0.0,
Nabin Hait82e3e252015-02-23 16:58:30 +0530245 "net_rate": 0.0,
246 "net_amount": 0.0,
Saurabhc6dbe702015-10-19 14:17:52 +0530247 "discount_percentage": 0.0,
248 "supplier": item.default_supplier,
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530249 "update_stock": args.get("update_stock") if args.get('doctype') in ['Sales Invoice', 'Purchase Invoice'] else 0,
Rohit Waghchaurec858d522016-12-12 13:01:43 +0530250 "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 +0530251 "is_fixed_asset": item.is_fixed_asset,
252 "weight_per_unit":item.weight_per_unit,
253 "weight_uom":item.weight_uom,
Shreya Shah44fa9a62018-01-08 14:58:20 +0530254 "last_purchase_rate": item.last_purchase_rate if args.get("doctype") in ["Purchase Order"] else 0
Nabin Hait139dc7b2014-02-12 14:53:18 +0530255 })
Anand Doshibd67e872014-04-11 16:51:27 +0530256
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530257 # calculate conversion factor
mbauskar287fe812017-04-10 19:15:57 +0530258 if item.stock_uom == args.uom:
259 out.conversion_factor = 1.0
260 else:
261 out.conversion_factor = args.conversion_factor or \
tundebabzyacccdb32017-11-23 08:35:15 +0100262 get_conversion_factor(item.item_code, args.uom).get("conversion_factor") or 1.0
mbauskar287fe812017-04-10 19:15:57 +0530263
264 args.conversion_factor = out.conversion_factor
265 out.stock_qty = out.qty * out.conversion_factor
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530266
Shreya Shah44fa9a62018-01-08 14:58:20 +0530267 # calculate last purchase rate
268 from erpnext.buying.doctype.purchase_order.purchase_order import item_last_purchase_rate
269 out.last_purchase_rate = item_last_purchase_rate(args.name, args.conversion_rate, item.item_code, out.conversion_factor)
270
Nabin Haitb09ed412015-02-17 10:33:58 +0530271 # if default specified in item is for another company, fetch from company
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530272 for d in [
273 ["Account", "income_account", "default_income_account"],
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530274 ["Account", "expense_account", "default_expense_account"],
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530275 ["Cost Center", "cost_center", "cost_center"],
276 ["Warehouse", "warehouse", ""]]:
Nabin Hait98a8fae2015-02-26 11:37:07 +0530277 company = frappe.db.get_value(d[0], out.get(d[1]), "company")
278 if not out[d[1]] or (company and args.company != company):
Nabin Haitb09ed412015-02-17 10:33:58 +0530279 out[d[1]] = frappe.db.get_value("Company", args.company, d[2]) if d[2] else None
280
Giovanni2144e022017-12-10 17:27:09 +0100281 for fieldname in ("item_name", "item_group", "barcodes", "brand", "stock_uom"):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530282 out[fieldname] = item.get(fieldname)
Anand Doshibd67e872014-04-11 16:51:27 +0530283
Nabin Hait436f5262014-02-10 14:47:54 +0530284 return out
Anand Doshibd67e872014-04-11 16:51:27 +0530285
tundebabzyacccdb32017-11-23 08:35:15 +0100286
Rushabh Mehta1a763742014-10-03 16:36:43 +0530287def get_default_income_account(args, item):
288 return (item.income_account
289 or args.income_account
Anand Doshid35354c2015-02-24 18:12:17 +0530290 or frappe.db.get_value("Item Group", item.item_group, "default_income_account"))
Rushabh Mehta1a763742014-10-03 16:36:43 +0530291
292def get_default_expense_account(args, item):
293 return (item.expense_account
294 or args.expense_account
Anand Doshid35354c2015-02-24 18:12:17 +0530295 or frappe.db.get_value("Item Group", item.item_group, "default_expense_account"))
Rushabh Mehta1a763742014-10-03 16:36:43 +0530296
297def get_default_cost_center(args, item):
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +0530298 return (frappe.db.get_value("Project", args.get("project"), "cost_center")
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530299 or (item.selling_cost_center if args.get("customer") else item.buying_cost_center)
gmarkee5a31462015-09-27 09:38:01 +0200300 or frappe.db.get_value("Item Group", item.item_group, "default_cost_center")
301 or args.get("cost_center"))
Rushabh Mehta1a763742014-10-03 16:36:43 +0530302
Rushabh Mehtaf14b8092014-04-03 14:30:42 +0530303def get_price_list_rate(args, item_doc, out):
Anand Doshi39a28912016-02-15 11:42:18 +0530304 meta = frappe.get_meta(args.parenttype or args.doctype)
Nabin Hait436f5262014-02-10 14:47:54 +0530305
306 if meta.get_field("currency"):
307 validate_price_list(args)
rohitwaghchaure4cccdbd2017-07-25 14:05:01 +0530308 if args.price_list:
309 validate_conversion_rate(args, meta)
Nabin Hait436f5262014-02-10 14:47:54 +0530310
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530311 price_list_rate = get_price_list_rate_for(args.price_list, item_doc.name)
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530312
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530313 # variant
314 if not price_list_rate and item_doc.variant_of:
315 price_list_rate = get_price_list_rate_for(args.price_list, item_doc.variant_of)
316
317 # insert in database
Rushabh Mehta1a763742014-10-03 16:36:43 +0530318 if not price_list_rate:
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530319 if args.price_list and args.rate:
320 insert_item_price(args)
Rushabh Mehta1a763742014-10-03 16:36:43 +0530321 return {}
Anand Doshibd67e872014-04-11 16:51:27 +0530322
Nabin Hait436f5262014-02-10 14:47:54 +0530323 out.price_list_rate = flt(price_list_rate) * flt(args.plc_conversion_rate) \
324 / flt(args.conversion_rate)
Zarrar17fd6612017-12-08 14:57:38 +0530325 if not args.price_list_uom_dependant:
Manas Solanki94769d82018-03-29 11:15:05 +0530326 out.price_list_rate = flt(out.price_list_rate * (flt(args.conversion_factor) or 1.0))
mbauskar287fe812017-04-10 19:15:57 +0530327
Nabin Hait34d28222016-01-19 15:45:49 +0530328 if not out.price_list_rate and args.transaction_type=="buying":
Nabin Hait436f5262014-02-10 14:47:54 +0530329 from erpnext.stock.doctype.item.item import get_last_purchase_details
Anand Doshibd67e872014-04-11 16:51:27 +0530330 out.update(get_last_purchase_details(item_doc.name,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530331 args.name, args.conversion_rate))
Anand Doshibd67e872014-04-11 16:51:27 +0530332
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530333def insert_item_price(args):
334 """Insert Item Price if Price List and Price List Rate are specified and currency is the same"""
335 if frappe.db.get_value("Price List", args.price_list, "currency") == args.currency \
336 and cint(frappe.db.get_single_value("Stock Settings", "auto_insert_price_list_rate_if_missing")):
337 if frappe.has_permission("Item Price", "write"):
Nabin Hait906bf642015-09-02 11:32:07 +0530338 price_list_rate = args.rate / args.conversion_factor \
339 if args.get("conversion_factor") else args.rate
Rushabh Mehta985cb822016-12-30 15:06:54 +0530340
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530341 item_price = frappe.get_doc({
342 "doctype": "Item Price",
343 "price_list": args.price_list,
344 "item_code": args.item_code,
345 "currency": args.currency,
Nabin Hait906bf642015-09-02 11:32:07 +0530346 "price_list_rate": price_list_rate
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530347 })
Rushabh Mehta985cb822016-12-30 15:06:54 +0530348
Kanchan Chauhan2ff7d2a2016-06-25 17:54:25 +0530349 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 +0530350
Kanchan Chauhan5ee8fcc2016-06-24 17:11:15 +0530351 if name:
352 item_price = frappe.get_doc('Item Price', name)
353 item_price.price_list_rate = price_list_rate
Rushabh Mehta985cb822016-12-30 15:06:54 +0530354 item_price.save()
Kanchan Chauhan5ee8fcc2016-06-24 17:11:15 +0530355 frappe.msgprint(_("Item Price updated for {0} in Price List {1}").format(args.item_code,
356 args.price_list))
Rushabh Mehta985cb822016-12-30 15:06:54 +0530357 else:
Kanchan Chauhan5ee8fcc2016-06-24 17:11:15 +0530358 item_price.insert()
359 frappe.msgprint(_("Item Price added for {0} in Price List {1}").format(args.item_code,
360 args.price_list))
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530361
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530362def get_price_list_rate_for(price_list, item_code):
Rushabh Mehta1a763742014-10-03 16:36:43 +0530363 return frappe.db.get_value("Item Price",
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530364 {"price_list": price_list, "item_code": item_code}, "price_list_rate")
Rushabh Mehta1a763742014-10-03 16:36:43 +0530365
Nabin Hait436f5262014-02-10 14:47:54 +0530366def validate_price_list(args):
367 if args.get("price_list"):
Anand Doshibd67e872014-04-11 16:51:27 +0530368 if not frappe.db.get_value("Price List",
Nabin Hait34d28222016-01-19 15:45:49 +0530369 {"name": args.price_list, args.transaction_type: 1, "enabled": 1}):
Rushabh Mehta985cb822016-12-30 15:06:54 +0530370 throw(_("Price List {0} is disabled or does not exist").format(args.price_list))
mbauskar0b665ac2017-04-11 02:49:24 -0400371 elif not args.get("supplier"):
Nabin Hait436f5262014-02-10 14:47:54 +0530372 throw(_("Price List not selected"))
Anand Doshibd67e872014-04-11 16:51:27 +0530373
Nabin Hait436f5262014-02-10 14:47:54 +0530374def validate_conversion_rate(args, meta):
Rushabh Mehta66e08e32014-09-29 12:17:03 +0530375 from erpnext.controllers.accounts_controller import validate_conversion_rate
Anand Doshibd67e872014-04-11 16:51:27 +0530376
Anand Doshife13bfe2015-08-25 12:49:40 +0530377 if (not args.conversion_rate
378 and args.currency==frappe.db.get_value("Company", args.company, "default_currency")):
379 args.conversion_rate = 1.0
380
Nabin Hait436f5262014-02-10 14:47:54 +0530381 # validate currency conversion rate
Anand Doshibd67e872014-04-11 16:51:27 +0530382 validate_conversion_rate(args.currency, args.conversion_rate,
Nabin Hait436f5262014-02-10 14:47:54 +0530383 meta.get_label("conversion_rate"), args.company)
Anand Doshibd67e872014-04-11 16:51:27 +0530384
385 args.conversion_rate = flt(args.conversion_rate,
386 get_field_precision(meta.get_field("conversion_rate"),
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530387 frappe._dict({"fields": args})))
Anand Doshibd67e872014-04-11 16:51:27 +0530388
Nabin Hait436f5262014-02-10 14:47:54 +0530389 # validate price list currency conversion rate
390 if not args.get("price_list_currency"):
391 throw(_("Price List Currency not selected"))
392 else:
Anand Doshibd67e872014-04-11 16:51:27 +0530393 validate_conversion_rate(args.price_list_currency, args.plc_conversion_rate,
Nabin Hait436f5262014-02-10 14:47:54 +0530394 meta.get_label("plc_conversion_rate"), args.company)
Anand Doshibd67e872014-04-11 16:51:27 +0530395
396 args.plc_conversion_rate = flt(args.plc_conversion_rate,
397 get_field_precision(meta.get_field("plc_conversion_rate"),
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530398 frappe._dict({"fields": args})))
Anand Doshibd67e872014-04-11 16:51:27 +0530399
Rushabh Mehtaf14b8092014-04-03 14:30:42 +0530400def get_party_item_code(args, item_doc, out):
Nabin Hait34d28222016-01-19 15:45:49 +0530401 if args.transaction_type=="selling" and args.customer:
Zarrar3fd63472018-03-09 12:10:45 +0530402 out.customer_item_code = None
Nabin Hait79f091e2014-12-26 11:41:15 +0530403 customer_item_code = item_doc.get("customer_items", {"customer_name": args.customer})
Zarrar3fd63472018-03-09 12:10:45 +0530404
405 if customer_item_code:
406 out.customer_item_code = customer_item_code[0].ref_code
407 else:
408 customer_group = frappe.db.get_value("Customer", args.customer, "customer_group")
409 customer_group_item_code = item_doc.get("customer_items", {"customer_group": customer_group})
410 if customer_group_item_code and not customer_group_item_code[0].customer_name:
411 out.customer_item_code = customer_group_item_code[0].ref_code
Anand Doshi54abf022016-01-21 22:28:47 +0530412
Nabin Hait34d28222016-01-19 15:45:49 +0530413 if args.transaction_type=="buying" and args.supplier:
Nabin Hait79f091e2014-12-26 11:41:15 +0530414 item_supplier = item_doc.get("supplier_items", {"supplier": args.supplier})
Nabin Hait436f5262014-02-10 14:47:54 +0530415 out.supplier_part_no = item_supplier[0].supplier_part_no if item_supplier else None
Anand Doshibd67e872014-04-11 16:51:27 +0530416
rohitwaghchaure2ea593b2018-04-12 13:37:08 +0530417def get_pos_profile_item_details(company, args, pos_profile=None, update_data=False):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530418 res = frappe._dict()
Anand Doshibd67e872014-04-11 16:51:27 +0530419
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530420 if not pos_profile:
rohitwaghchaurec037dc72017-11-28 16:11:15 +0530421 pos_profile = get_pos_profile(company, args.get('pos_profile'))
Anand Doshibd67e872014-04-11 16:51:27 +0530422
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530423 if pos_profile:
Nabin Hait436f5262014-02-10 14:47:54 +0530424 for fieldname in ("income_account", "cost_center", "warehouse", "expense_account"):
rohitwaghchaure2ea593b2018-04-12 13:37:08 +0530425 if (not args.get(fieldname) or update_data) and pos_profile.get(fieldname):
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530426 res[fieldname] = pos_profile.get(fieldname)
Anand Doshibd67e872014-04-11 16:51:27 +0530427
Nabin Hait436f5262014-02-10 14:47:54 +0530428 if res.get("warehouse"):
Saurabhd3135532016-02-25 18:59:20 +0530429 res.actual_qty = get_bin_details(args.item_code,
Nabin Hait436f5262014-02-10 14:47:54 +0530430 res.warehouse).get("actual_qty")
Anand Doshibd67e872014-04-11 16:51:27 +0530431
Nabin Hait436f5262014-02-10 14:47:54 +0530432 return res
433
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530434@frappe.whitelist()
rohitwaghchaurec037dc72017-11-28 16:11:15 +0530435def get_pos_profile(company, pos_profile=None, user=None):
436 if pos_profile:
437 return frappe.get_doc('POS Profile', pos_profile)
438
439 if not user:
440 user = frappe.session['user']
441
442 pos_profile = frappe.db.sql("""select pf.*
443 from
444 `tabPOS Profile` pf, `tabPOS Profile User` pfu
445 where
446 pfu.parent = pf.name and pfu.user = %s and pf.company = %s
447 and pf.disabled = 0 and pfu.default=1""", (user, company), as_dict=1)
Anand Doshibd67e872014-04-11 16:51:27 +0530448
Nabin Haitc469f2c2017-04-03 13:01:11 +0530449 if not pos_profile:
rohitwaghchaurec037dc72017-11-28 16:11:15 +0530450 pos_profile = frappe.db.sql("""select pf.*
451 from
452 `tabPOS Profile` pf left join `tabPOS Profile User` pfu
453 on
454 pf.name = pfu.parent
455 where
456 ifnull(pfu.user, '') = '' and pf.company = %s
457 and pf.disabled = 0""", (company), as_dict=1)
Anand Doshibd67e872014-04-11 16:51:27 +0530458
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530459 return pos_profile and pos_profile[0] or None
Anand Doshibd67e872014-04-11 16:51:27 +0530460
Kanchan Chauhan0a3f2c82016-10-25 23:15:39 +0530461def get_serial_nos_by_fifo(args):
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530462 if frappe.db.get_single_value("Stock Settings", "automatically_set_serial_nos_based_on_fifo"):
463 return "\n".join(frappe.db.sql_list("""select name from `tabSerial No`
Nabin Hait398c83a2015-10-22 15:11:44 +0530464 where item_code=%(item_code)s and warehouse=%(warehouse)s
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530465 order by timestamp(purchase_date, purchase_time) asc limit %(qty)s""", {
466 "item_code": args.item_code,
467 "warehouse": args.warehouse,
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530468 "qty": abs(cint(args.stock_qty))
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530469 }))
Nabin Hait08222152014-02-28 11:30:47 +0530470
Shreyaa20157a2018-04-13 12:03:42 +0530471def get_serial_no_batchwise(args):
472 if frappe.db.get_single_value("Stock Settings", "automatically_set_serial_nos_based_on_fifo"):
473 return "\n".join(frappe.db.sql_list("""select name from `tabSerial No`
Shreya14bd43d2018-04-27 16:24:05 +0530474 where item_code=%(item_code)s and warehouse=%(warehouse)s and (batch_no=%(batch_no)s or batch_no is NULL)
Shreyaa20157a2018-04-13 12:03:42 +0530475 order by timestamp(purchase_date, purchase_time) asc limit %(qty)s""", {
476 "item_code": args.item_code,
477 "warehouse": args.warehouse,
478 "batch_no": args.batch_no,
479 "qty": abs(cint(args.stock_qty))
480 }))
481
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530482@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530483def get_conversion_factor(item_code, uom):
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530484 variant_of = frappe.db.get_value("Item", item_code, "variant_of")
485 filters = {"parent": item_code, "uom": uom}
486 if variant_of:
Nabin Hait314086d2015-10-07 11:55:01 +0530487 filters["parent"] = ("in", (item_code, variant_of))
Anand Doshie9baaa62014-02-26 12:35:33 +0530488 return {"conversion_factor": frappe.db.get_value("UOM Conversion Detail",
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530489 filters, "conversion_factor")}
Nabin Hait08222152014-02-28 11:30:47 +0530490
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530491@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530492def get_projected_qty(item_code, warehouse):
Anand Doshibd67e872014-04-11 16:51:27 +0530493 return {"projected_qty": frappe.db.get_value("Bin",
Nabin Hait436f5262014-02-10 14:47:54 +0530494 {"item_code": item_code, "warehouse": warehouse}, "projected_qty")}
Nabin Hait08222152014-02-28 11:30:47 +0530495
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530496@frappe.whitelist()
Saurabhd3135532016-02-25 18:59:20 +0530497def get_bin_details(item_code, warehouse):
Anand Doshibd67e872014-04-11 16:51:27 +0530498 return frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse},
Manas Solankia884bd92018-02-09 13:12:07 +0530499 ["projected_qty", "actual_qty"], as_dict=True) \
500 or {"projected_qty": 0, "actual_qty": 0}
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530501
502@frappe.whitelist()
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530503def get_serial_no_details(item_code, warehouse, stock_qty, serial_no):
504 args = frappe._dict({"item_code":item_code, "warehouse":warehouse, "stock_qty":stock_qty, "serial_no":serial_no})
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530505 serial_no = get_serial_no(args)
506 return {'serial_no': serial_no}
Rushabh Mehta985cb822016-12-30 15:06:54 +0530507
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530508@frappe.whitelist()
Shreyaa20157a2018-04-13 12:03:42 +0530509def get_bin_details_and_serial_nos(item_code, warehouse, has_batch_no, stock_qty=None, serial_no=None):
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530510 bin_details_and_serial_nos = {}
511 bin_details_and_serial_nos.update(get_bin_details(item_code, warehouse))
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530512 if stock_qty > 0:
Shreyaa20157a2018-04-13 12:03:42 +0530513 if has_batch_no:
514 args = frappe._dict({"item_code":item_code, "warehouse":warehouse, "stock_qty":stock_qty})
515 serial_no = get_serial_no(args)
516 bin_details_and_serial_nos.update({'serial_no': serial_no})
517 return bin_details_and_serial_nos
518
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530519 bin_details_and_serial_nos.update(get_serial_no_details(item_code, warehouse, stock_qty, serial_no))
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530520 return bin_details_and_serial_nos
Anand Doshidffec8f2014-07-01 17:45:15 +0530521
522@frappe.whitelist()
Shreyaa20157a2018-04-13 12:03:42 +0530523def get_batch_qty_and_serial_no(batch_no, stock_qty, warehouse, item_code, has_serial_no):
524 batch_qty_and_serial_no = {}
525 batch_qty_and_serial_no.update(get_batch_qty(batch_no, warehouse, item_code))
526
527 if (flt(batch_qty_and_serial_no.get('actual_batch_qty')) >= flt(stock_qty)) and has_serial_no:
528 args = frappe._dict({"item_code":item_code, "warehouse":warehouse, "stock_qty":stock_qty, "batch_no":batch_no})
529 serial_no = get_serial_no(args)
530 batch_qty_and_serial_no.update({'serial_no': serial_no})
531 return batch_qty_and_serial_no
532
533@frappe.whitelist()
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530534def get_batch_qty(batch_no, warehouse, item_code):
sburanaw66951e52017-04-25 15:28:57 +0700535 from erpnext.stock.doctype.batch import batch
Sambhaji Kolate98dbccd2015-03-10 15:04:28 +0530536 if batch_no:
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530537 return {'actual_batch_qty': batch.get_batch_qty(batch_no, warehouse)}
Rushabh Mehta1e8025b2015-07-24 15:16:25 +0530538
Anand Doshidffec8f2014-07-01 17:45:15 +0530539@frappe.whitelist()
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530540def apply_price_list(args, as_doc=False):
541 """Apply pricelist on a document-like dict object and return as
542 {'parent': dict, 'children': list}
543
544 :param args: See below
545 :param as_doc: Updates value in the passed dict
546
Anand Doshidffec8f2014-07-01 17:45:15 +0530547 args = {
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530548 "doctype": "",
549 "name": "",
550 "items": [{"doctype": "", "name": "", "item_code": "", "brand": "", "item_group": ""}, ...],
Anand Doshidffec8f2014-07-01 17:45:15 +0530551 "conversion_rate": 1.0,
552 "selling_price_list": None,
553 "price_list_currency": None,
Shreya Shahb13a54a2017-12-06 19:17:04 +0530554 "price_list_uom_dependant": None,
Anand Doshidffec8f2014-07-01 17:45:15 +0530555 "plc_conversion_rate": 1.0,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530556 "doctype": "",
557 "name": "",
Anand Doshidffec8f2014-07-01 17:45:15 +0530558 "supplier": None,
559 "transaction_date": None,
560 "conversion_rate": 1.0,
561 "buying_price_list": None,
Anand Doshidffec8f2014-07-01 17:45:15 +0530562 "ignore_pricing_rule": 0/1
563 }
564 """
565 args = process_args(args)
566
567 parent = get_price_list_currency_and_exchange_rate(args)
568 children = []
569
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530570 if "items" in args:
571 item_list = args.get("items")
Anand Doshidffec8f2014-07-01 17:45:15 +0530572 args.update(parent)
573
574 for item in item_list:
575 args_copy = frappe._dict(args.copy())
576 args_copy.update(item)
577 item_details = apply_price_list_on_item(args_copy)
578 children.append(item_details)
579
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530580 if as_doc:
Shreya Shahb13a54a2017-12-06 19:17:04 +0530581 args.price_list_currency = parent.price_list_currency,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530582 args.plc_conversion_rate = parent.plc_conversion_rate
583 if args.get('items'):
584 for i, item in enumerate(args.get('items')):
585 for fieldname in children[i]:
586 # if the field exists in the original doc
587 # update the value
588 if fieldname in item and fieldname not in ("name", "doctype"):
589 item[fieldname] = children[i][fieldname]
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530590 return args
591 else:
592 return {
593 "parent": parent,
594 "children": children
595 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530596
597def apply_price_list_on_item(args):
598 item_details = frappe._dict()
599 item_doc = frappe.get_doc("Item", args.item_code)
600 get_price_list_rate(args, item_doc, item_details)
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530601
Anand Doshidffec8f2014-07-01 17:45:15 +0530602 item_details.update(get_pricing_rule_for_item(args))
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530603
Anand Doshidffec8f2014-07-01 17:45:15 +0530604 return item_details
605
606def get_price_list_currency(price_list):
Neil Trini Lasradoadb1b2c2015-03-18 11:40:39 +0530607 if price_list:
608 result = frappe.db.get_value("Price List", {"name": price_list,
609 "enabled": 1}, ["name", "currency"], as_dict=True)
Anand Doshidffec8f2014-07-01 17:45:15 +0530610
Neil Trini Lasradoadb1b2c2015-03-18 11:40:39 +0530611 if not result:
Rushabh Mehta985cb822016-12-30 15:06:54 +0530612 throw(_("Price List {0} is disabled or does not exist").format(price_list))
Anand Doshidffec8f2014-07-01 17:45:15 +0530613
Neil Trini Lasradoadb1b2c2015-03-18 11:40:39 +0530614 return result.currency
Anand Doshidffec8f2014-07-01 17:45:15 +0530615
Shreya Shahb13a54a2017-12-06 19:17:04 +0530616def get_price_list_uom_dependant(price_list):
617 if price_list:
618 result = frappe.db.get_value("Price List", {"name": price_list,
619 "enabled": 1}, ["name", "price_not_uom_dependant"], as_dict=True)
620
621 if not result:
622 throw(_("Price List {0} is disabled or does not exist").format(price_list))
623
624 return result.price_not_uom_dependant
625
626
Anand Doshidffec8f2014-07-01 17:45:15 +0530627def get_price_list_currency_and_exchange_rate(args):
Anand Doshi70f57eb2015-11-27 14:37:40 +0530628 if not args.price_list:
629 return {}
630
Anand Doshidffec8f2014-07-01 17:45:15 +0530631 price_list_currency = get_price_list_currency(args.price_list)
Shreya Shahb13a54a2017-12-06 19:17:04 +0530632 price_list_uom_dependant = get_price_list_uom_dependant(args.price_list)
Anand Doshidffec8f2014-07-01 17:45:15 +0530633 plc_conversion_rate = args.plc_conversion_rate
tundebabzy6e90f492018-02-12 10:48:57 +0100634 company_currency = get_company_currency(args.company)
Anand Doshidffec8f2014-07-01 17:45:15 +0530635
Nabin Haitdd82bf22015-05-11 16:49:17 +0530636 if (not plc_conversion_rate) or (price_list_currency and args.price_list_currency \
637 and price_list_currency != args.price_list_currency):
Rushabh Mehta6b537922017-03-14 16:59:11 +0530638 # cksgb 19/09/2016: added args.transaction_date as posting_date argument for get_exchange_rate
tundebabzy6e90f492018-02-12 10:48:57 +0100639 plc_conversion_rate = get_exchange_rate(price_list_currency, company_currency,
Nabin Haitad4f1c72016-12-09 12:14:47 +0530640 args.transaction_date) or plc_conversion_rate
Anand Doshidffec8f2014-07-01 17:45:15 +0530641
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530642 return frappe._dict({
Anand Doshidffec8f2014-07-01 17:45:15 +0530643 "price_list_currency": price_list_currency,
Shreya Shahb13a54a2017-12-06 19:17:04 +0530644 "price_list_uom_dependant": price_list_uom_dependant,
Anand Doshidffec8f2014-07-01 17:45:15 +0530645 "plc_conversion_rate": plc_conversion_rate
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530646 })
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530647
648@frappe.whitelist()
649def get_default_bom(item_code=None):
650 if item_code:
651 bom = frappe.db.get_value("BOM", {"docstatus": 1, "is_default": 1, "is_active": 1, "item": item_code})
652 if bom:
653 return bom
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530654
Saurabh3fbf3ce2016-03-09 15:31:04 +0530655def get_valuation_rate(item_code, warehouse=None):
Saurabhbd01a812016-03-07 14:32:23 +0530656 item = frappe.get_doc("Item", item_code)
657 if item.is_stock_item:
Saurabhbd01a812016-03-07 14:32:23 +0530658 if not warehouse:
659 warehouse = item.default_warehouse
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530660
661 return frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse},
Saurabhbd01a812016-03-07 14:32:23 +0530662 ["valuation_rate"], as_dict=True) or {"valuation_rate": 0}
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530663
Saurabhbd01a812016-03-07 14:32:23 +0530664 elif not item.is_stock_item:
Nabin Haitfe876c02017-03-06 16:32:57 +0530665 valuation_rate =frappe.db.sql("""select sum(base_net_amount) / sum(qty*conversion_factor)
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530666 from `tabPurchase Invoice Item`
Saurabhbd01a812016-03-07 14:32:23 +0530667 where item_code = %s and docstatus=1""", item_code)
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530668
Saurabhbd01a812016-03-07 14:32:23 +0530669 if valuation_rate:
670 return {"valuation_rate": valuation_rate[0][0] or 0.0}
671 else:
672 return {"valuation_rate": 0.0}
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530673
Saurabh2d7af632016-02-26 11:09:20 +0530674def get_gross_profit(out):
Saurabh5ada14b2016-02-26 18:02:55 +0530675 if out.valuation_rate:
676 out.update({
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530677 "gross_profit": ((out.base_rate - out.valuation_rate) * out.stock_qty)
Saurabh5ada14b2016-02-26 18:02:55 +0530678 })
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530679
Saurabhd3135532016-02-25 18:59:20 +0530680 return out
mbauskar6f603392016-01-21 16:10:22 +0530681
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530682@frappe.whitelist()
Rohit Waghchaureba3f0e62017-08-28 17:19:28 +0530683def get_serial_no(args, serial_nos=None):
684 serial_no = None
Achilles Rasquinha56b2e122018-02-13 14:42:40 +0530685 if isinstance(args, string_types):
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530686 args = json.loads(args)
687 args = frappe._dict(args)
688
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530689 if args.get('doctype') == 'Sales Invoice' and not args.get('update_stock'):
690 return ""
691
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530692 if args.get('warehouse') and args.get('stock_qty') and args.get('item_code'):
Shreyaa20157a2018-04-13 12:03:42 +0530693 has_serial_no = frappe.get_value('Item', {'item_code': args.item_code}, "has_serial_no")
Shreyaa20157a2018-04-13 12:03:42 +0530694 if args.get('batch_no') and has_serial_no == 1:
695 return get_serial_no_batchwise(args)
Shreya14bd43d2018-04-27 16:24:05 +0530696 # elif (args.get('has_batch_no') == 1) and has_serial_no == 1 and not args.get('batch_no'):
Shreyaa20157a2018-04-13 12:03:42 +0530697 # args.batch_no = get_batch_no(args.item_code, args.warehouse, args.qty)
Shreya14bd43d2018-04-27 16:24:05 +0530698 # actual_batch_qty = get_batch_qty(args.batch_no, args.warehouse, args.item_code)
Shreyaa20157a2018-04-13 12:03:42 +0530699 # if actual_batch_qty:
Shreya14bd43d2018-04-27 16:24:05 +0530700 # args.update(actual_batch_qty)
Shreyaa20157a2018-04-13 12:03:42 +0530701 # return get_serial_no_batchwise(args)
702 elif has_serial_no == 1:
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530703 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 +0530704 args = process_args(args)
705 serial_no = get_serial_nos_by_fifo(args)
Rohit Waghchaureba3f0e62017-08-28 17:19:28 +0530706
707 if not serial_no and serial_nos:
708 # For POS
709 serial_no = serial_nos
710
711 return serial_no