Rushabh Mehta | ad45e31 | 2013-11-20 12:59:58 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes 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 |
| 7 | from frappe.utils import flt, _round |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 8 | from erpnext.setup.utils import get_company_currency |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 9 | from erpnext.accounts.party import get_party_details |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 10 | |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 11 | from erpnext.controllers.stock_controller import StockController |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 12 | |
Nabin Hait | 89a94d8 | 2013-03-19 12:01:46 +0530 | [diff] [blame] | 13 | class BuyingController(StockController): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 14 | def onload_post_render(self): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 15 | # contact, address, item details |
| 16 | self.set_missing_values() |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 17 | |
Saurabh | 6f75318 | 2013-03-20 12:55:28 +0530 | [diff] [blame] | 18 | def validate(self): |
| 19 | super(BuyingController, self).validate() |
Rushabh Mehta | 886c9ef | 2013-07-08 12:08:06 +0530 | [diff] [blame] | 20 | if self.doc.supplier and not self.doc.supplier_name: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 21 | self.doc.supplier_name = frappe.db.get_value("Supplier", |
Rushabh Mehta | 886c9ef | 2013-07-08 12:08:06 +0530 | [diff] [blame] | 22 | self.doc.supplier, "supplier_name") |
Nabin Hait | 5566bca | 2013-10-18 17:00:53 +0530 | [diff] [blame] | 23 | self.is_item_table_empty() |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 24 | self.validate_stock_or_nonstock_items() |
Anand Doshi | 373680b | 2013-10-10 16:04:40 +0530 | [diff] [blame] | 25 | self.validate_warehouse() |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 26 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 27 | def set_missing_values(self, for_validate=False): |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 28 | super(BuyingController, self).set_missing_values(for_validate) |
| 29 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 30 | self.set_supplier_from_item_default() |
Nabin Hait | 096d363 | 2013-10-17 17:01:14 +0530 | [diff] [blame] | 31 | self.set_price_list_currency("Buying") |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 32 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 33 | # set contact and address details for supplier, if they are not mentioned |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 34 | if self.doc.supplier: |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 35 | self.doc.update_if_missing(get_party_details(self.doc.supplier, party_type="Supplier")) |
Rushabh Mehta | 886c9ef | 2013-07-08 12:08:06 +0530 | [diff] [blame] | 36 | |
Nabin Hait | 0aa71a5 | 2014-02-11 16:14:52 +0530 | [diff] [blame] | 37 | self.set_missing_item_details() |
Rushabh Mehta | e89c533 | 2013-08-29 15:32:46 +0530 | [diff] [blame] | 38 | if self.doc.fields.get("__islocal"): |
Akhilesh Darjee | 4f72156 | 2014-01-29 16:31:38 +0530 | [diff] [blame] | 39 | self.set_taxes("other_charges", "taxes_and_charges") |
Rushabh Mehta | 436467a | 2013-07-08 12:37:59 +0530 | [diff] [blame] | 40 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 41 | def set_supplier_from_item_default(self): |
| 42 | if self.meta.get_field("supplier") and not self.doc.supplier: |
| 43 | for d in self.doclist.get({"doctype": self.tname}): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 44 | supplier = frappe.db.get_value("Item", d.item_code, "default_supplier") |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 45 | if supplier: |
| 46 | self.doc.supplier = supplier |
| 47 | break |
Anand Doshi | 373680b | 2013-10-10 16:04:40 +0530 | [diff] [blame] | 48 | |
| 49 | def validate_warehouse(self): |
Anand Doshi | 8e332ff | 2013-12-26 18:30:39 +0530 | [diff] [blame] | 50 | from erpnext.stock.utils import validate_warehouse_company |
Anand Doshi | 373680b | 2013-10-10 16:04:40 +0530 | [diff] [blame] | 51 | |
| 52 | warehouses = list(set([d.warehouse for d in |
| 53 | self.doclist.get({"doctype": self.tname}) if d.warehouse])) |
| 54 | |
| 55 | for w in warehouses: |
Anand Doshi | 373680b | 2013-10-10 16:04:40 +0530 | [diff] [blame] | 56 | validate_warehouse_company(w, self.doc.company) |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 57 | |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 58 | def validate_stock_or_nonstock_items(self): |
Nabin Hait | a36adbd | 2013-08-02 14:50:12 +0530 | [diff] [blame] | 59 | if not self.get_stock_items(): |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 60 | tax_for_valuation = [d.account_head for d in |
Akhilesh Darjee | 4f72156 | 2014-01-29 16:31:38 +0530 | [diff] [blame] | 61 | self.doclist.get({"parentfield": "other_charges"}) |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 62 | if d.category in ["Valuation", "Valuation and Total"]] |
| 63 | if tax_for_valuation: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 64 | frappe.msgprint(_("""Tax Category can not be 'Valuation' or 'Valuation and Total' as all items are non-stock items"""), raise_exception=1) |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 65 | |
Nabin Hait | d3b6250 | 2013-01-21 17:24:31 +0530 | [diff] [blame] | 66 | def set_total_in_words(self): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 67 | from frappe.utils import money_in_words |
Nabin Hait | d3b6250 | 2013-01-21 17:24:31 +0530 | [diff] [blame] | 68 | company_currency = get_company_currency(self.doc.company) |
Nabin Hait | 5b4c294 | 2013-01-22 11:12:02 +0530 | [diff] [blame] | 69 | if self.meta.get_field("in_words"): |
| 70 | self.doc.in_words = money_in_words(self.doc.grand_total, company_currency) |
| 71 | if self.meta.get_field("in_words_import"): |
| 72 | self.doc.in_words_import = money_in_words(self.doc.grand_total_import, |
Anand Doshi | 613cb6a | 2013-02-06 17:33:46 +0530 | [diff] [blame] | 73 | self.doc.currency) |
| 74 | |
| 75 | def calculate_taxes_and_totals(self): |
Akhilesh Darjee | 4f72156 | 2014-01-29 16:31:38 +0530 | [diff] [blame] | 76 | self.other_fname = "other_charges" |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 77 | super(BuyingController, self).calculate_taxes_and_totals() |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 78 | self.calculate_total_advance("Purchase Invoice", "advance_allocation_details") |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 79 | |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 80 | def calculate_item_values(self): |
Anand Doshi | b8b1e58 | 2013-05-06 12:52:46 +0530 | [diff] [blame] | 81 | for item in self.item_doclist: |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 82 | self.round_floats_in(item) |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 83 | |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 84 | if item.discount_percentage == 100.0: |
Nabin Hait | 7979f7e | 2014-02-10 18:26:49 +0530 | [diff] [blame] | 85 | item.rate = 0.0 |
| 86 | elif not item.rate: |
| 87 | item.rate = flt(item.price_list_rate * (1.0 - (item.discount_percentage / 100.0)), |
| 88 | self.precision("rate", item)) |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 89 | |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 90 | item.amount = flt(item.rate * item.qty, |
| 91 | self.precision("amount", item)) |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 92 | item.item_tax_amount = 0.0; |
Rushabh Mehta | 5404778 | 2013-12-26 11:07:46 +0530 | [diff] [blame] | 93 | |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 94 | self._set_in_company_currency(item, "amount", "base_amount") |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 95 | self._set_in_company_currency(item, "price_list_rate", "base_price_list_rate") |
Nabin Hait | 7979f7e | 2014-02-10 18:26:49 +0530 | [diff] [blame] | 96 | self._set_in_company_currency(item, "rate", "base_rate") |
Rushabh Mehta | 5404778 | 2013-12-26 11:07:46 +0530 | [diff] [blame] | 97 | |
Anand Doshi | 5af812a | 2013-05-10 19:23:02 +0530 | [diff] [blame] | 98 | |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 99 | def calculate_net_total(self): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 100 | self.doc.net_total = self.doc.net_total_import = 0.0 |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 101 | |
| 102 | for item in self.item_doclist: |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 103 | self.doc.net_total += item.base_amount |
| 104 | self.doc.net_total_import += item.amount |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 105 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 106 | self.round_floats_in(self.doc, ["net_total", "net_total_import"]) |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 107 | |
| 108 | def calculate_totals(self): |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 109 | self.doc.grand_total = flt(self.tax_doclist[-1].total if self.tax_doclist |
| 110 | else self.doc.net_total, self.precision("grand_total")) |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 111 | self.doc.grand_total_import = flt(self.doc.grand_total / self.doc.conversion_rate, |
| 112 | self.precision("grand_total_import")) |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 113 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 114 | self.doc.total_tax = flt(self.doc.grand_total - self.doc.net_total, |
Anand Doshi | 5af812a | 2013-05-10 19:23:02 +0530 | [diff] [blame] | 115 | self.precision("total_tax")) |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 116 | |
| 117 | if self.meta.get_field("rounded_total"): |
Anand Doshi | 2e31e09 | 2013-08-19 19:58:23 +0530 | [diff] [blame] | 118 | self.doc.rounded_total = _round(self.doc.grand_total) |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 119 | |
| 120 | if self.meta.get_field("rounded_total_import"): |
Anand Doshi | 2e31e09 | 2013-08-19 19:58:23 +0530 | [diff] [blame] | 121 | self.doc.rounded_total_import = _round(self.doc.grand_total_import) |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 122 | |
| 123 | if self.meta.get_field("other_charges_added"): |
| 124 | self.doc.other_charges_added = flt(sum([flt(d.tax_amount) for d in self.tax_doclist |
| 125 | if d.add_deduct_tax=="Add" and d.category in ["Valuation and Total", "Total"]]), |
| 126 | self.precision("other_charges_added")) |
| 127 | |
| 128 | if self.meta.get_field("other_charges_deducted"): |
| 129 | self.doc.other_charges_deducted = flt(sum([flt(d.tax_amount) for d in self.tax_doclist |
| 130 | if d.add_deduct_tax=="Deduct" and d.category in ["Valuation and Total", "Total"]]), |
| 131 | self.precision("other_charges_deducted")) |
| 132 | |
| 133 | if self.meta.get_field("other_charges_added_import"): |
| 134 | self.doc.other_charges_added_import = flt(self.doc.other_charges_added / |
| 135 | self.doc.conversion_rate, self.precision("other_charges_added_import")) |
| 136 | |
| 137 | if self.meta.get_field("other_charges_deducted_import"): |
| 138 | self.doc.other_charges_deducted_import = flt(self.doc.other_charges_deducted / |
| 139 | self.doc.conversion_rate, self.precision("other_charges_deducted_import")) |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 140 | |
| 141 | def calculate_outstanding_amount(self): |
Nabin Hait | 2f8d445 | 2014-02-20 14:59:37 +0530 | [diff] [blame] | 142 | if self.doc.doctype == "Purchase Invoice" and self.doc.docstatus == 0: |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 143 | self.doc.total_advance = flt(self.doc.total_advance, |
Anand Doshi | 5af812a | 2013-05-10 19:23:02 +0530 | [diff] [blame] | 144 | self.precision("total_advance")) |
Anand Doshi | 9d794fc | 2013-02-27 13:34:22 +0530 | [diff] [blame] | 145 | self.doc.total_amount_to_pay = flt(self.doc.grand_total - flt(self.doc.write_off_amount, |
Anand Doshi | 5af812a | 2013-05-10 19:23:02 +0530 | [diff] [blame] | 146 | self.precision("write_off_amount")), self.precision("total_amount_to_pay")) |
Anand Doshi | 9d794fc | 2013-02-27 13:34:22 +0530 | [diff] [blame] | 147 | self.doc.outstanding_amount = flt(self.doc.total_amount_to_pay - self.doc.total_advance, |
Anand Doshi | 5af812a | 2013-05-10 19:23:02 +0530 | [diff] [blame] | 148 | self.precision("outstanding_amount")) |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 149 | |
| 150 | def _cleanup(self): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 151 | super(BuyingController, self)._cleanup() |
Anand Doshi | b392878 | 2013-06-05 17:46:20 +0530 | [diff] [blame] | 152 | |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 153 | if not self.meta.get_field("item_tax_amount", parentfield=self.fname): |
| 154 | for item in self.item_doclist: |
| 155 | del item.fields["item_tax_amount"] |
Nabin Hait | 5c6d13a | 2014-01-20 17:18:16 +0530 | [diff] [blame] | 156 | |
| 157 | if not self.meta.get_field("tax_amount_after_discount_amount", |
| 158 | parentfield=self.other_fname): |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 159 | for tax in self.tax_doclist: |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 160 | del tax.fields["tax_amount_after_discount_amount"] |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 161 | |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 162 | # update valuation rate |
| 163 | def update_valuation_rate(self, parentfield): |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 164 | """ |
| 165 | item_tax_amount is the total tax amount applied on that item |
| 166 | stored for valuation |
| 167 | |
| 168 | TODO: rename item_tax_amount to valuation_tax_amount |
| 169 | """ |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 170 | stock_items = self.get_stock_items() |
| 171 | |
| 172 | stock_items_qty, stock_items_amount = 0, 0 |
| 173 | last_stock_item_idx = 1 |
| 174 | for d in self.doclist.get({"parentfield": parentfield}): |
| 175 | if d.item_code and d.item_code in stock_items: |
| 176 | stock_items_qty += flt(d.qty) |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 177 | stock_items_amount += flt(d.base_amount) |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 178 | last_stock_item_idx = d.idx |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 179 | |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 180 | total_valuation_amount = sum([flt(d.tax_amount) for d in |
Akhilesh Darjee | 4f72156 | 2014-01-29 16:31:38 +0530 | [diff] [blame] | 181 | self.doclist.get({"parentfield": "other_charges"}) |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 182 | if d.category in ["Valuation", "Valuation and Total"]]) |
| 183 | |
| 184 | |
| 185 | valuation_amount_adjustment = total_valuation_amount |
| 186 | for i, item in enumerate(self.doclist.get({"parentfield": parentfield})): |
| 187 | if item.item_code and item.qty and item.item_code in stock_items: |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 188 | item_proportion = flt(item.base_amount) / stock_items_amount if stock_items_amount \ |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 189 | else flt(item.qty) / stock_items_qty |
| 190 | |
| 191 | if i == (last_stock_item_idx - 1): |
| 192 | item.item_tax_amount = flt(valuation_amount_adjustment, |
| 193 | self.precision("item_tax_amount", item)) |
| 194 | else: |
| 195 | item.item_tax_amount = flt(item_proportion * total_valuation_amount, |
| 196 | self.precision("item_tax_amount", item)) |
| 197 | valuation_amount_adjustment -= item.item_tax_amount |
| 198 | |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 199 | self.round_floats_in(item) |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 200 | |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 201 | item.conversion_factor = item.conversion_factor or flt(frappe.db.get_value( |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 202 | "UOM Conversion Detail", {"parent": item.item_code, "uom": item.uom}, |
| 203 | "conversion_factor")) or 1 |
Rushabh Mehta | 5404778 | 2013-12-26 11:07:46 +0530 | [diff] [blame] | 204 | qty_in_stock_uom = flt(item.qty * item.conversion_factor) |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 205 | item.valuation_rate = ((item.base_amount + item.item_tax_amount + item.rm_supp_cost) |
Rushabh Mehta | 5404778 | 2013-12-26 11:07:46 +0530 | [diff] [blame] | 206 | / qty_in_stock_uom) |
Anand Doshi | 4a7248e | 2013-02-27 18:10:30 +0530 | [diff] [blame] | 207 | else: |
Anand Doshi | 5af812a | 2013-05-10 19:23:02 +0530 | [diff] [blame] | 208 | item.valuation_rate = 0.0 |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 209 | |
| 210 | def validate_for_subcontracting(self): |
| 211 | if not self.doc.is_subcontracted and self.sub_contracted_items: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 212 | frappe.msgprint(_("""Please enter whether %s is made for subcontracting or purchasing, |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 213 | in 'Is Subcontracted' field""" % self.doc.doctype), raise_exception=1) |
| 214 | |
| 215 | if self.doc.doctype == "Purchase Receipt" and self.doc.is_subcontracted=="Yes" \ |
| 216 | and not self.doc.supplier_warehouse: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 217 | frappe.msgprint(_("Supplier Warehouse mandatory subcontracted purchase receipt"), |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 218 | raise_exception=1) |
| 219 | |
| 220 | def update_raw_materials_supplied(self, raw_material_table): |
| 221 | self.doclist = self.doc.clear_table(self.doclist, raw_material_table) |
| 222 | if self.doc.is_subcontracted=="Yes": |
| 223 | for item in self.doclist.get({"parentfield": self.fname}): |
| 224 | if item.item_code in self.sub_contracted_items: |
| 225 | self.add_bom_items(item, raw_material_table) |
| 226 | |
| 227 | def add_bom_items(self, d, raw_material_table): |
| 228 | bom_items = self.get_items_from_default_bom(d.item_code) |
| 229 | raw_materials_cost = 0 |
| 230 | for item in bom_items: |
| 231 | required_qty = flt(item.qty_consumed_per_unit) * flt(d.qty) * flt(d.conversion_factor) |
| 232 | rm_doclist = { |
| 233 | "parentfield": raw_material_table, |
| 234 | "doctype": self.doc.doctype + " Item Supplied", |
| 235 | "reference_name": d.name, |
| 236 | "bom_detail_no": item.name, |
| 237 | "main_item_code": d.item_code, |
| 238 | "rm_item_code": item.item_code, |
| 239 | "stock_uom": item.stock_uom, |
| 240 | "required_qty": required_qty, |
| 241 | "conversion_factor": d.conversion_factor, |
| 242 | "rate": item.rate, |
| 243 | "amount": required_qty * flt(item.rate) |
| 244 | } |
| 245 | if self.doc.doctype == "Purchase Receipt": |
| 246 | rm_doclist.update({ |
| 247 | "consumed_qty": required_qty, |
| 248 | "description": item.description, |
| 249 | }) |
| 250 | |
| 251 | self.doclist.append(rm_doclist) |
| 252 | |
| 253 | raw_materials_cost += required_qty * flt(item.rate) |
| 254 | |
| 255 | if self.doc.doctype == "Purchase Receipt": |
| 256 | d.rm_supp_cost = raw_materials_cost |
| 257 | |
| 258 | def get_items_from_default_bom(self, item_code): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 259 | bom_items = frappe.db.sql("""select t2.item_code, t2.qty_consumed_per_unit, |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 260 | t2.rate, t2.stock_uom, t2.name, t2.description |
| 261 | from `tabBOM` t1, `tabBOM Item` t2 |
| 262 | where t2.parent = t1.name and t1.item = %s and t1.is_default = 1 |
| 263 | and t1.docstatus = 1 and t1.is_active = 1""", item_code, as_dict=1) |
| 264 | if not bom_items: |
| 265 | msgprint(_("No default BOM exists for item: ") + item_code, raise_exception=1) |
| 266 | |
| 267 | return bom_items |
| 268 | |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 269 | @property |
| 270 | def sub_contracted_items(self): |
| 271 | if not hasattr(self, "_sub_contracted_items"): |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 272 | self._sub_contracted_items = [] |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 273 | item_codes = list(set(item.item_code for item in |
| 274 | self.doclist.get({"parentfield": self.fname}))) |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 275 | if item_codes: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 276 | self._sub_contracted_items = [r[0] for r in frappe.db.sql("""select name |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 277 | from `tabItem` where name in (%s) and is_sub_contracted_item='Yes'""" % \ |
| 278 | (", ".join((["%s"]*len(item_codes))),), item_codes)] |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 279 | |
| 280 | return self._sub_contracted_items |
| 281 | |
| 282 | @property |
| 283 | def purchase_items(self): |
| 284 | if not hasattr(self, "_purchase_items"): |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 285 | self._purchase_items = [] |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 286 | item_codes = list(set(item.item_code for item in |
| 287 | self.doclist.get({"parentfield": self.fname}))) |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 288 | if item_codes: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 289 | self._purchase_items = [r[0] for r in frappe.db.sql("""select name |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 290 | from `tabItem` where name in (%s) and is_purchase_item='Yes'""" % \ |
| 291 | (", ".join((["%s"]*len(item_codes))),), item_codes)] |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 292 | |
Anand Doshi | 55d9a62 | 2013-02-27 18:14:28 +0530 | [diff] [blame] | 293 | return self._purchase_items |
Nabin Hait | 5566bca | 2013-10-18 17:00:53 +0530 | [diff] [blame] | 294 | |
| 295 | |
| 296 | def is_item_table_empty(self): |
| 297 | if not len(self.doclist.get({"parentfield": self.fname})): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 298 | frappe.throw(_("Item table can not be blank")) |