Ravi Gohil (OpenERP) has proposed merging 
lp:~openerp-dev/openobject-addons/6.1-opw-577100-rgo into 
lp:openobject-addons/6.1.

Requested reviews:
  Naresh(OpenERP) (nch-openerp)

For more details, see:
https://code.launchpad.net/~openerp-dev/openobject-addons/6.1-opw-577100-rgo/+merge/120981

Hello,

Validating the voucher after adding/altering Tax Amount in Sale 
Receipt/Purchase Receipt/Journal Voucher forms results in unbalanced journal 
items.

Steps to reproduce:
1) Create a record for Sale Receipt, add lines and select a Tax,
2) Alter the tax amount and validate the voucher,

You will have unbalanced entries in 'Journal Items' tab.

This fix fixes this issue by generating balanced entries for voucher.

Please review the fix.

Thanks.
-- 
https://code.launchpad.net/~openerp-dev/openobject-addons/6.1-opw-577100-rgo/+merge/120981
Your team OpenERP R&D Team is subscribed to branch 
lp:~openerp-dev/openobject-addons/6.1-opw-577100-rgo.
=== modified file 'account_voucher/account_voucher.py'
--- account_voucher/account_voucher.py	2012-08-21 12:23:47 +0000
+++ account_voucher/account_voucher.py	2012-08-23 12:53:09 +0000
@@ -1016,6 +1016,7 @@
         voucher_brw = self.pool.get('account.voucher').browse(cr, uid, voucher_id, context)
         ctx = context.copy()
         ctx.update({'date': voucher_brw.date})
+        voucher_total = 0.0
         for line in voucher_brw.line_ids:
             #create one move line per voucher line where amount is not 0.0
             if not line.amount:
@@ -1090,6 +1091,8 @@
                     foreign_currency_diff = sign * line.move_line_id.amount_residual_currency + amount_currency
 
             move_line['amount_currency'] = amount_currency
+            tax_amount_line = move_line.copy()
+            voucher_total += line.amount
             voucher_line = move_line_obj.create(cr, uid, move_line)
             rec_ids = [voucher_line, line.move_line_id.id]
 
@@ -1122,8 +1125,43 @@
             if line.move_line_id.id:
                 rec_lst_ids.append(rec_ids)
 
+        tax_amount_diff = 0.0
+        if voucher_brw.tax_id:
+            taxed_amount = tax_obj.compute_all(cr, uid, [voucher_brw.tax_id], voucher_total, 1.00, force_excluded=True).get('taxes')[0]['amount']
+            tax_amount_diff = voucher_brw.tax_amount - taxed_amount
+            if tax_amount_diff:
+                credit = tax_amount_line.get('credit') and tax_amount_diff or 0.0
+                debit = tax_amount_line.get('debit') and tax_amount_diff or 0.0
+                tax_amount = voucher_brw.tax_id.tax_sign * abs(tax_amount_diff)
+                if credit:
+                    debit = credit<0 and abs(credit)
+                    if debit:
+                        tax_amount = -1 * voucher_brw.tax_id.tax_sign * abs(tax_amount_diff)
+                        credit = 0.0
+                else:
+                    credit = debit<0 and abs(debit)
+                    if credit:
+                        tax_amount = -1 * voucher_brw.tax_id.tax_sign * abs(tax_amount_diff)
+                        debit = 0.0
+                tax_amount_line.update(account_tax_id=False, 
+                                       account_id=voucher_brw.tax_id.account_collected_id.id or tax_amount_line['account_id'], 
+                                       credit=credit, 
+                                       debit=debit, 
+                                       name=_('Tax difference for ') + voucher_brw.tax_id.name,
+                                       tax_code_id=voucher_brw.tax_id.tax_code_id.id,
+                                       tax_amount=tax_amount)
+                move_line_obj.create(cr, uid, tax_amount_line, context=context)
+        
         return (tot_line, rec_lst_ids)
 
+    def onchange_tax(self, cr, uid, ids, tax_amount, line_ids, tax_id, partner_id=False, context=None):
+        if not tax_id:
+            return {}
+        else:
+            values = self.onchange_price(cr, uid, ids, line_ids, tax_id, partner_id=partner_id, context=context)['value']
+            total_amount = values['amount'] - values['tax_amount'] + tax_amount
+            return {'value': {'amount': total_amount}}
+
     def writeoff_move_line_get(self, cr, uid, voucher_id, line_total, move_id, name, company_currency, current_currency, context=None):
         '''
         Set a dict to be use to create the writeoff move line.

=== modified file 'account_voucher/account_voucher_view.xml'
--- account_voucher/account_voucher_view.xml	2012-01-31 13:36:57 +0000
+++ account_voucher/account_voucher_view.xml	2012-08-23 12:53:09 +0000
@@ -74,7 +74,7 @@
                                 </group>
                                 <group col="4" colspan="1" attrs="{'invisible':[('type','in',['payment', 'receipt', False])]}">
                                     <separator string="Total" colspan="4"/>
-                                    <field name="tax_id" on_change="onchange_price(line_ids, tax_id, partner_id)" widget="selection"/><field name="tax_amount" nolabel="1"/><button type="object" icon="terp-stock_format-scientific" name="compute_tax" groups="base.group_extended" string="Compute Tax" attrs="{'invisible': [('state','!=','draft')]}"/>
+                                    <field name="tax_id" on_change="onchange_price(line_ids, tax_id, partner_id)" widget="selection"/><field name="tax_amount" on_change="onchange_tax(tax_amount, line_ids, tax_id, partner_id)" nolabel="1"/><button type="object" icon="terp-stock_format-scientific" name="compute_tax" groups="base.group_extended" string="Compute Tax" attrs="{'invisible': [('state','!=','draft')]}"/>
                                     <label colspan="1" string=""/><field name="amount" string="Total"/>
                                 </group>
                             </group>

=== modified file 'account_voucher/voucher_sales_purchase_view.xml'
--- account_voucher/voucher_sales_purchase_view.xml	2012-01-31 13:36:57 +0000
+++ account_voucher/voucher_sales_purchase_view.xml	2012-08-23 12:53:09 +0000
@@ -121,7 +121,7 @@
                                 </group>
                                 <group col="4" colspan="1">
                                     <separator string="Total" colspan="4"/>
-                                    <field name="tax_id" on_change="onchange_price(line_cr_ids, tax_id, partner_id)" widget="selection" domain="[('type_tax_use','in',('sale','all')), ('parent_id', '=', False)]"/><field name="tax_amount" nolabel="1"/><button type="object" icon="terp-stock_format-scientific" name="compute_tax" groups="base.group_extended" string="Compute Tax" attrs="{'invisible': [('state','!=','draft')]}"/>
+                                    <field name="tax_id" on_change="onchange_price(line_cr_ids, tax_id, partner_id)" widget="selection" domain="[('type_tax_use','in',('sale','all')), ('parent_id', '=', False)]"/><field name="tax_amount" on_change="onchange_tax(tax_amount, line_cr_ids, tax_id, partner_id)" nolabel="1"/><button type="object" icon="terp-stock_format-scientific" name="compute_tax" groups="base.group_extended" string="Compute Tax" attrs="{'invisible': [('state','!=','draft')]}"/>
                                     <label string="" colspan="1"/><field name="amount" string="Total"/>
                                 </group>
                             </group>
@@ -242,7 +242,7 @@
                                 </group>
                                 <group col="4" colspan="1">
                                     <separator string="Total" colspan="4"/>
-                                    <field name="tax_id" on_change="onchange_price(line_dr_ids, tax_id, partner_id)" widget="selection" domain="[('type_tax_use','in',('purchase','all')), ('parent_id', '=', False)]"/><field name="tax_amount" nolabel="1"/><button type="object" icon="terp-stock_format-scientific" name="compute_tax" string="Compute Tax" groups="base.group_extended" attrs="{'invisible': [('state','!=','draft')]}"/>
+                                    <field name="tax_id" on_change="onchange_price(line_dr_ids, tax_id, partner_id)" widget="selection" domain="[('type_tax_use','in',('purchase','all')), ('parent_id', '=', False)]"/><field name="tax_amount" on_change="onchange_tax(tax_amount, line_dr_ids, tax_id, partner_id)" nolabel="1"/><button type="object" icon="terp-stock_format-scientific" name="compute_tax" string="Compute Tax" groups="base.group_extended" attrs="{'invisible': [('state','!=','draft')]}"/>
                                     <label string="" colspan="1"/><field name="amount" string="Total"/>
                                 </group>
                             </group>

_______________________________________________
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