blob: d7a2964c49e8ce5962fc8c211f3a8afcbd3a249f [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
20from 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
25from utilities.transaction_base import TransactionBase
26class BuyingController(TransactionBase):
27 def validate(self):
Anand Doshif8f0c0d2013-01-17 20:20:56 +053028 if self.meta.get_field("currency"):
29 self.company_currency = get_company_currency(self.doc.company)
30 self.validate_conversion_rate("currency", "conversion_rate")
Anand Doshi8957bf92013-01-17 20:29:28 +053031
32 if self.doc.price_list_name and self.doc.price_list_currency:
33 self.validate_conversion_rate("price_list_currency", "plc_conversion_rate")
Anand Doshi756dca72013-01-15 18:39:21 +053034
35 def update_item_details(self):
36 for item in self.doclist.get({"parentfield": self.fname}):
37 ret = get_item_details({
38 "doctype": self.doc.doctype,
39 "docname": self.doc.name,
40 "item_code": item.item_code,
41 "warehouse": item.warehouse,
42 "supplier": self.doc.supplier,
43 "transaction_date": self.doc.posting_date,
44 "conversion_rate": self.doc.conversion_rate
45 })
46 for r in ret:
47 if not item.fields.get(r):
48 item.fields[r] = ret[r]
Anand Doshif8f0c0d2013-01-17 20:20:56 +053049
50 def validate_conversion_rate(self, currency_field, conversion_rate_field):
51 """common validation for currency and price list currency"""
52
53 currency = self.doc.fields.get(currency_field)
54 conversion_rate = flt(self.doc.fields.get(conversion_rate_field))
55 conversion_rate_label = self.meta.get_label(conversion_rate_field)
56
57 if conversion_rate == 0:
58 msgprint(conversion_rate_label + _(' cannot be 0'), raise_exception=True)
59
60 # parenthesis for 'OR' are necessary as we want it to evaluate as
61 # mandatory valid condition and (1st optional valid condition
62 # or 2nd optional valid condition)
63 valid_conversion_rate = (conversion_rate and
64 ((currency == self.company_currency and conversion_rate == 1.00)
65 or (currency != self.company_currency and conversion_rate != 1.00)))
66
67 if not valid_conversion_rate:
68 msgprint(_('Please enter valid ') + conversion_rate_label + (': ')
Nabin Hait10221982013-01-18 16:47:39 +053069 + ("1 %s = [?] %s" % (currency, self.company_currency)),
Anand Doshif8f0c0d2013-01-17 20:20:56 +053070 raise_exception=True)