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