Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 5 | import frappe |
| 6 | from frappe import _, throw |
Nabin Hait | 2244ac4 | 2015-01-12 17:35:14 +0530 | [diff] [blame] | 7 | from frappe.utils import flt, cint, add_days, cstr |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 8 | import json |
Nabin Hait | 34d2822 | 2016-01-19 15:45:49 +0530 | [diff] [blame] | 9 | from erpnext.accounts.doctype.pricing_rule.pricing_rule import get_pricing_rule_for_item, set_transaction_type |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 10 | from erpnext.setup.utils import get_exchange_rate |
Rushabh Mehta | 66e08e3 | 2014-09-29 12:17:03 +0530 | [diff] [blame] | 11 | from frappe.model.meta import get_field_precision |
Nabin Hait | 98c2a05 | 2014-05-05 18:41:41 +0530 | [diff] [blame] | 12 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 13 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 14 | def 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 Hait | 615d342 | 2014-02-25 19:01:20 +0530 | [diff] [blame] | 23 | "plc_conversion_rate": 1.0, |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 24 | "doctype": "", |
| 25 | "name": "", |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 26 | "supplier": None, |
| 27 | "transaction_date": None, |
| 28 | "conversion_rate": 1.0, |
| 29 | "buying_price_list": None, |
| 30 | "is_subcontracted": "Yes" / "No", |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 31 | "ignore_pricing_rule": 0/1 |
Neil Trini Lasrado | 6e343e2 | 2016-03-09 17:02:59 +0530 | [diff] [blame] | 32 | "project": "" |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 33 | } |
| 34 | """ |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 35 | args = process_args(args) |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 36 | item_doc = frappe.get_doc("Item", args.item_code) |
| 37 | item = item_doc |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 38 | |
| 39 | validate_item_details(args, item) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 40 | |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 41 | out = get_basic_details(args, item) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 42 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 43 | get_party_item_code(args, item_doc, out) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 44 | |
| 45 | if out.get("warehouse"): |
Saurabh | d313553 | 2016-02-25 18:59:20 +0530 | [diff] [blame] | 46 | out.update(get_bin_details(args.item_code, out.warehouse)) |
Saurabh | bd01a81 | 2016-03-07 14:32:23 +0530 | [diff] [blame] | 47 | |
Saurabh | 3fbf3ce | 2016-03-09 15:31:04 +0530 | [diff] [blame] | 48 | if frappe.db.exists("Product Bundle", args.item_code): |
Saurabh | bd01a81 | 2016-03-07 14:32:23 +0530 | [diff] [blame] | 49 | valuation_rate = 0.0 |
Saurabh | 3fbf3ce | 2016-03-09 15:31:04 +0530 | [diff] [blame] | 50 | bundled_items = frappe.get_doc("Product Bundle", args.item_code) |
| 51 | |
| 52 | 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) |
Saurabh | bd01a81 | 2016-03-07 14:32:23 +0530 | [diff] [blame] | 56 | |
| 57 | out.update({ |
| 58 | "valuation_rate": valuation_rate |
| 59 | }) |
Saurabh | 3fbf3ce | 2016-03-09 15:31:04 +0530 | [diff] [blame] | 60 | |
Saurabh | 5ada14b | 2016-02-26 18:02:55 +0530 | [diff] [blame] | 61 | else: |
Saurabh | 3fbf3ce | 2016-03-09 15:31:04 +0530 | [diff] [blame] | 62 | out.update(get_valuation_rate(args.item_code, out.get("warehouse"))) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 63 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 64 | get_price_list_rate(args, item_doc, out) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 65 | |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 66 | if args.customer and cint(args.is_pos): |
Rushabh Mehta | ad9156a | 2015-08-18 15:29:42 +0530 | [diff] [blame] | 67 | out.update(get_pos_profile_item_details(args.company, args)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 68 | |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 69 | # 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 Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 74 | out.update(get_pricing_rule_for_item(args)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 75 | |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 76 | if args.get("doctype") in ("Sales Invoice", "Delivery Note"): |
Rushabh Mehta | 1e8025b | 2015-07-24 15:16:25 +0530 | [diff] [blame] | 77 | if item_doc.has_serial_no == 1 and not args.serial_no: |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 78 | out.serial_no = get_serial_nos_by_fifo(args, item_doc) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 79 | |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 80 | 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 Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 83 | |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 84 | if args.get("is_subcontracted") == "Yes": |
| 85 | out.bom = get_default_bom(args.item_code) |
Saurabh | d313553 | 2016-02-25 18:59:20 +0530 | [diff] [blame] | 86 | |
Saurabh | 2d7af63 | 2016-02-26 11:09:20 +0530 | [diff] [blame] | 87 | get_gross_profit(out) |
Anand Doshi | 70f57eb | 2015-11-27 14:37:40 +0530 | [diff] [blame] | 88 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 89 | return out |
| 90 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 91 | def process_args(args): |
| 92 | if isinstance(args, basestring): |
| 93 | args = json.loads(args) |
| 94 | |
| 95 | args = frappe._dict(args) |
| 96 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 97 | 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) |
mbauskar | 6f60339 | 2016-01-21 16:10:22 +0530 | [diff] [blame] | 104 | |
| 105 | set_transaction_type(args) |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 106 | return args |
| 107 | |
Neil Trini Lasrado | 4abda67 | 2015-02-25 17:34:27 +0530 | [diff] [blame] | 108 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 109 | def get_item_code(barcode=None, serial_no=None): |
| 110 | if barcode: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 111 | item_code = frappe.db.get_value("Item", {"barcode": barcode}) |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 112 | if not item_code: |
| 113 | frappe.throw(_("No Item with Barcode {0}").format(barcode)) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 114 | elif serial_no: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 115 | item_code = frappe.db.get_value("Serial No", serial_no, "item_code") |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 116 | if not item_code: |
| 117 | frappe.throw(_("No Item with Serial No {0}").format(serial_no)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 118 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 119 | return item_code |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 120 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 121 | def validate_item_details(args, item): |
| 122 | if not args.company: |
| 123 | throw(_("Please specify Company")) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 124 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 125 | from erpnext.stock.doctype.item.item import validate_end_of_life |
Anand Doshi | 21e09a2 | 2015-10-29 12:21:41 +0530 | [diff] [blame] | 126 | validate_end_of_life(item.name, item.end_of_life, item.disabled) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 127 | |
Nabin Hait | 34d2822 | 2016-01-19 15:45:49 +0530 | [diff] [blame] | 128 | if args.transaction_type=="selling": |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 129 | # validate if sales item or service item |
Anand Doshi | 01de945 | 2016-01-26 16:22:50 +0530 | [diff] [blame] | 130 | if item.is_sales_item != 1: |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 131 | throw(_("Item {0} must be a Sales Item").format(item.name)) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 132 | |
Rushabh Mehta | a1da88a | 2015-02-23 20:18:38 +0530 | [diff] [blame] | 133 | if cint(item.has_variants): |
| 134 | throw(_("Item {0} is a template, please select one of its variants").format(item.name)) |
| 135 | |
Nabin Hait | 34d2822 | 2016-01-19 15:45:49 +0530 | [diff] [blame] | 136 | elif args.transaction_type=="buying" and args.doctype != "Material Request": |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 137 | # validate if purchase item or subcontracted item |
Rushabh Mehta | 1e8025b | 2015-07-24 15:16:25 +0530 | [diff] [blame] | 138 | if item.is_purchase_item != 1: |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 139 | throw(_("Item {0} must be a Purchase Item").format(item.name)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 140 | |
Rushabh Mehta | 1e8025b | 2015-07-24 15:16:25 +0530 | [diff] [blame] | 141 | if args.get("is_subcontracted") == "Yes" and item.is_sub_contracted_item != 1: |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 142 | throw(_("Item {0} must be a Sub-contracted Item").format(item.name)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 143 | |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 144 | def get_basic_details(args, item): |
| 145 | if not item: |
| 146 | item = frappe.get_doc("Item", args.get("item_code")) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 147 | |
Rushabh Mehta | 724f9e5 | 2014-10-07 15:29:58 +0530 | [diff] [blame] | 148 | if item.variant_of: |
| 149 | item.update_template_tables() |
| 150 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 151 | from frappe.defaults import get_user_default_as_list |
Nabin Hait | b6b5645 | 2015-12-23 16:37:00 +0530 | [diff] [blame] | 152 | user_default_warehouse_list = get_user_default_as_list('Warehouse') |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 153 | user_default_warehouse = user_default_warehouse_list[0] \ |
| 154 | if len(user_default_warehouse_list)==1 else "" |
Saurabh | d313553 | 2016-02-25 18:59:20 +0530 | [diff] [blame] | 155 | |
| 156 | warehouse = user_default_warehouse or args.warehouse or item.default_warehouse |
Nabin Hait | 36028bf | 2014-02-12 19:09:28 +0530 | [diff] [blame] | 157 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 158 | out = frappe._dict({ |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 159 | "item_code": item.name, |
| 160 | "item_name": item.item_name, |
Neil Trini Lasrado | c48f06c | 2015-02-10 16:49:16 +0530 | [diff] [blame] | 161 | "description": cstr(item.description).strip(), |
| 162 | "image": cstr(item.image).strip(), |
Saurabh | d313553 | 2016-02-25 18:59:20 +0530 | [diff] [blame] | 163 | "warehouse": warehouse, |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 164 | "income_account": get_default_income_account(args, item), |
| 165 | "expense_account": get_default_expense_account(args, item), |
| 166 | "cost_center": get_default_cost_center(args, item), |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 167 | "batch_no": None, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 168 | "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in |
Nabin Hait | e7d1536 | 2014-12-25 16:01:55 +0530 | [diff] [blame] | 169 | item.get("taxes")))), |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 170 | "uom": item.stock_uom, |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 171 | "min_order_qty": flt(item.min_order_qty) if args.doctype == "Material Request" else "", |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 172 | "conversion_factor": 1.0, |
Nabin Hait | 4a6c178 | 2015-05-29 17:31:31 +0530 | [diff] [blame] | 173 | "qty": args.qty or 1.0, |
| 174 | "stock_qty": 1.0, |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 175 | "price_list_rate": 0.0, |
| 176 | "base_price_list_rate": 0.0, |
| 177 | "rate": 0.0, |
| 178 | "base_rate": 0.0, |
| 179 | "amount": 0.0, |
| 180 | "base_amount": 0.0, |
Nabin Hait | 82e3e25 | 2015-02-23 16:58:30 +0530 | [diff] [blame] | 181 | "net_rate": 0.0, |
| 182 | "net_amount": 0.0, |
Saurabh | c6dbe70 | 2015-10-19 14:17:52 +0530 | [diff] [blame] | 183 | "discount_percentage": 0.0, |
| 184 | "supplier": item.default_supplier, |
Saurabh | 5ada14b | 2016-02-26 18:02:55 +0530 | [diff] [blame] | 185 | "delivered_by_supplier": item.delivered_by_supplier |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 186 | }) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 187 | |
Nabin Hait | b09ed41 | 2015-02-17 10:33:58 +0530 | [diff] [blame] | 188 | # if default specified in item is for another company, fetch from company |
Saurabh | cc71327 | 2016-02-26 18:33:53 +0530 | [diff] [blame] | 189 | for d in [["Account", "income_account", "default_income_account"], |
| 190 | ["Account", "expense_account", "default_expense_account"], |
Nabin Hait | b09ed41 | 2015-02-17 10:33:58 +0530 | [diff] [blame] | 191 | ["Cost Center", "cost_center", "cost_center"], ["Warehouse", "warehouse", ""]]: |
Nabin Hait | 98a8fae | 2015-02-26 11:37:07 +0530 | [diff] [blame] | 192 | company = frappe.db.get_value(d[0], out.get(d[1]), "company") |
| 193 | if not out[d[1]] or (company and args.company != company): |
Nabin Hait | b09ed41 | 2015-02-17 10:33:58 +0530 | [diff] [blame] | 194 | out[d[1]] = frappe.db.get_value("Company", args.company, d[2]) if d[2] else None |
| 195 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 196 | for fieldname in ("item_name", "item_group", "barcode", "brand", "stock_uom"): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 197 | out[fieldname] = item.get(fieldname) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 198 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 199 | return out |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 200 | |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 201 | def get_default_income_account(args, item): |
| 202 | return (item.income_account |
| 203 | or args.income_account |
Anand Doshi | d35354c | 2015-02-24 18:12:17 +0530 | [diff] [blame] | 204 | or frappe.db.get_value("Item Group", item.item_group, "default_income_account")) |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 205 | |
| 206 | def get_default_expense_account(args, item): |
| 207 | return (item.expense_account |
| 208 | or args.expense_account |
Anand Doshi | d35354c | 2015-02-24 18:12:17 +0530 | [diff] [blame] | 209 | or frappe.db.get_value("Item Group", item.item_group, "default_expense_account")) |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 210 | |
| 211 | def get_default_cost_center(args, item): |
Neil Trini Lasrado | 6e343e2 | 2016-03-09 17:02:59 +0530 | [diff] [blame] | 212 | return (frappe.db.get_value("Project", args.get("project"), "cost_center") |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 213 | or (item.selling_cost_center if args.get("customer") else item.buying_cost_center) |
gmarke | e5a3146 | 2015-09-27 09:38:01 +0200 | [diff] [blame] | 214 | or frappe.db.get_value("Item Group", item.item_group, "default_cost_center") |
| 215 | or args.get("cost_center")) |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 216 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 217 | def get_price_list_rate(args, item_doc, out): |
Anand Doshi | 39a2891 | 2016-02-15 11:42:18 +0530 | [diff] [blame] | 218 | meta = frappe.get_meta(args.parenttype or args.doctype) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 219 | |
| 220 | if meta.get_field("currency"): |
| 221 | validate_price_list(args) |
| 222 | validate_conversion_rate(args, meta) |
| 223 | |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 224 | price_list_rate = get_price_list_rate_for(args.price_list, item_doc.name) |
Rushabh Mehta | 85abdc4 | 2015-09-03 10:29:38 +0530 | [diff] [blame] | 225 | |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 226 | # variant |
| 227 | if not price_list_rate and item_doc.variant_of: |
| 228 | price_list_rate = get_price_list_rate_for(args.price_list, item_doc.variant_of) |
| 229 | |
| 230 | # insert in database |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 231 | if not price_list_rate: |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 232 | if args.price_list and args.rate: |
| 233 | insert_item_price(args) |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 234 | return {} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 235 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 236 | out.price_list_rate = flt(price_list_rate) * flt(args.plc_conversion_rate) \ |
| 237 | / flt(args.conversion_rate) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 238 | |
Nabin Hait | 34d2822 | 2016-01-19 15:45:49 +0530 | [diff] [blame] | 239 | if not out.price_list_rate and args.transaction_type=="buying": |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 240 | from erpnext.stock.doctype.item.item import get_last_purchase_details |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 241 | out.update(get_last_purchase_details(item_doc.name, |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 242 | args.name, args.conversion_rate)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 243 | |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 244 | def insert_item_price(args): |
| 245 | """Insert Item Price if Price List and Price List Rate are specified and currency is the same""" |
| 246 | if frappe.db.get_value("Price List", args.price_list, "currency") == args.currency \ |
| 247 | and cint(frappe.db.get_single_value("Stock Settings", "auto_insert_price_list_rate_if_missing")): |
| 248 | if frappe.has_permission("Item Price", "write"): |
Rushabh Mehta | 85abdc4 | 2015-09-03 10:29:38 +0530 | [diff] [blame] | 249 | |
Nabin Hait | 906bf64 | 2015-09-02 11:32:07 +0530 | [diff] [blame] | 250 | price_list_rate = args.rate / args.conversion_factor \ |
| 251 | if args.get("conversion_factor") else args.rate |
Rushabh Mehta | 85abdc4 | 2015-09-03 10:29:38 +0530 | [diff] [blame] | 252 | |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 253 | item_price = frappe.get_doc({ |
| 254 | "doctype": "Item Price", |
| 255 | "price_list": args.price_list, |
| 256 | "item_code": args.item_code, |
| 257 | "currency": args.currency, |
Nabin Hait | 906bf64 | 2015-09-02 11:32:07 +0530 | [diff] [blame] | 258 | "price_list_rate": price_list_rate |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 259 | }) |
| 260 | item_price.insert() |
Rushabh Mehta | eb96137 | 2016-01-04 15:48:37 +0530 | [diff] [blame] | 261 | frappe.msgprint(_("Item Price added for {0} in Price List {1}").format(args.item_code, |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 262 | args.price_list)) |
| 263 | |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 264 | def get_price_list_rate_for(price_list, item_code): |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 265 | return frappe.db.get_value("Item Price", |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 266 | {"price_list": price_list, "item_code": item_code}, "price_list_rate") |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 267 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 268 | def validate_price_list(args): |
| 269 | if args.get("price_list"): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 270 | if not frappe.db.get_value("Price List", |
Nabin Hait | 34d2822 | 2016-01-19 15:45:49 +0530 | [diff] [blame] | 271 | {"name": args.price_list, args.transaction_type: 1, "enabled": 1}): |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 272 | throw(_("Price List {0} is disabled").format(args.price_list)) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 273 | else: |
| 274 | throw(_("Price List not selected")) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 275 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 276 | def validate_conversion_rate(args, meta): |
Rushabh Mehta | 66e08e3 | 2014-09-29 12:17:03 +0530 | [diff] [blame] | 277 | from erpnext.controllers.accounts_controller import validate_conversion_rate |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 278 | |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 279 | if (not args.conversion_rate |
| 280 | and args.currency==frappe.db.get_value("Company", args.company, "default_currency")): |
| 281 | args.conversion_rate = 1.0 |
| 282 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 283 | # validate currency conversion rate |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 284 | validate_conversion_rate(args.currency, args.conversion_rate, |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 285 | meta.get_label("conversion_rate"), args.company) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 286 | |
| 287 | args.conversion_rate = flt(args.conversion_rate, |
| 288 | get_field_precision(meta.get_field("conversion_rate"), |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 289 | frappe._dict({"fields": args}))) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 290 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 291 | # validate price list currency conversion rate |
| 292 | if not args.get("price_list_currency"): |
| 293 | throw(_("Price List Currency not selected")) |
| 294 | else: |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 295 | validate_conversion_rate(args.price_list_currency, args.plc_conversion_rate, |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 296 | meta.get_label("plc_conversion_rate"), args.company) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 297 | |
| 298 | args.plc_conversion_rate = flt(args.plc_conversion_rate, |
| 299 | get_field_precision(meta.get_field("plc_conversion_rate"), |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 300 | frappe._dict({"fields": args}))) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 301 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 302 | def get_party_item_code(args, item_doc, out): |
Nabin Hait | 34d2822 | 2016-01-19 15:45:49 +0530 | [diff] [blame] | 303 | if args.transaction_type=="selling" and args.customer: |
Nabin Hait | 79f091e | 2014-12-26 11:41:15 +0530 | [diff] [blame] | 304 | customer_item_code = item_doc.get("customer_items", {"customer_name": args.customer}) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 305 | out.customer_item_code = customer_item_code[0].ref_code if customer_item_code else None |
Anand Doshi | 54abf02 | 2016-01-21 22:28:47 +0530 | [diff] [blame] | 306 | |
Nabin Hait | 34d2822 | 2016-01-19 15:45:49 +0530 | [diff] [blame] | 307 | if args.transaction_type=="buying" and args.supplier: |
Nabin Hait | 79f091e | 2014-12-26 11:41:15 +0530 | [diff] [blame] | 308 | item_supplier = item_doc.get("supplier_items", {"supplier": args.supplier}) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 309 | out.supplier_part_no = item_supplier[0].supplier_part_no if item_supplier else None |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 310 | |
Rushabh Mehta | ad9156a | 2015-08-18 15:29:42 +0530 | [diff] [blame] | 311 | def get_pos_profile_item_details(company, args, pos_profile=None): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 312 | res = frappe._dict() |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 313 | |
Rushabh Mehta | ad9156a | 2015-08-18 15:29:42 +0530 | [diff] [blame] | 314 | if not pos_profile: |
| 315 | pos_profile = get_pos_profile(company) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 316 | |
Rushabh Mehta | ad9156a | 2015-08-18 15:29:42 +0530 | [diff] [blame] | 317 | if pos_profile: |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 318 | for fieldname in ("income_account", "cost_center", "warehouse", "expense_account"): |
Rushabh Mehta | ad9156a | 2015-08-18 15:29:42 +0530 | [diff] [blame] | 319 | if not args.get(fieldname) and pos_profile.get(fieldname): |
| 320 | res[fieldname] = pos_profile.get(fieldname) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 321 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 322 | if res.get("warehouse"): |
Saurabh | d313553 | 2016-02-25 18:59:20 +0530 | [diff] [blame] | 323 | res.actual_qty = get_bin_details(args.item_code, |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 324 | res.warehouse).get("actual_qty") |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 325 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 326 | return res |
| 327 | |
Rushabh Mehta | ad9156a | 2015-08-18 15:29:42 +0530 | [diff] [blame] | 328 | @frappe.whitelist() |
| 329 | def get_pos_profile(company): |
| 330 | pos_profile = frappe.db.sql("""select * from `tabPOS Profile` where user = %s |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 331 | and company = %s""", (frappe.session['user'], company), as_dict=1) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 332 | |
Rushabh Mehta | ad9156a | 2015-08-18 15:29:42 +0530 | [diff] [blame] | 333 | if not pos_profile: |
| 334 | pos_profile = frappe.db.sql("""select * from `tabPOS Profile` |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 335 | where ifnull(user,'') = '' and company = %s""", company, as_dict=1) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 336 | |
Rushabh Mehta | ad9156a | 2015-08-18 15:29:42 +0530 | [diff] [blame] | 337 | return pos_profile and pos_profile[0] or None |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 338 | |
Nabin Hait | 0822215 | 2014-02-28 11:30:47 +0530 | [diff] [blame] | 339 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 340 | def get_serial_nos_by_fifo(args, item_doc): |
Rushabh Mehta | 85abdc4 | 2015-09-03 10:29:38 +0530 | [diff] [blame] | 341 | if frappe.db.get_single_value("Stock Settings", "automatically_set_serial_nos_based_on_fifo"): |
| 342 | return "\n".join(frappe.db.sql_list("""select name from `tabSerial No` |
Nabin Hait | 398c83a | 2015-10-22 15:11:44 +0530 | [diff] [blame] | 343 | where item_code=%(item_code)s and warehouse=%(warehouse)s |
Rushabh Mehta | 85abdc4 | 2015-09-03 10:29:38 +0530 | [diff] [blame] | 344 | order by timestamp(purchase_date, purchase_time) asc limit %(qty)s""", { |
| 345 | "item_code": args.item_code, |
| 346 | "warehouse": args.warehouse, |
| 347 | "qty": abs(cint(args.qty)) |
| 348 | })) |
Nabin Hait | 0822215 | 2014-02-28 11:30:47 +0530 | [diff] [blame] | 349 | |
Sambhaji Kolate | 98dbccd | 2015-03-10 15:04:28 +0530 | [diff] [blame] | 350 | def get_actual_batch_qty(batch_no,warehouse,item_code): |
Nabin Hait | 2ed71ba | 2015-03-20 15:06:30 +0530 | [diff] [blame] | 351 | actual_batch_qty = 0 |
| 352 | if batch_no: |
| 353 | actual_batch_qty = flt(frappe.db.sql("""select sum(actual_qty) |
| 354 | from `tabStock Ledger Entry` |
| 355 | where warehouse=%s and item_code=%s and batch_no=%s""", |
| 356 | (warehouse, item_code, batch_no))[0][0]) |
| 357 | return actual_batch_qty |
Sambhaji Kolate | 98dbccd | 2015-03-10 15:04:28 +0530 | [diff] [blame] | 358 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 359 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 360 | def get_conversion_factor(item_code, uom): |
Rushabh Mehta | 724f9e5 | 2014-10-07 15:29:58 +0530 | [diff] [blame] | 361 | variant_of = frappe.db.get_value("Item", item_code, "variant_of") |
| 362 | filters = {"parent": item_code, "uom": uom} |
| 363 | if variant_of: |
Nabin Hait | 314086d | 2015-10-07 11:55:01 +0530 | [diff] [blame] | 364 | filters["parent"] = ("in", (item_code, variant_of)) |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 365 | return {"conversion_factor": frappe.db.get_value("UOM Conversion Detail", |
Rushabh Mehta | 724f9e5 | 2014-10-07 15:29:58 +0530 | [diff] [blame] | 366 | filters, "conversion_factor")} |
Nabin Hait | 0822215 | 2014-02-28 11:30:47 +0530 | [diff] [blame] | 367 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 368 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 369 | def get_projected_qty(item_code, warehouse): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 370 | return {"projected_qty": frappe.db.get_value("Bin", |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 371 | {"item_code": item_code, "warehouse": warehouse}, "projected_qty")} |
Nabin Hait | 0822215 | 2014-02-28 11:30:47 +0530 | [diff] [blame] | 372 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 373 | @frappe.whitelist() |
Saurabh | d313553 | 2016-02-25 18:59:20 +0530 | [diff] [blame] | 374 | def get_bin_details(item_code, warehouse): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 375 | return frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse}, |
Saurabh | bd01a81 | 2016-03-07 14:32:23 +0530 | [diff] [blame] | 376 | ["projected_qty", "actual_qty"], as_dict=True) \ |
Saurabh | 5ada14b | 2016-02-26 18:02:55 +0530 | [diff] [blame] | 377 | or {"projected_qty": 0, "actual_qty": 0, "valuation_rate": 0} |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 378 | |
| 379 | @frappe.whitelist() |
Sambhaji Kolate | 98dbccd | 2015-03-10 15:04:28 +0530 | [diff] [blame] | 380 | def get_batch_qty(batch_no,warehouse,item_code): |
| 381 | actual_batch_qty = get_actual_batch_qty(batch_no,warehouse,item_code) |
| 382 | if batch_no: |
| 383 | return {'actual_batch_qty': actual_batch_qty} |
Rushabh Mehta | 1e8025b | 2015-07-24 15:16:25 +0530 | [diff] [blame] | 384 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 385 | @frappe.whitelist() |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 386 | def apply_price_list(args, as_doc=False): |
| 387 | """Apply pricelist on a document-like dict object and return as |
| 388 | {'parent': dict, 'children': list} |
| 389 | |
| 390 | :param args: See below |
| 391 | :param as_doc: Updates value in the passed dict |
| 392 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 393 | args = { |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 394 | "doctype": "", |
| 395 | "name": "", |
| 396 | "items": [{"doctype": "", "name": "", "item_code": "", "brand": "", "item_group": ""}, ...], |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 397 | "conversion_rate": 1.0, |
| 398 | "selling_price_list": None, |
| 399 | "price_list_currency": None, |
| 400 | "plc_conversion_rate": 1.0, |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 401 | "doctype": "", |
| 402 | "name": "", |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 403 | "supplier": None, |
| 404 | "transaction_date": None, |
| 405 | "conversion_rate": 1.0, |
| 406 | "buying_price_list": None, |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 407 | "ignore_pricing_rule": 0/1 |
| 408 | } |
| 409 | """ |
| 410 | args = process_args(args) |
| 411 | |
| 412 | parent = get_price_list_currency_and_exchange_rate(args) |
| 413 | children = [] |
| 414 | |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 415 | if "items" in args: |
| 416 | item_list = args.get("items") |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 417 | args.update(parent) |
| 418 | |
| 419 | for item in item_list: |
| 420 | args_copy = frappe._dict(args.copy()) |
| 421 | args_copy.update(item) |
| 422 | item_details = apply_price_list_on_item(args_copy) |
| 423 | children.append(item_details) |
| 424 | |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 425 | if as_doc: |
| 426 | args.price_list_currency = parent.price_list_currency |
| 427 | args.plc_conversion_rate = parent.plc_conversion_rate |
| 428 | if args.get('items'): |
| 429 | for i, item in enumerate(args.get('items')): |
| 430 | for fieldname in children[i]: |
| 431 | # if the field exists in the original doc |
| 432 | # update the value |
| 433 | if fieldname in item and fieldname not in ("name", "doctype"): |
| 434 | item[fieldname] = children[i][fieldname] |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 435 | return args |
| 436 | else: |
| 437 | return { |
| 438 | "parent": parent, |
| 439 | "children": children |
| 440 | } |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 441 | |
| 442 | def apply_price_list_on_item(args): |
| 443 | item_details = frappe._dict() |
| 444 | item_doc = frappe.get_doc("Item", args.item_code) |
| 445 | get_price_list_rate(args, item_doc, item_details) |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 446 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 447 | item_details.update(get_pricing_rule_for_item(args)) |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 448 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 449 | return item_details |
| 450 | |
| 451 | def get_price_list_currency(price_list): |
Neil Trini Lasrado | adb1b2c | 2015-03-18 11:40:39 +0530 | [diff] [blame] | 452 | if price_list: |
| 453 | result = frappe.db.get_value("Price List", {"name": price_list, |
| 454 | "enabled": 1}, ["name", "currency"], as_dict=True) |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 455 | |
Neil Trini Lasrado | adb1b2c | 2015-03-18 11:40:39 +0530 | [diff] [blame] | 456 | if not result: |
| 457 | throw(_("Price List {0} is disabled").format(price_list)) |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 458 | |
Neil Trini Lasrado | adb1b2c | 2015-03-18 11:40:39 +0530 | [diff] [blame] | 459 | return result.currency |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 460 | |
| 461 | def get_price_list_currency_and_exchange_rate(args): |
Anand Doshi | 70f57eb | 2015-11-27 14:37:40 +0530 | [diff] [blame] | 462 | if not args.price_list: |
| 463 | return {} |
| 464 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 465 | price_list_currency = get_price_list_currency(args.price_list) |
| 466 | plc_conversion_rate = args.plc_conversion_rate |
| 467 | |
Nabin Hait | dd82bf2 | 2015-05-11 16:49:17 +0530 | [diff] [blame] | 468 | if (not plc_conversion_rate) or (price_list_currency and args.price_list_currency \ |
| 469 | and price_list_currency != args.price_list_currency): |
| 470 | plc_conversion_rate = get_exchange_rate(price_list_currency, args.currency) or plc_conversion_rate |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 471 | |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 472 | return frappe._dict({ |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 473 | "price_list_currency": price_list_currency, |
| 474 | "plc_conversion_rate": plc_conversion_rate |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 475 | }) |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 476 | |
| 477 | @frappe.whitelist() |
| 478 | def get_default_bom(item_code=None): |
| 479 | if item_code: |
| 480 | bom = frappe.db.get_value("BOM", {"docstatus": 1, "is_default": 1, "is_active": 1, "item": item_code}) |
| 481 | if bom: |
| 482 | return bom |
| 483 | else: |
| 484 | frappe.throw(_("No default BOM exists for Item {0}").format(item_code)) |
Saurabh | d313553 | 2016-02-25 18:59:20 +0530 | [diff] [blame] | 485 | |
Saurabh | 3fbf3ce | 2016-03-09 15:31:04 +0530 | [diff] [blame] | 486 | def get_valuation_rate(item_code, warehouse=None): |
Saurabh | bd01a81 | 2016-03-07 14:32:23 +0530 | [diff] [blame] | 487 | item = frappe.get_doc("Item", item_code) |
| 488 | if item.is_stock_item: |
Saurabh | bd01a81 | 2016-03-07 14:32:23 +0530 | [diff] [blame] | 489 | if not warehouse: |
| 490 | warehouse = item.default_warehouse |
| 491 | |
| 492 | return frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse}, |
| 493 | ["valuation_rate"], as_dict=True) or {"valuation_rate": 0} |
| 494 | |
| 495 | elif not item.is_stock_item: |
| 496 | valuation_rate =frappe.db.sql("""select sum(base_net_amount) / sum(qty) |
| 497 | from `tabPurchase Invoice Item` |
| 498 | where item_code = %s and docstatus=1""", item_code) |
| 499 | |
| 500 | if valuation_rate: |
| 501 | return {"valuation_rate": valuation_rate[0][0] or 0.0} |
| 502 | else: |
| 503 | return {"valuation_rate": 0.0} |
Saurabh | bd01a81 | 2016-03-07 14:32:23 +0530 | [diff] [blame] | 504 | |
Saurabh | 2d7af63 | 2016-02-26 11:09:20 +0530 | [diff] [blame] | 505 | def get_gross_profit(out): |
Saurabh | 5ada14b | 2016-02-26 18:02:55 +0530 | [diff] [blame] | 506 | if out.valuation_rate: |
| 507 | out.update({ |
| 508 | "gross_profit": ((out.base_rate - out.valuation_rate) * out.qty) |
| 509 | }) |
Saurabh | 2d7af63 | 2016-02-26 11:09:20 +0530 | [diff] [blame] | 510 | |
Saurabh | d313553 | 2016-02-25 18:59:20 +0530 | [diff] [blame] | 511 | return out |
mbauskar | 6f60339 | 2016-01-21 16:10:22 +0530 | [diff] [blame] | 512 | |