Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 1 | # 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 | |
| 17 | from __future__ import unicode_literals |
| 18 | import webnotes |
Anand Doshi | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 19 | from webnotes import _, msgprint |
| 20 | from webnotes.utils import flt |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 21 | |
| 22 | from buying.utils import get_item_details |
Anand Doshi | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 23 | from setup.utils import get_company_currency |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 24 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 25 | from controllers.accounts_controller import AccountsController |
| 26 | |
| 27 | class BuyingController(AccountsController): |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 28 | def validate(self): |
Anand Doshi | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 29 | 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 Doshi | 8957bf9 | 2013-01-17 20:29:28 +0530 | [diff] [blame] | 32 | |
| 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 Hait | d3b6250 | 2013-01-21 17:24:31 +0530 | [diff] [blame] | 35 | |
| 36 | # set total in words |
Nabin Hait | 5b4c294 | 2013-01-22 11:12:02 +0530 | [diff] [blame] | 37 | self.set_total_in_words() |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 38 | |
| 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 Doshi | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 53 | |
| 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 Hait | 1022198 | 2013-01-18 16:47:39 +0530 | [diff] [blame] | 73 | + ("1 %s = [?] %s" % (currency, self.company_currency)), |
Anand Doshi | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 74 | raise_exception=True) |
Nabin Hait | d3b6250 | 2013-01-21 17:24:31 +0530 | [diff] [blame] | 75 | |
| 76 | def set_total_in_words(self): |
Nabin Hait | 5b4c294 | 2013-01-22 11:12:02 +0530 | [diff] [blame] | 77 | from webnotes.utils import money_in_words |
Nabin Hait | d3b6250 | 2013-01-21 17:24:31 +0530 | [diff] [blame] | 78 | company_currency = get_company_currency(self.doc.company) |
Nabin Hait | 5b4c294 | 2013-01-22 11:12:02 +0530 | [diff] [blame] | 79 | 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) |