<commit_before># -*- encoding: utf-8 -*-
###############################################################################
#                                                                             #
# Copyright (C) 2013  <NAME> - Akretion                                  #
#                                                                             #
#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/>.        #
###############################################################################

import time

from openerp.osv import orm, fields
from openerp.addons import decimal_precision as dp

from .l10n_br_account_product import (
    PRODUCT_FISCAL_TYPE,
    PRODUCT_FISCAL_TYPE_DEFAULT)
from .product import PRODUCT_ORIGIN
from .sped.nfe.validator import txt


class AccountInvoice(orm.Model):
    _inherit = 'account.invoice'

    def _amount_all(self, cr, uid, ids, name, args, context=None):
        result = {}
        for invoice in self.browse(cr, uid, ids, context=context):
            result[invoice.id] = {
                'amount_untaxed': 0.0,
                'amount_tax': 0.0,
                'amount_tax_discount': 0.0,
                'amount_total': 0.0,
                'icms_base': 0.0,
                'icms_value': 0.0,
                'icms_st_base': 0.0,
                'icms_st_value': 0.0,
                'ipi_base': 0.0,
                'ipi_value': 0.0,
                'pis_base': 0.0,
                'pis_value': 0.0,
                'cofins_base': 0.0,
                'cofins_value': 0.0,
                'ii_value': 0.0,
                'amount_insurance': 0.0,
                'amount_freight': 0.0,
                'amount_costs': 0.0,
                'amount_gross': 0.0,
                'amount_discount': 0.0,
            }
            for line in invoice.invoice_line:
                result[invoice.id]['amount_untaxed'] += line.price_total
                if line.icms_cst_id.code not in ('101','102','201','202','300','500'):
                    result[invoice.id]['icms_base'] += line.icms_base
                    result[invoice.id]['icms_value'] += line.icms_value
                result[invoice.id]['icms_st_base'] += line.icms_st_base
                result[invoice.id]['icms_st_value'] += line.icms_st_value
                result[invoice.id]['ipi_base'] += line.ipi_base
                result[invoice.id]['ipi_value'] += line.ipi_value
                result[invoice.id]['pis_base'] += line.pis_base
                result[invoice.id]['pis_value'] += line.pis_value
                result[invoice.id]['cofins_base'] += line.cofins_base
                result[invoice.id]['cofins_value'] += line.cofins_value
                result[invoice.id]['ii_value'] += line.ii_value
                result[invoice.id]['amount_insurance'] += line.insurance_value
                result[invoice.id]['amount_freight'] += line.freight_value
                result[invoice.id]['amount_costs'] += line.other_costs_value
                result[invoice.id]['amount_gross'] += line.price_gross
                result[invoice.id]['amount_discount'] += line.discount_value

            for invoice_tax in invoice.tax_line:
                if not invoice_tax.tax_code_id.tax_discount:
                    result[invoice.id]['amount_tax'] += invoice_tax.amount

            result[invoice.id]['amount_total'] = result[invoice.id]['amount_tax'] + result[invoice.id]['amount_untaxed']
        return result

    def _get_invoice_line(self, cr, uid, ids, context=None):
        result = {}
        for line in self.pool.get('account.invoice.line').browse(
            cr, uid, ids, context=context):
            result[line.invoice_id.id] = True
        return list(result.keys())

    def _get_cfops(self, cr, uid, ids, name, arg, context=None):
        result = {}
        for invoice in self.browse(cr, uid, ids, context=context):
            result[invoice.id] = []
            new_ids = []
            for line in invoice.invoice_line:
                if line.cfop_id and not line.cfop_id.id in new_ids:
                    new_ids.append(line.cfop_id.id)
            new_ids.sort()
            result[invoice.id] = new_ids
        return result

    def _get_invoice_tax(self, cr, uid, ids, context=None):
        result = {}
        for tax in self.pool.get('account.invoice.tax').browse(
            cr, uid, ids, context=context):
            result[tax.invoice_id.id] = True
        return list(result.keys())

    _columns = {
        'partner_shipping_id': fields.many2one(
            'res.partner', 'Delivery Address',
            readonly=True, required=True,
            states={'draft': [('readonly', False)]},
            help="Delivery address for current sales order."),
        'state': fields.selection([
            ('draft', 'Draft'),
            ('proforma', 'Pro-forma'),
            ('proforma2', 'Pro-forma'),
            ('sefaz_export', 'Enviar para Receita'),
            ('sefaz_exception', 'Erro de autorização da Receita'),
            ('open', 'Open'),
            ('paid', 'Paid'),
            ('cancel', 'Cancelled')
            ], 'State', select=True, readonly=True,
            help=' * The \'Draft\' state is used when a user is encoding a new and unconfirmed Invoice. \
            \n* The \'Pro-forma\' when invoice is in Pro-forma state,invoice does not have an invoice number. \
            \n* The \'Open\' state is used when user create invoice,a invoice number is generated.Its in open state till user does not pay invoice. \
            \n* The \'Paid\' state is set automatically when invoice is paid.\
            \n* The \'sefaz_out\' Gerado aquivo de exportação para sistema daReceita.\
            \n* The \'sefaz_aut\' Recebido arquivo de autolização da Receita.\
            \n* The \'Cancelled\' state is used when user cancel invoice.'),
        'fiscal_type': fields.selection(PRODUCT_FISCAL_TYPE, 'Tipo Fiscal', required=True),
        'partner_shipping_id': fields.many2one(
            'res.partner', 'Endereço de Entrega', readonly=True,
            states={'draft': [('readonly', False)]},
            help="Shipping address for current sales order."),
        'nfe_purpose': fields.selection(
            [('1', 'Normal'),
             ('2', 'Complementar'),
             ('3', 'Ajuste')], 'Finalidade da Emissão', readonly=True,
            states={'draft': [('readonly', False)]}),
        'nfe_access_key': fields.char(
            'Chave de Acesso NFE', size=44,
            readonly=True, states={'draft': [('readonly', False)]}),
        'nfe_protocol_number': fields.char(
            'Protocolo', size=44,
            readonly=True, states={'draft': [('readonly', False)]}),        
        'nfe_status': fields.char('Status na Sefaz', size=44, readonly=True),
        'nfe_date': fields.datetime('Data do Status NFE', readonly=True),<commit_msg>[FIX] Tamanho do campo<commit_after># -*- encoding: utf-8 -*-
###############################################################################
#                                                                             #
# Copyright (C) 2013  <NAME> - Akretion                                  #
#                                                                             #
#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/>.        #
###############################################################################

import time

from openerp.osv import orm, fields
from openerp.addons import decimal_precision as dp

from .l10n_br_account_product import (
    PRODUCT_FISCAL_TYPE,
    PRODUCT_FISCAL_TYPE_DEFAULT)
from .product import PRODUCT_ORIGIN
from .sped.nfe.validator import txt


class AccountInvoice(orm.Model):
    _inherit = 'account.invoice'

    def _amount_all(self, cr, uid, ids, name, args, context=None):
        result = {}
        for invoice in self.browse(cr, uid, ids, context=context):
            result[invoice.id] = {
                'amount_untaxed': 0.0,
                'amount_tax': 0.0,
                'amount_tax_discount': 0.0,
                'amount_total': 0.0,
                'icms_base': 0.0,
                'icms_value': 0.0,
                'icms_st_base': 0.0,
                'icms_st_value': 0.0,
                'ipi_base': 0.0,
                'ipi_value': 0.0,
                'pis_base': 0.0,
                'pis_value': 0.0,
                'cofins_base': 0.0,
                'cofins_value': 0.0,
                'ii_value': 0.0,
                'amount_insurance': 0.0,
                'amount_freight': 0.0,
                'amount_costs': 0.0,
                'amount_gross': 0.0,
                'amount_discount': 0.0,
            }
            for line in invoice.invoice_line:
                result[invoice.id]['amount_untaxed'] += line.price_total
                if line.icms_cst_id.code not in ('101','102','201','202','300','500'):
                    result[invoice.id]['icms_base'] += line.icms_base
                    result[invoice.id]['icms_value'] += line.icms_value
                result[invoice.id]['icms_st_base'] += line.icms_st_base
                result[invoice.id]['icms_st_value'] += line.icms_st_value
                result[invoice.id]['ipi_base'] += line.ipi_base
                result[invoice.id]['ipi_value'] += line.ipi_value
                result[invoice.id]['pis_base'] += line.pis_base
                result[invoice.id]['pis_value'] += line.pis_value
                result[invoice.id]['cofins_base'] += line.cofins_base
                result[invoice.id]['cofins_value'] += line.cofins_value
                result[invoice.id]['ii_value'] += line.ii_value
                result[invoice.id]['amount_insurance'] += line.insurance_value
                result[invoice.id]['amount_freight'] += line.freight_value
                result[invoice.id]['amount_costs'] += line.other_costs_value
                result[invoice.id]['amount_gross'] += line.price_gross
                result[invoice.id]['amount_discount'] += line.discount_value

            for invoice_tax in invoice.tax_line:
                if not invoice_tax.tax_code_id.tax_discount:
                    result[invoice.id]['amount_tax'] += invoice_tax.amount

            result[invoice.id]['amount_total'] = result[invoice.id]['amount_tax'] + result[invoice.id]['amount_untaxed']
        return result

    def _get_invoice_line(self, cr, uid, ids, context=None):
        result = {}
        for line in self.pool.get('account.invoice.line').browse(
            cr, uid, ids, context=context):
            result[line.invoice_id.id] = True
        return list(result.keys())

    def _get_cfops(self, cr, uid, ids, name, arg, context=None):
        result = {}
        for invoice in self.browse(cr, uid, ids, context=context):
            result[invoice.id] = []
            new_ids = []
            for line in invoice.invoice_line:
                if line.cfop_id and not line.cfop_id.id in new_ids:
                    new_ids.append(line.cfop_id.id)
            new_ids.sort()
            result[invoice.id] = new_ids
        return result

    def _get_invoice_tax(self, cr, uid, ids, context=None):
        result = {}
        for tax in self.pool.get('account.invoice.tax').browse(
            cr, uid, ids, context=context):
            result[tax.invoice_id.id] = True
        return list(result.keys())

    _columns = {
        'partner_shipping_id': fields.many2one(
            'res.partner', 'Delivery Address',
            readonly=True, required=True,
            states={'draft': [('readonly', False)]},
            help="Delivery address for current sales order."),
        'state': fields.selection([
            ('draft', 'Draft'),
            ('proforma', 'Pro-forma'),
            ('proforma2', 'Pro-forma'),
            ('sefaz_export', 'Enviar para Receita'),
            ('sefaz_exception', 'Erro de autorização da Receita'),
            ('open', 'Open'),
            ('paid', 'Paid'),
            ('cancel', 'Cancelled')
            ], 'State', select=True, readonly=True,
            help=' * The \'Draft\' state is used when a user is encoding a new and unconfirmed Invoice. \
            \n* The \'Pro-forma\' when invoice is in Pro-forma state,invoice does not have an invoice number. \
            \n* The \'Open\' state is used when user create invoice,a invoice number is generated.Its in open state till user does not pay invoice. \
            \n* The \'Paid\' state is set automatically when invoice is paid.\
            \n* The \'sefaz_out\' Gerado aquivo de exportação para sistema daReceita.\
            \n* The \'sefaz_aut\' Recebido arquivo de autolização da Receita.\
            \n* The \'Cancelled\' state is used when user cancel invoice.'),
        'fiscal_type': fields.selection(PRODUCT_FISCAL_TYPE, 'Tipo Fiscal', required=True),
        'partner_shipping_id': fields.many2one(
            'res.partner', 'Endereço de Entrega', readonly=True,
            states={'draft': [('readonly', False)]},
            help="Shipping address for current sales order."),
        'nfe_purpose': fields.selection(
            [('1', 'Normal'),
             ('2', 'Complementar'),
             ('3', 'Ajuste')], 'Finalidade da Emissão', readonly=True,
            states={'draft': [('readonly', False)]}),
        'nfe_access_key': fields.char(
            'Chave de Acesso NFE', size=44,
            readonly=True, states={'draft': [('readonly', False)]}),
        'nfe_protocol_number': fields.char(
            'Protocolo', size=15,
            readonly=True, states={'draft': [('readonly', False)]}),        
        'nfe_status': fields.char('Status na Sefaz', size=44, readonly=True),
        'nfe_date': fields.datetime('Data do Status NFE', readonly=True),
{'fraction_non_alphanumeric': 0.14001160317153355, 'fraction_numerical': 0.010056081995745503, 'mean_word_length': 1.8665927568366594, 'pattern_counts': {'":': 0, '<': 7, '<?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': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3821196', 'n_tokens_mistral': 4590, 'n_tokens_neox': 4335, 'n_words': 1066}