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