blob: 1e3bd8a209001bb0aeb94ec48f095c6ff6618072 [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
Nabin Hait98c2a052014-05-05 18:41:41 +053012
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053013@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +053014def get_item_details(args):
15 """
16 args = {
17 "item_code": "",
18 "warehouse": None,
19 "customer": "",
20 "conversion_rate": 1.0,
21 "selling_price_list": None,
22 "price_list_currency": None,
Nabin Hait615d3422014-02-25 19:01:20 +053023 "plc_conversion_rate": 1.0,
Rushabh Mehtab46069d2016-01-15 16:59:26 +053024 "doctype": "",
25 "name": "",
Nabin Hait436f5262014-02-10 14:47:54 +053026 "supplier": None,
27 "transaction_date": None,
28 "conversion_rate": 1.0,
29 "buying_price_list": None,
30 "is_subcontracted": "Yes" / "No",
Nabin Hait444f9562014-06-20 15:59:49 +053031 "ignore_pricing_rule": 0/1
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +053032 "project": ""
Nabin Hait436f5262014-02-10 14:47:54 +053033 }
34 """
Anand Doshidffec8f2014-07-01 17:45:15 +053035 args = process_args(args)
Rushabh Mehtaf14b8092014-04-03 14:30:42 +053036 item_doc = frappe.get_doc("Item", args.item_code)
37 item = item_doc
Nabin Hait436f5262014-02-10 14:47:54 +053038
39 validate_item_details(args, item)
Anand Doshibd67e872014-04-11 16:51:27 +053040
Rushabh Mehta1a763742014-10-03 16:36:43 +053041 out = get_basic_details(args, item)
Anand Doshibd67e872014-04-11 16:51:27 +053042
Rushabh Mehtaf14b8092014-04-03 14:30:42 +053043 get_party_item_code(args, item_doc, out)
Nabin Hait436f5262014-02-10 14:47:54 +053044
45 if out.get("warehouse"):
Saurabhd3135532016-02-25 18:59:20 +053046 out.update(get_bin_details(args.item_code, out.warehouse))
Saurabhbd01a812016-03-07 14:32:23 +053047
Saurabh3fbf3ce2016-03-09 15:31:04 +053048 if frappe.db.exists("Product Bundle", args.item_code):
Saurabhbd01a812016-03-07 14:32:23 +053049 valuation_rate = 0.0
Saurabh3fbf3ce2016-03-09 15:31:04 +053050 bundled_items = frappe.get_doc("Product Bundle", args.item_code)
Rushabh Mehta0394aec2016-04-22 17:22:22 +053051
Saurabh3fbf3ce2016-03-09 15:31:04 +053052 for bundle_item in bundled_items.items:
53 valuation_rate += \
54 flt(get_valuation_rate(bundle_item.item_code, out.get("warehouse")).get("valuation_rate") \
55 * bundle_item.qty)
Saurabhbd01a812016-03-07 14:32:23 +053056
57 out.update({
58 "valuation_rate": valuation_rate
59 })
Saurabh3fbf3ce2016-03-09 15:31:04 +053060
Saurabh5ada14b2016-02-26 18:02:55 +053061 else:
Saurabh3fbf3ce2016-03-09 15:31:04 +053062 out.update(get_valuation_rate(args.item_code, out.get("warehouse")))
Anand Doshibd67e872014-04-11 16:51:27 +053063
Rushabh Mehtaf14b8092014-04-03 14:30:42 +053064 get_price_list_rate(args, item_doc, out)
Nabin Hait436f5262014-02-10 14:47:54 +053065
Rushabh Mehtab46069d2016-01-15 16:59:26 +053066 if args.customer and cint(args.is_pos):
Rushabh Mehtaad9156a2015-08-18 15:29:42 +053067 out.update(get_pos_profile_item_details(args.company, args))
Anand Doshibd67e872014-04-11 16:51:27 +053068
Nabin Haita3dd72a2014-05-28 12:49:20 +053069 # update args with out, if key or value not exists
70 for key, value in out.iteritems():
71 if args.get(key) is None:
72 args[key] = value
73
Nabin Hait444f9562014-06-20 15:59:49 +053074 out.update(get_pricing_rule_for_item(args))
Anand Doshibd67e872014-04-11 16:51:27 +053075
Rushabh Mehtab46069d2016-01-15 16:59:26 +053076 if args.get("doctype") in ("Sales Invoice", "Delivery Note"):
Rushabh Mehta1e8025b2015-07-24 15:16:25 +053077 if item_doc.has_serial_no == 1 and not args.serial_no:
Rushabh Mehtaf14b8092014-04-03 14:30:42 +053078 out.serial_no = get_serial_nos_by_fifo(args, item_doc)
Anand Doshibd67e872014-04-11 16:51:27 +053079
Nabin Hait139dc7b2014-02-12 14:53:18 +053080 if args.transaction_date and item.lead_time_days:
81 out.schedule_date = out.lead_time_date = add_days(args.transaction_date,
82 item.lead_time_days)
Anand Doshibd67e872014-04-11 16:51:27 +053083
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +053084 if args.get("is_subcontracted") == "Yes":
85 out.bom = get_default_bom(args.item_code)
Rushabh Mehta0394aec2016-04-22 17:22:22 +053086
Saurabh2d7af632016-02-26 11:09:20 +053087 get_gross_profit(out)
Anand Doshi70f57eb2015-11-27 14:37:40 +053088
Nabin Hait436f5262014-02-10 14:47:54 +053089 return out
90
Anand Doshidffec8f2014-07-01 17:45:15 +053091def process_args(args):
92 if isinstance(args, basestring):
93 args = json.loads(args)
94
95 args = frappe._dict(args)
96
Anand Doshidffec8f2014-07-01 17:45:15 +053097 if not args.get("price_list"):
98 args.price_list = args.get("selling_price_list") or args.get("buying_price_list")
99
100 if args.barcode:
101 args.item_code = get_item_code(barcode=args.barcode)
102 elif not args.item_code and args.serial_no:
103 args.item_code = get_item_code(serial_no=args.serial_no)
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530104
mbauskar6f603392016-01-21 16:10:22 +0530105 set_transaction_type(args)
Anand Doshidffec8f2014-07-01 17:45:15 +0530106 return args
107
Neil Trini Lasrado4abda672015-02-25 17:34:27 +0530108@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530109def get_item_code(barcode=None, serial_no=None):
110 if barcode:
Anand Doshie9baaa62014-02-26 12:35:33 +0530111 item_code = frappe.db.get_value("Item", {"barcode": barcode})
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530112 if not item_code:
113 frappe.throw(_("No Item with Barcode {0}").format(barcode))
Nabin Hait436f5262014-02-10 14:47:54 +0530114 elif serial_no:
Anand Doshie9baaa62014-02-26 12:35:33 +0530115 item_code = frappe.db.get_value("Serial No", serial_no, "item_code")
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530116 if not item_code:
117 frappe.throw(_("No Item with Serial No {0}").format(serial_no))
Anand Doshibd67e872014-04-11 16:51:27 +0530118
Nabin Hait436f5262014-02-10 14:47:54 +0530119 return item_code
Anand Doshibd67e872014-04-11 16:51:27 +0530120
Nabin Hait436f5262014-02-10 14:47:54 +0530121def validate_item_details(args, item):
122 if not args.company:
123 throw(_("Please specify Company"))
Anand Doshibd67e872014-04-11 16:51:27 +0530124
Nabin Hait436f5262014-02-10 14:47:54 +0530125 from erpnext.stock.doctype.item.item import validate_end_of_life
Anand Doshi21e09a22015-10-29 12:21:41 +0530126 validate_end_of_life(item.name, item.end_of_life, item.disabled)
Anand Doshibd67e872014-04-11 16:51:27 +0530127
Umair Sayyed72534de2016-04-15 12:52:12 +0530128 if args.transaction_type=="selling" and cint(item.has_variants):
129 throw(_("Item {0} is a template, please select one of its variants").format(item.name))
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530130
Nabin Hait34d28222016-01-19 15:45:49 +0530131 elif args.transaction_type=="buying" and args.doctype != "Material Request":
Rushabh Mehta1e8025b2015-07-24 15:16:25 +0530132 if args.get("is_subcontracted") == "Yes" and item.is_sub_contracted_item != 1:
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530133 throw(_("Item {0} must be a Sub-contracted Item").format(item.name))
Anand Doshibd67e872014-04-11 16:51:27 +0530134
Rushabh Mehta1a763742014-10-03 16:36:43 +0530135def get_basic_details(args, item):
136 if not item:
137 item = frappe.get_doc("Item", args.get("item_code"))
Anand Doshibd67e872014-04-11 16:51:27 +0530138
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530139 if item.variant_of:
140 item.update_template_tables()
141
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530142 from frappe.defaults import get_user_default_as_list
Nabin Haitb6b56452015-12-23 16:37:00 +0530143 user_default_warehouse_list = get_user_default_as_list('Warehouse')
Nabin Hait436f5262014-02-10 14:47:54 +0530144 user_default_warehouse = user_default_warehouse_list[0] \
145 if len(user_default_warehouse_list)==1 else ""
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530146
Saurabhd3135532016-02-25 18:59:20 +0530147 warehouse = user_default_warehouse or args.warehouse or item.default_warehouse
Nabin Hait36028bf2014-02-12 19:09:28 +0530148
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530149 out = frappe._dict({
Nabin Hait139dc7b2014-02-12 14:53:18 +0530150 "item_code": item.name,
151 "item_name": item.item_name,
Neil Trini Lasradoc48f06c2015-02-10 16:49:16 +0530152 "description": cstr(item.description).strip(),
153 "image": cstr(item.image).strip(),
Saurabhd3135532016-02-25 18:59:20 +0530154 "warehouse": warehouse,
Rushabh Mehta1a763742014-10-03 16:36:43 +0530155 "income_account": get_default_income_account(args, item),
156 "expense_account": get_default_expense_account(args, item),
157 "cost_center": get_default_cost_center(args, item),
Nabin Hait139dc7b2014-02-12 14:53:18 +0530158 "batch_no": None,
Anand Doshibd67e872014-04-11 16:51:27 +0530159 "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in
Nabin Haite7d15362014-12-25 16:01:55 +0530160 item.get("taxes")))),
Nabin Hait139dc7b2014-02-12 14:53:18 +0530161 "uom": item.stock_uom,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530162 "min_order_qty": flt(item.min_order_qty) if args.doctype == "Material Request" else "",
Nabin Hait139dc7b2014-02-12 14:53:18 +0530163 "conversion_factor": 1.0,
Nabin Hait4a6c1782015-05-29 17:31:31 +0530164 "qty": args.qty or 1.0,
165 "stock_qty": 1.0,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530166 "price_list_rate": 0.0,
167 "base_price_list_rate": 0.0,
168 "rate": 0.0,
169 "base_rate": 0.0,
170 "amount": 0.0,
171 "base_amount": 0.0,
Nabin Hait82e3e252015-02-23 16:58:30 +0530172 "net_rate": 0.0,
173 "net_amount": 0.0,
Saurabhc6dbe702015-10-19 14:17:52 +0530174 "discount_percentage": 0.0,
175 "supplier": item.default_supplier,
Nabin Haite4170002016-07-05 15:37:09 +0530176 "delivered_by_supplier": item.delivered_by_supplier,
177 "is_fixed_asset": item.is_fixed_asset
Nabin Hait139dc7b2014-02-12 14:53:18 +0530178 })
Anand Doshibd67e872014-04-11 16:51:27 +0530179
Nabin Haitb09ed412015-02-17 10:33:58 +0530180 # if default specified in item is for another company, fetch from company
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530181 for d in [["Account", "income_account", "default_income_account"],
182 ["Account", "expense_account", "default_expense_account"],
Nabin Haitb09ed412015-02-17 10:33:58 +0530183 ["Cost Center", "cost_center", "cost_center"], ["Warehouse", "warehouse", ""]]:
Nabin Hait98a8fae2015-02-26 11:37:07 +0530184 company = frappe.db.get_value(d[0], out.get(d[1]), "company")
185 if not out[d[1]] or (company and args.company != company):
Nabin Haitb09ed412015-02-17 10:33:58 +0530186 out[d[1]] = frappe.db.get_value("Company", args.company, d[2]) if d[2] else None
187
Nabin Hait436f5262014-02-10 14:47:54 +0530188 for fieldname in ("item_name", "item_group", "barcode", "brand", "stock_uom"):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530189 out[fieldname] = item.get(fieldname)
Anand Doshibd67e872014-04-11 16:51:27 +0530190
Nabin Hait436f5262014-02-10 14:47:54 +0530191 return out
Anand Doshibd67e872014-04-11 16:51:27 +0530192
Rushabh Mehta1a763742014-10-03 16:36:43 +0530193def get_default_income_account(args, item):
194 return (item.income_account
195 or args.income_account
Anand Doshid35354c2015-02-24 18:12:17 +0530196 or frappe.db.get_value("Item Group", item.item_group, "default_income_account"))
Rushabh Mehta1a763742014-10-03 16:36:43 +0530197
198def get_default_expense_account(args, item):
199 return (item.expense_account
200 or args.expense_account
Anand Doshid35354c2015-02-24 18:12:17 +0530201 or frappe.db.get_value("Item Group", item.item_group, "default_expense_account"))
Rushabh Mehta1a763742014-10-03 16:36:43 +0530202
203def get_default_cost_center(args, item):
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +0530204 return (frappe.db.get_value("Project", args.get("project"), "cost_center")
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530205 or (item.selling_cost_center if args.get("customer") else item.buying_cost_center)
gmarkee5a31462015-09-27 09:38:01 +0200206 or frappe.db.get_value("Item Group", item.item_group, "default_cost_center")
207 or args.get("cost_center"))
Rushabh Mehta1a763742014-10-03 16:36:43 +0530208
Rushabh Mehtaf14b8092014-04-03 14:30:42 +0530209def get_price_list_rate(args, item_doc, out):
Anand Doshi39a28912016-02-15 11:42:18 +0530210 meta = frappe.get_meta(args.parenttype or args.doctype)
Nabin Hait436f5262014-02-10 14:47:54 +0530211
212 if meta.get_field("currency"):
213 validate_price_list(args)
214 validate_conversion_rate(args, meta)
215
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530216 price_list_rate = get_price_list_rate_for(args.price_list, item_doc.name)
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530217
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530218 # variant
219 if not price_list_rate and item_doc.variant_of:
220 price_list_rate = get_price_list_rate_for(args.price_list, item_doc.variant_of)
221
222 # insert in database
Rushabh Mehta1a763742014-10-03 16:36:43 +0530223 if not price_list_rate:
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530224 if args.price_list and args.rate:
225 insert_item_price(args)
Rushabh Mehta1a763742014-10-03 16:36:43 +0530226 return {}
Anand Doshibd67e872014-04-11 16:51:27 +0530227
Nabin Hait436f5262014-02-10 14:47:54 +0530228 out.price_list_rate = flt(price_list_rate) * flt(args.plc_conversion_rate) \
229 / flt(args.conversion_rate)
Anand Doshibd67e872014-04-11 16:51:27 +0530230
Nabin Hait34d28222016-01-19 15:45:49 +0530231 if not out.price_list_rate and args.transaction_type=="buying":
Nabin Hait436f5262014-02-10 14:47:54 +0530232 from erpnext.stock.doctype.item.item import get_last_purchase_details
Anand Doshibd67e872014-04-11 16:51:27 +0530233 out.update(get_last_purchase_details(item_doc.name,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530234 args.name, args.conversion_rate))
Anand Doshibd67e872014-04-11 16:51:27 +0530235
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530236def insert_item_price(args):
237 """Insert Item Price if Price List and Price List Rate are specified and currency is the same"""
238 if frappe.db.get_value("Price List", args.price_list, "currency") == args.currency \
239 and cint(frappe.db.get_single_value("Stock Settings", "auto_insert_price_list_rate_if_missing")):
240 if frappe.has_permission("Item Price", "write"):
Nabin Hait906bf642015-09-02 11:32:07 +0530241 price_list_rate = args.rate / args.conversion_factor \
242 if args.get("conversion_factor") else args.rate
Kanchan Chauhan5ee8fcc2016-06-24 17:11:15 +0530243
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530244 item_price = frappe.get_doc({
245 "doctype": "Item Price",
246 "price_list": args.price_list,
247 "item_code": args.item_code,
248 "currency": args.currency,
Nabin Hait906bf642015-09-02 11:32:07 +0530249 "price_list_rate": price_list_rate
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530250 })
Kanchan Chauhan5ee8fcc2016-06-24 17:11:15 +0530251
Kanchan Chauhan2ff7d2a2016-06-25 17:54:25 +0530252 name = frappe.db.get_value('Item Price', {'item_code': args.item_code, 'price_list': args.price_list, 'currency': args.currency}, 'name')
Kanchan Chauhan5ee8fcc2016-06-24 17:11:15 +0530253
254 if name:
255 item_price = frappe.get_doc('Item Price', name)
256 item_price.price_list_rate = price_list_rate
257 item_price.save()
258 frappe.msgprint(_("Item Price updated for {0} in Price List {1}").format(args.item_code,
259 args.price_list))
260 else:
261 item_price.insert()
262 frappe.msgprint(_("Item Price added for {0} in Price List {1}").format(args.item_code,
263 args.price_list))
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530264
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530265def get_price_list_rate_for(price_list, item_code):
Rushabh Mehta1a763742014-10-03 16:36:43 +0530266 return frappe.db.get_value("Item Price",
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530267 {"price_list": price_list, "item_code": item_code}, "price_list_rate")
Rushabh Mehta1a763742014-10-03 16:36:43 +0530268
Nabin Hait436f5262014-02-10 14:47:54 +0530269def validate_price_list(args):
270 if args.get("price_list"):
Anand Doshibd67e872014-04-11 16:51:27 +0530271 if not frappe.db.get_value("Price List",
Nabin Hait34d28222016-01-19 15:45:49 +0530272 {"name": args.price_list, args.transaction_type: 1, "enabled": 1}):
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530273 throw(_("Price List {0} is disabled").format(args.price_list))
Nabin Hait436f5262014-02-10 14:47:54 +0530274 else:
275 throw(_("Price List not selected"))
Anand Doshibd67e872014-04-11 16:51:27 +0530276
Nabin Hait436f5262014-02-10 14:47:54 +0530277def validate_conversion_rate(args, meta):
Rushabh Mehta66e08e32014-09-29 12:17:03 +0530278 from erpnext.controllers.accounts_controller import validate_conversion_rate
Anand Doshibd67e872014-04-11 16:51:27 +0530279
Anand Doshife13bfe2015-08-25 12:49:40 +0530280 if (not args.conversion_rate
281 and args.currency==frappe.db.get_value("Company", args.company, "default_currency")):
282 args.conversion_rate = 1.0
283
Nabin Hait436f5262014-02-10 14:47:54 +0530284 # validate currency conversion rate
Anand Doshibd67e872014-04-11 16:51:27 +0530285 validate_conversion_rate(args.currency, args.conversion_rate,
Nabin Hait436f5262014-02-10 14:47:54 +0530286 meta.get_label("conversion_rate"), args.company)
Anand Doshibd67e872014-04-11 16:51:27 +0530287
288 args.conversion_rate = flt(args.conversion_rate,
289 get_field_precision(meta.get_field("conversion_rate"),
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530290 frappe._dict({"fields": args})))
Anand Doshibd67e872014-04-11 16:51:27 +0530291
Nabin Hait436f5262014-02-10 14:47:54 +0530292 # validate price list currency conversion rate
293 if not args.get("price_list_currency"):
294 throw(_("Price List Currency not selected"))
295 else:
Anand Doshibd67e872014-04-11 16:51:27 +0530296 validate_conversion_rate(args.price_list_currency, args.plc_conversion_rate,
Nabin Hait436f5262014-02-10 14:47:54 +0530297 meta.get_label("plc_conversion_rate"), args.company)
Anand Doshibd67e872014-04-11 16:51:27 +0530298
299 args.plc_conversion_rate = flt(args.plc_conversion_rate,
300 get_field_precision(meta.get_field("plc_conversion_rate"),
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530301 frappe._dict({"fields": args})))
Anand Doshibd67e872014-04-11 16:51:27 +0530302
Rushabh Mehtaf14b8092014-04-03 14:30:42 +0530303def get_party_item_code(args, item_doc, out):
Nabin Hait34d28222016-01-19 15:45:49 +0530304 if args.transaction_type=="selling" and args.customer:
Nabin Hait79f091e2014-12-26 11:41:15 +0530305 customer_item_code = item_doc.get("customer_items", {"customer_name": args.customer})
Nabin Hait436f5262014-02-10 14:47:54 +0530306 out.customer_item_code = customer_item_code[0].ref_code if customer_item_code else None
Anand Doshi54abf022016-01-21 22:28:47 +0530307
Nabin Hait34d28222016-01-19 15:45:49 +0530308 if args.transaction_type=="buying" and args.supplier:
Nabin Hait79f091e2014-12-26 11:41:15 +0530309 item_supplier = item_doc.get("supplier_items", {"supplier": args.supplier})
Nabin Hait436f5262014-02-10 14:47:54 +0530310 out.supplier_part_no = item_supplier[0].supplier_part_no if item_supplier else None
Anand Doshibd67e872014-04-11 16:51:27 +0530311
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530312def get_pos_profile_item_details(company, args, pos_profile=None):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530313 res = frappe._dict()
Anand Doshibd67e872014-04-11 16:51:27 +0530314
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530315 if not pos_profile:
316 pos_profile = get_pos_profile(company)
Anand Doshibd67e872014-04-11 16:51:27 +0530317
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530318 if pos_profile:
Nabin Hait436f5262014-02-10 14:47:54 +0530319 for fieldname in ("income_account", "cost_center", "warehouse", "expense_account"):
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530320 if not args.get(fieldname) and pos_profile.get(fieldname):
321 res[fieldname] = pos_profile.get(fieldname)
Anand Doshibd67e872014-04-11 16:51:27 +0530322
Nabin Hait436f5262014-02-10 14:47:54 +0530323 if res.get("warehouse"):
Saurabhd3135532016-02-25 18:59:20 +0530324 res.actual_qty = get_bin_details(args.item_code,
Nabin Hait436f5262014-02-10 14:47:54 +0530325 res.warehouse).get("actual_qty")
Anand Doshibd67e872014-04-11 16:51:27 +0530326
Nabin Hait436f5262014-02-10 14:47:54 +0530327 return res
328
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530329@frappe.whitelist()
330def get_pos_profile(company):
331 pos_profile = frappe.db.sql("""select * from `tabPOS Profile` where user = %s
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530332 and company = %s""", (frappe.session['user'], company), as_dict=1)
Anand Doshibd67e872014-04-11 16:51:27 +0530333
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530334 if not pos_profile:
335 pos_profile = frappe.db.sql("""select * from `tabPOS Profile`
Nabin Hait436f5262014-02-10 14:47:54 +0530336 where ifnull(user,'') = '' and company = %s""", company, as_dict=1)
Anand Doshibd67e872014-04-11 16:51:27 +0530337
Rushabh Mehtaad9156a2015-08-18 15:29:42 +0530338 return pos_profile and pos_profile[0] or None
Anand Doshibd67e872014-04-11 16:51:27 +0530339
Nabin Hait08222152014-02-28 11:30:47 +0530340
Rushabh Mehtaf14b8092014-04-03 14:30:42 +0530341def get_serial_nos_by_fifo(args, item_doc):
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530342 if frappe.db.get_single_value("Stock Settings", "automatically_set_serial_nos_based_on_fifo"):
343 return "\n".join(frappe.db.sql_list("""select name from `tabSerial No`
Nabin Hait398c83a2015-10-22 15:11:44 +0530344 where item_code=%(item_code)s and warehouse=%(warehouse)s
Rushabh Mehta85abdc42015-09-03 10:29:38 +0530345 order by timestamp(purchase_date, purchase_time) asc limit %(qty)s""", {
346 "item_code": args.item_code,
347 "warehouse": args.warehouse,
348 "qty": abs(cint(args.qty))
349 }))
Nabin Hait08222152014-02-28 11:30:47 +0530350
Sambhaji Kolate98dbccd2015-03-10 15:04:28 +0530351def get_actual_batch_qty(batch_no,warehouse,item_code):
Nabin Hait2ed71ba2015-03-20 15:06:30 +0530352 actual_batch_qty = 0
353 if batch_no:
354 actual_batch_qty = flt(frappe.db.sql("""select sum(actual_qty)
355 from `tabStock Ledger Entry`
356 where warehouse=%s and item_code=%s and batch_no=%s""",
357 (warehouse, item_code, batch_no))[0][0])
358 return actual_batch_qty
Sambhaji Kolate98dbccd2015-03-10 15:04:28 +0530359
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530360@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530361def get_conversion_factor(item_code, uom):
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530362 variant_of = frappe.db.get_value("Item", item_code, "variant_of")
363 filters = {"parent": item_code, "uom": uom}
364 if variant_of:
Nabin Hait314086d2015-10-07 11:55:01 +0530365 filters["parent"] = ("in", (item_code, variant_of))
Anand Doshie9baaa62014-02-26 12:35:33 +0530366 return {"conversion_factor": frappe.db.get_value("UOM Conversion Detail",
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530367 filters, "conversion_factor")}
Nabin Hait08222152014-02-28 11:30:47 +0530368
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530369@frappe.whitelist()
Nabin Hait436f5262014-02-10 14:47:54 +0530370def get_projected_qty(item_code, warehouse):
Anand Doshibd67e872014-04-11 16:51:27 +0530371 return {"projected_qty": frappe.db.get_value("Bin",
Nabin Hait436f5262014-02-10 14:47:54 +0530372 {"item_code": item_code, "warehouse": warehouse}, "projected_qty")}
Nabin Hait08222152014-02-28 11:30:47 +0530373
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530374@frappe.whitelist()
Saurabhd3135532016-02-25 18:59:20 +0530375def get_bin_details(item_code, warehouse):
Anand Doshibd67e872014-04-11 16:51:27 +0530376 return frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse},
Saurabhbd01a812016-03-07 14:32:23 +0530377 ["projected_qty", "actual_qty"], as_dict=True) \
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530378 or {"projected_qty": 0, "actual_qty": 0}
Anand Doshidffec8f2014-07-01 17:45:15 +0530379
380@frappe.whitelist()
Sambhaji Kolate98dbccd2015-03-10 15:04:28 +0530381def get_batch_qty(batch_no,warehouse,item_code):
382 actual_batch_qty = get_actual_batch_qty(batch_no,warehouse,item_code)
383 if batch_no:
384 return {'actual_batch_qty': actual_batch_qty}
Rushabh Mehta1e8025b2015-07-24 15:16:25 +0530385
Anand Doshidffec8f2014-07-01 17:45:15 +0530386@frappe.whitelist()
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530387def apply_price_list(args, as_doc=False):
388 """Apply pricelist on a document-like dict object and return as
389 {'parent': dict, 'children': list}
390
391 :param args: See below
392 :param as_doc: Updates value in the passed dict
393
Anand Doshidffec8f2014-07-01 17:45:15 +0530394 args = {
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530395 "doctype": "",
396 "name": "",
397 "items": [{"doctype": "", "name": "", "item_code": "", "brand": "", "item_group": ""}, ...],
Anand Doshidffec8f2014-07-01 17:45:15 +0530398 "conversion_rate": 1.0,
399 "selling_price_list": None,
400 "price_list_currency": None,
401 "plc_conversion_rate": 1.0,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530402 "doctype": "",
403 "name": "",
Anand Doshidffec8f2014-07-01 17:45:15 +0530404 "supplier": None,
405 "transaction_date": None,
406 "conversion_rate": 1.0,
407 "buying_price_list": None,
Anand Doshidffec8f2014-07-01 17:45:15 +0530408 "ignore_pricing_rule": 0/1
409 }
410 """
411 args = process_args(args)
412
413 parent = get_price_list_currency_and_exchange_rate(args)
414 children = []
415
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530416 if "items" in args:
417 item_list = args.get("items")
Anand Doshidffec8f2014-07-01 17:45:15 +0530418 args.update(parent)
419
420 for item in item_list:
421 args_copy = frappe._dict(args.copy())
422 args_copy.update(item)
423 item_details = apply_price_list_on_item(args_copy)
424 children.append(item_details)
425
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530426 if as_doc:
427 args.price_list_currency = parent.price_list_currency
428 args.plc_conversion_rate = parent.plc_conversion_rate
429 if args.get('items'):
430 for i, item in enumerate(args.get('items')):
431 for fieldname in children[i]:
432 # if the field exists in the original doc
433 # update the value
434 if fieldname in item and fieldname not in ("name", "doctype"):
435 item[fieldname] = children[i][fieldname]
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530436 return args
437 else:
438 return {
439 "parent": parent,
440 "children": children
441 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530442
443def apply_price_list_on_item(args):
444 item_details = frappe._dict()
445 item_doc = frappe.get_doc("Item", args.item_code)
446 get_price_list_rate(args, item_doc, item_details)
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530447
Anand Doshidffec8f2014-07-01 17:45:15 +0530448 item_details.update(get_pricing_rule_for_item(args))
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530449
Anand Doshidffec8f2014-07-01 17:45:15 +0530450 return item_details
451
452def get_price_list_currency(price_list):
Neil Trini Lasradoadb1b2c2015-03-18 11:40:39 +0530453 if price_list:
454 result = frappe.db.get_value("Price List", {"name": price_list,
455 "enabled": 1}, ["name", "currency"], as_dict=True)
Anand Doshidffec8f2014-07-01 17:45:15 +0530456
Neil Trini Lasradoadb1b2c2015-03-18 11:40:39 +0530457 if not result:
458 throw(_("Price List {0} is disabled").format(price_list))
Anand Doshidffec8f2014-07-01 17:45:15 +0530459
Neil Trini Lasradoadb1b2c2015-03-18 11:40:39 +0530460 return result.currency
Anand Doshidffec8f2014-07-01 17:45:15 +0530461
462def get_price_list_currency_and_exchange_rate(args):
Anand Doshi70f57eb2015-11-27 14:37:40 +0530463 if not args.price_list:
464 return {}
465
Anand Doshidffec8f2014-07-01 17:45:15 +0530466 price_list_currency = get_price_list_currency(args.price_list)
467 plc_conversion_rate = args.plc_conversion_rate
468
Nabin Haitdd82bf22015-05-11 16:49:17 +0530469 if (not plc_conversion_rate) or (price_list_currency and args.price_list_currency \
470 and price_list_currency != args.price_list_currency):
471 plc_conversion_rate = get_exchange_rate(price_list_currency, args.currency) or plc_conversion_rate
Anand Doshidffec8f2014-07-01 17:45:15 +0530472
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530473 return frappe._dict({
Anand Doshidffec8f2014-07-01 17:45:15 +0530474 "price_list_currency": price_list_currency,
475 "plc_conversion_rate": plc_conversion_rate
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530476 })
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530477
478@frappe.whitelist()
479def get_default_bom(item_code=None):
480 if item_code:
481 bom = frappe.db.get_value("BOM", {"docstatus": 1, "is_default": 1, "is_active": 1, "item": item_code})
482 if bom:
483 return bom
484 else:
485 frappe.throw(_("No default BOM exists for Item {0}").format(item_code))
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530486
Saurabh3fbf3ce2016-03-09 15:31:04 +0530487def get_valuation_rate(item_code, warehouse=None):
Saurabhbd01a812016-03-07 14:32:23 +0530488 item = frappe.get_doc("Item", item_code)
489 if item.is_stock_item:
Saurabhbd01a812016-03-07 14:32:23 +0530490 if not warehouse:
491 warehouse = item.default_warehouse
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530492
493 return frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse},
Saurabhbd01a812016-03-07 14:32:23 +0530494 ["valuation_rate"], as_dict=True) or {"valuation_rate": 0}
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530495
Saurabhbd01a812016-03-07 14:32:23 +0530496 elif not item.is_stock_item:
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530497 valuation_rate =frappe.db.sql("""select sum(base_net_amount) / sum(qty)
498 from `tabPurchase Invoice Item`
Saurabhbd01a812016-03-07 14:32:23 +0530499 where item_code = %s and docstatus=1""", item_code)
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530500
Saurabhbd01a812016-03-07 14:32:23 +0530501 if valuation_rate:
502 return {"valuation_rate": valuation_rate[0][0] or 0.0}
503 else:
504 return {"valuation_rate": 0.0}
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530505
Saurabh2d7af632016-02-26 11:09:20 +0530506def get_gross_profit(out):
Saurabh5ada14b2016-02-26 18:02:55 +0530507 if out.valuation_rate:
508 out.update({
509 "gross_profit": ((out.base_rate - out.valuation_rate) * out.qty)
510 })
Rushabh Mehta0394aec2016-04-22 17:22:22 +0530511
Saurabhd3135532016-02-25 18:59:20 +0530512 return out
mbauskar6f603392016-01-21 16:10:22 +0530513