blob: 340f03d3865a259ad95208117c0ad42627034374 [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
Manas Solanki03938482018-05-14 16:16:46 +05307from frappe.utils import flt, cint, add_days, cstr, add_months
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)
Rohit Waghchaure4d4fb6d2018-05-15 22:12:44 +0530104 if args.doctype == 'Material Request':
105 out.rate = args.rate or out.price_list_rate
106 out.amount = flt(args.qty * out.rate)
Anand Doshi70f57eb2015-11-27 14:37:40 +0530107
Nabin Hait436f5262014-02-10 14:47:54 +0530108 return out
109
Anand Doshidffec8f2014-07-01 17:45:15 +0530110def process_args(args):
Achilles Rasquinha56b2e122018-02-13 14:42:40 +0530111 if isinstance(args, string_types):
Anand Doshidffec8f2014-07-01 17:45:15 +0530112 args = json.loads(args)
113
114 args = frappe._dict(args)
115
Anand Doshidffec8f2014-07-01 17:45:15 +0530116 if not args.get("price_list"):
117 args.price_list = args.get("selling_price_list") or args.get("buying_price_list")
118
119 if args.barcode:
120 args.item_code = get_item_code(barcode=args.barcode)
121 elif not args.item_code and args.serial_no:
122 args.item_code = get_item_code(serial_no=args.serial_no)
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530123
mbauskar6f603392016-01-21 16:10:22 +0530124 set_transaction_type(args)
Anand Doshidffec8f2014-07-01 17:45:15 +0530125 return args
126
tundebabzyacccdb32017-11-23 08:35:15 +0100127
Neil Trini Lasrado4abda672015-02-25 17:34:27 +0530128@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530129def get_item_code(barcode=None, serial_no=None):
130 if barcode:
Giovanni2144e022017-12-10 17:27:09 +0100131 item_code = frappe.db.get_value("Item Barcode", {"barcode": barcode}, fieldname=["parent"])
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530132 if not item_code:
133 frappe.throw(_("No Item with Barcode {0}").format(barcode))
Nabin Hait436f5262014-02-10 14:47:54 +0530134 elif serial_no:
Anand Doshie9baaa62014-02-26 12:35:33 +0530135 item_code = frappe.db.get_value("Serial No", serial_no, "item_code")
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530136 if not item_code:
137 frappe.throw(_("No Item with Serial No {0}").format(serial_no))
Anand Doshibd67e872014-04-11 16:51:27 +0530138
Nabin Hait436f5262014-02-10 14:47:54 +0530139 return item_code
Anand Doshibd67e872014-04-11 16:51:27 +0530140
tundebabzyacccdb32017-11-23 08:35:15 +0100141
Nabin Hait436f5262014-02-10 14:47:54 +0530142def validate_item_details(args, item):
143 if not args.company:
144 throw(_("Please specify Company"))
Anand Doshibd67e872014-04-11 16:51:27 +0530145
Nabin Hait436f5262014-02-10 14:47:54 +0530146 from erpnext.stock.doctype.item.item import validate_end_of_life
Anand Doshi21e09a22015-10-29 12:21:41 +0530147 validate_end_of_life(item.name, item.end_of_life, item.disabled)
Anand Doshibd67e872014-04-11 16:51:27 +0530148
tundebabzyacccdb32017-11-23 08:35:15 +0100149 if args.transaction_type == "selling" and cint(item.has_variants):
Umair Sayyed72534de2016-04-15 12:52:12 +0530150 throw(_("Item {0} is a template, please select one of its variants").format(item.name))
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530151
tundebabzyacccdb32017-11-23 08:35:15 +0100152 elif args.transaction_type == "buying" and args.doctype != "Material Request":
Rushabh Mehta1e8025b2015-07-24 15:16:25 +0530153 if args.get("is_subcontracted") == "Yes" and item.is_sub_contracted_item != 1:
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530154 throw(_("Item {0} must be a Sub-contracted Item").format(item.name))
Anand Doshibd67e872014-04-11 16:51:27 +0530155
tundebabzyacccdb32017-11-23 08:35:15 +0100156
Rushabh Mehta1a763742014-10-03 16:36:43 +0530157def get_basic_details(args, item):
tundebabzyacccdb32017-11-23 08:35:15 +0100158 """
159 :param args: {
160 "item_code": "",
161 "warehouse": None,
162 "customer": "",
163 "conversion_rate": 1.0,
164 "selling_price_list": None,
165 "price_list_currency": None,
Shreya Shahb13a54a2017-12-06 19:17:04 +0530166 "price_list_uom_dependant": None,
tundebabzyacccdb32017-11-23 08:35:15 +0100167 "plc_conversion_rate": 1.0,
168 "doctype": "",
169 "name": "",
170 "supplier": None,
171 "transaction_date": None,
172 "conversion_rate": 1.0,
173 "buying_price_list": None,
174 "is_subcontracted": "Yes" / "No",
175 "ignore_pricing_rule": 0/1
176 "project": "",
177 barcode: "",
178 serial_no: "",
179 warehouse: "",
180 currency: "",
181 update_stock: "",
182 price_list: "",
183 company: "",
184 order_type: "",
185 is_pos: "",
186 ignore_pricing_rule: "",
187 project: "",
188 qty: "",
189 stock_qty: "",
190 conversion_factor: ""
191 }
192 :param item: `item_code` of Item object
193 :return: frappe._dict
194 """
195
Rushabh Mehta1a763742014-10-03 16:36:43 +0530196 if not item:
197 item = frappe.get_doc("Item", args.get("item_code"))
Anand Doshibd67e872014-04-11 16:51:27 +0530198
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530199 if item.variant_of:
200 item.update_template_tables()
201
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530202 from frappe.defaults import get_user_default_as_list
Nabin Haitb6b56452015-12-23 16:37:00 +0530203 user_default_warehouse_list = get_user_default_as_list('Warehouse')
Nabin Hait436f5262014-02-10 14:47:54 +0530204 user_default_warehouse = user_default_warehouse_list[0] \
tundebabzyacccdb32017-11-23 08:35:15 +0100205 if len(user_default_warehouse_list) == 1 else ""
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530206
Rohit Waghchaure6734c4a2017-01-06 14:39:40 +0530207 warehouse = user_default_warehouse or item.default_warehouse or args.warehouse
Nabin Hait36028bf2014-02-12 19:09:28 +0530208
rohitwaghchauree8ccc0e2017-11-21 16:17:22 +0530209 material_request_type = ''
210 if args.get('doctype') == "Material Request":
211 material_request_type = frappe.db.get_value('Material Request',
212 args.get('name'), 'material_request_type')
213
Nabin Hait79a1d2a2017-09-29 18:14:10 +0530214 #Set the UOM to the Default Sales UOM or Default Purchase UOM if configured in the Item Master
215 if not args.uom:
216 if args.get('doctype') in ['Quotation', 'Sales Order', 'Delivery Note', 'Sales Invoice']:
217 args.uom = item.sales_uom if item.sales_uom else item.stock_uom
rohitwaghchauree8ccc0e2017-11-21 16:17:22 +0530218 elif (args.get('doctype') in ['Purchase Order', 'Purchase Receipt', 'Purchase Invoice']) or \
219 (args.get('doctype') == 'Material Request' and material_request_type == 'Purchase'):
Nabin Hait79a1d2a2017-09-29 18:14:10 +0530220 args.uom = item.purchase_uom if item.purchase_uom else item.stock_uom
221 else:
222 args.uom = item.stock_uom
223
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530224 out = frappe._dict({
Nabin Hait139dc7b2014-02-12 14:53:18 +0530225 "item_code": item.name,
226 "item_name": item.item_name,
Neil Trini Lasradoc48f06c2015-02-10 16:49:16 +0530227 "description": cstr(item.description).strip(),
228 "image": cstr(item.image).strip(),
Saurabhd3135532016-02-25 18:59:20 +0530229 "warehouse": warehouse,
Rushabh Mehta1a763742014-10-03 16:36:43 +0530230 "income_account": get_default_income_account(args, item),
231 "expense_account": get_default_expense_account(args, item),
232 "cost_center": get_default_cost_center(args, item),
Rushabh Mehta551406a2017-04-21 12:40:19 +0530233 'has_serial_no': item.has_serial_no,
234 'has_batch_no': item.has_batch_no,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530235 "batch_no": None,
Anand Doshibd67e872014-04-11 16:51:27 +0530236 "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in
Nabin Haite7d15362014-12-25 16:01:55 +0530237 item.get("taxes")))),
Nabin Hait79a1d2a2017-09-29 18:14:10 +0530238 "uom": args.uom,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530239 "min_order_qty": flt(item.min_order_qty) if args.doctype == "Material Request" else "",
Nabin Hait4a6c1782015-05-29 17:31:31 +0530240 "qty": args.qty or 1.0,
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530241 "stock_qty": args.qty or 1.0,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530242 "price_list_rate": 0.0,
243 "base_price_list_rate": 0.0,
244 "rate": 0.0,
245 "base_rate": 0.0,
246 "amount": 0.0,
247 "base_amount": 0.0,
Nabin Hait82e3e252015-02-23 16:58:30 +0530248 "net_rate": 0.0,
249 "net_amount": 0.0,
Saurabhc6dbe702015-10-19 14:17:52 +0530250 "discount_percentage": 0.0,
251 "supplier": item.default_supplier,
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530252 "update_stock": args.get("update_stock") if args.get('doctype') in ['Sales Invoice', 'Purchase Invoice'] else 0,
Rohit Waghchaurec858d522016-12-12 13:01:43 +0530253 "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 +0530254 "is_fixed_asset": item.is_fixed_asset,
255 "weight_per_unit":item.weight_per_unit,
256 "weight_uom":item.weight_uom,
Shreya Shah44fa9a62018-01-08 14:58:20 +0530257 "last_purchase_rate": item.last_purchase_rate if args.get("doctype") in ["Purchase Order"] else 0
Nabin Hait139dc7b2014-02-12 14:53:18 +0530258 })
Anand Doshibd67e872014-04-11 16:51:27 +0530259
Manas Solanki03938482018-05-14 16:16:46 +0530260 if item.enable_deferred_revenue:
261 service_end_date = add_months(args.transaction_date, item.no_of_months)
262 out.update({
263 "enable_deferred_revenue": item.enable_deferred_revenue,
264 "deferred_revenue_account": get_default_deferred_revenue_account(args, item),
265 "service_start_date": args.transaction_date,
266 "service_end_date": service_end_date
267 })
268
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530269 # calculate conversion factor
mbauskar287fe812017-04-10 19:15:57 +0530270 if item.stock_uom == args.uom:
271 out.conversion_factor = 1.0
272 else:
273 out.conversion_factor = args.conversion_factor or \
tundebabzyacccdb32017-11-23 08:35:15 +0100274 get_conversion_factor(item.item_code, args.uom).get("conversion_factor") or 1.0
mbauskar287fe812017-04-10 19:15:57 +0530275
276 args.conversion_factor = out.conversion_factor
277 out.stock_qty = out.qty * out.conversion_factor
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530278
Shreya Shah44fa9a62018-01-08 14:58:20 +0530279 # calculate last purchase rate
280 from erpnext.buying.doctype.purchase_order.purchase_order import item_last_purchase_rate
281 out.last_purchase_rate = item_last_purchase_rate(args.name, args.conversion_rate, item.item_code, out.conversion_factor)
282
Nabin Haitb09ed412015-02-17 10:33:58 +0530283 # if default specified in item is for another company, fetch from company
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530284 for d in [
285 ["Account", "income_account", "default_income_account"],
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530286 ["Account", "expense_account", "default_expense_account"],
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530287 ["Cost Center", "cost_center", "cost_center"],
288 ["Warehouse", "warehouse", ""]]:
Nabin Hait98a8fae2015-02-26 11:37:07 +0530289 company = frappe.db.get_value(d[0], out.get(d[1]), "company")
290 if not out[d[1]] or (company and args.company != company):
Nabin Haitb09ed412015-02-17 10:33:58 +0530291 out[d[1]] = frappe.db.get_value("Company", args.company, d[2]) if d[2] else None
292
Giovanni2144e022017-12-10 17:27:09 +0100293 for fieldname in ("item_name", "item_group", "barcodes", "brand", "stock_uom"):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530294 out[fieldname] = item.get(fieldname)
Anand Doshibd67e872014-04-11 16:51:27 +0530295
Nabin Hait436f5262014-02-10 14:47:54 +0530296 return out
Anand Doshibd67e872014-04-11 16:51:27 +0530297
tundebabzyacccdb32017-11-23 08:35:15 +0100298
Rushabh Mehta1a763742014-10-03 16:36:43 +0530299def get_default_income_account(args, item):
300 return (item.income_account
301 or args.income_account
Anand Doshid35354c2015-02-24 18:12:17 +0530302 or frappe.db.get_value("Item Group", item.item_group, "default_income_account"))
Rushabh Mehta1a763742014-10-03 16:36:43 +0530303
304def get_default_expense_account(args, item):
305 return (item.expense_account
306 or args.expense_account
Anand Doshid35354c2015-02-24 18:12:17 +0530307 or frappe.db.get_value("Item Group", item.item_group, "default_expense_account"))
Rushabh Mehta1a763742014-10-03 16:36:43 +0530308
Manas Solanki03938482018-05-14 16:16:46 +0530309def get_default_deferred_revenue_account(args, item):
310 if item.enable_deferred_revenue:
311 return (item.deferred_revenue_account
312 or args.deferred_revenue_account
313 or frappe.db.get_value("Company", args.company, "default_deferred_revenue_account"))
314 else:
315 return None
316
Rushabh Mehta1a763742014-10-03 16:36:43 +0530317def get_default_cost_center(args, item):
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +0530318 return (frappe.db.get_value("Project", args.get("project"), "cost_center")
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530319 or (item.selling_cost_center if args.get("customer") else item.buying_cost_center)
gmarkee5a31462015-09-27 09:38:01 +0200320 or frappe.db.get_value("Item Group", item.item_group, "default_cost_center")
321 or args.get("cost_center"))
Rushabh Mehta1a763742014-10-03 16:36:43 +0530322
Rushabh Mehtaf14b8092014-04-03 14:30:42 +0530323def get_price_list_rate(args, item_doc, out):
Anand Doshi39a28912016-02-15 11:42:18 +0530324 meta = frappe.get_meta(args.parenttype or args.doctype)
Nabin Hait436f5262014-02-10 14:47:54 +0530325
Rohit Waghchaure4d4fb6d2018-05-15 22:12:44 +0530326 if meta.get_field("currency") or args.get('currency'):
Nabin Hait436f5262014-02-10 14:47:54 +0530327 validate_price_list(args)
Rohit Waghchaure4d4fb6d2018-05-15 22:12:44 +0530328 if meta.get_field("currency") and args.price_list:
rohitwaghchaure4cccdbd2017-07-25 14:05:01 +0530329 validate_conversion_rate(args, meta)
Nabin Hait436f5262014-02-10 14:47:54 +0530330
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530331 price_list_rate = get_price_list_rate_for(args.price_list, item_doc.name)
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530332
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530333 # variant
334 if not price_list_rate and item_doc.variant_of:
335 price_list_rate = get_price_list_rate_for(args.price_list, item_doc.variant_of)
336
337 # insert in database
Rushabh Mehta1a763742014-10-03 16:36:43 +0530338 if not price_list_rate:
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530339 if args.price_list and args.rate:
340 insert_item_price(args)
Rushabh Mehta1a763742014-10-03 16:36:43 +0530341 return {}
Anand Doshibd67e872014-04-11 16:51:27 +0530342
Nabin Hait436f5262014-02-10 14:47:54 +0530343 out.price_list_rate = flt(price_list_rate) * flt(args.plc_conversion_rate) \
344 / flt(args.conversion_rate)
Zarrar17fd6612017-12-08 14:57:38 +0530345 if not args.price_list_uom_dependant:
Manas Solanki94769d82018-03-29 11:15:05 +0530346 out.price_list_rate = flt(out.price_list_rate * (flt(args.conversion_factor) or 1.0))
mbauskar287fe812017-04-10 19:15:57 +0530347
Nabin Hait34d28222016-01-19 15:45:49 +0530348 if not out.price_list_rate and args.transaction_type=="buying":
Nabin Hait436f5262014-02-10 14:47:54 +0530349 from erpnext.stock.doctype.item.item import get_last_purchase_details
Anand Doshibd67e872014-04-11 16:51:27 +0530350 out.update(get_last_purchase_details(item_doc.name,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530351 args.name, args.conversion_rate))
Anand Doshibd67e872014-04-11 16:51:27 +0530352
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530353def insert_item_price(args):
354 """Insert Item Price if Price List and Price List Rate are specified and currency is the same"""
355 if frappe.db.get_value("Price List", args.price_list, "currency") == args.currency \
356 and cint(frappe.db.get_single_value("Stock Settings", "auto_insert_price_list_rate_if_missing")):
357 if frappe.has_permission("Item Price", "write"):
Nabin Hait906bf642015-09-02 11:32:07 +0530358 price_list_rate = args.rate / args.conversion_factor \
359 if args.get("conversion_factor") else args.rate
Rushabh Mehta985cb822016-12-30 15:06:54 +0530360
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530361 item_price = frappe.get_doc({
362 "doctype": "Item Price",
363 "price_list": args.price_list,
364 "item_code": args.item_code,
365 "currency": args.currency,
Nabin Hait906bf642015-09-02 11:32:07 +0530366 "price_list_rate": price_list_rate
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530367 })
Rushabh Mehta985cb822016-12-30 15:06:54 +0530368
Kanchan Chauhan2ff7d2a2016-06-25 17:54:25 +0530369 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 +0530370
Kanchan Chauhan5ee8fcc2016-06-24 17:11:15 +0530371 if name:
372 item_price = frappe.get_doc('Item Price', name)
373 item_price.price_list_rate = price_list_rate
Rushabh Mehta985cb822016-12-30 15:06:54 +0530374 item_price.save()
Kanchan Chauhan5ee8fcc2016-06-24 17:11:15 +0530375 frappe.msgprint(_("Item Price updated for {0} in Price List {1}").format(args.item_code,
376 args.price_list))
Rushabh Mehta985cb822016-12-30 15:06:54 +0530377 else:
Kanchan Chauhan5ee8fcc2016-06-24 17:11:15 +0530378 item_price.insert()
379 frappe.msgprint(_("Item Price added for {0} in Price List {1}").format(args.item_code,
380 args.price_list))
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530381
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530382def get_price_list_rate_for(price_list, item_code):
Rushabh Mehta1a763742014-10-03 16:36:43 +0530383 return frappe.db.get_value("Item Price",
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530384 {"price_list": price_list, "item_code": item_code}, "price_list_rate")
Rushabh Mehta1a763742014-10-03 16:36:43 +0530385
Nabin Hait436f5262014-02-10 14:47:54 +0530386def validate_price_list(args):
387 if args.get("price_list"):
Anand Doshibd67e872014-04-11 16:51:27 +0530388 if not frappe.db.get_value("Price List",
Nabin Hait34d28222016-01-19 15:45:49 +0530389 {"name": args.price_list, args.transaction_type: 1, "enabled": 1}):
Rushabh Mehta985cb822016-12-30 15:06:54 +0530390 throw(_("Price List {0} is disabled or does not exist").format(args.price_list))
mbauskar0b665ac2017-04-11 02:49:24 -0400391 elif not args.get("supplier"):
Nabin Hait436f5262014-02-10 14:47:54 +0530392 throw(_("Price List not selected"))
Anand Doshibd67e872014-04-11 16:51:27 +0530393
Nabin Hait436f5262014-02-10 14:47:54 +0530394def validate_conversion_rate(args, meta):
Rushabh Mehta66e08e32014-09-29 12:17:03 +0530395 from erpnext.controllers.accounts_controller import validate_conversion_rate
Anand Doshibd67e872014-04-11 16:51:27 +0530396
Anand Doshife13bfe2015-08-25 12:49:40 +0530397 if (not args.conversion_rate
398 and args.currency==frappe.db.get_value("Company", args.company, "default_currency")):
399 args.conversion_rate = 1.0
400
Nabin Hait436f5262014-02-10 14:47:54 +0530401 # validate currency conversion rate
Anand Doshibd67e872014-04-11 16:51:27 +0530402 validate_conversion_rate(args.currency, args.conversion_rate,
Nabin Hait436f5262014-02-10 14:47:54 +0530403 meta.get_label("conversion_rate"), args.company)
Anand Doshibd67e872014-04-11 16:51:27 +0530404
405 args.conversion_rate = flt(args.conversion_rate,
406 get_field_precision(meta.get_field("conversion_rate"),
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530407 frappe._dict({"fields": args})))
Anand Doshibd67e872014-04-11 16:51:27 +0530408
Nabin Hait436f5262014-02-10 14:47:54 +0530409 # validate price list currency conversion rate
410 if not args.get("price_list_currency"):
411 throw(_("Price List Currency not selected"))
412 else:
Anand Doshibd67e872014-04-11 16:51:27 +0530413 validate_conversion_rate(args.price_list_currency, args.plc_conversion_rate,
Nabin Hait436f5262014-02-10 14:47:54 +0530414 meta.get_label("plc_conversion_rate"), args.company)
Anand Doshibd67e872014-04-11 16:51:27 +0530415
416 args.plc_conversion_rate = flt(args.plc_conversion_rate,
417 get_field_precision(meta.get_field("plc_conversion_rate"),
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530418 frappe._dict({"fields": args})))
Anand Doshibd67e872014-04-11 16:51:27 +0530419
Rushabh Mehtaf14b8092014-04-03 14:30:42 +0530420def get_party_item_code(args, item_doc, out):
Nabin Hait34d28222016-01-19 15:45:49 +0530421 if args.transaction_type=="selling" and args.customer:
Zarrar3fd63472018-03-09 12:10:45 +0530422 out.customer_item_code = None
Nabin Hait79f091e2014-12-26 11:41:15 +0530423 customer_item_code = item_doc.get("customer_items", {"customer_name": args.customer})
Zarrar3fd63472018-03-09 12:10:45 +0530424
425 if customer_item_code:
426 out.customer_item_code = customer_item_code[0].ref_code
427 else:
428 customer_group = frappe.db.get_value("Customer", args.customer, "customer_group")
429 customer_group_item_code = item_doc.get("customer_items", {"customer_group": customer_group})
430 if customer_group_item_code and not customer_group_item_code[0].customer_name:
431 out.customer_item_code = customer_group_item_code[0].ref_code
Anand Doshi54abf022016-01-21 22:28:47 +0530432
Nabin Hait34d28222016-01-19 15:45:49 +0530433 if args.transaction_type=="buying" and args.supplier:
Nabin Hait79f091e2014-12-26 11:41:15 +0530434 item_supplier = item_doc.get("supplier_items", {"supplier": args.supplier})
Nabin Hait436f5262014-02-10 14:47:54 +0530435 out.supplier_part_no = item_supplier[0].supplier_part_no if item_supplier else None
Anand Doshibd67e872014-04-11 16:51:27 +0530436
rohitwaghchaure2ea593b2018-04-12 13:37:08 +0530437def get_pos_profile_item_details(company, args, pos_profile=None, update_data=False):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530438 res = frappe._dict()
Anand Doshibd67e872014-04-11 16:51:27 +0530439
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530440 if not pos_profile:
rohitwaghchaurec037dc72017-11-28 16:11:15 +0530441 pos_profile = get_pos_profile(company, args.get('pos_profile'))
Anand Doshibd67e872014-04-11 16:51:27 +0530442
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530443 if pos_profile:
Nabin Hait436f5262014-02-10 14:47:54 +0530444 for fieldname in ("income_account", "cost_center", "warehouse", "expense_account"):
rohitwaghchaure2ea593b2018-04-12 13:37:08 +0530445 if (not args.get(fieldname) or update_data) and pos_profile.get(fieldname):
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530446 res[fieldname] = pos_profile.get(fieldname)
Anand Doshibd67e872014-04-11 16:51:27 +0530447
Nabin Hait436f5262014-02-10 14:47:54 +0530448 if res.get("warehouse"):
Saurabhd3135532016-02-25 18:59:20 +0530449 res.actual_qty = get_bin_details(args.item_code,
Nabin Hait436f5262014-02-10 14:47:54 +0530450 res.warehouse).get("actual_qty")
Anand Doshibd67e872014-04-11 16:51:27 +0530451
Nabin Hait436f5262014-02-10 14:47:54 +0530452 return res
453
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530454@frappe.whitelist()
rohitwaghchaurec037dc72017-11-28 16:11:15 +0530455def get_pos_profile(company, pos_profile=None, user=None):
456 if pos_profile:
457 return frappe.get_doc('POS Profile', pos_profile)
458
459 if not user:
460 user = frappe.session['user']
461
462 pos_profile = frappe.db.sql("""select pf.*
463 from
464 `tabPOS Profile` pf, `tabPOS Profile User` pfu
465 where
466 pfu.parent = pf.name and pfu.user = %s and pf.company = %s
467 and pf.disabled = 0 and pfu.default=1""", (user, company), as_dict=1)
Anand Doshibd67e872014-04-11 16:51:27 +0530468
Nabin Haitc469f2c2017-04-03 13:01:11 +0530469 if not pos_profile:
rohitwaghchaurec037dc72017-11-28 16:11:15 +0530470 pos_profile = frappe.db.sql("""select pf.*
471 from
472 `tabPOS Profile` pf left join `tabPOS Profile User` pfu
473 on
474 pf.name = pfu.parent
475 where
476 ifnull(pfu.user, '') = '' and pf.company = %s
477 and pf.disabled = 0""", (company), as_dict=1)
Anand Doshibd67e872014-04-11 16:51:27 +0530478
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530479 return pos_profile and pos_profile[0] or None
Anand Doshibd67e872014-04-11 16:51:27 +0530480
Kanchan Chauhan0a3f2c82016-10-25 23:15:39 +0530481def get_serial_nos_by_fifo(args):
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530482 if frappe.db.get_single_value("Stock Settings", "automatically_set_serial_nos_based_on_fifo"):
483 return "\n".join(frappe.db.sql_list("""select name from `tabSerial No`
Nabin Hait398c83a2015-10-22 15:11:44 +0530484 where item_code=%(item_code)s and warehouse=%(warehouse)s
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530485 order by timestamp(purchase_date, purchase_time) asc limit %(qty)s""", {
486 "item_code": args.item_code,
487 "warehouse": args.warehouse,
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530488 "qty": abs(cint(args.stock_qty))
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530489 }))
Nabin Hait08222152014-02-28 11:30:47 +0530490
Shreyaa20157a2018-04-13 12:03:42 +0530491def get_serial_no_batchwise(args):
492 if frappe.db.get_single_value("Stock Settings", "automatically_set_serial_nos_based_on_fifo"):
493 return "\n".join(frappe.db.sql_list("""select name from `tabSerial No`
Shreya14bd43d2018-04-27 16:24:05 +0530494 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 +0530495 order by timestamp(purchase_date, purchase_time) asc limit %(qty)s""", {
496 "item_code": args.item_code,
497 "warehouse": args.warehouse,
498 "batch_no": args.batch_no,
499 "qty": abs(cint(args.stock_qty))
500 }))
501
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530502@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530503def get_conversion_factor(item_code, uom):
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530504 variant_of = frappe.db.get_value("Item", item_code, "variant_of")
505 filters = {"parent": item_code, "uom": uom}
506 if variant_of:
Nabin Hait314086d2015-10-07 11:55:01 +0530507 filters["parent"] = ("in", (item_code, variant_of))
Anand Doshie9baaa62014-02-26 12:35:33 +0530508 return {"conversion_factor": frappe.db.get_value("UOM Conversion Detail",
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530509 filters, "conversion_factor")}
Nabin Hait08222152014-02-28 11:30:47 +0530510
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530511@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530512def get_projected_qty(item_code, warehouse):
Anand Doshibd67e872014-04-11 16:51:27 +0530513 return {"projected_qty": frappe.db.get_value("Bin",
Nabin Hait436f5262014-02-10 14:47:54 +0530514 {"item_code": item_code, "warehouse": warehouse}, "projected_qty")}
Nabin Hait08222152014-02-28 11:30:47 +0530515
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530516@frappe.whitelist()
Saurabhd3135532016-02-25 18:59:20 +0530517def get_bin_details(item_code, warehouse):
Anand Doshibd67e872014-04-11 16:51:27 +0530518 return frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse},
Manas Solankia884bd92018-02-09 13:12:07 +0530519 ["projected_qty", "actual_qty"], as_dict=True) \
520 or {"projected_qty": 0, "actual_qty": 0}
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530521
522@frappe.whitelist()
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530523def get_serial_no_details(item_code, warehouse, stock_qty, serial_no):
524 args = frappe._dict({"item_code":item_code, "warehouse":warehouse, "stock_qty":stock_qty, "serial_no":serial_no})
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530525 serial_no = get_serial_no(args)
526 return {'serial_no': serial_no}
Rushabh Mehta985cb822016-12-30 15:06:54 +0530527
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530528@frappe.whitelist()
Shreyaa20157a2018-04-13 12:03:42 +0530529def 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 +0530530 bin_details_and_serial_nos = {}
531 bin_details_and_serial_nos.update(get_bin_details(item_code, warehouse))
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530532 if stock_qty > 0:
Shreyaa20157a2018-04-13 12:03:42 +0530533 if has_batch_no:
534 args = frappe._dict({"item_code":item_code, "warehouse":warehouse, "stock_qty":stock_qty})
535 serial_no = get_serial_no(args)
536 bin_details_and_serial_nos.update({'serial_no': serial_no})
537 return bin_details_and_serial_nos
538
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530539 bin_details_and_serial_nos.update(get_serial_no_details(item_code, warehouse, stock_qty, serial_no))
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530540 return bin_details_and_serial_nos
Anand Doshidffec8f2014-07-01 17:45:15 +0530541
542@frappe.whitelist()
Shreyaa20157a2018-04-13 12:03:42 +0530543def get_batch_qty_and_serial_no(batch_no, stock_qty, warehouse, item_code, has_serial_no):
544 batch_qty_and_serial_no = {}
545 batch_qty_and_serial_no.update(get_batch_qty(batch_no, warehouse, item_code))
546
547 if (flt(batch_qty_and_serial_no.get('actual_batch_qty')) >= flt(stock_qty)) and has_serial_no:
548 args = frappe._dict({"item_code":item_code, "warehouse":warehouse, "stock_qty":stock_qty, "batch_no":batch_no})
549 serial_no = get_serial_no(args)
550 batch_qty_and_serial_no.update({'serial_no': serial_no})
551 return batch_qty_and_serial_no
552
553@frappe.whitelist()
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530554def get_batch_qty(batch_no, warehouse, item_code):
sburanaw66951e52017-04-25 15:28:57 +0700555 from erpnext.stock.doctype.batch import batch
Sambhaji Kolate98dbccd2015-03-10 15:04:28 +0530556 if batch_no:
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530557 return {'actual_batch_qty': batch.get_batch_qty(batch_no, warehouse)}
Rushabh Mehta1e8025b2015-07-24 15:16:25 +0530558
Anand Doshidffec8f2014-07-01 17:45:15 +0530559@frappe.whitelist()
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530560def apply_price_list(args, as_doc=False):
561 """Apply pricelist on a document-like dict object and return as
562 {'parent': dict, 'children': list}
563
564 :param args: See below
565 :param as_doc: Updates value in the passed dict
566
Anand Doshidffec8f2014-07-01 17:45:15 +0530567 args = {
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530568 "doctype": "",
569 "name": "",
570 "items": [{"doctype": "", "name": "", "item_code": "", "brand": "", "item_group": ""}, ...],
Anand Doshidffec8f2014-07-01 17:45:15 +0530571 "conversion_rate": 1.0,
572 "selling_price_list": None,
573 "price_list_currency": None,
Shreya Shahb13a54a2017-12-06 19:17:04 +0530574 "price_list_uom_dependant": None,
Anand Doshidffec8f2014-07-01 17:45:15 +0530575 "plc_conversion_rate": 1.0,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530576 "doctype": "",
577 "name": "",
Anand Doshidffec8f2014-07-01 17:45:15 +0530578 "supplier": None,
579 "transaction_date": None,
580 "conversion_rate": 1.0,
581 "buying_price_list": None,
Anand Doshidffec8f2014-07-01 17:45:15 +0530582 "ignore_pricing_rule": 0/1
583 }
584 """
585 args = process_args(args)
586
587 parent = get_price_list_currency_and_exchange_rate(args)
588 children = []
589
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530590 if "items" in args:
591 item_list = args.get("items")
Anand Doshidffec8f2014-07-01 17:45:15 +0530592 args.update(parent)
593
594 for item in item_list:
595 args_copy = frappe._dict(args.copy())
596 args_copy.update(item)
597 item_details = apply_price_list_on_item(args_copy)
598 children.append(item_details)
599
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530600 if as_doc:
Shreya Shahb13a54a2017-12-06 19:17:04 +0530601 args.price_list_currency = parent.price_list_currency,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530602 args.plc_conversion_rate = parent.plc_conversion_rate
603 if args.get('items'):
604 for i, item in enumerate(args.get('items')):
605 for fieldname in children[i]:
606 # if the field exists in the original doc
607 # update the value
608 if fieldname in item and fieldname not in ("name", "doctype"):
609 item[fieldname] = children[i][fieldname]
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530610 return args
611 else:
612 return {
613 "parent": parent,
614 "children": children
615 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530616
617def apply_price_list_on_item(args):
618 item_details = frappe._dict()
619 item_doc = frappe.get_doc("Item", args.item_code)
620 get_price_list_rate(args, item_doc, item_details)
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530621
Anand Doshidffec8f2014-07-01 17:45:15 +0530622 item_details.update(get_pricing_rule_for_item(args))
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530623
Anand Doshidffec8f2014-07-01 17:45:15 +0530624 return item_details
625
626def get_price_list_currency(price_list):
Neil Trini Lasradoadb1b2c2015-03-18 11:40:39 +0530627 if price_list:
628 result = frappe.db.get_value("Price List", {"name": price_list,
629 "enabled": 1}, ["name", "currency"], as_dict=True)
Anand Doshidffec8f2014-07-01 17:45:15 +0530630
Neil Trini Lasradoadb1b2c2015-03-18 11:40:39 +0530631 if not result:
Rushabh Mehta985cb822016-12-30 15:06:54 +0530632 throw(_("Price List {0} is disabled or does not exist").format(price_list))
Anand Doshidffec8f2014-07-01 17:45:15 +0530633
Neil Trini Lasradoadb1b2c2015-03-18 11:40:39 +0530634 return result.currency
Anand Doshidffec8f2014-07-01 17:45:15 +0530635
Shreya Shahb13a54a2017-12-06 19:17:04 +0530636def get_price_list_uom_dependant(price_list):
637 if price_list:
638 result = frappe.db.get_value("Price List", {"name": price_list,
639 "enabled": 1}, ["name", "price_not_uom_dependant"], as_dict=True)
640
641 if not result:
642 throw(_("Price List {0} is disabled or does not exist").format(price_list))
643
644 return result.price_not_uom_dependant
645
646
Anand Doshidffec8f2014-07-01 17:45:15 +0530647def get_price_list_currency_and_exchange_rate(args):
Anand Doshi70f57eb2015-11-27 14:37:40 +0530648 if not args.price_list:
649 return {}
650
Anand Doshidffec8f2014-07-01 17:45:15 +0530651 price_list_currency = get_price_list_currency(args.price_list)
Shreya Shahb13a54a2017-12-06 19:17:04 +0530652 price_list_uom_dependant = get_price_list_uom_dependant(args.price_list)
Anand Doshidffec8f2014-07-01 17:45:15 +0530653 plc_conversion_rate = args.plc_conversion_rate
tundebabzy6e90f492018-02-12 10:48:57 +0100654 company_currency = get_company_currency(args.company)
Anand Doshidffec8f2014-07-01 17:45:15 +0530655
Nabin Haitdd82bf22015-05-11 16:49:17 +0530656 if (not plc_conversion_rate) or (price_list_currency and args.price_list_currency \
657 and price_list_currency != args.price_list_currency):
Rushabh Mehta6b537922017-03-14 16:59:11 +0530658 # cksgb 19/09/2016: added args.transaction_date as posting_date argument for get_exchange_rate
tundebabzy6e90f492018-02-12 10:48:57 +0100659 plc_conversion_rate = get_exchange_rate(price_list_currency, company_currency,
Nabin Haitad4f1c72016-12-09 12:14:47 +0530660 args.transaction_date) or plc_conversion_rate
Anand Doshidffec8f2014-07-01 17:45:15 +0530661
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530662 return frappe._dict({
Anand Doshidffec8f2014-07-01 17:45:15 +0530663 "price_list_currency": price_list_currency,
Shreya Shahb13a54a2017-12-06 19:17:04 +0530664 "price_list_uom_dependant": price_list_uom_dependant,
Anand Doshidffec8f2014-07-01 17:45:15 +0530665 "plc_conversion_rate": plc_conversion_rate
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530666 })
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530667
668@frappe.whitelist()
669def get_default_bom(item_code=None):
670 if item_code:
671 bom = frappe.db.get_value("BOM", {"docstatus": 1, "is_default": 1, "is_active": 1, "item": item_code})
672 if bom:
673 return bom
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530674
Saurabh3fbf3ce2016-03-09 15:31:04 +0530675def get_valuation_rate(item_code, warehouse=None):
Saurabhbd01a812016-03-07 14:32:23 +0530676 item = frappe.get_doc("Item", item_code)
677 if item.is_stock_item:
Saurabhbd01a812016-03-07 14:32:23 +0530678 if not warehouse:
679 warehouse = item.default_warehouse
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530680
681 return frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse},
Saurabhbd01a812016-03-07 14:32:23 +0530682 ["valuation_rate"], as_dict=True) or {"valuation_rate": 0}
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530683
Saurabhbd01a812016-03-07 14:32:23 +0530684 elif not item.is_stock_item:
Nabin Haitfe876c02017-03-06 16:32:57 +0530685 valuation_rate =frappe.db.sql("""select sum(base_net_amount) / sum(qty*conversion_factor)
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530686 from `tabPurchase Invoice Item`
Saurabhbd01a812016-03-07 14:32:23 +0530687 where item_code = %s and docstatus=1""", item_code)
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530688
Saurabhbd01a812016-03-07 14:32:23 +0530689 if valuation_rate:
690 return {"valuation_rate": valuation_rate[0][0] or 0.0}
691 else:
692 return {"valuation_rate": 0.0}
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530693
Saurabh2d7af632016-02-26 11:09:20 +0530694def get_gross_profit(out):
Saurabh5ada14b2016-02-26 18:02:55 +0530695 if out.valuation_rate:
696 out.update({
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530697 "gross_profit": ((out.base_rate - out.valuation_rate) * out.stock_qty)
Saurabh5ada14b2016-02-26 18:02:55 +0530698 })
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530699
Saurabhd3135532016-02-25 18:59:20 +0530700 return out
mbauskar6f603392016-01-21 16:10:22 +0530701
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530702@frappe.whitelist()
Rohit Waghchaureba3f0e62017-08-28 17:19:28 +0530703def get_serial_no(args, serial_nos=None):
704 serial_no = None
Achilles Rasquinha56b2e122018-02-13 14:42:40 +0530705 if isinstance(args, string_types):
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530706 args = json.loads(args)
707 args = frappe._dict(args)
708
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530709 if args.get('doctype') == 'Sales Invoice' and not args.get('update_stock'):
710 return ""
711
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530712 if args.get('warehouse') and args.get('stock_qty') and args.get('item_code'):
Shreyaa20157a2018-04-13 12:03:42 +0530713 has_serial_no = frappe.get_value('Item', {'item_code': args.item_code}, "has_serial_no")
Shreyaa20157a2018-04-13 12:03:42 +0530714 if args.get('batch_no') and has_serial_no == 1:
715 return get_serial_no_batchwise(args)
Shreyaa20157a2018-04-13 12:03:42 +0530716 elif has_serial_no == 1:
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530717 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 +0530718 args = process_args(args)
719 serial_no = get_serial_nos_by_fifo(args)
Rohit Waghchaureba3f0e62017-08-28 17:19:28 +0530720
721 if not serial_no and serial_nos:
722 # For POS
723 serial_no = serial_nos
724
725 return serial_no