Reviewers: ,


Please review this at http://codereview.tryton.org/396005/

Affected files:
  M invoice.py
  M invoice.xml


Index: invoice.py
===================================================================

--- a/invoice.py
+++ b/invoice.py
@@ -428,7 +428,7 @@
                 res['untaxed_amount'] += line.get('amount') or 0
                 with Transaction().set_context(**context):
                     taxes = tax_obj.compute(line.get('taxes', []),
-                            line.get('unit_price', Decimal('0.0')),
+                            line.get('unit_price') or Decimal('0.0'),
                             line.get('quantity', 0.0))
                 for tax in taxes:
                     key, val = self._compute_tax(tax,
@@ -446,7 +446,7 @@
         tax_keys = []
         for tax in vals.get('taxes', []):
             if tax.get('manual', False):
-                res['tax_amount'] += tax.get('amount', Decimal('0.0'))
+                res['tax_amount'] += tax.get('amount') or Decimal('0.0')
                 continue
             key = (tax.get('base_code'), tax.get('base_sign'),
                     tax.get('tax_code'), tax.get('tax_sign'),
@@ -459,7 +459,7 @@
             if currency:
                 if not currency_obj.is_zero(currency,
                         computed_taxes[key]['base'] - \
-                                tax.get('base', Decimal('0.0'))):
+                                (tax.get('base') or Decimal('0.0'))):
                     res['tax_amount'] += computed_taxes[key]['amount']
                     res['taxes'].setdefault('update', [])
                     res['taxes']['update'].append({
@@ -468,10 +468,10 @@
                         'base': computed_taxes[key]['base'],
                         })
                 else:
-                    res['tax_amount'] += tax.get('amount', Decimal('0.0'))
+ res['tax_amount'] += tax.get('amount') or Decimal('0.0')
             else:
                 if computed_taxes[key]['base'] - \
-                        tax.get('base', Decimal('0.0')) != Decimal('0.0'):
+ (tax.get('base') or Decimal('0.0')) != Decimal('0.0'):
                     res['tax_amount'] += computed_taxes[key]['amount']
                     res['taxes'].setdefault('update', [])
                     res['taxes']['update'].append({
@@ -480,7 +480,7 @@
                         'base': computed_taxes[key]['base'],
                         })
                 else:
-                    res['tax_amount'] += tax.get('amount', Decimal('0.0'))
+ res['tax_amount'] += tax.get('amount') or Decimal('0.0')
         for key in computed_taxes:
             if key not in tax_keys:
                 res['tax_amount'] += computed_taxes[key]['amount']
@@ -1970,7 +1970,9 @@
     base = fields.Numeric('Base', required=True,
         digits=(16, Eval('_parent_invoice', {}).get('currency_digits', 2)))
     amount = fields.Numeric('Amount', required=True,
-        digits=(16, Eval('_parent_invoice', {}).get('currency_digits', 2)))
+        digits=(16, Eval('_parent_invoice', {}).get('currency_digits', 2)),
+        on_change_with=['tax', 'base', 'amount', 'manual'],
+        depends=['tax', 'base', 'manual'])
     manual = fields.Boolean('Manual')
     base_code = fields.Many2One('account.tax.code', 'Base Code',
         domain=[
@@ -1982,7 +1984,13 @@
('company', '=', Eval('_parent_invoice', {}).get('company', 0)),
             ])
     tax_sign = fields.Numeric('Tax Sign', digits=(2, 0), required=True)
-    tax = fields.Many2One('account.tax', 'Tax')
+    tax = fields.Many2One('account.tax', 'Tax',
+        states={
+            'readonly': ~Eval('manual', False),
+            },
+        on_change=['tax', '_parent_invoice.party',
+            '_parent_invoice.type'],
+        depends=['manual'])

     def __init__(self):
         super(InvoiceTax, self).__init__()
@@ -2023,6 +2031,46 @@
     def default_tax_sign(self):
         return Decimal('1')

+    def on_change_tax(self, values):
+        pool = Pool()
+        tax_obj = pool.get('account.tax')
+        invoice_obj = pool.get('account.invoice')
+        changes = {}
+        if values.get('tax'):
+            invoice_values = dict(
+                (f[16:], v) for f, v in values.iteritems()
+                if f.startswith('_parent_invoice.'))
+            context = invoice_obj.get_tax_context(invoice_values)
+            with Transaction().set_context(**context):
+                tax = tax_obj.browse(values['tax'])
+            changes['description'] = tax.description
+ invoice_type = values.get('_parent_invoice.type', 'out_invoice')
+            if invoice_type in ('out_invoice', 'in_invoice'):
+                changes['base_code'] = tax.invoice_base_code.id
+                changes['base_sign'] = tax.invoice_base_sign
+                changes['tax_code'] = tax.invoice_tax_code.id
+                changes['tax_sign'] = tax.invoice_tax_sign
+                changes['account'] = tax.invoice_account.id
+            else:
+                changes['base_code'] = tax.credit_note_base_code.id
+                changes['base_sign'] = tax.credit_note_base_sign
+                changes['tax_code'] = tax.credit_note_tax_code.id
+                changes['tax_sign'] = tax.credit_note_tax_sign
+                changes['account'] = tax.credit_note_account.id
+        return changes
+
+    def on_change_with_amount(self, values):
+        pool = Pool()
+        tax_obj = pool.get('account.tax')
+        if values.get('tax') and values.get('manual', False):
+            tax_id = values['tax']
+            base = values.get('base') or Decimal(0)
+            for tax in tax_obj.compute([tax_id], base, 1):
+                if (tax['tax'].id == tax_id
+                        and tax['base'] == values.get('base')):
+                    return tax['amount']
+        return values.get('amount')
+
     def check_modify(self, ids):
         '''
         Check if the taxes can be modified

Index: invoice.xml
===================================================================

--- a/invoice.xml
+++ b/invoice.xml
@@ -655,6 +655,8 @@
                 <form string="Invoice Tax">
                     <label name="invoice"/>
                     <field name="invoice" colspan="3"/>
+                    <label name="tax"/>
+                    <field name="tax" colspan="3"/>
                     <label name="description"/>
                     <field name="description"/>
                     <label name="sequence"/>
@@ -677,7 +679,6 @@
                         <label name="base_sign"/>
                         <field name="base_sign"/>
                     </group>
-                    <field name="tax" colspan="4" invisible="1"/>
                 </form>
                 ]]>
             </field>
@@ -690,12 +691,12 @@
                 <![CDATA[
                 <tree string="Invoice Taxes">
                     <field name="invoice"/>
+                    <field name="tax"/>
                     <field name="description"/>
                     <field name="account"/>
                     <field name="manual"/>
                     <field name="base"/>
                     <field name="amount"/>
-                    <field name="tax" tree_invisible="1"/>
                 </tree>
                 ]]>
             </field>
@@ -718,6 +719,7 @@
                     <field name="tax_code" tree_invisible="1"/>
                     <field name="tax_sign" tree_invisible="1"/>
                     <field name="account" tree_invisible="1"/>
+                    <field name="tax" tree_invisible="1"/>
                 </tree>
                 ]]>
             </field>



--
[email protected] mailing list

Reply via email to