blob: 12d183cd2b483f15a1a9a719bb28c3aa5ed0ff15 [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302# License: GNU General Public License v3. See license.txt
Anand Doshi756dca72013-01-15 18:39:21 +05303
4from __future__ import unicode_literals
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05305import frappe
6from frappe import _, msgprint
Anand Doshi62b1cbf2014-07-25 12:50:00 +05307from frappe.utils import flt, rounded
Rushabh Mehta1f847992013-12-12 19:12:19 +05308from erpnext.setup.utils import get_company_currency
Rushabh Mehtacc008cc2014-02-03 16:14:56 +05309from erpnext.accounts.party import get_party_details
Anand Doshi756dca72013-01-15 18:39:21 +053010
Rushabh Mehta1f847992013-12-12 19:12:19 +053011from erpnext.controllers.stock_controller import StockController
Nabin Haitbf495c92013-01-30 12:49:08 +053012
Nabin Hait89a94d82013-03-19 12:01:46 +053013class BuyingController(StockController):
Rushabh Mehta5b51cc82014-07-21 18:25:45 +053014 def __setup__(self):
15 if hasattr(self, "fname"):
16 self.table_print_templates = {
17 self.fname: "templates/print_formats/includes/item_grid.html",
18 "other_charges": "templates/print_formats/includes/taxes.html",
19 }
20
Rushabh Mehtac0bb4532014-09-09 16:15:35 +053021 def get_feed(self):
22 return _("From {0} | {1} {2}").format(self.supplier_name, self.currency,
23 self.grand_total_import)
24
Saurabh6f753182013-03-20 12:55:28 +053025 def validate(self):
26 super(BuyingController, self).validate()
Nabin Hait365ae272014-04-03 17:38:54 +053027 if getattr(self, "supplier", None) and not self.supplier_name:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053028 self.supplier_name = frappe.db.get_value("Supplier",
Anand Doshif78d1ae2014-03-28 13:55:00 +053029 self.supplier, "supplier_name")
Nabin Hait5566bca2013-10-18 17:00:53 +053030 self.is_item_table_empty()
nabinhait614fb752014-07-14 10:47:50 +053031 self.set_qty_as_per_stock_uom()
Nabin Hait205f7ce2013-04-26 13:35:06 +053032 self.validate_stock_or_nonstock_items()
Anand Doshi373680b2013-10-10 16:04:40 +053033 self.validate_warehouse()
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053034
Anand Doshi3543f302013-05-24 19:25:01 +053035 def set_missing_values(self, for_validate=False):
Anand Doshiabc10032013-06-14 17:44:03 +053036 super(BuyingController, self).set_missing_values(for_validate)
37
Rushabh Mehta0b995402013-08-09 15:29:59 +053038 self.set_supplier_from_item_default()
Nabin Hait096d3632013-10-17 17:01:14 +053039 self.set_price_list_currency("Buying")
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053040
Anand Doshi3543f302013-05-24 19:25:01 +053041 # set contact and address details for supplier, if they are not mentioned
Nabin Hait365ae272014-04-03 17:38:54 +053042 if getattr(self, "supplier", None):
Anand Doshif78d1ae2014-03-28 13:55:00 +053043 self.update_if_missing(get_party_details(self.supplier, party_type="Supplier"))
Rushabh Mehta886c9ef2013-07-08 12:08:06 +053044
Nabin Hait0aa71a52014-02-11 16:14:52 +053045 self.set_missing_item_details()
Anand Doshif78d1ae2014-03-28 13:55:00 +053046 if self.get("__islocal"):
Akhilesh Darjee4f721562014-01-29 16:31:38 +053047 self.set_taxes("other_charges", "taxes_and_charges")
Rushabh Mehta436467a2013-07-08 12:37:59 +053048
Rushabh Mehta0b995402013-08-09 15:29:59 +053049 def set_supplier_from_item_default(self):
Anand Doshif78d1ae2014-03-28 13:55:00 +053050 if self.meta.get_field("supplier") and not self.supplier:
Rushabh Mehtaf191f852014-04-02 18:09:34 +053051 for d in self.get(self.fname):
Anand Doshie9baaa62014-02-26 12:35:33 +053052 supplier = frappe.db.get_value("Item", d.item_code, "default_supplier")
Rushabh Mehta0b995402013-08-09 15:29:59 +053053 if supplier:
Anand Doshif78d1ae2014-03-28 13:55:00 +053054 self.supplier = supplier
Rushabh Mehta0b995402013-08-09 15:29:59 +053055 break
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053056
Anand Doshi373680b2013-10-10 16:04:40 +053057 def validate_warehouse(self):
Anand Doshi8e332ff2013-12-26 18:30:39 +053058 from erpnext.stock.utils import validate_warehouse_company
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053059
60 warehouses = list(set([d.warehouse for d in
Nabin Hait312ba992014-04-04 11:06:10 +053061 self.get(self.fname) if getattr(d, "warehouse", None)]))
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053062
Anand Doshi373680b2013-10-10 16:04:40 +053063 for w in warehouses:
Anand Doshif78d1ae2014-03-28 13:55:00 +053064 validate_warehouse_company(w, self.company)
Rushabh Mehta0b995402013-08-09 15:29:59 +053065
Nabin Hait205f7ce2013-04-26 13:35:06 +053066 def validate_stock_or_nonstock_items(self):
Nabin Hait98d9d792014-06-16 15:54:05 +053067 if self.meta.get_field("other_charges") and not self.get_stock_items():
68 tax_for_valuation = [d.account_head for d in self.get("other_charges")
Nabin Hait205f7ce2013-04-26 13:35:06 +053069 if d.category in ["Valuation", "Valuation and Total"]]
70 if tax_for_valuation:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053071 frappe.throw(_("Tax Category can not be 'Valuation' or 'Valuation and Total' as all items are non-stock items"))
72
Nabin Haitd3b62502013-01-21 17:24:31 +053073 def set_total_in_words(self):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053074 from frappe.utils import money_in_words
Anand Doshif78d1ae2014-03-28 13:55:00 +053075 company_currency = get_company_currency(self.company)
Nabin Hait5b4c2942013-01-22 11:12:02 +053076 if self.meta.get_field("in_words"):
Anand Doshif78d1ae2014-03-28 13:55:00 +053077 self.in_words = money_in_words(self.grand_total, company_currency)
Nabin Hait5b4c2942013-01-22 11:12:02 +053078 if self.meta.get_field("in_words_import"):
Anand Doshif78d1ae2014-03-28 13:55:00 +053079 self.in_words_import = money_in_words(self.grand_total_import,
80 self.currency)
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053081
Anand Doshi613cb6a2013-02-06 17:33:46 +053082 def calculate_taxes_and_totals(self):
Akhilesh Darjee4f721562014-01-29 16:31:38 +053083 self.other_fname = "other_charges"
Anand Doshi3543f302013-05-24 19:25:01 +053084 super(BuyingController, self).calculate_taxes_and_totals()
Anand Doshi923d41d2013-05-28 17:23:36 +053085 self.calculate_total_advance("Purchase Invoice", "advance_allocation_details")
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053086
Anand Doshi8595fcf2013-02-08 19:28:14 +053087 def calculate_item_values(self):
Anand Doshib8b1e582013-05-06 12:52:46 +053088 for item in self.item_doclist:
Anand Doshi39384d32013-05-11 19:39:53 +053089 self.round_floats_in(item)
Anand Doshi8595fcf2013-02-08 19:28:14 +053090
Nabin Haita7f757a2014-02-10 17:54:04 +053091 if item.discount_percentage == 100.0:
Nabin Hait7979f7e2014-02-10 18:26:49 +053092 item.rate = 0.0
93 elif not item.rate:
94 item.rate = flt(item.price_list_rate * (1.0 - (item.discount_percentage / 100.0)),
95 self.precision("rate", item))
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053096
Nabin Hait1eb56012014-02-10 19:20:15 +053097 item.amount = flt(item.rate * item.qty,
98 self.precision("amount", item))
Anand Doshi3543f302013-05-24 19:25:01 +053099 item.item_tax_amount = 0.0;
Rushabh Mehta54047782013-12-26 11:07:46 +0530100
Nabin Hait1eb56012014-02-10 19:20:15 +0530101 self._set_in_company_currency(item, "amount", "base_amount")
Nabin Haita7f757a2014-02-10 17:54:04 +0530102 self._set_in_company_currency(item, "price_list_rate", "base_price_list_rate")
Nabin Hait7979f7e2014-02-10 18:26:49 +0530103 self._set_in_company_currency(item, "rate", "base_rate")
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530104
105
Anand Doshi8595fcf2013-02-08 19:28:14 +0530106 def calculate_net_total(self):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530107 self.net_total = self.net_total_import = 0.0
Anand Doshi8595fcf2013-02-08 19:28:14 +0530108
109 for item in self.item_doclist:
Anand Doshif78d1ae2014-03-28 13:55:00 +0530110 self.net_total += item.base_amount
111 self.net_total_import += item.amount
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530112
Anand Doshi81ba0b22014-03-28 15:23:26 +0530113 self.round_floats_in(self, ["net_total", "net_total_import"])
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530114
Anand Doshi8595fcf2013-02-08 19:28:14 +0530115 def calculate_totals(self):
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530116 self.grand_total = flt(self.tax_doclist[-1].total if self.tax_doclist
Anand Doshif78d1ae2014-03-28 13:55:00 +0530117 else self.net_total, self.precision("grand_total"))
118 self.grand_total_import = flt(self.grand_total / self.conversion_rate,
Anand Doshi3543f302013-05-24 19:25:01 +0530119 self.precision("grand_total_import"))
Anand Doshi8595fcf2013-02-08 19:28:14 +0530120
Anand Doshif78d1ae2014-03-28 13:55:00 +0530121 self.total_tax = flt(self.grand_total - self.net_total,
Anand Doshi5af812a2013-05-10 19:23:02 +0530122 self.precision("total_tax"))
Anand Doshi8595fcf2013-02-08 19:28:14 +0530123
124 if self.meta.get_field("rounded_total"):
Anand Doshi62b1cbf2014-07-25 12:50:00 +0530125 self.rounded_total = rounded(self.grand_total)
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530126
Anand Doshi8595fcf2013-02-08 19:28:14 +0530127 if self.meta.get_field("rounded_total_import"):
Anand Doshi62b1cbf2014-07-25 12:50:00 +0530128 self.rounded_total_import = rounded(self.grand_total_import)
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530129
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530130 if self.meta.get_field("other_charges_added"):
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530131 self.other_charges_added = flt(sum([flt(d.tax_amount) for d in self.tax_doclist
132 if d.add_deduct_tax=="Add" and d.category in ["Valuation and Total", "Total"]]),
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530133 self.precision("other_charges_added"))
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530134
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530135 if self.meta.get_field("other_charges_deducted"):
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530136 self.other_charges_deducted = flt(sum([flt(d.tax_amount) for d in self.tax_doclist
137 if d.add_deduct_tax=="Deduct" and d.category in ["Valuation and Total", "Total"]]),
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530138 self.precision("other_charges_deducted"))
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530139
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530140 if self.meta.get_field("other_charges_added_import"):
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530141 self.other_charges_added_import = flt(self.other_charges_added /
Anand Doshif78d1ae2014-03-28 13:55:00 +0530142 self.conversion_rate, self.precision("other_charges_added_import"))
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530143
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530144 if self.meta.get_field("other_charges_deducted_import"):
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530145 self.other_charges_deducted_import = flt(self.other_charges_deducted /
Anand Doshif78d1ae2014-03-28 13:55:00 +0530146 self.conversion_rate, self.precision("other_charges_deducted_import"))
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530147
Anand Doshi8595fcf2013-02-08 19:28:14 +0530148 def calculate_outstanding_amount(self):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530149 if self.doctype == "Purchase Invoice" and self.docstatus == 0:
150 self.total_advance = flt(self.total_advance,
Anand Doshi5af812a2013-05-10 19:23:02 +0530151 self.precision("total_advance"))
Anand Doshif78d1ae2014-03-28 13:55:00 +0530152 self.total_amount_to_pay = flt(self.grand_total - flt(self.write_off_amount,
Anand Doshi5af812a2013-05-10 19:23:02 +0530153 self.precision("write_off_amount")), self.precision("total_amount_to_pay"))
Anand Doshif78d1ae2014-03-28 13:55:00 +0530154 self.outstanding_amount = flt(self.total_amount_to_pay - self.total_advance,
Anand Doshi5af812a2013-05-10 19:23:02 +0530155 self.precision("outstanding_amount"))
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530156
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530157 # update valuation rate
158 def update_valuation_rate(self, parentfield):
Anand Doshi8595fcf2013-02-08 19:28:14 +0530159 """
160 item_tax_amount is the total tax amount applied on that item
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530161 stored for valuation
162
Anand Doshi8595fcf2013-02-08 19:28:14 +0530163 TODO: rename item_tax_amount to valuation_tax_amount
164 """
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530165 stock_items = self.get_stock_items()
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530166
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530167 stock_items_qty, stock_items_amount = 0, 0
168 last_stock_item_idx = 1
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +0530169 for d in self.get(parentfield):
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530170 if d.item_code and d.item_code in stock_items:
171 stock_items_qty += flt(d.qty)
Nabin Hait1eb56012014-02-10 19:20:15 +0530172 stock_items_amount += flt(d.base_amount)
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530173 last_stock_item_idx = d.idx
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530174
175 total_valuation_amount = sum([flt(d.tax_amount) for d in
176 self.get("other_charges")
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530177 if d.category in ["Valuation", "Valuation and Total"]])
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530178
179
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530180 valuation_amount_adjustment = total_valuation_amount
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +0530181 for i, item in enumerate(self.get(parentfield)):
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530182 if item.item_code and item.qty and item.item_code in stock_items:
Nabin Hait1eb56012014-02-10 19:20:15 +0530183 item_proportion = flt(item.base_amount) / stock_items_amount if stock_items_amount \
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530184 else flt(item.qty) / stock_items_qty
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530185
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530186 if i == (last_stock_item_idx - 1):
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530187 item.item_tax_amount = flt(valuation_amount_adjustment,
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530188 self.precision("item_tax_amount", item))
189 else:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530190 item.item_tax_amount = flt(item_proportion * total_valuation_amount,
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530191 self.precision("item_tax_amount", item))
192 valuation_amount_adjustment -= item.item_tax_amount
193
Anand Doshi39384d32013-05-11 19:39:53 +0530194 self.round_floats_in(item)
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530195
Anand Doshie9baaa62014-02-26 12:35:33 +0530196 item.conversion_factor = item.conversion_factor or flt(frappe.db.get_value(
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530197 "UOM Conversion Detail", {"parent": item.item_code, "uom": item.uom},
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530198 "conversion_factor")) or 1
Rushabh Mehta54047782013-12-26 11:07:46 +0530199 qty_in_stock_uom = flt(item.qty * item.conversion_factor)
nabinhait87f24012014-07-16 19:48:29 +0530200 rm_supp_cost = flt(item.rm_supp_cost) if self.doctype=="Purchase Receipt" else 0.0
nabinhaitcc0692d2014-07-17 19:16:38 +0530201
nabinhait87f24012014-07-16 19:48:29 +0530202 landed_cost_voucher_amount = flt(item.landed_cost_voucher_amount) \
203 if self.doctype == "Purchase Receipt" else 0.0
Nabin Hait14b8af22014-08-28 12:42:28 +0530204
nabinhait87f24012014-07-16 19:48:29 +0530205 item.valuation_rate = ((item.base_amount + item.item_tax_amount + rm_supp_cost
206 + landed_cost_voucher_amount) / qty_in_stock_uom)
Anand Doshi4a7248e2013-02-27 18:10:30 +0530207 else:
Anand Doshi5af812a2013-05-10 19:23:02 +0530208 item.valuation_rate = 0.0
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530209
Nabin Hait54d209f2013-03-01 18:51:10 +0530210 def validate_for_subcontracting(self):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530211 if not self.is_subcontracted and self.sub_contracted_items:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530212 frappe.throw(_("Please enter 'Is Subcontracted' as Yes or No"))
213
Anand Doshif78d1ae2014-03-28 13:55:00 +0530214 if self.doctype == "Purchase Receipt" and self.is_subcontracted=="Yes" \
215 and not self.supplier_warehouse:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530216 frappe.throw(_("Supplier Warehouse mandatory for sub-contracted Purchase Receipt"))
217
Nabin Hait344f4432014-05-08 19:08:20 +0530218 def create_raw_materials_supplied(self, raw_material_table):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530219 if self.is_subcontracted=="Yes":
Nabin Hait344f4432014-05-08 19:08:20 +0530220 parent_items = []
221 rm_supplied_idx = 0
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +0530222 for item in self.get(self.fname):
Nabin Haita0c239d2014-04-01 18:54:38 +0530223 if self.doctype == "Purchase Receipt":
224 item.rm_supp_cost = 0.0
Nabin Hait54d209f2013-03-01 18:51:10 +0530225 if item.item_code in self.sub_contracted_items:
Nabin Hait344f4432014-05-08 19:08:20 +0530226 self.update_raw_materials_supplied(item, raw_material_table, rm_supplied_idx)
227
228 if [item.item_code, item.name] not in parent_items:
229 parent_items.append([item.item_code, item.name])
230
231 self.cleanup_raw_materials_supplied(parent_items, raw_material_table)
Nabin Hait54d209f2013-03-01 18:51:10 +0530232
Nabin Haita0c239d2014-04-01 18:54:38 +0530233 elif self.doctype == "Purchase Receipt":
234 for item in self.get(self.fname):
235 item.rm_supp_cost = 0.0
236
Nabin Hait344f4432014-05-08 19:08:20 +0530237 def update_raw_materials_supplied(self, item, raw_material_table, rm_supplied_idx):
238 bom_items = self.get_items_from_default_bom(item.item_code)
Nabin Hait54d209f2013-03-01 18:51:10 +0530239 raw_materials_cost = 0
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530240
Nabin Hait344f4432014-05-08 19:08:20 +0530241 for bom_item in bom_items:
242 # check if exists
243 exists = 0
244 for d in self.get(raw_material_table):
245 if d.main_item_code == item.item_code and d.rm_item_code == bom_item.item_code \
246 and d.reference_name == item.name:
247 rm, exists = d, 1
248 break
249
250 if not exists:
251 rm = self.append(raw_material_table, {})
252
253 required_qty = flt(bom_item.qty_consumed_per_unit) * flt(item.qty) * flt(item.conversion_factor)
254 rm.reference_name = item.name
255 rm.bom_detail_no = bom_item.name
256 rm.main_item_code = item.item_code
257 rm.rm_item_code = bom_item.item_code
258 rm.stock_uom = bom_item.stock_uom
259 rm.required_qty = required_qty
260
261 rm.conversion_factor = item.conversion_factor
262 rm.rate = bom_item.rate
263 rm.amount = required_qty * flt(bom_item.rate)
264 rm.idx = rm_supplied_idx
265
Nabin Hait966edff2014-05-09 10:54:12 +0530266 if self.doctype == "Purchase Receipt":
Nabin Hait344f4432014-05-08 19:08:20 +0530267 rm.consumed_qty = required_qty
268 rm.description = bom_item.description
269 if item.batch_no and not rm.batch_no:
270 rm.batch_no = item.batch_no
271
272 rm_supplied_idx += 1
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530273
Nabin Hait75e50ee2014-05-11 12:11:15 +0530274 raw_materials_cost += required_qty * flt(bom_item.rate)
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530275
Anand Doshif78d1ae2014-03-28 13:55:00 +0530276 if self.doctype == "Purchase Receipt":
Nabin Hait344f4432014-05-08 19:08:20 +0530277 item.rm_supp_cost = raw_materials_cost
278
279 def cleanup_raw_materials_supplied(self, parent_items, raw_material_table):
280 """Remove all those child items which are no longer present in main item table"""
281 delete_list = []
282 for d in self.get(raw_material_table):
283 if [d.main_item_code, d.reference_name] not in parent_items:
284 # mark for deletion from doclist
Nabin Haitc3d1d6a2014-06-04 16:41:22 +0530285 delete_list.append(d)
Nabin Hait344f4432014-05-08 19:08:20 +0530286
287 # delete from doclist
288 if delete_list:
289 rm_supplied_details = self.get(raw_material_table)
290 self.set(raw_material_table, [])
291 for d in rm_supplied_details:
292 if d not in delete_list:
293 self.append(raw_material_table, d)
Nabin Hait54d209f2013-03-01 18:51:10 +0530294
295 def get_items_from_default_bom(self, item_code):
Nabin Hait14b8af22014-08-28 12:42:28 +0530296 bom_items = frappe.db.sql("""select t2.item_code,
297 ifnull(t2.qty, 0) / ifnull(t1.quantity, 1) as qty_consumed_per_unit,
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530298 t2.rate, t2.stock_uom, t2.name, t2.description
299 from `tabBOM` t1, `tabBOM Item` t2
300 where t2.parent = t1.name and t1.item = %s and t1.is_default = 1
Nabin Hait54d209f2013-03-01 18:51:10 +0530301 and t1.docstatus = 1 and t1.is_active = 1""", item_code, as_dict=1)
302 if not bom_items:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530303 msgprint(_("No default BOM exists for Item {0}").format(item_code), raise_exception=1)
304
Nabin Hait54d209f2013-03-01 18:51:10 +0530305 return bom_items
306
Nabin Hait5418d712013-02-27 18:11:17 +0530307 @property
308 def sub_contracted_items(self):
309 if not hasattr(self, "_sub_contracted_items"):
Nabin Haitebd51442013-04-23 15:36:26 +0530310 self._sub_contracted_items = []
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530311 item_codes = list(set(item.item_code for item in
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +0530312 self.get(self.fname)))
Nabin Haitebd51442013-04-23 15:36:26 +0530313 if item_codes:
Anand Doshie9baaa62014-02-26 12:35:33 +0530314 self._sub_contracted_items = [r[0] for r in frappe.db.sql("""select name
Nabin Haitebd51442013-04-23 15:36:26 +0530315 from `tabItem` where name in (%s) and is_sub_contracted_item='Yes'""" % \
316 (", ".join((["%s"]*len(item_codes))),), item_codes)]
Nabin Hait5418d712013-02-27 18:11:17 +0530317
318 return self._sub_contracted_items
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530319
Nabin Hait5418d712013-02-27 18:11:17 +0530320 @property
321 def purchase_items(self):
322 if not hasattr(self, "_purchase_items"):
Nabin Haitebd51442013-04-23 15:36:26 +0530323 self._purchase_items = []
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530324 item_codes = list(set(item.item_code for item in
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +0530325 self.get(self.fname)))
Nabin Haitebd51442013-04-23 15:36:26 +0530326 if item_codes:
Anand Doshie9baaa62014-02-26 12:35:33 +0530327 self._purchase_items = [r[0] for r in frappe.db.sql("""select name
Nabin Haitebd51442013-04-23 15:36:26 +0530328 from `tabItem` where name in (%s) and is_purchase_item='Yes'""" % \
329 (", ".join((["%s"]*len(item_codes))),), item_codes)]
Nabin Hait5418d712013-02-27 18:11:17 +0530330
Anand Doshi55d9a622013-02-27 18:14:28 +0530331 return self._purchase_items
Nabin Hait5566bca2013-10-18 17:00:53 +0530332
333
334 def is_item_table_empty(self):
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +0530335 if not len(self.get(self.fname)):
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530336 frappe.throw(_("Item table can not be blank"))
nabinhait614fb752014-07-14 10:47:50 +0530337
338 def set_qty_as_per_stock_uom(self):
339 for d in self.get(self.fname):
340 if d.meta.get_field("stock_qty") and not d.stock_qty:
341 if not d.conversion_factor:
342 frappe.throw(_("Row {0}: Conversion Factor is mandatory"))
Rushabh Mehta5b51cc82014-07-21 18:25:45 +0530343 d.stock_qty = flt(d.qty) * flt(d.conversion_factor)