blob: c5f5d08c47fdd5a8dc4a1e8151c19fe46a5b25b2 [file] [log] [blame]
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
2# License: GNU General Public License v3. See license.txt
Anand Doshi756dca72013-01-15 18:39:21 +05303
4from __future__ import unicode_literals
5import webnotes
Anand Doshif8f0c0d2013-01-17 20:20:56 +05306from webnotes import _, msgprint
Anand Doshi3543f302013-05-24 19:25:01 +05307from webnotes.utils import flt
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
Rushabh Mehtaecb36f22013-05-02 11:34:37 +053014class WrongWarehouseCompany(Exception): pass
15
Nabin Hait89a94d82013-03-19 12:01:46 +053016class BuyingController(StockController):
Anand Doshi3543f302013-05-24 19:25:01 +053017 def onload_post_render(self):
Anand Doshi3543f302013-05-24 19:25:01 +053018 # contact, address, item details
19 self.set_missing_values()
Anand Doshi99100a42013-07-04 17:13:53 +053020 self.set_taxes("purchase_tax_details", "purchase_other_charges")
Anand Doshi3543f302013-05-24 19:25:01 +053021
Saurabh6f753182013-03-20 12:55:28 +053022 def validate(self):
23 super(BuyingController, self).validate()
Rushabh Mehta886c9ef2013-07-08 12:08:06 +053024 if self.doc.supplier and not self.doc.supplier_name:
25 self.doc.supplier_name = webnotes.conn.get_value("Supplier",
26 self.doc.supplier, "supplier_name")
Nabin Hait205f7ce2013-04-26 13:35:06 +053027 self.validate_stock_or_nonstock_items()
Rushabh Mehtaecb36f22013-05-02 11:34:37 +053028 self.validate_warehouse_belongs_to_company()
Anand Doshi1dde46a2013-05-15 21:15:57 +053029
Anand Doshi3543f302013-05-24 19:25:01 +053030 def set_missing_values(self, for_validate=False):
Anand Doshiabc10032013-06-14 17:44:03 +053031 super(BuyingController, self).set_missing_values(for_validate)
32
33 self.set_price_list_currency("Buying")
34
Anand Doshi3543f302013-05-24 19:25:01 +053035 # set contact and address details for supplier, if they are not mentioned
36 if self.doc.supplier and not (self.doc.contact_person and self.doc.supplier_address):
Anand Doshif2045fb2013-07-25 17:46:02 +053037 for fieldname, val in self.get_supplier_defaults().items():
Anand Doshi3543f302013-05-24 19:25:01 +053038 if not self.doc.fields.get(fieldname) and self.meta.get_field(fieldname):
39 self.doc.fields[fieldname] = val
Rushabh Mehta886c9ef2013-07-08 12:08:06 +053040
Anand Doshi3543f302013-05-24 19:25:01 +053041 self.set_missing_item_details(get_item_details)
Rushabh Mehta436467a2013-07-08 12:37:59 +053042
Anand Doshi3543f302013-05-24 19:25:01 +053043 def get_purchase_tax_details(self):
44 self.doclist = self.doc.clear_table(self.doclist, "purchase_tax_details")
Anand Doshi99100a42013-07-04 17:13:53 +053045 self.set_taxes("purchase_tax_details", "purchase_other_charges")
Anand Doshibf3e54a2013-06-05 14:11:32 +053046
Rushabh Mehtaecb36f22013-05-02 11:34:37 +053047 def validate_warehouse_belongs_to_company(self):
Rushabh Mehtac7115982013-05-02 14:22:40 +053048 for warehouse, company in webnotes.conn.get_values("Warehouse",
49 self.doclist.get_distinct_values("warehouse"), "company").items():
50 if company and company != self.doc.company:
Anand Doshieb41ffd2013-05-02 18:10:19 +053051 webnotes.msgprint(_("Company mismatch for Warehouse") + (": %s" % (warehouse,)),
Rushabh Mehtaecb36f22013-05-02 11:34:37 +053052 raise_exception=WrongWarehouseCompany)
Rushabh Mehtac7115982013-05-02 14:22:40 +053053
Nabin Hait205f7ce2013-04-26 13:35:06 +053054 def validate_stock_or_nonstock_items(self):
Nabin Haita36adbd2013-08-02 14:50:12 +053055 if not self.get_stock_items():
Nabin Hait205f7ce2013-04-26 13:35:06 +053056 tax_for_valuation = [d.account_head for d in
57 self.doclist.get({"parentfield": "purchase_tax_details"})
58 if d.category in ["Valuation", "Valuation and Total"]]
59 if tax_for_valuation:
60 webnotes.msgprint(_("""Tax Category can not be 'Valuation' or 'Valuation and Total'
61 as all items are non-stock items"""), raise_exception=1)
62
Nabin Haitd3b62502013-01-21 17:24:31 +053063 def set_total_in_words(self):
Nabin Hait5b4c2942013-01-22 11:12:02 +053064 from webnotes.utils import money_in_words
Nabin Haitd3b62502013-01-21 17:24:31 +053065 company_currency = get_company_currency(self.doc.company)
Nabin Hait5b4c2942013-01-22 11:12:02 +053066 if self.meta.get_field("in_words"):
67 self.doc.in_words = money_in_words(self.doc.grand_total, company_currency)
68 if self.meta.get_field("in_words_import"):
69 self.doc.in_words_import = money_in_words(self.doc.grand_total_import,
Anand Doshi613cb6a2013-02-06 17:33:46 +053070 self.doc.currency)
71
72 def calculate_taxes_and_totals(self):
Anand Doshi3543f302013-05-24 19:25:01 +053073 self.other_fname = "purchase_tax_details"
74 super(BuyingController, self).calculate_taxes_and_totals()
Anand Doshi923d41d2013-05-28 17:23:36 +053075 self.calculate_total_advance("Purchase Invoice", "advance_allocation_details")
Anand Doshi8595fcf2013-02-08 19:28:14 +053076
Anand Doshi8595fcf2013-02-08 19:28:14 +053077 def calculate_item_values(self):
Anand Doshib8b1e582013-05-06 12:52:46 +053078 # hack! - cleaned up in _cleanup()
79 if self.doc.doctype != "Purchase Invoice":
Anand Doshi21f4ea32013-05-10 18:08:32 +053080 df = self.meta.get_field("purchase_rate", parentfield=self.fname)
81 df.fieldname = "rate"
Anand Doshia1d4b782013-02-26 18:09:47 +053082
Anand Doshib8b1e582013-05-06 12:52:46 +053083 for item in self.item_doclist:
Anand Doshia1d4b782013-02-26 18:09:47 +053084 # hack! - cleaned up in _cleanup()
85 if self.doc.doctype != "Purchase Invoice":
86 item.rate = item.purchase_rate
Anand Doshia1d4b782013-02-26 18:09:47 +053087
Anand Doshi39384d32013-05-11 19:39:53 +053088 self.round_floats_in(item)
Anand Doshi8595fcf2013-02-08 19:28:14 +053089
Anand Doshi3543f302013-05-24 19:25:01 +053090 if item.discount_rate == 100.0:
91 item.import_rate = 0.0
92 elif item.import_ref_rate:
93 item.import_rate = flt(item.import_ref_rate * (1.0 - (item.discount_rate / 100.0)),
94 self.precision("import_rate", item))
Anand Doshia1d4b782013-02-26 18:09:47 +053095
Anand Doshi8595fcf2013-02-08 19:28:14 +053096 item.import_amount = flt(item.import_rate * item.qty,
Anand Doshi39384d32013-05-11 19:39:53 +053097 self.precision("import_amount", item))
Anand Doshi3543f302013-05-24 19:25:01 +053098 item.item_tax_amount = 0.0;
Anand Doshia1d4b782013-02-26 18:09:47 +053099
Anand Doshi3543f302013-05-24 19:25:01 +0530100 self._set_in_company_currency(item, "import_ref_rate", "purchase_ref_rate")
101 self._set_in_company_currency(item, "import_rate", "rate")
102 self._set_in_company_currency(item, "import_amount", "amount")
Anand Doshi5af812a2013-05-10 19:23:02 +0530103
Anand Doshi8595fcf2013-02-08 19:28:14 +0530104 def calculate_net_total(self):
Anand Doshi3543f302013-05-24 19:25:01 +0530105 self.doc.net_total = self.doc.net_total_import = 0.0
Anand Doshi8595fcf2013-02-08 19:28:14 +0530106
107 for item in self.item_doclist:
108 self.doc.net_total += item.amount
109 self.doc.net_total_import += item.import_amount
110
Anand Doshi3543f302013-05-24 19:25:01 +0530111 self.round_floats_in(self.doc, ["net_total", "net_total_import"])
Anand Doshi8595fcf2013-02-08 19:28:14 +0530112
113 def calculate_totals(self):
Anand Doshi3543f302013-05-24 19:25:01 +0530114 self.doc.grand_total = flt(self.tax_doclist and \
115 self.tax_doclist[-1].total or self.doc.net_total, self.precision("grand_total"))
116 self.doc.grand_total_import = flt(self.doc.grand_total / self.doc.conversion_rate,
117 self.precision("grand_total_import"))
Anand Doshi8595fcf2013-02-08 19:28:14 +0530118
Anand Doshi3543f302013-05-24 19:25:01 +0530119 self.doc.total_tax = flt(self.doc.grand_total - self.doc.net_total,
Anand Doshi5af812a2013-05-10 19:23:02 +0530120 self.precision("total_tax"))
Anand Doshi8595fcf2013-02-08 19:28:14 +0530121
122 if self.meta.get_field("rounded_total"):
123 self.doc.rounded_total = round(self.doc.grand_total)
124
125 if self.meta.get_field("rounded_total_import"):
126 self.doc.rounded_total_import = round(self.doc.grand_total_import)
127
128 def calculate_outstanding_amount(self):
Anand Doshic99853c2013-05-14 13:35:09 +0530129 if self.doc.doctype == "Purchase Invoice" and self.doc.docstatus < 2:
Anand Doshi8595fcf2013-02-08 19:28:14 +0530130 self.doc.total_advance = flt(self.doc.total_advance,
Anand Doshi5af812a2013-05-10 19:23:02 +0530131 self.precision("total_advance"))
Anand Doshi9d794fc2013-02-27 13:34:22 +0530132 self.doc.total_amount_to_pay = flt(self.doc.grand_total - flt(self.doc.write_off_amount,
Anand Doshi5af812a2013-05-10 19:23:02 +0530133 self.precision("write_off_amount")), self.precision("total_amount_to_pay"))
Anand Doshi9d794fc2013-02-27 13:34:22 +0530134 self.doc.outstanding_amount = flt(self.doc.total_amount_to_pay - self.doc.total_advance,
Anand Doshi5af812a2013-05-10 19:23:02 +0530135 self.precision("outstanding_amount"))
Anand Doshi8595fcf2013-02-08 19:28:14 +0530136
137 def _cleanup(self):
Anand Doshi3543f302013-05-24 19:25:01 +0530138 super(BuyingController, self)._cleanup()
139
140 # except in purchase invoice, rate field is purchase_rate
Anand Doshi5af812a2013-05-10 19:23:02 +0530141 # reset fieldname of rate
142 if self.doc.doctype != "Purchase Invoice":
143 df = self.meta.get_field("rate", parentfield=self.fname)
144 df.fieldname = "purchase_rate"
Anand Doshi8595fcf2013-02-08 19:28:14 +0530145
Anand Doshi3543f302013-05-24 19:25:01 +0530146 for item in self.item_doclist:
147 item.purchase_rate = item.rate
148 del item.fields["rate"]
Anand Doshib3928782013-06-05 17:46:20 +0530149
Anand Doshi923d41d2013-05-28 17:23:36 +0530150 if not self.meta.get_field("item_tax_amount", parentfield=self.fname):
151 for item in self.item_doclist:
152 del item.fields["item_tax_amount"]
153
Anand Doshi8595fcf2013-02-08 19:28:14 +0530154 def set_item_tax_amount(self, item, tax, current_tax_amount):
155 """
156 item_tax_amount is the total tax amount applied on that item
157 stored for valuation
158
159 TODO: rename item_tax_amount to valuation_tax_amount
160 """
Anand Doshib3928782013-06-05 17:46:20 +0530161 if tax.category in ["Valuation", "Valuation and Total"] and \
162 self.meta.get_field("item_tax_amount", parentfield=self.fname):
163 item.item_tax_amount += flt(current_tax_amount, self.precision("item_tax_amount", item))
Anand Doshi4a7248e2013-02-27 18:10:30 +0530164
165 # update valuation rate
166 def update_valuation_rate(self, parentfield):
Anand Doshi5af812a2013-05-10 19:23:02 +0530167 for item in self.doclist.get({"parentfield": parentfield}):
168 item.conversion_factor = item.conversion_factor or flt(webnotes.conn.get_value(
169 "UOM Conversion Detail", {"parent": item.item_code, "uom": item.uom},
Nabin Hait0fc24542013-03-25 11:06:00 +0530170 "conversion_factor")) or 1
Anand Doshi39384d32013-05-11 19:39:53 +0530171
Anand Doshi5af812a2013-05-10 19:23:02 +0530172 if item.item_code and item.qty:
Anand Doshi39384d32013-05-11 19:39:53 +0530173 self.round_floats_in(item)
174
175 purchase_rate = item.rate if self.doc.doctype == "Purchase Invoice" else item.purchase_rate
176
Anand Doshi1bfec3a2013-02-28 19:17:46 +0530177 # if no item code, which is sometimes the case in purchase invoice,
178 # then it is not possible to track valuation against it
Anand Doshi39384d32013-05-11 19:39:53 +0530179 item.valuation_rate = flt((purchase_rate +
180 (item.item_tax_amount + item.rm_supp_cost) / item.qty) / item.conversion_factor,
181 self.precision("valuation_rate", item))
Anand Doshi4a7248e2013-02-27 18:10:30 +0530182 else:
Anand Doshi5af812a2013-05-10 19:23:02 +0530183 item.valuation_rate = 0.0
Nabin Hait54d209f2013-03-01 18:51:10 +0530184
185 def validate_for_subcontracting(self):
186 if not self.doc.is_subcontracted and self.sub_contracted_items:
187 webnotes.msgprint(_("""Please enter whether %s is made for subcontracting or purchasing,
188 in 'Is Subcontracted' field""" % self.doc.doctype), raise_exception=1)
189
190 if self.doc.doctype == "Purchase Receipt" and self.doc.is_subcontracted=="Yes" \
191 and not self.doc.supplier_warehouse:
192 webnotes.msgprint(_("Supplier Warehouse mandatory subcontracted purchase receipt"),
193 raise_exception=1)
194
195 def update_raw_materials_supplied(self, raw_material_table):
196 self.doclist = self.doc.clear_table(self.doclist, raw_material_table)
197 if self.doc.is_subcontracted=="Yes":
198 for item in self.doclist.get({"parentfield": self.fname}):
199 if item.item_code in self.sub_contracted_items:
200 self.add_bom_items(item, raw_material_table)
201
202 def add_bom_items(self, d, raw_material_table):
203 bom_items = self.get_items_from_default_bom(d.item_code)
204 raw_materials_cost = 0
205 for item in bom_items:
206 required_qty = flt(item.qty_consumed_per_unit) * flt(d.qty) * flt(d.conversion_factor)
207 rm_doclist = {
208 "parentfield": raw_material_table,
209 "doctype": self.doc.doctype + " Item Supplied",
210 "reference_name": d.name,
211 "bom_detail_no": item.name,
212 "main_item_code": d.item_code,
213 "rm_item_code": item.item_code,
214 "stock_uom": item.stock_uom,
215 "required_qty": required_qty,
216 "conversion_factor": d.conversion_factor,
217 "rate": item.rate,
218 "amount": required_qty * flt(item.rate)
219 }
220 if self.doc.doctype == "Purchase Receipt":
221 rm_doclist.update({
222 "consumed_qty": required_qty,
223 "description": item.description,
224 })
225
226 self.doclist.append(rm_doclist)
227
228 raw_materials_cost += required_qty * flt(item.rate)
229
230 if self.doc.doctype == "Purchase Receipt":
231 d.rm_supp_cost = raw_materials_cost
232
233 def get_items_from_default_bom(self, item_code):
234 # print webnotes.conn.sql("""select name from `tabBOM` where item = '_Test FG Item'""")
235 bom_items = webnotes.conn.sql("""select t2.item_code, t2.qty_consumed_per_unit,
236 t2.rate, t2.stock_uom, t2.name, t2.description
237 from `tabBOM` t1, `tabBOM Item` t2
238 where t2.parent = t1.name and t1.item = %s and t1.is_default = 1
239 and t1.docstatus = 1 and t1.is_active = 1""", item_code, as_dict=1)
240 if not bom_items:
241 msgprint(_("No default BOM exists for item: ") + item_code, raise_exception=1)
242
243 return bom_items
244
Nabin Hait5418d712013-02-27 18:11:17 +0530245 @property
246 def sub_contracted_items(self):
247 if not hasattr(self, "_sub_contracted_items"):
Nabin Haitebd51442013-04-23 15:36:26 +0530248 self._sub_contracted_items = []
Nabin Hait5418d712013-02-27 18:11:17 +0530249 item_codes = list(set(item.item_code for item in
250 self.doclist.get({"parentfield": self.fname})))
Nabin Haitebd51442013-04-23 15:36:26 +0530251 if item_codes:
252 self._sub_contracted_items = [r[0] for r in webnotes.conn.sql("""select name
253 from `tabItem` where name in (%s) and is_sub_contracted_item='Yes'""" % \
254 (", ".join((["%s"]*len(item_codes))),), item_codes)]
Nabin Hait5418d712013-02-27 18:11:17 +0530255
256 return self._sub_contracted_items
257
258 @property
259 def purchase_items(self):
260 if not hasattr(self, "_purchase_items"):
Nabin Haitebd51442013-04-23 15:36:26 +0530261 self._purchase_items = []
Nabin Hait5418d712013-02-27 18:11:17 +0530262 item_codes = list(set(item.item_code for item in
263 self.doclist.get({"parentfield": self.fname})))
Nabin Haitebd51442013-04-23 15:36:26 +0530264 if item_codes:
265 self._purchase_items = [r[0] for r in webnotes.conn.sql("""select name
266 from `tabItem` where name in (%s) and is_purchase_item='Yes'""" % \
267 (", ".join((["%s"]*len(item_codes))),), item_codes)]
Nabin Hait5418d712013-02-27 18:11:17 +0530268
Anand Doshi55d9a622013-02-27 18:14:28 +0530269 return self._purchase_items