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