Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. |
| 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 |
| 5 | import webnotes |
Anand Doshi | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 6 | from webnotes import _, msgprint |
Anand Doshi | 2e31e09 | 2013-08-19 19:58:23 +0530 | [diff] [blame] | 7 | from webnotes.utils import flt, _round |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 8 | |
| 9 | from buying.utils import get_item_details |
Anand Doshi | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 10 | from setup.utils import get_company_currency |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 11 | |
Nabin Hait | 89a94d8 | 2013-03-19 12:01:46 +0530 | [diff] [blame] | 12 | from controllers.stock_controller import StockController |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 13 | |
Rushabh Mehta | ecb36f2 | 2013-05-02 11:34:37 +0530 | [diff] [blame] | 14 | class WrongWarehouseCompany(Exception): pass |
| 15 | |
Nabin Hait | 89a94d8 | 2013-03-19 12:01:46 +0530 | [diff] [blame] | 16 | class BuyingController(StockController): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 17 | def onload_post_render(self): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 18 | # contact, address, item details |
| 19 | self.set_missing_values() |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 20 | |
Saurabh | 6f75318 | 2013-03-20 12:55:28 +0530 | [diff] [blame] | 21 | def validate(self): |
| 22 | super(BuyingController, self).validate() |
Rushabh Mehta | 886c9ef | 2013-07-08 12:08:06 +0530 | [diff] [blame] | 23 | if self.doc.supplier and not self.doc.supplier_name: |
| 24 | self.doc.supplier_name = webnotes.conn.get_value("Supplier", |
| 25 | self.doc.supplier, "supplier_name") |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 26 | self.validate_stock_or_nonstock_items() |
Rushabh Mehta | ecb36f2 | 2013-05-02 11:34:37 +0530 | [diff] [blame] | 27 | self.validate_warehouse_belongs_to_company() |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 28 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 29 | def set_missing_values(self, for_validate=False): |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 30 | super(BuyingController, self).set_missing_values(for_validate) |
| 31 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 32 | self.set_supplier_from_item_default() |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 33 | self.set_price_list_currency("Buying") |
| 34 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 35 | # set contact and address details for supplier, if they are not mentioned |
| 36 | if self.doc.supplier and not (self.doc.contact_person and self.doc.supplier_address): |
Anand Doshi | f2045fb | 2013-07-25 17:46:02 +0530 | [diff] [blame] | 37 | for fieldname, val in self.get_supplier_defaults().items(): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 38 | if not self.doc.fields.get(fieldname) and self.meta.get_field(fieldname): |
| 39 | self.doc.fields[fieldname] = val |
Rushabh Mehta | 886c9ef | 2013-07-08 12:08:06 +0530 | [diff] [blame] | 40 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 41 | self.set_missing_item_details(get_item_details) |
Rushabh Mehta | e89c533 | 2013-08-29 15:32:46 +0530 | [diff] [blame] | 42 | if self.doc.fields.get("__islocal"): |
| 43 | self.set_taxes("purchase_tax_details", "purchase_other_charges") |
Rushabh Mehta | 436467a | 2013-07-08 12:37:59 +0530 | [diff] [blame] | 44 | |
Rushabh Mehta | 0b99540 | 2013-08-09 15:29:59 +0530 | [diff] [blame] | 45 | def set_supplier_from_item_default(self): |
| 46 | if self.meta.get_field("supplier") and not self.doc.supplier: |
| 47 | for d in self.doclist.get({"doctype": self.tname}): |
| 48 | supplier = webnotes.conn.get_value("Item", d.item_code, "default_supplier") |
| 49 | if supplier: |
| 50 | self.doc.supplier = supplier |
| 51 | break |
| 52 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 53 | def get_purchase_tax_details(self): |
| 54 | self.doclist = self.doc.clear_table(self.doclist, "purchase_tax_details") |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 55 | self.set_taxes("purchase_tax_details", "purchase_other_charges") |
Anand Doshi | bf3e54a | 2013-06-05 14:11:32 +0530 | [diff] [blame] | 56 | |
Rushabh Mehta | ecb36f2 | 2013-05-02 11:34:37 +0530 | [diff] [blame] | 57 | def validate_warehouse_belongs_to_company(self): |
Rushabh Mehta | c711598 | 2013-05-02 14:22:40 +0530 | [diff] [blame] | 58 | for warehouse, company in webnotes.conn.get_values("Warehouse", |
| 59 | self.doclist.get_distinct_values("warehouse"), "company").items(): |
| 60 | if company and company != self.doc.company: |
Anand Doshi | eb41ffd | 2013-05-02 18:10:19 +0530 | [diff] [blame] | 61 | webnotes.msgprint(_("Company mismatch for Warehouse") + (": %s" % (warehouse,)), |
Rushabh Mehta | ecb36f2 | 2013-05-02 11:34:37 +0530 | [diff] [blame] | 62 | raise_exception=WrongWarehouseCompany) |
Rushabh Mehta | c711598 | 2013-05-02 14:22:40 +0530 | [diff] [blame] | 63 | |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 64 | def validate_stock_or_nonstock_items(self): |
Nabin Hait | bc2d9e8 | 2013-06-19 14:58:01 +0530 | [diff] [blame] | 65 | if not self.stock_items: |
Nabin Hait | 205f7ce | 2013-04-26 13:35:06 +0530 | [diff] [blame] | 66 | tax_for_valuation = [d.account_head for d in |
| 67 | self.doclist.get({"parentfield": "purchase_tax_details"}) |
| 68 | if d.category in ["Valuation", "Valuation and Total"]] |
| 69 | if tax_for_valuation: |
| 70 | webnotes.msgprint(_("""Tax Category can not be 'Valuation' or 'Valuation and Total' |
| 71 | as all items are non-stock items"""), raise_exception=1) |
| 72 | |
Nabin Hait | d3b6250 | 2013-01-21 17:24:31 +0530 | [diff] [blame] | 73 | def set_total_in_words(self): |
Nabin Hait | 5b4c294 | 2013-01-22 11:12:02 +0530 | [diff] [blame] | 74 | from webnotes.utils import money_in_words |
Nabin Hait | d3b6250 | 2013-01-21 17:24:31 +0530 | [diff] [blame] | 75 | company_currency = get_company_currency(self.doc.company) |
Nabin Hait | 5b4c294 | 2013-01-22 11:12:02 +0530 | [diff] [blame] | 76 | if self.meta.get_field("in_words"): |
| 77 | self.doc.in_words = money_in_words(self.doc.grand_total, company_currency) |
| 78 | if self.meta.get_field("in_words_import"): |
| 79 | 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] | 80 | self.doc.currency) |
| 81 | |
| 82 | def calculate_taxes_and_totals(self): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 83 | self.other_fname = "purchase_tax_details" |
| 84 | super(BuyingController, self).calculate_taxes_and_totals() |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 85 | self.calculate_total_advance("Purchase Invoice", "advance_allocation_details") |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 86 | |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 87 | def calculate_item_values(self): |
Anand Doshi | b8b1e58 | 2013-05-06 12:52:46 +0530 | [diff] [blame] | 88 | # hack! - cleaned up in _cleanup() |
| 89 | if self.doc.doctype != "Purchase Invoice": |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 90 | df = self.meta.get_field("purchase_rate", parentfield=self.fname) |
| 91 | df.fieldname = "rate" |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 92 | |
Anand Doshi | b8b1e58 | 2013-05-06 12:52:46 +0530 | [diff] [blame] | 93 | for item in self.item_doclist: |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 94 | # hack! - cleaned up in _cleanup() |
| 95 | if self.doc.doctype != "Purchase Invoice": |
| 96 | item.rate = item.purchase_rate |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 97 | |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 98 | self.round_floats_in(item) |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 99 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 100 | if item.discount_rate == 100.0: |
| 101 | item.import_rate = 0.0 |
Anand Doshi | 47e14a9 | 2013-08-13 13:01:12 +0530 | [diff] [blame] | 102 | elif not item.import_rate: |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 103 | item.import_rate = flt(item.import_ref_rate * (1.0 - (item.discount_rate / 100.0)), |
| 104 | self.precision("import_rate", item)) |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 105 | |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 106 | item.import_amount = flt(item.import_rate * item.qty, |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 107 | self.precision("import_amount", item)) |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 108 | item.item_tax_amount = 0.0; |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 109 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 110 | self._set_in_company_currency(item, "import_ref_rate", "purchase_ref_rate") |
| 111 | self._set_in_company_currency(item, "import_rate", "rate") |
| 112 | self._set_in_company_currency(item, "import_amount", "amount") |
Anand Doshi | 5af812a | 2013-05-10 19:23:02 +0530 | [diff] [blame] | 113 | |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 114 | def calculate_net_total(self): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 115 | self.doc.net_total = self.doc.net_total_import = 0.0 |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 116 | |
| 117 | for item in self.item_doclist: |
| 118 | self.doc.net_total += item.amount |
| 119 | self.doc.net_total_import += item.import_amount |
| 120 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 121 | self.round_floats_in(self.doc, ["net_total", "net_total_import"]) |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 122 | |
| 123 | def calculate_totals(self): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 124 | self.doc.grand_total = flt(self.tax_doclist and \ |
| 125 | self.tax_doclist[-1].total or self.doc.net_total, self.precision("grand_total")) |
| 126 | self.doc.grand_total_import = flt(self.doc.grand_total / self.doc.conversion_rate, |
| 127 | self.precision("grand_total_import")) |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 128 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 129 | 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] | 130 | self.precision("total_tax")) |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 131 | |
| 132 | if self.meta.get_field("rounded_total"): |
Anand Doshi | 2e31e09 | 2013-08-19 19:58:23 +0530 | [diff] [blame] | 133 | self.doc.rounded_total = _round(self.doc.grand_total) |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 134 | |
| 135 | if self.meta.get_field("rounded_total_import"): |
Anand Doshi | 2e31e09 | 2013-08-19 19:58:23 +0530 | [diff] [blame] | 136 | self.doc.rounded_total_import = _round(self.doc.grand_total_import) |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 137 | |
| 138 | def calculate_outstanding_amount(self): |
Anand Doshi | c99853c | 2013-05-14 13:35:09 +0530 | [diff] [blame] | 139 | if self.doc.doctype == "Purchase Invoice" and self.doc.docstatus < 2: |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 140 | self.doc.total_advance = flt(self.doc.total_advance, |
Anand Doshi | 5af812a | 2013-05-10 19:23:02 +0530 | [diff] [blame] | 141 | self.precision("total_advance")) |
Anand Doshi | 9d794fc | 2013-02-27 13:34:22 +0530 | [diff] [blame] | 142 | 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] | 143 | self.precision("write_off_amount")), self.precision("total_amount_to_pay")) |
Anand Doshi | 9d794fc | 2013-02-27 13:34:22 +0530 | [diff] [blame] | 144 | 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] | 145 | self.precision("outstanding_amount")) |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 146 | |
| 147 | def _cleanup(self): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 148 | super(BuyingController, self)._cleanup() |
| 149 | |
| 150 | # except in purchase invoice, rate field is purchase_rate |
Anand Doshi | 5af812a | 2013-05-10 19:23:02 +0530 | [diff] [blame] | 151 | # reset fieldname of rate |
| 152 | if self.doc.doctype != "Purchase Invoice": |
| 153 | df = self.meta.get_field("rate", parentfield=self.fname) |
| 154 | df.fieldname = "purchase_rate" |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 155 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 156 | for item in self.item_doclist: |
| 157 | item.purchase_rate = item.rate |
| 158 | del item.fields["rate"] |
Anand Doshi | b392878 | 2013-06-05 17:46:20 +0530 | [diff] [blame] | 159 | |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 160 | if not self.meta.get_field("item_tax_amount", parentfield=self.fname): |
| 161 | for item in self.item_doclist: |
| 162 | del item.fields["item_tax_amount"] |
| 163 | |
Anand Doshi | 8595fcf | 2013-02-08 19:28:14 +0530 | [diff] [blame] | 164 | def set_item_tax_amount(self, item, tax, current_tax_amount): |
| 165 | """ |
| 166 | item_tax_amount is the total tax amount applied on that item |
| 167 | stored for valuation |
| 168 | |
| 169 | TODO: rename item_tax_amount to valuation_tax_amount |
| 170 | """ |
Anand Doshi | b392878 | 2013-06-05 17:46:20 +0530 | [diff] [blame] | 171 | if tax.category in ["Valuation", "Valuation and Total"] and \ |
| 172 | self.meta.get_field("item_tax_amount", parentfield=self.fname): |
| 173 | item.item_tax_amount += flt(current_tax_amount, self.precision("item_tax_amount", item)) |
Anand Doshi | 4a7248e | 2013-02-27 18:10:30 +0530 | [diff] [blame] | 174 | |
| 175 | # update valuation rate |
| 176 | def update_valuation_rate(self, parentfield): |
Anand Doshi | 5af812a | 2013-05-10 19:23:02 +0530 | [diff] [blame] | 177 | for item in self.doclist.get({"parentfield": parentfield}): |
| 178 | item.conversion_factor = item.conversion_factor or flt(webnotes.conn.get_value( |
| 179 | "UOM Conversion Detail", {"parent": item.item_code, "uom": item.uom}, |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 180 | "conversion_factor")) or 1 |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 181 | |
Anand Doshi | 5af812a | 2013-05-10 19:23:02 +0530 | [diff] [blame] | 182 | if item.item_code and item.qty: |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 183 | self.round_floats_in(item) |
| 184 | |
| 185 | purchase_rate = item.rate if self.doc.doctype == "Purchase Invoice" else item.purchase_rate |
| 186 | |
Anand Doshi | 1bfec3a | 2013-02-28 19:17:46 +0530 | [diff] [blame] | 187 | # if no item code, which is sometimes the case in purchase invoice, |
| 188 | # then it is not possible to track valuation against it |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 189 | item.valuation_rate = flt((purchase_rate + |
| 190 | (item.item_tax_amount + item.rm_supp_cost) / item.qty) / item.conversion_factor, |
| 191 | self.precision("valuation_rate", item)) |
Anand Doshi | 4a7248e | 2013-02-27 18:10:30 +0530 | [diff] [blame] | 192 | else: |
Anand Doshi | 5af812a | 2013-05-10 19:23:02 +0530 | [diff] [blame] | 193 | item.valuation_rate = 0.0 |
Nabin Hait | 54d209f | 2013-03-01 18:51:10 +0530 | [diff] [blame] | 194 | |
| 195 | def validate_for_subcontracting(self): |
| 196 | if not self.doc.is_subcontracted and self.sub_contracted_items: |
| 197 | webnotes.msgprint(_("""Please enter whether %s is made for subcontracting or purchasing, |
| 198 | in 'Is Subcontracted' field""" % self.doc.doctype), raise_exception=1) |
| 199 | |
| 200 | if self.doc.doctype == "Purchase Receipt" and self.doc.is_subcontracted=="Yes" \ |
| 201 | and not self.doc.supplier_warehouse: |
| 202 | webnotes.msgprint(_("Supplier Warehouse mandatory subcontracted purchase receipt"), |
| 203 | raise_exception=1) |
| 204 | |
| 205 | def update_raw_materials_supplied(self, raw_material_table): |
| 206 | self.doclist = self.doc.clear_table(self.doclist, raw_material_table) |
| 207 | if self.doc.is_subcontracted=="Yes": |
| 208 | for item in self.doclist.get({"parentfield": self.fname}): |
| 209 | if item.item_code in self.sub_contracted_items: |
| 210 | self.add_bom_items(item, raw_material_table) |
| 211 | |
| 212 | def add_bom_items(self, d, raw_material_table): |
| 213 | bom_items = self.get_items_from_default_bom(d.item_code) |
| 214 | raw_materials_cost = 0 |
| 215 | for item in bom_items: |
| 216 | required_qty = flt(item.qty_consumed_per_unit) * flt(d.qty) * flt(d.conversion_factor) |
| 217 | rm_doclist = { |
| 218 | "parentfield": raw_material_table, |
| 219 | "doctype": self.doc.doctype + " Item Supplied", |
| 220 | "reference_name": d.name, |
| 221 | "bom_detail_no": item.name, |
| 222 | "main_item_code": d.item_code, |
| 223 | "rm_item_code": item.item_code, |
| 224 | "stock_uom": item.stock_uom, |
| 225 | "required_qty": required_qty, |
| 226 | "conversion_factor": d.conversion_factor, |
| 227 | "rate": item.rate, |
| 228 | "amount": required_qty * flt(item.rate) |
| 229 | } |
| 230 | if self.doc.doctype == "Purchase Receipt": |
| 231 | rm_doclist.update({ |
| 232 | "consumed_qty": required_qty, |
| 233 | "description": item.description, |
| 234 | }) |
| 235 | |
| 236 | self.doclist.append(rm_doclist) |
| 237 | |
| 238 | raw_materials_cost += required_qty * flt(item.rate) |
| 239 | |
| 240 | if self.doc.doctype == "Purchase Receipt": |
| 241 | d.rm_supp_cost = raw_materials_cost |
| 242 | |
| 243 | def get_items_from_default_bom(self, item_code): |
| 244 | # print webnotes.conn.sql("""select name from `tabBOM` where item = '_Test FG Item'""") |
| 245 | bom_items = webnotes.conn.sql("""select t2.item_code, t2.qty_consumed_per_unit, |
| 246 | t2.rate, t2.stock_uom, t2.name, t2.description |
| 247 | from `tabBOM` t1, `tabBOM Item` t2 |
| 248 | where t2.parent = t1.name and t1.item = %s and t1.is_default = 1 |
| 249 | and t1.docstatus = 1 and t1.is_active = 1""", item_code, as_dict=1) |
| 250 | if not bom_items: |
| 251 | msgprint(_("No default BOM exists for item: ") + item_code, raise_exception=1) |
| 252 | |
| 253 | return bom_items |
| 254 | |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 255 | @property |
| 256 | def sub_contracted_items(self): |
| 257 | if not hasattr(self, "_sub_contracted_items"): |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 258 | self._sub_contracted_items = [] |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 259 | item_codes = list(set(item.item_code for item in |
| 260 | self.doclist.get({"parentfield": self.fname}))) |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 261 | if item_codes: |
| 262 | self._sub_contracted_items = [r[0] for r in webnotes.conn.sql("""select name |
| 263 | from `tabItem` where name in (%s) and is_sub_contracted_item='Yes'""" % \ |
| 264 | (", ".join((["%s"]*len(item_codes))),), item_codes)] |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 265 | |
| 266 | return self._sub_contracted_items |
| 267 | |
| 268 | @property |
| 269 | def purchase_items(self): |
| 270 | if not hasattr(self, "_purchase_items"): |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 271 | self._purchase_items = [] |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 272 | item_codes = list(set(item.item_code for item in |
| 273 | self.doclist.get({"parentfield": self.fname}))) |
Nabin Hait | ebd5144 | 2013-04-23 15:36:26 +0530 | [diff] [blame] | 274 | if item_codes: |
| 275 | self._purchase_items = [r[0] for r in webnotes.conn.sql("""select name |
| 276 | from `tabItem` where name in (%s) and is_purchase_item='Yes'""" % \ |
| 277 | (", ".join((["%s"]*len(item_codes))),), item_codes)] |
Nabin Hait | 5418d71 | 2013-02-27 18:11:17 +0530 | [diff] [blame] | 278 | |
Anand Doshi | 55d9a62 | 2013-02-27 18:14:28 +0530 | [diff] [blame] | 279 | return self._purchase_items |