blob: f416b853fbc23e5b9fa1c46de402b3e19f53fb37 [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,
Nabin Haitc10c90a2013-02-06 11:44:43 +053048 "conversion_rate": self.doc.conversion_rate,
49 "price_list_name": self.doc.price_list_name,
50 "price_list_currency": self.doc.price_list_currency,
51 "plc_conversion_rate": self.doc.plc_conversion_rate
Anand Doshi756dca72013-01-15 18:39:21 +053052 })
53 for r in ret:
54 if not item.fields.get(r):
55 item.fields[r] = ret[r]
Anand Doshif8f0c0d2013-01-17 20:20:56 +053056
57 def validate_conversion_rate(self, currency_field, conversion_rate_field):
58 """common validation for currency and price list currency"""
59
60 currency = self.doc.fields.get(currency_field)
61 conversion_rate = flt(self.doc.fields.get(conversion_rate_field))
62 conversion_rate_label = self.meta.get_label(conversion_rate_field)
63
64 if conversion_rate == 0:
65 msgprint(conversion_rate_label + _(' cannot be 0'), raise_exception=True)
66
67 # parenthesis for 'OR' are necessary as we want it to evaluate as
68 # mandatory valid condition and (1st optional valid condition
69 # or 2nd optional valid condition)
70 valid_conversion_rate = (conversion_rate and
71 ((currency == self.company_currency and conversion_rate == 1.00)
72 or (currency != self.company_currency and conversion_rate != 1.00)))
73
74 if not valid_conversion_rate:
75 msgprint(_('Please enter valid ') + conversion_rate_label + (': ')
Nabin Hait10221982013-01-18 16:47:39 +053076 + ("1 %s = [?] %s" % (currency, self.company_currency)),
Anand Doshif8f0c0d2013-01-17 20:20:56 +053077 raise_exception=True)
Nabin Haitd3b62502013-01-21 17:24:31 +053078
79 def set_total_in_words(self):
Nabin Hait5b4c2942013-01-22 11:12:02 +053080 from webnotes.utils import money_in_words
Nabin Haitd3b62502013-01-21 17:24:31 +053081 company_currency = get_company_currency(self.doc.company)
Nabin Hait5b4c2942013-01-22 11:12:02 +053082 if self.meta.get_field("in_words"):
83 self.doc.in_words = money_in_words(self.doc.grand_total, company_currency)
84 if self.meta.get_field("in_words_import"):
85 self.doc.in_words_import = money_in_words(self.doc.grand_total_import,
Anand Doshi613cb6a2013-02-06 17:33:46 +053086 self.doc.currency)
87
88 def calculate_taxes_and_totals(self):
89 pass