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