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