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 | |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 16 | from erpnext.controllers.stock_controller import StockController |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 17 | |
Nabin Hait | 89a94d8 | 2013-03-19 12:01:46 +0530 | [diff] [blame] | 18 | class BuyingController(StockController): |
Rushabh Mehta | 5b51cc8 | 2014-07-21 18:25:45 +0530 | [diff] [blame] | 19 | def __setup__(self): |
Nabin Hait | de9c8a9 | 2015-02-23 01:06:00 +0530 | [diff] [blame] | 20 | if hasattr(self, "taxes"): |
Nabin Hait | 45dce89 | 2017-09-14 15:17:38 +0530 | [diff] [blame] | 21 | self.flags.print_taxes_with_zero_amount = cint(frappe.db.get_single_value("Print Settings", |
| 22 | "print_taxes_with_zero_amount")) |
rohitwaghchaure | bf4c114 | 2018-01-08 15:20:15 +0530 | [diff] [blame] | 23 | self.flags.show_inclusive_tax_in_print = self.is_inclusive_tax() |
| 24 | |
Rushabh Mehta | c567e8e | 2015-02-03 17:55:52 +0530 | [diff] [blame] | 25 | self.print_templates = { |
rohitwaghchaure | bf4c114 | 2018-01-08 15:20:15 +0530 | [diff] [blame] | 26 | "total": "templates/print_formats/includes/total.html", |
Rushabh Mehta | c567e8e | 2015-02-03 17:55:52 +0530 | [diff] [blame] | 27 | "taxes": "templates/print_formats/includes/taxes.html" |
Rushabh Mehta | 5b51cc8 | 2014-07-21 18:25:45 +0530 | [diff] [blame] | 28 | } |
| 29 | |
Rushabh Mehta | c0bb453 | 2014-09-09 16:15:35 +0530 | [diff] [blame] | 30 | def get_feed(self): |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 31 | if self.get("supplier_name"): |
| 32 | return _("From {0} | {1} {2}").format(self.supplier_name, self.currency, |
| 33 | self.grand_total) |
Rushabh Mehta | c0bb453 | 2014-09-09 16:15:35 +0530 | [diff] [blame] | 34 | |
Saurabh | 6f75318 | 2013-03-20 12:55:28 +0530 | [diff] [blame] | 35 | def validate(self): |
| 36 | super(BuyingController, self).validate() |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 37 | if getattr(self, "supplier", None) and not self.supplier_name: |
Nabin Hait | 1d21842 | 2015-07-17 15:19:02 +0530 | [diff] [blame] | 38 | self.supplier_name = frappe.db.get_value("Supplier", self.supplier, "supplier_name") |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 39 | |
nabinhait | 614fb75 | 2014-07-14 10:47:50 +0530 | [diff] [blame] | 40 | self.set_qty_as_per_stock_uom() |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 41 | self.validate_stock_or_nonstock_items() |
Anand Doshi | 373680b | 2013-10-10 16:04:40 +0530 | [diff] [blame] | 42 | self.validate_warehouse() |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 43 | |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 44 | if self.doctype=="Purchase Invoice": |
| 45 | self.validate_purchase_receipt_if_update_stock() |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 46 | |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 47 | 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] | 48 | # self.validate_purchase_return() |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 49 | self.validate_rejected_warehouse() |
| 50 | self.validate_accepted_rejected_qty() |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 51 | validate_for_items(self) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 52 | |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 53 | #sub-contracting |
| 54 | self.validate_for_subcontracting() |
| 55 | self.create_raw_materials_supplied("supplied_items") |
| 56 | self.set_landed_cost_voucher_amount() |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 57 | |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 58 | if self.doctype in ("Purchase Receipt", "Purchase Invoice"): |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 59 | self.update_valuation_rate("items") |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 60 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 61 | def set_missing_values(self, for_validate=False): |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 62 | super(BuyingController, self).set_missing_values(for_validate) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 63 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 64 | self.set_supplier_from_item_default() |
Nabin Hait | 096d363 | 2013-10-17 17:01:14 +0530 | [diff] [blame] | 65 | self.set_price_list_currency("Buying") |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 66 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 67 | # set contact and address details for supplier, if they are not mentioned |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 68 | if getattr(self, "supplier", None): |
Nabin Hait | 7eba1a3 | 2017-10-02 15:59:27 +0530 | [diff] [blame] | 69 | 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] | 70 | |
Nabin Hait | cccc45e | 2016-10-05 17:15:43 +0530 | [diff] [blame] | 71 | self.set_missing_item_details(for_validate) |
Rushabh Mehta | 436467a | 2013-07-08 12:37:59 +0530 | [diff] [blame] | 72 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 73 | def set_supplier_from_item_default(self): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 74 | if self.meta.get_field("supplier") and not self.supplier: |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 75 | for d in self.get("items"): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 76 | supplier = frappe.db.get_value("Item", d.item_code, "default_supplier") |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 77 | if supplier: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 78 | self.supplier = supplier |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 79 | break |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 80 | |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 81 | def validate_stock_or_nonstock_items(self): |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 82 | 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] | 83 | tax_for_valuation = [d for d in self.get("taxes") |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 84 | if d.category in ["Valuation", "Valuation and Total"]] |
tundebabzy | 9a34620 | 2017-06-16 11:00:14 +0100 | [diff] [blame] | 85 | |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 86 | if tax_for_valuation: |
tundebabzy | 9a34620 | 2017-06-16 11:00:14 +0100 | [diff] [blame] | 87 | for d in tax_for_valuation: |
| 88 | d.category = 'Total' |
| 89 | 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] | 90 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 91 | def get_asset_items(self): |
Rohit Waghchaure | d644e6d | 2018-05-12 15:27:18 +0530 | [diff] [blame] | 92 | if self.doctype not in ['Purchase Invoice', 'Purchase Receipt']: |
| 93 | return [] |
| 94 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 95 | return [d.item_code for d in self.items if d.is_fixed_asset] |
| 96 | |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 97 | def set_landed_cost_voucher_amount(self): |
| 98 | for d in self.get("items"): |
rohitwaghchaure | 2e8232e | 2017-08-10 11:32:59 +0530 | [diff] [blame] | 99 | lc_voucher_data = frappe.db.sql("""select sum(applicable_charges), cost_center |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 100 | from `tabLanded Cost Item` |
| 101 | where docstatus = 1 and purchase_receipt_item = %s""", d.name) |
rohitwaghchaure | 2e8232e | 2017-08-10 11:32:59 +0530 | [diff] [blame] | 102 | d.landed_cost_voucher_amount = lc_voucher_data[0][0] if lc_voucher_data else 0.0 |
| 103 | if not d.cost_center and lc_voucher_data and lc_voucher_data[0][1]: |
| 104 | d.db_set('cost_center', lc_voucher_data[0][1]) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 105 | |
Nabin Hait | d3b6250 | 2013-01-21 17:24:31 +0530 | [diff] [blame] | 106 | def set_total_in_words(self): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 107 | from frappe.utils import money_in_words |
Nabin Hait | 5690be1 | 2015-02-12 16:09:11 +0530 | [diff] [blame] | 108 | if self.meta.get_field("base_in_words"): |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 109 | 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] | 110 | if self.meta.get_field("in_words"): |
Nabin Hait | 188f69a | 2015-02-12 17:55:50 +0530 | [diff] [blame] | 111 | self.in_words = money_in_words(self.grand_total, 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 | """ |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 121 | stock_items = self.get_stock_items() + self.get_asset_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 | 07e5376 | 2018-01-05 18:19:59 +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))) |
pawan | 54465f5 | 2017-11-29 10:18:38 +0530 | [diff] [blame] | 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): |
rohitwaghchaure | bbd9b71 | 2018-03-07 15:39:40 +0530 | [diff] [blame] | 206 | exploded_item = 1 |
| 207 | if hasattr(item, 'include_exploded_items'): |
| 208 | exploded_item = item.get('include_exploded_items') |
| 209 | |
| 210 | bom_items = get_items_from_bom(item.item_code, item.bom, exploded_item) |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 211 | |
| 212 | used_alternative_items = [] |
| 213 | if self.doctype == 'Purchase Receipt' and item.purchase_order: |
| 214 | used_alternative_items = get_used_alternative_items(purchase_order = item.purchase_order) |
| 215 | |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 216 | raw_materials_cost = 0 |
pawan | 2ff844e | 2017-11-30 18:14:55 +0530 | [diff] [blame] | 217 | items = list(set([d.item_code for d in bom_items])) |
| 218 | item_wh = frappe._dict(frappe.db.sql("""select item_code, default_warehouse |
Nabin Hait | 07e5376 | 2018-01-05 18:19:59 +0530 | [diff] [blame] | 219 | from `tabItem` where name in ({0})""".format(", ".join(["%s"] * len(items))), items)) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 220 | |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 221 | for bom_item in bom_items: |
Nabin Hait | 07e5376 | 2018-01-05 18:19:59 +0530 | [diff] [blame] | 222 | if self.doctype == "Purchase Order": |
| 223 | reserve_warehouse = bom_item.source_warehouse or item_wh.get(bom_item.item_code) |
| 224 | if frappe.db.get_value("Warehouse", reserve_warehouse, "company") != self.company: |
| 225 | reserve_warehouse = None |
| 226 | |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 227 | conversion_factor = item.conversion_factor |
| 228 | if (self.doctype == 'Purchase Receipt' and item.purchase_order and |
| 229 | bom_item.item_code in used_alternative_items): |
| 230 | alternative_item_data = used_alternative_items.get(bom_item.item_code) |
| 231 | bom_item.item_code = alternative_item_data.item_code |
| 232 | bom_item.item_name = alternative_item_data.item_name |
| 233 | bom_item.stock_uom = alternative_item_data.stock_uom |
| 234 | conversion_factor = alternative_item_data.conversion_factor |
| 235 | bom_item.description = alternative_item_data.description |
| 236 | |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 237 | # check if exists |
| 238 | exists = 0 |
| 239 | for d in self.get(raw_material_table): |
| 240 | if d.main_item_code == item.item_code and d.rm_item_code == bom_item.item_code \ |
| 241 | and d.reference_name == item.name: |
| 242 | rm, exists = d, 1 |
| 243 | break |
| 244 | |
| 245 | if not exists: |
| 246 | rm = self.append(raw_material_table, {}) |
| 247 | |
Fahim Ali Zain TP | 277935b | 2018-02-12 11:59:07 +0530 | [diff] [blame] | 248 | 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] | 249 | flt(conversion_factor), rm.precision("required_qty")) |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 250 | rm.reference_name = item.name |
| 251 | rm.bom_detail_no = bom_item.name |
| 252 | rm.main_item_code = item.item_code |
| 253 | rm.rm_item_code = bom_item.item_code |
| 254 | rm.stock_uom = bom_item.stock_uom |
| 255 | rm.required_qty = required_qty |
Nabin Hait | 07e5376 | 2018-01-05 18:19:59 +0530 | [diff] [blame] | 256 | if self.doctype == "Purchase Order" and not rm.reserve_warehouse: |
| 257 | rm.reserve_warehouse = reserve_warehouse |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 258 | |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 259 | rm.conversion_factor = conversion_factor |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 260 | |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 261 | if self.doctype in ["Purchase Receipt", "Purchase Invoice"]: |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 262 | rm.consumed_qty = required_qty |
| 263 | rm.description = bom_item.description |
| 264 | if item.batch_no and not rm.batch_no: |
| 265 | rm.batch_no = item.batch_no |
| 266 | |
Nabin Hait | e96e83d | 2014-10-08 18:06:14 +0530 | [diff] [blame] | 267 | # get raw materials rate |
Nabin Hait | fce2881 | 2014-10-08 18:38:27 +0530 | [diff] [blame] | 268 | if self.doctype == "Purchase Receipt": |
| 269 | from erpnext.stock.utils import get_incoming_rate |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 270 | rm.rate = get_incoming_rate({ |
Nabin Hait | fce2881 | 2014-10-08 18:38:27 +0530 | [diff] [blame] | 271 | "item_code": bom_item.item_code, |
| 272 | "warehouse": self.supplier_warehouse, |
| 273 | "posting_date": self.posting_date, |
| 274 | "posting_time": self.posting_time, |
| 275 | "qty": -1 * required_qty, |
| 276 | "serial_no": rm.serial_no |
| 277 | }) |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 278 | if not rm.rate: |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 279 | rm.rate = get_valuation_rate(bom_item.item_code, self.supplier_warehouse, |
Rohit Waghchaure | a5f4094 | 2017-06-16 15:21:36 +0530 | [diff] [blame] | 280 | self.doctype, self.name, currency=self.company_currency, company = self.company) |
Nabin Hait | fce2881 | 2014-10-08 18:38:27 +0530 | [diff] [blame] | 281 | else: |
| 282 | rm.rate = bom_item.rate |
Nabin Hait | e96e83d | 2014-10-08 18:06:14 +0530 | [diff] [blame] | 283 | |
Nabin Hait | fce2881 | 2014-10-08 18:38:27 +0530 | [diff] [blame] | 284 | rm.amount = required_qty * flt(rm.rate) |
Nabin Hait | e96e83d | 2014-10-08 18:06:14 +0530 | [diff] [blame] | 285 | raw_materials_cost += flt(rm.amount) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 286 | |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 287 | if self.doctype in ("Purchase Receipt", "Purchase Invoice"): |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 288 | item.rm_supp_cost = raw_materials_cost |
| 289 | |
| 290 | def cleanup_raw_materials_supplied(self, parent_items, raw_material_table): |
| 291 | """Remove all those child items which are no longer present in main item table""" |
| 292 | delete_list = [] |
| 293 | for d in self.get(raw_material_table): |
| 294 | if [d.main_item_code, d.reference_name] not in parent_items: |
| 295 | # mark for deletion from doclist |
Nabin Hait | c3d1d6a | 2014-06-04 16:41:22 +0530 | [diff] [blame] | 296 | delete_list.append(d) |
Nabin Hait | 344f443 | 2014-05-08 19:08:20 +0530 | [diff] [blame] | 297 | |
| 298 | # delete from doclist |
| 299 | if delete_list: |
| 300 | rm_supplied_details = self.get(raw_material_table) |
| 301 | self.set(raw_material_table, []) |
| 302 | for d in rm_supplied_details: |
| 303 | if d not in delete_list: |
| 304 | self.append(raw_material_table, d) |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 305 | |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 306 | @property |
| 307 | def sub_contracted_items(self): |
| 308 | if not hasattr(self, "_sub_contracted_items"): |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 309 | self._sub_contracted_items = [] |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 310 | item_codes = list(set(item.item_code for item in |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 311 | self.get("items"))) |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 312 | if item_codes: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 313 | 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] | 314 | from `tabItem` where name in (%s) and is_sub_contracted_item=1""" % \ |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 315 | (", ".join((["%s"]*len(item_codes))),), item_codes)] |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 316 | |
| 317 | return self._sub_contracted_items |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 318 | |
nabinhait | 614fb75 | 2014-07-14 10:47:50 +0530 | [diff] [blame] | 319 | def set_qty_as_per_stock_uom(self): |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 320 | for d in self.get("items"): |
Nabin Hait | fd4bcd8 | 2015-05-22 16:55:40 +0530 | [diff] [blame] | 321 | if d.meta.get_field("stock_qty"): |
nabinhait | 614fb75 | 2014-07-14 10:47:50 +0530 | [diff] [blame] | 322 | if not d.conversion_factor: |
Nabin Hait | e6e456b | 2015-05-13 17:21:44 +0530 | [diff] [blame] | 323 | frappe.throw(_("Row {0}: Conversion Factor is mandatory").format(d.idx)) |
Rushabh Mehta | 5b51cc8 | 2014-07-21 18:25:45 +0530 | [diff] [blame] | 324 | d.stock_qty = flt(d.qty) * flt(d.conversion_factor) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 325 | |
Saurabh | 130c57b | 2016-04-04 15:49:36 +0530 | [diff] [blame] | 326 | def validate_purchase_return(self): |
| 327 | for d in self.get("items"): |
| 328 | if self.is_return and flt(d.rejected_qty) != 0: |
| 329 | frappe.throw(_("Row #{0}: Rejected Qty can not be entered in Purchase Return").format(d.idx)) |
| 330 | |
| 331 | # validate rate with ref PR |
| 332 | |
| 333 | def validate_rejected_warehouse(self): |
| 334 | for d in self.get("items"): |
| 335 | if flt(d.rejected_qty) and not d.rejected_warehouse: |
Saurabh | fbb342c | 2016-04-07 16:41:31 +0530 | [diff] [blame] | 336 | if self.rejected_warehouse: |
| 337 | d.rejected_warehouse = self.rejected_warehouse |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 338 | |
Saurabh | 130c57b | 2016-04-04 15:49:36 +0530 | [diff] [blame] | 339 | if not d.rejected_warehouse: |
| 340 | frappe.throw(_("Row #{0}: Rejected Warehouse is mandatory against rejected Item {1}").format(d.idx, d.item_code)) |
| 341 | |
| 342 | # validate accepted and rejected qty |
| 343 | def validate_accepted_rejected_qty(self): |
| 344 | for d in self.get("items"): |
Saurabh | 4f4d0a8 | 2017-03-13 14:31:48 +0530 | [diff] [blame] | 345 | self.validate_negative_quantity(d, ["received_qty","qty", "rejected_qty"]) |
Saurabh | 130c57b | 2016-04-04 15:49:36 +0530 | [diff] [blame] | 346 | if not flt(d.received_qty) and flt(d.qty): |
| 347 | d.received_qty = flt(d.qty) - flt(d.rejected_qty) |
| 348 | |
| 349 | elif not flt(d.qty) and flt(d.rejected_qty): |
| 350 | d.qty = flt(d.received_qty) - flt(d.rejected_qty) |
| 351 | |
| 352 | elif not flt(d.rejected_qty): |
| 353 | d.rejected_qty = flt(d.received_qty) - flt(d.qty) |
| 354 | |
| 355 | # Check Received Qty = Accepted Qty + Rejected Qty |
| 356 | if ((flt(d.qty) + flt(d.rejected_qty)) != flt(d.received_qty)): |
| 357 | 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] | 358 | |
Saurabh | 4f4d0a8 | 2017-03-13 14:31:48 +0530 | [diff] [blame] | 359 | def validate_negative_quantity(self, item_row, field_list): |
| 360 | if self.is_return: |
| 361 | return |
| 362 | |
| 363 | item_row = item_row.as_dict() |
| 364 | for fieldname in field_list: |
| 365 | if flt(item_row[fieldname]) < 0: |
| 366 | frappe.throw(_("Row #{0}: {1} can not be negative for item {2}".format(item_row['idx'], |
| 367 | frappe.get_meta(item_row.doctype).get_label(fieldname), item_row['item_code']))) |
| 368 | |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 369 | 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] | 370 | self.update_ordered_and_reserved_qty() |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 371 | |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 372 | sl_entries = [] |
| 373 | stock_items = self.get_stock_items() |
| 374 | |
| 375 | for d in self.get('items'): |
| 376 | if d.item_code in stock_items and d.warehouse: |
| 377 | pr_qty = flt(d.qty) * flt(d.conversion_factor) |
| 378 | |
| 379 | if pr_qty: |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 380 | sle = self.get_sl_entries(d, { |
| 381 | "actual_qty": flt(pr_qty), |
| 382 | "serial_no": cstr(d.serial_no).strip() |
| 383 | }) |
| 384 | if self.is_return: |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 385 | original_incoming_rate = frappe.db.get_value("Stock Ledger Entry", |
| 386 | {"voucher_type": "Purchase Receipt", "voucher_no": self.return_against, |
Nabin Hait | b81ed45 | 2016-05-11 12:52:31 +0530 | [diff] [blame] | 387 | "item_code": d.item_code}, "incoming_rate") |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 388 | |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 389 | sle.update({ |
Nabin Hait | b81ed45 | 2016-05-11 12:52:31 +0530 | [diff] [blame] | 390 | "outgoing_rate": original_incoming_rate |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 391 | }) |
| 392 | else: |
Nabin Hait | b81ed45 | 2016-05-11 12:52:31 +0530 | [diff] [blame] | 393 | val_rate_db_precision = 6 if cint(self.precision("valuation_rate", d)) <= 6 else 9 |
| 394 | incoming_rate = flt(d.valuation_rate, val_rate_db_precision) |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 395 | sle.update({ |
Nabin Hait | b81ed45 | 2016-05-11 12:52:31 +0530 | [diff] [blame] | 396 | "incoming_rate": incoming_rate |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 397 | }) |
| 398 | sl_entries.append(sle) |
| 399 | |
Rohit Waghchaure | 560ba39 | 2016-08-29 18:19:32 +0530 | [diff] [blame] | 400 | if flt(d.rejected_qty) != 0: |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 401 | sl_entries.append(self.get_sl_entries(d, { |
| 402 | "warehouse": d.rejected_warehouse, |
| 403 | "actual_qty": flt(d.rejected_qty) * flt(d.conversion_factor), |
| 404 | "serial_no": cstr(d.rejected_serial_no).strip(), |
| 405 | "incoming_rate": 0.0 |
| 406 | })) |
| 407 | |
| 408 | self.make_sl_entries_for_supplier_warehouse(sl_entries) |
| 409 | self.make_sl_entries(sl_entries, allow_negative_stock=allow_negative_stock, |
| 410 | via_landed_cost_voucher=via_landed_cost_voucher) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 411 | |
Nabin Hait | 07e5376 | 2018-01-05 18:19:59 +0530 | [diff] [blame] | 412 | def update_ordered_and_reserved_qty(self): |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 413 | po_map = {} |
| 414 | for d in self.get("items"): |
| 415 | if self.doctype=="Purchase Receipt" \ |
Rohit Waghchaure | a71d9d3 | 2016-07-05 00:34:00 +0530 | [diff] [blame] | 416 | and d.purchase_order: |
| 417 | po_map.setdefault(d.purchase_order, []).append(d.purchase_order_item) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 418 | |
Nabin Hait | 14aa9c5 | 2016-04-18 15:54:01 +0530 | [diff] [blame] | 419 | elif self.doctype=="Purchase Invoice" and d.purchase_order and d.po_detail: |
| 420 | po_map.setdefault(d.purchase_order, []).append(d.po_detail) |
| 421 | |
| 422 | for po, po_item_rows in po_map.items(): |
| 423 | if po and po_item_rows: |
| 424 | po_obj = frappe.get_doc("Purchase Order", po) |
| 425 | |
| 426 | if po_obj.status in ["Closed", "Cancelled"]: |
| 427 | frappe.throw(_("{0} {1} is cancelled or closed").format(_("Purchase Order"), po), |
| 428 | frappe.InvalidStatusError) |
| 429 | |
| 430 | po_obj.update_ordered_qty(po_item_rows) |
Nabin Hait | 07e5376 | 2018-01-05 18:19:59 +0530 | [diff] [blame] | 431 | if self.is_subcontracted: |
| 432 | po_obj.update_reserved_qty_for_subcontract() |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 433 | |
Saurabh | e29248b | 2016-04-04 11:55:52 +0530 | [diff] [blame] | 434 | def make_sl_entries_for_supplier_warehouse(self, sl_entries): |
| 435 | if hasattr(self, 'supplied_items'): |
| 436 | for d in self.get('supplied_items'): |
| 437 | # negative quantity is passed, as raw material qty has to be decreased |
| 438 | # when PR is submitted and it has to be increased when PR is cancelled |
| 439 | sl_entries.append(self.get_sl_entries(d, { |
| 440 | "item_code": d.rm_item_code, |
| 441 | "warehouse": self.supplier_warehouse, |
| 442 | "actual_qty": -1*flt(d.consumed_qty), |
| 443 | })) |
Rushabh Mehta | b33df4a | 2016-07-04 11:38:37 +0530 | [diff] [blame] | 444 | |
rohitwaghchaure | fe22686 | 2017-12-28 16:11:27 +0530 | [diff] [blame] | 445 | def on_submit(self): |
| 446 | if self.get('is_return'): |
| 447 | return |
| 448 | |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 449 | if self.doctype in ['Purchase Receipt', 'Purchase Invoice']: |
Rohit Waghchaure | c6deb13 | 2018-05-07 15:58:41 +0530 | [diff] [blame] | 450 | field = 'purchase_invoice' if self.doctype == 'Purchase Invoice' else 'purchase_receipt' |
| 451 | |
| 452 | self.process_fixed_asset() |
| 453 | self.update_fixed_asset(field) |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 454 | |
rohitwaghchaure | fe22686 | 2017-12-28 16:11:27 +0530 | [diff] [blame] | 455 | update_last_purchase_rate(self, is_submit = 1) |
| 456 | |
| 457 | def on_cancel(self): |
| 458 | if self.get('is_return'): |
| 459 | return |
| 460 | |
| 461 | update_last_purchase_rate(self, is_submit = 0) |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 462 | if self.doctype in ['Purchase Receipt', 'Purchase Invoice']: |
Rohit Waghchaure | c6deb13 | 2018-05-07 15:58:41 +0530 | [diff] [blame] | 463 | field = 'purchase_invoice' if self.doctype == 'Purchase Invoice' else 'purchase_receipt' |
| 464 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 465 | self.delete_linked_asset() |
| 466 | self.update_fixed_asset(field, delete_asset=True) |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 467 | |
| 468 | def process_fixed_asset(self): |
Rohit Waghchaure | c6deb13 | 2018-05-07 15:58:41 +0530 | [diff] [blame] | 469 | if self.doctype == 'Purchase Invoice' and not self.update_stock: |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 470 | return |
| 471 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 472 | asset_items = self.get_asset_items() |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 473 | if asset_items: |
| 474 | self.make_serial_nos_for_asset(asset_items) |
| 475 | |
| 476 | def make_serial_nos_for_asset(self, asset_items): |
| 477 | items_data = get_asset_item_details(asset_items) |
| 478 | |
| 479 | for d in self.items: |
| 480 | if d.is_fixed_asset: |
| 481 | item_data = items_data.get(d.item_code) |
Rohit Waghchaure | c6deb13 | 2018-05-07 15:58:41 +0530 | [diff] [blame] | 482 | if not d.asset: |
| 483 | asset = self.make_asset(d) |
| 484 | d.db_set('asset', asset) |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 485 | |
| 486 | if item_data.get('has_serial_no'): |
| 487 | # If item has serial no |
| 488 | if item_data.get('serial_no_series') and not d.serial_no: |
| 489 | serial_nos = get_auto_serial_nos(item_data.get('serial_no_series'), d.qty) |
| 490 | elif d.serial_no: |
| 491 | serial_nos = d.serial_no |
| 492 | elif not d.serial_no: |
| 493 | frappe.throw(_("Serial no is mandatory for the item {0}").format(d.item_code)) |
| 494 | |
| 495 | auto_make_serial_nos({ |
| 496 | 'serial_no': serial_nos, |
| 497 | 'item_code': d.item_code, |
| 498 | 'via_stock_ledger': False, |
| 499 | 'company': self.company, |
| 500 | 'actual_qty': d.qty, |
| 501 | 'purchase_document_type': self.doctype, |
Rohit Waghchaure | c6deb13 | 2018-05-07 15:58:41 +0530 | [diff] [blame] | 502 | 'purchase_document_no': self.name, |
| 503 | 'asset': d.asset |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 504 | }) |
| 505 | d.db_set('serial_no', serial_nos) |
| 506 | |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 507 | if d.asset: |
| 508 | self.make_asset_movement(d) |
| 509 | |
| 510 | def make_asset(self, row): |
Rohit Waghchaure | aa7b434 | 2018-05-11 01:56:05 +0530 | [diff] [blame] | 511 | if not row.asset_location: |
| 512 | frappe.throw(_("Row {0}: Enter location for the asset item {1}").format(row.idx, row.item_code)) |
| 513 | |
Rohit Waghchaure | f2684ae | 2018-05-08 13:22:22 +0530 | [diff] [blame] | 514 | item_data = frappe.db.get_value('Item', |
| 515 | row.item_code, ['asset_naming_series', 'asset_category'], as_dict=1) |
| 516 | |
Rohit Waghchaure | 16bc853 | 2018-05-12 12:06:00 +0530 | [diff] [blame] | 517 | purchase_amount = flt(row.base_net_amount + row.item_tax_amount) |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 518 | asset = frappe.get_doc({ |
| 519 | 'doctype': 'Asset', |
| 520 | 'item_code': row.item_code, |
Rohit Waghchaure | c6deb13 | 2018-05-07 15:58:41 +0530 | [diff] [blame] | 521 | 'asset_name': row.item_name, |
Rohit Waghchaure | aa7b434 | 2018-05-11 01:56:05 +0530 | [diff] [blame] | 522 | 'status': 'Receipt', |
Rohit Waghchaure | f2684ae | 2018-05-08 13:22:22 +0530 | [diff] [blame] | 523 | 'naming_series': item_data.get('asset_naming_series') or 'AST', |
| 524 | 'asset_category': item_data.get('asset_category'), |
Rohit Waghchaure | aa7b434 | 2018-05-11 01:56:05 +0530 | [diff] [blame] | 525 | 'location': row.asset_location, |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 526 | 'company': self.company, |
| 527 | 'purchase_date': self.posting_date, |
Rohit Waghchaure | 0ea6fe4 | 2018-05-08 23:31:58 +0530 | [diff] [blame] | 528 | 'calculate_depreciation': 1, |
Rohit Waghchaure | 16bc853 | 2018-05-12 12:06:00 +0530 | [diff] [blame] | 529 | 'purchase_receipt_amount': purchase_amount, |
| 530 | 'gross_purchase_amount': purchase_amount, |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 531 | 'purchase_receipt': self.name if self.doctype == 'Purchase Receipt' else None, |
| 532 | 'purchase_invoice': self.name if self.doctype == 'Purchase Invoice' else None |
| 533 | }) |
| 534 | |
| 535 | asset.flags.ignore_validate = True |
| 536 | asset.flags.ignore_mandatory = True |
Rohit Waghchaure | aa7b434 | 2018-05-11 01:56:05 +0530 | [diff] [blame] | 537 | asset.set_missing_values() |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 538 | asset.insert() |
| 539 | |
| 540 | frappe.msgprint(_("Asset {0} created").format(asset.name)) |
| 541 | return asset.name |
| 542 | |
| 543 | def make_asset_movement(self, row): |
| 544 | asset_movement = frappe.get_doc({ |
| 545 | 'doctype': 'Asset Movement', |
| 546 | 'asset': row.asset, |
Rohit Waghchaure | aa7b434 | 2018-05-11 01:56:05 +0530 | [diff] [blame] | 547 | 'target_location': row.asset_location, |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 548 | 'purpose': 'Receipt', |
| 549 | 'serial_no': row.serial_no, |
Rohit Waghchaure | aa7b434 | 2018-05-11 01:56:05 +0530 | [diff] [blame] | 550 | 'quantity': len(get_serial_nos(row.serial_no)), |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 551 | 'company': self.company, |
| 552 | 'transaction_date': self.posting_date, |
| 553 | 'reference_doctype': self.doctype, |
| 554 | 'reference_name': self.name |
| 555 | }).insert() |
| 556 | |
| 557 | return asset_movement.name |
| 558 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 559 | def update_fixed_asset(self, field, delete_asset = False): |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 560 | for d in self.get("items"): |
| 561 | if d.is_fixed_asset and d.asset: |
| 562 | asset = frappe.get_doc("Asset", d.asset) |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 563 | |
| 564 | if delete_asset and asset.docstatus == 0: |
| 565 | frappe.delete_doc("Asset", asset.name) |
| 566 | d.db_set('asset', None) |
| 567 | continue |
| 568 | |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 569 | if self.docstatus in [0, 1] and not asset.get(field): |
| 570 | asset.set(field, self.name) |
| 571 | asset.purchase_date = self.posting_date |
| 572 | asset.supplier = self.supplier |
Rohit Waghchaure | aa7b434 | 2018-05-11 01:56:05 +0530 | [diff] [blame] | 573 | elif self.docstatus == 2: |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 574 | asset.set(field, None) |
| 575 | asset.supplier = None |
| 576 | |
| 577 | asset.flags.ignore_validate_update_after_submit = True |
| 578 | if asset.docstatus == 0: |
| 579 | asset.flags.ignore_validate = True |
| 580 | |
| 581 | asset.save() |
| 582 | |
Rohit Waghchaure | af05995 | 2018-05-07 18:46:53 +0530 | [diff] [blame] | 583 | def delete_linked_asset(self): |
Rohit Waghchaure | c6deb13 | 2018-05-07 15:58:41 +0530 | [diff] [blame] | 584 | if self.doctype == 'Purchase Invoice' and not self.get('update_stock'): |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 585 | return |
| 586 | |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 587 | frappe.db.sql("delete from `tabAsset Movement` where reference_name=%s and docstatus = 0", self.name) |
| 588 | 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] | 589 | |
Nabin Hait | 5a83420 | 2017-10-05 19:51:10 +0530 | [diff] [blame] | 590 | def validate_schedule_date(self): |
| 591 | if not self.schedule_date: |
| 592 | self.schedule_date = min([d.schedule_date for d in self.get("items")]) |
| 593 | |
| 594 | if self.schedule_date: |
| 595 | for d in self.get('items'): |
| 596 | if not d.schedule_date: |
| 597 | d.schedule_date = self.schedule_date |
| 598 | |
Prateeksha Singh | cbd06fd | 2018-01-05 21:28:01 +0530 | [diff] [blame] | 599 | if (d.schedule_date and self.transaction_date and |
| 600 | getdate(d.schedule_date) < getdate(self.transaction_date)): |
Nabin Hait | 96b264b | 2018-01-02 11:50:29 +0530 | [diff] [blame] | 601 | 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] | 602 | else: |
Nabin Hait | 96b264b | 2018-01-02 11:50:29 +0530 | [diff] [blame] | 603 | frappe.throw(_("Please enter Reqd by Date")) |
Rushabh Mehta | 30dc9a1 | 2017-11-17 14:31:09 +0530 | [diff] [blame] | 604 | |
rohitwaghchaure | bbd9b71 | 2018-03-07 15:39:40 +0530 | [diff] [blame] | 605 | def get_items_from_bom(item_code, bom, exploded_item=1): |
| 606 | doctype = "BOM Item" if not exploded_item else "BOM Explosion Item" |
| 607 | |
| 608 | bom_items = frappe.db.sql("""select t2.item_code, t2.name, |
| 609 | t2.rate, t2.stock_uom, t2.source_warehouse, t2.description, |
| 610 | t2.stock_qty / ifnull(t1.quantity, 1) as qty_consumed_per_unit |
| 611 | from |
| 612 | `tabBOM` t1, `tab{0}` t2, tabItem t3 |
| 613 | where |
| 614 | t2.parent = t1.name and t1.item = %s |
| 615 | and t1.docstatus = 1 and t1.is_active = 1 and t1.name = %s |
| 616 | and t2.item_code = t3.name and t3.is_stock_item = 1""".format(doctype), |
| 617 | (item_code, bom), as_dict=1) |
| 618 | |
| 619 | if not bom_items: |
| 620 | msgprint(_("Specified BOM {0} does not exist for Item {1}").format(bom, item_code), raise_exception=1) |
| 621 | |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 622 | return bom_items |
Rohit Waghchaure | ab84254 | 2018-04-26 19:18:29 +0530 | [diff] [blame] | 623 | |
| 624 | def get_asset_item_details(asset_items): |
| 625 | asset_items_data = {} |
| 626 | for d in frappe.get_all('Item', fields = ["name", "has_serial_no", "serial_no_series"], |
| 627 | filters = {'name': ('in', asset_items)}): |
| 628 | asset_items_data.setdefault(d.name, d) |
| 629 | |
| 630 | return asset_items_data |