blob: 78ab2f17388282f456d235c8bafe8c7808436628 [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
Sambhaji Kolatee3d26432014-09-10 13:07:59 +05308
Rushabh Mehta1f847992013-12-12 19:12:19 +05309from erpnext.setup.utils import get_company_currency
Rushabh Mehtacc008cc2014-02-03 16:14:56 +053010from erpnext.accounts.party import get_party_details
Rushabh Mehta724f9e52014-10-07 15:29:58 +053011from erpnext.stock.get_item_details import get_conversion_factor
Anand Doshi756dca72013-01-15 18:39:21 +053012
Rushabh Mehta1f847992013-12-12 19:12:19 +053013from erpnext.controllers.stock_controller import StockController
Nabin Haitbf495c92013-01-30 12:49:08 +053014
Nabin Hait89a94d82013-03-19 12:01:46 +053015class BuyingController(StockController):
Rushabh Mehta5b51cc82014-07-21 18:25:45 +053016 def __setup__(self):
Nabin Haitdd38a262014-12-26 13:15:21 +053017 if hasattr(self, "items"):
Rushabh Mehtac567e8e2015-02-03 17:55:52 +053018 self.print_templates = {
19 "taxes": "templates/print_formats/includes/taxes.html"
Rushabh Mehta5b51cc82014-07-21 18:25:45 +053020 }
21
Rushabh Mehtac0bb4532014-09-09 16:15:35 +053022 def get_feed(self):
23 return _("From {0} | {1} {2}").format(self.supplier_name, self.currency,
Nabin Hait5690be12015-02-12 16:09:11 +053024 self.grand_total)
Rushabh Mehtac0bb4532014-09-09 16:15:35 +053025
Saurabh6f753182013-03-20 12:55:28 +053026 def validate(self):
27 super(BuyingController, self).validate()
Nabin Hait365ae272014-04-03 17:38:54 +053028 if getattr(self, "supplier", None) and not self.supplier_name:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053029 self.supplier_name = frappe.db.get_value("Supplier",
Anand Doshif78d1ae2014-03-28 13:55:00 +053030 self.supplier, "supplier_name")
Nabin Hait5566bca2013-10-18 17:00:53 +053031 self.is_item_table_empty()
nabinhait614fb752014-07-14 10:47:50 +053032 self.set_qty_as_per_stock_uom()
Nabin Hait205f7ce2013-04-26 13:35:06 +053033 self.validate_stock_or_nonstock_items()
Anand Doshi373680b2013-10-10 16:04:40 +053034 self.validate_warehouse()
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053035
Anand Doshi3543f302013-05-24 19:25:01 +053036 def set_missing_values(self, for_validate=False):
Anand Doshiabc10032013-06-14 17:44:03 +053037 super(BuyingController, self).set_missing_values(for_validate)
38
Rushabh Mehta0b995402013-08-09 15:29:59 +053039 self.set_supplier_from_item_default()
Nabin Hait096d3632013-10-17 17:01:14 +053040 self.set_price_list_currency("Buying")
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053041
Anand Doshi3543f302013-05-24 19:25:01 +053042 # set contact and address details for supplier, if they are not mentioned
Nabin Hait365ae272014-04-03 17:38:54 +053043 if getattr(self, "supplier", None):
Anand Doshif78d1ae2014-03-28 13:55:00 +053044 self.update_if_missing(get_party_details(self.supplier, party_type="Supplier"))
Rushabh Mehta886c9ef2013-07-08 12:08:06 +053045
Nabin Hait0aa71a52014-02-11 16:14:52 +053046 self.set_missing_item_details()
Anand Doshif78d1ae2014-03-28 13:55:00 +053047 if self.get("__islocal"):
Nabin Haite7d15362014-12-25 16:01:55 +053048 self.set_taxes("taxes", "taxes_and_charges")
Rushabh Mehta436467a2013-07-08 12:37:59 +053049
Rushabh Mehta0b995402013-08-09 15:29:59 +053050 def set_supplier_from_item_default(self):
Anand Doshif78d1ae2014-03-28 13:55:00 +053051 if self.meta.get_field("supplier") and not self.supplier:
Nabin Haitdd38a262014-12-26 13:15:21 +053052 for d in self.get("items"):
Anand Doshie9baaa62014-02-26 12:35:33 +053053 supplier = frappe.db.get_value("Item", d.item_code, "default_supplier")
Rushabh Mehta0b995402013-08-09 15:29:59 +053054 if supplier:
Anand Doshif78d1ae2014-03-28 13:55:00 +053055 self.supplier = supplier
Rushabh Mehta0b995402013-08-09 15:29:59 +053056 break
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053057
Anand Doshi373680b2013-10-10 16:04:40 +053058 def validate_warehouse(self):
Anand Doshi8e332ff2013-12-26 18:30:39 +053059 from erpnext.stock.utils import validate_warehouse_company
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053060
61 warehouses = list(set([d.warehouse for d in
Nabin Haitdd38a262014-12-26 13:15:21 +053062 self.get("items") if getattr(d, "warehouse", None)]))
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053063
Anand Doshi373680b2013-10-10 16:04:40 +053064 for w in warehouses:
Anand Doshif78d1ae2014-03-28 13:55:00 +053065 validate_warehouse_company(w, self.company)
Rushabh Mehta0b995402013-08-09 15:29:59 +053066
Nabin Hait205f7ce2013-04-26 13:35:06 +053067 def validate_stock_or_nonstock_items(self):
Nabin Haite7d15362014-12-25 16:01:55 +053068 if self.meta.get_field("taxes") and not self.get_stock_items():
69 tax_for_valuation = [d.account_head for d in self.get("taxes")
Nabin Hait205f7ce2013-04-26 13:35:06 +053070 if d.category in ["Valuation", "Valuation and Total"]]
71 if tax_for_valuation:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053072 frappe.throw(_("Tax Category can not be 'Valuation' or 'Valuation and Total' as all items are non-stock items"))
73
Nabin Haitd3b62502013-01-21 17:24:31 +053074 def set_total_in_words(self):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053075 from frappe.utils import money_in_words
Anand Doshif78d1ae2014-03-28 13:55:00 +053076 company_currency = get_company_currency(self.company)
Nabin Hait5690be12015-02-12 16:09:11 +053077 if self.meta.get_field("base_in_words"):
78 self.base_in_words = money_in_words(self.base_grand_total, company_currency)
Nabin Hait5b4c2942013-01-22 11:12:02 +053079 if self.meta.get_field("in_words"):
Nabin Hait5690be12015-02-12 16:09:11 +053080 self.in_words = money_in_words(self.grand_total,
Anand Doshif78d1ae2014-03-28 13:55:00 +053081 self.currency)
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053082
Anand Doshi613cb6a2013-02-06 17:33:46 +053083 def calculate_taxes_and_totals(self):
Anand Doshi3543f302013-05-24 19:25:01 +053084 super(BuyingController, self).calculate_taxes_and_totals()
Nabin Haite7d15362014-12-25 16:01:55 +053085 self.calculate_total_advance("Purchase Invoice", "advances")
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053086
Anand Doshi8595fcf2013-02-08 19:28:14 +053087 def calculate_item_values(self):
Nabin Haitdd38a262014-12-26 13:15:21 +053088 for item in self.get("items"):
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 Hait5e13e0c2015-02-05 17:18:03 +053097 item.amount = flt(item.rate * item.qty, self.precision("amount", item))
Anand Doshi3543f302013-05-24 19:25:01 +053098 item.item_tax_amount = 0.0;
Rushabh Mehta54047782013-12-26 11:07:46 +053099
Nabin Hait1eb56012014-02-10 19:20:15 +0530100 self._set_in_company_currency(item, "amount", "base_amount")
Nabin Haita7f757a2014-02-10 17:54:04 +0530101 self._set_in_company_currency(item, "price_list_rate", "base_price_list_rate")
Nabin Hait7979f7e2014-02-10 18:26:49 +0530102 self._set_in_company_currency(item, "rate", "base_rate")
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530103
104
Anand Doshi8595fcf2013-02-08 19:28:14 +0530105 def calculate_net_total(self):
Nabin Hait5690be12015-02-12 16:09:11 +0530106 self.base_net_total = self.net_total = 0.0
Anand Doshi8595fcf2013-02-08 19:28:14 +0530107
Nabin Haitdd38a262014-12-26 13:15:21 +0530108 for item in self.get("items"):
Nabin Hait5690be12015-02-12 16:09:11 +0530109 self.base_net_total += item.base_amount
110 self.net_total += item.amount
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530111
Nabin Hait5690be12015-02-12 16:09:11 +0530112 self.round_floats_in(self, ["base_net_total", "net_total"])
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530113
Anand Doshi8595fcf2013-02-08 19:28:14 +0530114 def calculate_totals(self):
Nabin Hait5690be12015-02-12 16:09:11 +0530115 self.base_grand_total = flt(self.get("taxes")[-1].total if self.get("taxes") else self.base_net_total)
Anand Doshi8595fcf2013-02-08 19:28:14 +0530116
Nabin Hait5690be12015-02-12 16:09:11 +0530117 self.base_total_taxes_and_charges = flt(self.base_grand_total - self.base_net_total, self.precision("base_total_taxes_and_charges"))
118
119 self.base_grand_total = flt(self.base_grand_total, self.precision("base_grand_total"))
120
121 if self.meta.get_field("base_rounded_total"):
122 self.base_rounded_total = rounded(self.base_grand_total)
123
124 if self.meta.get_field("base_taxes_and_charges_added"):
125 self.base_taxes_and_charges_added = flt(sum([flt(d.tax_amount) for d in self.get("taxes")
126 if d.add_deduct_tax=="Add" and d.category in ["Valuation and Total", "Total"]]),
127 self.precision("base_taxes_and_charges_added"))
128
129 if self.meta.get_field("base_taxes_and_charges_deducted"):
130 self.base_taxes_and_charges_deducted = flt(sum([flt(d.tax_amount) for d in self.get("taxes")
131 if d.add_deduct_tax=="Deduct" and d.category in ["Valuation and Total", "Total"]]),
132 self.precision("base_taxes_and_charges_deducted"))
133
134 self.grand_total = flt(self.base_grand_total / self.conversion_rate) \
135 if (self.base_taxes_and_charges_added or self.base_taxes_and_charges_deducted) else self.net_total
Nabin Haitccd9fd32014-11-14 14:27:24 +0530136
137 self.grand_total = flt(self.grand_total, self.precision("grand_total"))
Anand Doshi8595fcf2013-02-08 19:28:14 +0530138
139 if self.meta.get_field("rounded_total"):
Anand Doshi62b1cbf2014-07-25 12:50:00 +0530140 self.rounded_total = rounded(self.grand_total)
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530141
Nabin Hait5690be12015-02-12 16:09:11 +0530142 if self.meta.get_field("taxes_and_charges_added"):
143 self.taxes_and_charges_added = flt(self.base_taxes_and_charges_added /
144 self.conversion_rate, self.precision("taxes_and_charges_added"))
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530145
Nabin Hait5690be12015-02-12 16:09:11 +0530146 if self.meta.get_field("taxes_and_charges_deducted"):
147 self.taxes_and_charges_deducted = flt(self.base_taxes_and_charges_deducted /
148 self.conversion_rate, self.precision("taxes_and_charges_deducted"))
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530149
Anand Doshi8595fcf2013-02-08 19:28:14 +0530150 def calculate_outstanding_amount(self):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530151 if self.doctype == "Purchase Invoice" and self.docstatus == 0:
152 self.total_advance = flt(self.total_advance,
Anand Doshi5af812a2013-05-10 19:23:02 +0530153 self.precision("total_advance"))
Nabin Hait5690be12015-02-12 16:09:11 +0530154 self.total_amount_to_pay = flt(self.base_grand_total - flt(self.write_off_amount,
Anand Doshi5af812a2013-05-10 19:23:02 +0530155 self.precision("write_off_amount")), self.precision("total_amount_to_pay"))
Anand Doshif78d1ae2014-03-28 13:55:00 +0530156 self.outstanding_amount = flt(self.total_amount_to_pay - self.total_advance,
Anand Doshi5af812a2013-05-10 19:23:02 +0530157 self.precision("outstanding_amount"))
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530158
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530159 # update valuation rate
160 def update_valuation_rate(self, parentfield):
Anand Doshi8595fcf2013-02-08 19:28:14 +0530161 """
162 item_tax_amount is the total tax amount applied on that item
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530163 stored for valuation
164
Anand Doshi8595fcf2013-02-08 19:28:14 +0530165 TODO: rename item_tax_amount to valuation_tax_amount
166 """
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530167 stock_items = self.get_stock_items()
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530168
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530169 stock_items_qty, stock_items_amount = 0, 0
170 last_stock_item_idx = 1
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +0530171 for d in self.get(parentfield):
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530172 if d.item_code and d.item_code in stock_items:
173 stock_items_qty += flt(d.qty)
Nabin Hait1eb56012014-02-10 19:20:15 +0530174 stock_items_amount += flt(d.base_amount)
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530175 last_stock_item_idx = d.idx
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530176
177 total_valuation_amount = sum([flt(d.tax_amount) for d in
Nabin Haite7d15362014-12-25 16:01:55 +0530178 self.get("taxes")
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530179 if d.category in ["Valuation", "Valuation and Total"]])
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530180
181
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530182 valuation_amount_adjustment = total_valuation_amount
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +0530183 for i, item in enumerate(self.get(parentfield)):
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530184 if item.item_code and item.qty and item.item_code in stock_items:
Nabin Hait1eb56012014-02-10 19:20:15 +0530185 item_proportion = flt(item.base_amount) / stock_items_amount if stock_items_amount \
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530186 else flt(item.qty) / stock_items_qty
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530187
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530188 if i == (last_stock_item_idx - 1):
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530189 item.item_tax_amount = flt(valuation_amount_adjustment,
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530190 self.precision("item_tax_amount", item))
191 else:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530192 item.item_tax_amount = flt(item_proportion * total_valuation_amount,
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530193 self.precision("item_tax_amount", item))
194 valuation_amount_adjustment -= item.item_tax_amount
195
Anand Doshi39384d32013-05-11 19:39:53 +0530196 self.round_floats_in(item)
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530197
Rushabh Mehtac62b6a82014-10-07 17:15:30 +0530198 item.conversion_factor = get_conversion_factor(item.item_code, item.uom).get("conversion_factor") or 1.0
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530199
Rushabh Mehta54047782013-12-26 11:07:46 +0530200 qty_in_stock_uom = flt(item.qty * item.conversion_factor)
nabinhait87f24012014-07-16 19:48:29 +0530201 rm_supp_cost = flt(item.rm_supp_cost) if self.doctype=="Purchase Receipt" else 0.0
nabinhaitcc0692d2014-07-17 19:16:38 +0530202
nabinhait87f24012014-07-16 19:48:29 +0530203 landed_cost_voucher_amount = flt(item.landed_cost_voucher_amount) \
204 if self.doctype == "Purchase Receipt" else 0.0
Nabin Hait14b8af22014-08-28 12:42:28 +0530205
nabinhait87f24012014-07-16 19:48:29 +0530206 item.valuation_rate = ((item.base_amount + item.item_tax_amount + rm_supp_cost
207 + landed_cost_voucher_amount) / qty_in_stock_uom)
Anand Doshi4a7248e2013-02-27 18:10:30 +0530208 else:
Anand Doshi5af812a2013-05-10 19:23:02 +0530209 item.valuation_rate = 0.0
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530210
Nabin Hait54d209f2013-03-01 18:51:10 +0530211 def validate_for_subcontracting(self):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530212 if not self.is_subcontracted and self.sub_contracted_items:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530213 frappe.throw(_("Please enter 'Is Subcontracted' as Yes or No"))
214
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530215 if self.is_subcontracted == "Yes":
216 if self.doctype == "Purchase Receipt" and not self.supplier_warehouse:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530217 frappe.throw(_("Supplier Warehouse mandatory for sub-contracted Purchase Receipt"))
218
Nabin Haitdd38a262014-12-26 13:15:21 +0530219 for item in self.get("items"):
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530220 if item in self.sub_contracted_items and not item.bom:
221 frappe.throw(_("Please select BOM in BOM field for Item {0}").format(item.item_code))
222
223 else:
Nabin Haitdd38a262014-12-26 13:15:21 +0530224 for item in self.get("items"):
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530225 if item.bom:
226 item.bom = None
227
Nabin Hait344f4432014-05-08 19:08:20 +0530228 def create_raw_materials_supplied(self, raw_material_table):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530229 if self.is_subcontracted=="Yes":
Nabin Hait344f4432014-05-08 19:08:20 +0530230 parent_items = []
231 rm_supplied_idx = 0
Nabin Haitdd38a262014-12-26 13:15:21 +0530232 for item in self.get("items"):
Nabin Haita0c239d2014-04-01 18:54:38 +0530233 if self.doctype == "Purchase Receipt":
234 item.rm_supp_cost = 0.0
Nabin Hait54d209f2013-03-01 18:51:10 +0530235 if item.item_code in self.sub_contracted_items:
Nabin Hait344f4432014-05-08 19:08:20 +0530236 self.update_raw_materials_supplied(item, raw_material_table, rm_supplied_idx)
237
238 if [item.item_code, item.name] not in parent_items:
239 parent_items.append([item.item_code, item.name])
240
241 self.cleanup_raw_materials_supplied(parent_items, raw_material_table)
Nabin Hait54d209f2013-03-01 18:51:10 +0530242
Nabin Haita0c239d2014-04-01 18:54:38 +0530243 elif self.doctype == "Purchase Receipt":
Nabin Haitdd38a262014-12-26 13:15:21 +0530244 for item in self.get("items"):
Nabin Haita0c239d2014-04-01 18:54:38 +0530245 item.rm_supp_cost = 0.0
246
Nabin Hait344f4432014-05-08 19:08:20 +0530247 def update_raw_materials_supplied(self, item, raw_material_table, rm_supplied_idx):
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530248 bom_items = self.get_items_from_bom(item.item_code, item.bom)
Nabin Hait54d209f2013-03-01 18:51:10 +0530249 raw_materials_cost = 0
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530250
Nabin Hait344f4432014-05-08 19:08:20 +0530251 for bom_item in bom_items:
252 # check if exists
253 exists = 0
254 for d in self.get(raw_material_table):
255 if d.main_item_code == item.item_code and d.rm_item_code == bom_item.item_code \
256 and d.reference_name == item.name:
257 rm, exists = d, 1
258 break
259
260 if not exists:
261 rm = self.append(raw_material_table, {})
262
263 required_qty = flt(bom_item.qty_consumed_per_unit) * flt(item.qty) * flt(item.conversion_factor)
264 rm.reference_name = item.name
265 rm.bom_detail_no = bom_item.name
266 rm.main_item_code = item.item_code
267 rm.rm_item_code = bom_item.item_code
268 rm.stock_uom = bom_item.stock_uom
269 rm.required_qty = required_qty
270
271 rm.conversion_factor = item.conversion_factor
Nabin Hait344f4432014-05-08 19:08:20 +0530272 rm.idx = rm_supplied_idx
273
Nabin Hait966edff2014-05-09 10:54:12 +0530274 if self.doctype == "Purchase Receipt":
Nabin Hait344f4432014-05-08 19:08:20 +0530275 rm.consumed_qty = required_qty
276 rm.description = bom_item.description
277 if item.batch_no and not rm.batch_no:
278 rm.batch_no = item.batch_no
279
280 rm_supplied_idx += 1
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530281
Nabin Haite96e83d2014-10-08 18:06:14 +0530282 # get raw materials rate
Nabin Haitfce28812014-10-08 18:38:27 +0530283 if self.doctype == "Purchase Receipt":
284 from erpnext.stock.utils import get_incoming_rate
Nabin Haitfb6e4342014-10-15 11:34:40 +0530285 rm.rate = get_incoming_rate({
Nabin Haitfce28812014-10-08 18:38:27 +0530286 "item_code": bom_item.item_code,
287 "warehouse": self.supplier_warehouse,
288 "posting_date": self.posting_date,
289 "posting_time": self.posting_time,
290 "qty": -1 * required_qty,
291 "serial_no": rm.serial_no
292 })
Nabin Haitfb6e4342014-10-15 11:34:40 +0530293 if not rm.rate:
294 from erpnext.stock.stock_ledger import get_valuation_rate
295 rm.rate = get_valuation_rate(bom_item.item_code, self.supplier_warehouse)
Nabin Haitfce28812014-10-08 18:38:27 +0530296 else:
297 rm.rate = bom_item.rate
Nabin Haite96e83d2014-10-08 18:06:14 +0530298
Nabin Haitfce28812014-10-08 18:38:27 +0530299 rm.amount = required_qty * flt(rm.rate)
Nabin Haite96e83d2014-10-08 18:06:14 +0530300 raw_materials_cost += flt(rm.amount)
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530301
Anand Doshif78d1ae2014-03-28 13:55:00 +0530302 if self.doctype == "Purchase Receipt":
Nabin Hait344f4432014-05-08 19:08:20 +0530303 item.rm_supp_cost = raw_materials_cost
304
305 def cleanup_raw_materials_supplied(self, parent_items, raw_material_table):
306 """Remove all those child items which are no longer present in main item table"""
307 delete_list = []
308 for d in self.get(raw_material_table):
309 if [d.main_item_code, d.reference_name] not in parent_items:
310 # mark for deletion from doclist
Nabin Haitc3d1d6a2014-06-04 16:41:22 +0530311 delete_list.append(d)
Nabin Hait344f4432014-05-08 19:08:20 +0530312
313 # delete from doclist
314 if delete_list:
315 rm_supplied_details = self.get(raw_material_table)
316 self.set(raw_material_table, [])
317 for d in rm_supplied_details:
318 if d not in delete_list:
319 self.append(raw_material_table, d)
Nabin Hait54d209f2013-03-01 18:51:10 +0530320
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530321 def get_items_from_bom(self, item_code, bom):
Nabin Hait14b8af22014-08-28 12:42:28 +0530322 bom_items = frappe.db.sql("""select t2.item_code,
323 ifnull(t2.qty, 0) / ifnull(t1.quantity, 1) as qty_consumed_per_unit,
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530324 t2.rate, t2.stock_uom, t2.name, t2.description
325 from `tabBOM` t1, `tabBOM Item` t2
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530326 where t2.parent = t1.name and t1.item = %s
327 and t1.docstatus = 1 and t1.is_active = 1 and t1.name = %s""", (item_code, bom), as_dict=1)
328
Nabin Hait54d209f2013-03-01 18:51:10 +0530329 if not bom_items:
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530330 msgprint(_("Specified BOM {0} does not exist for Item {1}").format(bom, item_code), raise_exception=1)
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530331
Nabin Hait54d209f2013-03-01 18:51:10 +0530332 return bom_items
333
Nabin Hait5418d712013-02-27 18:11:17 +0530334 @property
335 def sub_contracted_items(self):
336 if not hasattr(self, "_sub_contracted_items"):
Nabin Haitebd51442013-04-23 15:36:26 +0530337 self._sub_contracted_items = []
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530338 item_codes = list(set(item.item_code for item in
Nabin Haitdd38a262014-12-26 13:15:21 +0530339 self.get("items")))
Nabin Haitebd51442013-04-23 15:36:26 +0530340 if item_codes:
Anand Doshie9baaa62014-02-26 12:35:33 +0530341 self._sub_contracted_items = [r[0] for r in frappe.db.sql("""select name
Nabin Haitebd51442013-04-23 15:36:26 +0530342 from `tabItem` where name in (%s) and is_sub_contracted_item='Yes'""" % \
343 (", ".join((["%s"]*len(item_codes))),), item_codes)]
Nabin Hait5418d712013-02-27 18:11:17 +0530344
345 return self._sub_contracted_items
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530346
Nabin Hait5418d712013-02-27 18:11:17 +0530347 @property
348 def purchase_items(self):
349 if not hasattr(self, "_purchase_items"):
Nabin Haitebd51442013-04-23 15:36:26 +0530350 self._purchase_items = []
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530351 item_codes = list(set(item.item_code for item in
Nabin Haitdd38a262014-12-26 13:15:21 +0530352 self.get("items")))
Nabin Haitebd51442013-04-23 15:36:26 +0530353 if item_codes:
Anand Doshie9baaa62014-02-26 12:35:33 +0530354 self._purchase_items = [r[0] for r in frappe.db.sql("""select name
Nabin Haitebd51442013-04-23 15:36:26 +0530355 from `tabItem` where name in (%s) and is_purchase_item='Yes'""" % \
356 (", ".join((["%s"]*len(item_codes))),), item_codes)]
Nabin Hait5418d712013-02-27 18:11:17 +0530357
Anand Doshi55d9a622013-02-27 18:14:28 +0530358 return self._purchase_items
Nabin Hait5566bca2013-10-18 17:00:53 +0530359
360
361 def is_item_table_empty(self):
Nabin Haitdd38a262014-12-26 13:15:21 +0530362 if not len(self.get("items")):
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530363 frappe.throw(_("Item table can not be blank"))
nabinhait614fb752014-07-14 10:47:50 +0530364
365 def set_qty_as_per_stock_uom(self):
Nabin Haitdd38a262014-12-26 13:15:21 +0530366 for d in self.get("items"):
nabinhait614fb752014-07-14 10:47:50 +0530367 if d.meta.get_field("stock_qty") and not d.stock_qty:
368 if not d.conversion_factor:
369 frappe.throw(_("Row {0}: Conversion Factor is mandatory"))
Rushabh Mehta5b51cc82014-07-21 18:25:45 +0530370 d.stock_qty = flt(d.qty) * flt(d.conversion_factor)
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530371