<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    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 import models, fields, api, _
from openerp.exceptions import Warning
from datetime import date

class current_account_imputation_wizard(models.TransientModel):
    _name = 'current.account.imputation.wizard'

    imputation_debit_lines = fields.One2many(
        'current.account.imputation.wizard.debit.line',
        'imputation_id',
        'Imputation debit lines'
    )

    imputation_credit_lines = fields.One2many(
        'current.account.imputation.wizard.credit.line',
        'imputation_id',
        'Imputation credit lines'
    )

    debit_residual = fields.Float(
        'Remaining to concile',
        readonly=True
    )

    credit_residual = fields.Float(
        'Total to concile',
        readonly=True
    )

    partner_id = fields.Many2one(
        'res.partner',
        'Partner'
    )

    wizard_type = fields.Selection(
        (('customer', 'Cliente'), ('supplier', 'Proveedor')),
        'Tipo de wizard'
    )

    @api.multi
    def concile_documents(self):

        self._create_moves(self._create_imputation())

        if self.partner_id.customer and not self.partner_id.supplier:

            return self.partner_id.get_customer_current_account()

        elif self.partner_id.supplier and not self.partner_id.customer:

            return self.partner_id.get_supplier_current_account()

        else:

            return {

                'name': _('Partner'),
                'views': [[False, "form"]],
                'res_id': self.partner_id.id,
                'res_model': 'res.partner',
                'type': 'ir.actions.act_window',
            }

    def _create_imputation(self):

        res_partner_imputation_proxy = self.env['res.partner.imputation']
        search_name = res_partner_imputation_proxy.search(
            [('partner_id', '=', self.partner_id.id)],
            limit=1,
            order='number desc'
        )
        number = search_name.number + 1 if search_name else 1
        name = 'IMPUTACION {}'.format(str(number).zfill(4))

        imputation = res_partner_imputation_proxy.create({
            'date': date.today(),
            'name': name,
            'partner_id': self.partner_id.id,
            'number': number,
            'state': 'valid',
        })

        return imputation

    @api.one
    def _create_moves(self, imputation):

        document_imputation_proxy = self.env['res.partner.document.imputation']
        current_account_type = self.wizard_type
        journal = self.env['account.journal'].search([('code', '=', 'IMP')])
        move_date = date.today()
        period = self.env['account.period'].find(move_date)
        date_formated = move_date.strftime('%d/%m/%Y')
        ref = 'Imputacion {} - {}'.format(self.partner_id.name.encode('utf-8'), date_formated)
        if not journal or len(journal) > 1:
            raise Warning('No se pudo generar el asiento debido a que no se encontro el diario de imputaciones')

        total_to_concile = 0
        for credit in self.imputation_credit_lines:
            total_to_concile += credit.amount_to_concile

        total_conciled = 0
        for debit in self.imputation_debit_lines:
            total_conciled += debit.amount_to_concile

        if total_to_concile != total_conciled:
            raise Warning('Todavia queda restante a imputar a los documentos')

        move_line_proxy = self.env['account.move.line']
        move_line_pool = self.pool.get('account.move.line')

        move = self.env['account.move'].create({<commit_msg>Ahora es un hotfix ;)<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    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 import models, fields, api, _
from openerp.exceptions import Warning
from datetime import date

class current_account_imputation_wizard(models.TransientModel):
    _name = 'current.account.imputation.wizard'

    imputation_debit_lines = fields.One2many(
        'current.account.imputation.wizard.debit.line',
        'imputation_id',
        'Imputation debit lines'
    )

    imputation_credit_lines = fields.One2many(
        'current.account.imputation.wizard.credit.line',
        'imputation_id',
        'Imputation credit lines'
    )

    debit_residual = fields.Float(
        'Remaining to concile',
        readonly=True
    )

    credit_residual = fields.Float(
        'Total to concile',
        readonly=True
    )

    partner_id = fields.Many2one(
        'res.partner',
        'Partner'
    )

    wizard_type = fields.Selection(
        (('customer', 'Cliente'), ('supplier', 'Proveedor')),
        'Tipo de wizard'
    )

    @api.multi
    def concile_documents(self):

        self._create_moves(self._create_imputation())

        if self.partner_id.customer and not self.partner_id.supplier:

            return self.partner_id.get_customer_current_account()

        elif self.partner_id.supplier and not self.partner_id.customer:

            return self.partner_id.get_supplier_current_account()

        else:

            return {

                'name': _('Partner'),
                'views': [[False, "form"]],
                'res_id': self.partner_id.id,
                'res_model': 'res.partner',
                'type': 'ir.actions.act_window',
            }

    def _create_imputation(self):

        res_partner_imputation_proxy = self.env['res.partner.imputation']
        search_name = res_partner_imputation_proxy.search(
            [('partner_id', '=', self.partner_id.id)],
            limit=1,
            order='number desc'
        )
        number = search_name.number + 1 if search_name else 1
        name = 'IMPUTACION {}'.format(str(number).zfill(4))

        imputation = res_partner_imputation_proxy.create({
            'date': date.today(),
            'name': name,
            'partner_id': self.partner_id.id,
            'number': number,
            'state': 'valid',
        })

        return imputation

    #Verifica si estan en un rango corto los numeros para validaciones
    def isclose(self, a, b, rel_tol=1e-09, abs_tol=0.0):
        return abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)

    @api.one
    def _create_moves(self, imputation):

        document_imputation_proxy = self.env['res.partner.document.imputation']
        current_account_type = self.wizard_type
        journal = self.env['account.journal'].search([('code', '=', 'IMP')])
        move_date = date.today()
        period = self.env['account.period'].find(move_date)
        date_formated = move_date.strftime('%d/%m/%Y')
        ref = 'Imputacion {} - {}'.format(self.partner_id.name.encode('utf-8'), date_formated)
        if not journal or len(journal) > 1:
            raise Warning('No se pudo generar el asiento debido a que no se encontro el diario de imputaciones')

        total_to_concile = 0
        for credit in self.imputation_credit_lines:
            total_to_concile += credit.amount_to_concile

        total_conciled = 0
        for debit in self.imputation_debit_lines:
            total_conciled += debit.amount_to_concile

        if not self.isclose(total_to_concile, total_conciled):
            raise Warning('Todavia queda restante a imputar a los documentos')

        move_line_proxy = self.env['account.move.line']
        move_line_pool = self.pool.get('account.move.line')

        move = self.env['account.move'].create({
{'fraction_non_alphanumeric': 0.13552951580809197, 'fraction_numerical': 0.003426929029405262, 'mean_word_length': 2.646513502619911, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6000523', 'n_tokens_mistral': 2781, 'n_tokens_neox': 2610, 'n_words': 639}