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 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 4 | |
| 5 | import json |
| 6 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 7 | import frappe |
| 8 | from frappe import _, throw |
Rushabh Mehta | 66e08e3 | 2014-09-29 12:17:03 +0530 | [diff] [blame] | 9 | from frappe.model.meta import get_field_precision |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 10 | from frappe.utils import add_days, add_months, cint, cstr, flt, getdate |
Nabin Hait | 98c2a05 | 2014-05-05 18:41:41 +0530 | [diff] [blame] | 11 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 12 | from erpnext import get_company_currency |
| 13 | from erpnext.accounts.doctype.pricing_rule.pricing_rule import ( |
| 14 | get_pricing_rule_for_item, |
| 15 | set_transaction_type, |
| 16 | ) |
| 17 | from erpnext.setup.doctype.brand.brand import get_brand_defaults |
| 18 | from erpnext.setup.doctype.item_group.item_group import get_item_group_defaults |
| 19 | from erpnext.setup.utils import get_exchange_rate |
| 20 | from erpnext.stock.doctype.batch.batch import get_batch_no |
| 21 | from erpnext.stock.doctype.item.item import get_item_defaults, get_uom_conv_factor |
| 22 | from erpnext.stock.doctype.item_manufacturer.item_manufacturer import get_item_manufacturer_part_no |
| 23 | from erpnext.stock.doctype.price_list.price_list import get_price_list_details |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 24 | |
Saqib | ac9e6ff | 2021-01-28 17:58:55 +0530 | [diff] [blame] | 25 | sales_doctypes = ['Quotation', 'Sales Order', 'Delivery Note', 'Sales Invoice', 'POS Invoice'] |
Nabin Hait | 0d1ccc3 | 2019-02-14 18:30:10 +0530 | [diff] [blame] | 26 | purchase_doctypes = ['Material Request', 'Supplier Quotation', 'Purchase Order', 'Purchase Receipt', 'Purchase Invoice'] |
| 27 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 28 | @frappe.whitelist() |
rohitwaghchaure | a85ddf2 | 2019-11-19 18:47:48 +0530 | [diff] [blame] | 29 | def get_item_details(args, doc=None, for_validate=False, overwrite_warehouse=True): |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 30 | """ |
| 31 | args = { |
| 32 | "item_code": "", |
| 33 | "warehouse": None, |
| 34 | "customer": "", |
| 35 | "conversion_rate": 1.0, |
| 36 | "selling_price_list": None, |
| 37 | "price_list_currency": None, |
Nabin Hait | 615d342 | 2014-02-25 19:01:20 +0530 | [diff] [blame] | 38 | "plc_conversion_rate": 1.0, |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 39 | "doctype": "", |
| 40 | "name": "", |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 41 | "supplier": None, |
| 42 | "transaction_date": None, |
| 43 | "conversion_rate": 1.0, |
| 44 | "buying_price_list": None, |
| 45 | "is_subcontracted": "Yes" / "No", |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 46 | "ignore_pricing_rule": 0/1 |
Neil Trini Lasrado | 6e343e2 | 2016-03-09 17:02:59 +0530 | [diff] [blame] | 47 | "project": "" |
Saif Ur Rehman | 2689dea | 2019-01-09 13:43:06 +0500 | [diff] [blame] | 48 | "set_warehouse": "" |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 49 | } |
| 50 | """ |
Suraj Shetty | 182f4de | 2019-08-16 08:16:22 +0530 | [diff] [blame] | 51 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 52 | args = process_args(args) |
marination | 392f323 | 2020-07-14 17:03:17 +0530 | [diff] [blame] | 53 | for_validate = process_string_args(for_validate) |
| 54 | overwrite_warehouse = process_string_args(overwrite_warehouse) |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 55 | item = frappe.get_cached_doc("Item", args.item_code) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 56 | validate_item_details(args, item) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 57 | |
Suraj Shetty | 182f4de | 2019-08-16 08:16:22 +0530 | [diff] [blame] | 58 | out = get_basic_details(args, item, overwrite_warehouse) |
| 59 | |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 60 | if isinstance(doc, str): |
Deepesh Garg | ef0d26c | 2020-01-06 15:34:15 +0530 | [diff] [blame] | 61 | doc = json.loads(doc) |
| 62 | |
| 63 | if doc and doc.get('doctype') == 'Purchase Invoice': |
| 64 | args['bill_date'] = doc.get('bill_date') |
| 65 | |
| 66 | if doc: |
| 67 | args['posting_date'] = doc.get('posting_date') |
| 68 | args['transaction_date'] = doc.get('transaction_date') |
| 69 | |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 70 | get_item_tax_template(args, item, out) |
Saif Ur Rehman | 648bd15 | 2019-01-03 03:48:01 +0500 | [diff] [blame] | 71 | out["item_tax_rate"] = get_item_tax_map(args.company, args.get("item_tax_template") if out.get("item_tax_template") is None \ |
| 72 | else out.get("item_tax_template"), as_json=True) |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 73 | |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 74 | get_party_item_code(args, item, out) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 75 | |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 76 | set_valuation_rate(out, args) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 77 | |
Manas Solanki | e5e87f7 | 2018-05-28 20:07:08 +0530 | [diff] [blame] | 78 | update_party_blanket_order(args, out) |
| 79 | |
Ankush | 16d4de5 | 2021-08-09 10:41:24 +0530 | [diff] [blame] | 80 | out.update(get_price_list_rate(args, item)) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 81 | |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 82 | if args.customer and cint(args.is_pos): |
Anuja Pawar | f132ed4 | 2021-05-07 12:11:09 +0530 | [diff] [blame] | 83 | out.update(get_pos_profile_item_details(args.company, args, update_data=True)) |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 84 | |
Nabin Hait | da17f9c | 2020-05-01 18:16:25 +0530 | [diff] [blame] | 85 | if (args.get("doctype") == "Material Request" and |
| 86 | args.get("material_request_type") == "Material Transfer"): |
| 87 | out.update(get_bin_details(args.item_code, args.get("from_warehouse"))) |
| 88 | |
| 89 | elif out.get("warehouse"): |
Saqib Ansari | eb3aae8 | 2021-10-12 13:29:32 +0530 | [diff] [blame] | 90 | if doc and doc.get('doctype') == 'Purchase Order': |
| 91 | # calculate company_total_stock only for po |
| 92 | bin_details = get_bin_details(args.item_code, out.warehouse, args.company) |
| 93 | else: |
| 94 | bin_details = get_bin_details(args.item_code, out.warehouse) |
| 95 | |
| 96 | out.update(bin_details) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 97 | |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 98 | # update args with out, if key or value not exists |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 99 | for key, value in out.items(): |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 100 | if args.get(key) is None: |
| 101 | args[key] = value |
| 102 | |
rohitwaghchaure | a85ddf2 | 2019-11-19 18:47:48 +0530 | [diff] [blame] | 103 | data = get_pricing_rule_for_item(args, out.price_list_rate, |
| 104 | doc, for_validate=for_validate) |
| 105 | |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 106 | out.update(data) |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 107 | |
| 108 | update_stock(args, out) |
| 109 | |
| 110 | if args.transaction_date and item.lead_time_days: |
| 111 | out.schedule_date = out.lead_time_date = add_days(args.transaction_date, |
| 112 | item.lead_time_days) |
| 113 | |
| 114 | if args.get("is_subcontracted") == "Yes": |
| 115 | out.bom = args.get('bom') or get_default_bom(args.item_code) |
| 116 | |
| 117 | get_gross_profit(out) |
| 118 | if args.doctype == 'Material Request': |
| 119 | out.rate = args.rate or out.price_list_rate |
Rohit Waghchaure | 1bc53a3 | 2021-03-31 15:28:26 +0530 | [diff] [blame] | 120 | out.amount = flt(args.qty) * flt(out.rate) |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 121 | |
| 122 | return out |
| 123 | |
| 124 | def update_stock(args, out): |
pratu16x7 | 577e4c4 | 2017-06-23 11:39:03 +0530 | [diff] [blame] | 125 | if (args.get("doctype") == "Delivery Note" or |
Nabin Hait | 3ce41d6 | 2017-05-03 18:22:24 +0530 | [diff] [blame] | 126 | (args.get("doctype") == "Sales Invoice" and args.get('update_stock'))) \ |
| 127 | and out.warehouse and out.stock_qty > 0: |
pratu16x7 | 577e4c4 | 2017-06-23 11:39:03 +0530 | [diff] [blame] | 128 | |
Nabin Hait | 8fac2ad | 2017-05-18 11:54:24 +0530 | [diff] [blame] | 129 | if out.has_batch_no and not args.get("batch_no"): |
Rushabh Mehta | 551406a | 2017-04-21 12:40:19 +0530 | [diff] [blame] | 130 | out.batch_no = get_batch_no(out.item_code, out.warehouse, out.qty) |
rohitwaghchaure | 2fb8cc5 | 2017-11-29 13:50:04 +0530 | [diff] [blame] | 131 | actual_batch_qty = get_batch_qty(out.batch_no, out.warehouse, out.item_code) |
| 132 | if actual_batch_qty: |
| 133 | out.update(actual_batch_qty) |
Shreya | 14bd43d | 2018-04-27 16:24:05 +0530 | [diff] [blame] | 134 | |
| 135 | if out.has_serial_no and args.get('batch_no'): |
Ranjith Kurungadam | d54991d | 2018-08-01 17:47:07 +0530 | [diff] [blame] | 136 | reserved_so = get_so_reservation_for_item(args) |
Shreya | 14bd43d | 2018-04-27 16:24:05 +0530 | [diff] [blame] | 137 | out.batch_no = args.get('batch_no') |
Ranjith Kurungadam | d54991d | 2018-08-01 17:47:07 +0530 | [diff] [blame] | 138 | out.serial_no = get_serial_no(out, args.serial_no, sales_order=reserved_so) |
Shreya | 14bd43d | 2018-04-27 16:24:05 +0530 | [diff] [blame] | 139 | |
| 140 | elif out.has_serial_no: |
Ranjith Kurungadam | d54991d | 2018-08-01 17:47:07 +0530 | [diff] [blame] | 141 | reserved_so = get_so_reservation_for_item(args) |
| 142 | out.serial_no = get_serial_no(out, args.serial_no, sales_order=reserved_so) |
Rushabh Mehta | 551406a | 2017-04-21 12:40:19 +0530 | [diff] [blame] | 143 | |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 144 | |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 145 | def set_valuation_rate(out, args): |
Nabin Hait | 50238c3 | 2018-08-08 18:43:04 +0530 | [diff] [blame] | 146 | if frappe.db.exists("Product Bundle", args.item_code, cache=True): |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 147 | valuation_rate = 0.0 |
| 148 | bundled_items = frappe.get_doc("Product Bundle", args.item_code) |
Rushabh Mehta | 0394aec | 2016-04-22 17:22:22 +0530 | [diff] [blame] | 149 | |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 150 | for bundle_item in bundled_items.items: |
| 151 | valuation_rate += \ |
| 152 | flt(get_valuation_rate(bundle_item.item_code, args.company, out.get("warehouse")).get("valuation_rate") \ |
| 153 | * bundle_item.qty) |
Anand Doshi | 70f57eb | 2015-11-27 14:37:40 +0530 | [diff] [blame] | 154 | |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 155 | out.update({ |
| 156 | "valuation_rate": valuation_rate |
| 157 | }) |
| 158 | |
| 159 | else: |
| 160 | out.update(get_valuation_rate(args.item_code, args.company, out.get("warehouse"))) |
| 161 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 162 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 163 | def process_args(args): |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 164 | if isinstance(args, str): |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 165 | args = json.loads(args) |
| 166 | |
| 167 | args = frappe._dict(args) |
| 168 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 169 | if not args.get("price_list"): |
| 170 | args.price_list = args.get("selling_price_list") or args.get("buying_price_list") |
| 171 | |
| 172 | if args.barcode: |
| 173 | args.item_code = get_item_code(barcode=args.barcode) |
| 174 | elif not args.item_code and args.serial_no: |
| 175 | args.item_code = get_item_code(serial_no=args.serial_no) |
Rushabh Mehta | 0394aec | 2016-04-22 17:22:22 +0530 | [diff] [blame] | 176 | |
mbauskar | 6f60339 | 2016-01-21 16:10:22 +0530 | [diff] [blame] | 177 | set_transaction_type(args) |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 178 | return args |
| 179 | |
marination | 392f323 | 2020-07-14 17:03:17 +0530 | [diff] [blame] | 180 | def process_string_args(args): |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 181 | if isinstance(args, str): |
marination | 392f323 | 2020-07-14 17:03:17 +0530 | [diff] [blame] | 182 | args = json.loads(args) |
| 183 | return args |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 184 | |
Neil Trini Lasrado | 4abda67 | 2015-02-25 17:34:27 +0530 | [diff] [blame] | 185 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 186 | def get_item_code(barcode=None, serial_no=None): |
| 187 | if barcode: |
Giovanni | 2144e02 | 2017-12-10 17:27:09 +0100 | [diff] [blame] | 188 | item_code = frappe.db.get_value("Item Barcode", {"barcode": barcode}, fieldname=["parent"]) |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 189 | if not item_code: |
| 190 | frappe.throw(_("No Item with Barcode {0}").format(barcode)) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 191 | elif serial_no: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 192 | item_code = frappe.db.get_value("Serial No", serial_no, "item_code") |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 193 | if not item_code: |
| 194 | frappe.throw(_("No Item with Serial No {0}").format(serial_no)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 195 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 196 | return item_code |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 197 | |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 198 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 199 | def validate_item_details(args, item): |
| 200 | if not args.company: |
| 201 | throw(_("Please specify Company")) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 202 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 203 | from erpnext.stock.doctype.item.item import validate_end_of_life |
Anand Doshi | 21e09a2 | 2015-10-29 12:21:41 +0530 | [diff] [blame] | 204 | validate_end_of_life(item.name, item.end_of_life, item.disabled) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 205 | |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 206 | if args.transaction_type == "selling" and cint(item.has_variants): |
Umair Sayyed | 72534de | 2016-04-15 12:52:12 +0530 | [diff] [blame] | 207 | 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] | 208 | |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 209 | elif args.transaction_type == "buying" and args.doctype != "Material Request": |
Rushabh Mehta | 1e8025b | 2015-07-24 15:16:25 +0530 | [diff] [blame] | 210 | 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] | 211 | throw(_("Item {0} must be a Sub-contracted Item").format(item.name)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 212 | |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 213 | |
Suraj Shetty | 182f4de | 2019-08-16 08:16:22 +0530 | [diff] [blame] | 214 | def get_basic_details(args, item, overwrite_warehouse=True): |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 215 | """ |
| 216 | :param args: { |
| 217 | "item_code": "", |
| 218 | "warehouse": None, |
| 219 | "customer": "", |
| 220 | "conversion_rate": 1.0, |
| 221 | "selling_price_list": None, |
| 222 | "price_list_currency": None, |
Shreya Shah | b13a54a | 2017-12-06 19:17:04 +0530 | [diff] [blame] | 223 | "price_list_uom_dependant": None, |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 224 | "plc_conversion_rate": 1.0, |
| 225 | "doctype": "", |
| 226 | "name": "", |
| 227 | "supplier": None, |
| 228 | "transaction_date": None, |
| 229 | "conversion_rate": 1.0, |
| 230 | "buying_price_list": None, |
| 231 | "is_subcontracted": "Yes" / "No", |
| 232 | "ignore_pricing_rule": 0/1 |
| 233 | "project": "", |
| 234 | barcode: "", |
| 235 | serial_no: "", |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 236 | currency: "", |
| 237 | update_stock: "", |
| 238 | price_list: "", |
| 239 | company: "", |
| 240 | order_type: "", |
| 241 | is_pos: "", |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 242 | project: "", |
| 243 | qty: "", |
| 244 | stock_qty: "", |
ronelvcabrera | 370cdc0 | 2019-12-04 18:37:11 +0800 | [diff] [blame] | 245 | conversion_factor: "", |
| 246 | against_blanket_order: 0/1 |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 247 | } |
| 248 | :param item: `item_code` of Item object |
| 249 | :return: frappe._dict |
| 250 | """ |
| 251 | |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 252 | if not item: |
| 253 | item = frappe.get_doc("Item", args.get("item_code")) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 254 | |
Rushabh Mehta | 724f9e5 | 2014-10-07 15:29:58 +0530 | [diff] [blame] | 255 | if item.variant_of: |
| 256 | item.update_template_tables() |
| 257 | |
Manas Solanki | b16a4ec | 2018-05-04 16:49:33 +0530 | [diff] [blame] | 258 | item_defaults = get_item_defaults(item.name, args.company) |
Shreya Shah | 3c9839f | 2018-07-17 18:01:44 +0530 | [diff] [blame] | 259 | item_group_defaults = get_item_group_defaults(item.name, args.company) |
Saif Ur Rehman | c819ea3 | 2018-09-25 14:53:12 +0500 | [diff] [blame] | 260 | brand_defaults = get_brand_defaults(item.name, args.company) |
Shreya Shah | 3c9839f | 2018-07-17 18:01:44 +0530 | [diff] [blame] | 261 | |
Saqib | 438e043 | 2020-04-03 10:02:56 +0530 | [diff] [blame] | 262 | defaults = frappe._dict({ |
| 263 | 'item_defaults': item_defaults, |
| 264 | 'item_group_defaults': item_group_defaults, |
| 265 | 'brand_defaults': brand_defaults |
| 266 | }) |
marination | e05013e | 2020-04-06 22:13:13 +0530 | [diff] [blame] | 267 | |
Saqib | 438e043 | 2020-04-03 10:02:56 +0530 | [diff] [blame] | 268 | warehouse = get_item_warehouse(item, args, overwrite_warehouse, defaults) |
Nabin Hait | 36028bf | 2014-02-12 19:09:28 +0530 | [diff] [blame] | 269 | |
Rohit Waghchaure | 6cdaa6e | 2018-05-18 16:30:14 +0530 | [diff] [blame] | 270 | if args.get('doctype') == "Material Request" and not args.get('material_request_type'): |
| 271 | args['material_request_type'] = frappe.db.get_value('Material Request', |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 272 | args.get('name'), 'material_request_type', cache=True) |
rohitwaghchaure | e8ccc0e | 2017-11-21 16:17:22 +0530 | [diff] [blame] | 273 | |
Anurag Mishra | f3b393f | 2019-11-28 18:52:16 +0530 | [diff] [blame] | 274 | expense_account = None |
| 275 | |
| 276 | if args.get('doctype') == 'Purchase Invoice' and item.is_fixed_asset: |
| 277 | from erpnext.assets.doctype.asset_category.asset_category import get_asset_category_account |
| 278 | expense_account = get_asset_category_account(fieldname = "fixed_asset_account", item = args.item_code, company= args.company) |
| 279 | |
Nabin Hait | 79a1d2a | 2017-09-29 18:14:10 +0530 | [diff] [blame] | 280 | #Set the UOM to the Default Sales UOM or Default Purchase UOM if configured in the Item Master |
Saqib | 438e043 | 2020-04-03 10:02:56 +0530 | [diff] [blame] | 281 | if not args.get('uom'): |
Nabin Hait | 0d1ccc3 | 2019-02-14 18:30:10 +0530 | [diff] [blame] | 282 | if args.get('doctype') in sales_doctypes: |
Nabin Hait | 79a1d2a | 2017-09-29 18:14:10 +0530 | [diff] [blame] | 283 | args.uom = item.sales_uom if item.sales_uom else item.stock_uom |
rohitwaghchaure | e8ccc0e | 2017-11-21 16:17:22 +0530 | [diff] [blame] | 284 | elif (args.get('doctype') in ['Purchase Order', 'Purchase Receipt', 'Purchase Invoice']) or \ |
Rohit Waghchaure | 6cdaa6e | 2018-05-18 16:30:14 +0530 | [diff] [blame] | 285 | (args.get('doctype') == 'Material Request' and args.get('material_request_type') == 'Purchase'): |
Nabin Hait | 79a1d2a | 2017-09-29 18:14:10 +0530 | [diff] [blame] | 286 | args.uom = item.purchase_uom if item.purchase_uom else item.stock_uom |
| 287 | else: |
| 288 | args.uom = item.stock_uom |
| 289 | |
Rohit Waghchaure | e1f0704 | 2021-08-23 14:27:55 +0530 | [diff] [blame] | 290 | if (args.get("batch_no") and |
| 291 | item.name != frappe.get_cached_value('Batch', args.get("batch_no"), 'item')): |
| 292 | args['batch_no'] = '' |
| 293 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 294 | out = frappe._dict({ |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 295 | "item_code": item.name, |
| 296 | "item_name": item.item_name, |
Neil Trini Lasrado | c48f06c | 2015-02-10 16:49:16 +0530 | [diff] [blame] | 297 | "description": cstr(item.description).strip(), |
| 298 | "image": cstr(item.image).strip(), |
Saurabh | d313553 | 2016-02-25 18:59:20 +0530 | [diff] [blame] | 299 | "warehouse": warehouse, |
Saif Ur Rehman | c819ea3 | 2018-09-25 14:53:12 +0500 | [diff] [blame] | 300 | "income_account": get_default_income_account(args, item_defaults, item_group_defaults, brand_defaults), |
Anurag Mishra | f3b393f | 2019-11-28 18:52:16 +0530 | [diff] [blame] | 301 | "expense_account": expense_account or get_default_expense_account(args, item_defaults, item_group_defaults, brand_defaults) , |
Saqib | c0cc72e | 2021-11-29 15:05:06 +0530 | [diff] [blame] | 302 | "discount_account": get_default_discount_account(args, item_defaults), |
Saif Ur Rehman | c819ea3 | 2018-09-25 14:53:12 +0500 | [diff] [blame] | 303 | "cost_center": get_default_cost_center(args, item_defaults, item_group_defaults, brand_defaults), |
Rushabh Mehta | 551406a | 2017-04-21 12:40:19 +0530 | [diff] [blame] | 304 | 'has_serial_no': item.has_serial_no, |
| 305 | 'has_batch_no': item.has_batch_no, |
marination | e05013e | 2020-04-06 22:13:13 +0530 | [diff] [blame] | 306 | "batch_no": args.get("batch_no"), |
Nabin Hait | 79a1d2a | 2017-09-29 18:14:10 +0530 | [diff] [blame] | 307 | "uom": args.uom, |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 308 | "min_order_qty": flt(item.min_order_qty) if args.doctype == "Material Request" else "", |
Fisher Yu | 2cd49f5 | 2019-01-11 18:01:33 +0800 | [diff] [blame] | 309 | "qty": flt(args.qty) or 1.0, |
| 310 | "stock_qty": flt(args.qty) or 1.0, |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 311 | "price_list_rate": 0.0, |
| 312 | "base_price_list_rate": 0.0, |
| 313 | "rate": 0.0, |
| 314 | "base_rate": 0.0, |
| 315 | "amount": 0.0, |
| 316 | "base_amount": 0.0, |
Nabin Hait | 82e3e25 | 2015-02-23 16:58:30 +0530 | [diff] [blame] | 317 | "net_rate": 0.0, |
| 318 | "net_amount": 0.0, |
Saurabh | c6dbe70 | 2015-10-19 14:17:52 +0530 | [diff] [blame] | 319 | "discount_percentage": 0.0, |
Saqib | c0cc72e | 2021-11-29 15:05:06 +0530 | [diff] [blame] | 320 | "discount_amount": 0.0, |
Saif Ur Rehman | c819ea3 | 2018-09-25 14:53:12 +0500 | [diff] [blame] | 321 | "supplier": get_default_supplier(args, item_defaults, item_group_defaults, brand_defaults), |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 322 | "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] | 323 | "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] | 324 | "is_fixed_asset": item.is_fixed_asset, |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 325 | "last_purchase_rate": item.last_purchase_rate if args.get("doctype") in ["Purchase Order"] else 0, |
ronelvcabrera | 370cdc0 | 2019-12-04 18:37:11 +0800 | [diff] [blame] | 326 | "transaction_date": args.get("transaction_date"), |
rohitwaghchaure | aa85e51 | 2020-05-19 19:51:45 +0530 | [diff] [blame] | 327 | "against_blanket_order": args.get("against_blanket_order"), |
Rohit Waghchaure | bde159a | 2021-03-22 23:36:48 +0530 | [diff] [blame] | 328 | "bom_no": item.get("default_bom"), |
rohitwaghchaure | 7b4a654 | 2021-09-03 22:00:14 +0530 | [diff] [blame] | 329 | "weight_per_unit": args.get("weight_per_unit") or item.get("weight_per_unit"), |
Raffael Meyer | e10ab16 | 2021-11-30 13:24:18 +0100 | [diff] [blame] | 330 | "weight_uom": args.get("weight_uom") or item.get("weight_uom"), |
| 331 | "grant_commission": item.get("grant_commission") |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 332 | }) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 333 | |
Zlash65 | 627be1d | 2019-01-15 14:16:32 +0530 | [diff] [blame] | 334 | if item.get("enable_deferred_revenue") or item.get("enable_deferred_expense"): |
Zlash65 | 80f5cb0 | 2018-08-29 11:39:08 +0530 | [diff] [blame] | 335 | out.update(calculate_service_end_date(args, item)) |
Manas Solanki | 0393848 | 2018-05-14 16:16:46 +0530 | [diff] [blame] | 336 | |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 337 | # calculate conversion factor |
mbauskar | 287fe81 | 2017-04-10 19:15:57 +0530 | [diff] [blame] | 338 | if item.stock_uom == args.uom: |
| 339 | out.conversion_factor = 1.0 |
| 340 | else: |
| 341 | out.conversion_factor = args.conversion_factor or \ |
Nabin Hait | 0d1ccc3 | 2019-02-14 18:30:10 +0530 | [diff] [blame] | 342 | get_conversion_factor(item.name, args.uom).get("conversion_factor") |
mbauskar | 287fe81 | 2017-04-10 19:15:57 +0530 | [diff] [blame] | 343 | |
| 344 | args.conversion_factor = out.conversion_factor |
| 345 | out.stock_qty = out.qty * out.conversion_factor |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 346 | |
Shreya Shah | 44fa9a6 | 2018-01-08 14:58:20 +0530 | [diff] [blame] | 347 | # calculate last purchase rate |
Nabin Hait | 0d1ccc3 | 2019-02-14 18:30:10 +0530 | [diff] [blame] | 348 | if args.get('doctype') in purchase_doctypes: |
| 349 | from erpnext.buying.doctype.purchase_order.purchase_order import item_last_purchase_rate |
| 350 | out.last_purchase_rate = item_last_purchase_rate(args.name, args.conversion_rate, item.name, out.conversion_factor) |
Shreya Shah | 44fa9a6 | 2018-01-08 14:58:20 +0530 | [diff] [blame] | 351 | |
Nabin Hait | b09ed41 | 2015-02-17 10:33:58 +0530 | [diff] [blame] | 352 | # if default specified in item is for another company, fetch from company |
Rushabh Mehta | e385b5b | 2017-04-20 15:21:01 +0530 | [diff] [blame] | 353 | for d in [ |
| 354 | ["Account", "income_account", "default_income_account"], |
Rushabh Mehta | 0394aec | 2016-04-22 17:22:22 +0530 | [diff] [blame] | 355 | ["Account", "expense_account", "default_expense_account"], |
Rushabh Mehta | e385b5b | 2017-04-20 15:21:01 +0530 | [diff] [blame] | 356 | ["Cost Center", "cost_center", "cost_center"], |
| 357 | ["Warehouse", "warehouse", ""]]: |
Nabin Hait | 33df0b4 | 2018-05-26 09:09:02 +0530 | [diff] [blame] | 358 | if not out[d[1]]: |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 359 | out[d[1]] = frappe.get_cached_value('Company', args.company, d[2]) if d[2] else None |
Nabin Hait | b09ed41 | 2015-02-17 10:33:58 +0530 | [diff] [blame] | 360 | |
Giovanni | 2144e02 | 2017-12-10 17:27:09 +0100 | [diff] [blame] | 361 | for fieldname in ("item_name", "item_group", "barcodes", "brand", "stock_uom"): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 362 | out[fieldname] = item.get(fieldname) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 363 | |
Rohit Waghchaure | 3cf2436 | 2019-06-02 16:03:05 +0530 | [diff] [blame] | 364 | if args.get("manufacturer"): |
| 365 | part_no = get_item_manufacturer_part_no(args.get("item_code"), args.get("manufacturer")) |
| 366 | if part_no: |
| 367 | out["manufacturer_part_no"] = part_no |
| 368 | else: |
| 369 | out["manufacturer_part_no"] = None |
| 370 | out["manufacturer"] = None |
marination | 3e7a8ab | 2020-04-07 21:00:57 +0530 | [diff] [blame] | 371 | else: |
rohitwaghchaure | be16e5c | 2020-05-01 10:50:17 +0530 | [diff] [blame] | 372 | data = frappe.get_value("Item", item.name, |
| 373 | ["default_item_manufacturer", "default_manufacturer_part_no"] , as_dict=1) |
| 374 | |
| 375 | if data: |
| 376 | out.update({ |
| 377 | "manufacturer": data.default_item_manufacturer, |
| 378 | "manufacturer_part_no": data.default_manufacturer_part_no |
| 379 | }) |
Rohit Waghchaure | 3cf2436 | 2019-06-02 16:03:05 +0530 | [diff] [blame] | 380 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 381 | child_doctype = args.doctype + ' Item' |
| 382 | meta = frappe.get_meta(child_doctype) |
| 383 | if meta.get_field("barcode"): |
| 384 | update_barcode_value(out) |
| 385 | |
Rohit Waghchaure | bde159a | 2021-03-22 23:36:48 +0530 | [diff] [blame] | 386 | if out.get("weight_per_unit"): |
| 387 | out['total_weight'] = out.weight_per_unit * out.stock_qty |
| 388 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 389 | return out |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 390 | |
Chillar Anand | 772d475 | 2021-10-06 22:28:48 +0530 | [diff] [blame] | 391 | def get_item_warehouse(item, args, overwrite_warehouse, defaults=None): |
Saqib | 438e043 | 2020-04-03 10:02:56 +0530 | [diff] [blame] | 392 | if not defaults: |
| 393 | defaults = frappe._dict({ |
| 394 | 'item_defaults' : get_item_defaults(item.name, args.company), |
| 395 | 'item_group_defaults' : get_item_group_defaults(item.name, args.company), |
| 396 | 'brand_defaults' : get_brand_defaults(item.name, args.company) |
| 397 | }) |
| 398 | |
| 399 | if overwrite_warehouse or not args.warehouse: |
| 400 | warehouse = ( |
| 401 | args.get("set_warehouse") or |
| 402 | defaults.item_defaults.get("default_warehouse") or |
| 403 | defaults.item_group_defaults.get("default_warehouse") or |
| 404 | defaults.brand_defaults.get("default_warehouse") or |
| 405 | args.get('warehouse') |
| 406 | ) |
| 407 | |
| 408 | if not warehouse: |
| 409 | defaults = frappe.defaults.get_defaults() or {} |
| 410 | warehouse_exists = frappe.db.exists("Warehouse", { |
| 411 | 'name': defaults.default_warehouse, |
| 412 | 'company': args.company |
| 413 | }) |
| 414 | if defaults.get("default_warehouse") and warehouse_exists: |
| 415 | warehouse = defaults.default_warehouse |
| 416 | |
| 417 | else: |
| 418 | warehouse = args.get('warehouse') |
marination | e05013e | 2020-04-06 22:13:13 +0530 | [diff] [blame] | 419 | |
Marica | 3b1be2b | 2020-10-22 17:04:31 +0530 | [diff] [blame] | 420 | if not warehouse: |
| 421 | default_warehouse = frappe.db.get_single_value("Stock Settings", "default_warehouse") |
| 422 | if frappe.db.get_value("Warehouse", default_warehouse, "company") == args.company: |
| 423 | return default_warehouse |
| 424 | |
Saqib | 438e043 | 2020-04-03 10:02:56 +0530 | [diff] [blame] | 425 | return warehouse |
| 426 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 427 | def update_barcode_value(out): |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 428 | barcode_data = get_barcode_data([out]) |
| 429 | |
| 430 | # If item has one barcode then update the value of the barcode field |
| 431 | if barcode_data and len(barcode_data.get(out.item_code)) == 1: |
| 432 | out['barcode'] = barcode_data.get(out.item_code)[0] |
| 433 | |
Saqib | a6f98d4 | 2020-07-23 18:51:26 +0530 | [diff] [blame] | 434 | def get_barcode_data(items_list): |
| 435 | # get itemwise batch no data |
| 436 | # exmaple: {'LED-GRE': [Batch001, Batch002]} |
| 437 | # where LED-GRE is item code, SN0001 is serial no and Pune is warehouse |
| 438 | |
| 439 | itemwise_barcode = {} |
| 440 | for item in items_list: |
| 441 | barcodes = frappe.db.sql(""" |
| 442 | select barcode from `tabItem Barcode` where parent = %s |
| 443 | """, item.item_code, as_dict=1) |
| 444 | |
| 445 | for barcode in barcodes: |
| 446 | if item.item_code not in itemwise_barcode: |
| 447 | itemwise_barcode.setdefault(item.item_code, []) |
| 448 | itemwise_barcode[item.item_code].append(barcode.get("barcode")) |
| 449 | |
| 450 | return itemwise_barcode |
| 451 | |
Zlash65 | 80f5cb0 | 2018-08-29 11:39:08 +0530 | [diff] [blame] | 452 | @frappe.whitelist() |
Deepesh Garg | 8cdd7ce | 2021-06-24 19:17:58 +0530 | [diff] [blame] | 453 | def get_item_tax_info(company, tax_category, item_codes, item_rates=None, item_tax_templates=None): |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 454 | out = {} |
Deepesh Garg | cae42b48 | 2021-06-25 13:34:00 +0530 | [diff] [blame] | 455 | |
Deepesh Garg | 8dd99c0 | 2021-06-25 13:38:06 +0530 | [diff] [blame] | 456 | if item_tax_templates is None: |
Deepesh Garg | cae42b48 | 2021-06-25 13:34:00 +0530 | [diff] [blame] | 457 | item_tax_templates = {} |
Rohit Waghchaure | 4acbeec | 2021-07-13 11:45:41 +0530 | [diff] [blame] | 458 | |
Deepesh Garg | 8dd99c0 | 2021-06-25 13:38:06 +0530 | [diff] [blame] | 459 | if item_rates is None: |
| 460 | item_rates = {} |
Deepesh Garg | cae42b48 | 2021-06-25 13:34:00 +0530 | [diff] [blame] | 461 | |
Deepesh Garg | 8cdd7ce | 2021-06-24 19:17:58 +0530 | [diff] [blame] | 462 | if isinstance(item_codes, (str,)): |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 463 | item_codes = json.loads(item_codes) |
| 464 | |
Deepesh Garg | 8cdd7ce | 2021-06-24 19:17:58 +0530 | [diff] [blame] | 465 | if isinstance(item_rates, (str,)): |
Deepesh Garg | 8a7e283 | 2021-06-04 22:53:26 +0530 | [diff] [blame] | 466 | item_rates = json.loads(item_rates) |
| 467 | |
Deepesh Garg | 8cdd7ce | 2021-06-24 19:17:58 +0530 | [diff] [blame] | 468 | if isinstance(item_tax_templates, (str,)): |
Deepesh Garg | 94d4604 | 2021-06-23 20:56:27 +0530 | [diff] [blame] | 469 | item_tax_templates = json.loads(item_tax_templates) |
| 470 | |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 471 | for item_code in item_codes: |
Deepesh Garg | 94d4604 | 2021-06-23 20:56:27 +0530 | [diff] [blame] | 472 | if not item_code or item_code[1] in out or not item_tax_templates.get(item_code[1]): |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 473 | continue |
Deepesh Garg | 94d4604 | 2021-06-23 20:56:27 +0530 | [diff] [blame] | 474 | |
Deepesh Garg | 8a7e283 | 2021-06-04 22:53:26 +0530 | [diff] [blame] | 475 | out[item_code[1]] = {} |
| 476 | item = frappe.get_cached_doc("Item", item_code[0]) |
Deepesh Garg | 8dd99c0 | 2021-06-25 13:38:06 +0530 | [diff] [blame] | 477 | args = {"company": company, "tax_category": tax_category, "net_rate": item_rates.get(item_code[1])} |
Deepesh Garg | 269962a | 2021-06-23 22:52:51 +0530 | [diff] [blame] | 478 | |
| 479 | if item_tax_templates: |
| 480 | args.update({"item_tax_template": item_tax_templates.get(item_code[1])}) |
Deepesh Garg | 94d4604 | 2021-06-23 20:56:27 +0530 | [diff] [blame] | 481 | |
Deepesh Garg | 8a7e283 | 2021-06-04 22:53:26 +0530 | [diff] [blame] | 482 | get_item_tax_template(args, item, out[item_code[1]]) |
| 483 | out[item_code[1]]["item_tax_rate"] = get_item_tax_map(company, out[item_code[1]].get("item_tax_template"), as_json=True) |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 484 | |
| 485 | return out |
| 486 | |
| 487 | def get_item_tax_template(args, item, out): |
| 488 | """ |
| 489 | args = { |
| 490 | "tax_category": None |
| 491 | "item_tax_template": None |
| 492 | } |
| 493 | """ |
Saqib Ansari | eb3aae8 | 2021-10-12 13:29:32 +0530 | [diff] [blame] | 494 | item_tax_template = None |
| 495 | if item.taxes: |
| 496 | item_tax_template = _get_item_tax_template(args, item.taxes, out) |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 497 | |
| 498 | if not item_tax_template: |
| 499 | item_group = item.item_group |
| 500 | while item_group and not item_tax_template: |
| 501 | item_group_doc = frappe.get_cached_doc("Item Group", item_group) |
| 502 | item_tax_template = _get_item_tax_template(args, item_group_doc.taxes, out) |
| 503 | item_group = item_group_doc.parent_item_group |
| 504 | |
18alantom | 308905b | 2021-05-03 23:34:34 +0530 | [diff] [blame] | 505 | def _get_item_tax_template(args, taxes, out=None, for_validate=False): |
| 506 | if out is None: |
| 507 | out = {} |
Deepesh Garg | ef0d26c | 2020-01-06 15:34:15 +0530 | [diff] [blame] | 508 | taxes_with_validity = [] |
| 509 | taxes_with_no_validity = [] |
| 510 | |
| 511 | for tax in taxes: |
Saqib Ansari | eb3aae8 | 2021-10-12 13:29:32 +0530 | [diff] [blame] | 512 | tax_company = frappe.get_cached_value("Item Tax Template", tax.item_tax_template, 'company') |
| 513 | if tax_company == args['company']: |
| 514 | if (tax.valid_from or tax.maximum_net_rate): |
| 515 | # In purchase Invoice first preference will be given to supplier invoice date |
| 516 | # if supplier date is not present then posting date |
| 517 | validation_date = args.get('transaction_date') or args.get('bill_date') or args.get('posting_date') |
Deepesh Garg | ef0d26c | 2020-01-06 15:34:15 +0530 | [diff] [blame] | 518 | |
Saqib Ansari | eb3aae8 | 2021-10-12 13:29:32 +0530 | [diff] [blame] | 519 | if getdate(tax.valid_from) <= getdate(validation_date) \ |
| 520 | and is_within_valid_range(args, tax): |
| 521 | taxes_with_validity.append(tax) |
| 522 | else: |
mohammadahmad1990 | e39649b | 2020-06-18 11:42:53 +0500 | [diff] [blame] | 523 | taxes_with_no_validity.append(tax) |
Deepesh Garg | ef0d26c | 2020-01-06 15:34:15 +0530 | [diff] [blame] | 524 | |
| 525 | if taxes_with_validity: |
| 526 | taxes = sorted(taxes_with_validity, key = lambda i: i.valid_from, reverse=True) |
| 527 | else: |
| 528 | taxes = taxes_with_no_validity |
| 529 | |
| 530 | if for_validate: |
| 531 | return [tax.item_tax_template for tax in taxes if (cstr(tax.tax_category) == cstr(args.get('tax_category')) \ |
| 532 | and (tax.item_tax_template not in taxes))] |
| 533 | |
| 534 | # all templates have validity and no template is valid |
| 535 | if not taxes_with_validity and (not taxes_with_no_validity): |
| 536 | return None |
| 537 | |
Deepesh Garg | 8a7e283 | 2021-06-04 22:53:26 +0530 | [diff] [blame] | 538 | # do not change if already a valid template |
Deepesh Garg | 8cdd7ce | 2021-06-24 19:17:58 +0530 | [diff] [blame] | 539 | if args.get('item_tax_template') in {t.item_tax_template for t in taxes}: |
Deepesh Garg | 94d4604 | 2021-06-23 20:56:27 +0530 | [diff] [blame] | 540 | out["item_tax_template"] = args.get('item_tax_template') |
Deepesh Garg | 3646c30 | 2021-06-05 13:21:03 +0530 | [diff] [blame] | 541 | return args.get('item_tax_template') |
Deepesh Garg | 8a7e283 | 2021-06-04 22:53:26 +0530 | [diff] [blame] | 542 | |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 543 | for tax in taxes: |
| 544 | if cstr(tax.tax_category) == cstr(args.get("tax_category")): |
| 545 | out["item_tax_template"] = tax.item_tax_template |
| 546 | return tax.item_tax_template |
| 547 | return None |
| 548 | |
Deepesh Garg | 8a7e283 | 2021-06-04 22:53:26 +0530 | [diff] [blame] | 549 | def is_within_valid_range(args, tax): |
| 550 | if not flt(tax.maximum_net_rate): |
| 551 | # No range specified, just ignore |
| 552 | return True |
| 553 | elif flt(tax.minimum_net_rate) <= flt(args.get('net_rate')) <= flt(tax.maximum_net_rate): |
| 554 | return True |
| 555 | |
| 556 | return False |
| 557 | |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 558 | @frappe.whitelist() |
Saif Ur Rehman | eeead1d | 2018-12-29 02:25:00 +0500 | [diff] [blame] | 559 | def get_item_tax_map(company, item_tax_template, as_json=True): |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 560 | item_tax_map = {} |
| 561 | if item_tax_template: |
| 562 | template = frappe.get_cached_doc("Item Tax Template", item_tax_template) |
| 563 | for d in template.taxes: |
Saif Ur Rehman | eeead1d | 2018-12-29 02:25:00 +0500 | [diff] [blame] | 564 | if frappe.get_cached_value("Account", d.tax_type, "company") == company: |
| 565 | item_tax_map[d.tax_type] = d.tax_rate |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 566 | |
| 567 | return json.dumps(item_tax_map) if as_json else item_tax_map |
| 568 | |
| 569 | @frappe.whitelist() |
Zlash65 | 80f5cb0 | 2018-08-29 11:39:08 +0530 | [diff] [blame] | 570 | def calculate_service_end_date(args, item=None): |
| 571 | args = process_args(args) |
| 572 | if not item: |
| 573 | item = frappe.get_cached_doc("Item", args.item_code) |
| 574 | |
Zlash65 | 627be1d | 2019-01-15 14:16:32 +0530 | [diff] [blame] | 575 | doctype = args.get("parenttype") or args.get("doctype") |
Zlash65 | a3dd798 | 2019-01-15 14:36:11 +0530 | [diff] [blame] | 576 | if doctype == "Sales Invoice": |
| 577 | enable_deferred = "enable_deferred_revenue" |
| 578 | no_of_months = "no_of_months" |
| 579 | account = "deferred_revenue_account" |
| 580 | else: |
| 581 | enable_deferred = "enable_deferred_expense" |
| 582 | no_of_months = "no_of_months_exp" |
| 583 | account = "deferred_expense_account" |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 584 | |
Zlash65 | 80f5cb0 | 2018-08-29 11:39:08 +0530 | [diff] [blame] | 585 | service_start_date = args.service_start_date if args.service_start_date else args.transaction_date |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 586 | service_end_date = add_months(service_start_date, item.get(no_of_months)) |
Zlash65 | 80f5cb0 | 2018-08-29 11:39:08 +0530 | [diff] [blame] | 587 | deferred_detail = { |
Zlash65 | 80f5cb0 | 2018-08-29 11:39:08 +0530 | [diff] [blame] | 588 | "service_start_date": service_start_date, |
| 589 | "service_end_date": service_end_date |
| 590 | } |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 591 | deferred_detail[enable_deferred] = item.get(enable_deferred) |
| 592 | deferred_detail[account] = get_default_deferred_account(args, item, fieldname=account) |
Zlash65 | 80f5cb0 | 2018-08-29 11:39:08 +0530 | [diff] [blame] | 593 | |
| 594 | return deferred_detail |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 595 | |
Saif Ur Rehman | c819ea3 | 2018-09-25 14:53:12 +0500 | [diff] [blame] | 596 | def get_default_income_account(args, item, item_group, brand): |
Manas Solanki | 38667ab | 2018-05-16 16:55:23 +0530 | [diff] [blame] | 597 | return (item.get("income_account") |
Shreya Shah | 3c9839f | 2018-07-17 18:01:44 +0530 | [diff] [blame] | 598 | or item_group.get("income_account") |
Saif Ur Rehman | c819ea3 | 2018-09-25 14:53:12 +0500 | [diff] [blame] | 599 | or brand.get("income_account") |
Shreya Shah | 3c9839f | 2018-07-17 18:01:44 +0530 | [diff] [blame] | 600 | or args.income_account) |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 601 | |
Saif Ur Rehman | c819ea3 | 2018-09-25 14:53:12 +0500 | [diff] [blame] | 602 | def get_default_expense_account(args, item, item_group, brand): |
Manas Solanki | 38667ab | 2018-05-16 16:55:23 +0530 | [diff] [blame] | 603 | return (item.get("expense_account") |
Shreya Shah | 3c9839f | 2018-07-17 18:01:44 +0530 | [diff] [blame] | 604 | or item_group.get("expense_account") |
Saif Ur Rehman | c819ea3 | 2018-09-25 14:53:12 +0500 | [diff] [blame] | 605 | or brand.get("expense_account") |
Shreya Shah | 3c9839f | 2018-07-17 18:01:44 +0530 | [diff] [blame] | 606 | or args.expense_account) |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 607 | |
GangaManoj | 0b7d8fb | 2021-07-13 01:37:30 +0530 | [diff] [blame] | 608 | def get_default_discount_account(args, item): |
| 609 | return (item.get("default_discount_account") |
GangaManoj | 657e9b5 | 2021-07-06 16:51:12 +0530 | [diff] [blame] | 610 | or args.discount_account) |
| 611 | |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 612 | def get_default_deferred_account(args, item, fieldname=None): |
Zlash65 | 627be1d | 2019-01-15 14:16:32 +0530 | [diff] [blame] | 613 | if item.get("enable_deferred_revenue") or item.get("enable_deferred_expense"): |
Zarrar | e83ff38 | 2018-09-21 15:45:40 +0530 | [diff] [blame] | 614 | return (item.get(fieldname) |
| 615 | or args.get(fieldname) |
| 616 | or frappe.get_cached_value('Company', args.company, "default_"+fieldname)) |
Manas Solanki | 0393848 | 2018-05-14 16:16:46 +0530 | [diff] [blame] | 617 | else: |
| 618 | return None |
| 619 | |
rohitwaghchaure | 1c8c1d7 | 2020-11-15 09:40:44 +0530 | [diff] [blame] | 620 | def get_default_cost_center(args, item=None, item_group=None, brand=None, company=None): |
Nabin Hait | 50238c3 | 2018-08-08 18:43:04 +0530 | [diff] [blame] | 621 | cost_center = None |
rohitwaghchaure | 1c8c1d7 | 2020-11-15 09:40:44 +0530 | [diff] [blame] | 622 | |
| 623 | if not company and args.get("company"): |
| 624 | company = args.get("company") |
| 625 | |
Nabin Hait | 50238c3 | 2018-08-08 18:43:04 +0530 | [diff] [blame] | 626 | if args.get('project'): |
| 627 | cost_center = frappe.db.get_value("Project", args.get("project"), "cost_center", cache=True) |
Rushabh Mehta | 49f9747 | 2018-08-30 18:50:48 +0530 | [diff] [blame] | 628 | |
rohitwaghchaure | 1c8c1d7 | 2020-11-15 09:40:44 +0530 | [diff] [blame] | 629 | if not cost_center and (item and item_group and brand): |
Nabin Hait | 50238c3 | 2018-08-08 18:43:04 +0530 | [diff] [blame] | 630 | if args.get('customer'): |
Saif Ur Rehman | c819ea3 | 2018-09-25 14:53:12 +0500 | [diff] [blame] | 631 | cost_center = item.get('selling_cost_center') or item_group.get('selling_cost_center') or brand.get('selling_cost_center') |
Nabin Hait | 50238c3 | 2018-08-08 18:43:04 +0530 | [diff] [blame] | 632 | else: |
Saif Ur Rehman | c819ea3 | 2018-09-25 14:53:12 +0500 | [diff] [blame] | 633 | cost_center = item.get('buying_cost_center') or item_group.get('buying_cost_center') or brand.get('buying_cost_center') |
Rushabh Mehta | 49f9747 | 2018-08-30 18:50:48 +0530 | [diff] [blame] | 634 | |
rohitwaghchaure | 1c8c1d7 | 2020-11-15 09:40:44 +0530 | [diff] [blame] | 635 | elif not cost_center and args.get("item_code") and company: |
| 636 | for method in ["get_item_defaults", "get_item_group_defaults", "get_brand_defaults"]: |
| 637 | path = "erpnext.stock.get_item_details.{0}".format(method) |
| 638 | data = frappe.get_attr(path)(args.get("item_code"), company) |
| 639 | |
| 640 | if data and (data.selling_cost_center or data.buying_cost_center): |
| 641 | return data.selling_cost_center or data.buying_cost_center |
| 642 | |
| 643 | if not cost_center and args.get("cost_center"): |
| 644 | cost_center = args.get("cost_center") |
Rohit Waghchaure | 9a792c2 | 2019-05-15 12:42:37 +0530 | [diff] [blame] | 645 | |
| 646 | if (company and cost_center |
| 647 | and frappe.get_cached_value("Cost Center", cost_center, "company") != company): |
| 648 | return None |
| 649 | |
rohitwaghchaure | 1c8c1d7 | 2020-11-15 09:40:44 +0530 | [diff] [blame] | 650 | if not cost_center and company: |
| 651 | cost_center = frappe.get_cached_value("Company", |
| 652 | company, "cost_center") |
| 653 | |
Rohit Waghchaure | 9a792c2 | 2019-05-15 12:42:37 +0530 | [diff] [blame] | 654 | return cost_center |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 655 | |
Saif Ur Rehman | c819ea3 | 2018-09-25 14:53:12 +0500 | [diff] [blame] | 656 | def get_default_supplier(args, item, item_group, brand): |
Shreya Shah | 3c9839f | 2018-07-17 18:01:44 +0530 | [diff] [blame] | 657 | return (item.get("default_supplier") |
Saif Ur Rehman | c819ea3 | 2018-09-25 14:53:12 +0500 | [diff] [blame] | 658 | or item_group.get("default_supplier") |
| 659 | or brand.get("default_supplier")) |
Shreya Shah | 3c9839f | 2018-07-17 18:01:44 +0530 | [diff] [blame] | 660 | |
Ankush | 16d4de5 | 2021-08-09 10:41:24 +0530 | [diff] [blame] | 661 | def get_price_list_rate(args, item_doc, out=None): |
| 662 | if out is None: |
| 663 | out = frappe._dict() |
| 664 | |
Anand Doshi | 39a2891 | 2016-02-15 11:42:18 +0530 | [diff] [blame] | 665 | meta = frappe.get_meta(args.parenttype or args.doctype) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 666 | |
Rohit Waghchaure | 4d4fb6d | 2018-05-15 22:12:44 +0530 | [diff] [blame] | 667 | if meta.get_field("currency") or args.get('currency'): |
Marica | e8bc912 | 2021-04-19 11:05:21 +0530 | [diff] [blame] | 668 | if not args.get("price_list_currency") or not args.get("plc_conversion_rate"): |
| 669 | # if currency and plc_conversion_rate exist then |
| 670 | # `get_price_list_currency_and_exchange_rate` has already been called |
| 671 | pl_details = get_price_list_currency_and_exchange_rate(args) |
| 672 | args.update(pl_details) |
| 673 | |
Nabin Hait | 06c8cf4 | 2019-05-16 19:17:02 +0530 | [diff] [blame] | 674 | if meta.get_field("currency"): |
rohitwaghchaure | 4cccdbd | 2017-07-25 14:05:01 +0530 | [diff] [blame] | 675 | validate_conversion_rate(args, meta) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 676 | |
Ankush | 16d4de5 | 2021-08-09 10:41:24 +0530 | [diff] [blame] | 677 | price_list_rate = get_price_list_rate_for(args, item_doc.name) |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 678 | |
hiousi | 1516e29 | 2019-01-08 18:34:08 +0100 | [diff] [blame] | 679 | # variant |
Ankush | 16d4de5 | 2021-08-09 10:41:24 +0530 | [diff] [blame] | 680 | if price_list_rate is None and item_doc.variant_of: |
hiousi | 1516e29 | 2019-01-08 18:34:08 +0100 | [diff] [blame] | 681 | price_list_rate = get_price_list_rate_for(args, item_doc.variant_of) |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 682 | |
| 683 | # insert in database |
Ankush | 16d4de5 | 2021-08-09 10:41:24 +0530 | [diff] [blame] | 684 | if price_list_rate is None: |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 685 | if args.price_list and args.rate: |
| 686 | insert_item_price(args) |
Ankush | 16d4de5 | 2021-08-09 10:41:24 +0530 | [diff] [blame] | 687 | return out |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 688 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 689 | out.price_list_rate = flt(price_list_rate) * flt(args.plc_conversion_rate) \ |
| 690 | / flt(args.conversion_rate) |
mbauskar | 287fe81 | 2017-04-10 19:15:57 +0530 | [diff] [blame] | 691 | |
Nabin Hait | 34d2822 | 2016-01-19 15:45:49 +0530 | [diff] [blame] | 692 | if not out.price_list_rate and args.transaction_type=="buying": |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 693 | from erpnext.stock.doctype.item.item import get_last_purchase_details |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 694 | out.update(get_last_purchase_details(item_doc.name, |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 695 | args.name, args.conversion_rate)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 696 | |
Ankush | 16d4de5 | 2021-08-09 10:41:24 +0530 | [diff] [blame] | 697 | return out |
| 698 | |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 699 | def insert_item_price(args): |
| 700 | """Insert Item Price if Price List and Price List Rate are specified and currency is the same""" |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 701 | if frappe.db.get_value("Price List", args.price_list, "currency", cache=True) == args.currency \ |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 702 | and cint(frappe.db.get_single_value("Stock Settings", "auto_insert_price_list_rate_if_missing")): |
| 703 | if frappe.has_permission("Item Price", "write"): |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 704 | price_list_rate = (args.rate / args.get('conversion_factor') |
| 705 | if args.get("conversion_factor") else args.rate) |
Rushabh Mehta | 985cb82 | 2016-12-30 15:06:54 +0530 | [diff] [blame] | 706 | |
Nabin Hait | b479a87 | 2018-09-07 13:17:11 +0530 | [diff] [blame] | 707 | item_price = frappe.db.get_value('Item Price', |
| 708 | {'item_code': args.item_code, 'price_list': args.price_list, 'currency': args.currency}, |
| 709 | ['name', 'price_list_rate'], as_dict=1) |
| 710 | if item_price and item_price.name: |
mergify[bot] | c0b889b | 2021-11-07 17:42:51 +0530 | [diff] [blame] | 711 | if item_price.price_list_rate != price_list_rate and frappe.db.get_single_value('Stock Settings', 'update_existing_price_list_rate'): |
Nabin Hait | b479a87 | 2018-09-07 13:17:11 +0530 | [diff] [blame] | 712 | frappe.db.set_value('Item Price', item_price.name, "price_list_rate", price_list_rate) |
| 713 | frappe.msgprint(_("Item Price updated for {0} in Price List {1}").format(args.item_code, |
| 714 | args.price_list), alert=True) |
Rushabh Mehta | 985cb82 | 2016-12-30 15:06:54 +0530 | [diff] [blame] | 715 | else: |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 716 | item_price = frappe.get_doc({ |
| 717 | "doctype": "Item Price", |
| 718 | "price_list": args.price_list, |
| 719 | "item_code": args.item_code, |
| 720 | "currency": args.currency, |
| 721 | "price_list_rate": price_list_rate |
| 722 | }) |
Kanchan Chauhan | 5ee8fcc | 2016-06-24 17:11:15 +0530 | [diff] [blame] | 723 | item_price.insert() |
| 724 | frappe.msgprint(_("Item Price added for {0} in Price List {1}").format(args.item_code, |
Rushabh Mehta | 49f9747 | 2018-08-30 18:50:48 +0530 | [diff] [blame] | 725 | args.price_list), alert=True) |
Rushabh Mehta | a208c56 | 2015-08-03 13:18:54 +0530 | [diff] [blame] | 726 | |
Nabin Hait | 01ca3e5 | 2019-01-25 16:25:15 +0530 | [diff] [blame] | 727 | def get_item_price(args, item_code, ignore_party=False): |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 728 | """ |
| 729 | Get name, price_list_rate from Item Price based on conditions |
Suraj Shetty | c87b47a | 2019-04-29 23:18:47 +0530 | [diff] [blame] | 730 | Check if the desired qty is within the increment of the packing list. |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 731 | :param args: dict (or frappe._dict) with mandatory fields price_list, uom |
Nabin Hait | 03b116d | 2020-01-20 15:45:04 +0530 | [diff] [blame] | 732 | optional fields transaction_date, customer, supplier |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 733 | :param item_code: str, Item Doctype field item_code |
| 734 | """ |
| 735 | |
| 736 | args['item_code'] = item_code |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 737 | |
Nabin Hait | 01ca3e5 | 2019-01-25 16:25:15 +0530 | [diff] [blame] | 738 | conditions = """where item_code=%(item_code)s |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 739 | and price_list=%(price_list)s |
| 740 | and ifnull(uom, '') in ('', %(uom)s)""" |
| 741 | |
Deepesh Garg | bd9bdc5 | 2021-01-28 12:05:57 +0530 | [diff] [blame] | 742 | conditions += "and ifnull(batch_no, '') in ('', %(batch_no)s)" |
| 743 | |
Nabin Hait | 01ca3e5 | 2019-01-25 16:25:15 +0530 | [diff] [blame] | 744 | if not ignore_party: |
Nabin Hait | 01ca3e5 | 2019-01-25 16:25:15 +0530 | [diff] [blame] | 745 | if args.get("customer"): |
| 746 | conditions += " and customer=%(customer)s" |
Nabin Hait | 4e45663 | 2019-01-29 14:02:08 +0530 | [diff] [blame] | 747 | elif args.get("supplier"): |
Nabin Hait | 01ca3e5 | 2019-01-25 16:25:15 +0530 | [diff] [blame] | 748 | conditions += " and supplier=%(supplier)s" |
Nabin Hait | 4e45663 | 2019-01-29 14:02:08 +0530 | [diff] [blame] | 749 | else: |
Saqib | 41b47a6 | 2020-05-15 04:24:36 +0530 | [diff] [blame] | 750 | conditions += "and (customer is null or customer = '') and (supplier is null or supplier = '')" |
Nabin Hait | 01ca3e5 | 2019-01-25 16:25:15 +0530 | [diff] [blame] | 751 | |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 752 | if args.get('transaction_date'): |
| 753 | conditions += """ and %(transaction_date)s between |
| 754 | ifnull(valid_from, '2000-01-01') and ifnull(valid_upto, '2500-12-31')""" |
Deepesh Garg | f15ff5f | 2020-07-11 13:58:53 +0530 | [diff] [blame] | 755 | |
Andy Zhu | b60abb0 | 2020-07-03 14:13:15 +1200 | [diff] [blame] | 756 | if args.get('posting_date'): |
| 757 | conditions += """ and %(posting_date)s between |
| 758 | ifnull(valid_from, '2000-01-01') and ifnull(valid_upto, '2500-12-31')""" |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 759 | |
| 760 | return frappe.db.sql(""" select name, price_list_rate, uom |
| 761 | from `tabItem Price` {conditions} |
Deepesh Garg | bd9bdc5 | 2021-01-28 12:05:57 +0530 | [diff] [blame] | 762 | order by valid_from desc, batch_no desc, uom desc """.format(conditions=conditions), args) |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 763 | |
| 764 | def get_price_list_rate_for(args, item_code): |
| 765 | """ |
Suraj Shetty | c87b47a | 2019-04-29 23:18:47 +0530 | [diff] [blame] | 766 | :param customer: link to Customer DocType |
| 767 | :param supplier: link to Supplier DocType |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 768 | :param price_list: str (Standard Buying or Standard Selling) |
| 769 | :param item_code: str, Item Doctype field item_code |
Suraj Shetty | c87b47a | 2019-04-29 23:18:47 +0530 | [diff] [blame] | 770 | :param qty: Desired Qty |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 771 | :param transaction_date: Date of the price |
| 772 | """ |
| 773 | item_price_args = { |
| 774 | "item_code": item_code, |
| 775 | "price_list": args.get('price_list'), |
| 776 | "customer": args.get('customer'), |
| 777 | "supplier": args.get('supplier'), |
| 778 | "uom": args.get('uom'), |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 779 | "transaction_date": args.get('transaction_date'), |
Andy Zhu | b60abb0 | 2020-07-03 14:13:15 +1200 | [diff] [blame] | 780 | "posting_date": args.get('posting_date'), |
Deepesh Garg | bd9bdc5 | 2021-01-28 12:05:57 +0530 | [diff] [blame] | 781 | "batch_no": args.get('batch_no') |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | item_price_data = 0 |
| 785 | price_list_rate = get_item_price(item_price_args, item_code) |
| 786 | if price_list_rate: |
| 787 | desired_qty = args.get("qty") |
Rohit Waghchaure | b48effe | 2019-02-27 19:31:32 +0530 | [diff] [blame] | 788 | if desired_qty and check_packing_list(price_list_rate[0][0], desired_qty, item_code): |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 789 | item_price_data = price_list_rate |
| 790 | else: |
Don-Leopardo | ccdf193 | 2020-01-09 03:54:43 -0300 | [diff] [blame] | 791 | for field in ["customer", "supplier"]: |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 792 | del item_price_args[field] |
| 793 | |
Nabin Hait | e321fbb | 2020-01-16 13:37:25 +0530 | [diff] [blame] | 794 | general_price_list_rate = get_item_price(item_price_args, item_code, |
| 795 | ignore_party=args.get("ignore_party")) |
Don-Leopardo | ccdf193 | 2020-01-09 03:54:43 -0300 | [diff] [blame] | 796 | |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 797 | if not general_price_list_rate and args.get("uom") != args.get("stock_uom"): |
Suraj Shetty | c87b47a | 2019-04-29 23:18:47 +0530 | [diff] [blame] | 798 | item_price_args["uom"] = args.get("stock_uom") |
Nabin Hait | 01ca3e5 | 2019-01-25 16:25:15 +0530 | [diff] [blame] | 799 | general_price_list_rate = get_item_price(item_price_args, item_code, ignore_party=args.get("ignore_party")) |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 800 | |
| 801 | if general_price_list_rate: |
| 802 | item_price_data = general_price_list_rate |
| 803 | |
| 804 | if item_price_data: |
| 805 | if item_price_data[0][2] == args.get("uom"): |
| 806 | return item_price_data[0][1] |
| 807 | elif not args.get('price_list_uom_dependant'): |
| 808 | return flt(item_price_data[0][1] * flt(args.get("conversion_factor", 1))) |
| 809 | else: |
| 810 | return item_price_data[0][1] |
| 811 | |
| 812 | def check_packing_list(price_list_rate_name, desired_qty, item_code): |
| 813 | """ |
Suraj Shetty | c87b47a | 2019-04-29 23:18:47 +0530 | [diff] [blame] | 814 | Check if the desired qty is within the increment of the packing list. |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 815 | :param price_list_rate_name: Name of Item Price |
Suraj Shetty | c87b47a | 2019-04-29 23:18:47 +0530 | [diff] [blame] | 816 | :param desired_qty: Desired Qt |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 817 | :param item_code: str, Item Doctype field item_code |
Suraj Shetty | c87b47a | 2019-04-29 23:18:47 +0530 | [diff] [blame] | 818 | :param qty: Desired Qt |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 819 | """ |
| 820 | |
Rohit Waghchaure | cd34c70 | 2019-02-27 18:01:25 +0530 | [diff] [blame] | 821 | flag = True |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 822 | item_price = frappe.get_doc("Item Price", price_list_rate_name) |
Rohit Waghchaure | b48effe | 2019-02-27 19:31:32 +0530 | [diff] [blame] | 823 | if item_price.packing_unit: |
Nabin Hait | e45ec66 | 2018-08-01 17:44:34 +0530 | [diff] [blame] | 824 | packing_increment = desired_qty % item_price.packing_unit |
| 825 | |
Rohit Waghchaure | cd34c70 | 2019-02-27 18:01:25 +0530 | [diff] [blame] | 826 | if packing_increment != 0: |
| 827 | flag = False |
| 828 | |
| 829 | return flag |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 830 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 831 | def validate_conversion_rate(args, meta): |
Rushabh Mehta | 66e08e3 | 2014-09-29 12:17:03 +0530 | [diff] [blame] | 832 | from erpnext.controllers.accounts_controller import validate_conversion_rate |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 833 | |
Rohit Waghchaure | 4acbeec | 2021-07-13 11:45:41 +0530 | [diff] [blame] | 834 | company_currency = frappe.get_cached_value('Company', args.company, "default_currency") |
| 835 | if (not args.conversion_rate and args.currency==company_currency): |
Anand Doshi | fe13bfe | 2015-08-25 12:49:40 +0530 | [diff] [blame] | 836 | args.conversion_rate = 1.0 |
| 837 | |
Rohit Waghchaure | 4acbeec | 2021-07-13 11:45:41 +0530 | [diff] [blame] | 838 | if (not args.ignore_conversion_rate and args.conversion_rate == 1 and args.currency!=company_currency): |
| 839 | args.conversion_rate = get_exchange_rate(args.currency, |
| 840 | company_currency, args.transaction_date, "for_buying") or 1.0 |
| 841 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 842 | # validate currency conversion rate |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 843 | validate_conversion_rate(args.currency, args.conversion_rate, |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 844 | meta.get_label("conversion_rate"), args.company) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 845 | |
| 846 | args.conversion_rate = flt(args.conversion_rate, |
| 847 | get_field_precision(meta.get_field("conversion_rate"), |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 848 | frappe._dict({"fields": args}))) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 849 | |
Nabin Hait | 06c8cf4 | 2019-05-16 19:17:02 +0530 | [diff] [blame] | 850 | if args.price_list: |
| 851 | if (not args.plc_conversion_rate |
| 852 | and args.price_list_currency==frappe.db.get_value("Price List", args.price_list, "currency", cache=True)): |
| 853 | args.plc_conversion_rate = 1.0 |
Manas Solanki | 3504a23 | 2018-06-06 12:46:06 +0530 | [diff] [blame] | 854 | |
Nabin Hait | 06c8cf4 | 2019-05-16 19:17:02 +0530 | [diff] [blame] | 855 | # validate price list currency conversion rate |
| 856 | if not args.get("price_list_currency"): |
| 857 | throw(_("Price List Currency not selected")) |
| 858 | else: |
| 859 | validate_conversion_rate(args.price_list_currency, args.plc_conversion_rate, |
| 860 | meta.get_label("plc_conversion_rate"), args.company) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 861 | |
Nabin Hait | 06c8cf4 | 2019-05-16 19:17:02 +0530 | [diff] [blame] | 862 | if meta.get_field("plc_conversion_rate"): |
| 863 | args.plc_conversion_rate = flt(args.plc_conversion_rate, |
| 864 | get_field_precision(meta.get_field("plc_conversion_rate"), |
| 865 | frappe._dict({"fields": args}))) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 866 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 867 | def get_party_item_code(args, item_doc, out): |
Nabin Hait | 34d2822 | 2016-01-19 15:45:49 +0530 | [diff] [blame] | 868 | if args.transaction_type=="selling" and args.customer: |
Zarrar | 3fd6347 | 2018-03-09 12:10:45 +0530 | [diff] [blame] | 869 | out.customer_item_code = None |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 870 | |
| 871 | if args.quotation_to and args.quotation_to != 'Customer': |
| 872 | return |
| 873 | |
Nabin Hait | 79f091e | 2014-12-26 11:41:15 +0530 | [diff] [blame] | 874 | customer_item_code = item_doc.get("customer_items", {"customer_name": args.customer}) |
Zarrar | 3fd6347 | 2018-03-09 12:10:45 +0530 | [diff] [blame] | 875 | |
| 876 | if customer_item_code: |
| 877 | out.customer_item_code = customer_item_code[0].ref_code |
| 878 | else: |
Nabin Hait | 9a33bc6 | 2018-08-09 10:47:09 +0530 | [diff] [blame] | 879 | customer_group = frappe.get_cached_value("Customer", args.customer, "customer_group") |
Zarrar | 3fd6347 | 2018-03-09 12:10:45 +0530 | [diff] [blame] | 880 | customer_group_item_code = item_doc.get("customer_items", {"customer_group": customer_group}) |
| 881 | if customer_group_item_code and not customer_group_item_code[0].customer_name: |
| 882 | out.customer_item_code = customer_group_item_code[0].ref_code |
Anand Doshi | 54abf02 | 2016-01-21 22:28:47 +0530 | [diff] [blame] | 883 | |
Nabin Hait | 34d2822 | 2016-01-19 15:45:49 +0530 | [diff] [blame] | 884 | if args.transaction_type=="buying" and args.supplier: |
Nabin Hait | 79f091e | 2014-12-26 11:41:15 +0530 | [diff] [blame] | 885 | item_supplier = item_doc.get("supplier_items", {"supplier": args.supplier}) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 886 | 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] | 887 | |
rohitwaghchaure | 2ea593b | 2018-04-12 13:37:08 +0530 | [diff] [blame] | 888 | 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] | 889 | res = frappe._dict() |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 890 | |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 891 | if not frappe.flags.pos_profile and not pos_profile: |
| 892 | pos_profile = frappe.flags.pos_profile = get_pos_profile(company, args.get('pos_profile')) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 893 | |
Rushabh Mehta | ad9156a | 2015-08-18 15:29:42 +0530 | [diff] [blame] | 894 | if pos_profile: |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 895 | for fieldname in ("income_account", "cost_center", "warehouse", "expense_account"): |
rohitwaghchaure | 2ea593b | 2018-04-12 13:37:08 +0530 | [diff] [blame] | 896 | 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] | 897 | res[fieldname] = pos_profile.get(fieldname) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 898 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 899 | if res.get("warehouse"): |
Saqib Ansari | eb3aae8 | 2021-10-12 13:29:32 +0530 | [diff] [blame] | 900 | res.actual_qty = get_bin_details(args.item_code, res.warehouse).get("actual_qty") |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 901 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 902 | return res |
| 903 | |
Rushabh Mehta | ad9156a | 2015-08-18 15:29:42 +0530 | [diff] [blame] | 904 | @frappe.whitelist() |
rohitwaghchaure | c037dc7 | 2017-11-28 16:11:15 +0530 | [diff] [blame] | 905 | def get_pos_profile(company, pos_profile=None, user=None): |
Rohit Waghchaure | e8e3fb1 | 2019-04-05 00:42:48 +0530 | [diff] [blame] | 906 | if pos_profile: return frappe.get_cached_doc('POS Profile', pos_profile) |
rohitwaghchaure | c037dc7 | 2017-11-28 16:11:15 +0530 | [diff] [blame] | 907 | |
| 908 | if not user: |
| 909 | user = frappe.session['user'] |
| 910 | |
Rohit Waghchaure | e8e3fb1 | 2019-04-05 00:42:48 +0530 | [diff] [blame] | 911 | condition = "pfu.user = %(user)s AND pfu.default=1" |
| 912 | if user and company: |
| 913 | condition = "pfu.user = %(user)s AND pf.company = %(company)s AND pfu.default=1" |
| 914 | |
Suraj Shetty | 1eb098c | 2018-11-21 14:36:58 +0530 | [diff] [blame] | 915 | pos_profile = frappe.db.sql("""SELECT pf.* |
| 916 | FROM |
| 917 | `tabPOS Profile` pf LEFT JOIN `tabPOS Profile User` pfu |
| 918 | ON |
rohitwaghchaure | c037dc7 | 2017-11-28 16:11:15 +0530 | [diff] [blame] | 919 | pf.name = pfu.parent |
Suraj Shetty | 1eb098c | 2018-11-21 14:36:58 +0530 | [diff] [blame] | 920 | WHERE |
Rohit Waghchaure | e8e3fb1 | 2019-04-05 00:42:48 +0530 | [diff] [blame] | 921 | {cond} AND pf.disabled = 0 |
| 922 | """.format(cond = condition), { |
Suraj Shetty | 1eb098c | 2018-11-21 14:36:58 +0530 | [diff] [blame] | 923 | 'user': user, |
| 924 | 'company': company |
| 925 | }, as_dict=1) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 926 | |
Rohit Waghchaure | e8e3fb1 | 2019-04-05 00:42:48 +0530 | [diff] [blame] | 927 | if not pos_profile and company: |
| 928 | pos_profile = frappe.db.sql("""SELECT pf.* |
| 929 | FROM |
| 930 | `tabPOS Profile` pf LEFT JOIN `tabPOS Profile User` pfu |
| 931 | ON |
| 932 | pf.name = pfu.parent |
| 933 | WHERE |
| 934 | pf.company = %(company)s AND pf.disabled = 0 |
| 935 | """, { |
| 936 | 'company': company |
| 937 | }, as_dict=1) |
| 938 | |
Rushabh Mehta | ad9156a | 2015-08-18 15:29:42 +0530 | [diff] [blame] | 939 | return pos_profile and pos_profile[0] or None |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 940 | |
Ranjith Kurungadam | d54991d | 2018-08-01 17:47:07 +0530 | [diff] [blame] | 941 | def get_serial_nos_by_fifo(args, sales_order=None): |
Rushabh Mehta | 85abdc4 | 2015-09-03 10:29:38 +0530 | [diff] [blame] | 942 | if frappe.db.get_single_value("Stock Settings", "automatically_set_serial_nos_based_on_fifo"): |
| 943 | return "\n".join(frappe.db.sql_list("""select name from `tabSerial No` |
Ranjith Kurungadam | d54991d | 2018-08-01 17:47:07 +0530 | [diff] [blame] | 944 | where item_code=%(item_code)s and warehouse=%(warehouse)s and |
| 945 | sales_order=IF(%(sales_order)s IS NULL, sales_order, %(sales_order)s) |
| 946 | order by timestamp(purchase_date, purchase_time) |
| 947 | asc limit %(qty)s""", |
| 948 | { |
Rushabh Mehta | 85abdc4 | 2015-09-03 10:29:38 +0530 | [diff] [blame] | 949 | "item_code": args.item_code, |
| 950 | "warehouse": args.warehouse, |
Ranjith Kurungadam | d54991d | 2018-08-01 17:47:07 +0530 | [diff] [blame] | 951 | "qty": abs(cint(args.stock_qty)), |
| 952 | "sales_order": sales_order |
Rushabh Mehta | 85abdc4 | 2015-09-03 10:29:38 +0530 | [diff] [blame] | 953 | })) |
Nabin Hait | 0822215 | 2014-02-28 11:30:47 +0530 | [diff] [blame] | 954 | |
Ranjith Kurungadam | d54991d | 2018-08-01 17:47:07 +0530 | [diff] [blame] | 955 | def get_serial_no_batchwise(args, sales_order=None): |
Shreya | a20157a | 2018-04-13 12:03:42 +0530 | [diff] [blame] | 956 | if frappe.db.get_single_value("Stock Settings", "automatically_set_serial_nos_based_on_fifo"): |
| 957 | return "\n".join(frappe.db.sql_list("""select name from `tabSerial No` |
Ranjith Kurungadam | d54991d | 2018-08-01 17:47:07 +0530 | [diff] [blame] | 958 | where item_code=%(item_code)s and warehouse=%(warehouse)s and |
| 959 | sales_order=IF(%(sales_order)s IS NULL, sales_order, %(sales_order)s) |
| 960 | and batch_no=IF(%(batch_no)s IS NULL, batch_no, %(batch_no)s) order |
| 961 | by timestamp(purchase_date, purchase_time) asc limit %(qty)s""", { |
Shreya | a20157a | 2018-04-13 12:03:42 +0530 | [diff] [blame] | 962 | "item_code": args.item_code, |
| 963 | "warehouse": args.warehouse, |
| 964 | "batch_no": args.batch_no, |
Ranjith Kurungadam | d54991d | 2018-08-01 17:47:07 +0530 | [diff] [blame] | 965 | "qty": abs(cint(args.stock_qty)), |
| 966 | "sales_order": sales_order |
Shreya | a20157a | 2018-04-13 12:03:42 +0530 | [diff] [blame] | 967 | })) |
| 968 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 969 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 970 | def get_conversion_factor(item_code, uom): |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 971 | variant_of = frappe.db.get_value("Item", item_code, "variant_of", cache=True) |
Rushabh Mehta | 724f9e5 | 2014-10-07 15:29:58 +0530 | [diff] [blame] | 972 | filters = {"parent": item_code, "uom": uom} |
| 973 | if variant_of: |
Nabin Hait | 314086d | 2015-10-07 11:55:01 +0530 | [diff] [blame] | 974 | filters["parent"] = ("in", (item_code, variant_of)) |
Sagar Vora | 508189e | 2018-09-11 17:22:25 +0530 | [diff] [blame] | 975 | conversion_factor = frappe.db.get_value("UOM Conversion Detail", |
| 976 | filters, "conversion_factor") |
| 977 | if not conversion_factor: |
| 978 | stock_uom = frappe.db.get_value("Item", item_code, "stock_uom") |
| 979 | conversion_factor = get_uom_conv_factor(uom, stock_uom) |
rohitwaghchaure | ffdadbf | 2019-01-08 16:21:25 +0530 | [diff] [blame] | 980 | return {"conversion_factor": conversion_factor or 1.0} |
Nabin Hait | 0822215 | 2014-02-28 11:30:47 +0530 | [diff] [blame] | 981 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 982 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 983 | def get_projected_qty(item_code, warehouse): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 984 | return {"projected_qty": frappe.db.get_value("Bin", |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 985 | {"item_code": item_code, "warehouse": warehouse}, "projected_qty")} |
Nabin Hait | 0822215 | 2014-02-28 11:30:47 +0530 | [diff] [blame] | 986 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 987 | @frappe.whitelist() |
Anupam | 4980dd6 | 2021-03-15 11:37:46 +0530 | [diff] [blame] | 988 | def get_bin_details(item_code, warehouse, company=None): |
| 989 | bin_details = frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse}, |
Nabin Hait | a804d6d | 2018-08-29 13:34:58 +0530 | [diff] [blame] | 990 | ["projected_qty", "actual_qty", "reserved_qty"], as_dict=True, cache=True) \ |
Shreya Shah | be77033 | 2018-08-22 14:45:22 +0530 | [diff] [blame] | 991 | or {"projected_qty": 0, "actual_qty": 0, "reserved_qty": 0} |
Anupam | 4980dd6 | 2021-03-15 11:37:46 +0530 | [diff] [blame] | 992 | if company: |
| 993 | bin_details['company_total_stock'] = get_company_total_stock(item_code, company) |
| 994 | return bin_details |
Kanchan Chauhan | af0d637 | 2016-11-13 21:19:09 +0530 | [diff] [blame] | 995 | |
Anupam | 4980dd6 | 2021-03-15 11:37:46 +0530 | [diff] [blame] | 996 | def get_company_total_stock(item_code, company): |
Anuja Pawar | f132ed4 | 2021-05-07 12:11:09 +0530 | [diff] [blame] | 997 | return frappe.db.sql("""SELECT sum(actual_qty) from |
| 998 | (`tabBin` INNER JOIN `tabWarehouse` ON `tabBin`.warehouse = `tabWarehouse`.name) |
18alantom | e36f303 | 2021-05-03 19:49:22 +0530 | [diff] [blame] | 999 | WHERE `tabWarehouse`.company = %s and `tabBin`.item_code = %s""", |
| 1000 | (company, item_code))[0][0] |
Anupam | 4980dd6 | 2021-03-15 11:37:46 +0530 | [diff] [blame] | 1001 | |
| 1002 | @frappe.whitelist() |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 1003 | def get_serial_no_details(item_code, warehouse, stock_qty, serial_no): |
| 1004 | 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] | 1005 | serial_no = get_serial_no(args) |
| 1006 | return {'serial_no': serial_no} |
Rushabh Mehta | 985cb82 | 2016-12-30 15:06:54 +0530 | [diff] [blame] | 1007 | |
Kanchan Chauhan | af0d637 | 2016-11-13 21:19:09 +0530 | [diff] [blame] | 1008 | @frappe.whitelist() |
Rohit Waghchaure | 6daab3c | 2019-09-19 17:01:26 +0530 | [diff] [blame] | 1009 | def get_bin_details_and_serial_nos(item_code, warehouse, has_batch_no=None, stock_qty=None, serial_no=None): |
Kanchan Chauhan | af0d637 | 2016-11-13 21:19:09 +0530 | [diff] [blame] | 1010 | bin_details_and_serial_nos = {} |
| 1011 | bin_details_and_serial_nos.update(get_bin_details(item_code, warehouse)) |
Rushabh Mehta | 49f9747 | 2018-08-30 18:50:48 +0530 | [diff] [blame] | 1012 | if flt(stock_qty) > 0: |
Shreya | a20157a | 2018-04-13 12:03:42 +0530 | [diff] [blame] | 1013 | if has_batch_no: |
| 1014 | args = frappe._dict({"item_code":item_code, "warehouse":warehouse, "stock_qty":stock_qty}) |
| 1015 | serial_no = get_serial_no(args) |
| 1016 | bin_details_and_serial_nos.update({'serial_no': serial_no}) |
| 1017 | return bin_details_and_serial_nos |
| 1018 | |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 1019 | 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] | 1020 | return bin_details_and_serial_nos |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1021 | |
| 1022 | @frappe.whitelist() |
Shreya | a20157a | 2018-04-13 12:03:42 +0530 | [diff] [blame] | 1023 | def get_batch_qty_and_serial_no(batch_no, stock_qty, warehouse, item_code, has_serial_no): |
| 1024 | batch_qty_and_serial_no = {} |
| 1025 | batch_qty_and_serial_no.update(get_batch_qty(batch_no, warehouse, item_code)) |
| 1026 | |
| 1027 | if (flt(batch_qty_and_serial_no.get('actual_batch_qty')) >= flt(stock_qty)) and has_serial_no: |
| 1028 | args = frappe._dict({"item_code":item_code, "warehouse":warehouse, "stock_qty":stock_qty, "batch_no":batch_no}) |
| 1029 | serial_no = get_serial_no(args) |
| 1030 | batch_qty_and_serial_no.update({'serial_no': serial_no}) |
| 1031 | return batch_qty_and_serial_no |
| 1032 | |
| 1033 | @frappe.whitelist() |
Rushabh Mehta | e385b5b | 2017-04-20 15:21:01 +0530 | [diff] [blame] | 1034 | def get_batch_qty(batch_no, warehouse, item_code): |
sburanaw | 66951e5 | 2017-04-25 15:28:57 +0700 | [diff] [blame] | 1035 | from erpnext.stock.doctype.batch import batch |
Sambhaji Kolate | 98dbccd | 2015-03-10 15:04:28 +0530 | [diff] [blame] | 1036 | if batch_no: |
Rushabh Mehta | e385b5b | 2017-04-20 15:21:01 +0530 | [diff] [blame] | 1037 | return {'actual_batch_qty': batch.get_batch_qty(batch_no, warehouse)} |
Rushabh Mehta | 1e8025b | 2015-07-24 15:16:25 +0530 | [diff] [blame] | 1038 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1039 | @frappe.whitelist() |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 1040 | def apply_price_list(args, as_doc=False): |
| 1041 | """Apply pricelist on a document-like dict object and return as |
| 1042 | {'parent': dict, 'children': list} |
| 1043 | |
| 1044 | :param args: See below |
| 1045 | :param as_doc: Updates value in the passed dict |
| 1046 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1047 | args = { |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 1048 | "doctype": "", |
| 1049 | "name": "", |
| 1050 | "items": [{"doctype": "", "name": "", "item_code": "", "brand": "", "item_group": ""}, ...], |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1051 | "conversion_rate": 1.0, |
| 1052 | "selling_price_list": None, |
| 1053 | "price_list_currency": None, |
Shreya Shah | b13a54a | 2017-12-06 19:17:04 +0530 | [diff] [blame] | 1054 | "price_list_uom_dependant": None, |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1055 | "plc_conversion_rate": 1.0, |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 1056 | "doctype": "", |
| 1057 | "name": "", |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1058 | "supplier": None, |
| 1059 | "transaction_date": None, |
| 1060 | "conversion_rate": 1.0, |
| 1061 | "buying_price_list": None, |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1062 | "ignore_pricing_rule": 0/1 |
| 1063 | } |
| 1064 | """ |
| 1065 | args = process_args(args) |
| 1066 | |
| 1067 | parent = get_price_list_currency_and_exchange_rate(args) |
Marica | e8bc912 | 2021-04-19 11:05:21 +0530 | [diff] [blame] | 1068 | args.update(parent) |
| 1069 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1070 | children = [] |
| 1071 | |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 1072 | if "items" in args: |
| 1073 | item_list = args.get("items") |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1074 | args.update(parent) |
| 1075 | |
| 1076 | for item in item_list: |
| 1077 | args_copy = frappe._dict(args.copy()) |
| 1078 | args_copy.update(item) |
| 1079 | item_details = apply_price_list_on_item(args_copy) |
| 1080 | children.append(item_details) |
| 1081 | |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 1082 | if as_doc: |
Shreya Shah | b13a54a | 2017-12-06 19:17:04 +0530 | [diff] [blame] | 1083 | args.price_list_currency = parent.price_list_currency, |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 1084 | args.plc_conversion_rate = parent.plc_conversion_rate |
| 1085 | if args.get('items'): |
| 1086 | for i, item in enumerate(args.get('items')): |
| 1087 | for fieldname in children[i]: |
| 1088 | # if the field exists in the original doc |
| 1089 | # update the value |
| 1090 | if fieldname in item and fieldname not in ("name", "doctype"): |
| 1091 | item[fieldname] = children[i][fieldname] |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 1092 | return args |
| 1093 | else: |
| 1094 | return { |
| 1095 | "parent": parent, |
| 1096 | "children": children |
| 1097 | } |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1098 | |
| 1099 | def apply_price_list_on_item(args): |
Deepesh Garg | ab09dc5 | 2021-12-20 12:16:14 +0530 | [diff] [blame] | 1100 | item_doc = frappe.db.get_value("Item", args.item_code, ['name', 'variant_of'], as_dict=1) |
Ankush | 16d4de5 | 2021-08-09 10:41:24 +0530 | [diff] [blame] | 1101 | item_details = get_price_list_rate(args, item_doc) |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 1102 | |
Rohit Waghchaure | 8bfe330 | 2019-03-18 14:34:19 +0530 | [diff] [blame] | 1103 | item_details.update(get_pricing_rule_for_item(args, item_details.price_list_rate)) |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 1104 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1105 | return item_details |
| 1106 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1107 | def get_price_list_currency_and_exchange_rate(args): |
Anand Doshi | 70f57eb | 2015-11-27 14:37:40 +0530 | [diff] [blame] | 1108 | if not args.price_list: |
| 1109 | return {} |
| 1110 | |
Shreya | 3f77852 | 2018-05-15 16:59:20 +0530 | [diff] [blame] | 1111 | if args.doctype in ['Quotation', 'Sales Order', 'Delivery Note', 'Sales Invoice']: |
| 1112 | args.update({"exchange_rate": "for_selling"}) |
| 1113 | elif args.doctype in ['Purchase Order', 'Purchase Receipt', 'Purchase Invoice']: |
| 1114 | args.update({"exchange_rate": "for_buying"}) |
| 1115 | |
rohitwaghchaure | a85ddf2 | 2019-11-19 18:47:48 +0530 | [diff] [blame] | 1116 | price_list_details = get_price_list_details(args.price_list) |
| 1117 | |
| 1118 | price_list_currency = price_list_details.get("currency") |
| 1119 | price_list_uom_dependant = price_list_details.get("price_list_uom_dependant") |
| 1120 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1121 | plc_conversion_rate = args.plc_conversion_rate |
tundebabzy | 6e90f49 | 2018-02-12 10:48:57 +0100 | [diff] [blame] | 1122 | company_currency = get_company_currency(args.company) |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1123 | |
Nabin Hait | dd82bf2 | 2015-05-11 16:49:17 +0530 | [diff] [blame] | 1124 | if (not plc_conversion_rate) or (price_list_currency and args.price_list_currency \ |
| 1125 | and price_list_currency != args.price_list_currency): |
Rushabh Mehta | 6b53792 | 2017-03-14 16:59:11 +0530 | [diff] [blame] | 1126 | # 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] | 1127 | plc_conversion_rate = get_exchange_rate(price_list_currency, company_currency, |
Shreya | 3f77852 | 2018-05-15 16:59:20 +0530 | [diff] [blame] | 1128 | args.transaction_date, args.exchange_rate) or plc_conversion_rate |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1129 | |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 1130 | return frappe._dict({ |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 1131 | "price_list_currency": price_list_currency, |
Shreya Shah | b13a54a | 2017-12-06 19:17:04 +0530 | [diff] [blame] | 1132 | "price_list_uom_dependant": price_list_uom_dependant, |
Marica | e8bc912 | 2021-04-19 11:05:21 +0530 | [diff] [blame] | 1133 | "plc_conversion_rate": plc_conversion_rate or 1 |
Rushabh Mehta | b46069d | 2016-01-15 16:59:26 +0530 | [diff] [blame] | 1134 | }) |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 1135 | |
| 1136 | @frappe.whitelist() |
| 1137 | def get_default_bom(item_code=None): |
| 1138 | if item_code: |
| 1139 | bom = frappe.db.get_value("BOM", {"docstatus": 1, "is_default": 1, "is_active": 1, "item": item_code}) |
| 1140 | if bom: |
| 1141 | return bom |
Rushabh Mehta | 0394aec | 2016-04-22 17:22:22 +0530 | [diff] [blame] | 1142 | |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 1143 | @frappe.whitelist() |
Manas Solanki | 087a225 | 2018-05-04 16:02:38 +0530 | [diff] [blame] | 1144 | def get_valuation_rate(item_code, company, warehouse=None): |
Manas Solanki | b16a4ec | 2018-05-04 16:49:33 +0530 | [diff] [blame] | 1145 | item = get_item_defaults(item_code, company) |
Shreya Shah | 3c9839f | 2018-07-17 18:01:44 +0530 | [diff] [blame] | 1146 | item_group = get_item_group_defaults(item_code, company) |
Saif Ur Rehman | c819ea3 | 2018-09-25 14:53:12 +0500 | [diff] [blame] | 1147 | brand = get_brand_defaults(item_code, company) |
Manas Solanki | 087a225 | 2018-05-04 16:02:38 +0530 | [diff] [blame] | 1148 | # item = frappe.get_doc("Item", item_code) |
Manas Solanki | e3910fb | 2018-05-16 15:50:17 +0530 | [diff] [blame] | 1149 | if item.get("is_stock_item"): |
Saurabh | bd01a81 | 2016-03-07 14:32:23 +0530 | [diff] [blame] | 1150 | if not warehouse: |
Saif Ur Rehman | c819ea3 | 2018-09-25 14:53:12 +0500 | [diff] [blame] | 1151 | warehouse = item.get("default_warehouse") or item_group.get("default_warehouse") or brand.get("default_warehouse") |
Rushabh Mehta | 0394aec | 2016-04-22 17:22:22 +0530 | [diff] [blame] | 1152 | |
| 1153 | return frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse}, |
Saurabh | bd01a81 | 2016-03-07 14:32:23 +0530 | [diff] [blame] | 1154 | ["valuation_rate"], as_dict=True) or {"valuation_rate": 0} |
Rushabh Mehta | 0394aec | 2016-04-22 17:22:22 +0530 | [diff] [blame] | 1155 | |
Manas Solanki | e3910fb | 2018-05-16 15:50:17 +0530 | [diff] [blame] | 1156 | elif not item.get("is_stock_item"): |
Nabin Hait | fe876c0 | 2017-03-06 16:32:57 +0530 | [diff] [blame] | 1157 | 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] | 1158 | from `tabPurchase Invoice Item` |
Saurabh | bd01a81 | 2016-03-07 14:32:23 +0530 | [diff] [blame] | 1159 | where item_code = %s and docstatus=1""", item_code) |
Rushabh Mehta | 0394aec | 2016-04-22 17:22:22 +0530 | [diff] [blame] | 1160 | |
Saurabh | bd01a81 | 2016-03-07 14:32:23 +0530 | [diff] [blame] | 1161 | if valuation_rate: |
| 1162 | return {"valuation_rate": valuation_rate[0][0] or 0.0} |
| 1163 | else: |
| 1164 | return {"valuation_rate": 0.0} |
Rushabh Mehta | 0394aec | 2016-04-22 17:22:22 +0530 | [diff] [blame] | 1165 | |
Saurabh | 2d7af63 | 2016-02-26 11:09:20 +0530 | [diff] [blame] | 1166 | def get_gross_profit(out): |
Saurabh | 5ada14b | 2016-02-26 18:02:55 +0530 | [diff] [blame] | 1167 | if out.valuation_rate: |
| 1168 | out.update({ |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 1169 | "gross_profit": ((out.base_rate - out.valuation_rate) * out.stock_qty) |
Saurabh | 5ada14b | 2016-02-26 18:02:55 +0530 | [diff] [blame] | 1170 | }) |
Rushabh Mehta | 0394aec | 2016-04-22 17:22:22 +0530 | [diff] [blame] | 1171 | |
Saurabh | d313553 | 2016-02-25 18:59:20 +0530 | [diff] [blame] | 1172 | return out |
mbauskar | 6f60339 | 2016-01-21 16:10:22 +0530 | [diff] [blame] | 1173 | |
Kanchan Chauhan | af0d637 | 2016-11-13 21:19:09 +0530 | [diff] [blame] | 1174 | @frappe.whitelist() |
Ranjith Kurungadam | d54991d | 2018-08-01 17:47:07 +0530 | [diff] [blame] | 1175 | def get_serial_no(args, serial_nos=None, sales_order=None): |
Rohit Waghchaure | ba3f0e6 | 2017-08-28 17:19:28 +0530 | [diff] [blame] | 1176 | serial_no = None |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 1177 | if isinstance(args, str): |
Kanchan Chauhan | af0d637 | 2016-11-13 21:19:09 +0530 | [diff] [blame] | 1178 | args = json.loads(args) |
| 1179 | args = frappe._dict(args) |
Rohit Waghchaure | dc981dc | 2017-04-03 14:17:08 +0530 | [diff] [blame] | 1180 | if args.get('doctype') == 'Sales Invoice' and not args.get('update_stock'): |
| 1181 | return "" |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 1182 | 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] | 1183 | 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] | 1184 | if args.get('batch_no') and has_serial_no == 1: |
Ranjith Kurungadam | d54991d | 2018-08-01 17:47:07 +0530 | [diff] [blame] | 1185 | return get_serial_no_batchwise(args, sales_order) |
Shreya | a20157a | 2018-04-13 12:03:42 +0530 | [diff] [blame] | 1186 | elif has_serial_no == 1: |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 1187 | 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] | 1188 | args = process_args(args) |
Ranjith Kurungadam | d54991d | 2018-08-01 17:47:07 +0530 | [diff] [blame] | 1189 | serial_no = get_serial_nos_by_fifo(args, sales_order) |
Rohit Waghchaure | ba3f0e6 | 2017-08-28 17:19:28 +0530 | [diff] [blame] | 1190 | |
| 1191 | if not serial_no and serial_nos: |
| 1192 | # For POS |
| 1193 | serial_no = serial_nos |
| 1194 | |
| 1195 | return serial_no |
Manas Solanki | e5e87f7 | 2018-05-28 20:07:08 +0530 | [diff] [blame] | 1196 | |
| 1197 | |
| 1198 | def update_party_blanket_order(args, out): |
ronelvcabrera | 370cdc0 | 2019-12-04 18:37:11 +0800 | [diff] [blame] | 1199 | if out["against_blanket_order"]: |
| 1200 | blanket_order_details = get_blanket_order_details(args) |
| 1201 | if blanket_order_details: |
| 1202 | out.update(blanket_order_details) |
Manas Solanki | e5e87f7 | 2018-05-28 20:07:08 +0530 | [diff] [blame] | 1203 | |
| 1204 | @frappe.whitelist() |
| 1205 | def get_blanket_order_details(args): |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 1206 | if isinstance(args, str): |
Manas Solanki | e5e87f7 | 2018-05-28 20:07:08 +0530 | [diff] [blame] | 1207 | args = frappe._dict(json.loads(args)) |
| 1208 | |
| 1209 | blanket_order_details = None |
Nabin Hait | 2737b08 | 2018-06-14 18:07:22 +0530 | [diff] [blame] | 1210 | condition = '' |
Manas Solanki | e5e87f7 | 2018-05-28 20:07:08 +0530 | [diff] [blame] | 1211 | if args.item_code: |
| 1212 | if args.customer and args.doctype == "Sales Order": |
Nabin Hait | 2737b08 | 2018-06-14 18:07:22 +0530 | [diff] [blame] | 1213 | condition = ' and bo.customer=%(customer)s' |
Manas Solanki | e5e87f7 | 2018-05-28 20:07:08 +0530 | [diff] [blame] | 1214 | elif args.supplier and args.doctype == "Purchase Order": |
Nabin Hait | 2737b08 | 2018-06-14 18:07:22 +0530 | [diff] [blame] | 1215 | condition = ' and bo.supplier=%(supplier)s' |
Manas Solanki | e5e87f7 | 2018-05-28 20:07:08 +0530 | [diff] [blame] | 1216 | if args.blanket_order: |
Nabin Hait | 2737b08 | 2018-06-14 18:07:22 +0530 | [diff] [blame] | 1217 | condition += ' and bo.name =%(blanket_order)s' |
| 1218 | if args.transaction_date: |
| 1219 | condition += ' and bo.to_date>=%(transaction_date)s' |
| 1220 | |
Manas Solanki | e5e87f7 | 2018-05-28 20:07:08 +0530 | [diff] [blame] | 1221 | blanket_order_details = frappe.db.sql(''' |
| 1222 | select boi.rate as blanket_order_rate, bo.name as blanket_order |
| 1223 | from `tabBlanket Order` bo, `tabBlanket Order Item` boi |
Nabin Hait | 2737b08 | 2018-06-14 18:07:22 +0530 | [diff] [blame] | 1224 | where bo.company=%(company)s and boi.item_code=%(item_code)s |
| 1225 | and bo.docstatus=1 and bo.name = boi.parent {0} |
| 1226 | '''.format(condition), args, as_dict=True) |
| 1227 | |
Manas Solanki | e5e87f7 | 2018-05-28 20:07:08 +0530 | [diff] [blame] | 1228 | blanket_order_details = blanket_order_details[0] if blanket_order_details else '' |
| 1229 | return blanket_order_details |
Ranjith Kurungadam | d54991d | 2018-08-01 17:47:07 +0530 | [diff] [blame] | 1230 | |
| 1231 | def get_so_reservation_for_item(args): |
| 1232 | reserved_so = None |
| 1233 | if args.get('against_sales_order'): |
| 1234 | if get_reserved_qty_for_so(args.get('against_sales_order'), args.get('item_code')): |
| 1235 | reserved_so = args.get('against_sales_order') |
| 1236 | elif args.get('against_sales_invoice'): |
| 1237 | sales_order = frappe.db.sql("""select sales_order from `tabSales Invoice Item` where |
| 1238 | parent=%s and item_code=%s""", (args.get('against_sales_invoice'), args.get('item_code'))) |
| 1239 | if sales_order and sales_order[0]: |
| 1240 | if get_reserved_qty_for_so(sales_order[0][0], args.get('item_code')): |
| 1241 | reserved_so = sales_order[0] |
| 1242 | elif args.get("sales_order"): |
| 1243 | if get_reserved_qty_for_so(args.get('sales_order'), args.get('item_code')): |
| 1244 | reserved_so = args.get('sales_order') |
| 1245 | return reserved_so |
| 1246 | |
| 1247 | def get_reserved_qty_for_so(sales_order, item_code): |
| 1248 | reserved_qty = frappe.db.sql("""select sum(qty) from `tabSales Order Item` |
| 1249 | where parent=%s and item_code=%s and ensure_delivery_based_on_produced_serial_no=1 |
| 1250 | """, (sales_order, item_code)) |
| 1251 | if reserved_qty and reserved_qty[0][0]: |
| 1252 | return reserved_qty[0][0] |
| 1253 | else: |
| 1254 | return 0 |