blob: a467eb22fe36cf5879eff9740222c200b72bc309 [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
Nabin Haitbf495c92013-01-30 12:49:08 +053025from controllers.accounts_controller import AccountsController
26
27class BuyingController(AccountsController):
Nabin Haitfb3fd6e2013-01-30 19:16:13 +053028 def validate(self):
Anand Doshif8f0c0d2013-01-17 20:20:56 +053029 if self.meta.get_field("currency"):
30 self.company_currency = get_company_currency(self.doc.company)
31 self.validate_conversion_rate("currency", "conversion_rate")
Anand Doshi8957bf92013-01-17 20:29:28 +053032
33 if self.doc.price_list_name and self.doc.price_list_currency:
34 self.validate_conversion_rate("price_list_currency", "plc_conversion_rate")
Nabin Haitd3b62502013-01-21 17:24:31 +053035
36 # set total in words
Nabin Hait5b4c2942013-01-22 11:12:02 +053037 self.set_total_in_words()
Anand Doshi756dca72013-01-15 18:39:21 +053038
39 def update_item_details(self):
40 for item in self.doclist.get({"parentfield": self.fname}):
41 ret = get_item_details({
42 "doctype": self.doc.doctype,
43 "docname": self.doc.name,
44 "item_code": item.item_code,
45 "warehouse": item.warehouse,
46 "supplier": self.doc.supplier,
47 "transaction_date": self.doc.posting_date,
48 "conversion_rate": self.doc.conversion_rate
49 })
50 for r in ret:
51 if not item.fields.get(r):
52 item.fields[r] = ret[r]
Anand Doshif8f0c0d2013-01-17 20:20:56 +053053
54 def validate_conversion_rate(self, currency_field, conversion_rate_field):
55 """common validation for currency and price list currency"""
56
57 currency = self.doc.fields.get(currency_field)
58 conversion_rate = flt(self.doc.fields.get(conversion_rate_field))
59 conversion_rate_label = self.meta.get_label(conversion_rate_field)
60
61 if conversion_rate == 0:
62 msgprint(conversion_rate_label + _(' cannot be 0'), raise_exception=True)
63
64 # parenthesis for 'OR' are necessary as we want it to evaluate as
65 # mandatory valid condition and (1st optional valid condition
66 # or 2nd optional valid condition)
67 valid_conversion_rate = (conversion_rate and
68 ((currency == self.company_currency and conversion_rate == 1.00)
69 or (currency != self.company_currency and conversion_rate != 1.00)))
70
71 if not valid_conversion_rate:
72 msgprint(_('Please enter valid ') + conversion_rate_label + (': ')
Nabin Hait10221982013-01-18 16:47:39 +053073 + ("1 %s = [?] %s" % (currency, self.company_currency)),
Anand Doshif8f0c0d2013-01-17 20:20:56 +053074 raise_exception=True)
Nabin Haitd3b62502013-01-21 17:24:31 +053075
76 def set_total_in_words(self):
Nabin Hait5b4c2942013-01-22 11:12:02 +053077 from webnotes.utils import money_in_words
Nabin Haitd3b62502013-01-21 17:24:31 +053078 company_currency = get_company_currency(self.doc.company)
Nabin Hait5b4c2942013-01-22 11:12:02 +053079 if self.meta.get_field("in_words"):
80 self.doc.in_words = money_in_words(self.doc.grand_total, company_currency)
81 if self.meta.get_field("in_words_import"):
82 self.doc.in_words_import = money_in_words(self.doc.grand_total_import,
83 self.doc.currency)