blob: f48b626d5db15987891b90e90e42a549fedb6fd3 [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")
Nabin Haitd3b62502013-01-21 17:24:31 +053034
35 # set total in words
Nabin Hait5b4c2942013-01-22 11:12:02 +053036 self.set_total_in_words()
Anand Doshi756dca72013-01-15 18:39:21 +053037
38 def update_item_details(self):
39 for item in self.doclist.get({"parentfield": self.fname}):
40 ret = get_item_details({
41 "doctype": self.doc.doctype,
42 "docname": self.doc.name,
43 "item_code": item.item_code,
44 "warehouse": item.warehouse,
45 "supplier": self.doc.supplier,
46 "transaction_date": self.doc.posting_date,
47 "conversion_rate": self.doc.conversion_rate
48 })
49 for r in ret:
50 if not item.fields.get(r):
51 item.fields[r] = ret[r]
Anand Doshif8f0c0d2013-01-17 20:20:56 +053052
53 def validate_conversion_rate(self, currency_field, conversion_rate_field):
54 """common validation for currency and price list currency"""
55
56 currency = self.doc.fields.get(currency_field)
57 conversion_rate = flt(self.doc.fields.get(conversion_rate_field))
58 conversion_rate_label = self.meta.get_label(conversion_rate_field)
59
60 if conversion_rate == 0:
61 msgprint(conversion_rate_label + _(' cannot be 0'), raise_exception=True)
62
63 # parenthesis for 'OR' are necessary as we want it to evaluate as
64 # mandatory valid condition and (1st optional valid condition
65 # or 2nd optional valid condition)
66 valid_conversion_rate = (conversion_rate and
67 ((currency == self.company_currency and conversion_rate == 1.00)
68 or (currency != self.company_currency and conversion_rate != 1.00)))
69
70 if not valid_conversion_rate:
71 msgprint(_('Please enter valid ') + conversion_rate_label + (': ')
Nabin Hait10221982013-01-18 16:47:39 +053072 + ("1 %s = [?] %s" % (currency, self.company_currency)),
Anand Doshif8f0c0d2013-01-17 20:20:56 +053073 raise_exception=True)
Nabin Haitd3b62502013-01-21 17:24:31 +053074
75 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,
82 self.doc.currency)