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