~akretion-team/banking-addons/70-sepa-direct-debit

« back to all changes in this revision

Viewing changes to account_bank_statement_tax/model/account_bank_statement.py

  • Committer: Alexis de Lattre
  • Date: 2013-11-07 11:54:02 UTC
  • mfrom: (177.1.25 banking-addons-70)
  • Revision ID: alexis@via.ecp.fr-20131107115402-4fvdugkobsl711ow
[MERGE] with main branch revno 202

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    Copyright (C) 2012 - 2013 Therp BV (<http://therp.nl>).
 
5
#    All Rights Reserved
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU Affero General Public License as
 
9
#    published by the Free Software Foundation, either version 3 of the
 
10
#    License, or (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU Affero General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU Affero General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
##############################################################################
 
21
 
 
22
from openerp.osv import orm
 
23
from openerp.tools import ustr
 
24
 
 
25
 
 
26
class AccountBankStatement(orm.Model):
 
27
    _inherit = 'account.bank.statement'
 
28
 
 
29
    def get_tax_move_lines(
 
30
            self, cr, uid, st_line, defaults,
 
31
            company_currency_id, context=None):
 
32
        """
 
33
        Process inclusive taxes on bank statement lines.
 
34
 
 
35
        @param st_line: browse record of the statement line
 
36
        @param defaults: dictionary of default move line values. Usually
 
37
        the same as the originating move line.
 
38
 
 
39
        return one or more serialized tax move lines and a set of values to 
 
40
        update the originating move line with, containing the new amount.
 
41
        """
 
42
 
 
43
        if not st_line.tax_id:
 
44
            return False, False
 
45
        tax_obj = self.pool.get('account.tax')
 
46
        move_lines = []
 
47
        update_move_line = {}
 
48
        base_amount = -defaults['credit'] or defaults['debit']
 
49
        tax_obj = self.pool.get('account.tax')
 
50
 
 
51
        fiscal_position = (
 
52
            st_line.partner_id.property_account_position
 
53
            if st_line.partner_id and 
 
54
                st_line.partner_id.property_account_position
 
55
            else False)
 
56
        tax_ids = self.pool.get('account.fiscal.position').map_tax(
 
57
            cr, uid, fiscal_position, [st_line.tax_id])
 
58
        taxes = tax_obj.browse(cr, uid, tax_ids, context=context)
 
59
 
 
60
        computed_taxes = tax_obj.compute_all(
 
61
            cr, uid, taxes, base_amount, 1.00)
 
62
 
 
63
        for tax in computed_taxes['taxes']:
 
64
            if tax['tax_code_id']:
 
65
                if not update_move_line.get('tax_code_id'):
 
66
                    update_move_line['tax_code_id'] = tax['base_code_id']
 
67
                    update_move_line['tax_amount'] = tax['base_sign'] * (
 
68
                        computed_taxes.get('total', 0.0))
 
69
                    # As the tax is inclusive, we need to correct the amount on the
 
70
                    # original move line
 
71
                    amount = computed_taxes.get('total', 0.0)
 
72
                    update_move_line['credit'] = ((amount < 0) and -amount) or 0.0
 
73
                    update_move_line['debit'] = ((amount > 0) and amount) or 0.0
 
74
 
 
75
                move_lines.append({
 
76
                    'move_id': defaults['move_id'],
 
77
                    'name': defaults.get('name', '') + ' ' + ustr(tax['name'] or ''),
 
78
                    'date': defaults.get('date', False),
 
79
                    'partner_id': defaults.get('partner_id', False),
 
80
                    'ref': defaults.get('ref', False),
 
81
                    'statement_id': defaults.get('statement_id'),
 
82
                    'tax_code_id': tax['tax_code_id'],
 
83
                    'tax_amount': tax['tax_sign'] * tax.get('amount', 0.0),
 
84
                    'account_id': tax.get('account_collected_id', defaults['account_id']),
 
85
                    'credit': tax['amount'] < 0 and - tax['amount'] or 0.0,
 
86
                    'debit': tax['amount'] > 0 and tax['amount'] or 0.0,
 
87
                    'account_id': tax.get('account_collected_id', defaults['account_id']),
 
88
                    })
 
89
 
 
90
        return move_lines, update_move_line
 
91
 
 
92
    def _prepare_bank_move_line(
 
93
            self, cr, uid, st_line, move_id, amount, company_currency_id,
 
94
            context=None):
 
95
        """
 
96
        Overload of the original method from the account module. Create
 
97
        the tax move lines.
 
98
        """
 
99
        res = super(AccountBankStatement, self)._prepare_bank_move_line(
 
100
            cr, uid, st_line, move_id, amount, company_currency_id,
 
101
            context=context)
 
102
        if st_line.tax_id:
 
103
            tax_move_lines, counterpart_update_vals = self.get_tax_move_lines(
 
104
                cr, uid, st_line, res, company_currency_id, context=context)
 
105
            res.update(counterpart_update_vals)
 
106
            for tax_move_line in tax_move_lines:
 
107
                self.pool.get('account.move.line').create(
 
108
                    cr, uid, tax_move_line, context=context)
 
109
        return res