<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2011-2012 Domsense s.r.l. (<http://www.domsense.com>).
#    Copyright (C) 2014 Agile Business Group sagl (<http://www.agilebg.com>)
#
#    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 <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import orm, fields
from openerp.tools.translate import _
from openerp import api


class AccountInvoice(orm.Model):

    def _get_vat_on_payment(self, cr, uid, context=None):
        return self.pool.get('res.users').browse(
            cr, uid, uid, context).company_id.vat_on_payment

    def _set_vat_on_payment_account(self, cr, uid, line_tuple, context=None):
        acc_pool = self.pool.get('account.account')
        account = acc_pool.browse(
            cr, uid, line_tuple[2]['account_id'], context=context)
        if account.type not in ['receivable', 'payable']:
            if not account.vat_on_payment_related_account_id:
                raise orm.except_orm(
                    _('Error'),
                    _("The invoice is 'VAT on payment' but "
                      "account %s does not have a related shadow "
                      "account")
                    % account.name)
            line_tuple[2]['real_account_id'] = line_tuple[
                2]['account_id']
            line_tuple[2]['account_id'] = (
                account.vat_on_payment_related_account_id.id)
        return line_tuple

    def _set_vat_on_payment_tax_code(self, cr, uid, line_tuple, context=None):
        tax_code_pool = self.pool.get('account.tax.code')
        tax_code = tax_code_pool.browse(
            cr, uid, line_tuple[2]['tax_code_id'], context=context)
        if not tax_code.vat_on_payment_related_tax_code_id:
            raise orm.except_orm(
                _('Error'),
                _("The invoice is 'VAT on payment' but "
                  "tax code %s does not have a related shadow "
                  "tax code")
                % tax_code.name)
        line_tuple[2]['real_tax_code_id'] = line_tuple[
            2]['tax_code_id']
        line_tuple[2]['tax_code_id'] = (
            tax_code.vat_on_payment_related_tax_code_id.id)
        return line_tuple

    def finalize_invoice_move_lines(self, cr, uid, invoice_browse, move_lines):
        """
        Use shadow accounts for journal entry to be generated, according to
        account and tax code related records
        """
        move_lines = super(AccountInvoice, self).finalize_invoice_move_lines(
            cr, uid, invoice_browse, move_lines)
        context = self.pool['res.users'].context_get(cr, uid)
        new_move_lines = []
        for line_tuple in move_lines:
            if invoice_browse.vat_on_payment:
                if line_tuple[2].get('account_id', False):
                    line_tuple = self._set_vat_on_payment_account(
                        cr, uid, line_tuple, context=context)
                if line_tuple[2].get('tax_code_id', False):
                    line_tuple = self._set_vat_on_payment_tax_code(<commit_msg>finalize_invoice_move_lines: use classic params<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2011-2012 Domsense s.r.l. (<http://www.domsense.com>).
#    Copyright (C) 2014 Agile Business Group sagl (<http://www.agilebg.com>)
#
#    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 <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import orm, fields
from openerp.tools.translate import _
from openerp import api


class AccountInvoice(orm.Model):

    def _get_vat_on_payment(self, cr, uid, context=None):
        return self.pool.get('res.users').browse(
            cr, uid, uid, context).company_id.vat_on_payment

    def _set_vat_on_payment_account(self, cr, uid, line_tuple, context=None):
        acc_pool = self.pool.get('account.account')
        account = acc_pool.browse(
            cr, uid, line_tuple[2]['account_id'], context=context)
        if account.type not in ['receivable', 'payable']:
            if not account.vat_on_payment_related_account_id:
                raise orm.except_orm(
                    _('Error'),
                    _("The invoice is 'VAT on payment' but "
                      "account %s does not have a related shadow "
                      "account")
                    % account.name)
            line_tuple[2]['real_account_id'] = line_tuple[
                2]['account_id']
            line_tuple[2]['account_id'] = (
                account.vat_on_payment_related_account_id.id)
        return line_tuple

    def _set_vat_on_payment_tax_code(self, cr, uid, line_tuple, context=None):
        tax_code_pool = self.pool.get('account.tax.code')
        tax_code = tax_code_pool.browse(
            cr, uid, line_tuple[2]['tax_code_id'], context=context)
        if not tax_code.vat_on_payment_related_tax_code_id:
            raise orm.except_orm(
                _('Error'),
                _("The invoice is 'VAT on payment' but "
                  "tax code %s does not have a related shadow "
                  "tax code")
                % tax_code.name)
        line_tuple[2]['real_tax_code_id'] = line_tuple[
            2]['tax_code_id']
        line_tuple[2]['tax_code_id'] = (
            tax_code.vat_on_payment_related_tax_code_id.id)
        return line_tuple

    def finalize_invoice_move_lines(self, cr, uid, ids, move_lines, context):
        """
        Use shadow accounts for journal entry to be generated, according to
        account and tax code related records
        """
        move_lines = super(AccountInvoice, self).finalize_invoice_move_lines(
            cr, uid, ids, move_lines, context)
        assert len(ids) == 1
        invoice_browse = self.browse(cr, uid, ids, context=context)
        context = self.pool['res.users'].context_get(cr, uid)
        new_move_lines = []
        for line_tuple in move_lines:
            if invoice_browse.vat_on_payment:
                if line_tuple[2].get('account_id', False):
                    line_tuple = self._set_vat_on_payment_account(
                        cr, uid, line_tuple, context=context)
                if line_tuple[2].get('tax_code_id', False):
                    line_tuple = self._set_vat_on_payment_tax_code(
{'fraction_non_alphanumeric': 0.12983531214094216, 'fraction_numerical': 0.006255585344057194, 'mean_word_length': 2.5272399819900944, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '811026', 'n_tokens_mistral': 2328, 'n_tokens_neox': 2187, 'n_words': 610}