blob: 2ceed97d5585b4fee1eb012d5bdabb03322ab1cd [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Nabin Hait436f5262014-02-10 14:47:54 +05302# License: GNU General Public License v3. See license.txt
3
4from __future__ import unicode_literals
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05305import frappe
6from frappe import _, throw
Nabin Hait2244ac42015-01-12 17:35:14 +05307from frappe.utils import flt, cint, add_days, cstr
Nabin Hait436f5262014-02-10 14:47:54 +05308import json
Nabin Hait34d28222016-01-19 15:45:49 +05309from erpnext.accounts.doctype.pricing_rule.pricing_rule import get_pricing_rule_for_item, set_transaction_type
Anand Doshidffec8f2014-07-01 17:45:15 +053010from erpnext.setup.utils import get_exchange_rate
Rushabh Mehta66e08e32014-09-29 12:17:03 +053011from frappe.model.meta import get_field_precision
Rushabh Mehta551406a2017-04-21 12:40:19 +053012from erpnext.stock.doctype.batch.batch import get_batch_no
Nabin Hait98c2a052014-05-05 18:41:41 +053013
tundebabzyacccdb32017-11-23 08:35:15 +010014
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053015@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +053016def get_item_details(args):
17 """
18 args = {
19 "item_code": "",
20 "warehouse": None,
21 "customer": "",
22 "conversion_rate": 1.0,
23 "selling_price_list": None,
24 "price_list_currency": None,
Nabin Hait615d3422014-02-25 19:01:20 +053025 "plc_conversion_rate": 1.0,
Rushabh Mehtab46069d2016-01-15 16:59:26 +053026 "doctype": "",
27 "name": "",
Nabin Hait436f5262014-02-10 14:47:54 +053028 "supplier": None,
29 "transaction_date": None,
30 "conversion_rate": 1.0,
31 "buying_price_list": None,
32 "is_subcontracted": "Yes" / "No",
Nabin Hait444f9562014-06-20 15:59:49 +053033 "ignore_pricing_rule": 0/1
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +053034 "project": ""
Nabin Hait436f5262014-02-10 14:47:54 +053035 }
36 """
Anand Doshidffec8f2014-07-01 17:45:15 +053037 args = process_args(args)
Rushabh Mehtaf14b8092014-04-03 14:30:42 +053038 item_doc = frappe.get_doc("Item", args.item_code)
39 item = item_doc
Nabin Hait436f5262014-02-10 14:47:54 +053040
41 validate_item_details(args, item)
Anand Doshibd67e872014-04-11 16:51:27 +053042
Rushabh Mehta1a763742014-10-03 16:36:43 +053043 out = get_basic_details(args, item)
Anand Doshibd67e872014-04-11 16:51:27 +053044
Rushabh Mehtaf14b8092014-04-03 14:30:42 +053045 get_party_item_code(args, item_doc, out)
Nabin Hait436f5262014-02-10 14:47:54 +053046
Saurabh3fbf3ce2016-03-09 15:31:04 +053047 if frappe.db.exists("Product Bundle", args.item_code):
Saurabhbd01a812016-03-07 14:32:23 +053048 valuation_rate = 0.0
Saurabh3fbf3ce2016-03-09 15:31:04 +053049 bundled_items = frappe.get_doc("Product Bundle", args.item_code)
Rushabh Mehta0394aec2016-04-22 17:22:22 +053050
Saurabh3fbf3ce2016-03-09 15:31:04 +053051 for bundle_item in bundled_items.items:
52 valuation_rate += \
53 flt(get_valuation_rate(bundle_item.item_code, out.get("warehouse")).get("valuation_rate") \
54 * bundle_item.qty)
Saurabhbd01a812016-03-07 14:32:23 +053055
56 out.update({
57 "valuation_rate": valuation_rate
58 })
Saurabh3fbf3ce2016-03-09 15:31:04 +053059
Saurabh5ada14b2016-02-26 18:02:55 +053060 else:
Saurabh3fbf3ce2016-03-09 15:31:04 +053061 out.update(get_valuation_rate(args.item_code, out.get("warehouse")))
Anand Doshibd67e872014-04-11 16:51:27 +053062
Rushabh Mehtaf14b8092014-04-03 14:30:42 +053063 get_price_list_rate(args, item_doc, out)
Nabin Hait436f5262014-02-10 14:47:54 +053064
Rushabh Mehtab46069d2016-01-15 16:59:26 +053065 if args.customer and cint(args.is_pos):
Rushabh Mehtaad9156a2015-08-18 15:29:42 +053066 out.update(get_pos_profile_item_details(args.company, args))
Rushabh Mehta6b537922017-03-14 16:59:11 +053067
Nabin Hait0287d312017-01-11 10:31:33 +053068 if out.get("warehouse"):
69 out.update(get_bin_details(args.item_code, out.warehouse))
Anand Doshibd67e872014-04-11 16:51:27 +053070
Nabin Haita3dd72a2014-05-28 12:49:20 +053071 # update args with out, if key or value not exists
72 for key, value in out.iteritems():
73 if args.get(key) is None:
74 args[key] = value
75
Nabin Hait444f9562014-06-20 15:59:49 +053076 out.update(get_pricing_rule_for_item(args))
Anand Doshibd67e872014-04-11 16:51:27 +053077
pratu16x7577e4c42017-06-23 11:39:03 +053078 if (args.get("doctype") == "Delivery Note" or
Nabin Hait3ce41d62017-05-03 18:22:24 +053079 (args.get("doctype") == "Sales Invoice" and args.get('update_stock'))) \
80 and out.warehouse and out.stock_qty > 0:
pratu16x7577e4c42017-06-23 11:39:03 +053081
Rushabh Mehta551406a2017-04-21 12:40:19 +053082 if out.has_serial_no:
Rohit Waghchaureba3f0e62017-08-28 17:19:28 +053083 out.serial_no = get_serial_no(out, args.serial_no)
Rushabh Mehta551406a2017-04-21 12:40:19 +053084
Nabin Hait8fac2ad2017-05-18 11:54:24 +053085 if out.has_batch_no and not args.get("batch_no"):
Rushabh Mehta551406a2017-04-21 12:40:19 +053086 out.batch_no = get_batch_no(out.item_code, out.warehouse, out.qty)
rohitwaghchaure2fb8cc52017-11-29 13:50:04 +053087 actual_batch_qty = get_batch_qty(out.batch_no, out.warehouse, out.item_code)
88 if actual_batch_qty:
89 out.update(actual_batch_qty)
Rushabh Mehta551406a2017-04-21 12:40:19 +053090
Nabin Hait139dc7b2014-02-12 14:53:18 +053091 if args.transaction_date and item.lead_time_days:
92 out.schedule_date = out.lead_time_date = add_days(args.transaction_date,
93 item.lead_time_days)
Anand Doshibd67e872014-04-11 16:51:27 +053094
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +053095 if args.get("is_subcontracted") == "Yes":
Saurabh590d4012017-09-18 15:49:19 +053096 out.bom = args.get('bom') or get_default_bom(args.item_code)
Rushabh Mehta0394aec2016-04-22 17:22:22 +053097
Saurabh2d7af632016-02-26 11:09:20 +053098 get_gross_profit(out)
Anand Doshi70f57eb2015-11-27 14:37:40 +053099
Nabin Hait436f5262014-02-10 14:47:54 +0530100 return out
101
Anand Doshidffec8f2014-07-01 17:45:15 +0530102def process_args(args):
103 if isinstance(args, basestring):
104 args = json.loads(args)
105
106 args = frappe._dict(args)
107
Anand Doshidffec8f2014-07-01 17:45:15 +0530108 if not args.get("price_list"):
109 args.price_list = args.get("selling_price_list") or args.get("buying_price_list")
110
111 if args.barcode:
112 args.item_code = get_item_code(barcode=args.barcode)
113 elif not args.item_code and args.serial_no:
114 args.item_code = get_item_code(serial_no=args.serial_no)
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530115
mbauskar6f603392016-01-21 16:10:22 +0530116 set_transaction_type(args)
Anand Doshidffec8f2014-07-01 17:45:15 +0530117 return args
118
tundebabzyacccdb32017-11-23 08:35:15 +0100119
Neil Trini Lasrado4abda672015-02-25 17:34:27 +0530120@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530121def get_item_code(barcode=None, serial_no=None):
122 if barcode:
Anand Doshie9baaa62014-02-26 12:35:33 +0530123 item_code = frappe.db.get_value("Item", {"barcode": barcode})
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530124 if not item_code:
125 frappe.throw(_("No Item with Barcode {0}").format(barcode))
Nabin Hait436f5262014-02-10 14:47:54 +0530126 elif serial_no:
Anand Doshie9baaa62014-02-26 12:35:33 +0530127 item_code = frappe.db.get_value("Serial No", serial_no, "item_code")
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530128 if not item_code:
129 frappe.throw(_("No Item with Serial No {0}").format(serial_no))
Anand Doshibd67e872014-04-11 16:51:27 +0530130
Nabin Hait436f5262014-02-10 14:47:54 +0530131 return item_code
Anand Doshibd67e872014-04-11 16:51:27 +0530132
tundebabzyacccdb32017-11-23 08:35:15 +0100133
Nabin Hait436f5262014-02-10 14:47:54 +0530134def validate_item_details(args, item):
135 if not args.company:
136 throw(_("Please specify Company"))
Anand Doshibd67e872014-04-11 16:51:27 +0530137
Nabin Hait436f5262014-02-10 14:47:54 +0530138 from erpnext.stock.doctype.item.item import validate_end_of_life
Anand Doshi21e09a22015-10-29 12:21:41 +0530139 validate_end_of_life(item.name, item.end_of_life, item.disabled)
Anand Doshibd67e872014-04-11 16:51:27 +0530140
tundebabzyacccdb32017-11-23 08:35:15 +0100141 if args.transaction_type == "selling" and cint(item.has_variants):
Umair Sayyed72534de2016-04-15 12:52:12 +0530142 throw(_("Item {0} is a template, please select one of its variants").format(item.name))
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530143
tundebabzyacccdb32017-11-23 08:35:15 +0100144 elif args.transaction_type == "buying" and args.doctype != "Material Request":
Rushabh Mehta1e8025b2015-07-24 15:16:25 +0530145 if args.get("is_subcontracted") == "Yes" and item.is_sub_contracted_item != 1:
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530146 throw(_("Item {0} must be a Sub-contracted Item").format(item.name))
Anand Doshibd67e872014-04-11 16:51:27 +0530147
tundebabzyacccdb32017-11-23 08:35:15 +0100148
Rushabh Mehta1a763742014-10-03 16:36:43 +0530149def get_basic_details(args, item):
tundebabzyacccdb32017-11-23 08:35:15 +0100150 """
151 :param args: {
152 "item_code": "",
153 "warehouse": None,
154 "customer": "",
155 "conversion_rate": 1.0,
156 "selling_price_list": None,
157 "price_list_currency": None,
Shreya Shahb13a54a2017-12-06 19:17:04 +0530158 "price_list_uom_dependant": None,
tundebabzyacccdb32017-11-23 08:35:15 +0100159 "plc_conversion_rate": 1.0,
160 "doctype": "",
161 "name": "",
162 "supplier": None,
163 "transaction_date": None,
164 "conversion_rate": 1.0,
165 "buying_price_list": None,
166 "is_subcontracted": "Yes" / "No",
167 "ignore_pricing_rule": 0/1
168 "project": "",
169 barcode: "",
170 serial_no: "",
171 warehouse: "",
172 currency: "",
173 update_stock: "",
174 price_list: "",
175 company: "",
176 order_type: "",
177 is_pos: "",
178 ignore_pricing_rule: "",
179 project: "",
180 qty: "",
181 stock_qty: "",
182 conversion_factor: ""
183 }
184 :param item: `item_code` of Item object
185 :return: frappe._dict
186 """
187
Rushabh Mehta1a763742014-10-03 16:36:43 +0530188 if not item:
189 item = frappe.get_doc("Item", args.get("item_code"))
Anand Doshibd67e872014-04-11 16:51:27 +0530190
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530191 if item.variant_of:
192 item.update_template_tables()
193
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530194 from frappe.defaults import get_user_default_as_list
Nabin Haitb6b56452015-12-23 16:37:00 +0530195 user_default_warehouse_list = get_user_default_as_list('Warehouse')
Nabin Hait436f5262014-02-10 14:47:54 +0530196 user_default_warehouse = user_default_warehouse_list[0] \
tundebabzyacccdb32017-11-23 08:35:15 +0100197 if len(user_default_warehouse_list) == 1 else ""
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530198
Rohit Waghchaure6734c4a2017-01-06 14:39:40 +0530199 warehouse = user_default_warehouse or item.default_warehouse or args.warehouse
Nabin Hait36028bf2014-02-12 19:09:28 +0530200
rohitwaghchauree8ccc0e2017-11-21 16:17:22 +0530201 material_request_type = ''
202 if args.get('doctype') == "Material Request":
203 material_request_type = frappe.db.get_value('Material Request',
204 args.get('name'), 'material_request_type')
205
Nabin Hait79a1d2a2017-09-29 18:14:10 +0530206 #Set the UOM to the Default Sales UOM or Default Purchase UOM if configured in the Item Master
207 if not args.uom:
208 if args.get('doctype') in ['Quotation', 'Sales Order', 'Delivery Note', 'Sales Invoice']:
209 args.uom = item.sales_uom if item.sales_uom else item.stock_uom
rohitwaghchauree8ccc0e2017-11-21 16:17:22 +0530210 elif (args.get('doctype') in ['Purchase Order', 'Purchase Receipt', 'Purchase Invoice']) or \
211 (args.get('doctype') == 'Material Request' and material_request_type == 'Purchase'):
Nabin Hait79a1d2a2017-09-29 18:14:10 +0530212 args.uom = item.purchase_uom if item.purchase_uom else item.stock_uom
213 else:
214 args.uom = item.stock_uom
215
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530216 out = frappe._dict({
Nabin Hait139dc7b2014-02-12 14:53:18 +0530217 "item_code": item.name,
218 "item_name": item.item_name,
Neil Trini Lasradoc48f06c2015-02-10 16:49:16 +0530219 "description": cstr(item.description).strip(),
220 "image": cstr(item.image).strip(),
Saurabhd3135532016-02-25 18:59:20 +0530221 "warehouse": warehouse,
Rushabh Mehta1a763742014-10-03 16:36:43 +0530222 "income_account": get_default_income_account(args, item),
223 "expense_account": get_default_expense_account(args, item),
224 "cost_center": get_default_cost_center(args, item),
Rushabh Mehta551406a2017-04-21 12:40:19 +0530225 'has_serial_no': item.has_serial_no,
226 'has_batch_no': item.has_batch_no,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530227 "batch_no": None,
Anand Doshibd67e872014-04-11 16:51:27 +0530228 "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in
Nabin Haite7d15362014-12-25 16:01:55 +0530229 item.get("taxes")))),
Nabin Hait79a1d2a2017-09-29 18:14:10 +0530230 "uom": args.uom,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530231 "min_order_qty": flt(item.min_order_qty) if args.doctype == "Material Request" else "",
Nabin Hait4a6c1782015-05-29 17:31:31 +0530232 "qty": args.qty or 1.0,
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530233 "stock_qty": args.qty or 1.0,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530234 "price_list_rate": 0.0,
235 "base_price_list_rate": 0.0,
236 "rate": 0.0,
237 "base_rate": 0.0,
238 "amount": 0.0,
239 "base_amount": 0.0,
Nabin Hait82e3e252015-02-23 16:58:30 +0530240 "net_rate": 0.0,
241 "net_amount": 0.0,
Saurabhc6dbe702015-10-19 14:17:52 +0530242 "discount_percentage": 0.0,
243 "supplier": item.default_supplier,
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530244 "update_stock": args.get("update_stock") if args.get('doctype') in ['Sales Invoice', 'Purchase Invoice'] else 0,
Rohit Waghchaurec858d522016-12-12 13:01:43 +0530245 "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 +0530246 "is_fixed_asset": item.is_fixed_asset,
247 "weight_per_unit":item.weight_per_unit,
248 "weight_uom":item.weight_uom,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530249 })
Anand Doshibd67e872014-04-11 16:51:27 +0530250
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530251 # calculate conversion factor
mbauskar287fe812017-04-10 19:15:57 +0530252 if item.stock_uom == args.uom:
253 out.conversion_factor = 1.0
254 else:
255 out.conversion_factor = args.conversion_factor or \
tundebabzyacccdb32017-11-23 08:35:15 +0100256 get_conversion_factor(item.item_code, args.uom).get("conversion_factor") or 1.0
mbauskar287fe812017-04-10 19:15:57 +0530257
258 args.conversion_factor = out.conversion_factor
259 out.stock_qty = out.qty * out.conversion_factor
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530260
Nabin Haitb09ed412015-02-17 10:33:58 +0530261 # if default specified in item is for another company, fetch from company
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530262 for d in [
263 ["Account", "income_account", "default_income_account"],
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530264 ["Account", "expense_account", "default_expense_account"],
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530265 ["Cost Center", "cost_center", "cost_center"],
266 ["Warehouse", "warehouse", ""]]:
Nabin Hait98a8fae2015-02-26 11:37:07 +0530267 company = frappe.db.get_value(d[0], out.get(d[1]), "company")
268 if not out[d[1]] or (company and args.company != company):
Nabin Haitb09ed412015-02-17 10:33:58 +0530269 out[d[1]] = frappe.db.get_value("Company", args.company, d[2]) if d[2] else None
270
Nabin Hait436f5262014-02-10 14:47:54 +0530271 for fieldname in ("item_name", "item_group", "barcode", "brand", "stock_uom"):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530272 out[fieldname] = item.get(fieldname)
Anand Doshibd67e872014-04-11 16:51:27 +0530273
Nabin Hait436f5262014-02-10 14:47:54 +0530274 return out
Anand Doshibd67e872014-04-11 16:51:27 +0530275
tundebabzyacccdb32017-11-23 08:35:15 +0100276
Rushabh Mehta1a763742014-10-03 16:36:43 +0530277def get_default_income_account(args, item):
278 return (item.income_account
279 or args.income_account
Anand Doshid35354c2015-02-24 18:12:17 +0530280 or frappe.db.get_value("Item Group", item.item_group, "default_income_account"))
Rushabh Mehta1a763742014-10-03 16:36:43 +0530281
282def get_default_expense_account(args, item):
283 return (item.expense_account
284 or args.expense_account
Anand Doshid35354c2015-02-24 18:12:17 +0530285 or frappe.db.get_value("Item Group", item.item_group, "default_expense_account"))
Rushabh Mehta1a763742014-10-03 16:36:43 +0530286
287def get_default_cost_center(args, item):
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +0530288 return (frappe.db.get_value("Project", args.get("project"), "cost_center")
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530289 or (item.selling_cost_center if args.get("customer") else item.buying_cost_center)
gmarkee5a31462015-09-27 09:38:01 +0200290 or frappe.db.get_value("Item Group", item.item_group, "default_cost_center")
291 or args.get("cost_center"))
Rushabh Mehta1a763742014-10-03 16:36:43 +0530292
Rushabh Mehtaf14b8092014-04-03 14:30:42 +0530293def get_price_list_rate(args, item_doc, out):
Anand Doshi39a28912016-02-15 11:42:18 +0530294 meta = frappe.get_meta(args.parenttype or args.doctype)
Nabin Hait436f5262014-02-10 14:47:54 +0530295
296 if meta.get_field("currency"):
297 validate_price_list(args)
rohitwaghchaure4cccdbd2017-07-25 14:05:01 +0530298 if args.price_list:
299 validate_conversion_rate(args, meta)
Nabin Hait436f5262014-02-10 14:47:54 +0530300
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530301 price_list_rate = get_price_list_rate_for(args.price_list, item_doc.name)
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530302
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530303 # variant
304 if not price_list_rate and item_doc.variant_of:
305 price_list_rate = get_price_list_rate_for(args.price_list, item_doc.variant_of)
306
307 # insert in database
Rushabh Mehta1a763742014-10-03 16:36:43 +0530308 if not price_list_rate:
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530309 if args.price_list and args.rate:
310 insert_item_price(args)
Rushabh Mehta1a763742014-10-03 16:36:43 +0530311 return {}
Anand Doshibd67e872014-04-11 16:51:27 +0530312
Nabin Hait436f5262014-02-10 14:47:54 +0530313 out.price_list_rate = flt(price_list_rate) * flt(args.plc_conversion_rate) \
314 / flt(args.conversion_rate)
Shreya Shahb13a54a2017-12-06 19:17:04 +0530315 if args.price_list_uom_dependant == 0:
316 out.price_list_rate = flt(out.price_list_rate * (args.conversion_factor or 1.0))
mbauskar287fe812017-04-10 19:15:57 +0530317
Nabin Hait34d28222016-01-19 15:45:49 +0530318 if not out.price_list_rate and args.transaction_type=="buying":
Nabin Hait436f5262014-02-10 14:47:54 +0530319 from erpnext.stock.doctype.item.item import get_last_purchase_details
Anand Doshibd67e872014-04-11 16:51:27 +0530320 out.update(get_last_purchase_details(item_doc.name,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530321 args.name, args.conversion_rate))
Anand Doshibd67e872014-04-11 16:51:27 +0530322
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530323def insert_item_price(args):
324 """Insert Item Price if Price List and Price List Rate are specified and currency is the same"""
325 if frappe.db.get_value("Price List", args.price_list, "currency") == args.currency \
326 and cint(frappe.db.get_single_value("Stock Settings", "auto_insert_price_list_rate_if_missing")):
327 if frappe.has_permission("Item Price", "write"):
Nabin Hait906bf642015-09-02 11:32:07 +0530328 price_list_rate = args.rate / args.conversion_factor \
329 if args.get("conversion_factor") else args.rate
Rushabh Mehta985cb822016-12-30 15:06:54 +0530330
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530331 item_price = frappe.get_doc({
332 "doctype": "Item Price",
333 "price_list": args.price_list,
334 "item_code": args.item_code,
335 "currency": args.currency,
Nabin Hait906bf642015-09-02 11:32:07 +0530336 "price_list_rate": price_list_rate
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530337 })
Rushabh Mehta985cb822016-12-30 15:06:54 +0530338
Kanchan Chauhan2ff7d2a2016-06-25 17:54:25 +0530339 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 +0530340
Kanchan Chauhan5ee8fcc2016-06-24 17:11:15 +0530341 if name:
342 item_price = frappe.get_doc('Item Price', name)
343 item_price.price_list_rate = price_list_rate
Rushabh Mehta985cb822016-12-30 15:06:54 +0530344 item_price.save()
Kanchan Chauhan5ee8fcc2016-06-24 17:11:15 +0530345 frappe.msgprint(_("Item Price updated for {0} in Price List {1}").format(args.item_code,
346 args.price_list))
Rushabh Mehta985cb822016-12-30 15:06:54 +0530347 else:
Kanchan Chauhan5ee8fcc2016-06-24 17:11:15 +0530348 item_price.insert()
349 frappe.msgprint(_("Item Price added for {0} in Price List {1}").format(args.item_code,
350 args.price_list))
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530351
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530352def get_price_list_rate_for(price_list, item_code):
Rushabh Mehta1a763742014-10-03 16:36:43 +0530353 return frappe.db.get_value("Item Price",
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530354 {"price_list": price_list, "item_code": item_code}, "price_list_rate")
Rushabh Mehta1a763742014-10-03 16:36:43 +0530355
Nabin Hait436f5262014-02-10 14:47:54 +0530356def validate_price_list(args):
357 if args.get("price_list"):
Anand Doshibd67e872014-04-11 16:51:27 +0530358 if not frappe.db.get_value("Price List",
Nabin Hait34d28222016-01-19 15:45:49 +0530359 {"name": args.price_list, args.transaction_type: 1, "enabled": 1}):
Rushabh Mehta985cb822016-12-30 15:06:54 +0530360 throw(_("Price List {0} is disabled or does not exist").format(args.price_list))
mbauskar0b665ac2017-04-11 02:49:24 -0400361 elif not args.get("supplier"):
Nabin Hait436f5262014-02-10 14:47:54 +0530362 throw(_("Price List not selected"))
Anand Doshibd67e872014-04-11 16:51:27 +0530363
Nabin Hait436f5262014-02-10 14:47:54 +0530364def validate_conversion_rate(args, meta):
Rushabh Mehta66e08e32014-09-29 12:17:03 +0530365 from erpnext.controllers.accounts_controller import validate_conversion_rate
Anand Doshibd67e872014-04-11 16:51:27 +0530366
Anand Doshife13bfe2015-08-25 12:49:40 +0530367 if (not args.conversion_rate
368 and args.currency==frappe.db.get_value("Company", args.company, "default_currency")):
369 args.conversion_rate = 1.0
370
Nabin Hait436f5262014-02-10 14:47:54 +0530371 # validate currency conversion rate
Anand Doshibd67e872014-04-11 16:51:27 +0530372 validate_conversion_rate(args.currency, args.conversion_rate,
Nabin Hait436f5262014-02-10 14:47:54 +0530373 meta.get_label("conversion_rate"), args.company)
Anand Doshibd67e872014-04-11 16:51:27 +0530374
375 args.conversion_rate = flt(args.conversion_rate,
376 get_field_precision(meta.get_field("conversion_rate"),
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530377 frappe._dict({"fields": args})))
Anand Doshibd67e872014-04-11 16:51:27 +0530378
Nabin Hait436f5262014-02-10 14:47:54 +0530379 # validate price list currency conversion rate
380 if not args.get("price_list_currency"):
381 throw(_("Price List Currency not selected"))
382 else:
Anand Doshibd67e872014-04-11 16:51:27 +0530383 validate_conversion_rate(args.price_list_currency, args.plc_conversion_rate,
Nabin Hait436f5262014-02-10 14:47:54 +0530384 meta.get_label("plc_conversion_rate"), args.company)
Anand Doshibd67e872014-04-11 16:51:27 +0530385
386 args.plc_conversion_rate = flt(args.plc_conversion_rate,
387 get_field_precision(meta.get_field("plc_conversion_rate"),
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530388 frappe._dict({"fields": args})))
Anand Doshibd67e872014-04-11 16:51:27 +0530389
Rushabh Mehtaf14b8092014-04-03 14:30:42 +0530390def get_party_item_code(args, item_doc, out):
Nabin Hait34d28222016-01-19 15:45:49 +0530391 if args.transaction_type=="selling" and args.customer:
Nabin Hait79f091e2014-12-26 11:41:15 +0530392 customer_item_code = item_doc.get("customer_items", {"customer_name": args.customer})
Nabin Hait436f5262014-02-10 14:47:54 +0530393 out.customer_item_code = customer_item_code[0].ref_code if customer_item_code else None
Anand Doshi54abf022016-01-21 22:28:47 +0530394
Nabin Hait34d28222016-01-19 15:45:49 +0530395 if args.transaction_type=="buying" and args.supplier:
Nabin Hait79f091e2014-12-26 11:41:15 +0530396 item_supplier = item_doc.get("supplier_items", {"supplier": args.supplier})
Nabin Hait436f5262014-02-10 14:47:54 +0530397 out.supplier_part_no = item_supplier[0].supplier_part_no if item_supplier else None
Anand Doshibd67e872014-04-11 16:51:27 +0530398
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530399def get_pos_profile_item_details(company, args, pos_profile=None):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530400 res = frappe._dict()
Anand Doshibd67e872014-04-11 16:51:27 +0530401
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530402 if not pos_profile:
rohitwaghchaurec037dc72017-11-28 16:11:15 +0530403 pos_profile = get_pos_profile(company, args.get('pos_profile'))
Anand Doshibd67e872014-04-11 16:51:27 +0530404
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530405 if pos_profile:
Nabin Hait436f5262014-02-10 14:47:54 +0530406 for fieldname in ("income_account", "cost_center", "warehouse", "expense_account"):
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530407 if not args.get(fieldname) and pos_profile.get(fieldname):
408 res[fieldname] = pos_profile.get(fieldname)
Anand Doshibd67e872014-04-11 16:51:27 +0530409
Nabin Hait436f5262014-02-10 14:47:54 +0530410 if res.get("warehouse"):
Saurabhd3135532016-02-25 18:59:20 +0530411 res.actual_qty = get_bin_details(args.item_code,
Nabin Hait436f5262014-02-10 14:47:54 +0530412 res.warehouse).get("actual_qty")
Anand Doshibd67e872014-04-11 16:51:27 +0530413
Nabin Hait436f5262014-02-10 14:47:54 +0530414 return res
415
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530416@frappe.whitelist()
rohitwaghchaurec037dc72017-11-28 16:11:15 +0530417def get_pos_profile(company, pos_profile=None, user=None):
418 if pos_profile:
419 return frappe.get_doc('POS Profile', pos_profile)
420
421 if not user:
422 user = frappe.session['user']
423
424 pos_profile = frappe.db.sql("""select pf.*
425 from
426 `tabPOS Profile` pf, `tabPOS Profile User` pfu
427 where
428 pfu.parent = pf.name and pfu.user = %s and pf.company = %s
429 and pf.disabled = 0 and pfu.default=1""", (user, company), as_dict=1)
Anand Doshibd67e872014-04-11 16:51:27 +0530430
Nabin Haitc469f2c2017-04-03 13:01:11 +0530431 if not pos_profile:
rohitwaghchaurec037dc72017-11-28 16:11:15 +0530432 pos_profile = frappe.db.sql("""select pf.*
433 from
434 `tabPOS Profile` pf left join `tabPOS Profile User` pfu
435 on
436 pf.name = pfu.parent
437 where
438 ifnull(pfu.user, '') = '' and pf.company = %s
439 and pf.disabled = 0""", (company), as_dict=1)
Anand Doshibd67e872014-04-11 16:51:27 +0530440
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530441 return pos_profile and pos_profile[0] or None
Anand Doshibd67e872014-04-11 16:51:27 +0530442
Kanchan Chauhan0a3f2c82016-10-25 23:15:39 +0530443def get_serial_nos_by_fifo(args):
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530444 if frappe.db.get_single_value("Stock Settings", "automatically_set_serial_nos_based_on_fifo"):
445 return "\n".join(frappe.db.sql_list("""select name from `tabSerial No`
Nabin Hait398c83a2015-10-22 15:11:44 +0530446 where item_code=%(item_code)s and warehouse=%(warehouse)s
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530447 order by timestamp(purchase_date, purchase_time) asc limit %(qty)s""", {
448 "item_code": args.item_code,
449 "warehouse": args.warehouse,
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530450 "qty": abs(cint(args.stock_qty))
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530451 }))
Nabin Hait08222152014-02-28 11:30:47 +0530452
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530453@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530454def get_conversion_factor(item_code, uom):
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530455 variant_of = frappe.db.get_value("Item", item_code, "variant_of")
456 filters = {"parent": item_code, "uom": uom}
457 if variant_of:
Nabin Hait314086d2015-10-07 11:55:01 +0530458 filters["parent"] = ("in", (item_code, variant_of))
Anand Doshie9baaa62014-02-26 12:35:33 +0530459 return {"conversion_factor": frappe.db.get_value("UOM Conversion Detail",
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530460 filters, "conversion_factor")}
Nabin Hait08222152014-02-28 11:30:47 +0530461
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530462@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530463def get_projected_qty(item_code, warehouse):
Anand Doshibd67e872014-04-11 16:51:27 +0530464 return {"projected_qty": frappe.db.get_value("Bin",
Nabin Hait436f5262014-02-10 14:47:54 +0530465 {"item_code": item_code, "warehouse": warehouse}, "projected_qty")}
Nabin Hait08222152014-02-28 11:30:47 +0530466
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530467@frappe.whitelist()
Saurabhd3135532016-02-25 18:59:20 +0530468def get_bin_details(item_code, warehouse):
Anand Doshibd67e872014-04-11 16:51:27 +0530469 return frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse},
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530470 ["projected_qty", "actual_qty"], as_dict=True) \
471 or {"projected_qty": 0, "actual_qty": 0}
472
473@frappe.whitelist()
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530474def get_serial_no_details(item_code, warehouse, stock_qty, serial_no):
475 args = frappe._dict({"item_code":item_code, "warehouse":warehouse, "stock_qty":stock_qty, "serial_no":serial_no})
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530476 serial_no = get_serial_no(args)
477 return {'serial_no': serial_no}
Rushabh Mehta985cb822016-12-30 15:06:54 +0530478
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530479@frappe.whitelist()
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530480def get_bin_details_and_serial_nos(item_code, warehouse, stock_qty=None, serial_no=None):
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530481 bin_details_and_serial_nos = {}
482 bin_details_and_serial_nos.update(get_bin_details(item_code, warehouse))
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530483 if stock_qty > 0:
484 bin_details_and_serial_nos.update(get_serial_no_details(item_code, warehouse, stock_qty, serial_no))
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530485 return bin_details_and_serial_nos
Anand Doshidffec8f2014-07-01 17:45:15 +0530486
487@frappe.whitelist()
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530488def get_batch_qty(batch_no, warehouse, item_code):
sburanaw66951e52017-04-25 15:28:57 +0700489 from erpnext.stock.doctype.batch import batch
Sambhaji Kolate98dbccd2015-03-10 15:04:28 +0530490 if batch_no:
Rushabh Mehtae385b5b2017-04-20 15:21:01 +0530491 return {'actual_batch_qty': batch.get_batch_qty(batch_no, warehouse)}
Rushabh Mehta1e8025b2015-07-24 15:16:25 +0530492
Anand Doshidffec8f2014-07-01 17:45:15 +0530493@frappe.whitelist()
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530494def apply_price_list(args, as_doc=False):
495 """Apply pricelist on a document-like dict object and return as
496 {'parent': dict, 'children': list}
497
498 :param args: See below
499 :param as_doc: Updates value in the passed dict
500
Anand Doshidffec8f2014-07-01 17:45:15 +0530501 args = {
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530502 "doctype": "",
503 "name": "",
504 "items": [{"doctype": "", "name": "", "item_code": "", "brand": "", "item_group": ""}, ...],
Anand Doshidffec8f2014-07-01 17:45:15 +0530505 "conversion_rate": 1.0,
506 "selling_price_list": None,
507 "price_list_currency": None,
Shreya Shahb13a54a2017-12-06 19:17:04 +0530508 "price_list_uom_dependant": None,
Anand Doshidffec8f2014-07-01 17:45:15 +0530509 "plc_conversion_rate": 1.0,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530510 "doctype": "",
511 "name": "",
Anand Doshidffec8f2014-07-01 17:45:15 +0530512 "supplier": None,
513 "transaction_date": None,
514 "conversion_rate": 1.0,
515 "buying_price_list": None,
Anand Doshidffec8f2014-07-01 17:45:15 +0530516 "ignore_pricing_rule": 0/1
517 }
518 """
519 args = process_args(args)
520
521 parent = get_price_list_currency_and_exchange_rate(args)
522 children = []
523
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530524 if "items" in args:
525 item_list = args.get("items")
Anand Doshidffec8f2014-07-01 17:45:15 +0530526 args.update(parent)
527
528 for item in item_list:
529 args_copy = frappe._dict(args.copy())
530 args_copy.update(item)
531 item_details = apply_price_list_on_item(args_copy)
532 children.append(item_details)
533
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530534 if as_doc:
Shreya Shahb13a54a2017-12-06 19:17:04 +0530535 args.price_list_currency = parent.price_list_currency,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530536 args.plc_conversion_rate = parent.plc_conversion_rate
537 if args.get('items'):
538 for i, item in enumerate(args.get('items')):
539 for fieldname in children[i]:
540 # if the field exists in the original doc
541 # update the value
542 if fieldname in item and fieldname not in ("name", "doctype"):
543 item[fieldname] = children[i][fieldname]
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530544 return args
545 else:
546 return {
547 "parent": parent,
548 "children": children
549 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530550
551def apply_price_list_on_item(args):
552 item_details = frappe._dict()
553 item_doc = frappe.get_doc("Item", args.item_code)
554 get_price_list_rate(args, item_doc, item_details)
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530555
Anand Doshidffec8f2014-07-01 17:45:15 +0530556 item_details.update(get_pricing_rule_for_item(args))
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530557
Anand Doshidffec8f2014-07-01 17:45:15 +0530558 return item_details
559
560def get_price_list_currency(price_list):
Neil Trini Lasradoadb1b2c2015-03-18 11:40:39 +0530561 if price_list:
562 result = frappe.db.get_value("Price List", {"name": price_list,
563 "enabled": 1}, ["name", "currency"], as_dict=True)
Anand Doshidffec8f2014-07-01 17:45:15 +0530564
Neil Trini Lasradoadb1b2c2015-03-18 11:40:39 +0530565 if not result:
Rushabh Mehta985cb822016-12-30 15:06:54 +0530566 throw(_("Price List {0} is disabled or does not exist").format(price_list))
Anand Doshidffec8f2014-07-01 17:45:15 +0530567
Neil Trini Lasradoadb1b2c2015-03-18 11:40:39 +0530568 return result.currency
Anand Doshidffec8f2014-07-01 17:45:15 +0530569
Shreya Shahb13a54a2017-12-06 19:17:04 +0530570def get_price_list_uom_dependant(price_list):
571 if price_list:
572 result = frappe.db.get_value("Price List", {"name": price_list,
573 "enabled": 1}, ["name", "price_not_uom_dependant"], as_dict=True)
574
575 if not result:
576 throw(_("Price List {0} is disabled or does not exist").format(price_list))
577
578 return result.price_not_uom_dependant
579
580
Anand Doshidffec8f2014-07-01 17:45:15 +0530581def get_price_list_currency_and_exchange_rate(args):
Anand Doshi70f57eb2015-11-27 14:37:40 +0530582 if not args.price_list:
583 return {}
584
Anand Doshidffec8f2014-07-01 17:45:15 +0530585 price_list_currency = get_price_list_currency(args.price_list)
Shreya Shahb13a54a2017-12-06 19:17:04 +0530586 price_list_uom_dependant = get_price_list_uom_dependant(args.price_list)
Anand Doshidffec8f2014-07-01 17:45:15 +0530587 plc_conversion_rate = args.plc_conversion_rate
588
Nabin Haitdd82bf22015-05-11 16:49:17 +0530589 if (not plc_conversion_rate) or (price_list_currency and args.price_list_currency \
590 and price_list_currency != args.price_list_currency):
Rushabh Mehta6b537922017-03-14 16:59:11 +0530591 # cksgb 19/09/2016: added args.transaction_date as posting_date argument for get_exchange_rate
Rushabh Mehta985cb822016-12-30 15:06:54 +0530592 plc_conversion_rate = get_exchange_rate(price_list_currency, args.currency,
Nabin Haitad4f1c72016-12-09 12:14:47 +0530593 args.transaction_date) or plc_conversion_rate
Anand Doshidffec8f2014-07-01 17:45:15 +0530594
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530595 return frappe._dict({
Anand Doshidffec8f2014-07-01 17:45:15 +0530596 "price_list_currency": price_list_currency,
Shreya Shahb13a54a2017-12-06 19:17:04 +0530597 "price_list_uom_dependant": price_list_uom_dependant,
Anand Doshidffec8f2014-07-01 17:45:15 +0530598 "plc_conversion_rate": plc_conversion_rate
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530599 })
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530600
601@frappe.whitelist()
602def get_default_bom(item_code=None):
603 if item_code:
604 bom = frappe.db.get_value("BOM", {"docstatus": 1, "is_default": 1, "is_active": 1, "item": item_code})
605 if bom:
606 return bom
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530607
Saurabh3fbf3ce2016-03-09 15:31:04 +0530608def get_valuation_rate(item_code, warehouse=None):
Saurabhbd01a812016-03-07 14:32:23 +0530609 item = frappe.get_doc("Item", item_code)
610 if item.is_stock_item:
Saurabhbd01a812016-03-07 14:32:23 +0530611 if not warehouse:
612 warehouse = item.default_warehouse
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530613
614 return frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse},
Saurabhbd01a812016-03-07 14:32:23 +0530615 ["valuation_rate"], as_dict=True) or {"valuation_rate": 0}
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530616
Saurabhbd01a812016-03-07 14:32:23 +0530617 elif not item.is_stock_item:
Nabin Haitfe876c02017-03-06 16:32:57 +0530618 valuation_rate =frappe.db.sql("""select sum(base_net_amount) / sum(qty*conversion_factor)
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530619 from `tabPurchase Invoice Item`
Saurabhbd01a812016-03-07 14:32:23 +0530620 where item_code = %s and docstatus=1""", item_code)
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530621
Saurabhbd01a812016-03-07 14:32:23 +0530622 if valuation_rate:
623 return {"valuation_rate": valuation_rate[0][0] or 0.0}
624 else:
625 return {"valuation_rate": 0.0}
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530626
Saurabh2d7af632016-02-26 11:09:20 +0530627def get_gross_profit(out):
Saurabh5ada14b2016-02-26 18:02:55 +0530628 if out.valuation_rate:
629 out.update({
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530630 "gross_profit": ((out.base_rate - out.valuation_rate) * out.stock_qty)
Saurabh5ada14b2016-02-26 18:02:55 +0530631 })
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530632
Saurabhd3135532016-02-25 18:59:20 +0530633 return out
mbauskar6f603392016-01-21 16:10:22 +0530634
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530635@frappe.whitelist()
Rohit Waghchaureba3f0e62017-08-28 17:19:28 +0530636def get_serial_no(args, serial_nos=None):
637 serial_no = None
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530638 if isinstance(args, basestring):
639 args = json.loads(args)
640 args = frappe._dict(args)
641
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530642 if args.get('doctype') == 'Sales Invoice' and not args.get('update_stock'):
643 return ""
644
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530645 if args.get('warehouse') and args.get('stock_qty') and args.get('item_code'):
Rushabh Mehta985cb822016-12-30 15:06:54 +0530646
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +0530647 if frappe.get_value('Item', {'item_code': args.item_code}, "has_serial_no") == 1:
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530648 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 +0530649 args = process_args(args)
650 serial_no = get_serial_nos_by_fifo(args)
Rohit Waghchaureba3f0e62017-08-28 17:19:28 +0530651
652 if not serial_no and serial_nos:
653 # For POS
654 serial_no = serial_nos
655
656 return serial_no