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 |
Manas Solanki | 0393848 | 2018-05-14 16:16:46 +0530 | [diff] [blame] | 7 | from frappe.utils import flt, cint, add_days, cstr, add_months |
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 |
tundebabzy | 6e90f49 | 2018-02-12 10:48:57 +0100 | [diff] [blame] | 13 | from erpnext import get_company_currency |
Manas Solanki | b16a4ec | 2018-05-04 16:49:33 +0530 | [diff] [blame] | 14 | from erpnext.stock.doctype.item.item import get_item_defaults |
Manas Solanki | 087a225 | 2018-05-04 16:02:38 +0530 | [diff] [blame] | 15 | |
Nabin Hait | 98c2a05 | 2014-05-05 18:41:41 +0530 | [diff] [blame] | 16 | |
Achilles Rasquinha | 714c746 | 2018-02-15 11:28:55 +0530 | [diff] [blame] | 17 | from six import string_types, iteritems |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 18 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 19 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 20 | def get_item_details(args): |
| 21 | """ |
| 22 | args = { |
| 23 | "item_code": "", |
| 24 | "warehouse": None, |
| 25 | "customer": "", |
| 26 | "conversion_rate": 1.0, |
| 27 | "selling_price_list": None, |
| 28 | "price_list_currency": None, |
Nabin Hait | 615d342 | 2014-02-25 19:01:20 +0530 | [diff] [blame] | 29 | "plc_conversion_rate": 1.0, |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 30 | "doctype": "", |
| 31 | "name": "", |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 32 | "supplier": None, |
| 33 | "transaction_date": None, |
| 34 | "conversion_rate": 1.0, |
| 35 | "buying_price_list": None, |
| 36 | "is_subcontracted": "Yes" / "No", |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 37 | "ignore_pricing_rule": 0/1 |
Neil Trini Lasrado | 6e343e2 | 2016-03-09 17:02:59 +0530 | [diff] [blame] | 38 | "project": "" |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 39 | } |
| 40 | """ |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 41 | args = process_args(args) |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 42 | item_doc = frappe.get_doc("Item", args.item_code) |
| 43 | item = item_doc |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 44 | |
| 45 | validate_item_details(args, item) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 46 | |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 47 | out = get_basic_details(args, item) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 48 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 49 | get_party_item_code(args, item_doc, out) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 50 | |
Saurabh | 3fbf3ce | 2016-03-09 15:31:04 +0530 | [diff] [blame] | 51 | if frappe.db.exists("Product Bundle", args.item_code): |
Saurabh | bd01a81 | 2016-03-07 14:32:23 +0530 | [diff] [blame] | 52 | valuation_rate = 0.0 |
Saurabh | 3fbf3ce | 2016-03-09 15:31:04 +0530 | [diff] [blame] | 53 | bundled_items = frappe.get_doc("Product Bundle", args.item_code) |
Rushabh Mehta | 0394aec | 2016-04-22 17:22:22 +0530 | [diff] [blame] | 54 | |
Saurabh | 3fbf3ce | 2016-03-09 15:31:04 +0530 | [diff] [blame] | 55 | for bundle_item in bundled_items.items: |
| 56 | valuation_rate += \ |
Manas Solanki | 087a225 | 2018-05-04 16:02:38 +0530 | [diff] [blame] | 57 | flt(get_valuation_rate(bundle_item.item_code, args.company, out.get("warehouse")).get("valuation_rate") \ |
Saurabh | 3fbf3ce | 2016-03-09 15:31:04 +0530 | [diff] [blame] | 58 | * bundle_item.qty) |
Saurabh | bd01a81 | 2016-03-07 14:32:23 +0530 | [diff] [blame] | 59 | |
| 60 | out.update({ |
| 61 | "valuation_rate": valuation_rate |
| 62 | }) |
Saurabh | 3fbf3ce | 2016-03-09 15:31:04 +0530 | [diff] [blame] | 63 | |
Saurabh | 5ada14b | 2016-02-26 18:02:55 +0530 | [diff] [blame] | 64 | else: |
Manas Solanki | 087a225 | 2018-05-04 16:02:38 +0530 | [diff] [blame] | 65 | out.update(get_valuation_rate(args.item_code, args.company, out.get("warehouse"))) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 66 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 67 | get_price_list_rate(args, item_doc, out) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 68 | |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 69 | if args.customer and cint(args.is_pos): |
Rushabh Mehta | ad9156a | 2015-08-18 15:29:42 +0530 | [diff] [blame] | 70 | out.update(get_pos_profile_item_details(args.company, args)) |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 71 | |
Nabin Hait | 0287d31 | 2017-01-11 10:31:33 +0530 | [diff] [blame] | 72 | if out.get("warehouse"): |
| 73 | out.update(get_bin_details(args.item_code, out.warehouse)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 74 | |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 75 | # update args with out, if key or value not exists |
Achilles Rasquinha | 714c746 | 2018-02-15 11:28:55 +0530 | [diff] [blame] | 76 | for key, value in iteritems(out): |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 77 | if args.get(key) is None: |
| 78 | args[key] = value |
| 79 | |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 80 | out.update(get_pricing_rule_for_item(args)) |
pratu16x7 | 577e4c4 | 2017-06-23 11:39:03 +0530 | [diff] [blame] | 81 | if (args.get("doctype") == "Delivery Note" or |
Nabin Hait | 3ce41d6 | 2017-05-03 18:22:24 +0530 | [diff] [blame] | 82 | (args.get("doctype") == "Sales Invoice" and args.get('update_stock'))) \ |
| 83 | and out.warehouse and out.stock_qty > 0: |
pratu16x7 | 577e4c4 | 2017-06-23 11:39:03 +0530 | [diff] [blame] | 84 | |
Nabin Hait | 8fac2ad | 2017-05-18 11:54:24 +0530 | [diff] [blame] | 85 | if out.has_batch_no and not args.get("batch_no"): |
Rushabh Mehta | 551406a | 2017-04-21 12:40:19 +0530 | [diff] [blame] | 86 | out.batch_no = get_batch_no(out.item_code, out.warehouse, out.qty) |
rohitwaghchaure | 2fb8cc5 | 2017-11-29 13:50:04 +0530 | [diff] [blame] | 87 | actual_batch_qty = get_batch_qty(out.batch_no, out.warehouse, out.item_code) |
| 88 | if actual_batch_qty: |
| 89 | out.update(actual_batch_qty) |
Shreya | 14bd43d | 2018-04-27 16:24:05 +0530 | [diff] [blame] | 90 | |
| 91 | if out.has_serial_no and args.get('batch_no'): |
| 92 | out.batch_no = args.get('batch_no') |
| 93 | out.serial_no = get_serial_no(out, args.serial_no) |
| 94 | |
| 95 | elif out.has_serial_no: |
Shreya | a20157a | 2018-04-13 12:03:42 +0530 | [diff] [blame] | 96 | out.serial_no = get_serial_no(out, args.serial_no) |
Rushabh Mehta | 551406a | 2017-04-21 12:40:19 +0530 | [diff] [blame] | 97 | |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 98 | if args.transaction_date and item.lead_time_days: |
| 99 | out.schedule_date = out.lead_time_date = add_days(args.transaction_date, |
| 100 | item.lead_time_days) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 101 | |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 102 | if args.get("is_subcontracted") == "Yes": |
Saurabh | 590d401 | 2017-09-18 15:49:19 +0530 | [diff] [blame] | 103 | out.bom = args.get('bom') or get_default_bom(args.item_code) |
Rushabh Mehta | 0394aec | 2016-04-22 17:22:22 +0530 | [diff] [blame] | 104 | |
Saurabh | 2d7af63 | 2016-02-26 11:09:20 +0530 | [diff] [blame] | 105 | get_gross_profit(out) |
Rohit Waghchaure | 4d4fb6d | 2018-05-15 22:12:44 +0530 | [diff] [blame] | 106 | if args.doctype == 'Material Request': |
| 107 | out.rate = args.rate or out.price_list_rate |
| 108 | out.amount = flt(args.qty * out.rate) |
Anand Doshi | 70f57eb | 2015-11-27 14:37:40 +0530 | [diff] [blame] | 109 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 110 | return out |
| 111 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 112 | def process_args(args): |
Achilles Rasquinha | 56b2e12 | 2018-02-13 14:42:40 +0530 | [diff] [blame] | 113 | if isinstance(args, string_types): |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 114 | args = json.loads(args) |
| 115 | |
| 116 | args = frappe._dict(args) |
| 117 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 118 | if not args.get("price_list"): |
| 119 | args.price_list = args.get("selling_price_list") or args.get("buying_price_list") |
| 120 | |
| 121 | if args.barcode: |
| 122 | args.item_code = get_item_code(barcode=args.barcode) |
| 123 | elif not args.item_code and args.serial_no: |
| 124 | args.item_code = get_item_code(serial_no=args.serial_no) |
Rushabh Mehta | 0394aec | 2016-04-22 17:22:22 +0530 | [diff] [blame] | 125 | |
mbauskar | 6f60339 | 2016-01-21 16:10:22 +0530 | [diff] [blame] | 126 | set_transaction_type(args) |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 127 | return args |
| 128 | |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 129 | |
Neil Trini Lasrado | 4abda67 | 2015-02-25 17:34:27 +0530 | [diff] [blame] | 130 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 131 | def get_item_code(barcode=None, serial_no=None): |
| 132 | if barcode: |
Giovanni | 2144e02 | 2017-12-10 17:27:09 +0100 | [diff] [blame] | 133 | item_code = frappe.db.get_value("Item Barcode", {"barcode": barcode}, fieldname=["parent"]) |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 134 | if not item_code: |
| 135 | frappe.throw(_("No Item with Barcode {0}").format(barcode)) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 136 | elif serial_no: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 137 | item_code = frappe.db.get_value("Serial No", serial_no, "item_code") |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 138 | if not item_code: |
| 139 | frappe.throw(_("No Item with Serial No {0}").format(serial_no)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 140 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 141 | return item_code |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 142 | |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 143 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 144 | def validate_item_details(args, item): |
| 145 | if not args.company: |
| 146 | throw(_("Please specify Company")) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 147 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 148 | from erpnext.stock.doctype.item.item import validate_end_of_life |
Anand Doshi | 21e09a2 | 2015-10-29 12:21:41 +0530 | [diff] [blame] | 149 | validate_end_of_life(item.name, item.end_of_life, item.disabled) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 150 | |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 151 | if args.transaction_type == "selling" and cint(item.has_variants): |
Umair Sayyed | 72534de | 2016-04-15 12:52:12 +0530 | [diff] [blame] | 152 | 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] | 153 | |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 154 | elif args.transaction_type == "buying" and args.doctype != "Material Request": |
Rushabh Mehta | 1e8025b | 2015-07-24 15:16:25 +0530 | [diff] [blame] | 155 | 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] | 156 | throw(_("Item {0} must be a Sub-contracted Item").format(item.name)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 157 | |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 158 | |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 159 | def get_basic_details(args, item): |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 160 | """ |
| 161 | :param args: { |
| 162 | "item_code": "", |
| 163 | "warehouse": None, |
| 164 | "customer": "", |
| 165 | "conversion_rate": 1.0, |
| 166 | "selling_price_list": None, |
| 167 | "price_list_currency": None, |
Shreya Shah | b13a54a | 2017-12-06 19:17:04 +0530 | [diff] [blame] | 168 | "price_list_uom_dependant": None, |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 169 | "plc_conversion_rate": 1.0, |
| 170 | "doctype": "", |
| 171 | "name": "", |
| 172 | "supplier": None, |
| 173 | "transaction_date": None, |
| 174 | "conversion_rate": 1.0, |
| 175 | "buying_price_list": None, |
| 176 | "is_subcontracted": "Yes" / "No", |
| 177 | "ignore_pricing_rule": 0/1 |
| 178 | "project": "", |
| 179 | barcode: "", |
| 180 | serial_no: "", |
| 181 | warehouse: "", |
| 182 | currency: "", |
| 183 | update_stock: "", |
| 184 | price_list: "", |
| 185 | company: "", |
| 186 | order_type: "", |
| 187 | is_pos: "", |
| 188 | ignore_pricing_rule: "", |
| 189 | project: "", |
| 190 | qty: "", |
| 191 | stock_qty: "", |
| 192 | conversion_factor: "" |
| 193 | } |
| 194 | :param item: `item_code` of Item object |
| 195 | :return: frappe._dict |
| 196 | """ |
| 197 | |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 198 | if not item: |
| 199 | item = frappe.get_doc("Item", args.get("item_code")) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 200 | |
Rushabh Mehta | 724f9e5 | 2014-10-07 15:29:58 +0530 | [diff] [blame] | 201 | if item.variant_of: |
| 202 | item.update_template_tables() |
| 203 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 204 | from frappe.defaults import get_user_default_as_list |
Nabin Hait | b6b5645 | 2015-12-23 16:37:00 +0530 | [diff] [blame] | 205 | user_default_warehouse_list = get_user_default_as_list('Warehouse') |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 206 | user_default_warehouse = user_default_warehouse_list[0] \ |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 207 | if len(user_default_warehouse_list) == 1 else "" |
Manas Solanki | 6461437 | 2018-05-07 16:55:32 +0530 | [diff] [blame] | 208 | |
Manas Solanki | b16a4ec | 2018-05-04 16:49:33 +0530 | [diff] [blame] | 209 | item_defaults = get_item_defaults(item.name, args.company) |
Manas Solanki | e3910fb | 2018-05-16 15:50:17 +0530 | [diff] [blame] | 210 | warehouse = user_default_warehouse or item_defaults.get("default_warehouse") or args.warehouse |
Nabin Hait | 36028bf | 2014-02-12 19:09:28 +0530 | [diff] [blame] | 211 | |
rohitwaghchaure | e8ccc0e | 2017-11-21 16:17:22 +0530 | [diff] [blame] | 212 | material_request_type = '' |
Rohit Waghchaure | 6cdaa6e | 2018-05-18 16:30:14 +0530 | [diff] [blame] | 213 | if args.get('doctype') == "Material Request" and not args.get('material_request_type'): |
| 214 | args['material_request_type'] = frappe.db.get_value('Material Request', |
rohitwaghchaure | e8ccc0e | 2017-11-21 16:17:22 +0530 | [diff] [blame] | 215 | args.get('name'), 'material_request_type') |
| 216 | |
Nabin Hait | 79a1d2a | 2017-09-29 18:14:10 +0530 | [diff] [blame] | 217 | #Set the UOM to the Default Sales UOM or Default Purchase UOM if configured in the Item Master |
| 218 | if not args.uom: |
| 219 | if args.get('doctype') in ['Quotation', 'Sales Order', 'Delivery Note', 'Sales Invoice']: |
| 220 | args.uom = item.sales_uom if item.sales_uom else item.stock_uom |
rohitwaghchaure | e8ccc0e | 2017-11-21 16:17:22 +0530 | [diff] [blame] | 221 | elif (args.get('doctype') in ['Purchase Order', 'Purchase Receipt', 'Purchase Invoice']) or \ |
Rohit Waghchaure | 6cdaa6e | 2018-05-18 16:30:14 +0530 | [diff] [blame] | 222 | (args.get('doctype') == 'Material Request' and args.get('material_request_type') == 'Purchase'): |
Nabin Hait | 79a1d2a | 2017-09-29 18:14:10 +0530 | [diff] [blame] | 223 | args.uom = item.purchase_uom if item.purchase_uom else item.stock_uom |
| 224 | else: |
| 225 | args.uom = item.stock_uom |
| 226 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 227 | out = frappe._dict({ |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 228 | "item_code": item.name, |
| 229 | "item_name": item.item_name, |
Neil Trini Lasrado | c48f06c | 2015-02-10 16:49:16 +0530 | [diff] [blame] | 230 | "description": cstr(item.description).strip(), |
| 231 | "image": cstr(item.image).strip(), |
Saurabh | d313553 | 2016-02-25 18:59:20 +0530 | [diff] [blame] | 232 | "warehouse": warehouse, |
Manas Solanki | b16a4ec | 2018-05-04 16:49:33 +0530 | [diff] [blame] | 233 | "income_account": get_default_income_account(args, item_defaults), |
| 234 | "expense_account": get_default_expense_account(args, item_defaults), |
| 235 | "cost_center": get_default_cost_center(args, item_defaults), |
Rushabh Mehta | 551406a | 2017-04-21 12:40:19 +0530 | [diff] [blame] | 236 | 'has_serial_no': item.has_serial_no, |
| 237 | 'has_batch_no': item.has_batch_no, |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 238 | "batch_no": None, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 239 | "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] | 240 | item.get("taxes")))), |
Nabin Hait | 79a1d2a | 2017-09-29 18:14:10 +0530 | [diff] [blame] | 241 | "uom": args.uom, |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 242 | "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] | 243 | "qty": args.qty or 1.0, |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 244 | "stock_qty": args.qty or 1.0, |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 245 | "price_list_rate": 0.0, |
| 246 | "base_price_list_rate": 0.0, |
| 247 | "rate": 0.0, |
| 248 | "base_rate": 0.0, |
| 249 | "amount": 0.0, |
| 250 | "base_amount": 0.0, |
Nabin Hait | 82e3e25 | 2015-02-23 16:58:30 +0530 | [diff] [blame] | 251 | "net_rate": 0.0, |
| 252 | "net_amount": 0.0, |
Saurabh | c6dbe70 | 2015-10-19 14:17:52 +0530 | [diff] [blame] | 253 | "discount_percentage": 0.0, |
Manas Solanki | f877628 | 2018-05-20 10:58:26 +0530 | [diff] [blame] | 254 | "supplier": item_defaults.get("default_supplier"), |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 255 | "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] | 256 | "delivered_by_supplier": item.delivered_by_supplier if args.get("doctype") in ["Sales Order", "Sales Invoice"] else 0, |
Vishal Dhayagude | d42242d | 2017-11-29 16:09:59 +0530 | [diff] [blame] | 257 | "is_fixed_asset": item.is_fixed_asset, |
| 258 | "weight_per_unit":item.weight_per_unit, |
| 259 | "weight_uom":item.weight_uom, |
Shreya Shah | 44fa9a6 | 2018-01-08 14:58:20 +0530 | [diff] [blame] | 260 | "last_purchase_rate": item.last_purchase_rate if args.get("doctype") in ["Purchase Order"] else 0 |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 261 | }) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 262 | |
Manas Solanki | 0393848 | 2018-05-14 16:16:46 +0530 | [diff] [blame] | 263 | if item.enable_deferred_revenue: |
| 264 | service_end_date = add_months(args.transaction_date, item.no_of_months) |
| 265 | out.update({ |
| 266 | "enable_deferred_revenue": item.enable_deferred_revenue, |
| 267 | "deferred_revenue_account": get_default_deferred_revenue_account(args, item), |
| 268 | "service_start_date": args.transaction_date, |
| 269 | "service_end_date": service_end_date |
| 270 | }) |
| 271 | |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 272 | # calculate conversion factor |
mbauskar | 287fe81 | 2017-04-10 19:15:57 +0530 | [diff] [blame] | 273 | if item.stock_uom == args.uom: |
| 274 | out.conversion_factor = 1.0 |
| 275 | else: |
| 276 | out.conversion_factor = args.conversion_factor or \ |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 277 | get_conversion_factor(item.item_code, args.uom).get("conversion_factor") or 1.0 |
mbauskar | 287fe81 | 2017-04-10 19:15:57 +0530 | [diff] [blame] | 278 | |
| 279 | args.conversion_factor = out.conversion_factor |
| 280 | out.stock_qty = out.qty * out.conversion_factor |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 281 | |
Shreya Shah | 44fa9a6 | 2018-01-08 14:58:20 +0530 | [diff] [blame] | 282 | # calculate last purchase rate |
| 283 | from erpnext.buying.doctype.purchase_order.purchase_order import item_last_purchase_rate |
| 284 | out.last_purchase_rate = item_last_purchase_rate(args.name, args.conversion_rate, item.item_code, out.conversion_factor) |
| 285 | |
Nabin Hait | b09ed41 | 2015-02-17 10:33:58 +0530 | [diff] [blame] | 286 | # if default specified in item is for another company, fetch from company |
Rushabh Mehta | e385b5b | 2017-04-20 15:21:01 +0530 | [diff] [blame] | 287 | for d in [ |
| 288 | ["Account", "income_account", "default_income_account"], |
Rushabh Mehta | 0394aec | 2016-04-22 17:22:22 +0530 | [diff] [blame] | 289 | ["Account", "expense_account", "default_expense_account"], |
Rushabh Mehta | e385b5b | 2017-04-20 15:21:01 +0530 | [diff] [blame] | 290 | ["Cost Center", "cost_center", "cost_center"], |
| 291 | ["Warehouse", "warehouse", ""]]: |
Nabin Hait | 33df0b4 | 2018-05-26 09:09:02 +0530 | [diff] [blame] | 292 | if not out[d[1]]: |
Nabin Hait | b09ed41 | 2015-02-17 10:33:58 +0530 | [diff] [blame] | 293 | out[d[1]] = frappe.db.get_value("Company", args.company, d[2]) if d[2] else None |
| 294 | |
Giovanni | 2144e02 | 2017-12-10 17:27:09 +0100 | [diff] [blame] | 295 | for fieldname in ("item_name", "item_group", "barcodes", "brand", "stock_uom"): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 296 | out[fieldname] = item.get(fieldname) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 297 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 298 | return out |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 299 | |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 300 | |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 301 | def get_default_income_account(args, item): |
Manas Solanki | 38667ab | 2018-05-16 16:55:23 +0530 | [diff] [blame] | 302 | return (item.get("income_account") |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 303 | or args.income_account |
Anand Doshi | d35354c | 2015-02-24 18:12:17 +0530 | [diff] [blame] | 304 | 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] | 305 | |
| 306 | def get_default_expense_account(args, item): |
Manas Solanki | 38667ab | 2018-05-16 16:55:23 +0530 | [diff] [blame] | 307 | return (item.get("expense_account") |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 308 | or args.expense_account |
Anand Doshi | d35354c | 2015-02-24 18:12:17 +0530 | [diff] [blame] | 309 | 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] | 310 | |
Manas Solanki | 0393848 | 2018-05-14 16:16:46 +0530 | [diff] [blame] | 311 | def get_default_deferred_revenue_account(args, item): |
| 312 | if item.enable_deferred_revenue: |
| 313 | return (item.deferred_revenue_account |
| 314 | or args.deferred_revenue_account |
| 315 | or frappe.db.get_value("Company", args.company, "default_deferred_revenue_account")) |
| 316 | else: |
| 317 | return None |
| 318 | |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 319 | def get_default_cost_center(args, item): |
Neil Trini Lasrado | 6e343e2 | 2016-03-09 17:02:59 +0530 | [diff] [blame] | 320 | return (frappe.db.get_value("Project", args.get("project"), "cost_center") |
Manas Solanki | 38667ab | 2018-05-16 16:55:23 +0530 | [diff] [blame] | 321 | or (item.get("selling_cost_center") if args.get("customer") else item.get("buying_cost_center")) |
gmarke | e5a3146 | 2015-09-27 09:38:01 +0200 | [diff] [blame] | 322 | or frappe.db.get_value("Item Group", item.item_group, "default_cost_center") |
| 323 | or args.get("cost_center")) |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 324 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 325 | def get_price_list_rate(args, item_doc, out): |
Anand Doshi | 39a2891 | 2016-02-15 11:42:18 +0530 | [diff] [blame] | 326 | meta = frappe.get_meta(args.parenttype or args.doctype) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 327 | |
Rohit Waghchaure | 4d4fb6d | 2018-05-15 22:12:44 +0530 | [diff] [blame] | 328 | if meta.get_field("currency") or args.get('currency'): |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 329 | validate_price_list(args) |
Rohit Waghchaure | 4d4fb6d | 2018-05-15 22:12:44 +0530 | [diff] [blame] | 330 | if meta.get_field("currency") and args.price_list: |
rohitwaghchaure | 4cccdbd | 2017-07-25 14:05:01 +0530 | [diff] [blame] | 331 | validate_conversion_rate(args, meta) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 332 | |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 333 | 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] | 334 | |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 335 | # variant |
| 336 | if not price_list_rate and item_doc.variant_of: |
| 337 | price_list_rate = get_price_list_rate_for(args.price_list, item_doc.variant_of) |
| 338 | |
| 339 | # insert in database |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 340 | if not price_list_rate: |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 341 | if args.price_list and args.rate: |
| 342 | insert_item_price(args) |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 343 | return {} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 344 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 345 | out.price_list_rate = flt(price_list_rate) * flt(args.plc_conversion_rate) \ |
| 346 | / flt(args.conversion_rate) |
Zarrar | 17fd661 | 2017-12-08 14:57:38 +0530 | [diff] [blame] | 347 | if not args.price_list_uom_dependant: |
Manas Solanki | 94769d8 | 2018-03-29 11:15:05 +0530 | [diff] [blame] | 348 | out.price_list_rate = flt(out.price_list_rate * (flt(args.conversion_factor) or 1.0)) |
mbauskar | 287fe81 | 2017-04-10 19:15:57 +0530 | [diff] [blame] | 349 | |
Nabin Hait | 34d2822 | 2016-01-19 15:45:49 +0530 | [diff] [blame] | 350 | if not out.price_list_rate and args.transaction_type=="buying": |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 351 | from erpnext.stock.doctype.item.item import get_last_purchase_details |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 352 | out.update(get_last_purchase_details(item_doc.name, |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 353 | args.name, args.conversion_rate)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 354 | |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 355 | def insert_item_price(args): |
| 356 | """Insert Item Price if Price List and Price List Rate are specified and currency is the same""" |
| 357 | if frappe.db.get_value("Price List", args.price_list, "currency") == args.currency \ |
| 358 | and cint(frappe.db.get_single_value("Stock Settings", "auto_insert_price_list_rate_if_missing")): |
| 359 | if frappe.has_permission("Item Price", "write"): |
Nabin Hait | 906bf64 | 2015-09-02 11:32:07 +0530 | [diff] [blame] | 360 | price_list_rate = args.rate / args.conversion_factor \ |
| 361 | if args.get("conversion_factor") else args.rate |
Rushabh Mehta | 985cb82 | 2016-12-30 15:06:54 +0530 | [diff] [blame] | 362 | |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 363 | item_price = frappe.get_doc({ |
| 364 | "doctype": "Item Price", |
| 365 | "price_list": args.price_list, |
| 366 | "item_code": args.item_code, |
| 367 | "currency": args.currency, |
Nabin Hait | 906bf64 | 2015-09-02 11:32:07 +0530 | [diff] [blame] | 368 | "price_list_rate": price_list_rate |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 369 | }) |
Rushabh Mehta | 985cb82 | 2016-12-30 15:06:54 +0530 | [diff] [blame] | 370 | |
Kanchan Chauhan | 2ff7d2a | 2016-06-25 17:54:25 +0530 | [diff] [blame] | 371 | 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] | 372 | |
Kanchan Chauhan | 5ee8fcc | 2016-06-24 17:11:15 +0530 | [diff] [blame] | 373 | if name: |
| 374 | item_price = frappe.get_doc('Item Price', name) |
| 375 | item_price.price_list_rate = price_list_rate |
Rushabh Mehta | 985cb82 | 2016-12-30 15:06:54 +0530 | [diff] [blame] | 376 | item_price.save() |
Kanchan Chauhan | 5ee8fcc | 2016-06-24 17:11:15 +0530 | [diff] [blame] | 377 | frappe.msgprint(_("Item Price updated for {0} in Price List {1}").format(args.item_code, |
| 378 | args.price_list)) |
Rushabh Mehta | 985cb82 | 2016-12-30 15:06:54 +0530 | [diff] [blame] | 379 | else: |
Kanchan Chauhan | 5ee8fcc | 2016-06-24 17:11:15 +0530 | [diff] [blame] | 380 | item_price.insert() |
| 381 | frappe.msgprint(_("Item Price added for {0} in Price List {1}").format(args.item_code, |
| 382 | args.price_list)) |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 383 | |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 384 | def get_price_list_rate_for(price_list, item_code): |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 385 | return frappe.db.get_value("Item Price", |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 386 | {"price_list": price_list, "item_code": item_code}, "price_list_rate") |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 387 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 388 | def validate_price_list(args): |
| 389 | if args.get("price_list"): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 390 | if not frappe.db.get_value("Price List", |
Nabin Hait | 34d2822 | 2016-01-19 15:45:49 +0530 | [diff] [blame] | 391 | {"name": args.price_list, args.transaction_type: 1, "enabled": 1}): |
Rushabh Mehta | 985cb82 | 2016-12-30 15:06:54 +0530 | [diff] [blame] | 392 | 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] | 393 | elif not args.get("supplier"): |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 394 | throw(_("Price List not selected")) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 395 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 396 | def validate_conversion_rate(args, meta): |
Rushabh Mehta | 66e08e3 | 2014-09-29 12:17:03 +0530 | [diff] [blame] | 397 | from erpnext.controllers.accounts_controller import validate_conversion_rate |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 398 | |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 399 | if (not args.conversion_rate |
| 400 | and args.currency==frappe.db.get_value("Company", args.company, "default_currency")): |
| 401 | args.conversion_rate = 1.0 |
| 402 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 403 | # validate currency conversion rate |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 404 | validate_conversion_rate(args.currency, args.conversion_rate, |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 405 | meta.get_label("conversion_rate"), args.company) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 406 | |
| 407 | args.conversion_rate = flt(args.conversion_rate, |
| 408 | get_field_precision(meta.get_field("conversion_rate"), |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 409 | frappe._dict({"fields": args}))) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 410 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 411 | # validate price list currency conversion rate |
| 412 | if not args.get("price_list_currency"): |
| 413 | throw(_("Price List Currency not selected")) |
| 414 | else: |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 415 | validate_conversion_rate(args.price_list_currency, args.plc_conversion_rate, |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 416 | meta.get_label("plc_conversion_rate"), args.company) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 417 | |
| 418 | args.plc_conversion_rate = flt(args.plc_conversion_rate, |
| 419 | get_field_precision(meta.get_field("plc_conversion_rate"), |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 420 | frappe._dict({"fields": args}))) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 421 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 422 | def get_party_item_code(args, item_doc, out): |
Nabin Hait | 34d2822 | 2016-01-19 15:45:49 +0530 | [diff] [blame] | 423 | if args.transaction_type=="selling" and args.customer: |
Zarrar | 3fd6347 | 2018-03-09 12:10:45 +0530 | [diff] [blame] | 424 | out.customer_item_code = None |
Nabin Hait | 79f091e | 2014-12-26 11:41:15 +0530 | [diff] [blame] | 425 | customer_item_code = item_doc.get("customer_items", {"customer_name": args.customer}) |
Zarrar | 3fd6347 | 2018-03-09 12:10:45 +0530 | [diff] [blame] | 426 | |
| 427 | if customer_item_code: |
| 428 | out.customer_item_code = customer_item_code[0].ref_code |
| 429 | else: |
| 430 | customer_group = frappe.db.get_value("Customer", args.customer, "customer_group") |
| 431 | customer_group_item_code = item_doc.get("customer_items", {"customer_group": customer_group}) |
| 432 | if customer_group_item_code and not customer_group_item_code[0].customer_name: |
| 433 | out.customer_item_code = customer_group_item_code[0].ref_code |
Anand Doshi | 54abf02 | 2016-01-21 22:28:47 +0530 | [diff] [blame] | 434 | |
Nabin Hait | 34d2822 | 2016-01-19 15:45:49 +0530 | [diff] [blame] | 435 | if args.transaction_type=="buying" and args.supplier: |
Nabin Hait | 79f091e | 2014-12-26 11:41:15 +0530 | [diff] [blame] | 436 | item_supplier = item_doc.get("supplier_items", {"supplier": args.supplier}) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 437 | 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] | 438 | |
rohitwaghchaure | 2ea593b | 2018-04-12 13:37:08 +0530 | [diff] [blame] | 439 | def get_pos_profile_item_details(company, args, pos_profile=None, update_data=False): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 440 | res = frappe._dict() |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 441 | |
Rushabh Mehta | ad9156a | 2015-08-18 15:29:42 +0530 | [diff] [blame] | 442 | if not pos_profile: |
rohitwaghchaure | c037dc7 | 2017-11-28 16:11:15 +0530 | [diff] [blame] | 443 | pos_profile = get_pos_profile(company, args.get('pos_profile')) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 444 | |
Rushabh Mehta | ad9156a | 2015-08-18 15:29:42 +0530 | [diff] [blame] | 445 | if pos_profile: |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 446 | for fieldname in ("income_account", "cost_center", "warehouse", "expense_account"): |
rohitwaghchaure | 2ea593b | 2018-04-12 13:37:08 +0530 | [diff] [blame] | 447 | if (not args.get(fieldname) or update_data) and pos_profile.get(fieldname): |
Rushabh Mehta | ad9156a | 2015-08-18 15:29:42 +0530 | [diff] [blame] | 448 | res[fieldname] = pos_profile.get(fieldname) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 449 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 450 | if res.get("warehouse"): |
Saurabh | d313553 | 2016-02-25 18:59:20 +0530 | [diff] [blame] | 451 | res.actual_qty = get_bin_details(args.item_code, |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 452 | res.warehouse).get("actual_qty") |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 453 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 454 | return res |
| 455 | |
Rushabh Mehta | ad9156a | 2015-08-18 15:29:42 +0530 | [diff] [blame] | 456 | @frappe.whitelist() |
rohitwaghchaure | c037dc7 | 2017-11-28 16:11:15 +0530 | [diff] [blame] | 457 | def get_pos_profile(company, pos_profile=None, user=None): |
| 458 | if pos_profile: |
| 459 | return frappe.get_doc('POS Profile', pos_profile) |
| 460 | |
| 461 | if not user: |
| 462 | user = frappe.session['user'] |
| 463 | |
| 464 | pos_profile = frappe.db.sql("""select pf.* |
| 465 | from |
| 466 | `tabPOS Profile` pf, `tabPOS Profile User` pfu |
| 467 | where |
| 468 | pfu.parent = pf.name and pfu.user = %s and pf.company = %s |
| 469 | and pf.disabled = 0 and pfu.default=1""", (user, company), as_dict=1) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 470 | |
Nabin Hait | c469f2c | 2017-04-03 13:01:11 +0530 | [diff] [blame] | 471 | if not pos_profile: |
rohitwaghchaure | c037dc7 | 2017-11-28 16:11:15 +0530 | [diff] [blame] | 472 | pos_profile = frappe.db.sql("""select pf.* |
| 473 | from |
| 474 | `tabPOS Profile` pf left join `tabPOS Profile User` pfu |
| 475 | on |
| 476 | pf.name = pfu.parent |
| 477 | where |
| 478 | ifnull(pfu.user, '') = '' and pf.company = %s |
| 479 | and pf.disabled = 0""", (company), as_dict=1) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 480 | |
Rushabh Mehta | ad9156a | 2015-08-18 15:29:42 +0530 | [diff] [blame] | 481 | return pos_profile and pos_profile[0] or None |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 482 | |
Kanchan Chauhan | 0a3f2c8 | 2016-10-25 23:15:39 +0530 | [diff] [blame] | 483 | def get_serial_nos_by_fifo(args): |
Rushabh Mehta | 85abdc4 | 2015-09-03 10:29:38 +0530 | [diff] [blame] | 484 | if frappe.db.get_single_value("Stock Settings", "automatically_set_serial_nos_based_on_fifo"): |
| 485 | return "\n".join(frappe.db.sql_list("""select name from `tabSerial No` |
Nabin Hait | 398c83a | 2015-10-22 15:11:44 +0530 | [diff] [blame] | 486 | where item_code=%(item_code)s and warehouse=%(warehouse)s |
Rushabh Mehta | 85abdc4 | 2015-09-03 10:29:38 +0530 | [diff] [blame] | 487 | order by timestamp(purchase_date, purchase_time) asc limit %(qty)s""", { |
| 488 | "item_code": args.item_code, |
| 489 | "warehouse": args.warehouse, |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 490 | "qty": abs(cint(args.stock_qty)) |
Rushabh Mehta | 85abdc4 | 2015-09-03 10:29:38 +0530 | [diff] [blame] | 491 | })) |
Nabin Hait | 0822215 | 2014-02-28 11:30:47 +0530 | [diff] [blame] | 492 | |
Shreya | a20157a | 2018-04-13 12:03:42 +0530 | [diff] [blame] | 493 | def get_serial_no_batchwise(args): |
| 494 | if frappe.db.get_single_value("Stock Settings", "automatically_set_serial_nos_based_on_fifo"): |
| 495 | return "\n".join(frappe.db.sql_list("""select name from `tabSerial No` |
Shreya | 14bd43d | 2018-04-27 16:24:05 +0530 | [diff] [blame] | 496 | where item_code=%(item_code)s and warehouse=%(warehouse)s and (batch_no=%(batch_no)s or batch_no is NULL) |
Shreya | a20157a | 2018-04-13 12:03:42 +0530 | [diff] [blame] | 497 | order by timestamp(purchase_date, purchase_time) asc limit %(qty)s""", { |
| 498 | "item_code": args.item_code, |
| 499 | "warehouse": args.warehouse, |
| 500 | "batch_no": args.batch_no, |
| 501 | "qty": abs(cint(args.stock_qty)) |
| 502 | })) |
| 503 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 504 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 505 | def get_conversion_factor(item_code, uom): |
Rushabh Mehta | 724f9e5 | 2014-10-07 15:29:58 +0530 | [diff] [blame] | 506 | variant_of = frappe.db.get_value("Item", item_code, "variant_of") |
| 507 | filters = {"parent": item_code, "uom": uom} |
| 508 | if variant_of: |
Nabin Hait | 314086d | 2015-10-07 11:55:01 +0530 | [diff] [blame] | 509 | filters["parent"] = ("in", (item_code, variant_of)) |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 510 | return {"conversion_factor": frappe.db.get_value("UOM Conversion Detail", |
Rushabh Mehta | 724f9e5 | 2014-10-07 15:29:58 +0530 | [diff] [blame] | 511 | filters, "conversion_factor")} |
Nabin Hait | 0822215 | 2014-02-28 11:30:47 +0530 | [diff] [blame] | 512 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 513 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 514 | def get_projected_qty(item_code, warehouse): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 515 | return {"projected_qty": frappe.db.get_value("Bin", |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 516 | {"item_code": item_code, "warehouse": warehouse}, "projected_qty")} |
Nabin Hait | 0822215 | 2014-02-28 11:30:47 +0530 | [diff] [blame] | 517 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 518 | @frappe.whitelist() |
Saurabh | d313553 | 2016-02-25 18:59:20 +0530 | [diff] [blame] | 519 | def get_bin_details(item_code, warehouse): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 520 | return frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse}, |
Manas Solanki | a884bd9 | 2018-02-09 13:12:07 +0530 | [diff] [blame] | 521 | ["projected_qty", "actual_qty"], as_dict=True) \ |
| 522 | or {"projected_qty": 0, "actual_qty": 0} |
Kanchan Chauhan | af0d637 | 2016-11-13 21:19:09 +0530 | [diff] [blame] | 523 | |
| 524 | @frappe.whitelist() |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 525 | def get_serial_no_details(item_code, warehouse, stock_qty, serial_no): |
| 526 | 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] | 527 | serial_no = get_serial_no(args) |
| 528 | return {'serial_no': serial_no} |
Rushabh Mehta | 985cb82 | 2016-12-30 15:06:54 +0530 | [diff] [blame] | 529 | |
Kanchan Chauhan | af0d637 | 2016-11-13 21:19:09 +0530 | [diff] [blame] | 530 | @frappe.whitelist() |
Shreya | a20157a | 2018-04-13 12:03:42 +0530 | [diff] [blame] | 531 | def get_bin_details_and_serial_nos(item_code, warehouse, has_batch_no, stock_qty=None, serial_no=None): |
Kanchan Chauhan | af0d637 | 2016-11-13 21:19:09 +0530 | [diff] [blame] | 532 | bin_details_and_serial_nos = {} |
| 533 | bin_details_and_serial_nos.update(get_bin_details(item_code, warehouse)) |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 534 | if stock_qty > 0: |
Shreya | a20157a | 2018-04-13 12:03:42 +0530 | [diff] [blame] | 535 | if has_batch_no: |
| 536 | args = frappe._dict({"item_code":item_code, "warehouse":warehouse, "stock_qty":stock_qty}) |
| 537 | serial_no = get_serial_no(args) |
| 538 | bin_details_and_serial_nos.update({'serial_no': serial_no}) |
| 539 | return bin_details_and_serial_nos |
| 540 | |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 541 | 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] | 542 | return bin_details_and_serial_nos |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 543 | |
| 544 | @frappe.whitelist() |
Shreya | a20157a | 2018-04-13 12:03:42 +0530 | [diff] [blame] | 545 | def get_batch_qty_and_serial_no(batch_no, stock_qty, warehouse, item_code, has_serial_no): |
| 546 | batch_qty_and_serial_no = {} |
| 547 | batch_qty_and_serial_no.update(get_batch_qty(batch_no, warehouse, item_code)) |
| 548 | |
| 549 | if (flt(batch_qty_and_serial_no.get('actual_batch_qty')) >= flt(stock_qty)) and has_serial_no: |
| 550 | args = frappe._dict({"item_code":item_code, "warehouse":warehouse, "stock_qty":stock_qty, "batch_no":batch_no}) |
| 551 | serial_no = get_serial_no(args) |
| 552 | batch_qty_and_serial_no.update({'serial_no': serial_no}) |
| 553 | return batch_qty_and_serial_no |
| 554 | |
| 555 | @frappe.whitelist() |
Rushabh Mehta | e385b5b | 2017-04-20 15:21:01 +0530 | [diff] [blame] | 556 | def get_batch_qty(batch_no, warehouse, item_code): |
sburanaw | 66951e5 | 2017-04-25 15:28:57 +0700 | [diff] [blame] | 557 | from erpnext.stock.doctype.batch import batch |
Sambhaji Kolate | 98dbccd | 2015-03-10 15:04:28 +0530 | [diff] [blame] | 558 | if batch_no: |
Rushabh Mehta | e385b5b | 2017-04-20 15:21:01 +0530 | [diff] [blame] | 559 | return {'actual_batch_qty': batch.get_batch_qty(batch_no, warehouse)} |
Rushabh Mehta | 1e8025b | 2015-07-24 15:16:25 +0530 | [diff] [blame] | 560 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 561 | @frappe.whitelist() |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 562 | def apply_price_list(args, as_doc=False): |
| 563 | """Apply pricelist on a document-like dict object and return as |
| 564 | {'parent': dict, 'children': list} |
| 565 | |
| 566 | :param args: See below |
| 567 | :param as_doc: Updates value in the passed dict |
| 568 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 569 | args = { |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 570 | "doctype": "", |
| 571 | "name": "", |
| 572 | "items": [{"doctype": "", "name": "", "item_code": "", "brand": "", "item_group": ""}, ...], |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 573 | "conversion_rate": 1.0, |
| 574 | "selling_price_list": None, |
| 575 | "price_list_currency": None, |
Shreya Shah | b13a54a | 2017-12-06 19:17:04 +0530 | [diff] [blame] | 576 | "price_list_uom_dependant": None, |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 577 | "plc_conversion_rate": 1.0, |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 578 | "doctype": "", |
| 579 | "name": "", |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 580 | "supplier": None, |
| 581 | "transaction_date": None, |
| 582 | "conversion_rate": 1.0, |
| 583 | "buying_price_list": None, |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 584 | "ignore_pricing_rule": 0/1 |
| 585 | } |
| 586 | """ |
| 587 | args = process_args(args) |
| 588 | |
| 589 | parent = get_price_list_currency_and_exchange_rate(args) |
| 590 | children = [] |
| 591 | |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 592 | if "items" in args: |
| 593 | item_list = args.get("items") |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 594 | args.update(parent) |
| 595 | |
| 596 | for item in item_list: |
| 597 | args_copy = frappe._dict(args.copy()) |
| 598 | args_copy.update(item) |
| 599 | item_details = apply_price_list_on_item(args_copy) |
| 600 | children.append(item_details) |
| 601 | |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 602 | if as_doc: |
Shreya Shah | b13a54a | 2017-12-06 19:17:04 +0530 | [diff] [blame] | 603 | args.price_list_currency = parent.price_list_currency, |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 604 | args.plc_conversion_rate = parent.plc_conversion_rate |
| 605 | if args.get('items'): |
| 606 | for i, item in enumerate(args.get('items')): |
| 607 | for fieldname in children[i]: |
| 608 | # if the field exists in the original doc |
| 609 | # update the value |
| 610 | if fieldname in item and fieldname not in ("name", "doctype"): |
| 611 | item[fieldname] = children[i][fieldname] |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 612 | return args |
| 613 | else: |
| 614 | return { |
| 615 | "parent": parent, |
| 616 | "children": children |
| 617 | } |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 618 | |
| 619 | def apply_price_list_on_item(args): |
| 620 | item_details = frappe._dict() |
| 621 | item_doc = frappe.get_doc("Item", args.item_code) |
| 622 | get_price_list_rate(args, item_doc, item_details) |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 623 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 624 | item_details.update(get_pricing_rule_for_item(args)) |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 625 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 626 | return item_details |
| 627 | |
| 628 | def get_price_list_currency(price_list): |
Neil Trini Lasrado | adb1b2c | 2015-03-18 11:40:39 +0530 | [diff] [blame] | 629 | if price_list: |
| 630 | result = frappe.db.get_value("Price List", {"name": price_list, |
| 631 | "enabled": 1}, ["name", "currency"], as_dict=True) |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 632 | |
Neil Trini Lasrado | adb1b2c | 2015-03-18 11:40:39 +0530 | [diff] [blame] | 633 | if not result: |
Rushabh Mehta | 985cb82 | 2016-12-30 15:06:54 +0530 | [diff] [blame] | 634 | 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] | 635 | |
Neil Trini Lasrado | adb1b2c | 2015-03-18 11:40:39 +0530 | [diff] [blame] | 636 | return result.currency |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 637 | |
Shreya Shah | b13a54a | 2017-12-06 19:17:04 +0530 | [diff] [blame] | 638 | def get_price_list_uom_dependant(price_list): |
| 639 | if price_list: |
| 640 | result = frappe.db.get_value("Price List", {"name": price_list, |
| 641 | "enabled": 1}, ["name", "price_not_uom_dependant"], as_dict=True) |
| 642 | |
| 643 | if not result: |
| 644 | throw(_("Price List {0} is disabled or does not exist").format(price_list)) |
| 645 | |
| 646 | return result.price_not_uom_dependant |
| 647 | |
| 648 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 649 | def get_price_list_currency_and_exchange_rate(args): |
Anand Doshi | 70f57eb | 2015-11-27 14:37:40 +0530 | [diff] [blame] | 650 | if not args.price_list: |
| 651 | return {} |
| 652 | |
Shreya | 3f77852 | 2018-05-15 16:59:20 +0530 | [diff] [blame] | 653 | if args.doctype in ['Quotation', 'Sales Order', 'Delivery Note', 'Sales Invoice']: |
| 654 | args.update({"exchange_rate": "for_selling"}) |
| 655 | elif args.doctype in ['Purchase Order', 'Purchase Receipt', 'Purchase Invoice']: |
| 656 | args.update({"exchange_rate": "for_buying"}) |
| 657 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 658 | price_list_currency = get_price_list_currency(args.price_list) |
Shreya Shah | b13a54a | 2017-12-06 19:17:04 +0530 | [diff] [blame] | 659 | price_list_uom_dependant = get_price_list_uom_dependant(args.price_list) |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 660 | plc_conversion_rate = args.plc_conversion_rate |
tundebabzy | 6e90f49 | 2018-02-12 10:48:57 +0100 | [diff] [blame] | 661 | company_currency = get_company_currency(args.company) |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 662 | |
Nabin Hait | dd82bf2 | 2015-05-11 16:49:17 +0530 | [diff] [blame] | 663 | if (not plc_conversion_rate) or (price_list_currency and args.price_list_currency \ |
| 664 | and price_list_currency != args.price_list_currency): |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 665 | # cksgb 19/09/2016: added args.transaction_date as posting_date argument for get_exchange_rate |
tundebabzy | 6e90f49 | 2018-02-12 10:48:57 +0100 | [diff] [blame] | 666 | plc_conversion_rate = get_exchange_rate(price_list_currency, company_currency, |
Shreya | 3f77852 | 2018-05-15 16:59:20 +0530 | [diff] [blame] | 667 | args.transaction_date, args.exchange_rate) or plc_conversion_rate |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 668 | |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 669 | return frappe._dict({ |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 670 | "price_list_currency": price_list_currency, |
Shreya Shah | b13a54a | 2017-12-06 19:17:04 +0530 | [diff] [blame] | 671 | "price_list_uom_dependant": price_list_uom_dependant, |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 672 | "plc_conversion_rate": plc_conversion_rate |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 673 | }) |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 674 | |
| 675 | @frappe.whitelist() |
| 676 | def get_default_bom(item_code=None): |
| 677 | if item_code: |
| 678 | bom = frappe.db.get_value("BOM", {"docstatus": 1, "is_default": 1, "is_active": 1, "item": item_code}) |
| 679 | if bom: |
| 680 | return bom |
Rushabh Mehta | 0394aec | 2016-04-22 17:22:22 +0530 | [diff] [blame] | 681 | |
Manas Solanki | 087a225 | 2018-05-04 16:02:38 +0530 | [diff] [blame] | 682 | def get_valuation_rate(item_code, company, warehouse=None): |
Manas Solanki | b16a4ec | 2018-05-04 16:49:33 +0530 | [diff] [blame] | 683 | item = get_item_defaults(item_code, company) |
Manas Solanki | 087a225 | 2018-05-04 16:02:38 +0530 | [diff] [blame] | 684 | # item = frappe.get_doc("Item", item_code) |
Manas Solanki | e3910fb | 2018-05-16 15:50:17 +0530 | [diff] [blame] | 685 | if item.get("is_stock_item"): |
Saurabh | bd01a81 | 2016-03-07 14:32:23 +0530 | [diff] [blame] | 686 | if not warehouse: |
Manas Solanki | e3910fb | 2018-05-16 15:50:17 +0530 | [diff] [blame] | 687 | warehouse = item.get("default_warehouse") |
Rushabh Mehta | 0394aec | 2016-04-22 17:22:22 +0530 | [diff] [blame] | 688 | |
| 689 | return frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse}, |
Saurabh | bd01a81 | 2016-03-07 14:32:23 +0530 | [diff] [blame] | 690 | ["valuation_rate"], as_dict=True) or {"valuation_rate": 0} |
Rushabh Mehta | 0394aec | 2016-04-22 17:22:22 +0530 | [diff] [blame] | 691 | |
Manas Solanki | e3910fb | 2018-05-16 15:50:17 +0530 | [diff] [blame] | 692 | elif not item.get("is_stock_item"): |
Nabin Hait | fe876c0 | 2017-03-06 16:32:57 +0530 | [diff] [blame] | 693 | 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] | 694 | from `tabPurchase Invoice Item` |
Saurabh | bd01a81 | 2016-03-07 14:32:23 +0530 | [diff] [blame] | 695 | where item_code = %s and docstatus=1""", item_code) |
Rushabh Mehta | 0394aec | 2016-04-22 17:22:22 +0530 | [diff] [blame] | 696 | |
Saurabh | bd01a81 | 2016-03-07 14:32:23 +0530 | [diff] [blame] | 697 | if valuation_rate: |
| 698 | return {"valuation_rate": valuation_rate[0][0] or 0.0} |
| 699 | else: |
| 700 | return {"valuation_rate": 0.0} |
Rushabh Mehta | 0394aec | 2016-04-22 17:22:22 +0530 | [diff] [blame] | 701 | |
Saurabh | 2d7af63 | 2016-02-26 11:09:20 +0530 | [diff] [blame] | 702 | def get_gross_profit(out): |
Saurabh | 5ada14b | 2016-02-26 18:02:55 +0530 | [diff] [blame] | 703 | if out.valuation_rate: |
| 704 | out.update({ |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 705 | "gross_profit": ((out.base_rate - out.valuation_rate) * out.stock_qty) |
Saurabh | 5ada14b | 2016-02-26 18:02:55 +0530 | [diff] [blame] | 706 | }) |
Rushabh Mehta | 0394aec | 2016-04-22 17:22:22 +0530 | [diff] [blame] | 707 | |
Saurabh | d313553 | 2016-02-25 18:59:20 +0530 | [diff] [blame] | 708 | return out |
mbauskar | 6f60339 | 2016-01-21 16:10:22 +0530 | [diff] [blame] | 709 | |
Kanchan Chauhan | af0d637 | 2016-11-13 21:19:09 +0530 | [diff] [blame] | 710 | @frappe.whitelist() |
Rohit Waghchaure | ba3f0e6 | 2017-08-28 17:19:28 +0530 | [diff] [blame] | 711 | def get_serial_no(args, serial_nos=None): |
| 712 | serial_no = None |
Achilles Rasquinha | 56b2e12 | 2018-02-13 14:42:40 +0530 | [diff] [blame] | 713 | if isinstance(args, string_types): |
Kanchan Chauhan | af0d637 | 2016-11-13 21:19:09 +0530 | [diff] [blame] | 714 | args = json.loads(args) |
| 715 | args = frappe._dict(args) |
| 716 | |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 717 | if args.get('doctype') == 'Sales Invoice' and not args.get('update_stock'): |
| 718 | return "" |
| 719 | |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 720 | if args.get('warehouse') and args.get('stock_qty') and args.get('item_code'): |
Shreya | a20157a | 2018-04-13 12:03:42 +0530 | [diff] [blame] | 721 | has_serial_no = frappe.get_value('Item', {'item_code': args.item_code}, "has_serial_no") |
Shreya | a20157a | 2018-04-13 12:03:42 +0530 | [diff] [blame] | 722 | if args.get('batch_no') and has_serial_no == 1: |
| 723 | return get_serial_no_batchwise(args) |
Shreya | a20157a | 2018-04-13 12:03:42 +0530 | [diff] [blame] | 724 | elif has_serial_no == 1: |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 725 | 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] | 726 | args = process_args(args) |
| 727 | serial_no = get_serial_nos_by_fifo(args) |
Rohit Waghchaure | ba3f0e6 | 2017-08-28 17:19:28 +0530 | [diff] [blame] | 728 | |
| 729 | if not serial_no and serial_nos: |
| 730 | # For POS |
| 731 | serial_no = serial_nos |
| 732 | |
| 733 | return serial_no |