Rucha (Open ERP) has proposed merging 
lp:~openerp-dev/openobject-addons/trunk-multicurrency-gain-loss-rpa into 
lp:openobject-addons.

Requested reviews:
  qdp (OpenERP) (qdp)

For more details, see:
https://code.launchpad.net/~openerp-dev/openobject-addons/trunk-multicurrency-gain-loss-rpa/+merge/80332

To develop:
http://cdn.qbgarage.com/gallery/qb2009-multicurr/multicurr_unrealizedgainsrept.jpg?9d7bd4


Create a new action with a domain on account.account: currency_id<>False
  with a tree view the looks like the above screenshot


You need to add on the account.account:
  - a related on the currency rate "Exchange Rate"
- foreign balance: fields.function that computes the sum(amount_currency) of 
this account
  - "Adjusted Balance" = fields.function that computes foreign balance * 
exchange rate
- gain_loss_unrealized = fields.function that compute adjusted balance - balance

Menu:
  Accounting / Reporting / Generic Reporting / Multi-Currencies / Unrealized 
Gains and losses 
-- 
https://code.launchpad.net/~openerp-dev/openobject-addons/trunk-multicurrency-gain-loss-rpa/+merge/80332
Your team OpenERP R&D Team is subscribed to branch 
lp:~openerp-dev/openobject-addons/trunk-multicurrency-gain-loss-rpa.
=== modified file 'account/account.py'
--- account/account.py	2011-10-19 08:14:03 +0000
+++ account/account.py	2011-10-25 13:07:26 +0000
@@ -243,7 +243,8 @@
             'balance': "COALESCE(SUM(l.debit),0) " \
                        "- COALESCE(SUM(l.credit), 0) as balance",
             'debit': "COALESCE(SUM(l.debit), 0) as debit",
-            'credit': "COALESCE(SUM(l.credit), 0) as credit"
+            'credit': "COALESCE(SUM(l.credit), 0) as credit",
+            'foreign_balance': "COALESCE(SUM(l.amount_currency), 0) as foreign_balance",
         }
         #get all the necessary accounts
         children_and_consolidated = self._get_children_and_consol(cr, uid, ids, context=context)
@@ -270,7 +271,7 @@
             # ON l.account_id = tmp.id
             # or make _get_children_and_consol return a query and join on that
             request = ("SELECT l.account_id as id, " +\
-                       ', '.join(map(mapping.__getitem__, field_names)) +
+                       ', '.join(map(mapping.__getitem__, mapping.keys())) +
                        " FROM account_move_line l" \
                        " WHERE l.account_id IN %s " \
                             + filters +
@@ -289,7 +290,7 @@
             sums = {}
             currency_obj = self.pool.get('res.currency')
             while brs:
-                current = brs[0]
+                current = brs.pop(0)
 #                can_compute = True
 #                for child in current.child_id:
 #                    if child.id not in sums:
@@ -299,7 +300,6 @@
 #                        except ValueError:
 #                            brs.insert(0, child)
 #                if can_compute:
-                brs.pop(0)
                 for fn in field_names:
                     sums.setdefault(current.id, {})[fn] = accounts.get(current.id, {}).get(fn, 0.0)
                     for child in current.child_id:
@@ -307,6 +307,16 @@
                             sums[current.id][fn] += sums[child.id][fn]
                         else:
                             sums[current.id][fn] += currency_obj.compute(cr, uid, child.company_id.currency_id.id, current.company_id.currency_id.id, sums[child.id][fn], context=context)
+
+                # as we have to relay on values computed before this is calculated separately than previous fields
+                if current.currency_id and current.exchange_rate and \
+                            ('adjusted_balance' in field_names or 'unrealized_gain_loss' in field_names):
+                    # Computing Adjusted Balance and Unrealized Gains and losses
+                    # Adjusted Balance = Foreign Balance / Exchange Rate
+                    # Unrealized Gains and losses = Adjusted Balance - Balance
+                    adj_bal = sums[current.id].get('foreign_balance', 0.0) / current.exchange_rate
+                    sums[current.id].update({'adjusted_balance': adj_bal, 'unrealized_gain_loss': adj_bal - sums[current.id].get('balance', 0.0)})
+
             for id in ids:
                 res[id] = sums.get(id, null_result)
         else:
@@ -418,7 +428,14 @@
         'balance': fields.function(__compute, digits_compute=dp.get_precision('Account'), string='Balance', multi='balance'),
         'credit': fields.function(__compute, fnct_inv=_set_credit_debit, digits_compute=dp.get_precision('Account'), string='Credit', multi='balance'),
         'debit': fields.function(__compute, fnct_inv=_set_credit_debit, digits_compute=dp.get_precision('Account'), string='Debit', multi='balance'),
+        'foreign_balance': fields.function(__compute, digits_compute=dp.get_precision('Account'), string='Foreign Balance', multi='balance',
+                                           help="Total amount (in Secondary currency) for transactions held in secondary currency for this account."),
+        'adjusted_balance': fields.function(__compute, digits_compute=dp.get_precision('Account'), string='Adjusted Balance', multi='balance', 
+                                            help="Total amount (in Company currency) for transactions held in secondary currency for this account."),
+        'unrealized_gain_loss': fields.function(__compute, digits_compute=dp.get_precision('Account'), string='Unrealized Gain or Loss', multi='balance', 
+                                                help="Value of Loss or Gain due to changes in exchange rate when doing multi-currency transactions."),
         'reconcile': fields.boolean('Allow Reconciliation', help="Check this box if this account allows reconciliation of journal items."),
+        'exchange_rate': fields.related('currency_id', 'rate', type='float', string='Exchange Rate'),
         'shortcut': fields.char('Shortcut', size=12),
         'tax_ids': fields.many2many('account.tax', 'account_account_tax_default_rel',
             'account_id', 'tax_id', 'Default Taxes'),

=== modified file 'account/account_menuitem.xml'
--- account/account_menuitem.xml	2011-09-09 11:14:08 +0000
+++ account/account_menuitem.xml	2011-10-25 13:07:26 +0000
@@ -43,6 +43,7 @@
 
         <menuitem id="menu_finance_statistic_report_statement" name="Statistic Reports" parent="menu_finance_reporting" sequence="300"/>
         <menuitem id="next_id_22" name="Partners" parent="menu_finance_generic_reporting" sequence="1"/>
+        <menuitem id="menu_multi_currency" name="Multi-Currencies" parent="menu_finance_generic_reporting" sequence="10"/>
         <menuitem
             parent="account.menu_finance_legal_statement"
             id="final_accounting_reports"

=== modified file 'account/account_view.xml'
--- account/account_view.xml	2011-10-19 12:20:53 +0000
+++ account/account_view.xml	2011-10-25 13:07:26 +0000
@@ -295,6 +295,40 @@
             <field name="domain">[('parent_id','=',False)]</field>
         </record>
 
+         <record id="view_account_gain_loss_tree" model="ir.ui.view">
+           <field name="name">Unrealized Gain or Loss</field>
+            <field name="model">account.account</field>
+            <field name="type">tree</field>
+            <field name="arch" type="xml">
+                <tree string="Unrealized Gains and losses">
+                    <field name="code"/>
+                    <field name="name"/>
+                    <field name="currency_id"/>
+                    <field name="exchange_rate"/>
+                    <field name="foreign_balance"/>
+                    <field name="adjusted_balance"/>
+                    <field name="balance"/>
+                    <field name="unrealized_gain_loss"/>
+                </tree>
+            </field>
+        </record>
+
+          <record id="action_account_gain_loss" model="ir.actions.act_window">
+            <field name="name">Unrealized Gain or Loss</field>
+            <field name="res_model">account.account</field>
+            <field name="view_type">form</field>
+            <field name="view_mode">tree</field>
+            <field name="view_id" ref="view_account_gain_loss_tree"/>
+            <field name="domain">[('currency_id','!=',False)]</field>
+            <field name="help">When doing multi-currency transactions, you may loose or gain some amount due to changes in exchange rate, Unrealized Gain or Loss shows this amount for accounts (having secondary currency).</field>
+        </record>
+
+        <menuitem
+            name="Unrealized Gain or Loss"
+            action="action_account_gain_loss"
+            id="menu_unrealized_gains_losses"
+            parent="account.menu_multi_currency"/>
+
         <!--
         Journal
 

_______________________________________________
Mailing list: https://launchpad.net/~openerp-dev-gtk
Post to     : [email protected]
Unsubscribe : https://launchpad.net/~openerp-dev-gtk
More help   : https://help.launchpad.net/ListHelp

Reply via email to