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 |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 13 | |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 14 | from erpnext.controllers.stock_controller import StockController |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 15 | |
Nabin Hait | 89a94d8 | 2013-03-19 12:01:46 +0530 | [diff] [blame] | 16 | class BuyingController(StockController): |
Rushabh Mehta | 5b51cc8 | 2014-07-21 18:25:45 +0530 | [diff] [blame] | 17 | def __setup__(self): |
Nabin Hait | de9c8a9 | 2015-02-23 01:06:00 +0530 | [diff] [blame] | 18 | if hasattr(self, "taxes"): |
Nabin Hait | 45dce89 | 2017-09-14 15:17:38 +0530 | [diff] [blame] | 19 | self.flags.print_taxes_with_zero_amount = cint(frappe.db.get_single_value("Print Settings", |
| 20 | "print_taxes_with_zero_amount")) |
rohitwaghchaure | bf4c114 | 2018-01-08 15:20:15 +0530 | [diff] [blame] | 21 | self.flags.show_inclusive_tax_in_print = self.is_inclusive_tax() |
| 22 | |
Rushabh Mehta | c567e8e | 2015-02-03 17:55:52 +0530 | [diff] [blame] | 23 | self.print_templates = { |
rohitwaghchaure | bf4c114 | 2018-01-08 15:20:15 +0530 | [diff] [blame] | 24 | "total": "templates/print_formats/includes/total.html", |
Rushabh Mehta | c567e8e | 2015-02-03 17:55:52 +0530 | [diff] [blame] | 25 | "taxes": "templates/print_formats/includes/taxes.html" |
Rushabh Mehta | 5b51cc8 | 2014-07-21 18:25:45 +0530 | [diff] [blame] | 26 | } |
| 27 | |
Rushabh Mehta | c0bb453 | 2014-09-09 16:15:35 +0530 | [diff] [blame] | 28 | def get_feed(self): |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 29 | if self.get("supplier_name"): |
| 30 | return _("From {0} | {1} {2}").format(self.supplier_name, self.currency, |
| 31 | self.grand_total) |
Rushabh Mehta | c0bb453 | 2014-09-09 16:15:35 +0530 | [diff] [blame] | 32 | |
Saurabh | 6f75318 | 2013-03-20 12:55:28 +0530 | [diff] [blame] | 33 | def validate(self): |
| 34 | super(BuyingController, self).validate() |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 35 | if getattr(self, "supplier", None) and not self.supplier_name: |
Nabin Hait | 1d21842 | 2015-07-17 15:19:02 +0530 | [diff] [blame] | 36 | self.supplier_name = frappe.db.get_value("Supplier", self.supplier, "supplier_name") |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 37 | |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 38 | self.validate_items() |
nabinhait | 614fb75 | 2014-07-14 10:47:50 +0530 | [diff] [blame] | 39 | self.set_qty_as_per_stock_uom() |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 40 | self.validate_stock_or_nonstock_items() |
Anand Doshi | 373680b | 2013-10-10 16:04:40 +0530 | [diff] [blame] | 41 | self.validate_warehouse() |
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"): |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 58 | self.update_valuation_rate("items") |
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): |
Nabin Hait | 7eba1a3 | 2017-10-02 15:59:27 +0530 | [diff] [blame] | 68 | 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] | 69 | |
Nabin Hait | cccc45e | 2016-10-05 17:15:43 +0530 | [diff] [blame] | 70 | self.set_missing_item_details(for_validate) |
Rushabh Mehta | 436467a | 2013-07-08 12:37:59 +0530 | [diff] [blame] | 71 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 72 | def set_supplier_from_item_default(self): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 73 | if self.meta.get_field("supplier") and not self.supplier: |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 74 | for d in self.get("items"): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 75 | supplier = frappe.db.get_value("Item", d.item_code, "default_supplier") |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 76 | if supplier: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 77 | self.supplier = supplier |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 78 | break |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 79 | |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 80 | def validate_stock_or_nonstock_items(self): |
Nabin Hait | e7d1536 | 2014-12-25 16:01:55 +0530 | [diff] [blame] | 81 | if self.meta.get_field("taxes") and not self.get_stock_items(): |
tundebabzy | 9a34620 | 2017-06-16 11:00:14 +0100 | [diff] [blame] | 82 | tax_for_valuation = [d for d in self.get("taxes") |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 83 | if d.category in ["Valuation", "Valuation and Total"]] |
tundebabzy | 9a34620 | 2017-06-16 11:00:14 +0100 | [diff] [blame] | 84 | |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 85 | if tax_for_valuation: |
tundebabzy | 9a34620 | 2017-06-16 11:00:14 +0100 | [diff] [blame] | 86 | for d in tax_for_valuation: |
| 87 | d.category = 'Total' |
| 88 | 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] | 89 | |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 90 | def set_landed_cost_voucher_amount(self): |
| 91 | for d in self.get("items"): |
rohitwaghchaure | 2e8232e | 2017-08-10 11:32:59 +0530 | [diff] [blame] | 92 | lc_voucher_data = frappe.db.sql("""select sum(applicable_charges), cost_center |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 93 | from `tabLanded Cost Item` |
| 94 | where docstatus = 1 and purchase_receipt_item = %s""", d.name) |
rohitwaghchaure | 2e8232e | 2017-08-10 11:32:59 +0530 | [diff] [blame] | 95 | d.landed_cost_voucher_amount = lc_voucher_data[0][0] if lc_voucher_data else 0.0 |
| 96 | if not d.cost_center and lc_voucher_data and lc_voucher_data[0][1]: |
| 97 | d.db_set('cost_center', lc_voucher_data[0][1]) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 98 | |
Nabin Hait | d3b6250 | 2013-01-21 17:24:31 +0530 | [diff] [blame] | 99 | def set_total_in_words(self): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 100 | from frappe.utils import money_in_words |
Nabin Hait | 5690be1 | 2015-02-12 16:09:11 +0530 | [diff] [blame] | 101 | if self.meta.get_field("base_in_words"): |
rohitwaghchaure | ae4ff5a | 2018-08-01 18:09:51 +0530 | [diff] [blame] | 102 | amount = (self.base_rounded_total |
| 103 | if not self.get("disable_rounded_total") else self.base_grand_total) |
| 104 | |
| 105 | self.base_in_words = money_in_words(amount, self.company_currency) |
| 106 | |
Nabin Hait | 5b4c294 | 2013-01-22 11:12:02 +0530 | [diff] [blame] | 107 | if self.meta.get_field("in_words"): |
rohitwaghchaure | ae4ff5a | 2018-08-01 18:09:51 +0530 | [diff] [blame] | 108 | amount = (self.rounded_total |
| 109 | if not self.get("disable_rounded_total") else self.grand_total) |
| 110 | |
| 111 | self.in_words = money_in_words(amount, self.currency) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 112 | |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 113 | # update valuation rate |
| 114 | def update_valuation_rate(self, parentfield): |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 115 | """ |
| 116 | item_tax_amount is the total tax amount applied on that item |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 117 | stored for valuation |
| 118 | |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 119 | TODO: rename item_tax_amount to valuation_tax_amount |
| 120 | """ |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 121 | stock_items = self.get_stock_items() |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 122 | |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 123 | stock_items_qty, stock_items_amount = 0, 0 |
| 124 | last_stock_item_idx = 1 |
Rushabh Mehta | d2b34dc | 2014-03-27 16:12:56 +0530 | [diff] [blame] | 125 | for d in self.get(parentfield): |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 126 | if d.item_code and d.item_code in stock_items: |
| 127 | stock_items_qty += flt(d.qty) |
Nabin Hait | 82e3e25 | 2015-02-23 16:58:30 +0530 | [diff] [blame] | 128 | stock_items_amount += flt(d.base_net_amount) |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 129 | last_stock_item_idx = d.idx |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 130 | |
Nabin Hait | 82e3e25 | 2015-02-23 16:58:30 +0530 | [diff] [blame] | 131 | 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] | 132 | if d.category in ["Valuation", "Valuation and Total"]]) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 133 | |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 134 | valuation_amount_adjustment = total_valuation_amount |
Rushabh Mehta | d2b34dc | 2014-03-27 16:12:56 +0530 | [diff] [blame] | 135 | for i, item in enumerate(self.get(parentfield)): |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 136 | 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] | 137 | 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] | 138 | else flt(item.qty) / stock_items_qty |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 139 | if i == (last_stock_item_idx - 1): |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 140 | item.item_tax_amount = flt(valuation_amount_adjustment, |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 141 | self.precision("item_tax_amount", item)) |
| 142 | else: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 143 | item.item_tax_amount = flt(item_proportion * total_valuation_amount, |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 144 | self.precision("item_tax_amount", item)) |
| 145 | valuation_amount_adjustment -= item.item_tax_amount |
| 146 | |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 147 | self.round_floats_in(item) |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 148 | if flt(item.conversion_factor)==0.0: |
bobzz-zone | b4c7bad | 2015-08-05 11:30:12 +0700 | [diff] [blame] | 149 | 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] | 150 | |
Rushabh Mehta | 5404778 | 2013-12-26 11:07:46 +0530 | [diff] [blame] | 151 | qty_in_stock_uom = flt(item.qty * item.conversion_factor) |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 152 | 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] | 153 | |
nabinhait | 87f2401 | 2014-07-16 19:48:29 +0530 | [diff] [blame] | 154 | landed_cost_voucher_amount = flt(item.landed_cost_voucher_amount) \ |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 155 | if self.doctype in ["Purchase Receipt", "Purchase Invoice"] else 0.0 |
Nabin Hait | 14b8af2 | 2014-08-28 12:42:28 +0530 | [diff] [blame] | 156 | |
Nabin Hait | 82e3e25 | 2015-02-23 16:58:30 +0530 | [diff] [blame] | 157 | 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] | 158 | + landed_cost_voucher_amount) / qty_in_stock_uom) |
Anand Doshi | 4a7248e | 2013-02-27 18:10:30 +0530 | [diff] [blame] | 159 | else: |
Anand Doshi | 5af812a | 2013-05-10 19:23:02 +0530 | [diff] [blame] | 160 | item.valuation_rate = 0.0 |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 161 | |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 162 | def validate_for_subcontracting(self): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 163 | if not self.is_subcontracted and self.sub_contracted_items: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 164 | frappe.throw(_("Please enter 'Is Subcontracted' as Yes or No")) |
| 165 | |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 166 | if self.is_subcontracted == "Yes": |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 167 | 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] | 168 | frappe.throw(_("Supplier Warehouse mandatory for sub-contracted Purchase Receipt")) |
| 169 | |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 170 | for item in self.get("items"): |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 171 | if item in self.sub_contracted_items and not item.bom: |
| 172 | frappe.throw(_("Please select BOM in BOM field for Item {0}").format(item.item_code)) |
| 173 | |
Nabin Hait | 2c7a6e6 | 2018-03-12 14:12:12 +0530 | [diff] [blame] | 174 | if self.doctype == "Purchase Order": |
| 175 | for supplied_item in self.get("supplied_items"): |
| 176 | if not supplied_item.reserve_warehouse: |
| 177 | frappe.throw(_("Reserved Warehouse is mandatory for Item {0} in Raw Materials supplied").format(frappe.bold(supplied_item.rm_item_code))) |
| 178 | |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 179 | else: |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 180 | for item in self.get("items"): |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 181 | if item.bom: |
| 182 | item.bom = None |
| 183 | |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 184 | def create_raw_materials_supplied(self, raw_material_table): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 185 | if self.is_subcontracted=="Yes": |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 186 | parent_items = [] |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 187 | for item in self.get("items"): |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 188 | if self.doctype in ["Purchase Receipt", "Purchase Invoice"]: |
Nabin Hait | a0c239d | 2014-04-01 18:54:38 +0530 | [diff] [blame] | 189 | item.rm_supp_cost = 0.0 |
rohitwaghchaure | 4dc5f0e | 2017-11-22 15:21:47 +0530 | [diff] [blame] | 190 | if item.bom and item.item_code in self.sub_contracted_items: |
Rushabh Mehta | e693f79 | 2015-03-03 15:50:03 +0530 | [diff] [blame] | 191 | self.update_raw_materials_supplied(item, raw_material_table) |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 192 | |
| 193 | if [item.item_code, item.name] not in parent_items: |
| 194 | parent_items.append([item.item_code, item.name]) |
| 195 | |
| 196 | self.cleanup_raw_materials_supplied(parent_items, raw_material_table) |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 197 | |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 198 | elif self.doctype in ["Purchase Receipt", "Purchase Invoice"]: |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 199 | for item in self.get("items"): |
Nabin Hait | a0c239d | 2014-04-01 18:54:38 +0530 | [diff] [blame] | 200 | item.rm_supp_cost = 0.0 |
| 201 | |
Rohit Waghchaure | 4b9d2f2 | 2017-02-16 15:53:40 +0530 | [diff] [blame] | 202 | if self.is_subcontracted == "No" and self.get("supplied_items"): |
| 203 | self.set('supplied_items', []) |
| 204 | |
Rushabh Mehta | e693f79 | 2015-03-03 15:50:03 +0530 | [diff] [blame] | 205 | def update_raw_materials_supplied(self, item, raw_material_table): |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 206 | bom_items = self.get_items_from_bom(item.item_code, item.bom) |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 207 | raw_materials_cost = 0 |
Nabin Hait | 2c7a6e6 | 2018-03-12 14:12:12 +0530 | [diff] [blame] | 208 | items = list(set([d.item_code for d in bom_items])) |
| 209 | item_wh = frappe._dict(frappe.db.sql("""select item_code, default_warehouse |
| 210 | from `tabItem` where name in ({0})""".format(", ".join(["%s"] * len(items))), items)) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 211 | |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 212 | for bom_item in bom_items: |
Nabin Hait | 2c7a6e6 | 2018-03-12 14:12:12 +0530 | [diff] [blame] | 213 | if self.doctype == "Purchase Order": |
| 214 | reserve_warehouse = bom_item.source_warehouse or item_wh.get(bom_item.item_code) |
| 215 | if frappe.db.get_value("Warehouse", reserve_warehouse, "company") != self.company: |
| 216 | reserve_warehouse = None |
| 217 | |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 218 | # check if exists |
| 219 | exists = 0 |
| 220 | for d in self.get(raw_material_table): |
| 221 | if d.main_item_code == item.item_code and d.rm_item_code == bom_item.item_code \ |
| 222 | and d.reference_name == item.name: |
| 223 | rm, exists = d, 1 |
| 224 | break |
| 225 | |
| 226 | if not exists: |
| 227 | rm = self.append(raw_material_table, {}) |
| 228 | |
Nabin Hait | 1b89be0 | 2017-11-30 15:59:56 +0530 | [diff] [blame] | 229 | required_qty = flt(flt(bom_item.qty_consumed_per_unit) * flt(item.qty) * |
| 230 | flt(item.conversion_factor), rm.precision("required_qty")) |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 231 | rm.reference_name = item.name |
| 232 | rm.bom_detail_no = bom_item.name |
| 233 | rm.main_item_code = item.item_code |
| 234 | rm.rm_item_code = bom_item.item_code |
| 235 | rm.stock_uom = bom_item.stock_uom |
| 236 | rm.required_qty = required_qty |
Nabin Hait | 2c7a6e6 | 2018-03-12 14:12:12 +0530 | [diff] [blame] | 237 | if self.doctype == "Purchase Order" and not rm.reserve_warehouse: |
| 238 | rm.reserve_warehouse = reserve_warehouse |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 239 | |
| 240 | rm.conversion_factor = item.conversion_factor |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 241 | |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 242 | if self.doctype in ["Purchase Receipt", "Purchase Invoice"]: |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 243 | rm.consumed_qty = required_qty |
| 244 | rm.description = bom_item.description |
| 245 | if item.batch_no and not rm.batch_no: |
| 246 | rm.batch_no = item.batch_no |
| 247 | |
Nabin Hait | e96e83d | 2014-10-08 18:06:14 +0530 | [diff] [blame] | 248 | # get raw materials rate |
Nabin Hait | fce2881 | 2014-10-08 18:38:27 +0530 | [diff] [blame] | 249 | if self.doctype == "Purchase Receipt": |
| 250 | from erpnext.stock.utils import get_incoming_rate |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 251 | rm.rate = get_incoming_rate({ |
Nabin Hait | fce2881 | 2014-10-08 18:38:27 +0530 | [diff] [blame] | 252 | "item_code": bom_item.item_code, |
| 253 | "warehouse": self.supplier_warehouse, |
| 254 | "posting_date": self.posting_date, |
| 255 | "posting_time": self.posting_time, |
| 256 | "qty": -1 * required_qty, |
| 257 | "serial_no": rm.serial_no |
| 258 | }) |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 259 | if not rm.rate: |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 260 | rm.rate = get_valuation_rate(bom_item.item_code, self.supplier_warehouse, |
Rohit Waghchaure | a5f4094 | 2017-06-16 15:21:36 +0530 | [diff] [blame] | 261 | self.doctype, self.name, currency=self.company_currency, company = self.company) |
Nabin Hait | fce2881 | 2014-10-08 18:38:27 +0530 | [diff] [blame] | 262 | else: |
| 263 | rm.rate = bom_item.rate |
Nabin Hait | e96e83d | 2014-10-08 18:06:14 +0530 | [diff] [blame] | 264 | |
Nabin Hait | fce2881 | 2014-10-08 18:38:27 +0530 | [diff] [blame] | 265 | rm.amount = required_qty * flt(rm.rate) |
Nabin Hait | e96e83d | 2014-10-08 18:06:14 +0530 | [diff] [blame] | 266 | raw_materials_cost += flt(rm.amount) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 267 | |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 268 | if self.doctype in ("Purchase Receipt", "Purchase Invoice"): |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 269 | item.rm_supp_cost = raw_materials_cost |
| 270 | |
| 271 | def cleanup_raw_materials_supplied(self, parent_items, raw_material_table): |
| 272 | """Remove all those child items which are no longer present in main item table""" |
| 273 | delete_list = [] |
| 274 | for d in self.get(raw_material_table): |
| 275 | if [d.main_item_code, d.reference_name] not in parent_items: |
| 276 | # mark for deletion from doclist |
Nabin Hait | c3d1d6a | 2014-06-04 16:41:22 +0530 | [diff] [blame] | 277 | delete_list.append(d) |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 278 | |
| 279 | # delete from doclist |
| 280 | if delete_list: |
| 281 | rm_supplied_details = self.get(raw_material_table) |
| 282 | self.set(raw_material_table, []) |
| 283 | for d in rm_supplied_details: |
| 284 | if d not in delete_list: |
| 285 | self.append(raw_material_table, d) |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 286 | |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 287 | def get_items_from_bom(self, item_code, bom): |
Nabin Hait | 14b8af2 | 2014-08-28 12:42:28 +0530 | [diff] [blame] | 288 | bom_items = frappe.db.sql("""select t2.item_code, |
Ben Cornwell-Mott | 0f0b121 | 2017-05-23 15:15:17 -0700 | [diff] [blame] | 289 | t2.stock_qty / ifnull(t1.quantity, 1) as qty_consumed_per_unit, |
Nabin Hait | 2c7a6e6 | 2018-03-12 14:12:12 +0530 | [diff] [blame] | 290 | t2.rate, t2.stock_uom, t2.name, t2.description, t2.source_warehouse |
Nabin Hait | 574d5ff | 2015-05-07 16:29:29 +0530 | [diff] [blame] | 291 | from `tabBOM` t1, `tabBOM Item` t2, tabItem t3 |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 292 | where t2.parent = t1.name and t1.item = %s |
Rushabh Mehta | 1e8025b | 2015-07-24 15:16:25 +0530 | [diff] [blame] | 293 | and t1.docstatus = 1 and t1.is_active = 1 and t1.name = %s |
| 294 | and t2.item_code = t3.name and t3.is_stock_item = 1""", (item_code, bom), as_dict=1) |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 295 | |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 296 | if not bom_items: |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 297 | msgprint(_("Specified BOM {0} does not exist for Item {1}").format(bom, item_code), raise_exception=1) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 298 | |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 299 | return bom_items |
| 300 | |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 301 | @property |
| 302 | def sub_contracted_items(self): |
| 303 | if not hasattr(self, "_sub_contracted_items"): |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 304 | self._sub_contracted_items = [] |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 305 | item_codes = list(set(item.item_code for item in |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 306 | self.get("items"))) |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 307 | if item_codes: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 308 | 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] | 309 | from `tabItem` where name in (%s) and is_sub_contracted_item=1""" % \ |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 310 | (", ".join((["%s"]*len(item_codes))),), item_codes)] |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 311 | |
| 312 | return self._sub_contracted_items |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 313 | |
nabinhait | 614fb75 | 2014-07-14 10:47:50 +0530 | [diff] [blame] | 314 | def set_qty_as_per_stock_uom(self): |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 315 | for d in self.get("items"): |
Nabin Hait | fd4bcd8 | 2015-05-22 16:55:40 +0530 | [diff] [blame] | 316 | if d.meta.get_field("stock_qty"): |
nabinhait | 614fb75 | 2014-07-14 10:47:50 +0530 | [diff] [blame] | 317 | if not d.conversion_factor: |
Nabin Hait | e6e456b | 2015-05-13 17:21:44 +0530 | [diff] [blame] | 318 | frappe.throw(_("Row {0}: Conversion Factor is mandatory").format(d.idx)) |
Rushabh Mehta | 5b51cc8 | 2014-07-21 18:25:45 +0530 | [diff] [blame] | 319 | d.stock_qty = flt(d.qty) * flt(d.conversion_factor) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 320 | |
Saurabh | 130c57b | 2016-04-04 15:49:36 +0530 | [diff] [blame] | 321 | def validate_purchase_return(self): |
| 322 | for d in self.get("items"): |
| 323 | if self.is_return and flt(d.rejected_qty) != 0: |
| 324 | frappe.throw(_("Row #{0}: Rejected Qty can not be entered in Purchase Return").format(d.idx)) |
| 325 | |
| 326 | # validate rate with ref PR |
| 327 | |
| 328 | def validate_rejected_warehouse(self): |
| 329 | for d in self.get("items"): |
| 330 | if flt(d.rejected_qty) and not d.rejected_warehouse: |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 331 | if self.rejected_warehouse: |
| 332 | d.rejected_warehouse = self.rejected_warehouse |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 333 | |
Saurabh | 130c57b | 2016-04-04 15:49:36 +0530 | [diff] [blame] | 334 | if not d.rejected_warehouse: |
| 335 | frappe.throw(_("Row #{0}: Rejected Warehouse is mandatory against rejected Item {1}").format(d.idx, d.item_code)) |
| 336 | |
| 337 | # validate accepted and rejected qty |
| 338 | def validate_accepted_rejected_qty(self): |
| 339 | for d in self.get("items"): |
Saurabh | 4f4d0a8 | 2017-03-13 14:31:48 +0530 | [diff] [blame] | 340 | self.validate_negative_quantity(d, ["received_qty","qty", "rejected_qty"]) |
Saurabh | 130c57b | 2016-04-04 15:49:36 +0530 | [diff] [blame] | 341 | if not flt(d.received_qty) and flt(d.qty): |
| 342 | d.received_qty = flt(d.qty) - flt(d.rejected_qty) |
| 343 | |
| 344 | elif not flt(d.qty) and flt(d.rejected_qty): |
| 345 | d.qty = flt(d.received_qty) - flt(d.rejected_qty) |
| 346 | |
| 347 | elif not flt(d.rejected_qty): |
| 348 | d.rejected_qty = flt(d.received_qty) - flt(d.qty) |
| 349 | |
| 350 | # Check Received Qty = Accepted Qty + Rejected Qty |
| 351 | if ((flt(d.qty) + flt(d.rejected_qty)) != flt(d.received_qty)): |
| 352 | 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] | 353 | |
Saurabh | 4f4d0a8 | 2017-03-13 14:31:48 +0530 | [diff] [blame] | 354 | def validate_negative_quantity(self, item_row, field_list): |
| 355 | if self.is_return: |
| 356 | return |
| 357 | |
| 358 | item_row = item_row.as_dict() |
| 359 | for fieldname in field_list: |
| 360 | if flt(item_row[fieldname]) < 0: |
| 361 | frappe.throw(_("Row #{0}: {1} can not be negative for item {2}".format(item_row['idx'], |
| 362 | frappe.get_meta(item_row.doctype).get_label(fieldname), item_row['item_code']))) |
| 363 | |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 364 | def update_stock_ledger(self, allow_negative_stock=False, via_landed_cost_voucher=False): |
Nabin Hait | 2c7a6e6 | 2018-03-12 14:12:12 +0530 | [diff] [blame] | 365 | self.update_ordered_and_reserved_qty() |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 366 | |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 367 | sl_entries = [] |
| 368 | stock_items = self.get_stock_items() |
| 369 | |
| 370 | for d in self.get('items'): |
| 371 | if d.item_code in stock_items and d.warehouse: |
| 372 | pr_qty = flt(d.qty) * flt(d.conversion_factor) |
| 373 | |
| 374 | if pr_qty: |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 375 | sle = self.get_sl_entries(d, { |
| 376 | "actual_qty": flt(pr_qty), |
| 377 | "serial_no": cstr(d.serial_no).strip() |
| 378 | }) |
| 379 | if self.is_return: |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 380 | original_incoming_rate = frappe.db.get_value("Stock Ledger Entry", |
| 381 | {"voucher_type": "Purchase Receipt", "voucher_no": self.return_against, |
Nabin Hait | b81ed45 | 2016-05-11 12:52:31 +0530 | [diff] [blame] | 382 | "item_code": d.item_code}, "incoming_rate") |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 383 | |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 384 | sle.update({ |
Nabin Hait | b81ed45 | 2016-05-11 12:52:31 +0530 | [diff] [blame] | 385 | "outgoing_rate": original_incoming_rate |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 386 | }) |
| 387 | else: |
Nabin Hait | b81ed45 | 2016-05-11 12:52:31 +0530 | [diff] [blame] | 388 | val_rate_db_precision = 6 if cint(self.precision("valuation_rate", d)) <= 6 else 9 |
| 389 | incoming_rate = flt(d.valuation_rate, val_rate_db_precision) |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 390 | sle.update({ |
Nabin Hait | b81ed45 | 2016-05-11 12:52:31 +0530 | [diff] [blame] | 391 | "incoming_rate": incoming_rate |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 392 | }) |
| 393 | sl_entries.append(sle) |
| 394 | |
Rohit Waghchaure | 560ba39 | 2016-08-29 18:19:32 +0530 | [diff] [blame] | 395 | if flt(d.rejected_qty) != 0: |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 396 | sl_entries.append(self.get_sl_entries(d, { |
| 397 | "warehouse": d.rejected_warehouse, |
| 398 | "actual_qty": flt(d.rejected_qty) * flt(d.conversion_factor), |
| 399 | "serial_no": cstr(d.rejected_serial_no).strip(), |
| 400 | "incoming_rate": 0.0 |
| 401 | })) |
| 402 | |
| 403 | self.make_sl_entries_for_supplier_warehouse(sl_entries) |
| 404 | self.make_sl_entries(sl_entries, allow_negative_stock=allow_negative_stock, |
| 405 | via_landed_cost_voucher=via_landed_cost_voucher) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 406 | |
Nabin Hait | 2c7a6e6 | 2018-03-12 14:12:12 +0530 | [diff] [blame] | 407 | def update_ordered_and_reserved_qty(self): |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 408 | po_map = {} |
| 409 | for d in self.get("items"): |
| 410 | if self.doctype=="Purchase Receipt" \ |
Rohit Waghchaure | a71d9d3 | 2016-07-05 00:34:00 +0530 | [diff] [blame] | 411 | and d.purchase_order: |
| 412 | po_map.setdefault(d.purchase_order, []).append(d.purchase_order_item) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 413 | |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 414 | elif self.doctype=="Purchase Invoice" and d.purchase_order and d.po_detail: |
| 415 | po_map.setdefault(d.purchase_order, []).append(d.po_detail) |
| 416 | |
| 417 | for po, po_item_rows in po_map.items(): |
| 418 | if po and po_item_rows: |
| 419 | po_obj = frappe.get_doc("Purchase Order", po) |
| 420 | |
| 421 | if po_obj.status in ["Closed", "Cancelled"]: |
| 422 | frappe.throw(_("{0} {1} is cancelled or closed").format(_("Purchase Order"), po), |
| 423 | frappe.InvalidStatusError) |
| 424 | |
| 425 | po_obj.update_ordered_qty(po_item_rows) |
Nabin Hait | 2c7a6e6 | 2018-03-12 14:12:12 +0530 | [diff] [blame] | 426 | if self.is_subcontracted: |
| 427 | po_obj.update_reserved_qty_for_subcontract() |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 428 | |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 429 | def make_sl_entries_for_supplier_warehouse(self, sl_entries): |
| 430 | if hasattr(self, 'supplied_items'): |
| 431 | for d in self.get('supplied_items'): |
| 432 | # negative quantity is passed, as raw material qty has to be decreased |
| 433 | # when PR is submitted and it has to be increased when PR is cancelled |
| 434 | sl_entries.append(self.get_sl_entries(d, { |
| 435 | "item_code": d.rm_item_code, |
| 436 | "warehouse": self.supplier_warehouse, |
| 437 | "actual_qty": -1*flt(d.consumed_qty), |
| 438 | })) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 439 | |
rohitwaghchaure | fe22686 | 2017-12-28 16:11:27 +0530 | [diff] [blame] | 440 | def on_submit(self): |
| 441 | if self.get('is_return'): |
| 442 | return |
| 443 | |
| 444 | update_last_purchase_rate(self, is_submit = 1) |
| 445 | |
| 446 | def on_cancel(self): |
| 447 | if self.get('is_return'): |
| 448 | return |
| 449 | |
| 450 | update_last_purchase_rate(self, is_submit = 0) |
| 451 | |
Nabin Hait | 5a83420 | 2017-10-05 19:51:10 +0530 | [diff] [blame] | 452 | def validate_schedule_date(self): |
| 453 | if not self.schedule_date: |
| 454 | self.schedule_date = min([d.schedule_date for d in self.get("items")]) |
| 455 | |
| 456 | if self.schedule_date: |
| 457 | for d in self.get('items'): |
| 458 | if not d.schedule_date: |
| 459 | d.schedule_date = self.schedule_date |
| 460 | |
Prateeksha Singh | 8b94f1b | 2018-01-08 16:46:39 +0530 | [diff] [blame] | 461 | if (d.schedule_date and self.transaction_date and |
| 462 | getdate(d.schedule_date) < getdate(self.transaction_date)): |
Nabin Hait | 96b264b | 2018-01-02 11:50:29 +0530 | [diff] [blame] | 463 | 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] | 464 | else: |
Nabin Hait | 96b264b | 2018-01-02 11:50:29 +0530 | [diff] [blame] | 465 | frappe.throw(_("Please enter Reqd by Date")) |
Rushabh Mehta | 30dc9a1 | 2017-11-17 14:31:09 +0530 | [diff] [blame] | 466 | |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 467 | def validate_items(self): |
| 468 | # 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] | 469 | if self.doctype=="Material Request": return |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 470 | |
rohitwaghchaure | 50d8c4a | 2018-06-05 13:08:10 +0530 | [diff] [blame] | 471 | if hasattr(self, "is_subcontracted") and self.is_subcontracted == 'Yes': |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 472 | validate_item_type(self, "is_sub_contracted_item", "subcontracted") |
| 473 | else: |
| 474 | validate_item_type(self, "is_purchase_item", "purchase") |
| 475 | |
| 476 | def validate_item_type(doc, fieldname, message): |
| 477 | # iterate through items and check if they are valid sales or purchase items |
Zarrar | 7c088ff | 2018-06-05 10:32:09 +0530 | [diff] [blame] | 478 | items = [d.item_code for d in doc.items if d.item_code] |
| 479 | |
| 480 | # No validation check inase of creating transaction using 'Opening Invoice Creation Tool' |
| 481 | if not items: |
| 482 | return |
| 483 | |
Zarrar | fc03a04 | 2018-06-04 12:52:52 +0530 | [diff] [blame] | 484 | item_list = ", ".join(["'%s'" % frappe.db.escape(d) for d in items]) |
| 485 | |
| 486 | invalid_items = [d[0] for d in frappe.db.sql(""" |
| 487 | select item_code from tabItem where name in ({0}) and {1}=0 |
| 488 | """.format(item_list, fieldname), as_list=True)] |
| 489 | |
| 490 | if invalid_items: |
| 491 | frappe.throw(_("Following item {items} {verb} not marked as {message} item.\ |
| 492 | You can enable them as {message} item from its Item master".format( |
| 493 | items = ", ".join([d for d in invalid_items]), |
| 494 | verb = "are" if len(invalid_items) > 1 else "is", |
| 495 | message = message))) |