27
28
from openerp.osv import osv, fields
30
class account_invoice(osv.Model):
31
_inherit = 'account.invoice'
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
36
for tax in compute_taxes.values():
37
ait_obj.create(cr, uid, tax)
40
for tax in inv.tax_line:
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)
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.'))
30
57
class account_invoice_tax(osv.Model):
31
58
_inherit = 'account.invoice.tax'
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."),
65
def compute(self, cr, uid, invoice_id, context=None):#method overriding
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)
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']:
75
val['invoice_id'] = inv.id
76
val['name'] = tax['name']
77
val['amount'] = tax['amount']
79
val['sequence'] = tax['sequence']
80
val['base'] = cur_obj.round(cr, uid, cur, tax['price_unit'] * line['quantity'])
82
val['tax_id'] = tax['id']
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']
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']
100
#key = (val['tax_code_id'], val['base_code_id'], val['account_id'], val['account_analytic_id'])
101
key = (val['tax_id'])
103
if not key in tax_grouped:
104
tax_grouped[key] = val
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']
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'])