blob: 35b9d2527924e469e722dfeb66bfa1ce9414478b [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
Anand Doshi756dca72013-01-15 18:39:21 +05308
9from buying.utils import get_item_details
Anand Doshif8f0c0d2013-01-17 20:20:56 +053010from setup.utils import get_company_currency
Anand Doshi756dca72013-01-15 18:39:21 +053011
Nabin Hait89a94d82013-03-19 12:01:46 +053012from 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
35 if self.doc.supplier and not (self.doc.contact_person and self.doc.supplier_address):
Anand Doshif2045fb2013-07-25 17:46:02 +053036 for fieldname, val in self.get_supplier_defaults().items():
Anand Doshi3543f302013-05-24 19:25:01 +053037 if not self.doc.fields.get(fieldname) and self.meta.get_field(fieldname):
38 self.doc.fields[fieldname] = val
Rushabh Mehta886c9ef2013-07-08 12:08:06 +053039
Anand Doshi3543f302013-05-24 19:25:01 +053040 self.set_missing_item_details(get_item_details)
Rushabh Mehtae89c5332013-08-29 15:32:46 +053041 if self.doc.fields.get("__islocal"):
42 self.set_taxes("purchase_tax_details", "purchase_other_charges")
Rushabh Mehta436467a2013-07-08 12:37:59 +053043
Rushabh Mehta0b995402013-08-09 15:29:59 +053044 def set_supplier_from_item_default(self):
45 if self.meta.get_field("supplier") and not self.doc.supplier:
46 for d in self.doclist.get({"doctype": self.tname}):
47 supplier = webnotes.conn.get_value("Item", d.item_code, "default_supplier")
48 if supplier:
49 self.doc.supplier = supplier
50 break
Anand Doshi373680b2013-10-10 16:04:40 +053051
52 def validate_warehouse(self):
53 from stock.utils import validate_warehouse_user, validate_warehouse_company
54
55 warehouses = list(set([d.warehouse for d in
56 self.doclist.get({"doctype": self.tname}) if d.warehouse]))
57
58 for w in warehouses:
59 validate_warehouse_user(w)
60 validate_warehouse_company(w, self.doc.company)
Rushabh Mehta0b995402013-08-09 15:29:59 +053061
Anand Doshi3543f302013-05-24 19:25:01 +053062 def get_purchase_tax_details(self):
63 self.doclist = self.doc.clear_table(self.doclist, "purchase_tax_details")
Anand Doshi99100a42013-07-04 17:13:53 +053064 self.set_taxes("purchase_tax_details", "purchase_other_charges")
Rushabh Mehtac7115982013-05-02 14:22:40 +053065
Nabin Hait205f7ce2013-04-26 13:35:06 +053066 def validate_stock_or_nonstock_items(self):
Nabin Haita36adbd2013-08-02 14:50:12 +053067 if not self.get_stock_items():
Nabin Hait205f7ce2013-04-26 13:35:06 +053068 tax_for_valuation = [d.account_head for d in
69 self.doclist.get({"parentfield": "purchase_tax_details"})
70 if d.category in ["Valuation", "Valuation and Total"]]
71 if tax_for_valuation:
72 webnotes.msgprint(_("""Tax Category can not be 'Valuation' or 'Valuation and Total'
73 as all items are non-stock items"""), raise_exception=1)
74
Nabin Haitd3b62502013-01-21 17:24:31 +053075 def set_total_in_words(self):
Nabin Hait5b4c2942013-01-22 11:12:02 +053076 from webnotes.utils import money_in_words
Nabin Haitd3b62502013-01-21 17:24:31 +053077 company_currency = get_company_currency(self.doc.company)
Nabin Hait5b4c2942013-01-22 11:12:02 +053078 if self.meta.get_field("in_words"):
79 self.doc.in_words = money_in_words(self.doc.grand_total, company_currency)
80 if self.meta.get_field("in_words_import"):
81 self.doc.in_words_import = money_in_words(self.doc.grand_total_import,
Anand Doshi613cb6a2013-02-06 17:33:46 +053082 self.doc.currency)
83
84 def calculate_taxes_and_totals(self):
Anand Doshi3543f302013-05-24 19:25:01 +053085 self.other_fname = "purchase_tax_details"
86 super(BuyingController, self).calculate_taxes_and_totals()
Anand Doshi923d41d2013-05-28 17:23:36 +053087 self.calculate_total_advance("Purchase Invoice", "advance_allocation_details")
Anand Doshi8595fcf2013-02-08 19:28:14 +053088
Anand Doshi8595fcf2013-02-08 19:28:14 +053089 def calculate_item_values(self):
Anand Doshib8b1e582013-05-06 12:52:46 +053090 # hack! - cleaned up in _cleanup()
91 if self.doc.doctype != "Purchase Invoice":
Anand Doshi21f4ea32013-05-10 18:08:32 +053092 df = self.meta.get_field("purchase_rate", parentfield=self.fname)
93 df.fieldname = "rate"
Anand Doshia1d4b782013-02-26 18:09:47 +053094
Anand Doshib8b1e582013-05-06 12:52:46 +053095 for item in self.item_doclist:
Anand Doshia1d4b782013-02-26 18:09:47 +053096 # hack! - cleaned up in _cleanup()
97 if self.doc.doctype != "Purchase Invoice":
98 item.rate = item.purchase_rate
Anand Doshia1d4b782013-02-26 18:09:47 +053099
Anand Doshi39384d32013-05-11 19:39:53 +0530100 self.round_floats_in(item)
Anand Doshi8595fcf2013-02-08 19:28:14 +0530101
Anand Doshi3543f302013-05-24 19:25:01 +0530102 if item.discount_rate == 100.0:
103 item.import_rate = 0.0
Anand Doshi47e14a92013-08-13 13:01:12 +0530104 elif not item.import_rate:
Anand Doshi3543f302013-05-24 19:25:01 +0530105 item.import_rate = flt(item.import_ref_rate * (1.0 - (item.discount_rate / 100.0)),
106 self.precision("import_rate", item))
Anand Doshia1d4b782013-02-26 18:09:47 +0530107
Anand Doshi8595fcf2013-02-08 19:28:14 +0530108 item.import_amount = flt(item.import_rate * item.qty,
Anand Doshi39384d32013-05-11 19:39:53 +0530109 self.precision("import_amount", item))
Anand Doshi3543f302013-05-24 19:25:01 +0530110 item.item_tax_amount = 0.0;
Nabin Hait86145322013-12-23 12:14:45 +0530111
112 self._set_in_company_currency(item, "import_amount", "amount")
Anand Doshi3543f302013-05-24 19:25:01 +0530113 self._set_in_company_currency(item, "import_ref_rate", "purchase_ref_rate")
114 self._set_in_company_currency(item, "import_rate", "rate")
Nabin Hait86145322013-12-23 12:14:45 +0530115
Anand Doshi5af812a2013-05-10 19:23:02 +0530116
Anand Doshi8595fcf2013-02-08 19:28:14 +0530117 def calculate_net_total(self):
Anand Doshi3543f302013-05-24 19:25:01 +0530118 self.doc.net_total = self.doc.net_total_import = 0.0
Anand Doshi8595fcf2013-02-08 19:28:14 +0530119
120 for item in self.item_doclist:
121 self.doc.net_total += item.amount
122 self.doc.net_total_import += item.import_amount
123
Anand Doshi3543f302013-05-24 19:25:01 +0530124 self.round_floats_in(self.doc, ["net_total", "net_total_import"])
Anand Doshi8595fcf2013-02-08 19:28:14 +0530125
126 def calculate_totals(self):
Anand Doshi3543f302013-05-24 19:25:01 +0530127 self.doc.grand_total = flt(self.tax_doclist and \
128 self.tax_doclist[-1].total or self.doc.net_total, self.precision("grand_total"))
129 self.doc.grand_total_import = flt(self.doc.grand_total / self.doc.conversion_rate,
130 self.precision("grand_total_import"))
Anand Doshi8595fcf2013-02-08 19:28:14 +0530131
Anand Doshi3543f302013-05-24 19:25:01 +0530132 self.doc.total_tax = flt(self.doc.grand_total - self.doc.net_total,
Anand Doshi5af812a2013-05-10 19:23:02 +0530133 self.precision("total_tax"))
Anand Doshi8595fcf2013-02-08 19:28:14 +0530134
135 if self.meta.get_field("rounded_total"):
Anand Doshi2e31e092013-08-19 19:58:23 +0530136 self.doc.rounded_total = _round(self.doc.grand_total)
Anand Doshi8595fcf2013-02-08 19:28:14 +0530137
138 if self.meta.get_field("rounded_total_import"):
Anand Doshi2e31e092013-08-19 19:58:23 +0530139 self.doc.rounded_total_import = _round(self.doc.grand_total_import)
Anand Doshi8595fcf2013-02-08 19:28:14 +0530140
141 def calculate_outstanding_amount(self):
Anand Doshic99853c2013-05-14 13:35:09 +0530142 if self.doc.doctype == "Purchase Invoice" and self.doc.docstatus < 2:
Anand Doshi8595fcf2013-02-08 19:28:14 +0530143 self.doc.total_advance = flt(self.doc.total_advance,
Anand Doshi5af812a2013-05-10 19:23:02 +0530144 self.precision("total_advance"))
Anand Doshi9d794fc2013-02-27 13:34:22 +0530145 self.doc.total_amount_to_pay = flt(self.doc.grand_total - flt(self.doc.write_off_amount,
Anand Doshi5af812a2013-05-10 19:23:02 +0530146 self.precision("write_off_amount")), self.precision("total_amount_to_pay"))
Anand Doshi9d794fc2013-02-27 13:34:22 +0530147 self.doc.outstanding_amount = flt(self.doc.total_amount_to_pay - self.doc.total_advance,
Anand Doshi5af812a2013-05-10 19:23:02 +0530148 self.precision("outstanding_amount"))
Anand Doshi8595fcf2013-02-08 19:28:14 +0530149
150 def _cleanup(self):
Anand Doshi3543f302013-05-24 19:25:01 +0530151 super(BuyingController, self)._cleanup()
152
153 # except in purchase invoice, rate field is purchase_rate
Anand Doshi5af812a2013-05-10 19:23:02 +0530154 # reset fieldname of rate
155 if self.doc.doctype != "Purchase Invoice":
156 df = self.meta.get_field("rate", parentfield=self.fname)
157 df.fieldname = "purchase_rate"
Anand Doshi8595fcf2013-02-08 19:28:14 +0530158
Anand Doshi3543f302013-05-24 19:25:01 +0530159 for item in self.item_doclist:
160 item.purchase_rate = item.rate
161 del item.fields["rate"]
Anand Doshib3928782013-06-05 17:46:20 +0530162
Anand Doshi923d41d2013-05-28 17:23:36 +0530163 if not self.meta.get_field("item_tax_amount", parentfield=self.fname):
164 for item in self.item_doclist:
165 del item.fields["item_tax_amount"]
166
Anand Doshi8595fcf2013-02-08 19:28:14 +0530167 def set_item_tax_amount(self, item, tax, current_tax_amount):
168 """
169 item_tax_amount is the total tax amount applied on that item
170 stored for valuation
171
172 TODO: rename item_tax_amount to valuation_tax_amount
173 """
Anand Doshib3928782013-06-05 17:46:20 +0530174 if tax.category in ["Valuation", "Valuation and Total"] and \
175 self.meta.get_field("item_tax_amount", parentfield=self.fname):
176 item.item_tax_amount += flt(current_tax_amount, self.precision("item_tax_amount", item))
Anand Doshi4a7248e2013-02-27 18:10:30 +0530177
178 # update valuation rate
179 def update_valuation_rate(self, parentfield):
Anand Doshi5af812a2013-05-10 19:23:02 +0530180 for item in self.doclist.get({"parentfield": parentfield}):
181 item.conversion_factor = item.conversion_factor or flt(webnotes.conn.get_value(
182 "UOM Conversion Detail", {"parent": item.item_code, "uom": item.uom},
Nabin Hait0fc24542013-03-25 11:06:00 +0530183 "conversion_factor")) or 1
Anand Doshi39384d32013-05-11 19:39:53 +0530184
Anand Doshi5af812a2013-05-10 19:23:02 +0530185 if item.item_code and item.qty:
Anand Doshi39384d32013-05-11 19:39:53 +0530186 self.round_floats_in(item)
Nabin Hait86145322013-12-23 12:14:45 +0530187
Anand Doshi1bfec3a2013-02-28 19:17:46 +0530188 # if no item code, which is sometimes the case in purchase invoice,
189 # then it is not possible to track valuation against it
Nabin Hait86145322013-12-23 12:14:45 +0530190 qty_in_stock_uom = flt(item.qty * item.conversion_factor)
191 item.valuation_rate = ((item.amount + item.item_tax_amount + item.rm_supp_cost)
192 / qty_in_stock_uom)
Anand Doshi4a7248e2013-02-27 18:10:30 +0530193 else:
Anand Doshi5af812a2013-05-10 19:23:02 +0530194 item.valuation_rate = 0.0
Nabin Hait54d209f2013-03-01 18:51:10 +0530195
196 def validate_for_subcontracting(self):
197 if not self.doc.is_subcontracted and self.sub_contracted_items:
198 webnotes.msgprint(_("""Please enter whether %s is made for subcontracting or purchasing,
199 in 'Is Subcontracted' field""" % self.doc.doctype), raise_exception=1)
200
201 if self.doc.doctype == "Purchase Receipt" and self.doc.is_subcontracted=="Yes" \
202 and not self.doc.supplier_warehouse:
203 webnotes.msgprint(_("Supplier Warehouse mandatory subcontracted purchase receipt"),
204 raise_exception=1)
205
206 def update_raw_materials_supplied(self, raw_material_table):
207 self.doclist = self.doc.clear_table(self.doclist, raw_material_table)
208 if self.doc.is_subcontracted=="Yes":
209 for item in self.doclist.get({"parentfield": self.fname}):
210 if item.item_code in self.sub_contracted_items:
211 self.add_bom_items(item, raw_material_table)
212
213 def add_bom_items(self, d, raw_material_table):
214 bom_items = self.get_items_from_default_bom(d.item_code)
215 raw_materials_cost = 0
216 for item in bom_items:
217 required_qty = flt(item.qty_consumed_per_unit) * flt(d.qty) * flt(d.conversion_factor)
218 rm_doclist = {
219 "parentfield": raw_material_table,
220 "doctype": self.doc.doctype + " Item Supplied",
221 "reference_name": d.name,
222 "bom_detail_no": item.name,
223 "main_item_code": d.item_code,
224 "rm_item_code": item.item_code,
225 "stock_uom": item.stock_uom,
226 "required_qty": required_qty,
227 "conversion_factor": d.conversion_factor,
228 "rate": item.rate,
229 "amount": required_qty * flt(item.rate)
230 }
231 if self.doc.doctype == "Purchase Receipt":
232 rm_doclist.update({
233 "consumed_qty": required_qty,
234 "description": item.description,
235 })
236
237 self.doclist.append(rm_doclist)
238
239 raw_materials_cost += required_qty * flt(item.rate)
240
241 if self.doc.doctype == "Purchase Receipt":
242 d.rm_supp_cost = raw_materials_cost
243
244 def get_items_from_default_bom(self, item_code):
245 # print webnotes.conn.sql("""select name from `tabBOM` where item = '_Test FG Item'""")
246 bom_items = webnotes.conn.sql("""select t2.item_code, t2.qty_consumed_per_unit,
247 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
261 self.doclist.get({"parentfield": self.fname})))
Nabin Haitebd51442013-04-23 15:36:26 +0530262 if item_codes:
263 self._sub_contracted_items = [r[0] for r in webnotes.conn.sql("""select name
264 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
274 self.doclist.get({"parentfield": self.fname})))
Nabin Haitebd51442013-04-23 15:36:26 +0530275 if item_codes:
276 self._purchase_items = [r[0] for r in webnotes.conn.sql("""select name
277 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):
284 if not len(self.doclist.get({"parentfield": self.fname})):
285 webnotes.throw(_("Item table can not be blank"))