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 |
Sambhaji Kolate | e3d2643 | 2014-09-10 13:07:59 +0530 | [diff] [blame] | 8 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 9 | from erpnext.accounts.party import get_party_details |
Rushabh Mehta | 724f9e5 | 2014-10-07 15:29:58 +0530 | [diff] [blame] | 10 | from erpnext.stock.get_item_details import get_conversion_factor |
rohitwaghchaure | fe22686 | 2017-12-28 16:11:27 +0530 | [diff] [blame] | 11 | from erpnext.buying.utils import validate_for_items, update_last_purchase_rate |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 12 | from erpnext.stock.stock_ledger import get_valuation_rate |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 13 | 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] | 14 | from erpnext.stock.doctype.serial_no.serial_no import get_auto_serial_nos, auto_make_serial_nos, get_serial_nos |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 15 | |
Rohit Waghchaure | 4d4fb6d | 2018-05-15 22:12:44 +0530 | [diff] [blame] | 16 | from erpnext.accounts.doctype.budget.budget import validate_expense_against_budget |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 17 | from erpnext.controllers.stock_controller import StockController |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 18 | |
Nabin Hait | 89a94d8 | 2013-03-19 12:01:46 +0530 | [diff] [blame] | 19 | class BuyingController(StockController): |
Rushabh Mehta | 5b51cc8 | 2014-07-21 18:25:45 +0530 | [diff] [blame] | 20 | def __setup__(self): |
Nabin Hait | de9c8a9 | 2015-02-23 01:06:00 +0530 | [diff] [blame] | 21 | if hasattr(self, "taxes"): |
Nabin Hait | 45dce89 | 2017-09-14 15:17:38 +0530 | [diff] [blame] | 22 | self.flags.print_taxes_with_zero_amount = cint(frappe.db.get_single_value("Print Settings", |
| 23 | "print_taxes_with_zero_amount")) |
rohitwaghchaure | bf4c114 | 2018-01-08 15:20:15 +0530 | [diff] [blame] | 24 | self.flags.show_inclusive_tax_in_print = self.is_inclusive_tax() |
| 25 | |
Rushabh Mehta | c567e8e | 2015-02-03 17:55:52 +0530 | [diff] [blame] | 26 | self.print_templates = { |
rohitwaghchaure | bf4c114 | 2018-01-08 15:20:15 +0530 | [diff] [blame] | 27 | "total": "templates/print_formats/includes/total.html", |
Rushabh Mehta | c567e8e | 2015-02-03 17:55:52 +0530 | [diff] [blame] | 28 | "taxes": "templates/print_formats/includes/taxes.html" |
Rushabh Mehta | 5b51cc8 | 2014-07-21 18:25:45 +0530 | [diff] [blame] | 29 | } |
| 30 | |
Rushabh Mehta | c0bb453 | 2014-09-09 16:15:35 +0530 | [diff] [blame] | 31 | def get_feed(self): |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 32 | if self.get("supplier_name"): |
| 33 | return _("From {0} | {1} {2}").format(self.supplier_name, self.currency, |
| 34 | self.grand_total) |
Rushabh Mehta | c0bb453 | 2014-09-09 16:15:35 +0530 | [diff] [blame] | 35 | |
Saurabh | 6f75318 | 2013-03-20 12:55:28 +0530 | [diff] [blame] | 36 | def validate(self): |
| 37 | super(BuyingController, self).validate() |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 38 | if getattr(self, "supplier", None) and not self.supplier_name: |
Nabin Hait | 1d21842 | 2015-07-17 15:19:02 +0530 | [diff] [blame] | 39 | self.supplier_name = frappe.db.get_value("Supplier", self.supplier, "supplier_name") |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 40 | |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 41 | self.validate_items() |
nabinhait | 614fb75 | 2014-07-14 10:47:50 +0530 | [diff] [blame] | 42 | self.set_qty_as_per_stock_uom() |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 43 | self.validate_stock_or_nonstock_items() |
Anand Doshi | 373680b | 2013-10-10 16:04:40 +0530 | [diff] [blame] | 44 | self.validate_warehouse() |
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 Invoice": |
| 47 | self.validate_purchase_receipt_if_update_stock() |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 48 | |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 49 | 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] | 50 | # self.validate_purchase_return() |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 51 | self.validate_rejected_warehouse() |
| 52 | self.validate_accepted_rejected_qty() |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 53 | validate_for_items(self) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 54 | |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 55 | #sub-contracting |
| 56 | self.validate_for_subcontracting() |
| 57 | self.create_raw_materials_supplied("supplied_items") |
| 58 | self.set_landed_cost_voucher_amount() |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 59 | |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 60 | if self.doctype in ("Purchase Receipt", "Purchase Invoice"): |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 61 | self.update_valuation_rate("items") |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 62 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 63 | def set_missing_values(self, for_validate=False): |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 64 | super(BuyingController, self).set_missing_values(for_validate) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 65 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 66 | self.set_supplier_from_item_default() |
Nabin Hait | 096d363 | 2013-10-17 17:01:14 +0530 | [diff] [blame] | 67 | self.set_price_list_currency("Buying") |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 68 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 69 | # set contact and address details for supplier, if they are not mentioned |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 70 | if getattr(self, "supplier", None): |
Nabin Hait | 7eba1a3 | 2017-10-02 15:59:27 +0530 | [diff] [blame] | 71 | self.update_if_missing(get_party_details(self.supplier, party_type="Supplier", ignore_permissions=self.flags.ignore_permissions, doctype=self.doctype, company=self.company)) |
Rushabh Mehta | 886c9ef | 2013-07-08 12:08:06 +0530 | [diff] [blame] | 72 | |
Nabin Hait | cccc45e | 2016-10-05 17:15:43 +0530 | [diff] [blame] | 73 | self.set_missing_item_details(for_validate) |
Rushabh Mehta | 436467a | 2013-07-08 12:37:59 +0530 | [diff] [blame] | 74 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 75 | def set_supplier_from_item_default(self): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 76 | if self.meta.get_field("supplier") and not self.supplier: |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 77 | for d in self.get("items"): |
Nabin Hait | 33df0b4 | 2018-05-26 09:09:02 +0530 | [diff] [blame] | 78 | supplier = frappe.db.get_value("Item Default", |
| 79 | {"parent": d.item_code, "company": self.company}, "default_supplier") |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 80 | if supplier: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 81 | self.supplier = supplier |
Shreya Shah | 3c9839f | 2018-07-17 18:01:44 +0530 | [diff] [blame] | 82 | else: |
| 83 | item_group = frappe.db.get_value("Item", d.item_code, "item_group") |
| 84 | supplier = frappe.db.get_value("Item Default", |
| 85 | {"parent": item_group, "company": self.company}, "default_supplier") |
| 86 | if supplier: |
| 87 | self.supplier = supplier |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 88 | break |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 89 | |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 90 | def validate_stock_or_nonstock_items(self): |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 91 | if self.meta.get_field("taxes") and not self.get_stock_items() and not self.get_asset_items(): |
tundebabzy | 9a34620 | 2017-06-16 11:00:14 +0100 | [diff] [blame] | 92 | tax_for_valuation = [d for d in self.get("taxes") |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 93 | if d.category in ["Valuation", "Valuation and Total"]] |
tundebabzy | 9a34620 | 2017-06-16 11:00:14 +0100 | [diff] [blame] | 94 | |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 95 | if tax_for_valuation: |
tundebabzy | 9a34620 | 2017-06-16 11:00:14 +0100 | [diff] [blame] | 96 | for d in tax_for_valuation: |
| 97 | d.category = 'Total' |
| 98 | msgprint(_('Tax Category has been changed to "Total" because all the Items are non-stock items')) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 99 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 100 | def get_asset_items(self): |
Rohit Waghchaure | d644e6d | 2018-05-12 15:27:18 +0530 | [diff] [blame] | 101 | if self.doctype not in ['Purchase Invoice', 'Purchase Receipt']: |
| 102 | return [] |
| 103 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 104 | return [d.item_code for d in self.items if d.is_fixed_asset] |
| 105 | |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 106 | def set_landed_cost_voucher_amount(self): |
| 107 | for d in self.get("items"): |
rohitwaghchaure | 2e8232e | 2017-08-10 11:32:59 +0530 | [diff] [blame] | 108 | lc_voucher_data = frappe.db.sql("""select sum(applicable_charges), cost_center |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 109 | from `tabLanded Cost Item` |
| 110 | where docstatus = 1 and purchase_receipt_item = %s""", d.name) |
rohitwaghchaure | 2e8232e | 2017-08-10 11:32:59 +0530 | [diff] [blame] | 111 | d.landed_cost_voucher_amount = lc_voucher_data[0][0] if lc_voucher_data else 0.0 |
| 112 | if not d.cost_center and lc_voucher_data and lc_voucher_data[0][1]: |
| 113 | d.db_set('cost_center', lc_voucher_data[0][1]) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 114 | |
Nabin Hait | d3b6250 | 2013-01-21 17:24:31 +0530 | [diff] [blame] | 115 | def set_total_in_words(self): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 116 | from frappe.utils import money_in_words |
Nabin Hait | 5690be1 | 2015-02-12 16:09:11 +0530 | [diff] [blame] | 117 | if self.meta.get_field("base_in_words"): |
Nabin Hait | 6b539d6 | 2018-08-29 12:41:12 +0530 | [diff] [blame] | 118 | if self.meta.get_field("base_rounded_total") and not self.is_rounded_total_disabled(): |
| 119 | amount = self.base_rounded_total |
| 120 | else: |
| 121 | amount = self.base_grand_total |
rohitwaghchaure | ae4ff5a | 2018-08-01 18:09:51 +0530 | [diff] [blame] | 122 | self.base_in_words = money_in_words(amount, self.company_currency) |
| 123 | |
Nabin Hait | 5b4c294 | 2013-01-22 11:12:02 +0530 | [diff] [blame] | 124 | if self.meta.get_field("in_words"): |
Nabin Hait | 6b539d6 | 2018-08-29 12:41:12 +0530 | [diff] [blame] | 125 | if self.meta.get_field("rounded_total") and not self.is_rounded_total_disabled(): |
| 126 | amount = self.rounded_total |
| 127 | else: |
| 128 | amount = self.grand_total |
rohitwaghchaure | ae4ff5a | 2018-08-01 18:09:51 +0530 | [diff] [blame] | 129 | |
| 130 | self.in_words = money_in_words(amount, self.currency) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 131 | |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 132 | # update valuation rate |
| 133 | def update_valuation_rate(self, parentfield): |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 134 | """ |
| 135 | item_tax_amount is the total tax amount applied on that item |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 136 | stored for valuation |
| 137 | |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 138 | TODO: rename item_tax_amount to valuation_tax_amount |
| 139 | """ |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 140 | stock_items = self.get_stock_items() + self.get_asset_items() |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 141 | |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 142 | stock_items_qty, stock_items_amount = 0, 0 |
| 143 | last_stock_item_idx = 1 |
Rushabh Mehta | d2b34dc | 2014-03-27 16:12:56 +0530 | [diff] [blame] | 144 | for d in self.get(parentfield): |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 145 | if d.item_code and d.item_code in stock_items: |
| 146 | stock_items_qty += flt(d.qty) |
Nabin Hait | 82e3e25 | 2015-02-23 16:58:30 +0530 | [diff] [blame] | 147 | stock_items_amount += flt(d.base_net_amount) |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 148 | last_stock_item_idx = d.idx |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 149 | |
Nabin Hait | 82e3e25 | 2015-02-23 16:58:30 +0530 | [diff] [blame] | 150 | 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] | 151 | if d.category in ["Valuation", "Valuation and Total"]]) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 152 | |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 153 | valuation_amount_adjustment = total_valuation_amount |
Rushabh Mehta | d2b34dc | 2014-03-27 16:12:56 +0530 | [diff] [blame] | 154 | for i, item in enumerate(self.get(parentfield)): |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 155 | if item.item_code and item.qty and item.item_code in stock_items: |
Nabin Hait | 82e3e25 | 2015-02-23 16:58:30 +0530 | [diff] [blame] | 156 | item_proportion = flt(item.base_net_amount) / stock_items_amount if stock_items_amount \ |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 157 | else flt(item.qty) / stock_items_qty |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 158 | if i == (last_stock_item_idx - 1): |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 159 | item.item_tax_amount = flt(valuation_amount_adjustment, |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 160 | self.precision("item_tax_amount", item)) |
| 161 | else: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 162 | item.item_tax_amount = flt(item_proportion * total_valuation_amount, |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 163 | self.precision("item_tax_amount", item)) |
| 164 | valuation_amount_adjustment -= item.item_tax_amount |
| 165 | |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 166 | self.round_floats_in(item) |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 167 | if flt(item.conversion_factor)==0.0: |
bobzz-zone | b4c7bad | 2015-08-05 11:30:12 +0700 | [diff] [blame] | 168 | 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] | 169 | |
Rushabh Mehta | 5404778 | 2013-12-26 11:07:46 +0530 | [diff] [blame] | 170 | qty_in_stock_uom = flt(item.qty * item.conversion_factor) |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 171 | rm_supp_cost = flt(item.rm_supp_cost) if self.doctype in ["Purchase Receipt", "Purchase Invoice"] else 0.0 |
nabinhait | cc0692d | 2014-07-17 19:16:38 +0530 | [diff] [blame] | 172 | |
nabinhait | 87f2401 | 2014-07-16 19:48:29 +0530 | [diff] [blame] | 173 | landed_cost_voucher_amount = flt(item.landed_cost_voucher_amount) \ |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 174 | if self.doctype in ["Purchase Receipt", "Purchase Invoice"] else 0.0 |
Nabin Hait | 14b8af2 | 2014-08-28 12:42:28 +0530 | [diff] [blame] | 175 | |
Nabin Hait | 82e3e25 | 2015-02-23 16:58:30 +0530 | [diff] [blame] | 176 | item.valuation_rate = ((item.base_net_amount + item.item_tax_amount + rm_supp_cost |
nabinhait | 87f2401 | 2014-07-16 19:48:29 +0530 | [diff] [blame] | 177 | + landed_cost_voucher_amount) / qty_in_stock_uom) |
Anand Doshi | 4a7248e | 2013-02-27 18:10:30 +0530 | [diff] [blame] | 178 | else: |
Anand Doshi | 5af812a | 2013-05-10 19:23:02 +0530 | [diff] [blame] | 179 | item.valuation_rate = 0.0 |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 180 | |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 181 | def validate_for_subcontracting(self): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 182 | if not self.is_subcontracted and self.sub_contracted_items: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 183 | frappe.throw(_("Please enter 'Is Subcontracted' as Yes or No")) |
| 184 | |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 185 | if self.is_subcontracted == "Yes": |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 186 | if self.doctype in ["Purchase Receipt", "Purchase Invoice"] and not self.supplier_warehouse: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 187 | frappe.throw(_("Supplier Warehouse mandatory for sub-contracted Purchase Receipt")) |
| 188 | |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 189 | for item in self.get("items"): |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 190 | if item in self.sub_contracted_items and not item.bom: |
| 191 | frappe.throw(_("Please select BOM in BOM field for Item {0}").format(item.item_code)) |
| 192 | |
Nabin Hait | 07e5376 | 2018-01-05 18:19:59 +0530 | [diff] [blame] | 193 | if self.doctype == "Purchase Order": |
| 194 | for supplied_item in self.get("supplied_items"): |
| 195 | if not supplied_item.reserve_warehouse: |
| 196 | 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] | 197 | |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 198 | else: |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 199 | for item in self.get("items"): |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 200 | if item.bom: |
| 201 | item.bom = None |
| 202 | |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 203 | def create_raw_materials_supplied(self, raw_material_table): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 204 | if self.is_subcontracted=="Yes": |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 205 | parent_items = [] |
rohitwaghchaure | 2da6b3d | 2018-06-05 13:06:52 +0530 | [diff] [blame] | 206 | backflush_raw_materials_based_on = frappe.db.get_single_value("Buying Settings", |
| 207 | "backflush_raw_materials_of_subcontract_based_on") |
| 208 | if (self.doctype == 'Purchase Receipt' and |
| 209 | backflush_raw_materials_based_on != 'BOM'): |
| 210 | self.update_raw_materials_supplied_based_on_stock_entries(raw_material_table) |
| 211 | else: |
| 212 | for item in self.get("items"): |
| 213 | if self.doctype in ["Purchase Receipt", "Purchase Invoice"]: |
| 214 | item.rm_supp_cost = 0.0 |
| 215 | if item.bom and item.item_code in self.sub_contracted_items: |
| 216 | self.update_raw_materials_supplied_based_on_bom(item, raw_material_table) |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 217 | |
rohitwaghchaure | 2da6b3d | 2018-06-05 13:06:52 +0530 | [diff] [blame] | 218 | if [item.item_code, item.name] not in parent_items: |
| 219 | parent_items.append([item.item_code, item.name]) |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 220 | |
rohitwaghchaure | 2da6b3d | 2018-06-05 13:06:52 +0530 | [diff] [blame] | 221 | self.cleanup_raw_materials_supplied(parent_items, raw_material_table) |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 222 | |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 223 | elif self.doctype in ["Purchase Receipt", "Purchase Invoice"]: |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 224 | for item in self.get("items"): |
Nabin Hait | a0c239d | 2014-04-01 18:54:38 +0530 | [diff] [blame] | 225 | item.rm_supp_cost = 0.0 |
| 226 | |
Rohit Waghchaure | 4b9d2f2 | 2017-02-16 15:53:40 +0530 | [diff] [blame] | 227 | if self.is_subcontracted == "No" and self.get("supplied_items"): |
| 228 | self.set('supplied_items', []) |
| 229 | |
rohitwaghchaure | 2da6b3d | 2018-06-05 13:06:52 +0530 | [diff] [blame] | 230 | def update_raw_materials_supplied_based_on_stock_entries(self, raw_material_table): |
| 231 | self.set(raw_material_table, []) |
| 232 | purchase_orders = [d.purchase_order for d in self.items] |
| 233 | if purchase_orders: |
| 234 | items = get_subcontracted_raw_materials_from_se(purchase_orders) |
| 235 | backflushed_raw_materials = get_backflushed_subcontracted_raw_materials_from_se(purchase_orders, self.name) |
| 236 | |
| 237 | for d in items: |
| 238 | qty = d.qty - backflushed_raw_materials.get(d.item_code, 0) |
| 239 | rm = self.append(raw_material_table, {}) |
| 240 | rm.rm_item_code = d.item_code |
| 241 | rm.item_name = d.item_name |
| 242 | rm.main_item_code = d.main_item_code |
| 243 | rm.description = d.description |
| 244 | rm.stock_uom = d.stock_uom |
| 245 | rm.required_qty = qty |
| 246 | rm.consumed_qty = qty |
| 247 | rm.serial_no = d.serial_no |
| 248 | rm.batch_no = d.batch_no |
| 249 | |
| 250 | # get raw materials rate |
| 251 | from erpnext.stock.utils import get_incoming_rate |
| 252 | rm.rate = get_incoming_rate({ |
| 253 | "item_code": d.item_code, |
| 254 | "warehouse": self.supplier_warehouse, |
| 255 | "posting_date": self.posting_date, |
| 256 | "posting_time": self.posting_time, |
| 257 | "qty": -1 * qty, |
| 258 | "serial_no": rm.serial_no |
| 259 | }) |
| 260 | if not rm.rate: |
| 261 | rm.rate = get_valuation_rate(d.item_code, self.supplier_warehouse, |
| 262 | self.doctype, self.name, currency=self.company_currency, company = self.company) |
| 263 | |
| 264 | rm.amount = qty * flt(rm.rate) |
| 265 | |
| 266 | def update_raw_materials_supplied_based_on_bom(self, item, raw_material_table): |
rohitwaghchaure | bbd9b71 | 2018-03-07 15:39:40 +0530 | [diff] [blame] | 267 | exploded_item = 1 |
| 268 | if hasattr(item, 'include_exploded_items'): |
| 269 | exploded_item = item.get('include_exploded_items') |
| 270 | |
| 271 | bom_items = get_items_from_bom(item.item_code, item.bom, exploded_item) |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 272 | |
| 273 | used_alternative_items = [] |
| 274 | if self.doctype == 'Purchase Receipt' and item.purchase_order: |
| 275 | used_alternative_items = get_used_alternative_items(purchase_order = item.purchase_order) |
| 276 | |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 277 | raw_materials_cost = 0 |
pawan | 2ff844e | 2017-11-30 18:14:55 +0530 | [diff] [blame] | 278 | items = list(set([d.item_code for d in bom_items])) |
Manas Solanki | 087a225 | 2018-05-04 16:02:38 +0530 | [diff] [blame] | 279 | 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] | 280 | from `tabItem` i, `tabItem Default` id |
| 281 | 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] | 282 | .format(", ".join(["%s"] * len(items))), [self.company] + items)) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 283 | |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 284 | for bom_item in bom_items: |
Nabin Hait | 07e5376 | 2018-01-05 18:19:59 +0530 | [diff] [blame] | 285 | if self.doctype == "Purchase Order": |
| 286 | reserve_warehouse = bom_item.source_warehouse or item_wh.get(bom_item.item_code) |
| 287 | if frappe.db.get_value("Warehouse", reserve_warehouse, "company") != self.company: |
| 288 | reserve_warehouse = None |
| 289 | |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 290 | conversion_factor = item.conversion_factor |
| 291 | if (self.doctype == 'Purchase Receipt' and item.purchase_order and |
| 292 | bom_item.item_code in used_alternative_items): |
| 293 | alternative_item_data = used_alternative_items.get(bom_item.item_code) |
| 294 | bom_item.item_code = alternative_item_data.item_code |
| 295 | bom_item.item_name = alternative_item_data.item_name |
| 296 | bom_item.stock_uom = alternative_item_data.stock_uom |
| 297 | conversion_factor = alternative_item_data.conversion_factor |
| 298 | bom_item.description = alternative_item_data.description |
| 299 | |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 300 | # check if exists |
| 301 | exists = 0 |
| 302 | for d in self.get(raw_material_table): |
| 303 | if d.main_item_code == item.item_code and d.rm_item_code == bom_item.item_code \ |
| 304 | and d.reference_name == item.name: |
| 305 | rm, exists = d, 1 |
| 306 | break |
| 307 | |
| 308 | if not exists: |
| 309 | rm = self.append(raw_material_table, {}) |
| 310 | |
Fahim Ali Zain TP | 277935b | 2018-02-12 11:59:07 +0530 | [diff] [blame] | 311 | 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] | 312 | flt(conversion_factor), rm.precision("required_qty")) |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 313 | rm.reference_name = item.name |
| 314 | rm.bom_detail_no = bom_item.name |
| 315 | rm.main_item_code = item.item_code |
| 316 | rm.rm_item_code = bom_item.item_code |
| 317 | rm.stock_uom = bom_item.stock_uom |
| 318 | rm.required_qty = required_qty |
Nabin Hait | 07e5376 | 2018-01-05 18:19:59 +0530 | [diff] [blame] | 319 | if self.doctype == "Purchase Order" and not rm.reserve_warehouse: |
| 320 | rm.reserve_warehouse = reserve_warehouse |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 321 | |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 322 | rm.conversion_factor = conversion_factor |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 323 | |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 324 | if self.doctype in ["Purchase Receipt", "Purchase Invoice"]: |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 325 | rm.consumed_qty = required_qty |
| 326 | rm.description = bom_item.description |
| 327 | if item.batch_no and not rm.batch_no: |
| 328 | rm.batch_no = item.batch_no |
| 329 | |
Nabin Hait | e96e83d | 2014-10-08 18:06:14 +0530 | [diff] [blame] | 330 | # get raw materials rate |
Nabin Hait | fce2881 | 2014-10-08 18:38:27 +0530 | [diff] [blame] | 331 | if self.doctype == "Purchase Receipt": |
| 332 | from erpnext.stock.utils import get_incoming_rate |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 333 | rm.rate = get_incoming_rate({ |
Nabin Hait | fce2881 | 2014-10-08 18:38:27 +0530 | [diff] [blame] | 334 | "item_code": bom_item.item_code, |
| 335 | "warehouse": self.supplier_warehouse, |
| 336 | "posting_date": self.posting_date, |
| 337 | "posting_time": self.posting_time, |
| 338 | "qty": -1 * required_qty, |
| 339 | "serial_no": rm.serial_no |
| 340 | }) |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 341 | if not rm.rate: |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 342 | rm.rate = get_valuation_rate(bom_item.item_code, self.supplier_warehouse, |
Rohit Waghchaure | a5f4094 | 2017-06-16 15:21:36 +0530 | [diff] [blame] | 343 | self.doctype, self.name, currency=self.company_currency, company = self.company) |
Nabin Hait | fce2881 | 2014-10-08 18:38:27 +0530 | [diff] [blame] | 344 | else: |
| 345 | rm.rate = bom_item.rate |
Nabin Hait | e96e83d | 2014-10-08 18:06:14 +0530 | [diff] [blame] | 346 | |
Nabin Hait | fce2881 | 2014-10-08 18:38:27 +0530 | [diff] [blame] | 347 | rm.amount = required_qty * flt(rm.rate) |
Nabin Hait | e96e83d | 2014-10-08 18:06:14 +0530 | [diff] [blame] | 348 | raw_materials_cost += flt(rm.amount) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 349 | |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 350 | if self.doctype in ("Purchase Receipt", "Purchase Invoice"): |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 351 | item.rm_supp_cost = raw_materials_cost |
| 352 | |
| 353 | def cleanup_raw_materials_supplied(self, parent_items, raw_material_table): |
| 354 | """Remove all those child items which are no longer present in main item table""" |
| 355 | delete_list = [] |
| 356 | for d in self.get(raw_material_table): |
| 357 | if [d.main_item_code, d.reference_name] not in parent_items: |
| 358 | # mark for deletion from doclist |
Nabin Hait | c3d1d6a | 2014-06-04 16:41:22 +0530 | [diff] [blame] | 359 | delete_list.append(d) |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 360 | |
| 361 | # delete from doclist |
| 362 | if delete_list: |
| 363 | rm_supplied_details = self.get(raw_material_table) |
| 364 | self.set(raw_material_table, []) |
| 365 | for d in rm_supplied_details: |
| 366 | if d not in delete_list: |
| 367 | self.append(raw_material_table, d) |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 368 | |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 369 | @property |
| 370 | def sub_contracted_items(self): |
| 371 | if not hasattr(self, "_sub_contracted_items"): |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 372 | self._sub_contracted_items = [] |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 373 | item_codes = list(set(item.item_code for item in |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 374 | self.get("items"))) |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 375 | if item_codes: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 376 | self._sub_contracted_items = [r[0] for r in frappe.db.sql("""select name |
Rushabh Mehta | 1e8025b | 2015-07-24 15:16:25 +0530 | [diff] [blame] | 377 | from `tabItem` where name in (%s) and is_sub_contracted_item=1""" % \ |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 378 | (", ".join((["%s"]*len(item_codes))),), item_codes)] |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 379 | |
| 380 | return self._sub_contracted_items |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 381 | |
nabinhait | 614fb75 | 2014-07-14 10:47:50 +0530 | [diff] [blame] | 382 | def set_qty_as_per_stock_uom(self): |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 383 | for d in self.get("items"): |
Nabin Hait | fd4bcd8 | 2015-05-22 16:55:40 +0530 | [diff] [blame] | 384 | if d.meta.get_field("stock_qty"): |
nabinhait | 614fb75 | 2014-07-14 10:47:50 +0530 | [diff] [blame] | 385 | if not d.conversion_factor: |
Nabin Hait | e6e456b | 2015-05-13 17:21:44 +0530 | [diff] [blame] | 386 | frappe.throw(_("Row {0}: Conversion Factor is mandatory").format(d.idx)) |
Rushabh Mehta | 5b51cc8 | 2014-07-21 18:25:45 +0530 | [diff] [blame] | 387 | d.stock_qty = flt(d.qty) * flt(d.conversion_factor) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 388 | |
Saurabh | 130c57b | 2016-04-04 15:49:36 +0530 | [diff] [blame] | 389 | def validate_purchase_return(self): |
| 390 | for d in self.get("items"): |
| 391 | if self.is_return and flt(d.rejected_qty) != 0: |
| 392 | frappe.throw(_("Row #{0}: Rejected Qty can not be entered in Purchase Return").format(d.idx)) |
| 393 | |
| 394 | # validate rate with ref PR |
| 395 | |
| 396 | def validate_rejected_warehouse(self): |
| 397 | for d in self.get("items"): |
| 398 | if flt(d.rejected_qty) and not d.rejected_warehouse: |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 399 | if self.rejected_warehouse: |
| 400 | d.rejected_warehouse = self.rejected_warehouse |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 401 | |
Saurabh | 130c57b | 2016-04-04 15:49:36 +0530 | [diff] [blame] | 402 | if not d.rejected_warehouse: |
| 403 | frappe.throw(_("Row #{0}: Rejected Warehouse is mandatory against rejected Item {1}").format(d.idx, d.item_code)) |
| 404 | |
| 405 | # validate accepted and rejected qty |
| 406 | def validate_accepted_rejected_qty(self): |
| 407 | for d in self.get("items"): |
Saurabh | 4f4d0a8 | 2017-03-13 14:31:48 +0530 | [diff] [blame] | 408 | self.validate_negative_quantity(d, ["received_qty","qty", "rejected_qty"]) |
Saurabh | 130c57b | 2016-04-04 15:49:36 +0530 | [diff] [blame] | 409 | if not flt(d.received_qty) and flt(d.qty): |
| 410 | d.received_qty = flt(d.qty) - flt(d.rejected_qty) |
| 411 | |
| 412 | elif not flt(d.qty) and flt(d.rejected_qty): |
| 413 | d.qty = flt(d.received_qty) - flt(d.rejected_qty) |
| 414 | |
| 415 | elif not flt(d.rejected_qty): |
| 416 | d.rejected_qty = flt(d.received_qty) - flt(d.qty) |
| 417 | |
| 418 | # Check Received Qty = Accepted Qty + Rejected Qty |
| 419 | if ((flt(d.qty) + flt(d.rejected_qty)) != flt(d.received_qty)): |
| 420 | 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] | 421 | |
Saurabh | 4f4d0a8 | 2017-03-13 14:31:48 +0530 | [diff] [blame] | 422 | def validate_negative_quantity(self, item_row, field_list): |
| 423 | if self.is_return: |
| 424 | return |
| 425 | |
| 426 | item_row = item_row.as_dict() |
| 427 | for fieldname in field_list: |
| 428 | if flt(item_row[fieldname]) < 0: |
| 429 | frappe.throw(_("Row #{0}: {1} can not be negative for item {2}".format(item_row['idx'], |
| 430 | frappe.get_meta(item_row.doctype).get_label(fieldname), item_row['item_code']))) |
| 431 | |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 432 | 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] | 433 | self.update_ordered_and_reserved_qty() |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 434 | |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 435 | sl_entries = [] |
| 436 | stock_items = self.get_stock_items() |
| 437 | |
| 438 | for d in self.get('items'): |
| 439 | if d.item_code in stock_items and d.warehouse: |
| 440 | pr_qty = flt(d.qty) * flt(d.conversion_factor) |
| 441 | |
| 442 | if pr_qty: |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 443 | sle = self.get_sl_entries(d, { |
| 444 | "actual_qty": flt(pr_qty), |
| 445 | "serial_no": cstr(d.serial_no).strip() |
| 446 | }) |
| 447 | if self.is_return: |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 448 | original_incoming_rate = frappe.db.get_value("Stock Ledger Entry", |
| 449 | {"voucher_type": "Purchase Receipt", "voucher_no": self.return_against, |
Nabin Hait | b81ed45 | 2016-05-11 12:52:31 +0530 | [diff] [blame] | 450 | "item_code": d.item_code}, "incoming_rate") |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 451 | |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 452 | sle.update({ |
Nabin Hait | b81ed45 | 2016-05-11 12:52:31 +0530 | [diff] [blame] | 453 | "outgoing_rate": original_incoming_rate |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 454 | }) |
| 455 | else: |
Nabin Hait | b81ed45 | 2016-05-11 12:52:31 +0530 | [diff] [blame] | 456 | val_rate_db_precision = 6 if cint(self.precision("valuation_rate", d)) <= 6 else 9 |
| 457 | incoming_rate = flt(d.valuation_rate, val_rate_db_precision) |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 458 | sle.update({ |
Nabin Hait | b81ed45 | 2016-05-11 12:52:31 +0530 | [diff] [blame] | 459 | "incoming_rate": incoming_rate |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 460 | }) |
| 461 | sl_entries.append(sle) |
| 462 | |
Rohit Waghchaure | 560ba39 | 2016-08-29 18:19:32 +0530 | [diff] [blame] | 463 | if flt(d.rejected_qty) != 0: |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 464 | sl_entries.append(self.get_sl_entries(d, { |
| 465 | "warehouse": d.rejected_warehouse, |
| 466 | "actual_qty": flt(d.rejected_qty) * flt(d.conversion_factor), |
| 467 | "serial_no": cstr(d.rejected_serial_no).strip(), |
| 468 | "incoming_rate": 0.0 |
| 469 | })) |
| 470 | |
| 471 | self.make_sl_entries_for_supplier_warehouse(sl_entries) |
| 472 | self.make_sl_entries(sl_entries, allow_negative_stock=allow_negative_stock, |
| 473 | via_landed_cost_voucher=via_landed_cost_voucher) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 474 | |
Nabin Hait | 07e5376 | 2018-01-05 18:19:59 +0530 | [diff] [blame] | 475 | def update_ordered_and_reserved_qty(self): |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 476 | po_map = {} |
| 477 | for d in self.get("items"): |
| 478 | if self.doctype=="Purchase Receipt" \ |
Rohit Waghchaure | a71d9d3 | 2016-07-05 00:34:00 +0530 | [diff] [blame] | 479 | and d.purchase_order: |
| 480 | po_map.setdefault(d.purchase_order, []).append(d.purchase_order_item) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 481 | |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 482 | elif self.doctype=="Purchase Invoice" and d.purchase_order and d.po_detail: |
| 483 | po_map.setdefault(d.purchase_order, []).append(d.po_detail) |
| 484 | |
| 485 | for po, po_item_rows in po_map.items(): |
| 486 | if po and po_item_rows: |
| 487 | po_obj = frappe.get_doc("Purchase Order", po) |
| 488 | |
| 489 | if po_obj.status in ["Closed", "Cancelled"]: |
| 490 | frappe.throw(_("{0} {1} is cancelled or closed").format(_("Purchase Order"), po), |
| 491 | frappe.InvalidStatusError) |
| 492 | |
| 493 | po_obj.update_ordered_qty(po_item_rows) |
Nabin Hait | 07e5376 | 2018-01-05 18:19:59 +0530 | [diff] [blame] | 494 | if self.is_subcontracted: |
| 495 | po_obj.update_reserved_qty_for_subcontract() |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 496 | |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 497 | def make_sl_entries_for_supplier_warehouse(self, sl_entries): |
| 498 | if hasattr(self, 'supplied_items'): |
| 499 | for d in self.get('supplied_items'): |
| 500 | # negative quantity is passed, as raw material qty has to be decreased |
| 501 | # when PR is submitted and it has to be increased when PR is cancelled |
| 502 | sl_entries.append(self.get_sl_entries(d, { |
| 503 | "item_code": d.rm_item_code, |
| 504 | "warehouse": self.supplier_warehouse, |
| 505 | "actual_qty": -1*flt(d.consumed_qty), |
| 506 | })) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 507 | |
rohitwaghchaure | fe22686 | 2017-12-28 16:11:27 +0530 | [diff] [blame] | 508 | def on_submit(self): |
| 509 | if self.get('is_return'): |
| 510 | return |
| 511 | |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 512 | if self.doctype in ['Purchase Receipt', 'Purchase Invoice']: |
Rohit Waghchaure | c6deb13 | 2018-05-07 15:58:41 +0530 | [diff] [blame] | 513 | field = 'purchase_invoice' if self.doctype == 'Purchase Invoice' else 'purchase_receipt' |
| 514 | |
| 515 | self.process_fixed_asset() |
| 516 | self.update_fixed_asset(field) |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 517 | |
rohitwaghchaure | fe22686 | 2017-12-28 16:11:27 +0530 | [diff] [blame] | 518 | update_last_purchase_rate(self, is_submit = 1) |
| 519 | |
| 520 | def on_cancel(self): |
| 521 | if self.get('is_return'): |
| 522 | return |
| 523 | |
| 524 | update_last_purchase_rate(self, is_submit = 0) |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 525 | if self.doctype in ['Purchase Receipt', 'Purchase Invoice']: |
Rohit Waghchaure | c6deb13 | 2018-05-07 15:58:41 +0530 | [diff] [blame] | 526 | field = 'purchase_invoice' if self.doctype == 'Purchase Invoice' else 'purchase_receipt' |
| 527 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 528 | self.delete_linked_asset() |
| 529 | self.update_fixed_asset(field, delete_asset=True) |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 530 | |
Rohit Waghchaure | 4d4fb6d | 2018-05-15 22:12:44 +0530 | [diff] [blame] | 531 | def validate_budget(self): |
| 532 | if self.docstatus == 1: |
| 533 | for data in self.get('items'): |
rohitwaghchaure | 77a45b4 | 2018-06-15 18:03:31 +0530 | [diff] [blame] | 534 | args = data.as_dict() |
| 535 | args.update({ |
| 536 | 'doctype': self.doctype, |
rohitwaghchaure | 34c1877 | 2018-06-25 10:31:08 +0530 | [diff] [blame] | 537 | 'company': self.company, |
| 538 | 'posting_date': (self.schedule_date |
| 539 | if self.doctype == 'Material Request' else self.transaction_date) |
rohitwaghchaure | 77a45b4 | 2018-06-15 18:03:31 +0530 | [diff] [blame] | 540 | }) |
| 541 | |
| 542 | validate_expense_against_budget(args) |
Rohit Waghchaure | 4d4fb6d | 2018-05-15 22:12:44 +0530 | [diff] [blame] | 543 | |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 544 | def process_fixed_asset(self): |
Rohit Waghchaure | c6deb13 | 2018-05-07 15:58:41 +0530 | [diff] [blame] | 545 | if self.doctype == 'Purchase Invoice' and not self.update_stock: |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 546 | return |
| 547 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 548 | asset_items = self.get_asset_items() |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 549 | if asset_items: |
| 550 | self.make_serial_nos_for_asset(asset_items) |
| 551 | |
| 552 | def make_serial_nos_for_asset(self, asset_items): |
| 553 | items_data = get_asset_item_details(asset_items) |
| 554 | |
| 555 | for d in self.items: |
| 556 | if d.is_fixed_asset: |
| 557 | item_data = items_data.get(d.item_code) |
Rohit Waghchaure | c6deb13 | 2018-05-07 15:58:41 +0530 | [diff] [blame] | 558 | if not d.asset: |
| 559 | asset = self.make_asset(d) |
| 560 | d.db_set('asset', asset) |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 561 | |
| 562 | if item_data.get('has_serial_no'): |
| 563 | # If item has serial no |
| 564 | if item_data.get('serial_no_series') and not d.serial_no: |
| 565 | serial_nos = get_auto_serial_nos(item_data.get('serial_no_series'), d.qty) |
| 566 | elif d.serial_no: |
| 567 | serial_nos = d.serial_no |
| 568 | elif not d.serial_no: |
| 569 | frappe.throw(_("Serial no is mandatory for the item {0}").format(d.item_code)) |
| 570 | |
| 571 | auto_make_serial_nos({ |
| 572 | 'serial_no': serial_nos, |
| 573 | 'item_code': d.item_code, |
| 574 | 'via_stock_ledger': False, |
| 575 | 'company': self.company, |
| 576 | 'actual_qty': d.qty, |
| 577 | 'purchase_document_type': self.doctype, |
Rohit Waghchaure | c6deb13 | 2018-05-07 15:58:41 +0530 | [diff] [blame] | 578 | 'purchase_document_no': self.name, |
Rohit Waghchaure | 647d595 | 2018-06-11 18:53:55 +0530 | [diff] [blame] | 579 | 'asset': d.asset, |
| 580 | 'location': d.asset_location |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 581 | }) |
| 582 | d.db_set('serial_no', serial_nos) |
| 583 | |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 584 | if d.asset: |
| 585 | self.make_asset_movement(d) |
| 586 | |
| 587 | def make_asset(self, row): |
Rohit Waghchaure | aa7b434 | 2018-05-11 01:56:05 +0530 | [diff] [blame] | 588 | if not row.asset_location: |
| 589 | frappe.throw(_("Row {0}: Enter location for the asset item {1}").format(row.idx, row.item_code)) |
| 590 | |
Rohit Waghchaure | f2684ae | 2018-05-08 13:22:22 +0530 | [diff] [blame] | 591 | item_data = frappe.db.get_value('Item', |
| 592 | row.item_code, ['asset_naming_series', 'asset_category'], as_dict=1) |
| 593 | |
Rohit Waghchaure | 16bc853 | 2018-05-12 12:06:00 +0530 | [diff] [blame] | 594 | purchase_amount = flt(row.base_net_amount + row.item_tax_amount) |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 595 | asset = frappe.get_doc({ |
| 596 | 'doctype': 'Asset', |
| 597 | 'item_code': row.item_code, |
Rohit Waghchaure | c6deb13 | 2018-05-07 15:58:41 +0530 | [diff] [blame] | 598 | 'asset_name': row.item_name, |
Rohit Waghchaure | f2684ae | 2018-05-08 13:22:22 +0530 | [diff] [blame] | 599 | 'naming_series': item_data.get('asset_naming_series') or 'AST', |
| 600 | 'asset_category': item_data.get('asset_category'), |
Rohit Waghchaure | aa7b434 | 2018-05-11 01:56:05 +0530 | [diff] [blame] | 601 | 'location': row.asset_location, |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 602 | 'company': self.company, |
| 603 | 'purchase_date': self.posting_date, |
Rohit Waghchaure | 0ea6fe4 | 2018-05-08 23:31:58 +0530 | [diff] [blame] | 604 | 'calculate_depreciation': 1, |
Rohit Waghchaure | 16bc853 | 2018-05-12 12:06:00 +0530 | [diff] [blame] | 605 | 'purchase_receipt_amount': purchase_amount, |
| 606 | 'gross_purchase_amount': purchase_amount, |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 607 | 'purchase_receipt': self.name if self.doctype == 'Purchase Receipt' else None, |
| 608 | 'purchase_invoice': self.name if self.doctype == 'Purchase Invoice' else None |
| 609 | }) |
| 610 | |
| 611 | asset.flags.ignore_validate = True |
| 612 | asset.flags.ignore_mandatory = True |
Rohit Waghchaure | aa7b434 | 2018-05-11 01:56:05 +0530 | [diff] [blame] | 613 | asset.set_missing_values() |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 614 | asset.insert() |
| 615 | |
| 616 | frappe.msgprint(_("Asset {0} created").format(asset.name)) |
| 617 | return asset.name |
| 618 | |
| 619 | def make_asset_movement(self, row): |
| 620 | asset_movement = frappe.get_doc({ |
| 621 | 'doctype': 'Asset Movement', |
| 622 | 'asset': row.asset, |
Rohit Waghchaure | aa7b434 | 2018-05-11 01:56:05 +0530 | [diff] [blame] | 623 | 'target_location': row.asset_location, |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 624 | 'purpose': 'Receipt', |
| 625 | 'serial_no': row.serial_no, |
Rohit Waghchaure | aa7b434 | 2018-05-11 01:56:05 +0530 | [diff] [blame] | 626 | 'quantity': len(get_serial_nos(row.serial_no)), |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 627 | 'company': self.company, |
| 628 | 'transaction_date': self.posting_date, |
| 629 | 'reference_doctype': self.doctype, |
| 630 | 'reference_name': self.name |
| 631 | }).insert() |
| 632 | |
| 633 | return asset_movement.name |
| 634 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 635 | def update_fixed_asset(self, field, delete_asset = False): |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 636 | for d in self.get("items"): |
| 637 | if d.is_fixed_asset and d.asset: |
| 638 | asset = frappe.get_doc("Asset", d.asset) |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 639 | |
| 640 | if delete_asset and asset.docstatus == 0: |
| 641 | frappe.delete_doc("Asset", asset.name) |
| 642 | d.db_set('asset', None) |
| 643 | continue |
| 644 | |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 645 | if self.docstatus in [0, 1] and not asset.get(field): |
| 646 | asset.set(field, self.name) |
| 647 | asset.purchase_date = self.posting_date |
| 648 | asset.supplier = self.supplier |
Rohit Waghchaure | aa7b434 | 2018-05-11 01:56:05 +0530 | [diff] [blame] | 649 | elif self.docstatus == 2: |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 650 | asset.set(field, None) |
| 651 | asset.supplier = None |
| 652 | |
| 653 | asset.flags.ignore_validate_update_after_submit = True |
Rohit Waghchaure | a02640e | 2018-05-16 10:49:12 +0530 | [diff] [blame] | 654 | asset.flags.ignore_mandatory = True |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 655 | if asset.docstatus == 0: |
| 656 | asset.flags.ignore_validate = True |
| 657 | |
| 658 | asset.save() |
| 659 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 660 | def delete_linked_asset(self): |
Rohit Waghchaure | c6deb13 | 2018-05-07 15:58:41 +0530 | [diff] [blame] | 661 | if self.doctype == 'Purchase Invoice' and not self.get('update_stock'): |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 662 | return |
| 663 | |
Nabin Hait | 1e7c32b | 2018-09-26 18:01:00 +0530 | [diff] [blame] | 664 | frappe.db.sql("delete from `tabAsset Movement` where reference_name=%s", self.name) |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 665 | frappe.db.sql("delete from `tabSerial No` where purchase_document_no=%s", self.name) |
rohitwaghchaure | fe22686 | 2017-12-28 16:11:27 +0530 | [diff] [blame] | 666 | |
Nabin Hait | 5a83420 | 2017-10-05 19:51:10 +0530 | [diff] [blame] | 667 | def validate_schedule_date(self): |
| 668 | if not self.schedule_date: |
| 669 | self.schedule_date = min([d.schedule_date for d in self.get("items")]) |
| 670 | |
| 671 | if self.schedule_date: |
| 672 | for d in self.get('items'): |
| 673 | if not d.schedule_date: |
| 674 | d.schedule_date = self.schedule_date |
| 675 | |
Prateeksha Singh | cbd06fd | 2018-01-05 21:28:01 +0530 | [diff] [blame] | 676 | if (d.schedule_date and self.transaction_date and |
| 677 | getdate(d.schedule_date) < getdate(self.transaction_date)): |
Nabin Hait | 96b264b | 2018-01-02 11:50:29 +0530 | [diff] [blame] | 678 | 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] | 679 | else: |
Nabin Hait | 96b264b | 2018-01-02 11:50:29 +0530 | [diff] [blame] | 680 | frappe.throw(_("Please enter Reqd by Date")) |
Rushabh Mehta | 30dc9a1 | 2017-11-17 14:31:09 +0530 | [diff] [blame] | 681 | |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 682 | def validate_items(self): |
| 683 | # 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] | 684 | if self.doctype=="Material Request": return |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 685 | |
rohitwaghchaure | 50d8c4a | 2018-06-05 13:08:10 +0530 | [diff] [blame] | 686 | if hasattr(self, "is_subcontracted") and self.is_subcontracted == 'Yes': |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 687 | validate_item_type(self, "is_sub_contracted_item", "subcontracted") |
| 688 | else: |
| 689 | validate_item_type(self, "is_purchase_item", "purchase") |
| 690 | |
rohitwaghchaure | bbd9b71 | 2018-03-07 15:39:40 +0530 | [diff] [blame] | 691 | def get_items_from_bom(item_code, bom, exploded_item=1): |
| 692 | doctype = "BOM Item" if not exploded_item else "BOM Explosion Item" |
| 693 | |
| 694 | bom_items = frappe.db.sql("""select t2.item_code, t2.name, |
| 695 | t2.rate, t2.stock_uom, t2.source_warehouse, t2.description, |
| 696 | t2.stock_qty / ifnull(t1.quantity, 1) as qty_consumed_per_unit |
| 697 | from |
| 698 | `tabBOM` t1, `tab{0}` t2, tabItem t3 |
| 699 | where |
| 700 | t2.parent = t1.name and t1.item = %s |
| 701 | and t1.docstatus = 1 and t1.is_active = 1 and t1.name = %s |
| 702 | and t2.item_code = t3.name and t3.is_stock_item = 1""".format(doctype), |
| 703 | (item_code, bom), as_dict=1) |
| 704 | |
| 705 | if not bom_items: |
| 706 | msgprint(_("Specified BOM {0} does not exist for Item {1}").format(bom, item_code), raise_exception=1) |
| 707 | |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 708 | return bom_items |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 709 | |
rohitwaghchaure | 2da6b3d | 2018-06-05 13:06:52 +0530 | [diff] [blame] | 710 | def get_subcontracted_raw_materials_from_se(purchase_orders): |
| 711 | return frappe.db.sql(""" |
| 712 | select |
| 713 | sed.item_name, sed.item_code, sum(sed.qty) as qty, sed.description, |
| 714 | sed.stock_uom, sed.subcontracted_item as main_item_code, sed.serial_no, sed.batch_no |
| 715 | from `tabStock Entry` se,`tabStock Entry Detail` sed |
| 716 | where |
| 717 | se.name = sed.parent and se.docstatus=1 and se.purpose='Subcontract' |
Rohit Waghchaure | ed71c36 | 2018-06-28 18:36:35 +0530 | [diff] [blame] | 718 | and se.purchase_order in (%s) and ifnull(sed.t_warehouse, '') != '' |
rohitwaghchaure | 2da6b3d | 2018-06-05 13:06:52 +0530 | [diff] [blame] | 719 | group by sed.item_code, sed.t_warehouse |
| 720 | """ % (','.join(['%s'] * len(purchase_orders))), tuple(purchase_orders), as_dict=1) |
| 721 | |
| 722 | def get_backflushed_subcontracted_raw_materials_from_se(purchase_orders, purchase_receipt): |
| 723 | return frappe._dict(frappe.db.sql(""" |
| 724 | select |
| 725 | prsi.rm_item_code as item_code, sum(prsi.consumed_qty) as qty |
| 726 | from `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pri, `tabPurchase Receipt Item Supplied` prsi |
| 727 | where |
Rohit Waghchaure | ed71c36 | 2018-06-28 18:36:35 +0530 | [diff] [blame] | 728 | pr.name = pri.parent and pr.name = prsi.parent and pri.purchase_order in (%s) |
rohitwaghchaure | 01fe4a3 | 2018-07-01 16:41:39 +0530 | [diff] [blame] | 729 | and pri.item_code = prsi.main_item_code and pr.name != '%s' and pr.docstatus = 1 |
rohitwaghchaure | 2da6b3d | 2018-06-05 13:06:52 +0530 | [diff] [blame] | 730 | group by prsi.rm_item_code |
| 731 | """ % (','.join(['%s'] * len(purchase_orders)), purchase_receipt), tuple(purchase_orders))) |
| 732 | |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 733 | def get_asset_item_details(asset_items): |
| 734 | asset_items_data = {} |
| 735 | for d in frappe.get_all('Item', fields = ["name", "has_serial_no", "serial_no_series"], |
| 736 | filters = {'name': ('in', asset_items)}): |
| 737 | asset_items_data.setdefault(d.name, d) |
| 738 | |
| 739 | return asset_items_data |
Ameya Shenoy | 873e28d | 2018-06-06 05:53:19 +0000 | [diff] [blame] | 740 | |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 741 | def validate_item_type(doc, fieldname, message): |
| 742 | # iterate through items and check if they are valid sales or purchase items |
Zarrar | 7c088ff | 2018-06-05 10:32:09 +0530 | [diff] [blame] | 743 | items = [d.item_code for d in doc.items if d.item_code] |
| 744 | |
| 745 | # No validation check inase of creating transaction using 'Opening Invoice Creation Tool' |
| 746 | if not items: |
| 747 | return |
| 748 | |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 749 | item_list = ", ".join(["'%s'" % frappe.db.escape(d) for d in items]) |
| 750 | |
| 751 | invalid_items = [d[0] for d in frappe.db.sql(""" |
| 752 | select item_code from tabItem where name in ({0}) and {1}=0 |
| 753 | """.format(item_list, fieldname), as_list=True)] |
| 754 | |
| 755 | if invalid_items: |
Faris Ansari | 2ab6e0e | 2018-09-19 13:13:59 +0530 | [diff] [blame] | 756 | items = ", ".join([d for d in invalid_items]) |
| 757 | |
| 758 | if len(invalid_items) > 1: |
| 759 | 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)) |
| 760 | else: |
| 761 | 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)) |
| 762 | |
| 763 | frappe.throw(error_message) |