~vauxoo/addons-vauxoo/7.0-addons-vauxoo-send_email_add_follower-dev-julio

« back to all changes in this revision

Viewing changes to account_invoice_tax/account_invoice_tax.py

[REF][account_invoice_tax] Remove patch and add method override :(

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
#    info@vauxoo.com
8
8
############################################################################
9
9
#    Coded by: El Rodo (rodo@vauxoo.com)
 
10
#    Re-coded by: Moises Lopez (moises@vauxoo.com)
10
11
############################################################################
11
12
#
12
13
#    This program is free software: you can redistribute it and/or modify
26
27
 
27
28
from openerp.osv import osv, fields
28
29
 
 
30
class account_invoice(osv.Model):
 
31
    _inherit = 'account.invoice'
29
32
 
 
33
    def check_tax_lines(self, cr, uid, inv, compute_taxes, ait_obj):#method overriding
 
34
        company_currency = self.pool['res.company'].browse(cr, uid, inv.company_id.id).currency_id
 
35
        if not inv.tax_line:
 
36
            for tax in compute_taxes.values():
 
37
                ait_obj.create(cr, uid, tax)
 
38
        else:
 
39
            tax_key = []
 
40
            for tax in inv.tax_line:
 
41
                if tax.manual:
 
42
                    continue
 
43
                #start custom  change
 
44
                #key = (tax.tax_code_id.id, tax.base_code_id.id, tax.account_id.id, tax.account_analytic_id.id)
 
45
                key = (tax.tax_id and tax.tax_id.id or False)
 
46
                #end custom change
 
47
                tax_key.append(key)
 
48
                if not key in compute_taxes:
 
49
                    raise osv.except_osv(_('Warning!'), _('Global taxes defined, but they are not in invoice lines !'))
 
50
                base = compute_taxes[key]['base']
 
51
                if abs(base - tax.base) > company_currency.rounding:
 
52
                    raise osv.except_osv(_('Warning!'), _('Tax base different!\nClick on compute to update the tax base.'))
 
53
            for key in compute_taxes:
 
54
                if not key in tax_key:
 
55
                    raise osv.except_osv(_('Warning!'), _('Taxes are missing!\nClick on compute button.'))
 
56
                    
30
57
class account_invoice_tax(osv.Model):
31
58
    _inherit = 'account.invoice.tax'
 
59
    
32
60
    _columns = {
33
 
        'tax_id': fields.many2one('account.tax', 'Tax', help="Tax"),
 
61
        'tax_id': fields.many2one('account.tax', 'Tax', required=False, ondelete='set null',
 
62
            help="Tax relation to original tax, to be able to take off all data from invoices."),
34
63
    }
 
64
    
 
65
    def compute(self, cr, uid, invoice_id, context=None):#method overriding
 
66
        tax_grouped = {}
 
67
        tax_obj = self.pool.get('account.tax')
 
68
        cur_obj = self.pool.get('res.currency')
 
69
        inv = self.pool.get('account.invoice').browse(cr, uid, invoice_id, context=context)
 
70
        cur = inv.currency_id
 
71
        company_currency = self.pool['res.company'].browse(cr, uid, inv.company_id.id).currency_id.id
 
72
        for line in inv.invoice_line:
 
73
            for tax in tax_obj.compute_all(cr, uid, line.invoice_line_tax_id, (line.price_unit* (1-(line.discount or 0.0)/100.0)), line.quantity, line.product_id, inv.partner_id)['taxes']:
 
74
                val={}
 
75
                val['invoice_id'] = inv.id
 
76
                val['name'] = tax['name']
 
77
                val['amount'] = tax['amount']
 
78
                val['manual'] = False
 
79
                val['sequence'] = tax['sequence']
 
80
                val['base'] = cur_obj.round(cr, uid, cur, tax['price_unit'] * line['quantity'])
 
81
                #start custom change
 
82
                val['tax_id'] = tax['id']
 
83
                #end custom change
 
84
 
 
85
                if inv.type in ('out_invoice','in_invoice'):
 
86
                    val['base_code_id'] = tax['base_code_id']
 
87
                    val['tax_code_id'] = tax['tax_code_id']
 
88
                    val['base_amount'] = cur_obj.compute(cr, uid, inv.currency_id.id, company_currency, val['base'] * tax['base_sign'], context={'date': inv.date_invoice or time.strftime('%Y-%m-%d')}, round=False)
 
89
                    val['tax_amount'] = cur_obj.compute(cr, uid, inv.currency_id.id, company_currency, val['amount'] * tax['tax_sign'], context={'date': inv.date_invoice or time.strftime('%Y-%m-%d')}, round=False)
 
90
                    val['account_id'] = tax['account_collected_id'] or line.account_id.id
 
91
                    val['account_analytic_id'] = tax['account_analytic_collected_id']
 
92
                else:
 
93
                    val['base_code_id'] = tax['ref_base_code_id']
 
94
                    val['tax_code_id'] = tax['ref_tax_code_id']
 
95
                    val['base_amount'] = cur_obj.compute(cr, uid, inv.currency_id.id, company_currency, val['base'] * tax['ref_base_sign'], context={'date': inv.date_invoice or time.strftime('%Y-%m-%d')}, round=False)
 
96
                    val['tax_amount'] = cur_obj.compute(cr, uid, inv.currency_id.id, company_currency, val['amount'] * tax['ref_tax_sign'], context={'date': inv.date_invoice or time.strftime('%Y-%m-%d')}, round=False)
 
97
                    val['account_id'] = tax['account_paid_id'] or line.account_id.id
 
98
                    val['account_analytic_id'] = tax['account_analytic_paid_id']
 
99
                #start custom change
 
100
                #key = (val['tax_code_id'], val['base_code_id'], val['account_id'], val['account_analytic_id'])
 
101
                key = (val['tax_id'])
 
102
                #end custom change
 
103
                if not key in tax_grouped:
 
104
                    tax_grouped[key] = val
 
105
                else:
 
106
                    tax_grouped[key]['amount'] += val['amount']
 
107
                    tax_grouped[key]['base'] += val['base']
 
108
                    tax_grouped[key]['base_amount'] += val['base_amount']
 
109
                    tax_grouped[key]['tax_amount'] += val['tax_amount']
 
110
 
 
111
        for t in tax_grouped.values():
 
112
            t['base'] = cur_obj.round(cr, uid, cur, t['base'])
 
113
            t['amount'] = cur_obj.round(cr, uid, cur, t['amount'])
 
114
            t['base_amount'] = cur_obj.round(cr, uid, cur, t['base_amount'])
 
115
            t['tax_amount'] = cur_obj.round(cr, uid, cur, t['tax_amount'])
 
116
        return tax_grouped