[MERGE] base: raise an exception if the format of the bank account is wrong
[odoo/odoo.git] / openerp / addons / base / res / res_company.py
index edcc733..83b24b5 100644 (file)
@@ -24,6 +24,7 @@ from osv import fields
 import os
 import tools
 import openerp
+from openerp import SUPERUSER_ID
 from tools.translate import _
 from tools.safe_eval import safe_eval as eval
 
@@ -88,7 +89,6 @@ class res_company(osv.osv):
                         result[company.id][field] = address[field] or False
         return result
 
-
     def _set_address_data(self, cr, uid, company_id, name, value, arg, context=None):
         """ Write the 'address' functional fields. """
         company = self.browse(cr, uid, company_id, context=context)
@@ -102,18 +102,18 @@ class res_company(osv.osv):
                 part_obj.create(cr, uid, {name: value or False, 'parent_id': company.partner_id.id}, context=context)
         return True
 
-
     _columns = {
         'name': fields.related('partner_id', 'name', string='Company Name', size=128, required=True, store=True, type='char'),
-        'customize_footer': fields.boolean('Customize Footer', help="If it is true then general information footer will not update automatically."),
         'parent_id': fields.many2one('res.company', 'Parent Company', select=True),
         'child_ids': fields.one2many('res.company', 'parent_id', 'Child Companies'),
         'partner_id': fields.many2one('res.partner', 'Partner', required=True),
-        'rml_footer': fields.text('General Information Footer'),
-        'rml_header': fields.text('RML Header and Footer', required=True),
+        'rml_header': fields.text('RML Header', required=True),
         'rml_header1': fields.char('Company Slogan', size=200, help="Appears by default on the top right corner of your printed documents (report header)."),
         'rml_header2': fields.text('RML Internal Header', required=True),
         'rml_header3': fields.text('RML Internal Header for Landscape Reports', required=True),
+        'rml_footer': fields.text('Report Footer', help="Footer text displayed at the bottom of all reports. Automatically set based on company details, "\
+                                  "but may also be customized by directly editing it."),
+        'custom_footer': fields.boolean('Custom Footer', help="Check this to define the report footer manually.  Otherwise it will be filled in automatically."),
         'logo': fields.related('partner_id', 'image', string="Logo", type="binary"),
         'currency_id': fields.many2one('res.currency', 'Currency', required=True),
         'currency_ids': fields.one2many('res.currency', 'company_id', 'Currency'),
@@ -138,31 +138,42 @@ class res_company(osv.osv):
         ('name_uniq', 'unique (name)', 'The company name must be unique !')
     ]
 
-    def _set_rml_footer(self, cr, uid, ids, footer=False, context=None):
-        return self.write(cr, uid, ids, {'rml_footer': footer}, context=context)
-
-    def on_change_footer(self, cr, uid, ids, customize_footer, phone, email, fax, website, vat, company_registry=False, bank_ids=False, context=None):
-        val = []
-        if customize_footer:
-            return {}
-
-        if phone: val.append(_('Phone: ')+phone)
-        if fax: val.append(_('Fax: ')+fax)
-        if email: val.append(_('Email: ')+email)
-        if website: val.append(_('Website: ')+website)
-        if vat: val.append(_('TIN: ')+vat)
-        if company_registry: val.append(_('Reg: ')+company_registry)
-        # if the bank accounts got edited and at least one if flagged as "show in the footer"
-        if bank_ids:
-            bank_ids = self.browse(cr, uid, ids[0], context=context).bank_ids
-            bank_account_numbers = [bank_account.acc_number for bank_account in bank_ids if bank_account.footer]
-            # append the account(s) in the footer and manage plural form of "account" if necessary
-            val.append(_('Bank Account'+('s' if len(bank_account_numbers) > 1 else '')+': ')+', '.join(bank_account_numbers))
-
-        footer = ' | '.join(val)
-        self._set_rml_footer(cr, uid, ids, footer, context=context)
+    def onchange_footer(self, cr, uid, ids, context=None):
+        # when touched, the footer becomes custom
+        return {'value': {'custom_footer': True}}
+
+    def set_auto_footer(self, cr, uid, ids, context=None):
+        # unset the flag 'custom_footer'; this will automatically compute the footer
+        return self.write(cr, uid, ids, {'custom_footer': False}, context=context)
+
+    def compute_footer(self, cr, uid, ids, context=None):
+        res_partner_bank = self.pool.get('res.partner.bank')
+        for company in self.browse(cr, uid, ids, context):
+            if not company.custom_footer:
+                # first line (notice that missing elements are filtered out before the join)
+                res = ' | '.join(filter(bool, [
+                    company.phone            and '%s: %s' % (_('Phone'), company.phone),
+                    company.fax              and '%s: %s' % (_('Fax'), company.fax),
+                    company.email            and '%s: %s' % (_('Email'), company.email),
+                    company.website          and '%s: %s' % (_('Website'), company.website),
+                    company.vat              and '%s: %s' % (_('TIN'), company.vat),
+                    company.company_registry and '%s: %s' % (_('Reg'), company.company_registry),
+                ]))
+                # second line: bank accounts
+                account_ids = [acc.id for acc in company.bank_ids if acc.footer]
+                account_names = res_partner_bank.name_get(cr, uid, account_ids, context=context)
+                if account_names:
+                    title = _('Bank Accounts') if len(account_names) > 1 else _('Bank Account')
+                    res += '\n%s: %s' % (title, ', '.join(name for id, name in account_names))
+                # update footer
+                self.write(cr, uid, [company.id], {'rml_footer': res}, context=context)
+        return True
 
-        return {'value': {'rml_footer': footer}}
+    def on_change_country(self, cr, uid, ids, country_id, context=None):
+        currency_id = self._get_euro(cr, uid, context=context)
+        if country_id:
+            currency_id = self.pool.get('res.country').browse(cr, uid, country_id, context=context).currency_id.id
+        return {'value': {'currency_id': currency_id}}
 
     def _search(self, cr, uid, args, offset=0, limit=None, order=None,
             context=None, count=False, access_rights_uid=None):
@@ -173,7 +184,7 @@ class res_company(osv.osv):
             # select only the currently visible companies (according to rules,
             # which are probably to allow to see the child companies) even if
             # she belongs to some other companies.
-            user = self.pool.get('res.users').browse(cr, 1, uid, context=context)
+            user = self.pool.get('res.users').browse(cr, SUPERUSER_ID, uid, context=context)
             cmp_ids = list(set([user.company_id.id] + [cmp.id for cmp in user.company_ids]))
             return cmp_ids
         return super(res_company, self)._search(cr, uid, args, offset=offset, limit=limit, order=order,
@@ -237,17 +248,22 @@ class res_company(osv.osv):
         self.cache_restart(cr)
         company_id = super(res_company, self).create(cr, uid, vals, context=context)
         obj_partner.write(cr, uid, partner_id, {'company_id': company_id}, context=context)
+        self.compute_footer(cr, uid, [company_id], context=context)
         return company_id
 
-    def write(self, cr, *args, **argv):
+    def write(self, cr, uid, ids, values, context=None):
         self.cache_restart(cr)
-        return super(res_company, self).write(cr, *args, **argv)
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+        super(res_company, self).write(cr, uid, ids, values, context=context)
+        if 'rml_footer' not in values:
+            self.compute_footer(cr, uid, ids, context=context)
+        return True
 
     def _get_euro(self, cr, uid, context=None):
-        try:
-            return self.pool.get('res.currency').search(cr, uid, [])[0]
-        except:
-            return False
+        rate_obj = self.pool.get('res.currency.rate')
+        rate_id = rate_obj.search(cr, uid, [('rate', '=', 1)], context=context)
+        return rate_id and rate_obj.browse(cr, uid, rate_id[0], context=context).currency_id.id or False
 
     def _get_logo(self, cr, uid, ids):
         return open(os.path.join( tools.config['root_path'], 'addons', 'base', 'res', 'res_company_logo.png'), 'rb') .read().encode('base64')
@@ -285,36 +301,45 @@ class res_company(osv.osv):
 
     _header_main = """
 <header>
-<pageTemplate>
-    <frame id="first" x1="1.3cm" y1="2.5cm" height="%s" width="19.0cm"/>
-    <pageGraphics>
-        <!-- You Logo - Change X,Y,Width and Height -->
-        <image x="1.3cm" y="%s" height="40.0" >[[ company.logo or removeParentNode('image') ]]</image>
-        <setFont name="DejaVu Sans" size="8"/>
-        <fill color="black"/>
-        <stroke color="black"/>
-        <lines>1.3cm %s 20cm %s</lines>
-
-        <drawRightString x="20cm" y="%s">[[ company.rml_header1 ]]</drawRightString>
-
-
-        <drawString x="1.3cm" y="%s">[[ company.partner_id.name ]]</drawString>
-        <drawString x="1.3cm" y="%s">[[ company.partner_id.street or  '' ]]</drawString>
-        <drawString x="1.3cm" y="%s">[[ company.partner_id.city or '' ]] - [[ company.partner_id.country_id and company.partner_id.country_id.name  or '']]</drawString>
-        <drawString x="1.3cm" y="%s">Phone:</drawString>
-        <drawRightString x="7cm" y="%s">[[ company.partner_id.phone or '' ]]</drawRightString>
-        <drawString x="1.3cm" y="%s">Mail:</drawString>
-        <drawRightString x="7cm" y="%s">[[ company.partner_id.email or '' ]]</drawRightString>
-        <lines>1.3cm %s 7cm %s</lines>
-
-        <!--page bottom-->
-
-        <lines>1.2cm 2.15cm 19.9cm 2.15cm</lines>
-
-        <drawCentredString x="10.5cm" y="1.7cm">[[ company.rml_footer ]]</drawCentredString>
-        <drawCentredString x="10.5cm" y="0.8cm">Contact : [[ user.name ]] - Page: <pageNumber/></drawCentredString>
-    </pageGraphics>
-</pageTemplate>
+    <pageTemplate>
+        <frame id="first" x1="1.3cm" y1="3.0cm" height="%s" width="19.0cm"/>
+         <stylesheet>
+            <paraStyle name="main_footer"  fontName="DejaVu Sans" fontSize="8.0" alignment="CENTER"/>
+         </stylesheet>
+        <pageGraphics>
+            <!-- You Logo - Change X,Y,Width and Height -->
+            <image x="1.3cm" y="%s" height="40.0" >[[ company.logo or removeParentNode('image') ]]</image>
+            <setFont name="DejaVu Sans" size="8"/>
+            <fill color="black"/>
+            <stroke color="black"/>
+
+            <!-- page header -->
+            <lines>1.3cm %s 20cm %s</lines>
+            <drawRightString x="20cm" y="%s">[[ company.rml_header1 ]]</drawRightString>
+            <drawString x="1.3cm" y="%s">[[ company.partner_id.name ]]</drawString>
+            <drawString x="1.3cm" y="%s">[[ company.partner_id.street or  '' ]]</drawString>
+            <drawString x="1.3cm" y="%s">[[ company.partner_id.city or '' ]] - [[ company.partner_id.country_id and company.partner_id.country_id.name  or '']]</drawString>
+            <drawString x="1.3cm" y="%s">Phone:</drawString>
+            <drawRightString x="7cm" y="%s">[[ company.partner_id.phone or '' ]]</drawRightString>
+            <drawString x="1.3cm" y="%s">Mail:</drawString>
+            <drawRightString x="7cm" y="%s">[[ company.partner_id.email or '' ]]</drawRightString>
+            <lines>1.3cm %s 7cm %s</lines>
+
+            <!-- left margin -->
+            <rotate degrees="90"/>
+            <fill color="grey"/>
+            <drawString x="2.65cm" y="-0.4cm">produced by OpenERP.com</drawString>
+            <fill color="black"/>
+            <rotate degrees="-90"/>
+
+            <!--page bottom-->
+            <lines>1.2cm 2.65cm 19.9cm 2.65cm</lines>
+            <place x="1.3cm" y="0cm" height="2.55cm" width="19.0cm">
+                <para style="main_footer">[[ company.rml_footer ]]</para>
+                <para style="main_footer">Contact : [[ user.name ]] - Page: <pageNumber/></para>
+            </place>
+        </pageGraphics>
+    </pageTemplate>
 </header>"""
 
     _header_a4 = _header_main % ('23.0cm', '27.6cm', '27.7cm', '27.7cm', '27.8cm', '27.2cm', '26.8cm', '26.4cm', '26.0cm', '26.0cm', '25.6cm', '25.6cm', '25.5cm', '25.5cm')