From 9a6f414d136c61e7a5bbba0bcaf0b2f2a6268eb7 Mon Sep 17 00:00:00 2001
From: Arthur Maniet <arthurmaniet@me.com>
Date: Fri, 29 May 2015 14:48:26 +0200
Subject: [PATCH] [FIX] account: payment and related features usability

---
 addons/account/account.py                     |  2 +-
 addons/account/account_payment.py             | 25 +++++++++++--------
 addons/account/views/account_bank_view.xml    |  4 +--
 addons/account/views/account_invoice_view.xml |  4 +--
 addons/account/views/account_payment_view.xml | 12 ++++-----
 addons/account/views/account_view.xml         | 20 +++++++--------
 addons/account/views/company_view.xml         |  4 +--
 addons/account/views/res_config_view.xml      |  2 +-
 .../wizard/journal_creation.xml               |  2 +-
 .../data/check_writing.xml                    |  2 +-
 .../views/account_payment_view.xml            | 12 +++++++++
 openerp/addons/base/res/res_bank.py           |  2 +-
 12 files changed, 54 insertions(+), 37 deletions(-)

diff --git a/addons/account/account.py b/addons/account/account.py
index 04bf1a02fe9d..665e1f985193 100644
--- a/addons/account/account.py
+++ b/addons/account/account.py
@@ -221,7 +221,7 @@ class AccountJournal(models.Model):
     sequence = fields.Integer(help='Used to order Journals in the dashboard view')
 
     #groups_id = fields.Many2many('res.groups', 'account_journal_group_rel', 'journal_id', 'group_id', string='Groups')
-    currency_id = fields.Many2one('res.currency', help='The currency used to enter statement', oldname='currency')
+    currency_id = fields.Many2one('res.currency', help='The currency used to enter statement', string="Currency", oldname='currency')
     company_id = fields.Many2one('res.company', string='Company', required=True, index=1, default=lambda self: self.env.user.company_id,
         help="Company related to this journal")
 
diff --git a/addons/account/account_payment.py b/addons/account/account_payment.py
index e2e64b884e23..99a201fb8f34 100644
--- a/addons/account/account_payment.py
+++ b/addons/account/account_payment.py
@@ -30,7 +30,7 @@ class account_abstract_payment(models.AbstractModel):
     _name = "account.abstract.payment"
     _description = "Contains the logic shared between models which allows to register payments"
 
-    payment_type = fields.Selection([('outbound', 'Send Money'), ('inbound', 'Receive Money')], default='outbound', required=True)
+    payment_type = fields.Selection([('outbound', 'Send Money'), ('inbound', 'Receive Money')], default='outbound', string='Payment Type', required=True)
     payment_method = fields.Many2one('account.payment.method', string='Payment Method', required=True)
     payment_method_code = fields.Char(related='payment_method.code',
         help="Technical field used to adapt the interface to the payment method selected.")
@@ -75,6 +75,7 @@ class account_abstract_payment(models.AbstractModel):
             return {'domain': {'payment_method': [('payment_type', '=', payment_type), ('id', 'in', payment_methods.ids)]}}
         return {}
 
+    # FIXME : If you change the currency_id, invoice_ids is mysteriously obliterated 
     @api.onchange('currency_id')
     def _onchange_currency_id(self):
         self._set_total_invoices_amount()
@@ -254,15 +255,19 @@ class account_payment(models.Model):
         res['domain']['journal_id'].append(('type', 'in', ('bank', 'cash')))
         return res
 
-    @api.onchange('invoice_ids')
-    def _onchange_invoice(self):
-        if len(self.invoice_ids) == 1:
-            self.communication = self.invoice_ids[0].reference
-            self.currency_id = self.invoice_ids[0].currency_id
-            self.payment_type = self.invoice_ids[0].type in ('out_invoice', 'in_refund') and 'inbound' or 'outbound'
-            self.partner_type = MAP_INVOICE_TYPE_PARTNER_TYPE[self.invoice_ids[0].type]
-            self.partner_id = self.invoice_ids[0].partner_id
-        self._set_total_invoices_amount()
+    @api.model
+    def default_get(self, fields):
+        rec = super(account_payment, self).default_get(fields)
+        invoice_ids = rec.get('invoice_ids') and rec['invoice_ids'][0][2] or None
+        if invoice_ids and len(invoice_ids) == 1:
+            invoice = self.env['account.invoice'].browse(invoice_ids)
+            rec['communication'] = rec.get('communication', invoice.reference)
+            rec['currency_id'] = rec.get('currency_id', invoice.currency_id.id)
+            rec['payment_type'] = rec.get('payment_type', invoice.type in ('out_invoice', 'in_refund') and 'inbound' or 'outbound')
+            rec['partner_type'] = rec.get('partner_type', MAP_INVOICE_TYPE_PARTNER_TYPE[invoice.type])
+            rec['partner_id'] = rec.get('partner_id', invoice.partner_id.id)
+            rec['amount'] = rec.get('amount', invoice.residual_signed)
+        return rec
 
     def _get_invoices(self):
         return self.invoice_ids
diff --git a/addons/account/views/account_bank_view.xml b/addons/account/views/account_bank_view.xml
index e895c1809954..d125251c360d 100644
--- a/addons/account/views/account_bank_view.xml
+++ b/addons/account/views/account_bank_view.xml
@@ -15,7 +15,7 @@
                 <group name="accounting" col="2" colspan="2" attrs="{'invisible': [('company_id','=', False)]}">
                     <separator string="Accounting Information" colspan="2"/>
                     <field name="journal_id"/>
-                    <field name="currency_id" groups="base.group_multi_currency"/>
+                    <field name="currency_id" groups="base.group_multi_currency" options="{'no_create': True}"/>
                 </group>
             </group>
         </field>
@@ -28,7 +28,7 @@
         <field name="inherit_id" ref="base.view_partner_bank_tree"/>
         <field name="arch" type="xml">
             <field name="acc_number" position="after">
-                <field name="currency_id" groups="base.group_multi_currency"/>
+                <field name="currency_id" options="{'no_create': True}" groups="base.group_multi_currency"/>
             </field>
         </field>
     </record>
diff --git a/addons/account/views/account_invoice_view.xml b/addons/account/views/account_invoice_view.xml
index 799f03fbbf09..8fb3c77c234f 100644
--- a/addons/account/views/account_invoice_view.xml
+++ b/addons/account/views/account_invoice_view.xml
@@ -245,7 +245,7 @@
                             <field name="date_invoice" string="Bill Date"/>
                             <field name="date_due"/>
                             <field name="move_name" invisible="1"/>
-                            <field name="currency_id" groups="base.group_multi_currency"/>
+                            <field name="currency_id" options="{'no_create': True}" groups="base.group_multi_currency"/>
                             <field name="company_currency_id" invisible="1"/>
                             <field name="check_total" groups="account.group_supplier_inv_check_total"/>
                         </group>
@@ -384,7 +384,7 @@
                             <field name="user_id" groups="base.group_user" context="{'default_groups_ref': ['base.group_user', 'base.group_partner_manager', 'account.group_account_invoice']}"/>
                             <label for="currency_id" groups="base.group_multi_currency"/>
                             <div groups="base.group_multi_currency">
-                                <field name="currency_id" class="oe_inline"/>
+                                <field name="currency_id" options="{'no_create': True}" class="oe_inline"/>
                                 <field name="company_currency_id" invisible="1"/>
                             </div>
                         </group>
diff --git a/addons/account/views/account_payment_view.xml b/addons/account/views/account_payment_view.xml
index 1e290ca63d3f..682d793fb4a0 100644
--- a/addons/account/views/account_payment_view.xml
+++ b/addons/account/views/account_payment_view.xml
@@ -32,8 +32,8 @@
                     <field name="payment_method"/>
                     <field name="partner_id" string="Supplier"/>
                     <field name="amount"/>
-                    <field name="state"/>
                     <field name="company_id" groups="base.group_multi_company"/>
+                    <field name="state"/>
                     <field name="currency_id" invisible="1"/>
                     <field name="partner_type" invisible="1"/>
                 </tree>
@@ -81,11 +81,11 @@
                     <sheet>
                         <div class="oe_right oe_button_box">
                             <button class="oe_inline oe_stat_button" name="button_journal_entries"
-                                    string="Journal Items" type="object"
+                                    string="Journal Items" type="object" groups="account.group_account_manager"
                                     attrs="{'invisible':[('move_line_ids','=',[])]}" icon="fa-bars"/>
                             <field name="move_line_ids" invisible="1"/>
                             <button class="oe_inline oe_stat_button" name="button_invoices"
-                                    string="Paid Invoices" type="object"
+                                    string="Invoices" type="object"
                                     attrs="{'invisible':[('has_invoices','=',False)]}" icon="fa-bars"/>
                             <field name="has_invoices" invisible="1"/>
                         </div>
@@ -101,12 +101,12 @@
                                 <field name="journal_id" widget="selection" attrs="{'readonly': [('state', '!=', 'draft')]}"/>
                                 <field name="destination_journal_id" widget="selection" attrs="{'required': [('payment_type', '=', 'transfer')], 'invisible': [('payment_type', '!=', 'transfer')], 'readonly': [('state', '!=', 'draft')]}"/>
                                 <field name="hide_payment_method" invisible="1"/>
-                                <field name="payment_method" widget="radio" attrs="{'invisible': [('hide_payment_method', '=', True)], 'readonly': [('state', '!=', 'draft')]}"/>
+                                <field name="payment_method" string=" " widget="radio" attrs="{'invisible': [('hide_payment_method', '=', True)], 'readonly': [('state', '!=', 'draft')]}"/>
                                 <field name="payment_method_code" invisible="1"/>
                                 <label for="amount"/>
                                 <div name="amount_div">
                                     <field name="amount" class="oe_inline" attrs="{'readonly': [('state', '!=', 'draft')]}"/>
-                                    <field name="currency_id" groups="base.group_multi_currency" class="oe_inline" attrs="{'readonly': [('state', '!=', 'draft')]}"/>
+                                    <field name="currency_id" options="{'no_create': True}" groups="base.group_multi_currency" class="oe_inline" attrs="{'readonly': [('state', '!=', 'draft')]}"/>
                                 </div>
                             </group>
                             <group>
@@ -190,7 +190,7 @@
                                 <label for="amount"/>
                                 <div name="amount_div">
                                     <field name="amount" class="oe_inline"/>
-                                    <field name="currency_id" groups="base.group_multi_currency" class="oe_inline"/>
+                                    <field name="currency_id" options="{'no_create': True}" groups="base.group_multi_currency" class="oe_inline" invisible="1"/><!-- FIXME see account_abstract_payment._onchange_currency_id -->
                                 </div>
                             </group>
                             <group>
diff --git a/addons/account/views/account_view.xml b/addons/account/views/account_view.xml
index ffcae4a161f6..061b046120ce 100644
--- a/addons/account/views/account_view.xml
+++ b/addons/account/views/account_view.xml
@@ -20,7 +20,7 @@
                                     <field name="user_type" widget="selection"/>
                                     <field name="tax_ids" widget="many2many_tags"/>
                                     <field name="company_id" options="{'no_create': True}" groups="base.group_multi_company"/>
-                                    <field name="currency_id" groups="base.group_multi_currency"/>
+                                    <field name="currency_id" options="{'no_create': True}" groups="base.group_multi_currency"/>
                                 </group>
                                 <group>
                                     <field name="tag_ids" widget="many2many_tags" domain="[('applicability', '!=', 'taxes')]" context="{'default_applicability': 'accounts'}"/>
@@ -105,8 +105,8 @@
                     <field name="code"/>
                     <field name="name"/>
                     <field name="user_type"/>
-                    <field name="currency_id" groups="base.group_multi_currency"/>
-                    <field name="company_id" groups="base.group_multi_company"/>
+                    <field name="currency_id" options="{'no_create': True}" groups="base.group_multi_currency"/>
+                    <field name="company_id" options="{'no_create': True}" groups="base.group_multi_company"/>
                 </tree>
             </field>
         </record>
@@ -254,10 +254,10 @@
                                 <field name="type"/>
                             </group>
                             <group>
-                                <field name="default_debit_account_id" domain="[('deprecated', '=', False)]"/>
-                                <field name="default_credit_account_id" domain="[('deprecated', '=', False)]"/>
-                                <field name="currency_id" groups="base.group_multi_currency"/>
-                                <field name="company_id" groups="base.group_multi_company"/>
+                                <field name="default_debit_account_id" domain="[('deprecated', '=', False)]" attrs="{'invisible': [('default_debit_account_id', '=', False), ('type', 'in', ['bank', 'cash'])]}"/>
+                                <field name="default_credit_account_id" domain="[('deprecated', '=', False)]" attrs="{'invisible': [('default_credit_account_id', '=', False), ('type', 'in', ['bank', 'cash'])]}"/>
+                                <field name="currency_id" options="{'no_create': True}" groups="base.group_multi_currency"/>
+                                <field name="company_id" options="{'no_create': True}" groups="base.group_multi_company"/>
                             </group>
                         </group>
                         <notebook>
@@ -496,7 +496,7 @@
                                     <field name="amount" attrs="{'readonly' : [('journal_entry_ids', '!=', [])] }"/>
                                     <field name="company_currency_id" invisible="1"/>
                                     <field name="amount_currency" groups="base.group_multi_currency" attrs="{'readonly' : [('journal_entry_ids', '!=', [])] }"/>
-                                    <field name="currency_id" groups="base.group_multi_currency" attrs="{'readonly' : [('journal_entry_ids', '!=', [])] }"/>
+                                    <field name="currency_id" options="{'no_create': True}" groups="base.group_multi_currency" attrs="{'readonly' : [('journal_entry_ids', '!=', [])] }"/>
                                     <field name="bank_account_id" groups="base.group_no_one" attrs="{'readonly' : [('journal_entry_ids', '!=', [])] }"
                                         domain="['|', ('partner_id', '=', partner_id), ('partner_id', '=', False)]"/>
                                 </tree>
@@ -1233,7 +1233,7 @@
                                     <field name="analytic_account_id" groups="analytic.group_analytic_accounting"/>
                                     <field name="amount_currency" groups="base.group_multi_currency"/>
                                     <field name="company_currency_id" invisible="1"/>
-                                    <field name="currency_id" groups="base.group_multi_currency"/>
+                                    <field name="currency_id" options="{'no_create': True}" groups="base.group_multi_currency"/>
                                     <field name="reconciled"/>
                                     <field name="debit" sum="Total Debit"/>
                                     <field name="credit" sum="Total Credit"/>
@@ -1412,7 +1412,7 @@
                         <field name="code"/>
                         <newline/>
                         <field name="user_type" widget="selection"/>
-                        <field name="currency_id" groups="base.group_multi_currency"/>
+                        <field name="currency_id" options="{'no_create': True}" groups="base.group_multi_currency"/>
                         <field name="tag_ids" domain="[('applicability', '!=', 'taxes')]" widget="many2many_tags" context="{'default_applicability': 'accounts'}"/>
                         <field name="reconcile"/>
                         <field name="chart_template_id"/>
diff --git a/addons/account/views/company_view.xml b/addons/account/views/company_view.xml
index 0d66f3f69915..b2cbadae98da 100644
--- a/addons/account/views/company_view.xml
+++ b/addons/account/views/company_view.xml
@@ -10,8 +10,8 @@
                     <field name="anglo_saxon_accounting"/>
                 </field>
                 <xpath expr="//group[@name='account_grp']" position="inside">
-                        <field name="bank_account_code_char"/>
-                        <field name="accounts_code_digits"/>
+                    <field name="bank_account_code_char" groups="base.group_no_one"/>
+                    <field name="accounts_code_digits" groups="base.group_no_one"/>
                 </xpath>
             </field>
         </record>
diff --git a/addons/account/views/res_config_view.xml b/addons/account/views/res_config_view.xml
index 579326a915f1..902760d5f30e 100644
--- a/addons/account/views/res_config_view.xml
+++ b/addons/account/views/res_config_view.xml
@@ -120,7 +120,7 @@
                             </div>
                             <div>
                                 <label for="currency_id"/>
-                                <field name="currency_id" class="oe_inline"/>
+                                <field name="currency_id" options="{'no_create': True}" class="oe_inline"/>
                             </div>
                             <div>
                                 <field name="tax_calculation_rounding_method" class="oe_inline" />
diff --git a/addons/account_bank_statement_import/wizard/journal_creation.xml b/addons/account_bank_statement_import/wizard/journal_creation.xml
index a834327e8ed1..4f9c53af1a27 100644
--- a/addons/account_bank_statement_import/wizard/journal_creation.xml
+++ b/addons/account_bank_statement_import/wizard/journal_creation.xml
@@ -11,7 +11,7 @@
                     <p>Just click OK to create the account/journal and finish the import. If this was a mistake, hit cancel to abort the import.</p>
                     <group>
                         <field name="name"/>
-                        <field name="currency_id"/>
+                        <field name="currency_id" options="{'no_create': True}"/>
                         <field name="account_number"/>
                     </group>
                     <footer>
diff --git a/addons/account_check_writing/data/check_writing.xml b/addons/account_check_writing/data/check_writing.xml
index 3326daab831c..8ee714432275 100644
--- a/addons/account_check_writing/data/check_writing.xml
+++ b/addons/account_check_writing/data/check_writing.xml
@@ -3,7 +3,7 @@
     <data noupdate="1">
 
         <record id="account_payment_method_check_writing" model="account.payment.method">
-            <field name="name">Check Writing</field>
+            <field name="name">Check</field>
             <field name="code">check_writing</field>
             <field name="payment_type">outbound</field>
         </record>
diff --git a/addons/account_check_writing/views/account_payment_view.xml b/addons/account_check_writing/views/account_payment_view.xml
index e0b983e8dfef..fccfa1255a95 100644
--- a/addons/account_check_writing/views/account_payment_view.xml
+++ b/addons/account_check_writing/views/account_payment_view.xml
@@ -48,5 +48,17 @@
                 </xpath>
             </field>
         </record>
+
+        <record id="view_payment_check_writing_search" model="ir.ui.view">
+            <field name="name">account.payment.check.writing.search</field>
+            <field name="model">account.payment</field>
+            <field name="inherit_id" ref="account.view_account_payment_search"/>
+            <field name="arch" type="xml">
+                <xpath expr="//filter[@name='state_sent']" position="before">
+                    <filter string="Checks To Send" domain="[('payment_method.code', '=', 'check_writing'), ('state','=','posted')]" name="checks_to_send"/>
+                </xpath>
+            </field>
+        </record>
+
     </data>
 </openerp>
diff --git a/openerp/addons/base/res/res_bank.py b/openerp/addons/base/res/res_bank.py
index 24604bad8977..b04dc37ef868 100644
--- a/openerp/addons/base/res/res_bank.py
+++ b/openerp/addons/base/res/res_bank.py
@@ -189,7 +189,7 @@ class res_partner_bank(osv.osv):
                 except Exception:
                     raise UserError(_("Bank account name formating error") + ': ' + _("Check the format_layout field set on the Bank Account Type."))
             if data.get('currency_id'):
-                currency_name = self.pool.get('res.currency').browse(cr, uid, data['currency_id'], context=context).name
+                currency_name = self.pool.get('res.currency').browse(cr, uid, data['currency_id'][0], context=context).name
                 name += ' (' + currency_name + ')'
             res.append((data.get('id', False), name))
         return res
-- 
GitLab