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