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 | |
| 4 | from __future__ import unicode_literals |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 5 | import frappe |
| 6 | from frappe import _, msgprint |
Nabin Hait | 5a83420 | 2017-10-05 19:51:10 +0530 | [diff] [blame] | 7 | from frappe.utils import flt,cint, cstr, getdate |
rohitwaghchaure | 4c688b7 | 2020-09-15 19:40:37 +0530 | [diff] [blame] | 8 | from six import iteritems |
Rohit Waghchaure | 8b82971 | 2021-04-03 14:20:30 +0530 | [diff] [blame] | 9 | from collections import OrderedDict |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 10 | from erpnext.accounts.party import get_party_details |
Rushabh Mehta | 724f9e5 | 2014-10-07 15:29:58 +0530 | [diff] [blame] | 11 | from erpnext.stock.get_item_details import get_conversion_factor |
rohitwaghchaure | fe22686 | 2017-12-28 16:11:27 +0530 | [diff] [blame] | 12 | from erpnext.buying.utils import validate_for_items, update_last_purchase_rate |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 13 | from erpnext.stock.stock_ledger import get_valuation_rate |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 14 | from erpnext.stock.doctype.stock_entry.stock_entry import get_used_alternative_items |
Rohit Waghchaure | aa7b434 | 2018-05-11 01:56:05 +0530 | [diff] [blame] | 15 | from erpnext.stock.doctype.serial_no.serial_no import get_auto_serial_nos, auto_make_serial_nos, get_serial_nos |
Rohit Waghchaure | f4dc716 | 2018-11-15 17:04:02 +0530 | [diff] [blame] | 16 | from frappe.contacts.doctype.address.address import get_address_display |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 17 | |
Rohit Waghchaure | 4d4fb6d | 2018-05-15 22:12:44 +0530 | [diff] [blame] | 18 | from erpnext.accounts.doctype.budget.budget import validate_expense_against_budget |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 19 | from erpnext.controllers.stock_controller import StockController |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 20 | from erpnext.controllers.sales_and_purchase_return import get_rate_for_return |
| 21 | from erpnext.stock.utils import get_incoming_rate |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 22 | |
Nabin Hait | 89a94d8 | 2013-03-19 12:01:46 +0530 | [diff] [blame] | 23 | class BuyingController(StockController): |
Faris Ansari | bc6a2b2 | 2020-10-27 19:42:58 +0530 | [diff] [blame] | 24 | |
Rushabh Mehta | c0bb453 | 2014-09-09 16:15:35 +0530 | [diff] [blame] | 25 | def get_feed(self): |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 26 | if self.get("supplier_name"): |
| 27 | return _("From {0} | {1} {2}").format(self.supplier_name, self.currency, |
| 28 | self.grand_total) |
Rushabh Mehta | c0bb453 | 2014-09-09 16:15:35 +0530 | [diff] [blame] | 29 | |
Saurabh | 6f75318 | 2013-03-20 12:55:28 +0530 | [diff] [blame] | 30 | def validate(self): |
| 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() |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 42 | |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 43 | if self.doctype=="Purchase Invoice": |
| 44 | self.validate_purchase_receipt_if_update_stock() |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 45 | |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 46 | if self.doctype=="Purchase Receipt" or (self.doctype=="Purchase Invoice" and self.update_stock): |
Rohit Waghchaure | 560ba39 | 2016-08-29 18:19:32 +0530 | [diff] [blame] | 47 | # self.validate_purchase_return() |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 48 | self.validate_rejected_warehouse() |
| 49 | self.validate_accepted_rejected_qty() |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 50 | validate_for_items(self) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 51 | |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 52 | #sub-contracting |
| 53 | self.validate_for_subcontracting() |
| 54 | self.create_raw_materials_supplied("supplied_items") |
| 55 | self.set_landed_cost_voucher_amount() |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 56 | |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 57 | if self.doctype in ("Purchase Receipt", "Purchase Invoice"): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 58 | self.update_valuation_rate() |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 59 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 60 | def set_missing_values(self, for_validate=False): |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 61 | super(BuyingController, self).set_missing_values(for_validate) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 62 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 63 | self.set_supplier_from_item_default() |
Nabin Hait | 096d363 | 2013-10-17 17:01:14 +0530 | [diff] [blame] | 64 | self.set_price_list_currency("Buying") |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 65 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 66 | # set contact and address details for supplier, if they are not mentioned |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 67 | if getattr(self, "supplier", None): |
Shreya Shah | 5615cb4 | 2018-10-07 11:42:07 +0530 | [diff] [blame] | 68 | self.update_if_missing(get_party_details(self.supplier, party_type="Supplier", ignore_permissions=self.flags.ignore_permissions, |
Shreya Shah | 314c97c | 2018-10-11 16:59:40 +0530 | [diff] [blame] | 69 | doctype=self.doctype, company=self.company, party_address=self.supplier_address, shipping_address=self.get('shipping_address'))) |
Rushabh Mehta | 886c9ef | 2013-07-08 12:08:06 +0530 | [diff] [blame] | 70 | |
Nabin Hait | cccc45e | 2016-10-05 17:15:43 +0530 | [diff] [blame] | 71 | self.set_missing_item_details(for_validate) |
Rushabh Mehta | 436467a | 2013-07-08 12:37:59 +0530 | [diff] [blame] | 72 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 73 | def set_supplier_from_item_default(self): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 74 | if self.meta.get_field("supplier") and not self.supplier: |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 75 | for d in self.get("items"): |
Nabin Hait | 33df0b4 | 2018-05-26 09:09:02 +0530 | [diff] [blame] | 76 | supplier = frappe.db.get_value("Item Default", |
| 77 | {"parent": d.item_code, "company": self.company}, "default_supplier") |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 78 | if supplier: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 79 | self.supplier = supplier |
Shreya Shah | 3c9839f | 2018-07-17 18:01:44 +0530 | [diff] [blame] | 80 | else: |
| 81 | item_group = frappe.db.get_value("Item", d.item_code, "item_group") |
| 82 | supplier = frappe.db.get_value("Item Default", |
| 83 | {"parent": item_group, "company": self.company}, "default_supplier") |
| 84 | if supplier: |
| 85 | self.supplier = supplier |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 86 | break |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 87 | |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 88 | def validate_stock_or_nonstock_items(self): |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 89 | 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] | 90 | msg = _('Tax Category has been changed to "Total" because all the Items are non-stock items') |
| 91 | self.update_tax_category(msg) |
| 92 | |
Deepesh Garg | f17ea2c | 2020-12-11 21:30:39 +0530 | [diff] [blame] | 93 | def update_tax_category(self, msg): |
| 94 | tax_for_valuation = [d for d in self.get("taxes") |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 95 | if d.category in ["Valuation", "Valuation and Total"]] |
tundebabzy | 9a34620 | 2017-06-16 11:00:14 +0100 | [diff] [blame] | 96 | |
Deepesh Garg | f17ea2c | 2020-12-11 21:30:39 +0530 | [diff] [blame] | 97 | if tax_for_valuation: |
| 98 | for d in tax_for_valuation: |
| 99 | d.category = 'Total' |
| 100 | |
| 101 | msgprint(msg) |
Marica | 1c8cbd0 | 2020-05-02 17:55:15 +0530 | [diff] [blame] | 102 | |
Saqib | 562fd0e | 2020-04-08 12:11:40 +0530 | [diff] [blame] | 103 | def validate_asset_return(self): |
| 104 | if self.doctype not in ['Purchase Receipt', 'Purchase Invoice'] or not self.is_return: |
| 105 | return |
| 106 | |
| 107 | purchase_doc_field = 'purchase_receipt' if self.doctype == 'Purchase Receipt' else 'purchase_invoice' |
| 108 | not_cancelled_asset = [d.name for d in frappe.db.get_all("Asset", { |
| 109 | purchase_doc_field: self.return_against, |
| 110 | "docstatus": 1 |
| 111 | })] |
| 112 | if self.is_return and len(not_cancelled_asset): |
Rohit Waghchaure | 103ecec | 2020-10-26 11:55:08 +0530 | [diff] [blame] | 113 | frappe.throw(_("{} has submitted assets linked to it. You need to cancel the assets to create purchase return.") |
| 114 | .format(self.return_against), title=_("Not Allowed")) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 115 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 116 | def get_asset_items(self): |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 117 | if self.doctype not in ['Purchase Order', 'Purchase Invoice', 'Purchase Receipt']: |
Rohit Waghchaure | d644e6d | 2018-05-12 15:27:18 +0530 | [diff] [blame] | 118 | return [] |
| 119 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 120 | return [d.item_code for d in self.items if d.is_fixed_asset] |
| 121 | |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 122 | def set_landed_cost_voucher_amount(self): |
| 123 | for d in self.get("items"): |
rohitwaghchaure | 2e8232e | 2017-08-10 11:32:59 +0530 | [diff] [blame] | 124 | lc_voucher_data = frappe.db.sql("""select sum(applicable_charges), cost_center |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 125 | from `tabLanded Cost Item` |
| 126 | where docstatus = 1 and purchase_receipt_item = %s""", d.name) |
rohitwaghchaure | 2e8232e | 2017-08-10 11:32:59 +0530 | [diff] [blame] | 127 | d.landed_cost_voucher_amount = lc_voucher_data[0][0] if lc_voucher_data else 0.0 |
| 128 | if not d.cost_center and lc_voucher_data and lc_voucher_data[0][1]: |
| 129 | d.db_set('cost_center', lc_voucher_data[0][1]) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 130 | |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 131 | def validate_from_warehouse(self): |
| 132 | for item in self.get('items'): |
| 133 | if item.get('from_warehouse') and (item.get('from_warehouse') == item.get('warehouse')): |
| 134 | frappe.throw(_("Row #{0}: Accepted Warehouse and Supplier Warehouse cannot be same").format(item.idx)) |
| 135 | |
| 136 | if item.get('from_warehouse') and self.get('is_subcontracted') == 'Yes': |
| 137 | frappe.throw(_("Row #{0}: Cannot select Supplier Warehouse while suppling raw materials to subcontractor").format(item.idx)) |
| 138 | |
Rohit Waghchaure | f4dc716 | 2018-11-15 17:04:02 +0530 | [diff] [blame] | 139 | def set_supplier_address(self): |
| 140 | address_dict = { |
| 141 | 'supplier_address': 'address_display', |
| 142 | 'shipping_address': 'shipping_address_display' |
| 143 | } |
| 144 | |
| 145 | for address_field, address_display_field in address_dict.items(): |
| 146 | if self.get(address_field): |
| 147 | self.set(address_display_field, get_address_display(self.get(address_field))) |
| 148 | |
Nabin Hait | d3b6250 | 2013-01-21 17:24:31 +0530 | [diff] [blame] | 149 | def set_total_in_words(self): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 150 | from frappe.utils import money_in_words |
Nabin Hait | 5690be1 | 2015-02-12 16:09:11 +0530 | [diff] [blame] | 151 | if self.meta.get_field("base_in_words"): |
Nabin Hait | 6b539d6 | 2018-08-29 12:41:12 +0530 | [diff] [blame] | 152 | if self.meta.get_field("base_rounded_total") and not self.is_rounded_total_disabled(): |
| 153 | amount = self.base_rounded_total |
| 154 | else: |
| 155 | amount = self.base_grand_total |
rohitwaghchaure | ae4ff5a | 2018-08-01 18:09:51 +0530 | [diff] [blame] | 156 | self.base_in_words = money_in_words(amount, self.company_currency) |
| 157 | |
Nabin Hait | 5b4c294 | 2013-01-22 11:12:02 +0530 | [diff] [blame] | 158 | if self.meta.get_field("in_words"): |
Nabin Hait | 6b539d6 | 2018-08-29 12:41:12 +0530 | [diff] [blame] | 159 | if self.meta.get_field("rounded_total") and not self.is_rounded_total_disabled(): |
| 160 | amount = self.rounded_total |
| 161 | else: |
| 162 | amount = self.grand_total |
rohitwaghchaure | ae4ff5a | 2018-08-01 18:09:51 +0530 | [diff] [blame] | 163 | |
| 164 | self.in_words = money_in_words(amount, self.currency) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 165 | |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 166 | # update valuation rate |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 167 | def update_valuation_rate(self, reset_outgoing_rate=True): |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 168 | """ |
| 169 | item_tax_amount is the total tax amount applied on that item |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 170 | stored for valuation |
| 171 | |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 172 | TODO: rename item_tax_amount to valuation_tax_amount |
| 173 | """ |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 174 | stock_and_asset_items = self.get_stock_items() + self.get_asset_items() |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 175 | |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 176 | stock_and_asset_items_qty, stock_and_asset_items_amount = 0, 0 |
| 177 | last_item_idx = 1 |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 178 | for d in self.get("items"): |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 179 | if d.item_code and d.item_code in stock_and_asset_items: |
| 180 | stock_and_asset_items_qty += flt(d.qty) |
| 181 | stock_and_asset_items_amount += flt(d.base_net_amount) |
| 182 | last_item_idx = d.idx |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 183 | |
Nabin Hait | 82e3e25 | 2015-02-23 16:58:30 +0530 | [diff] [blame] | 184 | total_valuation_amount = sum([flt(d.base_tax_amount_after_discount_amount) for d in self.get("taxes") |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 185 | if d.category in ["Valuation", "Valuation and Total"]]) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 186 | |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 187 | valuation_amount_adjustment = total_valuation_amount |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 188 | for i, item in enumerate(self.get("items")): |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 189 | if item.item_code and item.qty and item.item_code in stock_and_asset_items: |
| 190 | item_proportion = flt(item.base_net_amount) / stock_and_asset_items_amount if stock_and_asset_items_amount \ |
| 191 | else flt(item.qty) / stock_and_asset_items_qty |
Suraj Shetty | 48e9bc3 | 2020-01-29 15:06:18 +0530 | [diff] [blame] | 192 | |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 193 | if i == (last_item_idx - 1): |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 194 | item.item_tax_amount = flt(valuation_amount_adjustment, |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 195 | self.precision("item_tax_amount", item)) |
| 196 | else: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 197 | item.item_tax_amount = flt(item_proportion * total_valuation_amount, |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 198 | self.precision("item_tax_amount", item)) |
| 199 | valuation_amount_adjustment -= item.item_tax_amount |
| 200 | |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 201 | self.round_floats_in(item) |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 202 | if flt(item.conversion_factor)==0.0: |
bobzz-zone | b4c7bad | 2015-08-05 11:30:12 +0700 | [diff] [blame] | 203 | item.conversion_factor = get_conversion_factor(item.item_code, item.uom).get("conversion_factor") or 1.0 |
Rushabh Mehta | 724f9e5 | 2014-10-07 15:29:58 +0530 | [diff] [blame] | 204 | |
Rushabh Mehta | 5404778 | 2013-12-26 11:07:46 +0530 | [diff] [blame] | 205 | qty_in_stock_uom = flt(item.qty * item.conversion_factor) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 206 | item.rm_supp_cost = self.get_supplied_items_cost(item.name, reset_outgoing_rate) |
| 207 | item.valuation_rate = ((item.base_net_amount + item.item_tax_amount + item.rm_supp_cost |
| 208 | + flt(item.landed_cost_voucher_amount)) / qty_in_stock_uom) |
Anand Doshi | 4a7248e | 2013-02-27 18:10:30 +0530 | [diff] [blame] | 209 | else: |
Anand Doshi | 5af812a | 2013-05-10 19:23:02 +0530 | [diff] [blame] | 210 | item.valuation_rate = 0.0 |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 211 | |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 212 | def set_incoming_rate(self): |
| 213 | if self.doctype not in ("Purchase Receipt", "Purchase Invoice", "Purchase Order"): |
| 214 | return |
| 215 | |
| 216 | ref_doctype_map = { |
| 217 | "Purchase Order": "Sales Order Item", |
| 218 | "Purchase Receipt": "Delivery Note Item", |
| 219 | "Purchase Invoice": "Sales Invoice Item", |
| 220 | } |
| 221 | |
| 222 | ref_doctype = ref_doctype_map.get(self.doctype) |
| 223 | items = self.get("items") |
| 224 | for d in items: |
| 225 | if not cint(self.get("is_return")): |
| 226 | # Get outgoing rate based on original item cost based on valuation method |
| 227 | |
| 228 | if not d.get(frappe.scrub(ref_doctype)): |
| 229 | outgoing_rate = get_incoming_rate({ |
| 230 | "item_code": d.item_code, |
| 231 | "warehouse": d.get('from_warehouse'), |
| 232 | "posting_date": self.get('posting_date') or self.get('transation_date'), |
| 233 | "posting_time": self.get('posting_time'), |
| 234 | "qty": -1 * flt(d.get('stock_qty')), |
| 235 | "serial_no": d.get('serial_no'), |
| 236 | "company": self.company, |
| 237 | "voucher_type": self.doctype, |
| 238 | "voucher_no": self.name, |
| 239 | "allow_zero_valuation": d.get("allow_zero_valuation") |
| 240 | }, raise_error_if_no_rate=False) |
| 241 | |
| 242 | rate = flt(outgoing_rate * d.conversion_factor, d.precision('rate')) |
| 243 | else: |
| 244 | rate = frappe.db.get_value(ref_doctype, d.get(frappe.scrub(ref_doctype)), 'rate') |
| 245 | |
| 246 | if self.is_internal_transfer(): |
| 247 | if rate != d.rate: |
| 248 | d.rate = rate |
| 249 | d.discount_percentage = 0 |
| 250 | d.discount_amount = 0 |
| 251 | frappe.msgprint(_("Row {0}: Item rate has been updated as per valuation rate since its an internal stock transfer") |
| 252 | .format(d.idx), alert=1) |
| 253 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 254 | def get_supplied_items_cost(self, item_row_id, reset_outgoing_rate=True): |
| 255 | supplied_items_cost = 0.0 |
| 256 | for d in self.get("supplied_items"): |
| 257 | if d.reference_name == item_row_id: |
| 258 | if reset_outgoing_rate and frappe.db.get_value('Item', d.rm_item_code, 'is_stock_item'): |
| 259 | rate = get_incoming_rate({ |
| 260 | "item_code": d.rm_item_code, |
| 261 | "warehouse": self.supplier_warehouse, |
| 262 | "posting_date": self.posting_date, |
| 263 | "posting_time": self.posting_time, |
| 264 | "qty": -1 * d.consumed_qty, |
| 265 | "serial_no": d.serial_no |
| 266 | }) |
| 267 | |
| 268 | if rate > 0: |
| 269 | d.rate = rate |
| 270 | |
Nabin Hait | b99c77b | 2020-12-25 18:12:35 +0530 | [diff] [blame] | 271 | d.amount = flt(flt(d.consumed_qty) * flt(d.rate), d.precision("amount")) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 272 | supplied_items_cost += flt(d.amount) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 273 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 274 | return supplied_items_cost |
| 275 | |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 276 | def validate_for_subcontracting(self): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 277 | if not self.is_subcontracted and self.sub_contracted_items: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 278 | frappe.throw(_("Please enter 'Is Subcontracted' as Yes or No")) |
| 279 | |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 280 | if self.is_subcontracted == "Yes": |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 281 | if self.doctype in ["Purchase Receipt", "Purchase Invoice"] and not self.supplier_warehouse: |
Afshan | 27bcb2a | 2021-03-09 22:25:48 +0530 | [diff] [blame] | 282 | frappe.throw(_("Supplier Warehouse mandatory for sub-contracted {0}").format(self.doctype)) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 283 | |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 284 | for item in self.get("items"): |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 285 | if item in self.sub_contracted_items and not item.bom: |
| 286 | frappe.throw(_("Please select BOM in BOM field for Item {0}").format(item.item_code)) |
| 287 | |
Nabin Hait | 07e5376 | 2018-01-05 18:19:59 +0530 | [diff] [blame] | 288 | if self.doctype == "Purchase Order": |
| 289 | for supplied_item in self.get("supplied_items"): |
| 290 | if not supplied_item.reserve_warehouse: |
| 291 | frappe.throw(_("Reserved Warehouse is mandatory for Item {0} in Raw Materials supplied").format(frappe.bold(supplied_item.rm_item_code))) |
pawan | 54465f5 | 2017-11-29 10:18:38 +0530 | [diff] [blame] | 292 | |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 293 | else: |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 294 | for item in self.get("items"): |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 295 | if item.bom: |
| 296 | item.bom = None |
| 297 | |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 298 | def create_raw_materials_supplied(self, raw_material_table): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 299 | if self.is_subcontracted=="Yes": |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 300 | parent_items = [] |
rohitwaghchaure | 2da6b3d | 2018-06-05 13:06:52 +0530 | [diff] [blame] | 301 | backflush_raw_materials_based_on = frappe.db.get_single_value("Buying Settings", |
| 302 | "backflush_raw_materials_of_subcontract_based_on") |
| 303 | if (self.doctype == 'Purchase Receipt' and |
| 304 | backflush_raw_materials_based_on != 'BOM'): |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 305 | self.update_raw_materials_supplied_based_on_stock_entries() |
rohitwaghchaure | 2da6b3d | 2018-06-05 13:06:52 +0530 | [diff] [blame] | 306 | else: |
| 307 | for item in self.get("items"): |
| 308 | if self.doctype in ["Purchase Receipt", "Purchase Invoice"]: |
| 309 | item.rm_supp_cost = 0.0 |
| 310 | if item.bom and item.item_code in self.sub_contracted_items: |
| 311 | self.update_raw_materials_supplied_based_on_bom(item, raw_material_table) |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 312 | |
rohitwaghchaure | 2da6b3d | 2018-06-05 13:06:52 +0530 | [diff] [blame] | 313 | if [item.item_code, item.name] not in parent_items: |
| 314 | parent_items.append([item.item_code, item.name]) |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 315 | |
rohitwaghchaure | 2da6b3d | 2018-06-05 13:06:52 +0530 | [diff] [blame] | 316 | self.cleanup_raw_materials_supplied(parent_items, raw_material_table) |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 317 | |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 318 | elif self.doctype in ["Purchase Receipt", "Purchase Invoice"]: |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 319 | for item in self.get("items"): |
Nabin Hait | a0c239d | 2014-04-01 18:54:38 +0530 | [diff] [blame] | 320 | item.rm_supp_cost = 0.0 |
| 321 | |
Rohit Waghchaure | 4b9d2f2 | 2017-02-16 15:53:40 +0530 | [diff] [blame] | 322 | if self.is_subcontracted == "No" and self.get("supplied_items"): |
| 323 | self.set('supplied_items', []) |
| 324 | |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 325 | def update_raw_materials_supplied_based_on_stock_entries(self): |
| 326 | self.set('supplied_items', []) |
rohitwaghchaure | 2da6b3d | 2018-06-05 13:06:52 +0530 | [diff] [blame] | 327 | |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 328 | purchase_orders = set([d.purchase_order for d in self.items]) |
rohitwaghchaure | 2da6b3d | 2018-06-05 13:06:52 +0530 | [diff] [blame] | 329 | |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 330 | # qty of raw materials backflushed (for each item per purchase order) |
| 331 | backflushed_raw_materials_map = get_backflushed_subcontracted_raw_materials(purchase_orders) |
rohitwaghchaure | 2da6b3d | 2018-06-05 13:06:52 +0530 | [diff] [blame] | 332 | |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 333 | # qty of "finished good" item yet to be received |
| 334 | qty_to_be_received_map = get_qty_to_be_received(purchase_orders) |
| 335 | |
| 336 | for item in self.get('items'): |
marination | e927428 | 2020-08-28 12:21:10 +0530 | [diff] [blame] | 337 | if not item.purchase_order: |
| 338 | continue |
| 339 | |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 340 | # reset raw_material cost |
| 341 | item.rm_supp_cost = 0 |
| 342 | |
| 343 | # qty of raw materials transferred to the supplier |
| 344 | transferred_raw_materials = get_subcontracted_raw_materials_from_se(item.purchase_order, item.item_code) |
| 345 | |
| 346 | non_stock_items = get_non_stock_items(item.purchase_order, item.item_code) |
| 347 | |
| 348 | item_key = '{}{}'.format(item.item_code, item.purchase_order) |
| 349 | |
| 350 | fg_yet_to_be_received = qty_to_be_received_map.get(item_key) |
| 351 | |
marination | e927428 | 2020-08-28 12:21:10 +0530 | [diff] [blame] | 352 | if not fg_yet_to_be_received: |
| 353 | frappe.throw(_("Row #{0}: Item {1} is already fully received in Purchase Order {2}") |
| 354 | .format(item.idx, frappe.bold(item.item_code), |
| 355 | frappe.utils.get_link_to_form("Purchase Order", item.purchase_order)), |
| 356 | title=_("Limit Crossed")) |
| 357 | |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 358 | transferred_batch_qty_map = get_transferred_batch_qty_map(item.purchase_order, item.item_code) |
Rohit Waghchaure | 538d62b | 2020-09-22 10:36:52 +0530 | [diff] [blame] | 359 | # backflushed_batch_qty_map = get_backflushed_batch_qty_map(item.purchase_order, item.item_code) |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 360 | |
| 361 | for raw_material in transferred_raw_materials + non_stock_items: |
Rohit Waghchaure | 71913c3 | 2020-10-30 02:47:39 +0530 | [diff] [blame] | 362 | rm_item_key = (raw_material.rm_item_code, item.item_code, item.purchase_order) |
Rohit Waghchaure | c2a5f99 | 2019-12-20 12:56:01 +0530 | [diff] [blame] | 363 | raw_material_data = backflushed_raw_materials_map.get(rm_item_key, {}) |
| 364 | |
| 365 | consumed_qty = raw_material_data.get('qty', 0) |
Rohit Waghchaure | 7cd7bf7 | 2021-01-13 00:05:29 +0530 | [diff] [blame] | 366 | consumed_serial_nos = raw_material_data.get('serial_no', '') |
Rohit Waghchaure | c2a5f99 | 2019-12-20 12:56:01 +0530 | [diff] [blame] | 367 | consumed_batch_nos = raw_material_data.get('batch_nos', '') |
| 368 | |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 369 | transferred_qty = raw_material.qty |
| 370 | |
| 371 | rm_qty_to_be_consumed = transferred_qty - consumed_qty |
| 372 | |
| 373 | # backflush all remaining transferred qty in the last Purchase Receipt |
| 374 | if fg_yet_to_be_received == item.qty: |
| 375 | qty = rm_qty_to_be_consumed |
| 376 | else: |
| 377 | qty = (rm_qty_to_be_consumed / fg_yet_to_be_received) * item.qty |
| 378 | |
| 379 | if frappe.get_cached_value('UOM', raw_material.stock_uom, 'must_be_whole_number'): |
| 380 | qty = frappe.utils.ceil(qty) |
| 381 | |
| 382 | if qty > rm_qty_to_be_consumed: |
| 383 | qty = rm_qty_to_be_consumed |
| 384 | |
| 385 | if not qty: continue |
| 386 | |
| 387 | if raw_material.serial_nos: |
| 388 | set_serial_nos(raw_material, consumed_serial_nos, qty) |
| 389 | |
| 390 | if raw_material.batch_nos: |
Rohit Waghchaure | 538d62b | 2020-09-22 10:36:52 +0530 | [diff] [blame] | 391 | backflushed_batch_qty_map = raw_material_data.get('consumed_batch', {}) |
| 392 | |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 393 | batches_qty = get_batches_with_qty(raw_material.rm_item_code, raw_material.main_item_code, |
rohitwaghchaure | 1cfa616 | 2020-09-14 21:12:16 +0530 | [diff] [blame] | 394 | qty, transferred_batch_qty_map, backflushed_batch_qty_map, item.purchase_order) |
Rohit Waghchaure | 8b82971 | 2021-04-03 14:20:30 +0530 | [diff] [blame] | 395 | |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 396 | for batch_data in batches_qty: |
| 397 | qty = batch_data['qty'] |
| 398 | raw_material.batch_no = batch_data['batch'] |
Rohit Waghchaure | 8b82971 | 2021-04-03 14:20:30 +0530 | [diff] [blame] | 399 | if qty > 0: |
| 400 | self.append_raw_material_to_be_backflushed(item, raw_material, qty) |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 401 | else: |
| 402 | self.append_raw_material_to_be_backflushed(item, raw_material, qty) |
| 403 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 404 | def append_raw_material_to_be_backflushed(self, fg_item_row, raw_material_data, qty): |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 405 | rm = self.append('supplied_items', {}) |
| 406 | rm.update(raw_material_data) |
| 407 | |
rohitwaghchaure | 1cfa616 | 2020-09-14 21:12:16 +0530 | [diff] [blame] | 408 | if not rm.main_item_code: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 409 | rm.main_item_code = fg_item_row.item_code |
rohitwaghchaure | 1cfa616 | 2020-09-14 21:12:16 +0530 | [diff] [blame] | 410 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 411 | rm.reference_name = fg_item_row.name |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 412 | rm.required_qty = qty |
| 413 | rm.consumed_qty = qty |
| 414 | |
rohitwaghchaure | 2da6b3d | 2018-06-05 13:06:52 +0530 | [diff] [blame] | 415 | def update_raw_materials_supplied_based_on_bom(self, item, raw_material_table): |
rohitwaghchaure | bbd9b71 | 2018-03-07 15:39:40 +0530 | [diff] [blame] | 416 | exploded_item = 1 |
| 417 | if hasattr(item, 'include_exploded_items'): |
| 418 | exploded_item = item.get('include_exploded_items') |
| 419 | |
| 420 | bom_items = get_items_from_bom(item.item_code, item.bom, exploded_item) |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 421 | |
| 422 | used_alternative_items = [] |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 423 | if self.doctype in ["Purchase Receipt", "Purchase Invoice"] and item.purchase_order: |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 424 | used_alternative_items = get_used_alternative_items(purchase_order = item.purchase_order) |
| 425 | |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 426 | raw_materials_cost = 0 |
pawan | 2ff844e | 2017-11-30 18:14:55 +0530 | [diff] [blame] | 427 | items = list(set([d.item_code for d in bom_items])) |
Manas Solanki | 087a225 | 2018-05-04 16:02:38 +0530 | [diff] [blame] | 428 | item_wh = frappe._dict(frappe.db.sql("""select i.item_code, id.default_warehouse |
Nabin Hait | 5f86175 | 2018-05-23 19:37:06 +0530 | [diff] [blame] | 429 | from `tabItem` i, `tabItem Default` id |
| 430 | where id.parent=i.name and id.company=%s and i.name in ({0})""" |
Manas Solanki | 087a225 | 2018-05-04 16:02:38 +0530 | [diff] [blame] | 431 | .format(", ".join(["%s"] * len(items))), [self.company] + items)) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 432 | |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 433 | for bom_item in bom_items: |
Nabin Hait | 07e5376 | 2018-01-05 18:19:59 +0530 | [diff] [blame] | 434 | if self.doctype == "Purchase Order": |
| 435 | reserve_warehouse = bom_item.source_warehouse or item_wh.get(bom_item.item_code) |
| 436 | if frappe.db.get_value("Warehouse", reserve_warehouse, "company") != self.company: |
| 437 | reserve_warehouse = None |
| 438 | |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 439 | conversion_factor = item.conversion_factor |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 440 | if (self.doctype in ["Purchase Receipt", "Purchase Invoice"] and item.purchase_order and |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 441 | bom_item.item_code in used_alternative_items): |
| 442 | alternative_item_data = used_alternative_items.get(bom_item.item_code) |
| 443 | bom_item.item_code = alternative_item_data.item_code |
| 444 | bom_item.item_name = alternative_item_data.item_name |
| 445 | bom_item.stock_uom = alternative_item_data.stock_uom |
| 446 | conversion_factor = alternative_item_data.conversion_factor |
| 447 | bom_item.description = alternative_item_data.description |
| 448 | |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 449 | # check if exists |
| 450 | exists = 0 |
| 451 | for d in self.get(raw_material_table): |
| 452 | if d.main_item_code == item.item_code and d.rm_item_code == bom_item.item_code \ |
| 453 | and d.reference_name == item.name: |
| 454 | rm, exists = d, 1 |
| 455 | break |
| 456 | |
| 457 | if not exists: |
| 458 | rm = self.append(raw_material_table, {}) |
| 459 | |
Fahim Ali Zain TP | 277935b | 2018-02-12 11:59:07 +0530 | [diff] [blame] | 460 | required_qty = flt(flt(bom_item.qty_consumed_per_unit) * (flt(item.qty) + getattr(item, 'rejected_qty', 0)) * |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 461 | flt(conversion_factor), rm.precision("required_qty")) |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 462 | rm.reference_name = item.name |
| 463 | rm.bom_detail_no = bom_item.name |
| 464 | rm.main_item_code = item.item_code |
| 465 | rm.rm_item_code = bom_item.item_code |
| 466 | rm.stock_uom = bom_item.stock_uom |
| 467 | rm.required_qty = required_qty |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 468 | rm.rate = bom_item.rate |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 469 | rm.conversion_factor = conversion_factor |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 470 | |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 471 | if self.doctype in ["Purchase Receipt", "Purchase Invoice"]: |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 472 | rm.consumed_qty = required_qty |
| 473 | rm.description = bom_item.description |
Nabin Hait | 88e8688 | 2019-09-10 14:07:45 +0530 | [diff] [blame] | 474 | if item.batch_no and frappe.db.get_value("Item", rm.rm_item_code, "has_batch_no") and not rm.batch_no: |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 475 | rm.batch_no = item.batch_no |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 476 | elif not rm.reserve_warehouse: |
| 477 | rm.reserve_warehouse = reserve_warehouse |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 478 | |
| 479 | def cleanup_raw_materials_supplied(self, parent_items, raw_material_table): |
| 480 | """Remove all those child items which are no longer present in main item table""" |
| 481 | delete_list = [] |
| 482 | for d in self.get(raw_material_table): |
| 483 | if [d.main_item_code, d.reference_name] not in parent_items: |
| 484 | # mark for deletion from doclist |
Nabin Hait | c3d1d6a | 2014-06-04 16:41:22 +0530 | [diff] [blame] | 485 | delete_list.append(d) |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 486 | |
| 487 | # delete from doclist |
| 488 | if delete_list: |
| 489 | rm_supplied_details = self.get(raw_material_table) |
| 490 | self.set(raw_material_table, []) |
| 491 | for d in rm_supplied_details: |
| 492 | if d not in delete_list: |
| 493 | self.append(raw_material_table, d) |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 494 | |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 495 | @property |
| 496 | def sub_contracted_items(self): |
| 497 | if not hasattr(self, "_sub_contracted_items"): |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 498 | self._sub_contracted_items = [] |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 499 | item_codes = list(set(item.item_code for item in |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 500 | self.get("items"))) |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 501 | if item_codes: |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 502 | items = frappe.get_all('Item', filters={ |
| 503 | 'name': ['in', item_codes], |
| 504 | 'is_sub_contracted_item': 1 |
| 505 | }) |
| 506 | self._sub_contracted_items = [item.name for item in items] |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 507 | |
| 508 | return self._sub_contracted_items |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 509 | |
nabinhait | 614fb75 | 2014-07-14 10:47:50 +0530 | [diff] [blame] | 510 | def set_qty_as_per_stock_uom(self): |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 511 | for d in self.get("items"): |
Nabin Hait | fd4bcd8 | 2015-05-22 16:55:40 +0530 | [diff] [blame] | 512 | if d.meta.get_field("stock_qty"): |
Deepesh Garg | 8b0302b | 2019-08-05 10:14:19 +0530 | [diff] [blame] | 513 | # Check if item code is present |
| 514 | # Conversion factor should not be mandatory for non itemized items |
| 515 | if not d.conversion_factor and d.item_code: |
Nabin Hait | e6e456b | 2015-05-13 17:21:44 +0530 | [diff] [blame] | 516 | frappe.throw(_("Row {0}: Conversion Factor is mandatory").format(d.idx)) |
Rushabh Mehta | 5b51cc8 | 2014-07-21 18:25:45 +0530 | [diff] [blame] | 517 | d.stock_qty = flt(d.qty) * flt(d.conversion_factor) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 518 | |
marination | d6596a1 | 2020-11-02 15:07:48 +0530 | [diff] [blame] | 519 | if self.doctype=="Purchase Receipt" and d.meta.get_field("received_stock_qty"): |
| 520 | # Set Received Qty in Stock UOM |
| 521 | d.received_stock_qty = flt(d.received_qty) * flt(d.conversion_factor, d.precision("conversion_factor")) |
| 522 | |
Saurabh | 130c57b | 2016-04-04 15:49:36 +0530 | [diff] [blame] | 523 | def validate_purchase_return(self): |
| 524 | for d in self.get("items"): |
| 525 | if self.is_return and flt(d.rejected_qty) != 0: |
| 526 | frappe.throw(_("Row #{0}: Rejected Qty can not be entered in Purchase Return").format(d.idx)) |
| 527 | |
| 528 | # validate rate with ref PR |
| 529 | |
| 530 | def validate_rejected_warehouse(self): |
| 531 | for d in self.get("items"): |
| 532 | if flt(d.rejected_qty) and not d.rejected_warehouse: |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 533 | if self.rejected_warehouse: |
| 534 | d.rejected_warehouse = self.rejected_warehouse |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 535 | |
Saurabh | 130c57b | 2016-04-04 15:49:36 +0530 | [diff] [blame] | 536 | if not d.rejected_warehouse: |
| 537 | frappe.throw(_("Row #{0}: Rejected Warehouse is mandatory against rejected Item {1}").format(d.idx, d.item_code)) |
| 538 | |
| 539 | # validate accepted and rejected qty |
| 540 | def validate_accepted_rejected_qty(self): |
| 541 | for d in self.get("items"): |
Saurabh | 4f4d0a8 | 2017-03-13 14:31:48 +0530 | [diff] [blame] | 542 | self.validate_negative_quantity(d, ["received_qty","qty", "rejected_qty"]) |
Saurabh | 130c57b | 2016-04-04 15:49:36 +0530 | [diff] [blame] | 543 | if not flt(d.received_qty) and flt(d.qty): |
| 544 | d.received_qty = flt(d.qty) - flt(d.rejected_qty) |
| 545 | |
| 546 | elif not flt(d.qty) and flt(d.rejected_qty): |
| 547 | d.qty = flt(d.received_qty) - flt(d.rejected_qty) |
| 548 | |
| 549 | elif not flt(d.rejected_qty): |
| 550 | d.rejected_qty = flt(d.received_qty) - flt(d.qty) |
| 551 | |
Mangesh-Khairnar | 8c621ab | 2019-06-26 11:10:17 +0530 | [diff] [blame] | 552 | val = flt(d.qty) + flt(d.rejected_qty) |
Saurabh | 130c57b | 2016-04-04 15:49:36 +0530 | [diff] [blame] | 553 | # Check Received Qty = Accepted Qty + Rejected Qty |
Mangesh-Khairnar | 8c621ab | 2019-06-26 11:10:17 +0530 | [diff] [blame] | 554 | if (flt(val, d.precision("received_qty")) != flt(d.received_qty, d.precision("received_qty"))): |
Saurabh | 130c57b | 2016-04-04 15:49:36 +0530 | [diff] [blame] | 555 | frappe.throw(_("Accepted + Rejected Qty must be equal to Received quantity for Item {0}").format(d.item_code)) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 556 | |
Saurabh | 4f4d0a8 | 2017-03-13 14:31:48 +0530 | [diff] [blame] | 557 | def validate_negative_quantity(self, item_row, field_list): |
| 558 | if self.is_return: |
| 559 | return |
| 560 | |
| 561 | item_row = item_row.as_dict() |
| 562 | for fieldname in field_list: |
| 563 | if flt(item_row[fieldname]) < 0: |
Suraj Shetty | da2c69e | 2020-01-29 15:34:06 +0530 | [diff] [blame] | 564 | frappe.throw(_("Row #{0}: {1} can not be negative for item {2}").format(item_row['idx'], |
| 565 | frappe.get_meta(item_row.doctype).get_label(fieldname), item_row['item_code'])) |
Saurabh | 4f4d0a8 | 2017-03-13 14:31:48 +0530 | [diff] [blame] | 566 | |
Mangesh-Khairnar | 5e474f4 | 2019-03-06 14:46:38 +0530 | [diff] [blame] | 567 | def check_for_on_hold_or_closed_status(self, ref_doctype, ref_fieldname): |
| 568 | for d in self.get("items"): |
| 569 | if d.get(ref_fieldname): |
| 570 | status = frappe.db.get_value(ref_doctype, d.get(ref_fieldname), "status") |
| 571 | if status in ("Closed", "On Hold"): |
| 572 | frappe.throw(_("{0} {1} is {2}").format(ref_doctype,d.get(ref_fieldname), status)) |
| 573 | |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 574 | 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] | 575 | self.update_ordered_and_reserved_qty() |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 576 | |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 577 | sl_entries = [] |
| 578 | stock_items = self.get_stock_items() |
| 579 | |
| 580 | for d in self.get('items'): |
| 581 | if d.item_code in stock_items and d.warehouse: |
| 582 | pr_qty = flt(d.qty) * flt(d.conversion_factor) |
| 583 | |
| 584 | if pr_qty: |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 585 | |
| 586 | if d.from_warehouse and ((not cint(self.is_return) and self.docstatus==1) |
| 587 | or (cint(self.is_return) and self.docstatus==2)): |
| 588 | from_warehouse_sle = self.get_sl_entries(d, { |
| 589 | "actual_qty": -1 * pr_qty, |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 590 | "warehouse": d.from_warehouse, |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 591 | "outgoing_rate": d.rate, |
| 592 | "recalculate_rate": 1, |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 593 | "dependant_sle_voucher_detail_no": d.name |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 594 | }) |
| 595 | |
| 596 | sl_entries.append(from_warehouse_sle) |
| 597 | |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 598 | sle = self.get_sl_entries(d, { |
| 599 | "actual_qty": flt(pr_qty), |
| 600 | "serial_no": cstr(d.serial_no).strip() |
| 601 | }) |
| 602 | if self.is_return: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 603 | outgoing_rate = get_rate_for_return(self.doctype, self.name, d.item_code, self.return_against, item_row=d) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 604 | |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 605 | sle.update({ |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 606 | "outgoing_rate": outgoing_rate, |
| 607 | "recalculate_rate": 1 |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 608 | }) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 609 | if d.from_warehouse: |
| 610 | sle.dependant_sle_voucher_detail_no = d.name |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 611 | else: |
Nabin Hait | b81ed45 | 2016-05-11 12:52:31 +0530 | [diff] [blame] | 612 | val_rate_db_precision = 6 if cint(self.precision("valuation_rate", d)) <= 6 else 9 |
| 613 | incoming_rate = flt(d.valuation_rate, val_rate_db_precision) |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 614 | sle.update({ |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 615 | "incoming_rate": incoming_rate, |
| 616 | "recalculate_rate": 1 if (self.is_subcontracted and d.bom) or d.from_warehouse else 0 |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 617 | }) |
| 618 | sl_entries.append(sle) |
| 619 | |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 620 | if d.from_warehouse and ((not cint(self.is_return) and self.docstatus==2) |
| 621 | or (cint(self.is_return) and self.docstatus==1)): |
| 622 | from_warehouse_sle = self.get_sl_entries(d, { |
| 623 | "actual_qty": -1 * pr_qty, |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 624 | "warehouse": d.from_warehouse, |
| 625 | "recalculate_rate": 1 |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 626 | }) |
| 627 | |
| 628 | sl_entries.append(from_warehouse_sle) |
| 629 | |
Rohit Waghchaure | 560ba39 | 2016-08-29 18:19:32 +0530 | [diff] [blame] | 630 | if flt(d.rejected_qty) != 0: |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 631 | sl_entries.append(self.get_sl_entries(d, { |
| 632 | "warehouse": d.rejected_warehouse, |
| 633 | "actual_qty": flt(d.rejected_qty) * flt(d.conversion_factor), |
| 634 | "serial_no": cstr(d.rejected_serial_no).strip(), |
| 635 | "incoming_rate": 0.0 |
| 636 | })) |
| 637 | |
| 638 | self.make_sl_entries_for_supplier_warehouse(sl_entries) |
| 639 | self.make_sl_entries(sl_entries, allow_negative_stock=allow_negative_stock, |
| 640 | via_landed_cost_voucher=via_landed_cost_voucher) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 641 | |
Nabin Hait | 07e5376 | 2018-01-05 18:19:59 +0530 | [diff] [blame] | 642 | def update_ordered_and_reserved_qty(self): |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 643 | po_map = {} |
| 644 | for d in self.get("items"): |
| 645 | if self.doctype=="Purchase Receipt" \ |
Rohit Waghchaure | a71d9d3 | 2016-07-05 00:34:00 +0530 | [diff] [blame] | 646 | and d.purchase_order: |
| 647 | po_map.setdefault(d.purchase_order, []).append(d.purchase_order_item) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 648 | |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 649 | elif self.doctype=="Purchase Invoice" and d.purchase_order and d.po_detail: |
| 650 | po_map.setdefault(d.purchase_order, []).append(d.po_detail) |
| 651 | |
| 652 | for po, po_item_rows in po_map.items(): |
| 653 | if po and po_item_rows: |
| 654 | po_obj = frappe.get_doc("Purchase Order", po) |
| 655 | |
| 656 | if po_obj.status in ["Closed", "Cancelled"]: |
| 657 | frappe.throw(_("{0} {1} is cancelled or closed").format(_("Purchase Order"), po), |
| 658 | frappe.InvalidStatusError) |
| 659 | |
| 660 | po_obj.update_ordered_qty(po_item_rows) |
Nabin Hait | 07e5376 | 2018-01-05 18:19:59 +0530 | [diff] [blame] | 661 | if self.is_subcontracted: |
| 662 | po_obj.update_reserved_qty_for_subcontract() |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 663 | |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 664 | def make_sl_entries_for_supplier_warehouse(self, sl_entries): |
| 665 | if hasattr(self, 'supplied_items'): |
| 666 | for d in self.get('supplied_items'): |
| 667 | # negative quantity is passed, as raw material qty has to be decreased |
| 668 | # when PR is submitted and it has to be increased when PR is cancelled |
| 669 | sl_entries.append(self.get_sl_entries(d, { |
| 670 | "item_code": d.rm_item_code, |
| 671 | "warehouse": self.supplier_warehouse, |
| 672 | "actual_qty": -1*flt(d.consumed_qty), |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 673 | "dependant_sle_voucher_detail_no": d.reference_name |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 674 | })) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 675 | |
rohitwaghchaure | fe22686 | 2017-12-28 16:11:27 +0530 | [diff] [blame] | 676 | def on_submit(self): |
| 677 | if self.get('is_return'): |
| 678 | return |
| 679 | |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 680 | if self.doctype in ['Purchase Receipt', 'Purchase Invoice']: |
Rohit Waghchaure | c6deb13 | 2018-05-07 15:58:41 +0530 | [diff] [blame] | 681 | field = 'purchase_invoice' if self.doctype == 'Purchase Invoice' else 'purchase_receipt' |
| 682 | |
| 683 | self.process_fixed_asset() |
| 684 | self.update_fixed_asset(field) |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 685 | |
rohitwaghchaure | fe22686 | 2017-12-28 16:11:27 +0530 | [diff] [blame] | 686 | update_last_purchase_rate(self, is_submit = 1) |
| 687 | |
| 688 | def on_cancel(self): |
Rohit Waghchaure | 5fa9a7a | 2019-04-01 00:40:38 +0530 | [diff] [blame] | 689 | super(BuyingController, self).on_cancel() |
| 690 | |
rohitwaghchaure | fe22686 | 2017-12-28 16:11:27 +0530 | [diff] [blame] | 691 | if self.get('is_return'): |
| 692 | return |
| 693 | |
| 694 | update_last_purchase_rate(self, is_submit = 0) |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 695 | if self.doctype in ['Purchase Receipt', 'Purchase Invoice']: |
Rohit Waghchaure | c6deb13 | 2018-05-07 15:58:41 +0530 | [diff] [blame] | 696 | field = 'purchase_invoice' if self.doctype == 'Purchase Invoice' else 'purchase_receipt' |
| 697 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 698 | self.delete_linked_asset() |
| 699 | self.update_fixed_asset(field, delete_asset=True) |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 700 | |
Rohit Waghchaure | 4d4fb6d | 2018-05-15 22:12:44 +0530 | [diff] [blame] | 701 | def validate_budget(self): |
| 702 | if self.docstatus == 1: |
| 703 | for data in self.get('items'): |
rohitwaghchaure | 77a45b4 | 2018-06-15 18:03:31 +0530 | [diff] [blame] | 704 | args = data.as_dict() |
| 705 | args.update({ |
| 706 | 'doctype': self.doctype, |
rohitwaghchaure | 34c1877 | 2018-06-25 10:31:08 +0530 | [diff] [blame] | 707 | 'company': self.company, |
| 708 | 'posting_date': (self.schedule_date |
| 709 | if self.doctype == 'Material Request' else self.transaction_date) |
rohitwaghchaure | 77a45b4 | 2018-06-15 18:03:31 +0530 | [diff] [blame] | 710 | }) |
| 711 | |
| 712 | validate_expense_against_budget(args) |
Rohit Waghchaure | 4d4fb6d | 2018-05-15 22:12:44 +0530 | [diff] [blame] | 713 | |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 714 | def process_fixed_asset(self): |
Rohit Waghchaure | c6deb13 | 2018-05-07 15:58:41 +0530 | [diff] [blame] | 715 | if self.doctype == 'Purchase Invoice' and not self.update_stock: |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 716 | return |
| 717 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 718 | asset_items = self.get_asset_items() |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 719 | if asset_items: |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 720 | self.auto_make_assets(asset_items) |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 721 | |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 722 | def auto_make_assets(self, asset_items): |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 723 | items_data = get_asset_item_details(asset_items) |
Saqib | f37a46e | 2019-11-22 16:32:50 +0530 | [diff] [blame] | 724 | messages = [] |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 725 | |
| 726 | for d in self.items: |
| 727 | if d.is_fixed_asset: |
| 728 | item_data = items_data.get(d.item_code) |
| 729 | |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 730 | if item_data.get('auto_create_assets'): |
| 731 | # If asset has to be auto created |
| 732 | # Check for asset naming series |
| 733 | if item_data.get('asset_naming_series'): |
Saqib | bba78f0 | 2020-03-31 10:49:44 +0530 | [diff] [blame] | 734 | created_assets = [] |
| 735 | |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 736 | for qty in range(cint(d.qty)): |
Saqib | bba78f0 | 2020-03-31 10:49:44 +0530 | [diff] [blame] | 737 | asset = self.make_asset(d) |
| 738 | created_assets.append(asset) |
Marica | 1c8cbd0 | 2020-05-02 17:55:15 +0530 | [diff] [blame] | 739 | |
Saqib | bba78f0 | 2020-03-31 10:49:44 +0530 | [diff] [blame] | 740 | if len(created_assets) > 5: |
| 741 | # dont show asset form links if more than 5 assets are created |
Marica | 1c8cbd0 | 2020-05-02 17:55:15 +0530 | [diff] [blame] | 742 | messages.append(_('{} Assets created for {}').format(len(created_assets), frappe.bold(d.item_code))) |
Saqib | bba78f0 | 2020-03-31 10:49:44 +0530 | [diff] [blame] | 743 | else: |
| 744 | assets_link = list(map(lambda d: frappe.utils.get_link_to_form('Asset', d), created_assets)) |
| 745 | assets_link = frappe.bold(','.join(assets_link)) |
| 746 | |
| 747 | is_plural = 's' if len(created_assets) != 1 else '' |
| 748 | messages.append( |
| 749 | _('Asset{} {assets_link} created for {}').format(is_plural, frappe.bold(d.item_code), assets_link=assets_link) |
| 750 | ) |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 751 | else: |
Saqib | bba78f0 | 2020-03-31 10:49:44 +0530 | [diff] [blame] | 752 | frappe.throw(_("Row {}: Asset Naming Series is mandatory for the auto creation for item {}") |
| 753 | .format(d.idx, frappe.bold(d.item_code))) |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 754 | else: |
Saqib | bba78f0 | 2020-03-31 10:49:44 +0530 | [diff] [blame] | 755 | messages.append(_("Assets not created for {0}. You will have to create asset manually.") |
| 756 | .format(frappe.bold(d.item_code))) |
Saqib | f37a46e | 2019-11-22 16:32:50 +0530 | [diff] [blame] | 757 | |
| 758 | for message in messages: |
Saqib | bba78f0 | 2020-03-31 10:49:44 +0530 | [diff] [blame] | 759 | frappe.msgprint(message, title="Success", indicator="green") |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 760 | |
| 761 | def make_asset(self, row): |
Rohit Waghchaure | aa7b434 | 2018-05-11 01:56:05 +0530 | [diff] [blame] | 762 | if not row.asset_location: |
| 763 | frappe.throw(_("Row {0}: Enter location for the asset item {1}").format(row.idx, row.item_code)) |
| 764 | |
Rohit Waghchaure | f2684ae | 2018-05-08 13:22:22 +0530 | [diff] [blame] | 765 | item_data = frappe.db.get_value('Item', |
| 766 | row.item_code, ['asset_naming_series', 'asset_category'], as_dict=1) |
| 767 | |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 768 | purchase_amount = flt(row.base_rate + row.item_tax_amount) |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 769 | asset = frappe.get_doc({ |
| 770 | 'doctype': 'Asset', |
| 771 | 'item_code': row.item_code, |
Rohit Waghchaure | c6deb13 | 2018-05-07 15:58:41 +0530 | [diff] [blame] | 772 | 'asset_name': row.item_name, |
Rohit Waghchaure | f2684ae | 2018-05-08 13:22:22 +0530 | [diff] [blame] | 773 | 'naming_series': item_data.get('asset_naming_series') or 'AST', |
| 774 | 'asset_category': item_data.get('asset_category'), |
Rohit Waghchaure | aa7b434 | 2018-05-11 01:56:05 +0530 | [diff] [blame] | 775 | 'location': row.asset_location, |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 776 | 'company': self.company, |
thefalconx33 | c15cc8f | 2019-09-24 18:49:16 +0530 | [diff] [blame] | 777 | 'supplier': self.supplier, |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 778 | 'purchase_date': self.posting_date, |
Rohit Waghchaure | 0ea6fe4 | 2018-05-08 23:31:58 +0530 | [diff] [blame] | 779 | 'calculate_depreciation': 1, |
Rohit Waghchaure | 16bc853 | 2018-05-12 12:06:00 +0530 | [diff] [blame] | 780 | 'purchase_receipt_amount': purchase_amount, |
| 781 | 'gross_purchase_amount': purchase_amount, |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 782 | 'purchase_receipt': self.name if self.doctype == 'Purchase Receipt' else None, |
| 783 | 'purchase_invoice': self.name if self.doctype == 'Purchase Invoice' else None |
| 784 | }) |
| 785 | |
| 786 | asset.flags.ignore_validate = True |
| 787 | asset.flags.ignore_mandatory = True |
Rohit Waghchaure | aa7b434 | 2018-05-11 01:56:05 +0530 | [diff] [blame] | 788 | asset.set_missing_values() |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 789 | asset.insert() |
| 790 | |
Saqib | bba78f0 | 2020-03-31 10:49:44 +0530 | [diff] [blame] | 791 | return asset.name |
| 792 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 793 | def update_fixed_asset(self, field, delete_asset = False): |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 794 | for d in self.get("items"): |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 795 | if d.is_fixed_asset: |
| 796 | is_auto_create_enabled = frappe.db.get_value('Item', d.item_code, 'auto_create_assets') |
| 797 | 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] | 798 | |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 799 | for asset in assets: |
| 800 | asset = frappe.get_doc('Asset', asset.name) |
| 801 | if delete_asset and is_auto_create_enabled: |
| 802 | # need to delete movements to delete assets otherwise throws link exists error |
Saqib | cd3976f | 2019-11-25 12:24:34 +0530 | [diff] [blame] | 803 | movements = frappe.db.sql( |
Suraj Shetty | 48e9bc3 | 2020-01-29 15:06:18 +0530 | [diff] [blame] | 804 | """SELECT asm.name |
Saqib | cd3976f | 2019-11-25 12:24:34 +0530 | [diff] [blame] | 805 | FROM `tabAsset Movement` asm, `tabAsset Movement Item` asm_item |
| 806 | WHERE asm_item.parent=asm.name and asm_item.asset=%s""", asset.name, as_dict=1) |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 807 | for movement in movements: |
| 808 | frappe.delete_doc('Asset Movement', movement.name, force=1) |
| 809 | frappe.delete_doc("Asset", asset.name, force=1) |
| 810 | continue |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 811 | |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 812 | if self.docstatus in [0, 1] and not asset.get(field): |
| 813 | asset.set(field, self.name) |
| 814 | asset.purchase_date = self.posting_date |
| 815 | asset.supplier = self.supplier |
| 816 | elif self.docstatus == 2: |
Saqib | cd3976f | 2019-11-25 12:24:34 +0530 | [diff] [blame] | 817 | if asset.docstatus == 0: |
| 818 | asset.set(field, None) |
| 819 | asset.supplier = None |
| 820 | if asset.docstatus == 1 and delete_asset: |
Rohit Waghchaure | 103ecec | 2020-10-26 11:55:08 +0530 | [diff] [blame] | 821 | frappe.throw(_('Cannot cancel this document as it is linked with submitted asset {0}. Please cancel it to continue.') |
| 822 | .format(frappe.utils.get_link_to_form('Asset', asset.name))) |
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.flags.ignore_validate_update_after_submit = True |
| 825 | asset.flags.ignore_mandatory = True |
| 826 | if asset.docstatus == 0: |
| 827 | asset.flags.ignore_validate = True |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 828 | |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 829 | asset.save() |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 830 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 831 | def delete_linked_asset(self): |
Rohit Waghchaure | c6deb13 | 2018-05-07 15:58:41 +0530 | [diff] [blame] | 832 | if self.doctype == 'Purchase Invoice' and not self.get('update_stock'): |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 833 | return |
| 834 | |
Nabin Hait | 1e7c32b | 2018-09-26 18:01:00 +0530 | [diff] [blame] | 835 | frappe.db.sql("delete from `tabAsset Movement` where reference_name=%s", self.name) |
rohitwaghchaure | fe22686 | 2017-12-28 16:11:27 +0530 | [diff] [blame] | 836 | |
Nabin Hait | 5a83420 | 2017-10-05 19:51:10 +0530 | [diff] [blame] | 837 | def validate_schedule_date(self): |
Sahil Khan | 34766c0 | 2018-12-20 18:50:24 +0530 | [diff] [blame] | 838 | if not self.get("items"): |
| 839 | return |
Nabin Hait | a113861 | 2019-10-15 12:43:27 +0530 | [diff] [blame] | 840 | |
18alantom | 2a20a03 | 2021-05-05 11:59:15 +0530 | [diff] [blame] | 841 | if any(d.schedule_date for d in self.get("items")): |
| 842 | # Select earliest schedule_date. |
| 843 | self.schedule_date = min(d.schedule_date for d in self.get("items") |
| 844 | if d.schedule_date is not None) |
Nabin Hait | 5a83420 | 2017-10-05 19:51:10 +0530 | [diff] [blame] | 845 | |
| 846 | if self.schedule_date: |
| 847 | for d in self.get('items'): |
| 848 | if not d.schedule_date: |
| 849 | d.schedule_date = self.schedule_date |
| 850 | |
Prateeksha Singh | cbd06fd | 2018-01-05 21:28:01 +0530 | [diff] [blame] | 851 | if (d.schedule_date and self.transaction_date and |
| 852 | getdate(d.schedule_date) < getdate(self.transaction_date)): |
Nabin Hait | 96b264b | 2018-01-02 11:50:29 +0530 | [diff] [blame] | 853 | 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] | 854 | else: |
Nabin Hait | 96b264b | 2018-01-02 11:50:29 +0530 | [diff] [blame] | 855 | frappe.throw(_("Please enter Reqd by Date")) |
Rushabh Mehta | 30dc9a1 | 2017-11-17 14:31:09 +0530 | [diff] [blame] | 856 | |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 857 | def validate_items(self): |
| 858 | # validate items to see if they have is_purchase_item or is_subcontracted_item enabled |
Nabin Hait | 0bef91c | 2018-06-18 16:33:48 +0530 | [diff] [blame] | 859 | if self.doctype=="Material Request": return |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 860 | |
rohitwaghchaure | 50d8c4a | 2018-06-05 13:08:10 +0530 | [diff] [blame] | 861 | if hasattr(self, "is_subcontracted") and self.is_subcontracted == 'Yes': |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 862 | validate_item_type(self, "is_sub_contracted_item", "subcontracted") |
| 863 | else: |
| 864 | validate_item_type(self, "is_purchase_item", "purchase") |
| 865 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 866 | |
rohitwaghchaure | bbd9b71 | 2018-03-07 15:39:40 +0530 | [diff] [blame] | 867 | def get_items_from_bom(item_code, bom, exploded_item=1): |
| 868 | doctype = "BOM Item" if not exploded_item else "BOM Explosion Item" |
| 869 | |
| 870 | bom_items = frappe.db.sql("""select t2.item_code, t2.name, |
| 871 | t2.rate, t2.stock_uom, t2.source_warehouse, t2.description, |
| 872 | t2.stock_qty / ifnull(t1.quantity, 1) as qty_consumed_per_unit |
| 873 | from |
| 874 | `tabBOM` t1, `tab{0}` t2, tabItem t3 |
| 875 | where |
| 876 | t2.parent = t1.name and t1.item = %s |
| 877 | and t1.docstatus = 1 and t1.is_active = 1 and t1.name = %s |
Kanchan Chauhan | 654c36e | 2020-10-08 16:37:05 +0530 | [diff] [blame] | 878 | and t2.sourced_by_supplier = 0 |
Rohit Waghchaure | 10e8073 | 2019-08-08 18:19:24 +0530 | [diff] [blame] | 879 | and t2.item_code = t3.name""".format(doctype), |
rohitwaghchaure | bbd9b71 | 2018-03-07 15:39:40 +0530 | [diff] [blame] | 880 | (item_code, bom), as_dict=1) |
| 881 | |
| 882 | if not bom_items: |
| 883 | msgprint(_("Specified BOM {0} does not exist for Item {1}").format(bom, item_code), raise_exception=1) |
| 884 | |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 885 | return bom_items |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 886 | |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 887 | def get_subcontracted_raw_materials_from_se(purchase_order, fg_item): |
| 888 | common_query = """ |
| 889 | SELECT |
| 890 | sed.item_code AS rm_item_code, |
| 891 | SUM(sed.qty) AS qty, |
| 892 | sed.description, |
| 893 | sed.stock_uom, |
| 894 | sed.subcontracted_item AS main_item_code, |
| 895 | {serial_no_concat_syntax} AS serial_nos, |
| 896 | {batch_no_concat_syntax} AS batch_nos |
| 897 | FROM `tabStock Entry` se,`tabStock Entry Detail` sed |
| 898 | WHERE |
| 899 | se.name = sed.parent |
| 900 | AND se.docstatus=1 |
| 901 | AND se.purpose='Send to Subcontractor' |
| 902 | AND se.purchase_order = %s |
| 903 | AND IFNULL(sed.t_warehouse, '') != '' |
rohitwaghchaure | 1cfa616 | 2020-09-14 21:12:16 +0530 | [diff] [blame] | 904 | AND IFNULL(sed.subcontracted_item, '') in ('', %s) |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 905 | GROUP BY sed.item_code, sed.subcontracted_item |
| 906 | """ |
| 907 | raw_materials = frappe.db.multisql({ |
| 908 | 'mariadb': common_query.format( |
| 909 | serial_no_concat_syntax="GROUP_CONCAT(sed.serial_no)", |
| 910 | batch_no_concat_syntax="GROUP_CONCAT(sed.batch_no)" |
| 911 | ), |
| 912 | 'postgres': common_query.format( |
| 913 | serial_no_concat_syntax="STRING_AGG(sed.serial_no, ',')", |
| 914 | batch_no_concat_syntax="STRING_AGG(sed.batch_no, ',')" |
| 915 | ) |
| 916 | }, (purchase_order, fg_item), as_dict=1) |
rohitwaghchaure | 2da6b3d | 2018-06-05 13:06:52 +0530 | [diff] [blame] | 917 | |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 918 | return raw_materials |
| 919 | |
| 920 | def get_backflushed_subcontracted_raw_materials(purchase_orders): |
rohitwaghchaure | 4c688b7 | 2020-09-15 19:40:37 +0530 | [diff] [blame] | 921 | purchase_receipts = frappe.get_all("Purchase Receipt Item", |
| 922 | fields = ["purchase_order", "item_code", "name", "parent"], |
| 923 | filters={"docstatus": 1, "purchase_order": ("in", list(purchase_orders))}) |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 924 | |
rohitwaghchaure | 4c688b7 | 2020-09-15 19:40:37 +0530 | [diff] [blame] | 925 | distinct_purchase_receipts = {} |
| 926 | for pr in purchase_receipts: |
| 927 | key = (pr.purchase_order, pr.item_code, pr.parent) |
| 928 | distinct_purchase_receipts.setdefault(key, []).append(pr.name) |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 929 | |
| 930 | backflushed_raw_materials_map = frappe._dict() |
rohitwaghchaure | 4c688b7 | 2020-09-15 19:40:37 +0530 | [diff] [blame] | 931 | for args, references in iteritems(distinct_purchase_receipts): |
| 932 | purchase_receipt_supplied_items = get_supplied_items(args[1], args[2], references) |
| 933 | |
| 934 | for data in purchase_receipt_supplied_items: |
Rohit Waghchaure | 71913c3 | 2020-10-30 02:47:39 +0530 | [diff] [blame] | 935 | pr_key = (data.rm_item_code, data.main_item_code, args[0]) |
rohitwaghchaure | 4c688b7 | 2020-09-15 19:40:37 +0530 | [diff] [blame] | 936 | if pr_key not in backflushed_raw_materials_map: |
| 937 | backflushed_raw_materials_map.setdefault(pr_key, frappe._dict({ |
| 938 | "qty": 0.0, |
| 939 | "serial_no": [], |
Rohit Waghchaure | 538d62b | 2020-09-22 10:36:52 +0530 | [diff] [blame] | 940 | "batch_no": [], |
| 941 | "consumed_batch": {} |
rohitwaghchaure | 4c688b7 | 2020-09-15 19:40:37 +0530 | [diff] [blame] | 942 | })) |
| 943 | |
| 944 | row = backflushed_raw_materials_map.get(pr_key) |
| 945 | row.qty += data.consumed_qty |
| 946 | |
| 947 | for field in ["serial_no", "batch_no"]: |
| 948 | if data.get(field): |
| 949 | row[field].append(data.get(field)) |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 950 | |
Rohit Waghchaure | 538d62b | 2020-09-22 10:36:52 +0530 | [diff] [blame] | 951 | if data.get("batch_no"): |
| 952 | if data.get("batch_no") in row.consumed_batch: |
| 953 | row.consumed_batch[data.get("batch_no")] += data.consumed_qty |
| 954 | else: |
| 955 | row.consumed_batch[data.get("batch_no")] = data.consumed_qty |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 956 | |
| 957 | return backflushed_raw_materials_map |
rohitwaghchaure | 2da6b3d | 2018-06-05 13:06:52 +0530 | [diff] [blame] | 958 | |
rohitwaghchaure | 4c688b7 | 2020-09-15 19:40:37 +0530 | [diff] [blame] | 959 | def get_supplied_items(item_code, purchase_receipt, references): |
| 960 | return frappe.get_all("Purchase Receipt Item Supplied", |
Rohit Waghchaure | 71913c3 | 2020-10-30 02:47:39 +0530 | [diff] [blame] | 961 | fields=["rm_item_code", "main_item_code", "consumed_qty", "serial_no", "batch_no"], |
rohitwaghchaure | 4c688b7 | 2020-09-15 19:40:37 +0530 | [diff] [blame] | 962 | filters={"main_item_code": item_code, "parent": purchase_receipt, "reference_name": ("in", references)}) |
| 963 | |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 964 | def get_asset_item_details(asset_items): |
| 965 | asset_items_data = {} |
Saqib | d995609 | 2019-11-18 11:46:55 +0530 | [diff] [blame] | 966 | for d in frappe.get_all('Item', fields = ["name", "auto_create_assets", "asset_naming_series"], |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 967 | filters = {'name': ('in', asset_items)}): |
| 968 | asset_items_data.setdefault(d.name, d) |
| 969 | |
| 970 | return asset_items_data |
Ameya Shenoy | 873e28d | 2018-06-06 05:53:19 +0000 | [diff] [blame] | 971 | |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 972 | def validate_item_type(doc, fieldname, message): |
| 973 | # iterate through items and check if they are valid sales or purchase items |
Zarrar | 7c088ff | 2018-06-05 10:32:09 +0530 | [diff] [blame] | 974 | items = [d.item_code for d in doc.items if d.item_code] |
| 975 | |
| 976 | # No validation check inase of creating transaction using 'Opening Invoice Creation Tool' |
| 977 | if not items: |
| 978 | return |
| 979 | |
Suraj Shetty | bfc195d | 2018-09-21 10:20:52 +0530 | [diff] [blame] | 980 | item_list = ", ".join(["%s" % frappe.db.escape(d) for d in items]) |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 981 | |
| 982 | invalid_items = [d[0] for d in frappe.db.sql(""" |
| 983 | select item_code from tabItem where name in ({0}) and {1}=0 |
| 984 | """.format(item_list, fieldname), as_list=True)] |
| 985 | |
| 986 | if invalid_items: |
Faris Ansari | 2ab6e0e | 2018-09-19 13:13:59 +0530 | [diff] [blame] | 987 | items = ", ".join([d for d in invalid_items]) |
| 988 | |
| 989 | if len(invalid_items) > 1: |
Suraj Shetty | 48e9bc3 | 2020-01-29 15:06:18 +0530 | [diff] [blame] | 990 | error_message = _("Following items {0} are not marked as {1} item. You can enable them as {1} item from its Item master").format(items, message) |
Faris Ansari | 2ab6e0e | 2018-09-19 13:13:59 +0530 | [diff] [blame] | 991 | else: |
Suraj Shetty | 48e9bc3 | 2020-01-29 15:06:18 +0530 | [diff] [blame] | 992 | error_message = _("Following item {0} is not marked as {1} item. You can enable them as {1} item from its Item master").format(items, message) |
Faris Ansari | 2ab6e0e | 2018-09-19 13:13:59 +0530 | [diff] [blame] | 993 | |
| 994 | frappe.throw(error_message) |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 995 | |
| 996 | def get_qty_to_be_received(purchase_orders): |
| 997 | return frappe._dict(frappe.db.sql(""" |
| 998 | SELECT CONCAT(poi.`item_code`, poi.`parent`) AS item_key, |
| 999 | SUM(poi.`qty`) - SUM(poi.`received_qty`) AS qty_to_be_received |
| 1000 | FROM `tabPurchase Order Item` poi |
| 1001 | WHERE |
| 1002 | poi.`parent` in %s |
| 1003 | GROUP BY poi.`item_code`, poi.`parent` |
| 1004 | HAVING SUM(poi.`qty`) > SUM(poi.`received_qty`) |
| 1005 | """, (purchase_orders))) |
| 1006 | |
| 1007 | def get_non_stock_items(purchase_order, fg_item_code): |
| 1008 | return frappe.db.sql(""" |
| 1009 | SELECT |
| 1010 | pois.main_item_code, |
| 1011 | pois.rm_item_code, |
| 1012 | item.description, |
| 1013 | pois.required_qty AS qty, |
| 1014 | pois.rate, |
| 1015 | 1 as non_stock_item, |
| 1016 | pois.stock_uom |
| 1017 | FROM `tabPurchase Order Item Supplied` pois, `tabItem` item |
| 1018 | WHERE |
| 1019 | pois.`rm_item_code` = item.`name` |
| 1020 | AND item.is_stock_item = 0 |
| 1021 | AND pois.`parent` = %s |
| 1022 | AND pois.`main_item_code` = %s |
| 1023 | """, (purchase_order, fg_item_code), as_dict=1) |
| 1024 | |
| 1025 | |
| 1026 | def set_serial_nos(raw_material, consumed_serial_nos, qty): |
| 1027 | serial_nos = set(get_serial_nos(raw_material.serial_nos)) - \ |
| 1028 | set(get_serial_nos(consumed_serial_nos)) |
| 1029 | if serial_nos and qty <= len(serial_nos): |
| 1030 | raw_material.serial_no = '\n'.join(list(serial_nos)[0:frappe.utils.cint(qty)]) |
| 1031 | |
| 1032 | def get_transferred_batch_qty_map(purchase_order, fg_item): |
| 1033 | # returns |
| 1034 | # { |
| 1035 | # (item_code, fg_code): { |
| 1036 | # batch1: 10, # qty |
| 1037 | # batch2: 16 |
| 1038 | # }, |
| 1039 | # } |
| 1040 | transferred_batch_qty_map = {} |
| 1041 | transferred_batches = frappe.db.sql(""" |
| 1042 | SELECT |
| 1043 | sed.batch_no, |
| 1044 | SUM(sed.qty) AS qty, |
rohitwaghchaure | 1cfa616 | 2020-09-14 21:12:16 +0530 | [diff] [blame] | 1045 | sed.item_code, |
| 1046 | sed.subcontracted_item |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 1047 | FROM `tabStock Entry` se,`tabStock Entry Detail` sed |
| 1048 | WHERE |
| 1049 | se.name = sed.parent |
| 1050 | AND se.docstatus=1 |
| 1051 | AND se.purpose='Send to Subcontractor' |
| 1052 | AND se.purchase_order = %s |
rohitwaghchaure | 1cfa616 | 2020-09-14 21:12:16 +0530 | [diff] [blame] | 1053 | AND ifnull(sed.subcontracted_item, '') in ('', %s) |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 1054 | AND sed.batch_no IS NOT NULL |
| 1055 | GROUP BY |
| 1056 | sed.batch_no, |
| 1057 | sed.item_code |
| 1058 | """, (purchase_order, fg_item), as_dict=1) |
| 1059 | |
| 1060 | for batch_data in transferred_batches: |
rohitwaghchaure | 1cfa616 | 2020-09-14 21:12:16 +0530 | [diff] [blame] | 1061 | key = ((batch_data.item_code, fg_item) |
| 1062 | if batch_data.subcontracted_item else (batch_data.item_code, purchase_order)) |
Rohit Waghchaure | 8b82971 | 2021-04-03 14:20:30 +0530 | [diff] [blame] | 1063 | transferred_batch_qty_map.setdefault(key, OrderedDict()) |
rohitwaghchaure | 1cfa616 | 2020-09-14 21:12:16 +0530 | [diff] [blame] | 1064 | transferred_batch_qty_map[key][batch_data.batch_no] = batch_data.qty |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 1065 | |
| 1066 | return transferred_batch_qty_map |
| 1067 | |
| 1068 | def get_backflushed_batch_qty_map(purchase_order, fg_item): |
| 1069 | # returns |
| 1070 | # { |
| 1071 | # (item_code, fg_code): { |
| 1072 | # batch1: 10, # qty |
| 1073 | # batch2: 16 |
| 1074 | # }, |
| 1075 | # } |
| 1076 | backflushed_batch_qty_map = {} |
| 1077 | backflushed_batches = frappe.db.sql(""" |
| 1078 | SELECT |
| 1079 | pris.batch_no, |
| 1080 | SUM(pris.consumed_qty) AS qty, |
| 1081 | pris.rm_item_code AS item_code |
| 1082 | FROM `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pri, `tabPurchase Receipt Item Supplied` pris |
| 1083 | WHERE |
| 1084 | pr.name = pri.parent |
| 1085 | AND pri.parent = pris.parent |
| 1086 | AND pri.purchase_order = %s |
| 1087 | AND pri.item_code = pris.main_item_code |
| 1088 | AND pr.docstatus = 1 |
| 1089 | AND pris.main_item_code = %s |
| 1090 | AND pris.batch_no IS NOT NULL |
| 1091 | GROUP BY |
| 1092 | pris.rm_item_code, pris.batch_no |
| 1093 | """, (purchase_order, fg_item), as_dict=1) |
| 1094 | |
| 1095 | for batch_data in backflushed_batches: |
| 1096 | backflushed_batch_qty_map.setdefault((batch_data.item_code, fg_item), {}) |
| 1097 | backflushed_batch_qty_map[(batch_data.item_code, fg_item)][batch_data.batch_no] = batch_data.qty |
| 1098 | |
| 1099 | return backflushed_batch_qty_map |
| 1100 | |
Rohit Waghchaure | 538d62b | 2020-09-22 10:36:52 +0530 | [diff] [blame] | 1101 | def get_batches_with_qty(item_code, fg_item, required_qty, transferred_batch_qty_map, backflushed_batches, po): |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 1102 | # Returns available batches to be backflushed based on requirements |
| 1103 | transferred_batches = transferred_batch_qty_map.get((item_code, fg_item), {}) |
rohitwaghchaure | 1cfa616 | 2020-09-14 21:12:16 +0530 | [diff] [blame] | 1104 | if not transferred_batches: |
| 1105 | transferred_batches = transferred_batch_qty_map.get((item_code, po), {}) |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 1106 | |
| 1107 | available_batches = [] |
| 1108 | |
| 1109 | for (batch, transferred_qty) in transferred_batches.items(): |
| 1110 | backflushed_qty = backflushed_batches.get(batch, 0) |
| 1111 | available_qty = transferred_qty - backflushed_qty |
| 1112 | |
| 1113 | if available_qty >= required_qty: |
| 1114 | available_batches.append({'batch': batch, 'qty': required_qty}) |
| 1115 | break |
Rohit Waghchaure | 8b82971 | 2021-04-03 14:20:30 +0530 | [diff] [blame] | 1116 | elif available_qty != 0: |
Suraj Shetty | 9d6d95c | 2019-11-29 13:26:52 +0530 | [diff] [blame] | 1117 | available_batches.append({'batch': batch, 'qty': available_qty}) |
| 1118 | required_qty -= available_qty |
| 1119 | |
Rohit Waghchaure | 8b82971 | 2021-04-03 14:20:30 +0530 | [diff] [blame] | 1120 | for row in available_batches: |
| 1121 | if backflushed_batches.get(row.get('batch'), 0) > 0: |
| 1122 | backflushed_batches[row.get('batch')] += row.get('qty') |
| 1123 | else: |
| 1124 | backflushed_batches[row.get('batch')] = row.get('qty') |
| 1125 | |
Saqib | bba78f0 | 2020-03-31 10:49:44 +0530 | [diff] [blame] | 1126 | return available_batches |