Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 3 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 4 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 5 | import frappe |
marination | 293f6cb | 2021-11-16 14:22:26 +0530 | [diff] [blame] | 6 | from frappe import ValidationError, _, msgprint |
Rohit Waghchaure | f4dc716 | 2018-11-15 17:04:02 +0530 | [diff] [blame] | 7 | from frappe.contacts.doctype.address.address import get_address_display |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 8 | from frappe.utils import cint, cstr, flt, getdate |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 9 | |
Rohit Waghchaure | 4d4fb6d | 2018-05-15 22:12:44 +0530 | [diff] [blame] | 10 | from erpnext.accounts.doctype.budget.budget import validate_expense_against_budget |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 11 | from erpnext.accounts.party import get_party_details |
| 12 | from erpnext.buying.utils import update_last_purchase_rate, validate_for_items |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 13 | from erpnext.controllers.sales_and_purchase_return import get_rate_for_return |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 14 | from erpnext.controllers.stock_controller import StockController |
| 15 | from erpnext.controllers.subcontracting import Subcontracting |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 16 | from erpnext.stock.get_item_details import get_conversion_factor |
| 17 | from erpnext.stock.utils import get_incoming_rate |
| 18 | |
marination | 293f6cb | 2021-11-16 14:22:26 +0530 | [diff] [blame] | 19 | |
| 20 | class QtyMismatchError(ValidationError): |
| 21 | pass |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 22 | |
Faris Ansari | bc6a2b2 | 2020-10-27 19:42:58 +0530 | [diff] [blame] | 23 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 24 | class BuyingController(StockController, Subcontracting): |
Nabin Hait | 993c6c0 | 2022-04-12 12:24:47 +0530 | [diff] [blame] | 25 | def __setup__(self): |
| 26 | self.flags.ignore_permlevel_for_fields = ["buying_price_list", "price_list_currency"] |
| 27 | |
Rushabh Mehta | c0bb453 | 2014-09-09 16:15:35 +0530 | [diff] [blame] | 28 | def get_feed(self): |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 29 | if self.get("supplier_name"): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 30 | return _("From {0} | {1} {2}").format(self.supplier_name, self.currency, self.grand_total) |
Rushabh Mehta | c0bb453 | 2014-09-09 16:15:35 +0530 | [diff] [blame] | 31 | |
Saurabh | 6f75318 | 2013-03-20 12:55:28 +0530 | [diff] [blame] | 32 | def validate(self): |
| 33 | super(BuyingController, self).validate() |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 34 | if getattr(self, "supplier", None) and not self.supplier_name: |
Nabin Hait | 1d21842 | 2015-07-17 15:19:02 +0530 | [diff] [blame] | 35 | self.supplier_name = frappe.db.get_value("Supplier", self.supplier, "supplier_name") |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 36 | |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 37 | self.validate_items() |
nabinhait | 614fb75 | 2014-07-14 10:47:50 +0530 | [diff] [blame] | 38 | self.set_qty_as_per_stock_uom() |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 39 | self.validate_stock_or_nonstock_items() |
Anand Doshi | 373680b | 2013-10-10 16:04:40 +0530 | [diff] [blame] | 40 | self.validate_warehouse() |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 41 | self.validate_from_warehouse() |
Rohit Waghchaure | f4dc716 | 2018-11-15 17:04:02 +0530 | [diff] [blame] | 42 | self.set_supplier_address() |
Saqib | 562fd0e | 2020-04-08 12:11:40 +0530 | [diff] [blame] | 43 | self.validate_asset_return() |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 44 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 45 | if self.doctype == "Purchase Invoice": |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 46 | self.validate_purchase_receipt_if_update_stock() |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 47 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 48 | if self.doctype == "Purchase Receipt" or ( |
| 49 | self.doctype == "Purchase Invoice" and self.update_stock |
| 50 | ): |
Rohit Waghchaure | 560ba39 | 2016-08-29 18:19:32 +0530 | [diff] [blame] | 51 | # self.validate_purchase_return() |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 52 | self.validate_rejected_warehouse() |
| 53 | self.validate_accepted_rejected_qty() |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 54 | validate_for_items(self) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 55 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 56 | # sub-contracting |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 57 | self.validate_for_subcontracting() |
| 58 | self.create_raw_materials_supplied("supplied_items") |
| 59 | self.set_landed_cost_voucher_amount() |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 60 | |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 61 | if self.doctype in ("Purchase Receipt", "Purchase Invoice"): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 62 | self.update_valuation_rate() |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 63 | |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 64 | def onload(self): |
| 65 | super(BuyingController, self).onload() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 66 | self.set_onload( |
| 67 | "backflush_based_on", |
| 68 | frappe.db.get_single_value( |
| 69 | "Buying Settings", "backflush_raw_materials_of_subcontract_based_on" |
| 70 | ), |
| 71 | ) |
Rohit Waghchaure | 110e152 | 2021-06-15 17:29:52 +0530 | [diff] [blame] | 72 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 73 | def set_missing_values(self, for_validate=False): |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 74 | super(BuyingController, self).set_missing_values(for_validate) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 75 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 76 | self.set_supplier_from_item_default() |
Nabin Hait | 096d363 | 2013-10-17 17:01:14 +0530 | [diff] [blame] | 77 | self.set_price_list_currency("Buying") |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 78 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 79 | # set contact and address details for supplier, if they are not mentioned |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 80 | if getattr(self, "supplier", None): |
marination | 78b6b29 | 2022-01-24 13:39:10 +0530 | [diff] [blame] | 81 | self.update_if_missing( |
| 82 | get_party_details( |
| 83 | self.supplier, |
| 84 | party_type="Supplier", |
| 85 | doctype=self.doctype, |
| 86 | company=self.company, |
| 87 | party_address=self.get("supplier_address"), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 88 | shipping_address=self.get("shipping_address"), |
| 89 | fetch_payment_terms_template=not self.get("ignore_default_payment_terms_template"), |
| 90 | ignore_permissions=self.flags.ignore_permissions, |
marination | 78b6b29 | 2022-01-24 13:39:10 +0530 | [diff] [blame] | 91 | ) |
| 92 | ) |
Rushabh Mehta | 886c9ef | 2013-07-08 12:08:06 +0530 | [diff] [blame] | 93 | |
Nabin Hait | cccc45e | 2016-10-05 17:15:43 +0530 | [diff] [blame] | 94 | self.set_missing_item_details(for_validate) |
Rushabh Mehta | 436467a | 2013-07-08 12:37:59 +0530 | [diff] [blame] | 95 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 96 | def set_supplier_from_item_default(self): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 97 | if self.meta.get_field("supplier") and not self.supplier: |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 98 | for d in self.get("items"): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 99 | supplier = frappe.db.get_value( |
| 100 | "Item Default", {"parent": d.item_code, "company": self.company}, "default_supplier" |
| 101 | ) |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 102 | if supplier: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 103 | self.supplier = supplier |
Shreya Shah | 3c9839f | 2018-07-17 18:01:44 +0530 | [diff] [blame] | 104 | else: |
| 105 | item_group = frappe.db.get_value("Item", d.item_code, "item_group") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 106 | supplier = frappe.db.get_value( |
| 107 | "Item Default", {"parent": item_group, "company": self.company}, "default_supplier" |
| 108 | ) |
Shreya Shah | 3c9839f | 2018-07-17 18:01:44 +0530 | [diff] [blame] | 109 | if supplier: |
| 110 | self.supplier = supplier |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 111 | break |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 112 | |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 113 | def validate_stock_or_nonstock_items(self): |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 114 | if self.meta.get_field("taxes") and not self.get_stock_items() and not self.get_asset_items(): |
Deepesh Garg | f17ea2c | 2020-12-11 21:30:39 +0530 | [diff] [blame] | 115 | msg = _('Tax Category has been changed to "Total" because all the Items are non-stock items') |
| 116 | self.update_tax_category(msg) |
| 117 | |
Deepesh Garg | f17ea2c | 2020-12-11 21:30:39 +0530 | [diff] [blame] | 118 | def update_tax_category(self, msg): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 119 | tax_for_valuation = [ |
| 120 | d for d in self.get("taxes") if d.category in ["Valuation", "Valuation and Total"] |
| 121 | ] |
tundebabzy | 9a34620 | 2017-06-16 11:00:14 +0100 | [diff] [blame] | 122 | |
Deepesh Garg | f17ea2c | 2020-12-11 21:30:39 +0530 | [diff] [blame] | 123 | if tax_for_valuation: |
| 124 | for d in tax_for_valuation: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 125 | d.category = "Total" |
Deepesh Garg | f17ea2c | 2020-12-11 21:30:39 +0530 | [diff] [blame] | 126 | |
| 127 | msgprint(msg) |
Marica | 1c8cbd0 | 2020-05-02 17:55:15 +0530 | [diff] [blame] | 128 | |
Saqib | 562fd0e | 2020-04-08 12:11:40 +0530 | [diff] [blame] | 129 | def validate_asset_return(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 130 | if self.doctype not in ["Purchase Receipt", "Purchase Invoice"] or not self.is_return: |
Saqib | 562fd0e | 2020-04-08 12:11:40 +0530 | [diff] [blame] | 131 | return |
| 132 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 133 | purchase_doc_field = ( |
| 134 | "purchase_receipt" if self.doctype == "Purchase Receipt" else "purchase_invoice" |
| 135 | ) |
| 136 | not_cancelled_asset = [ |
| 137 | d.name |
| 138 | for d in frappe.db.get_all("Asset", {purchase_doc_field: self.return_against, "docstatus": 1}) |
| 139 | ] |
Saqib | 562fd0e | 2020-04-08 12:11:40 +0530 | [diff] [blame] | 140 | if self.is_return and len(not_cancelled_asset): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 141 | frappe.throw( |
| 142 | _( |
| 143 | "{} has submitted assets linked to it. You need to cancel the assets to create purchase return." |
| 144 | ).format(self.return_against), |
| 145 | title=_("Not Allowed"), |
| 146 | ) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 147 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 148 | def get_asset_items(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 149 | if self.doctype not in ["Purchase Order", "Purchase Invoice", "Purchase Receipt"]: |
Rohit Waghchaure | d644e6d | 2018-05-12 15:27:18 +0530 | [diff] [blame] | 150 | return [] |
| 151 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 152 | return [d.item_code for d in self.items if d.is_fixed_asset] |
| 153 | |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 154 | def set_landed_cost_voucher_amount(self): |
| 155 | for d in self.get("items"): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 156 | lc_voucher_data = frappe.db.sql( |
| 157 | """select sum(applicable_charges), cost_center |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 158 | from `tabLanded Cost Item` |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 159 | where docstatus = 1 and purchase_receipt_item = %s""", |
| 160 | d.name, |
| 161 | ) |
rohitwaghchaure | 2e8232e | 2017-08-10 11:32:59 +0530 | [diff] [blame] | 162 | d.landed_cost_voucher_amount = lc_voucher_data[0][0] if lc_voucher_data else 0.0 |
| 163 | if not d.cost_center and lc_voucher_data and lc_voucher_data[0][1]: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 164 | d.db_set("cost_center", lc_voucher_data[0][1]) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 165 | |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 166 | def validate_from_warehouse(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 167 | for item in self.get("items"): |
| 168 | if item.get("from_warehouse") and (item.get("from_warehouse") == item.get("warehouse")): |
| 169 | frappe.throw( |
| 170 | _("Row #{0}: Accepted Warehouse and Supplier Warehouse cannot be same").format(item.idx) |
| 171 | ) |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 172 | |
Sagar Sharma | d074c93 | 2022-03-31 19:57:42 +0530 | [diff] [blame] | 173 | if item.get("from_warehouse") and self.get("is_subcontracted"): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 174 | frappe.throw( |
| 175 | _( |
| 176 | "Row #{0}: Cannot select Supplier Warehouse while suppling raw materials to subcontractor" |
| 177 | ).format(item.idx) |
| 178 | ) |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 179 | |
Rohit Waghchaure | f4dc716 | 2018-11-15 17:04:02 +0530 | [diff] [blame] | 180 | def set_supplier_address(self): |
| 181 | address_dict = { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 182 | "supplier_address": "address_display", |
| 183 | "shipping_address": "shipping_address_display", |
Rohit Waghchaure | f4dc716 | 2018-11-15 17:04:02 +0530 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | for address_field, address_display_field in address_dict.items(): |
| 187 | if self.get(address_field): |
| 188 | self.set(address_display_field, get_address_display(self.get(address_field))) |
| 189 | |
Nabin Hait | d3b6250 | 2013-01-21 17:24:31 +0530 | [diff] [blame] | 190 | def set_total_in_words(self): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 191 | from frappe.utils import money_in_words |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 192 | |
Nabin Hait | 5690be1 | 2015-02-12 16:09:11 +0530 | [diff] [blame] | 193 | if self.meta.get_field("base_in_words"): |
Nabin Hait | 6b539d6 | 2018-08-29 12:41:12 +0530 | [diff] [blame] | 194 | if self.meta.get_field("base_rounded_total") and not self.is_rounded_total_disabled(): |
| 195 | amount = self.base_rounded_total |
| 196 | else: |
| 197 | amount = self.base_grand_total |
rohitwaghchaure | ae4ff5a | 2018-08-01 18:09:51 +0530 | [diff] [blame] | 198 | self.base_in_words = money_in_words(amount, self.company_currency) |
| 199 | |
Nabin Hait | 5b4c294 | 2013-01-22 11:12:02 +0530 | [diff] [blame] | 200 | if self.meta.get_field("in_words"): |
Nabin Hait | 6b539d6 | 2018-08-29 12:41:12 +0530 | [diff] [blame] | 201 | if self.meta.get_field("rounded_total") and not self.is_rounded_total_disabled(): |
| 202 | amount = self.rounded_total |
| 203 | else: |
| 204 | amount = self.grand_total |
rohitwaghchaure | ae4ff5a | 2018-08-01 18:09:51 +0530 | [diff] [blame] | 205 | |
| 206 | self.in_words = money_in_words(amount, self.currency) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 207 | |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 208 | # update valuation rate |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 209 | def update_valuation_rate(self, reset_outgoing_rate=True): |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 210 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 211 | item_tax_amount is the total tax amount applied on that item |
| 212 | stored for valuation |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 213 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 214 | TODO: rename item_tax_amount to valuation_tax_amount |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 215 | """ |
Rohit Waghchaure | 4d81d45 | 2021-06-15 10:21:44 +0530 | [diff] [blame] | 216 | stock_and_asset_items = [] |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 217 | stock_and_asset_items = self.get_stock_items() + self.get_asset_items() |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 218 | |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 219 | stock_and_asset_items_qty, stock_and_asset_items_amount = 0, 0 |
| 220 | last_item_idx = 1 |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 221 | for d in self.get("items"): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 222 | if d.item_code and d.item_code in stock_and_asset_items: |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 223 | stock_and_asset_items_qty += flt(d.qty) |
| 224 | stock_and_asset_items_amount += flt(d.base_net_amount) |
| 225 | last_item_idx = d.idx |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 226 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 227 | total_valuation_amount = sum( |
| 228 | flt(d.base_tax_amount_after_discount_amount) |
| 229 | for d in self.get("taxes") |
| 230 | if d.category in ["Valuation", "Valuation and Total"] |
| 231 | ) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 232 | |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 233 | valuation_amount_adjustment = total_valuation_amount |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 234 | for i, item in enumerate(self.get("items")): |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 235 | if item.item_code and item.qty and item.item_code in stock_and_asset_items: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 236 | item_proportion = ( |
| 237 | flt(item.base_net_amount) / stock_and_asset_items_amount |
| 238 | if stock_and_asset_items_amount |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 239 | else flt(item.qty) / stock_and_asset_items_qty |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 240 | ) |
Suraj Shetty | 48e9bc3 | 2020-01-29 15:06:18 +0530 | [diff] [blame] | 241 | |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 242 | if i == (last_item_idx - 1): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 243 | item.item_tax_amount = flt( |
| 244 | valuation_amount_adjustment, self.precision("item_tax_amount", item) |
| 245 | ) |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 246 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 247 | item.item_tax_amount = flt( |
| 248 | item_proportion * total_valuation_amount, self.precision("item_tax_amount", item) |
| 249 | ) |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 250 | valuation_amount_adjustment -= item.item_tax_amount |
| 251 | |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 252 | self.round_floats_in(item) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 253 | if flt(item.conversion_factor) == 0.0: |
| 254 | item.conversion_factor = ( |
| 255 | get_conversion_factor(item.item_code, item.uom).get("conversion_factor") or 1.0 |
| 256 | ) |
Rushabh Mehta | 724f9e5 | 2014-10-07 15:29:58 +0530 | [diff] [blame] | 257 | |
Rushabh Mehta | 5404778 | 2013-12-26 11:07:46 +0530 | [diff] [blame] | 258 | qty_in_stock_uom = flt(item.qty * item.conversion_factor) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 259 | item.rm_supp_cost = self.get_supplied_items_cost(item.name, reset_outgoing_rate) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 260 | item.valuation_rate = ( |
| 261 | item.base_net_amount |
| 262 | + item.item_tax_amount |
| 263 | + item.rm_supp_cost |
| 264 | + flt(item.landed_cost_voucher_amount) |
| 265 | ) / qty_in_stock_uom |
Anand Doshi | 4a7248e | 2013-02-27 18:10:30 +0530 | [diff] [blame] | 266 | else: |
Anand Doshi | 5af812a | 2013-05-10 19:23:02 +0530 | [diff] [blame] | 267 | item.valuation_rate = 0.0 |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 268 | |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 269 | def set_incoming_rate(self): |
| 270 | if self.doctype not in ("Purchase Receipt", "Purchase Invoice", "Purchase Order"): |
| 271 | return |
| 272 | |
| 273 | ref_doctype_map = { |
| 274 | "Purchase Order": "Sales Order Item", |
| 275 | "Purchase Receipt": "Delivery Note Item", |
| 276 | "Purchase Invoice": "Sales Invoice Item", |
| 277 | } |
| 278 | |
| 279 | ref_doctype = ref_doctype_map.get(self.doctype) |
| 280 | items = self.get("items") |
| 281 | for d in items: |
| 282 | if not cint(self.get("is_return")): |
| 283 | # Get outgoing rate based on original item cost based on valuation method |
| 284 | |
| 285 | if not d.get(frappe.scrub(ref_doctype)): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 286 | outgoing_rate = get_incoming_rate( |
| 287 | { |
| 288 | "item_code": d.item_code, |
| 289 | "warehouse": d.get("from_warehouse"), |
| 290 | "posting_date": self.get("posting_date") or self.get("transation_date"), |
| 291 | "posting_time": self.get("posting_time"), |
| 292 | "qty": -1 * flt(d.get("stock_qty")), |
| 293 | "serial_no": d.get("serial_no"), |
| 294 | "batch_no": d.get("batch_no"), |
| 295 | "company": self.company, |
| 296 | "voucher_type": self.doctype, |
| 297 | "voucher_no": self.name, |
| 298 | "allow_zero_valuation": d.get("allow_zero_valuation"), |
| 299 | }, |
| 300 | raise_error_if_no_rate=False, |
| 301 | ) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 302 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 303 | rate = flt(outgoing_rate * d.conversion_factor, d.precision("rate")) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 304 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 305 | rate = frappe.db.get_value(ref_doctype, d.get(frappe.scrub(ref_doctype)), "rate") |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 306 | |
| 307 | if self.is_internal_transfer(): |
| 308 | if rate != d.rate: |
| 309 | d.rate = rate |
| 310 | d.discount_percentage = 0 |
| 311 | d.discount_amount = 0 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 312 | frappe.msgprint( |
| 313 | _( |
| 314 | "Row {0}: Item rate has been updated as per valuation rate since its an internal stock transfer" |
| 315 | ).format(d.idx), |
| 316 | alert=1, |
| 317 | ) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 318 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 319 | def get_supplied_items_cost(self, item_row_id, reset_outgoing_rate=True): |
| 320 | supplied_items_cost = 0.0 |
| 321 | for d in self.get("supplied_items"): |
| 322 | if d.reference_name == item_row_id: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 323 | if reset_outgoing_rate and frappe.get_cached_value("Item", d.rm_item_code, "is_stock_item"): |
| 324 | rate = get_incoming_rate( |
| 325 | { |
| 326 | "item_code": d.rm_item_code, |
| 327 | "warehouse": self.supplier_warehouse, |
| 328 | "posting_date": self.posting_date, |
| 329 | "posting_time": self.posting_time, |
| 330 | "qty": -1 * d.consumed_qty, |
| 331 | "serial_no": d.serial_no, |
| 332 | "batch_no": d.batch_no, |
| 333 | } |
| 334 | ) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 335 | |
| 336 | if rate > 0: |
| 337 | d.rate = rate |
| 338 | |
Nabin Hait | b99c77b | 2020-12-25 18:12:35 +0530 | [diff] [blame] | 339 | d.amount = flt(flt(d.consumed_qty) * flt(d.rate), d.precision("amount")) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 340 | supplied_items_cost += flt(d.amount) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 341 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 342 | return supplied_items_cost |
| 343 | |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 344 | def validate_for_subcontracting(self): |
Sagar Sharma | d074c93 | 2022-03-31 19:57:42 +0530 | [diff] [blame] | 345 | if self.is_subcontracted: |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 346 | if self.doctype in ["Purchase Receipt", "Purchase Invoice"] and not self.supplier_warehouse: |
Afshan | 27bcb2a | 2021-03-09 22:25:48 +0530 | [diff] [blame] | 347 | frappe.throw(_("Supplier Warehouse mandatory for sub-contracted {0}").format(self.doctype)) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 348 | |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 349 | for item in self.get("items"): |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 350 | if item in self.sub_contracted_items and not item.bom: |
| 351 | frappe.throw(_("Please select BOM in BOM field for Item {0}").format(item.item_code)) |
| 352 | |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 353 | if self.doctype != "Purchase Order": |
| 354 | return |
pawan | 54465f5 | 2017-11-29 10:18:38 +0530 | [diff] [blame] | 355 | |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 356 | for row in self.get("supplied_items"): |
| 357 | if not row.reserve_warehouse: |
| 358 | msg = f"Reserved Warehouse is mandatory for the Item {frappe.bold(row.rm_item_code)} in Raw Materials supplied" |
| 359 | frappe.throw(_(msg)) |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 360 | else: |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 361 | for item in self.get("items"): |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 362 | if item.bom: |
| 363 | item.bom = None |
| 364 | |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 365 | def create_raw_materials_supplied(self, raw_material_table): |
Sagar Sharma | d074c93 | 2022-03-31 19:57:42 +0530 | [diff] [blame] | 366 | if self.is_subcontracted: |
Rohit Waghchaure | 7b7ceaa | 2021-05-24 20:11:15 +0530 | [diff] [blame] | 367 | self.set_materials_for_subcontracted_items(raw_material_table) |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 368 | |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 369 | elif self.doctype in ["Purchase Receipt", "Purchase Invoice"]: |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 370 | for item in self.get("items"): |
Nabin Hait | a0c239d | 2014-04-01 18:54:38 +0530 | [diff] [blame] | 371 | item.rm_supp_cost = 0.0 |
| 372 | |
Sagar Sharma | d074c93 | 2022-03-31 19:57:42 +0530 | [diff] [blame] | 373 | if not self.is_subcontracted and self.get("supplied_items"): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 374 | self.set("supplied_items", []) |
Rohit Waghchaure | 4b9d2f2 | 2017-02-16 15:53:40 +0530 | [diff] [blame] | 375 | |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 376 | @property |
| 377 | def sub_contracted_items(self): |
| 378 | if not hasattr(self, "_sub_contracted_items"): |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 379 | self._sub_contracted_items = [] |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 380 | item_codes = list(set(item.item_code for item in self.get("items"))) |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 381 | if item_codes: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 382 | items = frappe.get_all( |
| 383 | "Item", filters={"name": ["in", item_codes], "is_sub_contracted_item": 1} |
| 384 | ) |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 385 | self._sub_contracted_items = [item.name for item in items] |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 386 | |
| 387 | return self._sub_contracted_items |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 388 | |
nabinhait | 614fb75 | 2014-07-14 10:47:50 +0530 | [diff] [blame] | 389 | def set_qty_as_per_stock_uom(self): |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 390 | for d in self.get("items"): |
Nabin Hait | fd4bcd8 | 2015-05-22 16:55:40 +0530 | [diff] [blame] | 391 | if d.meta.get_field("stock_qty"): |
Deepesh Garg | 8b0302b | 2019-08-05 10:14:19 +0530 | [diff] [blame] | 392 | # Check if item code is present |
| 393 | # Conversion factor should not be mandatory for non itemized items |
| 394 | if not d.conversion_factor and d.item_code: |
Nabin Hait | e6e456b | 2015-05-13 17:21:44 +0530 | [diff] [blame] | 395 | frappe.throw(_("Row {0}: Conversion Factor is mandatory").format(d.idx)) |
Rushabh Mehta | 5b51cc8 | 2014-07-21 18:25:45 +0530 | [diff] [blame] | 396 | d.stock_qty = flt(d.qty) * flt(d.conversion_factor) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 397 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 398 | if self.doctype == "Purchase Receipt" and d.meta.get_field("received_stock_qty"): |
marination | d6596a1 | 2020-11-02 15:07:48 +0530 | [diff] [blame] | 399 | # Set Received Qty in Stock UOM |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 400 | d.received_stock_qty = flt(d.received_qty) * flt( |
| 401 | d.conversion_factor, d.precision("conversion_factor") |
| 402 | ) |
marination | d6596a1 | 2020-11-02 15:07:48 +0530 | [diff] [blame] | 403 | |
Saurabh | 130c57b | 2016-04-04 15:49:36 +0530 | [diff] [blame] | 404 | def validate_purchase_return(self): |
| 405 | for d in self.get("items"): |
| 406 | if self.is_return and flt(d.rejected_qty) != 0: |
| 407 | frappe.throw(_("Row #{0}: Rejected Qty can not be entered in Purchase Return").format(d.idx)) |
| 408 | |
| 409 | # validate rate with ref PR |
| 410 | |
| 411 | def validate_rejected_warehouse(self): |
| 412 | for d in self.get("items"): |
| 413 | if flt(d.rejected_qty) and not d.rejected_warehouse: |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 414 | if self.rejected_warehouse: |
| 415 | d.rejected_warehouse = self.rejected_warehouse |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 416 | |
Saurabh | 130c57b | 2016-04-04 15:49:36 +0530 | [diff] [blame] | 417 | if not d.rejected_warehouse: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 418 | frappe.throw( |
| 419 | _("Row #{0}: Rejected Warehouse is mandatory against rejected Item {1}").format( |
| 420 | d.idx, d.item_code |
| 421 | ) |
| 422 | ) |
Saurabh | 130c57b | 2016-04-04 15:49:36 +0530 | [diff] [blame] | 423 | |
| 424 | # validate accepted and rejected qty |
| 425 | def validate_accepted_rejected_qty(self): |
| 426 | for d in self.get("items"): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 427 | self.validate_negative_quantity(d, ["received_qty", "qty", "rejected_qty"]) |
Saurabh | 130c57b | 2016-04-04 15:49:36 +0530 | [diff] [blame] | 428 | |
marination | 5fb5a75 | 2021-11-08 13:22:57 +0530 | [diff] [blame] | 429 | if not flt(d.received_qty) and (flt(d.qty) or flt(d.rejected_qty)): |
| 430 | d.received_qty = flt(d.qty) + flt(d.rejected_qty) |
Saurabh | 130c57b | 2016-04-04 15:49:36 +0530 | [diff] [blame] | 431 | |
Saurabh | 130c57b | 2016-04-04 15:49:36 +0530 | [diff] [blame] | 432 | # Check Received Qty = Accepted Qty + Rejected Qty |
marination | afe1c45 | 2021-11-15 17:17:29 +0530 | [diff] [blame] | 433 | val = flt(d.qty) + flt(d.rejected_qty) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 434 | if flt(val, d.precision("received_qty")) != flt(d.received_qty, d.precision("received_qty")): |
| 435 | message = _( |
| 436 | "Row #{0}: Received Qty must be equal to Accepted + Rejected Qty for Item {1}" |
| 437 | ).format(d.idx, d.item_code) |
marination | 05ec7cc | 2021-11-15 17:49:14 +0530 | [diff] [blame] | 438 | frappe.throw(msg=message, title=_("Mismatch"), exc=QtyMismatchError) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 439 | |
Saurabh | 4f4d0a8 | 2017-03-13 14:31:48 +0530 | [diff] [blame] | 440 | def validate_negative_quantity(self, item_row, field_list): |
| 441 | if self.is_return: |
| 442 | return |
| 443 | |
| 444 | item_row = item_row.as_dict() |
| 445 | for fieldname in field_list: |
| 446 | if flt(item_row[fieldname]) < 0: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 447 | frappe.throw( |
| 448 | _("Row #{0}: {1} can not be negative for item {2}").format( |
| 449 | item_row["idx"], |
| 450 | frappe.get_meta(item_row.doctype).get_label(fieldname), |
| 451 | item_row["item_code"], |
| 452 | ) |
| 453 | ) |
Saurabh | 4f4d0a8 | 2017-03-13 14:31:48 +0530 | [diff] [blame] | 454 | |
Mangesh-Khairnar | 5e474f4 | 2019-03-06 14:46:38 +0530 | [diff] [blame] | 455 | def check_for_on_hold_or_closed_status(self, ref_doctype, ref_fieldname): |
| 456 | for d in self.get("items"): |
| 457 | if d.get(ref_fieldname): |
| 458 | status = frappe.db.get_value(ref_doctype, d.get(ref_fieldname), "status") |
| 459 | if status in ("Closed", "On Hold"): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 460 | frappe.throw(_("{0} {1} is {2}").format(ref_doctype, d.get(ref_fieldname), status)) |
Mangesh-Khairnar | 5e474f4 | 2019-03-06 14:46:38 +0530 | [diff] [blame] | 461 | |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 462 | def update_stock_ledger(self, allow_negative_stock=False, via_landed_cost_voucher=False): |
Nabin Hait | 07e5376 | 2018-01-05 18:19:59 +0530 | [diff] [blame] | 463 | self.update_ordered_and_reserved_qty() |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 464 | |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 465 | sl_entries = [] |
| 466 | stock_items = self.get_stock_items() |
| 467 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 468 | for d in self.get("items"): |
Rohit Waghchaure | 0a71cab | 2022-04-04 15:10:57 +0530 | [diff] [blame] | 469 | if d.item_code not in stock_items: |
| 470 | continue |
| 471 | |
Rohit Waghchaure | ac5df1a | 2022-04-04 15:38:42 +0530 | [diff] [blame] | 472 | if d.warehouse: |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 473 | pr_qty = flt(d.qty) * flt(d.conversion_factor) |
| 474 | |
| 475 | if pr_qty: |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 476 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 477 | if d.from_warehouse and ( |
| 478 | (not cint(self.is_return) and self.docstatus == 1) |
| 479 | or (cint(self.is_return) and self.docstatus == 2) |
| 480 | ): |
| 481 | from_warehouse_sle = self.get_sl_entries( |
| 482 | d, |
| 483 | { |
| 484 | "actual_qty": -1 * pr_qty, |
| 485 | "warehouse": d.from_warehouse, |
| 486 | "outgoing_rate": d.rate, |
| 487 | "recalculate_rate": 1, |
| 488 | "dependant_sle_voucher_detail_no": d.name, |
| 489 | }, |
| 490 | ) |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 491 | |
| 492 | sl_entries.append(from_warehouse_sle) |
| 493 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 494 | sle = self.get_sl_entries( |
| 495 | d, {"actual_qty": flt(pr_qty), "serial_no": cstr(d.serial_no).strip()} |
| 496 | ) |
Rohit Waghchaure | 0a71cab | 2022-04-04 15:10:57 +0530 | [diff] [blame] | 497 | |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 498 | if self.is_return: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 499 | outgoing_rate = get_rate_for_return( |
| 500 | self.doctype, self.name, d.item_code, self.return_against, item_row=d |
| 501 | ) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 502 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 503 | sle.update({"outgoing_rate": outgoing_rate, "recalculate_rate": 1}) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 504 | if d.from_warehouse: |
| 505 | sle.dependant_sle_voucher_detail_no = d.name |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 506 | else: |
Nabin Hait | b81ed45 | 2016-05-11 12:52:31 +0530 | [diff] [blame] | 507 | val_rate_db_precision = 6 if cint(self.precision("valuation_rate", d)) <= 6 else 9 |
| 508 | incoming_rate = flt(d.valuation_rate, val_rate_db_precision) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 509 | sle.update( |
| 510 | { |
| 511 | "incoming_rate": incoming_rate, |
| 512 | "recalculate_rate": 1 if (self.is_subcontracted and d.bom) or d.from_warehouse else 0, |
| 513 | } |
| 514 | ) |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 515 | sl_entries.append(sle) |
| 516 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 517 | if d.from_warehouse and ( |
| 518 | (not cint(self.is_return) and self.docstatus == 2) |
| 519 | or (cint(self.is_return) and self.docstatus == 1) |
| 520 | ): |
| 521 | from_warehouse_sle = self.get_sl_entries( |
| 522 | d, {"actual_qty": -1 * pr_qty, "warehouse": d.from_warehouse, "recalculate_rate": 1} |
| 523 | ) |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 524 | |
| 525 | sl_entries.append(from_warehouse_sle) |
| 526 | |
Rohit Waghchaure | 0a71cab | 2022-04-04 15:10:57 +0530 | [diff] [blame] | 527 | if flt(d.rejected_qty) != 0: |
| 528 | sl_entries.append( |
| 529 | self.get_sl_entries( |
| 530 | d, |
| 531 | { |
| 532 | "warehouse": d.rejected_warehouse, |
| 533 | "actual_qty": flt(d.rejected_qty) * flt(d.conversion_factor), |
| 534 | "serial_no": cstr(d.rejected_serial_no).strip(), |
| 535 | "incoming_rate": 0.0, |
| 536 | }, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 537 | ) |
Rohit Waghchaure | 0a71cab | 2022-04-04 15:10:57 +0530 | [diff] [blame] | 538 | ) |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 539 | |
| 540 | self.make_sl_entries_for_supplier_warehouse(sl_entries) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 541 | self.make_sl_entries( |
| 542 | sl_entries, |
| 543 | allow_negative_stock=allow_negative_stock, |
| 544 | via_landed_cost_voucher=via_landed_cost_voucher, |
| 545 | ) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 546 | |
Nabin Hait | 07e5376 | 2018-01-05 18:19:59 +0530 | [diff] [blame] | 547 | def update_ordered_and_reserved_qty(self): |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 548 | po_map = {} |
| 549 | for d in self.get("items"): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 550 | if self.doctype == "Purchase Receipt" and d.purchase_order: |
| 551 | po_map.setdefault(d.purchase_order, []).append(d.purchase_order_item) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 552 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 553 | elif self.doctype == "Purchase Invoice" and d.purchase_order and d.po_detail: |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 554 | po_map.setdefault(d.purchase_order, []).append(d.po_detail) |
| 555 | |
| 556 | for po, po_item_rows in po_map.items(): |
| 557 | if po and po_item_rows: |
| 558 | po_obj = frappe.get_doc("Purchase Order", po) |
| 559 | |
| 560 | if po_obj.status in ["Closed", "Cancelled"]: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 561 | frappe.throw( |
| 562 | _("{0} {1} is cancelled or closed").format(_("Purchase Order"), po), |
| 563 | frappe.InvalidStatusError, |
| 564 | ) |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 565 | |
| 566 | po_obj.update_ordered_qty(po_item_rows) |
Nabin Hait | 07e5376 | 2018-01-05 18:19:59 +0530 | [diff] [blame] | 567 | if self.is_subcontracted: |
| 568 | po_obj.update_reserved_qty_for_subcontract() |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 569 | |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 570 | def make_sl_entries_for_supplier_warehouse(self, sl_entries): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 571 | if hasattr(self, "supplied_items"): |
| 572 | for d in self.get("supplied_items"): |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 573 | # negative quantity is passed, as raw material qty has to be decreased |
| 574 | # when PR is submitted and it has to be increased when PR is cancelled |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 575 | sl_entries.append( |
| 576 | self.get_sl_entries( |
| 577 | d, |
| 578 | { |
| 579 | "item_code": d.rm_item_code, |
| 580 | "warehouse": self.supplier_warehouse, |
| 581 | "actual_qty": -1 * flt(d.consumed_qty), |
| 582 | "dependant_sle_voucher_detail_no": d.reference_name, |
| 583 | }, |
| 584 | ) |
| 585 | ) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 586 | |
rohitwaghchaure | fe22686 | 2017-12-28 16:11:27 +0530 | [diff] [blame] | 587 | def on_submit(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 588 | if self.get("is_return"): |
rohitwaghchaure | fe22686 | 2017-12-28 16:11:27 +0530 | [diff] [blame] | 589 | return |
| 590 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 591 | if self.doctype in ["Purchase Receipt", "Purchase Invoice"]: |
| 592 | field = "purchase_invoice" if self.doctype == "Purchase Invoice" else "purchase_receipt" |
Rohit Waghchaure | c6deb13 | 2018-05-07 15:58:41 +0530 | [diff] [blame] | 593 | |
| 594 | self.process_fixed_asset() |
| 595 | self.update_fixed_asset(field) |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 596 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 597 | if self.doctype in ["Purchase Order", "Purchase Receipt"]: |
| 598 | update_last_purchase_rate(self, is_submit=1) |
rohitwaghchaure | fe22686 | 2017-12-28 16:11:27 +0530 | [diff] [blame] | 599 | |
| 600 | def on_cancel(self): |
Rohit Waghchaure | 5fa9a7a | 2019-04-01 00:40:38 +0530 | [diff] [blame] | 601 | super(BuyingController, self).on_cancel() |
| 602 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 603 | if self.get("is_return"): |
rohitwaghchaure | fe22686 | 2017-12-28 16:11:27 +0530 | [diff] [blame] | 604 | return |
| 605 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 606 | if self.doctype in ["Purchase Order", "Purchase Receipt"]: |
| 607 | update_last_purchase_rate(self, is_submit=0) |
Rohit Waghchaure | 4d81d45 | 2021-06-15 10:21:44 +0530 | [diff] [blame] | 608 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 609 | if self.doctype in ["Purchase Receipt", "Purchase Invoice"]: |
| 610 | field = "purchase_invoice" if self.doctype == "Purchase Invoice" else "purchase_receipt" |
Rohit Waghchaure | c6deb13 | 2018-05-07 15:58:41 +0530 | [diff] [blame] | 611 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 612 | self.delete_linked_asset() |
| 613 | self.update_fixed_asset(field, delete_asset=True) |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 614 | |
Rohit Waghchaure | 4d4fb6d | 2018-05-15 22:12:44 +0530 | [diff] [blame] | 615 | def validate_budget(self): |
| 616 | if self.docstatus == 1: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 617 | for data in self.get("items"): |
rohitwaghchaure | 77a45b4 | 2018-06-15 18:03:31 +0530 | [diff] [blame] | 618 | args = data.as_dict() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 619 | args.update( |
| 620 | { |
| 621 | "doctype": self.doctype, |
| 622 | "company": self.company, |
| 623 | "posting_date": ( |
| 624 | self.schedule_date if self.doctype == "Material Request" else self.transaction_date |
| 625 | ), |
| 626 | } |
| 627 | ) |
rohitwaghchaure | 77a45b4 | 2018-06-15 18:03:31 +0530 | [diff] [blame] | 628 | |
| 629 | validate_expense_against_budget(args) |
Rohit Waghchaure | 4d4fb6d | 2018-05-15 22:12:44 +0530 | [diff] [blame] | 630 | |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 631 | def process_fixed_asset(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 632 | if self.doctype == "Purchase Invoice" and not self.update_stock: |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 633 | return |
| 634 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 635 | asset_items = self.get_asset_items() |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 636 | if asset_items: |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 637 | self.auto_make_assets(asset_items) |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 638 | |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 639 | def auto_make_assets(self, asset_items): |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 640 | items_data = get_asset_item_details(asset_items) |
Saqib | f37a46e | 2019-11-22 16:32:50 +0530 | [diff] [blame] | 641 | messages = [] |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 642 | |
| 643 | for d in self.items: |
| 644 | if d.is_fixed_asset: |
| 645 | item_data = items_data.get(d.item_code) |
| 646 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 647 | if item_data.get("auto_create_assets"): |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 648 | # If asset has to be auto created |
| 649 | # Check for asset naming series |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 650 | if item_data.get("asset_naming_series"): |
Saqib | bba78f0 | 2020-03-31 10:49:44 +0530 | [diff] [blame] | 651 | created_assets = [] |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 652 | if item_data.get("is_grouped_asset"): |
Saqib Ansari | 35a6b58 | 2022-01-18 13:10:23 +0530 | [diff] [blame] | 653 | asset = self.make_asset(d, is_grouped_asset=True) |
Saqib | bba78f0 | 2020-03-31 10:49:44 +0530 | [diff] [blame] | 654 | created_assets.append(asset) |
Saqib Ansari | 35a6b58 | 2022-01-18 13:10:23 +0530 | [diff] [blame] | 655 | else: |
| 656 | for qty in range(cint(d.qty)): |
| 657 | asset = self.make_asset(d) |
| 658 | created_assets.append(asset) |
Marica | 1c8cbd0 | 2020-05-02 17:55:15 +0530 | [diff] [blame] | 659 | |
Saqib | bba78f0 | 2020-03-31 10:49:44 +0530 | [diff] [blame] | 660 | if len(created_assets) > 5: |
| 661 | # dont show asset form links if more than 5 assets are created |
Saqib | bba78f0 | 2020-03-31 10:49:44 +0530 | [diff] [blame] | 662 | messages.append( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 663 | _("{} Assets created for {}").format(len(created_assets), frappe.bold(d.item_code)) |
| 664 | ) |
| 665 | else: |
| 666 | assets_link = list(map(lambda d: frappe.utils.get_link_to_form("Asset", d), created_assets)) |
| 667 | assets_link = frappe.bold(",".join(assets_link)) |
| 668 | |
| 669 | is_plural = "s" if len(created_assets) != 1 else "" |
| 670 | messages.append( |
| 671 | _("Asset{} {assets_link} created for {}").format( |
| 672 | is_plural, frappe.bold(d.item_code), assets_link=assets_link |
| 673 | ) |
Saqib | bba78f0 | 2020-03-31 10:49:44 +0530 | [diff] [blame] | 674 | ) |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 675 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 676 | frappe.throw( |
| 677 | _("Row {}: Asset Naming Series is mandatory for the auto creation for item {}").format( |
| 678 | d.idx, frappe.bold(d.item_code) |
| 679 | ) |
| 680 | ) |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 681 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 682 | messages.append( |
| 683 | _("Assets not created for {0}. You will have to create asset manually.").format( |
| 684 | frappe.bold(d.item_code) |
| 685 | ) |
| 686 | ) |
Saqib | f37a46e | 2019-11-22 16:32:50 +0530 | [diff] [blame] | 687 | |
| 688 | for message in messages: |
Saqib | bba78f0 | 2020-03-31 10:49:44 +0530 | [diff] [blame] | 689 | frappe.msgprint(message, title="Success", indicator="green") |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 690 | |
Saqib Ansari | 35a6b58 | 2022-01-18 13:10:23 +0530 | [diff] [blame] | 691 | def make_asset(self, row, is_grouped_asset=False): |
Rohit Waghchaure | aa7b434 | 2018-05-11 01:56:05 +0530 | [diff] [blame] | 692 | if not row.asset_location: |
| 693 | frappe.throw(_("Row {0}: Enter location for the asset item {1}").format(row.idx, row.item_code)) |
| 694 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 695 | item_data = frappe.db.get_value( |
| 696 | "Item", row.item_code, ["asset_naming_series", "asset_category"], as_dict=1 |
| 697 | ) |
Rohit Waghchaure | f2684ae | 2018-05-08 13:22:22 +0530 | [diff] [blame] | 698 | |
Saqib Ansari | 35a6b58 | 2022-01-18 13:10:23 +0530 | [diff] [blame] | 699 | if is_grouped_asset: |
| 700 | purchase_amount = flt(row.base_amount + row.item_tax_amount) |
| 701 | else: |
| 702 | purchase_amount = flt(row.base_rate + row.item_tax_amount) |
| 703 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 704 | asset = frappe.get_doc( |
| 705 | { |
| 706 | "doctype": "Asset", |
| 707 | "item_code": row.item_code, |
| 708 | "asset_name": row.item_name, |
| 709 | "naming_series": item_data.get("asset_naming_series") or "AST", |
| 710 | "asset_category": item_data.get("asset_category"), |
| 711 | "location": row.asset_location, |
| 712 | "company": self.company, |
| 713 | "supplier": self.supplier, |
| 714 | "purchase_date": self.posting_date, |
| 715 | "calculate_depreciation": 1, |
| 716 | "purchase_receipt_amount": purchase_amount, |
| 717 | "gross_purchase_amount": purchase_amount, |
| 718 | "asset_quantity": row.qty if is_grouped_asset else 0, |
| 719 | "purchase_receipt": self.name if self.doctype == "Purchase Receipt" else None, |
| 720 | "purchase_invoice": self.name if self.doctype == "Purchase Invoice" else None, |
| 721 | } |
| 722 | ) |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 723 | |
| 724 | asset.flags.ignore_validate = True |
| 725 | asset.flags.ignore_mandatory = True |
Rohit Waghchaure | aa7b434 | 2018-05-11 01:56:05 +0530 | [diff] [blame] | 726 | asset.set_missing_values() |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 727 | asset.insert() |
| 728 | |
Saqib | bba78f0 | 2020-03-31 10:49:44 +0530 | [diff] [blame] | 729 | return asset.name |
| 730 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 731 | def update_fixed_asset(self, field, delete_asset=False): |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 732 | for d in self.get("items"): |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 733 | if d.is_fixed_asset: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 734 | is_auto_create_enabled = frappe.db.get_value("Item", d.item_code, "auto_create_assets") |
| 735 | assets = frappe.db.get_all("Asset", filters={field: self.name, "item_code": d.item_code}) |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 736 | |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 737 | for asset in assets: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 738 | asset = frappe.get_doc("Asset", asset.name) |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 739 | if delete_asset and is_auto_create_enabled: |
| 740 | # need to delete movements to delete assets otherwise throws link exists error |
Saqib | cd3976f | 2019-11-25 12:24:34 +0530 | [diff] [blame] | 741 | movements = frappe.db.sql( |
Suraj Shetty | 48e9bc3 | 2020-01-29 15:06:18 +0530 | [diff] [blame] | 742 | """SELECT asm.name |
Saqib | cd3976f | 2019-11-25 12:24:34 +0530 | [diff] [blame] | 743 | FROM `tabAsset Movement` asm, `tabAsset Movement Item` asm_item |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 744 | WHERE asm_item.parent=asm.name and asm_item.asset=%s""", |
| 745 | asset.name, |
| 746 | as_dict=1, |
| 747 | ) |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 748 | for movement in movements: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 749 | frappe.delete_doc("Asset Movement", movement.name, force=1) |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 750 | frappe.delete_doc("Asset", asset.name, force=1) |
| 751 | continue |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 752 | |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 753 | if self.docstatus in [0, 1] and not asset.get(field): |
| 754 | asset.set(field, self.name) |
| 755 | asset.purchase_date = self.posting_date |
| 756 | asset.supplier = self.supplier |
| 757 | elif self.docstatus == 2: |
Saqib | cd3976f | 2019-11-25 12:24:34 +0530 | [diff] [blame] | 758 | if asset.docstatus == 0: |
| 759 | asset.set(field, None) |
| 760 | asset.supplier = None |
| 761 | if asset.docstatus == 1 and delete_asset: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 762 | frappe.throw( |
| 763 | _( |
| 764 | "Cannot cancel this document as it is linked with submitted asset {0}. Please cancel it to continue." |
| 765 | ).format(frappe.utils.get_link_to_form("Asset", asset.name)) |
| 766 | ) |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 767 | |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 768 | asset.flags.ignore_validate_update_after_submit = True |
| 769 | asset.flags.ignore_mandatory = True |
| 770 | if asset.docstatus == 0: |
| 771 | asset.flags.ignore_validate = True |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 772 | |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 773 | asset.save() |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 774 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 775 | def delete_linked_asset(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 776 | if self.doctype == "Purchase Invoice" and not self.get("update_stock"): |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 777 | return |
| 778 | |
Nabin Hait | 1e7c32b | 2018-09-26 18:01:00 +0530 | [diff] [blame] | 779 | frappe.db.sql("delete from `tabAsset Movement` where reference_name=%s", self.name) |
rohitwaghchaure | fe22686 | 2017-12-28 16:11:27 +0530 | [diff] [blame] | 780 | |
Nabin Hait | 5a83420 | 2017-10-05 19:51:10 +0530 | [diff] [blame] | 781 | def validate_schedule_date(self): |
Sahil Khan | 34766c0 | 2018-12-20 18:50:24 +0530 | [diff] [blame] | 782 | if not self.get("items"): |
| 783 | return |
Nabin Hait | a113861 | 2019-10-15 12:43:27 +0530 | [diff] [blame] | 784 | |
18alantom | 2a20a03 | 2021-05-05 11:59:15 +0530 | [diff] [blame] | 785 | if any(d.schedule_date for d in self.get("items")): |
| 786 | # Select earliest schedule_date. |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 787 | self.schedule_date = min( |
| 788 | d.schedule_date for d in self.get("items") if d.schedule_date is not None |
| 789 | ) |
Nabin Hait | 5a83420 | 2017-10-05 19:51:10 +0530 | [diff] [blame] | 790 | |
| 791 | if self.schedule_date: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 792 | for d in self.get("items"): |
Nabin Hait | 5a83420 | 2017-10-05 19:51:10 +0530 | [diff] [blame] | 793 | if not d.schedule_date: |
| 794 | d.schedule_date = self.schedule_date |
| 795 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 796 | if ( |
| 797 | d.schedule_date |
| 798 | and self.transaction_date |
| 799 | and getdate(d.schedule_date) < getdate(self.transaction_date) |
| 800 | ): |
Nabin Hait | 96b264b | 2018-01-02 11:50:29 +0530 | [diff] [blame] | 801 | frappe.throw(_("Row #{0}: Reqd by Date cannot be before Transaction Date").format(d.idx)) |
Nabin Hait | 5a83420 | 2017-10-05 19:51:10 +0530 | [diff] [blame] | 802 | else: |
Nabin Hait | 96b264b | 2018-01-02 11:50:29 +0530 | [diff] [blame] | 803 | frappe.throw(_("Please enter Reqd by Date")) |
Rushabh Mehta | 30dc9a1 | 2017-11-17 14:31:09 +0530 | [diff] [blame] | 804 | |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 805 | def validate_items(self): |
| 806 | # validate items to see if they have is_purchase_item or is_subcontracted_item enabled |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 807 | if self.doctype == "Material Request": |
| 808 | return |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 809 | |
Sagar Sharma | d074c93 | 2022-03-31 19:57:42 +0530 | [diff] [blame] | 810 | if hasattr(self, "is_subcontracted") and self.is_subcontracted: |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 811 | validate_item_type(self, "is_sub_contracted_item", "subcontracted") |
| 812 | else: |
| 813 | validate_item_type(self, "is_purchase_item", "purchase") |
| 814 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 815 | |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 816 | def get_asset_item_details(asset_items): |
| 817 | asset_items_data = {} |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 818 | for d in frappe.get_all( |
| 819 | "Item", |
| 820 | fields=["name", "auto_create_assets", "asset_naming_series", "is_grouped_asset"], |
| 821 | filters={"name": ("in", asset_items)}, |
| 822 | ): |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 823 | asset_items_data.setdefault(d.name, d) |
| 824 | |
| 825 | return asset_items_data |
Ameya Shenoy | 873e28d | 2018-06-06 05:53:19 +0000 | [diff] [blame] | 826 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 827 | |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 828 | def validate_item_type(doc, fieldname, message): |
| 829 | # iterate through items and check if they are valid sales or purchase items |
Zarrar | 7c088ff | 2018-06-05 10:32:09 +0530 | [diff] [blame] | 830 | items = [d.item_code for d in doc.items if d.item_code] |
| 831 | |
| 832 | # No validation check inase of creating transaction using 'Opening Invoice Creation Tool' |
| 833 | if not items: |
| 834 | return |
| 835 | |
Suraj Shetty | bfc195d | 2018-09-21 10:20:52 +0530 | [diff] [blame] | 836 | item_list = ", ".join(["%s" % frappe.db.escape(d) for d in items]) |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 837 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 838 | invalid_items = [ |
| 839 | d[0] |
| 840 | for d in frappe.db.sql( |
| 841 | """ |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 842 | select item_code from tabItem where name in ({0}) and {1}=0 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 843 | """.format( |
| 844 | item_list, fieldname |
| 845 | ), |
| 846 | as_list=True, |
| 847 | ) |
| 848 | ] |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 849 | |
| 850 | if invalid_items: |
Faris Ansari | 2ab6e0e | 2018-09-19 13:13:59 +0530 | [diff] [blame] | 851 | items = ", ".join([d for d in invalid_items]) |
| 852 | |
| 853 | if len(invalid_items) > 1: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 854 | error_message = _( |
| 855 | "Following items {0} are not marked as {1} item. You can enable them as {1} item from its Item master" |
| 856 | ).format(items, message) |
Faris Ansari | 2ab6e0e | 2018-09-19 13:13:59 +0530 | [diff] [blame] | 857 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 858 | error_message = _( |
| 859 | "Following item {0} is not marked as {1} item. You can enable them as {1} item from its Item master" |
| 860 | ).format(items, message) |
Faris Ansari | 2ab6e0e | 2018-09-19 13:13:59 +0530 | [diff] [blame] | 861 | |
| 862 | frappe.throw(error_message) |