From b45d21491a97446c8bb892b3892486efab83126a Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Mon, 29 Jul 2013 15:19:47 +0200 Subject: [PATCH 01/10] [ADD] Basic tax processing from the bank statement line --- account_bank_statement_tax/__init__.py | 1 + account_bank_statement_tax/__openerp__.py | 40 +++++++ account_bank_statement_tax/model/__init__.py | 2 + .../model/account_bank_statement.py | 102 ++++++++++++++++++ .../model/account_bank_statement_line.py | 34 ++++++ .../view/account_bank_statement.xml | 20 ++++ 6 files changed, 199 insertions(+) create mode 100644 account_bank_statement_tax/__init__.py create mode 100644 account_bank_statement_tax/__openerp__.py create mode 100644 account_bank_statement_tax/model/__init__.py create mode 100644 account_bank_statement_tax/model/account_bank_statement.py create mode 100644 account_bank_statement_tax/model/account_bank_statement_line.py create mode 100644 account_bank_statement_tax/view/account_bank_statement.xml diff --git a/account_bank_statement_tax/__init__.py b/account_bank_statement_tax/__init__.py new file mode 100644 index 000000000..16e8b082f --- /dev/null +++ b/account_bank_statement_tax/__init__.py @@ -0,0 +1 @@ +import model diff --git a/account_bank_statement_tax/__openerp__.py b/account_bank_statement_tax/__openerp__.py new file mode 100644 index 000000000..b488d090d --- /dev/null +++ b/account_bank_statement_tax/__openerp__.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Copyright (C) 2012 - 2013 Therp BV (). +# All Rights Reserved +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +{ + 'name': 'Apply a tax on bank statement lines', + 'version': '0.1', + 'license': 'AGPL-3', + 'author': 'Therp BV', + 'website': 'https://launchpad.net/banking-addons', + 'category': 'Banking addons', + 'depends': [ + 'account', + ], + 'data': [ + 'view/account_bank_statement.xml', + ], + 'description': ''' +Allow an (inclusive) tax to be set on a bank statement line. When the +statement is configured, the tax will be processed like on a move line. + ''', + 'installable': True, +} diff --git a/account_bank_statement_tax/model/__init__.py b/account_bank_statement_tax/model/__init__.py new file mode 100644 index 000000000..0a522c87a --- /dev/null +++ b/account_bank_statement_tax/model/__init__.py @@ -0,0 +1,2 @@ +import account_bank_statement_line +import account_bank_statement diff --git a/account_bank_statement_tax/model/account_bank_statement.py b/account_bank_statement_tax/model/account_bank_statement.py new file mode 100644 index 000000000..9f6930b21 --- /dev/null +++ b/account_bank_statement_tax/model/account_bank_statement.py @@ -0,0 +1,102 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Copyright (C) 2012 - 2013 Therp BV (). +# All Rights Reserved +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +from openerp.osv import orm +from openerp.tools import ustr + + +class AccountBankStatement(orm.Model): + _inherit = 'account.bank.statement' + + def get_tax_move_lines( + self, cr, uid, st_line, defaults, + company_currency_id, context=None): + """ + Process inclusive taxes on bank statement lines. + + @param st_line: browse record of the statement line + @param defaults: dictionary of default move line values. Usually + the same as the originating move line. + + return one or more serialized tax move lines and a set of values to + update the originating move line with, containing the new amount. + + TODO: apply fiscal position, multicurrency + """ + + if not st_line.tax_id: + return False, False + tax_obj = self.pool.get('account.tax') + move_lines = [] + update_move_line = {} + import pdb + pdb.set_trace() + base_amount = -defaults['credit'] or defaults['debit'] + computed_taxes = tax_obj.compute_all( + cr, uid, [st_line.tax_id], base_amount, 1.00) + + for tax in computed_taxes['taxes']: + if tax['tax_code_id']: + if not update_move_line.get('tax_code_id'): + update_move_line['tax_code_id'] = tax['base_code_id'] + update_move_line['tax_amount'] = tax['base_sign'] * abs( + computed_taxes.get('total', 0.0)) + # As the tax is inclusive, we need to correct the amount on the + # original move line + amount = computed_taxes.get('total', 0.0) + update_move_line['credit'] = ((amount < 0) and -amount) or 0.0 + update_move_line['debit'] = ((amount > 0) and amount) or 0.0 + + move_lines.append({ + 'move_id': defaults['move_id'], + 'name': defaults.get('name', '') + ' ' + ustr(tax['name'] or ''), + 'date': defaults.get('date', False), + 'partner_id': defaults.get('partner_id', False), + 'ref': defaults.get('ref', False), + 'statement_id': defaults.get('statement_id'), + 'tax_code_id': tax['tax_code_id'], + 'tax_amount': tax['tax_sign'] * abs(tax.get('amount', 0.0)), + 'account_id': tax.get('account_collected_id', defaults['account_id']), + 'credit': tax['amount'] < 0 and - tax['amount'] or 0.0, + 'debit': tax['amount'] > 0 and tax['amount'] or 0.0, + 'account_id': tax.get('account_collected_id', defaults['account_id']), + }) + + return move_lines, update_move_line + + def _prepare_counterpart_move_line( + self, cr, uid, st_line, move_id, amount, company_currency_id, + context=None): + """ + Overload of the original method from the account module. Create + the tax move lines. + """ + res = super(AccountBankStatement, self)._prepare_counterpart_move_line( + cr, uid, st_line, move_id, amount, company_currency_id, + context=context) + if st_line.tax_id: + tax_move_lines, counterpart_update_vals = self.get_tax_move_lines( + cr, uid, st_line, res, company_currency_id, context=context) + res.update(counterpart_update_vals) + for tax_move_line in tax_move_lines: + self.pool.get('account.move.line').create( + cr, uid, tax_move_line, context=context) + return res diff --git a/account_bank_statement_tax/model/account_bank_statement_line.py b/account_bank_statement_tax/model/account_bank_statement_line.py new file mode 100644 index 000000000..4d965cdef --- /dev/null +++ b/account_bank_statement_tax/model/account_bank_statement_line.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Copyright (C) 2012 - 2013 Therp BV (). +# All Rights Reserved +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +from openerp.osv import orm, fields + + +class AccountBankStatementLine(orm.Model): + _inherit = 'account.bank.statement.line' + + _columns = { + 'tax_id': fields.many2one( + 'account.tax', 'Tax', + domain=[('price_include','=', True)], + help="Apply an (inclusive) tax from the bank statement line", + ), + } diff --git a/account_bank_statement_tax/view/account_bank_statement.xml b/account_bank_statement_tax/view/account_bank_statement.xml new file mode 100644 index 000000000..2065e6d7d --- /dev/null +++ b/account_bank_statement_tax/view/account_bank_statement.xml @@ -0,0 +1,20 @@ + + + + + Add tax to the embedded bank statement line form + + account.bank.statement + + + + + + + + From b514b7c2c45be50eb9d121881063d2674a18d45d Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Mon, 9 Sep 2013 20:44:42 +0200 Subject: [PATCH 02/10] [FIX] Remove debug statement --- account_bank_statement_tax/model/account_bank_statement.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/account_bank_statement_tax/model/account_bank_statement.py b/account_bank_statement_tax/model/account_bank_statement.py index 9f6930b21..0010c046f 100644 --- a/account_bank_statement_tax/model/account_bank_statement.py +++ b/account_bank_statement_tax/model/account_bank_statement.py @@ -47,8 +47,6 @@ class AccountBankStatement(orm.Model): tax_obj = self.pool.get('account.tax') move_lines = [] update_move_line = {} - import pdb - pdb.set_trace() base_amount = -defaults['credit'] or defaults['debit'] computed_taxes = tax_obj.compute_all( cr, uid, [st_line.tax_id], base_amount, 1.00) From 72a05b9f1f55f172da708958f00827a8804ad3c4 Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Sat, 5 Oct 2013 15:34:08 +0200 Subject: [PATCH 03/10] [FIX] Don't allow selection of child taxes --- account_bank_statement_tax/view/account_bank_statement.xml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/account_bank_statement_tax/view/account_bank_statement.xml b/account_bank_statement_tax/view/account_bank_statement.xml index 2065e6d7d..63d2e6e08 100644 --- a/account_bank_statement_tax/view/account_bank_statement.xml +++ b/account_bank_statement_tax/view/account_bank_statement.xml @@ -9,9 +9,7 @@ From 63a3bf45ae7c90c458c04f000b368add108239b1 Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Sat, 5 Oct 2013 15:49:56 +0200 Subject: [PATCH 04/10] [FIX] core method naming confusion --- account_bank_statement_tax/model/account_bank_statement.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/account_bank_statement_tax/model/account_bank_statement.py b/account_bank_statement_tax/model/account_bank_statement.py index 0010c046f..0fab1ccbe 100644 --- a/account_bank_statement_tax/model/account_bank_statement.py +++ b/account_bank_statement_tax/model/account_bank_statement.py @@ -80,14 +80,14 @@ class AccountBankStatement(orm.Model): return move_lines, update_move_line - def _prepare_counterpart_move_line( + def _prepare_bank_move_line( self, cr, uid, st_line, move_id, amount, company_currency_id, context=None): """ Overload of the original method from the account module. Create the tax move lines. """ - res = super(AccountBankStatement, self)._prepare_counterpart_move_line( + res = super(AccountBankStatement, self)._prepare_bank_move_line( cr, uid, st_line, move_id, amount, company_currency_id, context=context) if st_line.tax_id: From e117d1115205ae205ea8d982f201ff3a8cc4e85f Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Sat, 5 Oct 2013 16:52:58 +0200 Subject: [PATCH 05/10] [ADD] Support for fiscal positions [FIX] Remove faulty calls to abs() --- .../model/account_bank_statement.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/account_bank_statement_tax/model/account_bank_statement.py b/account_bank_statement_tax/model/account_bank_statement.py index 0fab1ccbe..1b6c640f1 100644 --- a/account_bank_statement_tax/model/account_bank_statement.py +++ b/account_bank_statement_tax/model/account_bank_statement.py @@ -48,14 +48,25 @@ class AccountBankStatement(orm.Model): move_lines = [] update_move_line = {} base_amount = -defaults['credit'] or defaults['debit'] + tax_obj = self.pool.get('account.tax') + + fiscal_position = ( + st_line.partner_id.property_account_position + if st_line.partner_id and + st_line.partner_id.property_account_position + else False) + tax_ids = self.pool.get('account.fiscal.position').map_tax( + cr, uid, fiscal_position, [st_line.tax_id]) + taxes = tax_obj.browse(cr, uid, tax_ids, context=context) + computed_taxes = tax_obj.compute_all( - cr, uid, [st_line.tax_id], base_amount, 1.00) + cr, uid, taxes, base_amount, 1.00) for tax in computed_taxes['taxes']: if tax['tax_code_id']: if not update_move_line.get('tax_code_id'): update_move_line['tax_code_id'] = tax['base_code_id'] - update_move_line['tax_amount'] = tax['base_sign'] * abs( + update_move_line['tax_amount'] = tax['base_sign'] * ( computed_taxes.get('total', 0.0)) # As the tax is inclusive, we need to correct the amount on the # original move line @@ -71,7 +82,7 @@ class AccountBankStatement(orm.Model): 'ref': defaults.get('ref', False), 'statement_id': defaults.get('statement_id'), 'tax_code_id': tax['tax_code_id'], - 'tax_amount': tax['tax_sign'] * abs(tax.get('amount', 0.0)), + 'tax_amount': tax['tax_sign'] * tax.get('amount', 0.0), 'account_id': tax.get('account_collected_id', defaults['account_id']), 'credit': tax['amount'] < 0 and - tax['amount'] or 0.0, 'debit': tax['amount'] > 0 and tax['amount'] or 0.0, From 0ebc565cd03462abb3cb83408ba0bb21feb06e51 Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Sat, 5 Oct 2013 16:55:37 +0200 Subject: [PATCH 06/10] [ADD] Attribution --- account_bank_statement_tax/__openerp__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/account_bank_statement_tax/__openerp__.py b/account_bank_statement_tax/__openerp__.py index b488d090d..a7643f093 100644 --- a/account_bank_statement_tax/__openerp__.py +++ b/account_bank_statement_tax/__openerp__.py @@ -35,6 +35,8 @@ 'description': ''' Allow an (inclusive) tax to be set on a bank statement line. When the statement is configured, the tax will be processed like on a move line. + +This module is co-funded by BAS Solutions. ''', 'installable': True, } From c83789e19b96fe91e552748f4da4b0e4dc2dac83 Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Sat, 5 Oct 2013 16:58:18 +0200 Subject: [PATCH 07/10] [FIX] Remove todo --- account_bank_statement_tax/model/account_bank_statement.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/account_bank_statement_tax/model/account_bank_statement.py b/account_bank_statement_tax/model/account_bank_statement.py index 1b6c640f1..b6c940aed 100644 --- a/account_bank_statement_tax/model/account_bank_statement.py +++ b/account_bank_statement_tax/model/account_bank_statement.py @@ -38,8 +38,6 @@ class AccountBankStatement(orm.Model): return one or more serialized tax move lines and a set of values to update the originating move line with, containing the new amount. - - TODO: apply fiscal position, multicurrency """ if not st_line.tax_id: From 74b96a834bdc2f63bb91031d3d0cc4da68e5d17f Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Sun, 6 Oct 2013 20:13:07 +0200 Subject: [PATCH 08/10] [FIX] Description --- account_bank_statement_tax/__openerp__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/account_bank_statement_tax/__openerp__.py b/account_bank_statement_tax/__openerp__.py index a7643f093..695421ed1 100644 --- a/account_bank_statement_tax/__openerp__.py +++ b/account_bank_statement_tax/__openerp__.py @@ -34,7 +34,8 @@ ], 'description': ''' Allow an (inclusive) tax to be set on a bank statement line. When the -statement is configured, the tax will be processed like on a move line. +statement is confirmed, the tax will be processed like a tax set on a +move line. This module is co-funded by BAS Solutions. ''', From 1eec20504cbdce4eaf94e35609e5f1143056448c Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Sun, 6 Oct 2013 20:55:54 +0200 Subject: [PATCH 09/10] [RFR] Don't repeat assignment within loop --- .../model/account_bank_statement.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/account_bank_statement_tax/model/account_bank_statement.py b/account_bank_statement_tax/model/account_bank_statement.py index b6c940aed..d0d734b15 100644 --- a/account_bank_statement_tax/model/account_bank_statement.py +++ b/account_bank_statement_tax/model/account_bank_statement.py @@ -66,11 +66,6 @@ class AccountBankStatement(orm.Model): update_move_line['tax_code_id'] = tax['base_code_id'] update_move_line['tax_amount'] = tax['base_sign'] * ( computed_taxes.get('total', 0.0)) - # As the tax is inclusive, we need to correct the amount on the - # original move line - amount = computed_taxes.get('total', 0.0) - update_move_line['credit'] = ((amount < 0) and -amount) or 0.0 - update_move_line['debit'] = ((amount > 0) and amount) or 0.0 move_lines.append({ 'move_id': defaults['move_id'], @@ -87,6 +82,13 @@ class AccountBankStatement(orm.Model): 'account_id': tax.get('account_collected_id', defaults['account_id']), }) + if move_lines: + # As the tax is inclusive, we need to correct the amount on the + # original move line + amount = computed_taxes.get('total', 0.0) + update_move_line['credit'] = ((amount < 0) and -amount) or 0.0 + update_move_line['debit'] = ((amount > 0) and amount) or 0.0 + return move_lines, update_move_line def _prepare_bank_move_line( From ebb2bb5c2b94d29119adb9a220f5520568b181f4 Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Sun, 6 Oct 2013 21:03:02 +0200 Subject: [PATCH 10/10] [RFR] Revert previous commit, problem between keyboard and chair --- .../model/account_bank_statement.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/account_bank_statement_tax/model/account_bank_statement.py b/account_bank_statement_tax/model/account_bank_statement.py index d0d734b15..b6c940aed 100644 --- a/account_bank_statement_tax/model/account_bank_statement.py +++ b/account_bank_statement_tax/model/account_bank_statement.py @@ -66,6 +66,11 @@ class AccountBankStatement(orm.Model): update_move_line['tax_code_id'] = tax['base_code_id'] update_move_line['tax_amount'] = tax['base_sign'] * ( computed_taxes.get('total', 0.0)) + # As the tax is inclusive, we need to correct the amount on the + # original move line + amount = computed_taxes.get('total', 0.0) + update_move_line['credit'] = ((amount < 0) and -amount) or 0.0 + update_move_line['debit'] = ((amount > 0) and amount) or 0.0 move_lines.append({ 'move_id': defaults['move_id'], @@ -82,13 +87,6 @@ class AccountBankStatement(orm.Model): 'account_id': tax.get('account_collected_id', defaults['account_id']), }) - if move_lines: - # As the tax is inclusive, we need to correct the amount on the - # original move line - amount = computed_taxes.get('total', 0.0) - update_move_line['credit'] = ((amount < 0) and -amount) or 0.0 - update_move_line['debit'] = ((amount > 0) and amount) or 0.0 - return move_lines, update_move_line def _prepare_bank_move_line(