blob: 2563e609fd6696e1b57494725312da47be1b2902 [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
Nabin Hait5566bca2013-10-18 17:00:53 +05305import webnotes
Anand Doshif8f0c0d2013-01-17 20:20:56 +05306from webnotes import _, msgprint
Anand Doshi2e31e092013-08-19 19:58:23 +05307from webnotes.utils import flt, _round
Rushabh Mehta1f847992013-12-12 19:12:19 +05308from erpnext.buying.utils import get_item_details
9from erpnext.setup.utils import get_company_currency
Rushabh Mehtacc008cc2014-02-03 16:14:56 +053010from erpnext.accounts.party import get_party_details
Anand Doshi756dca72013-01-15 18:39:21 +053011
Rushabh Mehta1f847992013-12-12 19:12:19 +053012from erpnext.controllers.stock_controller import StockController
Nabin Haitbf495c92013-01-30 12:49:08 +053013
Nabin Hait89a94d82013-03-19 12:01:46 +053014class BuyingController(StockController):
Anand Doshi3543f302013-05-24 19:25:01 +053015 def onload_post_render(self):
Anand Doshi3543f302013-05-24 19:25:01 +053016 # contact, address, item details
17 self.set_missing_values()
Anand Doshi3543f302013-05-24 19:25:01 +053018
Saurabh6f753182013-03-20 12:55:28 +053019 def validate(self):
20 super(BuyingController, self).validate()
Rushabh Mehta886c9ef2013-07-08 12:08:06 +053021 if self.doc.supplier and not self.doc.supplier_name:
22 self.doc.supplier_name = webnotes.conn.get_value("Supplier",
23 self.doc.supplier, "supplier_name")
Nabin Hait5566bca2013-10-18 17:00:53 +053024 self.is_item_table_empty()
Nabin Hait205f7ce2013-04-26 13:35:06 +053025 self.validate_stock_or_nonstock_items()
Anand Doshi373680b2013-10-10 16:04:40 +053026 self.validate_warehouse()
Anand Doshi1dde46a2013-05-15 21:15:57 +053027
Anand Doshi3543f302013-05-24 19:25:01 +053028 def set_missing_values(self, for_validate=False):
Anand Doshiabc10032013-06-14 17:44:03 +053029 super(BuyingController, self).set_missing_values(for_validate)
30
Rushabh Mehta0b995402013-08-09 15:29:59 +053031 self.set_supplier_from_item_default()
Nabin Hait096d3632013-10-17 17:01:14 +053032 self.set_price_list_currency("Buying")
Anand Doshiabc10032013-06-14 17:44:03 +053033
Anand Doshi3543f302013-05-24 19:25:01 +053034 # set contact and address details for supplier, if they are not mentioned
Rushabh Mehta49dd7be2014-01-28 17:43:10 +053035 if self.doc.supplier:
Rushabh Mehtacc008cc2014-02-03 16:14:56 +053036 self.doc.update_if_missing(get_party_details(self.doc.supplier, party_type="Supplier"))
Rushabh Mehta886c9ef2013-07-08 12:08:06 +053037
Anand Doshi3543f302013-05-24 19:25:01 +053038 self.set_missing_item_details(get_item_details)
Rushabh Mehtae89c5332013-08-29 15:32:46 +053039 if self.doc.fields.get("__islocal"):
Akhilesh Darjee4f721562014-01-29 16:31:38 +053040 self.set_taxes("other_charges", "taxes_and_charges")
Rushabh Mehta436467a2013-07-08 12:37:59 +053041
Rushabh Mehta0b995402013-08-09 15:29:59 +053042 def set_supplier_from_item_default(self):
43 if self.meta.get_field("supplier") and not self.doc.supplier:
44 for d in self.doclist.get({"doctype": self.tname}):
45 supplier = webnotes.conn.get_value("Item", d.item_code, "default_supplier")
46 if supplier:
47 self.doc.supplier = supplier
48 break
Anand Doshi373680b2013-10-10 16:04:40 +053049
50 def validate_warehouse(self):
Anand Doshi8e332ff2013-12-26 18:30:39 +053051 from erpnext.stock.utils import validate_warehouse_company
Anand Doshi373680b2013-10-10 16:04:40 +053052
53 warehouses = list(set([d.warehouse for d in
54 self.doclist.get({"doctype": self.tname}) if d.warehouse]))
55
56 for w in warehouses:
Anand Doshi373680b2013-10-10 16:04:40 +053057 validate_warehouse_company(w, self.doc.company)
Rushabh Mehta0b995402013-08-09 15:29:59 +053058
Nabin Hait205f7ce2013-04-26 13:35:06 +053059 def validate_stock_or_nonstock_items(self):
Nabin Haita36adbd2013-08-02 14:50:12 +053060 if not self.get_stock_items():
Nabin Hait205f7ce2013-04-26 13:35:06 +053061 tax_for_valuation = [d.account_head for d in
Akhilesh Darjee4f721562014-01-29 16:31:38 +053062 self.doclist.get({"parentfield": "other_charges"})
Nabin Hait205f7ce2013-04-26 13:35:06 +053063 if d.category in ["Valuation", "Valuation and Total"]]
64 if tax_for_valuation:
Rushabh Mehta2b089852013-12-19 18:27:48 +053065 webnotes.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 +053066
Nabin Haitd3b62502013-01-21 17:24:31 +053067 def set_total_in_words(self):
Nabin Hait5b4c2942013-01-22 11:12:02 +053068 from webnotes.utils import money_in_words
Nabin Haitd3b62502013-01-21 17:24:31 +053069 company_currency = get_company_currency(self.doc.company)
Nabin Hait5b4c2942013-01-22 11:12:02 +053070 if self.meta.get_field("in_words"):
71 self.doc.in_words = money_in_words(self.doc.grand_total, company_currency)
72 if self.meta.get_field("in_words_import"):
73 self.doc.in_words_import = money_in_words(self.doc.grand_total_import,
Anand Doshi613cb6a2013-02-06 17:33:46 +053074 self.doc.currency)
75
76 def calculate_taxes_and_totals(self):
Akhilesh Darjee4f721562014-01-29 16:31:38 +053077 self.other_fname = "other_charges"
Anand Doshi3543f302013-05-24 19:25:01 +053078 super(BuyingController, self).calculate_taxes_and_totals()
Anand Doshi923d41d2013-05-28 17:23:36 +053079 self.calculate_total_advance("Purchase Invoice", "advance_allocation_details")
Anand Doshi8595fcf2013-02-08 19:28:14 +053080
Anand Doshi8595fcf2013-02-08 19:28:14 +053081 def calculate_item_values(self):
Anand Doshib8b1e582013-05-06 12:52:46 +053082 for item in self.item_doclist:
Anand Doshi39384d32013-05-11 19:39:53 +053083 self.round_floats_in(item)
Anand Doshi8595fcf2013-02-08 19:28:14 +053084
Nabin Haita7f757a2014-02-10 17:54:04 +053085 if item.discount_percentage == 100.0:
Nabin Hait7979f7e2014-02-10 18:26:49 +053086 item.rate = 0.0
87 elif not item.rate:
88 item.rate = flt(item.price_list_rate * (1.0 - (item.discount_percentage / 100.0)),
89 self.precision("rate", item))
Anand Doshia1d4b782013-02-26 18:09:47 +053090
Nabin Hait1eb56012014-02-10 19:20:15 +053091 item.amount = flt(item.rate * item.qty,
92 self.precision("amount", item))
Anand Doshi3543f302013-05-24 19:25:01 +053093 item.item_tax_amount = 0.0;
Rushabh Mehta54047782013-12-26 11:07:46 +053094
Nabin Hait1eb56012014-02-10 19:20:15 +053095 self._set_in_company_currency(item, "amount", "base_amount")
Nabin Haita7f757a2014-02-10 17:54:04 +053096 self._set_in_company_currency(item, "price_list_rate", "base_price_list_rate")
Nabin Hait7979f7e2014-02-10 18:26:49 +053097 self._set_in_company_currency(item, "rate", "base_rate")
Rushabh Mehta54047782013-12-26 11:07:46 +053098
Anand Doshi5af812a2013-05-10 19:23:02 +053099
Anand Doshi8595fcf2013-02-08 19:28:14 +0530100 def calculate_net_total(self):
Anand Doshi3543f302013-05-24 19:25:01 +0530101 self.doc.net_total = self.doc.net_total_import = 0.0
Anand Doshi8595fcf2013-02-08 19:28:14 +0530102
103 for item in self.item_doclist:
Nabin Hait1eb56012014-02-10 19:20:15 +0530104 self.doc.net_total += item.base_amount
105 self.doc.net_total_import += item.amount
Anand Doshi8595fcf2013-02-08 19:28:14 +0530106
Anand Doshi3543f302013-05-24 19:25:01 +0530107 self.round_floats_in(self.doc, ["net_total", "net_total_import"])
Anand Doshi8595fcf2013-02-08 19:28:14 +0530108
109 def calculate_totals(self):
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530110 self.doc.grand_total = flt(self.tax_doclist[-1].total if self.tax_doclist
111 else self.doc.net_total, self.precision("grand_total"))
Anand Doshi3543f302013-05-24 19:25:01 +0530112 self.doc.grand_total_import = flt(self.doc.grand_total / self.doc.conversion_rate,
113 self.precision("grand_total_import"))
Anand Doshi8595fcf2013-02-08 19:28:14 +0530114
Anand Doshi3543f302013-05-24 19:25:01 +0530115 self.doc.total_tax = flt(self.doc.grand_total - self.doc.net_total,
Anand Doshi5af812a2013-05-10 19:23:02 +0530116 self.precision("total_tax"))
Anand Doshi8595fcf2013-02-08 19:28:14 +0530117
118 if self.meta.get_field("rounded_total"):
Anand Doshi2e31e092013-08-19 19:58:23 +0530119 self.doc.rounded_total = _round(self.doc.grand_total)
Anand Doshi8595fcf2013-02-08 19:28:14 +0530120
121 if self.meta.get_field("rounded_total_import"):
Anand Doshi2e31e092013-08-19 19:58:23 +0530122 self.doc.rounded_total_import = _round(self.doc.grand_total_import)
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530123
124 if self.meta.get_field("other_charges_added"):
125 self.doc.other_charges_added = flt(sum([flt(d.tax_amount) for d in self.tax_doclist
126 if d.add_deduct_tax=="Add" and d.category in ["Valuation and Total", "Total"]]),
127 self.precision("other_charges_added"))
128
129 if self.meta.get_field("other_charges_deducted"):
130 self.doc.other_charges_deducted = flt(sum([flt(d.tax_amount) for d in self.tax_doclist
131 if d.add_deduct_tax=="Deduct" and d.category in ["Valuation and Total", "Total"]]),
132 self.precision("other_charges_deducted"))
133
134 if self.meta.get_field("other_charges_added_import"):
135 self.doc.other_charges_added_import = flt(self.doc.other_charges_added /
136 self.doc.conversion_rate, self.precision("other_charges_added_import"))
137
138 if self.meta.get_field("other_charges_deducted_import"):
139 self.doc.other_charges_deducted_import = flt(self.doc.other_charges_deducted /
140 self.doc.conversion_rate, self.precision("other_charges_deducted_import"))
Anand Doshi8595fcf2013-02-08 19:28:14 +0530141
142 def calculate_outstanding_amount(self):
Anand Doshic99853c2013-05-14 13:35:09 +0530143 if self.doc.doctype == "Purchase Invoice" and self.doc.docstatus < 2:
Anand Doshi8595fcf2013-02-08 19:28:14 +0530144 self.doc.total_advance = flt(self.doc.total_advance,
Anand Doshi5af812a2013-05-10 19:23:02 +0530145 self.precision("total_advance"))
Anand Doshi9d794fc2013-02-27 13:34:22 +0530146 self.doc.total_amount_to_pay = flt(self.doc.grand_total - flt(self.doc.write_off_amount,
Anand Doshi5af812a2013-05-10 19:23:02 +0530147 self.precision("write_off_amount")), self.precision("total_amount_to_pay"))
Anand Doshi9d794fc2013-02-27 13:34:22 +0530148 self.doc.outstanding_amount = flt(self.doc.total_amount_to_pay - self.doc.total_advance,
Anand Doshi5af812a2013-05-10 19:23:02 +0530149 self.precision("outstanding_amount"))
Anand Doshi8595fcf2013-02-08 19:28:14 +0530150
151 def _cleanup(self):
Anand Doshi3543f302013-05-24 19:25:01 +0530152 super(BuyingController, self)._cleanup()
Anand Doshib3928782013-06-05 17:46:20 +0530153
Anand Doshi923d41d2013-05-28 17:23:36 +0530154 if not self.meta.get_field("item_tax_amount", parentfield=self.fname):
155 for item in self.item_doclist:
156 del item.fields["item_tax_amount"]
Nabin Hait5c6d13a2014-01-20 17:18:16 +0530157
158 if not self.meta.get_field("tax_amount_after_discount_amount",
159 parentfield=self.other_fname):
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530160 for tax in self.tax_doclist:
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530161 del tax.fields["tax_amount_after_discount_amount"]
Anand Doshi923d41d2013-05-28 17:23:36 +0530162
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530163 # update valuation rate
164 def update_valuation_rate(self, parentfield):
Anand Doshi8595fcf2013-02-08 19:28:14 +0530165 """
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 """
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530171 stock_items = self.get_stock_items()
172
173 stock_items_qty, stock_items_amount = 0, 0
174 last_stock_item_idx = 1
175 for d in self.doclist.get({"parentfield": parentfield}):
176 if d.item_code and d.item_code in stock_items:
177 stock_items_qty += flt(d.qty)
Nabin Hait1eb56012014-02-10 19:20:15 +0530178 stock_items_amount += flt(d.base_amount)
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530179 last_stock_item_idx = d.idx
Anand Doshi39384d32013-05-11 19:39:53 +0530180
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530181 total_valuation_amount = sum([flt(d.tax_amount) for d in
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530182 self.doclist.get({"parentfield": "other_charges"})
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530183 if d.category in ["Valuation", "Valuation and Total"]])
184
185
186 valuation_amount_adjustment = total_valuation_amount
187 for i, item in enumerate(self.doclist.get({"parentfield": parentfield})):
188 if item.item_code and item.qty and item.item_code in stock_items:
Nabin Hait1eb56012014-02-10 19:20:15 +0530189 item_proportion = flt(item.base_amount) / stock_items_amount if stock_items_amount \
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530190 else flt(item.qty) / stock_items_qty
191
192 if i == (last_stock_item_idx - 1):
193 item.item_tax_amount = flt(valuation_amount_adjustment,
194 self.precision("item_tax_amount", item))
195 else:
196 item.item_tax_amount = flt(item_proportion * total_valuation_amount,
197 self.precision("item_tax_amount", item))
198 valuation_amount_adjustment -= item.item_tax_amount
199
Anand Doshi39384d32013-05-11 19:39:53 +0530200 self.round_floats_in(item)
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530201
202 item.conversion_factor = item.conversion_factor or flt(webnotes.conn.get_value(
203 "UOM Conversion Detail", {"parent": item.item_code, "uom": item.uom},
204 "conversion_factor")) or 1
Rushabh Mehta54047782013-12-26 11:07:46 +0530205 qty_in_stock_uom = flt(item.qty * item.conversion_factor)
Nabin Hait1eb56012014-02-10 19:20:15 +0530206 item.valuation_rate = ((item.base_amount + item.item_tax_amount + item.rm_supp_cost)
Rushabh Mehta54047782013-12-26 11:07:46 +0530207 / 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
Nabin Hait54d209f2013-03-01 18:51:10 +0530210
211 def validate_for_subcontracting(self):
212 if not self.doc.is_subcontracted and self.sub_contracted_items:
213 webnotes.msgprint(_("""Please enter whether %s is made for subcontracting or purchasing,
214 in 'Is Subcontracted' field""" % self.doc.doctype), raise_exception=1)
215
216 if self.doc.doctype == "Purchase Receipt" and self.doc.is_subcontracted=="Yes" \
217 and not self.doc.supplier_warehouse:
218 webnotes.msgprint(_("Supplier Warehouse mandatory subcontracted purchase receipt"),
219 raise_exception=1)
220
221 def update_raw_materials_supplied(self, raw_material_table):
222 self.doclist = self.doc.clear_table(self.doclist, raw_material_table)
223 if self.doc.is_subcontracted=="Yes":
224 for item in self.doclist.get({"parentfield": self.fname}):
225 if item.item_code in self.sub_contracted_items:
226 self.add_bom_items(item, raw_material_table)
227
228 def add_bom_items(self, d, raw_material_table):
229 bom_items = self.get_items_from_default_bom(d.item_code)
230 raw_materials_cost = 0
231 for item in bom_items:
232 required_qty = flt(item.qty_consumed_per_unit) * flt(d.qty) * flt(d.conversion_factor)
233 rm_doclist = {
234 "parentfield": raw_material_table,
235 "doctype": self.doc.doctype + " Item Supplied",
236 "reference_name": d.name,
237 "bom_detail_no": item.name,
238 "main_item_code": d.item_code,
239 "rm_item_code": item.item_code,
240 "stock_uom": item.stock_uom,
241 "required_qty": required_qty,
242 "conversion_factor": d.conversion_factor,
243 "rate": item.rate,
244 "amount": required_qty * flt(item.rate)
245 }
246 if self.doc.doctype == "Purchase Receipt":
247 rm_doclist.update({
248 "consumed_qty": required_qty,
249 "description": item.description,
250 })
251
252 self.doclist.append(rm_doclist)
253
254 raw_materials_cost += required_qty * flt(item.rate)
255
256 if self.doc.doctype == "Purchase Receipt":
257 d.rm_supp_cost = raw_materials_cost
258
259 def get_items_from_default_bom(self, item_code):
260 # print webnotes.conn.sql("""select name from `tabBOM` where item = '_Test FG Item'""")
261 bom_items = webnotes.conn.sql("""select t2.item_code, t2.qty_consumed_per_unit,
262 t2.rate, t2.stock_uom, t2.name, t2.description
263 from `tabBOM` t1, `tabBOM Item` t2
264 where t2.parent = t1.name and t1.item = %s and t1.is_default = 1
265 and t1.docstatus = 1 and t1.is_active = 1""", item_code, as_dict=1)
266 if not bom_items:
267 msgprint(_("No default BOM exists for item: ") + item_code, raise_exception=1)
268
269 return bom_items
270
Nabin Hait5418d712013-02-27 18:11:17 +0530271 @property
272 def sub_contracted_items(self):
273 if not hasattr(self, "_sub_contracted_items"):
Nabin Haitebd51442013-04-23 15:36:26 +0530274 self._sub_contracted_items = []
Nabin Hait5418d712013-02-27 18:11:17 +0530275 item_codes = list(set(item.item_code for item in
276 self.doclist.get({"parentfield": self.fname})))
Nabin Haitebd51442013-04-23 15:36:26 +0530277 if item_codes:
278 self._sub_contracted_items = [r[0] for r in webnotes.conn.sql("""select name
279 from `tabItem` where name in (%s) and is_sub_contracted_item='Yes'""" % \
280 (", ".join((["%s"]*len(item_codes))),), item_codes)]
Nabin Hait5418d712013-02-27 18:11:17 +0530281
282 return self._sub_contracted_items
283
284 @property
285 def purchase_items(self):
286 if not hasattr(self, "_purchase_items"):
Nabin Haitebd51442013-04-23 15:36:26 +0530287 self._purchase_items = []
Nabin Hait5418d712013-02-27 18:11:17 +0530288 item_codes = list(set(item.item_code for item in
289 self.doclist.get({"parentfield": self.fname})))
Nabin Haitebd51442013-04-23 15:36:26 +0530290 if item_codes:
291 self._purchase_items = [r[0] for r in webnotes.conn.sql("""select name
292 from `tabItem` where name in (%s) and is_purchase_item='Yes'""" % \
293 (", ".join((["%s"]*len(item_codes))),), item_codes)]
Nabin Hait5418d712013-02-27 18:11:17 +0530294
Anand Doshi55d9a622013-02-27 18:14:28 +0530295 return self._purchase_items
Nabin Hait5566bca2013-10-18 17:00:53 +0530296
297
298 def is_item_table_empty(self):
299 if not len(self.doclist.get({"parentfield": self.fname})):
300 webnotes.throw(_("Item table can not be blank"))