blob: 52dc3a885801386df5a4be6dff3ba3cf6b70e22b [file] [log] [blame]
Anand Doshi756dca72013-01-15 18:39:21 +05301# ERPNext - web based ERP (http://erpnext.com)
2# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17from __future__ import unicode_literals
18import webnotes
Anand Doshif8f0c0d2013-01-17 20:20:56 +053019from webnotes import _, msgprint
Anand Doshi3543f302013-05-24 19:25:01 +053020from webnotes.utils import flt
Anand Doshi756dca72013-01-15 18:39:21 +053021
22from buying.utils import get_item_details
Anand Doshif8f0c0d2013-01-17 20:20:56 +053023from setup.utils import get_company_currency
Anand Doshi756dca72013-01-15 18:39:21 +053024
Nabin Hait89a94d82013-03-19 12:01:46 +053025from controllers.stock_controller import StockController
Nabin Haitbf495c92013-01-30 12:49:08 +053026
Rushabh Mehtaecb36f22013-05-02 11:34:37 +053027class WrongWarehouseCompany(Exception): pass
28
Nabin Hait89a94d82013-03-19 12:01:46 +053029class BuyingController(StockController):
Anand Doshi3543f302013-05-24 19:25:01 +053030 def onload_post_render(self):
Anand Doshi3543f302013-05-24 19:25:01 +053031 # contact, address, item details
32 self.set_missing_values()
Anand Doshi3543f302013-05-24 19:25:01 +053033 self.set_taxes("Purchase Taxes and Charges", "purchase_tax_details", "purchase_other_charges")
34
Saurabh6f753182013-03-20 12:55:28 +053035 def validate(self):
36 super(BuyingController, self).validate()
Nabin Hait205f7ce2013-04-26 13:35:06 +053037 self.validate_stock_or_nonstock_items()
Rushabh Mehtaecb36f22013-05-02 11:34:37 +053038 self.validate_warehouse_belongs_to_company()
Anand Doshi1dde46a2013-05-15 21:15:57 +053039
Anand Doshi3543f302013-05-24 19:25:01 +053040 def set_missing_values(self, for_validate=False):
Anand Doshiabc10032013-06-14 17:44:03 +053041 super(BuyingController, self).set_missing_values(for_validate)
42
43 self.set_price_list_currency("Buying")
44
Anand Doshi3543f302013-05-24 19:25:01 +053045 # set contact and address details for supplier, if they are not mentioned
46 if self.doc.supplier and not (self.doc.contact_person and self.doc.supplier_address):
47 for fieldname, val in self.get_default_address_and_contact("supplier").items():
48 if not self.doc.fields.get(fieldname) and self.meta.get_field(fieldname):
49 self.doc.fields[fieldname] = val
50
51 self.set_missing_item_details(get_item_details)
52
53 def set_supplier_defaults(self):
Anand Doshibf3e54a2013-06-05 14:11:32 +053054 self.doc.fields.update(self.get_default_supplier_address(self.doc.fields))
Saurabh6f753182013-03-20 12:55:28 +053055
Anand Doshi3543f302013-05-24 19:25:01 +053056 def get_purchase_tax_details(self):
57 self.doclist = self.doc.clear_table(self.doclist, "purchase_tax_details")
Anand Doshi27a98162013-06-10 16:32:55 +053058 self.set_taxes("Purchase Taxes and Charges", "purchase_tax_details", "purchase_other_charges")
Anand Doshibf3e54a2013-06-05 14:11:32 +053059
Rushabh Mehtaecb36f22013-05-02 11:34:37 +053060 def validate_warehouse_belongs_to_company(self):
Rushabh Mehtac7115982013-05-02 14:22:40 +053061 for warehouse, company in webnotes.conn.get_values("Warehouse",
62 self.doclist.get_distinct_values("warehouse"), "company").items():
63 if company and company != self.doc.company:
Anand Doshieb41ffd2013-05-02 18:10:19 +053064 webnotes.msgprint(_("Company mismatch for Warehouse") + (": %s" % (warehouse,)),
Rushabh Mehtaecb36f22013-05-02 11:34:37 +053065 raise_exception=WrongWarehouseCompany)
Rushabh Mehtac7115982013-05-02 14:22:40 +053066
Nabin Hait205f7ce2013-04-26 13:35:06 +053067 def validate_stock_or_nonstock_items(self):
68 items = [d.item_code for d in self.doclist.get({"parentfield": self.fname})]
Nabin Haitd58d1b52013-04-26 17:25:44 +053069 if self.stock_items:
Nabin Hait205f7ce2013-04-26 13:35:06 +053070 nonstock_items = list(set(items) - set(self.stock_items))
Nabin Haitd58d1b52013-04-26 17:25:44 +053071 if nonstock_items:
72 webnotes.msgprint(_("Stock and non-stock items can not be entered in the same ") +
73 self.doc.doctype + _(""". You should make separate documents for them.
74 Stock Items: """) + ", ".join(self.stock_items) + _("""
75 Non-stock Items: """) + ", ".join(nonstock_items), raise_exception=1)
Nabin Hait205f7ce2013-04-26 13:35:06 +053076
77 elif items and not self.stock_items:
78 tax_for_valuation = [d.account_head for d in
79 self.doclist.get({"parentfield": "purchase_tax_details"})
80 if d.category in ["Valuation", "Valuation and Total"]]
81 if tax_for_valuation:
82 webnotes.msgprint(_("""Tax Category can not be 'Valuation' or 'Valuation and Total'
83 as all items are non-stock items"""), raise_exception=1)
84
Nabin Haitd3b62502013-01-21 17:24:31 +053085 def set_total_in_words(self):
Nabin Hait5b4c2942013-01-22 11:12:02 +053086 from webnotes.utils import money_in_words
Nabin Haitd3b62502013-01-21 17:24:31 +053087 company_currency = get_company_currency(self.doc.company)
Nabin Hait5b4c2942013-01-22 11:12:02 +053088 if self.meta.get_field("in_words"):
89 self.doc.in_words = money_in_words(self.doc.grand_total, company_currency)
90 if self.meta.get_field("in_words_import"):
91 self.doc.in_words_import = money_in_words(self.doc.grand_total_import,
Anand Doshi613cb6a2013-02-06 17:33:46 +053092 self.doc.currency)
93
94 def calculate_taxes_and_totals(self):
Anand Doshi3543f302013-05-24 19:25:01 +053095 self.other_fname = "purchase_tax_details"
96 super(BuyingController, self).calculate_taxes_and_totals()
Anand Doshi923d41d2013-05-28 17:23:36 +053097 self.calculate_total_advance("Purchase Invoice", "advance_allocation_details")
Anand Doshi8595fcf2013-02-08 19:28:14 +053098
Anand Doshi8595fcf2013-02-08 19:28:14 +053099 def calculate_item_values(self):
Anand Doshib8b1e582013-05-06 12:52:46 +0530100 # hack! - cleaned up in _cleanup()
101 if self.doc.doctype != "Purchase Invoice":
Anand Doshi21f4ea32013-05-10 18:08:32 +0530102 df = self.meta.get_field("purchase_rate", parentfield=self.fname)
103 df.fieldname = "rate"
Anand Doshia1d4b782013-02-26 18:09:47 +0530104
Anand Doshib8b1e582013-05-06 12:52:46 +0530105 for item in self.item_doclist:
Anand Doshia1d4b782013-02-26 18:09:47 +0530106 # hack! - cleaned up in _cleanup()
107 if self.doc.doctype != "Purchase Invoice":
108 item.rate = item.purchase_rate
Anand Doshia1d4b782013-02-26 18:09:47 +0530109
Anand Doshi39384d32013-05-11 19:39:53 +0530110 self.round_floats_in(item)
Anand Doshi8595fcf2013-02-08 19:28:14 +0530111
Anand Doshi3543f302013-05-24 19:25:01 +0530112 if item.discount_rate == 100.0:
113 item.import_rate = 0.0
114 elif item.import_ref_rate:
115 item.import_rate = flt(item.import_ref_rate * (1.0 - (item.discount_rate / 100.0)),
116 self.precision("import_rate", item))
Anand Doshia1d4b782013-02-26 18:09:47 +0530117
Anand Doshi8595fcf2013-02-08 19:28:14 +0530118 item.import_amount = flt(item.import_rate * item.qty,
Anand Doshi39384d32013-05-11 19:39:53 +0530119 self.precision("import_amount", item))
Anand Doshi3543f302013-05-24 19:25:01 +0530120 item.item_tax_amount = 0.0;
Anand Doshia1d4b782013-02-26 18:09:47 +0530121
Anand Doshi3543f302013-05-24 19:25:01 +0530122 self._set_in_company_currency(item, "import_ref_rate", "purchase_ref_rate")
123 self._set_in_company_currency(item, "import_rate", "rate")
124 self._set_in_company_currency(item, "import_amount", "amount")
Anand Doshi5af812a2013-05-10 19:23:02 +0530125
Anand Doshi8595fcf2013-02-08 19:28:14 +0530126 def calculate_net_total(self):
Anand Doshi3543f302013-05-24 19:25:01 +0530127 self.doc.net_total = self.doc.net_total_import = 0.0
Anand Doshi8595fcf2013-02-08 19:28:14 +0530128
129 for item in self.item_doclist:
130 self.doc.net_total += item.amount
131 self.doc.net_total_import += item.import_amount
132
Anand Doshi3543f302013-05-24 19:25:01 +0530133 self.round_floats_in(self.doc, ["net_total", "net_total_import"])
Anand Doshi8595fcf2013-02-08 19:28:14 +0530134
135 def calculate_totals(self):
Anand Doshi3543f302013-05-24 19:25:01 +0530136 self.doc.grand_total = flt(self.tax_doclist and \
137 self.tax_doclist[-1].total or self.doc.net_total, self.precision("grand_total"))
138 self.doc.grand_total_import = flt(self.doc.grand_total / self.doc.conversion_rate,
139 self.precision("grand_total_import"))
Anand Doshi8595fcf2013-02-08 19:28:14 +0530140
Anand Doshi3543f302013-05-24 19:25:01 +0530141 self.doc.total_tax = flt(self.doc.grand_total - self.doc.net_total,
Anand Doshi5af812a2013-05-10 19:23:02 +0530142 self.precision("total_tax"))
Anand Doshi8595fcf2013-02-08 19:28:14 +0530143
144 if self.meta.get_field("rounded_total"):
145 self.doc.rounded_total = round(self.doc.grand_total)
146
147 if self.meta.get_field("rounded_total_import"):
148 self.doc.rounded_total_import = round(self.doc.grand_total_import)
149
150 def calculate_outstanding_amount(self):
Anand Doshic99853c2013-05-14 13:35:09 +0530151 if self.doc.doctype == "Purchase Invoice" and self.doc.docstatus < 2:
Anand Doshi8595fcf2013-02-08 19:28:14 +0530152 self.doc.total_advance = flt(self.doc.total_advance,
Anand Doshi5af812a2013-05-10 19:23:02 +0530153 self.precision("total_advance"))
Anand Doshi9d794fc2013-02-27 13:34:22 +0530154 self.doc.total_amount_to_pay = flt(self.doc.grand_total - flt(self.doc.write_off_amount,
Anand Doshi5af812a2013-05-10 19:23:02 +0530155 self.precision("write_off_amount")), self.precision("total_amount_to_pay"))
Anand Doshi9d794fc2013-02-27 13:34:22 +0530156 self.doc.outstanding_amount = flt(self.doc.total_amount_to_pay - self.doc.total_advance,
Anand Doshi5af812a2013-05-10 19:23:02 +0530157 self.precision("outstanding_amount"))
Anand Doshi8595fcf2013-02-08 19:28:14 +0530158
159 def _cleanup(self):
Anand Doshi3543f302013-05-24 19:25:01 +0530160 super(BuyingController, self)._cleanup()
161
162 # except in purchase invoice, rate field is purchase_rate
Anand Doshi5af812a2013-05-10 19:23:02 +0530163 # reset fieldname of rate
164 if self.doc.doctype != "Purchase Invoice":
165 df = self.meta.get_field("rate", parentfield=self.fname)
166 df.fieldname = "purchase_rate"
Anand Doshi8595fcf2013-02-08 19:28:14 +0530167
Anand Doshi3543f302013-05-24 19:25:01 +0530168 for item in self.item_doclist:
169 item.purchase_rate = item.rate
170 del item.fields["rate"]
Anand Doshib3928782013-06-05 17:46:20 +0530171
Anand Doshi923d41d2013-05-28 17:23:36 +0530172 if not self.meta.get_field("item_tax_amount", parentfield=self.fname):
173 for item in self.item_doclist:
174 del item.fields["item_tax_amount"]
175
Anand Doshi8595fcf2013-02-08 19:28:14 +0530176 def set_item_tax_amount(self, item, tax, current_tax_amount):
177 """
178 item_tax_amount is the total tax amount applied on that item
179 stored for valuation
180
181 TODO: rename item_tax_amount to valuation_tax_amount
182 """
Anand Doshib3928782013-06-05 17:46:20 +0530183 if tax.category in ["Valuation", "Valuation and Total"] and \
184 self.meta.get_field("item_tax_amount", parentfield=self.fname):
185 item.item_tax_amount += flt(current_tax_amount, self.precision("item_tax_amount", item))
Anand Doshi4a7248e2013-02-27 18:10:30 +0530186
187 # update valuation rate
188 def update_valuation_rate(self, parentfield):
Anand Doshi5af812a2013-05-10 19:23:02 +0530189 for item in self.doclist.get({"parentfield": parentfield}):
190 item.conversion_factor = item.conversion_factor or flt(webnotes.conn.get_value(
191 "UOM Conversion Detail", {"parent": item.item_code, "uom": item.uom},
Nabin Hait0fc24542013-03-25 11:06:00 +0530192 "conversion_factor")) or 1
Anand Doshi39384d32013-05-11 19:39:53 +0530193
Anand Doshi5af812a2013-05-10 19:23:02 +0530194 if item.item_code and item.qty:
Anand Doshi39384d32013-05-11 19:39:53 +0530195 self.round_floats_in(item)
196
197 purchase_rate = item.rate if self.doc.doctype == "Purchase Invoice" else item.purchase_rate
198
Anand Doshi1bfec3a2013-02-28 19:17:46 +0530199 # if no item code, which is sometimes the case in purchase invoice,
200 # then it is not possible to track valuation against it
Anand Doshi39384d32013-05-11 19:39:53 +0530201 item.valuation_rate = flt((purchase_rate +
202 (item.item_tax_amount + item.rm_supp_cost) / item.qty) / item.conversion_factor,
203 self.precision("valuation_rate", item))
Anand Doshi4a7248e2013-02-27 18:10:30 +0530204 else:
Anand Doshi5af812a2013-05-10 19:23:02 +0530205 item.valuation_rate = 0.0
Nabin Hait54d209f2013-03-01 18:51:10 +0530206
207 def validate_for_subcontracting(self):
208 if not self.doc.is_subcontracted and self.sub_contracted_items:
209 webnotes.msgprint(_("""Please enter whether %s is made for subcontracting or purchasing,
210 in 'Is Subcontracted' field""" % self.doc.doctype), raise_exception=1)
211
212 if self.doc.doctype == "Purchase Receipt" and self.doc.is_subcontracted=="Yes" \
213 and not self.doc.supplier_warehouse:
214 webnotes.msgprint(_("Supplier Warehouse mandatory subcontracted purchase receipt"),
215 raise_exception=1)
216
217 def update_raw_materials_supplied(self, raw_material_table):
218 self.doclist = self.doc.clear_table(self.doclist, raw_material_table)
219 if self.doc.is_subcontracted=="Yes":
220 for item in self.doclist.get({"parentfield": self.fname}):
221 if item.item_code in self.sub_contracted_items:
222 self.add_bom_items(item, raw_material_table)
223
224 def add_bom_items(self, d, raw_material_table):
225 bom_items = self.get_items_from_default_bom(d.item_code)
226 raw_materials_cost = 0
227 for item in bom_items:
228 required_qty = flt(item.qty_consumed_per_unit) * flt(d.qty) * flt(d.conversion_factor)
229 rm_doclist = {
230 "parentfield": raw_material_table,
231 "doctype": self.doc.doctype + " Item Supplied",
232 "reference_name": d.name,
233 "bom_detail_no": item.name,
234 "main_item_code": d.item_code,
235 "rm_item_code": item.item_code,
236 "stock_uom": item.stock_uom,
237 "required_qty": required_qty,
238 "conversion_factor": d.conversion_factor,
239 "rate": item.rate,
240 "amount": required_qty * flt(item.rate)
241 }
242 if self.doc.doctype == "Purchase Receipt":
243 rm_doclist.update({
244 "consumed_qty": required_qty,
245 "description": item.description,
246 })
247
248 self.doclist.append(rm_doclist)
249
250 raw_materials_cost += required_qty * flt(item.rate)
251
252 if self.doc.doctype == "Purchase Receipt":
253 d.rm_supp_cost = raw_materials_cost
254
255 def get_items_from_default_bom(self, item_code):
256 # print webnotes.conn.sql("""select name from `tabBOM` where item = '_Test FG Item'""")
257 bom_items = webnotes.conn.sql("""select t2.item_code, t2.qty_consumed_per_unit,
258 t2.rate, t2.stock_uom, t2.name, t2.description
259 from `tabBOM` t1, `tabBOM Item` t2
260 where t2.parent = t1.name and t1.item = %s and t1.is_default = 1
261 and t1.docstatus = 1 and t1.is_active = 1""", item_code, as_dict=1)
262 if not bom_items:
263 msgprint(_("No default BOM exists for item: ") + item_code, raise_exception=1)
264
265 return bom_items
266
Nabin Hait5418d712013-02-27 18:11:17 +0530267 @property
268 def sub_contracted_items(self):
269 if not hasattr(self, "_sub_contracted_items"):
Nabin Haitebd51442013-04-23 15:36:26 +0530270 self._sub_contracted_items = []
Nabin Hait5418d712013-02-27 18:11:17 +0530271 item_codes = list(set(item.item_code for item in
272 self.doclist.get({"parentfield": self.fname})))
Nabin Haitebd51442013-04-23 15:36:26 +0530273 if item_codes:
274 self._sub_contracted_items = [r[0] for r in webnotes.conn.sql("""select name
275 from `tabItem` where name in (%s) and is_sub_contracted_item='Yes'""" % \
276 (", ".join((["%s"]*len(item_codes))),), item_codes)]
Nabin Hait5418d712013-02-27 18:11:17 +0530277
278 return self._sub_contracted_items
279
280 @property
281 def purchase_items(self):
282 if not hasattr(self, "_purchase_items"):
Nabin Haitebd51442013-04-23 15:36:26 +0530283 self._purchase_items = []
Nabin Hait5418d712013-02-27 18:11:17 +0530284 item_codes = list(set(item.item_code for item in
285 self.doclist.get({"parentfield": self.fname})))
Nabin Haitebd51442013-04-23 15:36:26 +0530286 if item_codes:
287 self._purchase_items = [r[0] for r in webnotes.conn.sql("""select name
288 from `tabItem` where name in (%s) and is_purchase_item='Yes'""" % \
289 (", ".join((["%s"]*len(item_codes))),), item_codes)]
Nabin Hait5418d712013-02-27 18:11:17 +0530290
Anand Doshibf3e54a2013-06-05 14:11:32 +0530291 return self._purchase_items