blob: 24dce4b87665942e48119c47844614ff87cab24b [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
7from frappe.utils import flt, _round
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):
Anand Doshi3543f302013-05-24 19:25:01 +053014 def onload_post_render(self):
Anand Doshi3543f302013-05-24 19:25:01 +053015 # contact, address, item details
16 self.set_missing_values()
Anand Doshi3543f302013-05-24 19:25:01 +053017
Saurabh6f753182013-03-20 12:55:28 +053018 def validate(self):
19 super(BuyingController, self).validate()
Anand Doshif78d1ae2014-03-28 13:55:00 +053020 if self.supplier and not self.supplier_name:
21 self.supplier_name = frappe.db.get_value("Supplier",
22 self.supplier, "supplier_name")
Nabin Hait5566bca2013-10-18 17:00:53 +053023 self.is_item_table_empty()
Nabin Hait205f7ce2013-04-26 13:35:06 +053024 self.validate_stock_or_nonstock_items()
Anand Doshi373680b2013-10-10 16:04:40 +053025 self.validate_warehouse()
Anand Doshi1dde46a2013-05-15 21:15:57 +053026
Anand Doshi3543f302013-05-24 19:25:01 +053027 def set_missing_values(self, for_validate=False):
Anand Doshiabc10032013-06-14 17:44:03 +053028 super(BuyingController, self).set_missing_values(for_validate)
29
Rushabh Mehta0b995402013-08-09 15:29:59 +053030 self.set_supplier_from_item_default()
Nabin Hait096d3632013-10-17 17:01:14 +053031 self.set_price_list_currency("Buying")
Anand Doshiabc10032013-06-14 17:44:03 +053032
Anand Doshi3543f302013-05-24 19:25:01 +053033 # set contact and address details for supplier, if they are not mentioned
Anand Doshif78d1ae2014-03-28 13:55:00 +053034 if self.supplier:
35 self.update_if_missing(get_party_details(self.supplier, party_type="Supplier"))
Rushabh Mehta886c9ef2013-07-08 12:08:06 +053036
Nabin Hait0aa71a52014-02-11 16:14:52 +053037 self.set_missing_item_details()
Anand Doshif78d1ae2014-03-28 13:55:00 +053038 if self.get("__islocal"):
Akhilesh Darjee4f721562014-01-29 16:31:38 +053039 self.set_taxes("other_charges", "taxes_and_charges")
Rushabh Mehta436467a2013-07-08 12:37:59 +053040
Rushabh Mehta0b995402013-08-09 15:29:59 +053041 def set_supplier_from_item_default(self):
Anand Doshif78d1ae2014-03-28 13:55:00 +053042 if self.meta.get_field("supplier") and not self.supplier:
Rushabh Mehtaf191f852014-04-02 18:09:34 +053043 for d in self.get(self.fname):
Anand Doshie9baaa62014-02-26 12:35:33 +053044 supplier = frappe.db.get_value("Item", d.item_code, "default_supplier")
Rushabh Mehta0b995402013-08-09 15:29:59 +053045 if supplier:
Anand Doshif78d1ae2014-03-28 13:55:00 +053046 self.supplier = supplier
Rushabh Mehta0b995402013-08-09 15:29:59 +053047 break
Anand Doshi373680b2013-10-10 16:04:40 +053048
49 def validate_warehouse(self):
Anand Doshi8e332ff2013-12-26 18:30:39 +053050 from erpnext.stock.utils import validate_warehouse_company
Anand Doshi373680b2013-10-10 16:04:40 +053051
52 warehouses = list(set([d.warehouse for d in
Rushabh Mehtaf191f852014-04-02 18:09:34 +053053 self.get(self.fname) if d.warehouse]))
Anand Doshi373680b2013-10-10 16:04:40 +053054
55 for w in warehouses:
Anand Doshif78d1ae2014-03-28 13:55:00 +053056 validate_warehouse_company(w, self.company)
Rushabh Mehta0b995402013-08-09 15:29:59 +053057
Nabin Hait205f7ce2013-04-26 13:35:06 +053058 def validate_stock_or_nonstock_items(self):
Nabin Haita36adbd2013-08-02 14:50:12 +053059 if not self.get_stock_items():
Nabin Hait205f7ce2013-04-26 13:35:06 +053060 tax_for_valuation = [d.account_head for d in
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +053061 self.get("other_charges")
Nabin Hait205f7ce2013-04-26 13:35:06 +053062 if d.category in ["Valuation", "Valuation and Total"]]
63 if tax_for_valuation:
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053064 frappe.msgprint(_("""Tax Category can not be 'Valuation' or 'Valuation and Total' as all items are non-stock items"""), raise_exception=1)
Nabin Hait205f7ce2013-04-26 13:35:06 +053065
Nabin Haitd3b62502013-01-21 17:24:31 +053066 def set_total_in_words(self):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053067 from frappe.utils import money_in_words
Anand Doshif78d1ae2014-03-28 13:55:00 +053068 company_currency = get_company_currency(self.company)
Nabin Hait5b4c2942013-01-22 11:12:02 +053069 if self.meta.get_field("in_words"):
Anand Doshif78d1ae2014-03-28 13:55:00 +053070 self.in_words = money_in_words(self.grand_total, company_currency)
Nabin Hait5b4c2942013-01-22 11:12:02 +053071 if self.meta.get_field("in_words_import"):
Anand Doshif78d1ae2014-03-28 13:55:00 +053072 self.in_words_import = money_in_words(self.grand_total_import,
73 self.currency)
Anand Doshi613cb6a2013-02-06 17:33:46 +053074
75 def calculate_taxes_and_totals(self):
Akhilesh Darjee4f721562014-01-29 16:31:38 +053076 self.other_fname = "other_charges"
Anand Doshi3543f302013-05-24 19:25:01 +053077 super(BuyingController, self).calculate_taxes_and_totals()
Anand Doshi923d41d2013-05-28 17:23:36 +053078 self.calculate_total_advance("Purchase Invoice", "advance_allocation_details")
Anand Doshi8595fcf2013-02-08 19:28:14 +053079
Anand Doshi8595fcf2013-02-08 19:28:14 +053080 def calculate_item_values(self):
Anand Doshib8b1e582013-05-06 12:52:46 +053081 for item in self.item_doclist:
Anand Doshi39384d32013-05-11 19:39:53 +053082 self.round_floats_in(item)
Anand Doshi8595fcf2013-02-08 19:28:14 +053083
Nabin Haita7f757a2014-02-10 17:54:04 +053084 if item.discount_percentage == 100.0:
Nabin Hait7979f7e2014-02-10 18:26:49 +053085 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 Doshia1d4b782013-02-26 18:09:47 +053089
Nabin Hait1eb56012014-02-10 19:20:15 +053090 item.amount = flt(item.rate * item.qty,
91 self.precision("amount", item))
Anand Doshi3543f302013-05-24 19:25:01 +053092 item.item_tax_amount = 0.0;
Rushabh Mehta54047782013-12-26 11:07:46 +053093
Nabin Hait1eb56012014-02-10 19:20:15 +053094 self._set_in_company_currency(item, "amount", "base_amount")
Nabin Haita7f757a2014-02-10 17:54:04 +053095 self._set_in_company_currency(item, "price_list_rate", "base_price_list_rate")
Nabin Hait7979f7e2014-02-10 18:26:49 +053096 self._set_in_company_currency(item, "rate", "base_rate")
Rushabh Mehta54047782013-12-26 11:07:46 +053097
Anand Doshi5af812a2013-05-10 19:23:02 +053098
Anand Doshi8595fcf2013-02-08 19:28:14 +053099 def calculate_net_total(self):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530100 self.net_total = self.net_total_import = 0.0
Anand Doshi8595fcf2013-02-08 19:28:14 +0530101
102 for item in self.item_doclist:
Anand Doshif78d1ae2014-03-28 13:55:00 +0530103 self.net_total += item.base_amount
104 self.net_total_import += item.amount
Anand Doshi8595fcf2013-02-08 19:28:14 +0530105
Anand Doshi81ba0b22014-03-28 15:23:26 +0530106 self.round_floats_in(self, ["net_total", "net_total_import"])
Anand Doshi8595fcf2013-02-08 19:28:14 +0530107
108 def calculate_totals(self):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530109 self.grand_total = flt(self.tax_doclist[-1].total if self.tax_doclist
110 else self.net_total, self.precision("grand_total"))
111 self.grand_total_import = flt(self.grand_total / self.conversion_rate,
Anand Doshi3543f302013-05-24 19:25:01 +0530112 self.precision("grand_total_import"))
Anand Doshi8595fcf2013-02-08 19:28:14 +0530113
Anand Doshif78d1ae2014-03-28 13:55:00 +0530114 self.total_tax = flt(self.grand_total - self.net_total,
Anand Doshi5af812a2013-05-10 19:23:02 +0530115 self.precision("total_tax"))
Anand Doshi8595fcf2013-02-08 19:28:14 +0530116
117 if self.meta.get_field("rounded_total"):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530118 self.rounded_total = _round(self.grand_total)
Anand Doshi8595fcf2013-02-08 19:28:14 +0530119
120 if self.meta.get_field("rounded_total_import"):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530121 self.rounded_total_import = _round(self.grand_total_import)
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530122
123 if self.meta.get_field("other_charges_added"):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530124 self.other_charges_added = flt(sum([flt(d.tax_amount) for d in self.tax_doclist
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530125 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"):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530129 self.other_charges_deducted = flt(sum([flt(d.tax_amount) for d in self.tax_doclist
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530130 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"):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530134 self.other_charges_added_import = flt(self.other_charges_added /
135 self.conversion_rate, self.precision("other_charges_added_import"))
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530136
137 if self.meta.get_field("other_charges_deducted_import"):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530138 self.other_charges_deducted_import = flt(self.other_charges_deducted /
139 self.conversion_rate, self.precision("other_charges_deducted_import"))
Anand Doshi8595fcf2013-02-08 19:28:14 +0530140
141 def calculate_outstanding_amount(self):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530142 if self.doctype == "Purchase Invoice" and self.docstatus == 0:
143 self.total_advance = flt(self.total_advance,
Anand Doshi5af812a2013-05-10 19:23:02 +0530144 self.precision("total_advance"))
Anand Doshif78d1ae2014-03-28 13:55:00 +0530145 self.total_amount_to_pay = flt(self.grand_total - flt(self.write_off_amount,
Anand Doshi5af812a2013-05-10 19:23:02 +0530146 self.precision("write_off_amount")), self.precision("total_amount_to_pay"))
Anand Doshif78d1ae2014-03-28 13:55:00 +0530147 self.outstanding_amount = flt(self.total_amount_to_pay - self.total_advance,
Anand Doshi5af812a2013-05-10 19:23:02 +0530148 self.precision("outstanding_amount"))
Anand Doshi8595fcf2013-02-08 19:28:14 +0530149
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530150 # update valuation rate
151 def update_valuation_rate(self, parentfield):
Anand Doshi8595fcf2013-02-08 19:28:14 +0530152 """
153 item_tax_amount is the total tax amount applied on that item
154 stored for valuation
155
156 TODO: rename item_tax_amount to valuation_tax_amount
157 """
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530158 stock_items = self.get_stock_items()
159
160 stock_items_qty, stock_items_amount = 0, 0
161 last_stock_item_idx = 1
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +0530162 for d in self.get(parentfield):
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530163 if d.item_code and d.item_code in stock_items:
164 stock_items_qty += flt(d.qty)
Nabin Hait1eb56012014-02-10 19:20:15 +0530165 stock_items_amount += flt(d.base_amount)
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530166 last_stock_item_idx = d.idx
Anand Doshi39384d32013-05-11 19:39:53 +0530167
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530168 total_valuation_amount = sum([flt(d.tax_amount) for d in
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +0530169 self.get("other_charges")
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530170 if d.category in ["Valuation", "Valuation and Total"]])
171
172
173 valuation_amount_adjustment = total_valuation_amount
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +0530174 for i, item in enumerate(self.get(parentfield)):
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530175 if item.item_code and item.qty and item.item_code in stock_items:
Nabin Hait1eb56012014-02-10 19:20:15 +0530176 item_proportion = flt(item.base_amount) / stock_items_amount if stock_items_amount \
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530177 else flt(item.qty) / stock_items_qty
178
179 if i == (last_stock_item_idx - 1):
180 item.item_tax_amount = flt(valuation_amount_adjustment,
181 self.precision("item_tax_amount", item))
182 else:
183 item.item_tax_amount = flt(item_proportion * total_valuation_amount,
184 self.precision("item_tax_amount", item))
185 valuation_amount_adjustment -= item.item_tax_amount
186
Anand Doshi39384d32013-05-11 19:39:53 +0530187 self.round_floats_in(item)
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530188
Anand Doshie9baaa62014-02-26 12:35:33 +0530189 item.conversion_factor = item.conversion_factor or flt(frappe.db.get_value(
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530190 "UOM Conversion Detail", {"parent": item.item_code, "uom": item.uom},
191 "conversion_factor")) or 1
Rushabh Mehta54047782013-12-26 11:07:46 +0530192 qty_in_stock_uom = flt(item.qty * item.conversion_factor)
Nabin Hait1eb56012014-02-10 19:20:15 +0530193 item.valuation_rate = ((item.base_amount + item.item_tax_amount + item.rm_supp_cost)
Rushabh Mehta54047782013-12-26 11:07:46 +0530194 / qty_in_stock_uom)
Anand Doshi4a7248e2013-02-27 18:10:30 +0530195 else:
Anand Doshi5af812a2013-05-10 19:23:02 +0530196 item.valuation_rate = 0.0
Nabin Hait54d209f2013-03-01 18:51:10 +0530197
198 def validate_for_subcontracting(self):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530199 if not self.is_subcontracted and self.sub_contracted_items:
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530200 frappe.msgprint(_("""Please enter whether %s is made for subcontracting or purchasing,
Anand Doshif78d1ae2014-03-28 13:55:00 +0530201 in 'Is Subcontracted' field""" % self.doctype), raise_exception=1)
Nabin Hait54d209f2013-03-01 18:51:10 +0530202
Anand Doshif78d1ae2014-03-28 13:55:00 +0530203 if self.doctype == "Purchase Receipt" and self.is_subcontracted=="Yes" \
204 and not self.supplier_warehouse:
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530205 frappe.msgprint(_("Supplier Warehouse mandatory subcontracted purchase receipt"),
Nabin Hait54d209f2013-03-01 18:51:10 +0530206 raise_exception=1)
207
208 def update_raw_materials_supplied(self, raw_material_table):
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +0530209 self.set(raw_material_table, [])
Anand Doshif78d1ae2014-03-28 13:55:00 +0530210 if self.is_subcontracted=="Yes":
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +0530211 for item in self.get(self.fname):
Nabin Hait54d209f2013-03-01 18:51:10 +0530212 if item.item_code in self.sub_contracted_items:
213 self.add_bom_items(item, raw_material_table)
214
215 def add_bom_items(self, d, raw_material_table):
216 bom_items = self.get_items_from_default_bom(d.item_code)
217 raw_materials_cost = 0
218 for item in bom_items:
219 required_qty = flt(item.qty_consumed_per_unit) * flt(d.qty) * flt(d.conversion_factor)
220 rm_doclist = {
Anand Doshif78d1ae2014-03-28 13:55:00 +0530221 "doctype": self.doctype + " Item Supplied",
Nabin Hait54d209f2013-03-01 18:51:10 +0530222 "reference_name": d.name,
223 "bom_detail_no": item.name,
224 "main_item_code": d.item_code,
225 "rm_item_code": item.item_code,
226 "stock_uom": item.stock_uom,
227 "required_qty": required_qty,
228 "conversion_factor": d.conversion_factor,
229 "rate": item.rate,
230 "amount": required_qty * flt(item.rate)
231 }
Anand Doshif78d1ae2014-03-28 13:55:00 +0530232 if self.doctype == "Purchase Receipt":
Nabin Hait54d209f2013-03-01 18:51:10 +0530233 rm_doclist.update({
234 "consumed_qty": required_qty,
235 "description": item.description,
236 })
237
Rushabh Mehtae88bc8b2014-03-27 17:51:41 +0530238 self.append(raw_material_table, rm_doclist)
Nabin Hait54d209f2013-03-01 18:51:10 +0530239
240 raw_materials_cost += required_qty * flt(item.rate)
241
Anand Doshif78d1ae2014-03-28 13:55:00 +0530242 if self.doctype == "Purchase Receipt":
Nabin Hait54d209f2013-03-01 18:51:10 +0530243 d.rm_supp_cost = raw_materials_cost
244
245 def get_items_from_default_bom(self, item_code):
Anand Doshie9baaa62014-02-26 12:35:33 +0530246 bom_items = frappe.db.sql("""select t2.item_code, t2.qty_consumed_per_unit,
Nabin Hait54d209f2013-03-01 18:51:10 +0530247 t2.rate, t2.stock_uom, t2.name, t2.description
248 from `tabBOM` t1, `tabBOM Item` t2
249 where t2.parent = t1.name and t1.item = %s and t1.is_default = 1
250 and t1.docstatus = 1 and t1.is_active = 1""", item_code, as_dict=1)
251 if not bom_items:
252 msgprint(_("No default BOM exists for item: ") + item_code, raise_exception=1)
253
254 return bom_items
255
Nabin Hait5418d712013-02-27 18:11:17 +0530256 @property
257 def sub_contracted_items(self):
258 if not hasattr(self, "_sub_contracted_items"):
Nabin Haitebd51442013-04-23 15:36:26 +0530259 self._sub_contracted_items = []
Nabin Hait5418d712013-02-27 18:11:17 +0530260 item_codes = list(set(item.item_code for item in
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +0530261 self.get(self.fname)))
Nabin Haitebd51442013-04-23 15:36:26 +0530262 if item_codes:
Anand Doshie9baaa62014-02-26 12:35:33 +0530263 self._sub_contracted_items = [r[0] for r in frappe.db.sql("""select name
Nabin Haitebd51442013-04-23 15:36:26 +0530264 from `tabItem` where name in (%s) and is_sub_contracted_item='Yes'""" % \
265 (", ".join((["%s"]*len(item_codes))),), item_codes)]
Nabin Hait5418d712013-02-27 18:11:17 +0530266
267 return self._sub_contracted_items
268
269 @property
270 def purchase_items(self):
271 if not hasattr(self, "_purchase_items"):
Nabin Haitebd51442013-04-23 15:36:26 +0530272 self._purchase_items = []
Nabin Hait5418d712013-02-27 18:11:17 +0530273 item_codes = list(set(item.item_code for item in
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +0530274 self.get(self.fname)))
Nabin Haitebd51442013-04-23 15:36:26 +0530275 if item_codes:
Anand Doshie9baaa62014-02-26 12:35:33 +0530276 self._purchase_items = [r[0] for r in frappe.db.sql("""select name
Nabin Haitebd51442013-04-23 15:36:26 +0530277 from `tabItem` where name in (%s) and is_purchase_item='Yes'""" % \
278 (", ".join((["%s"]*len(item_codes))),), item_codes)]
Nabin Hait5418d712013-02-27 18:11:17 +0530279
Anand Doshi55d9a622013-02-27 18:14:28 +0530280 return self._purchase_items
Nabin Hait5566bca2013-10-18 17:00:53 +0530281
282
283 def is_item_table_empty(self):
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +0530284 if not len(self.get(self.fname)):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530285 frappe.throw(_("Item table can not be blank"))