~mileo/openerp.pt-br-localiz/openerp-pt-br-frete-em-linhas

« back to all changes in this revision

Viewing changes to l10n_br_account/account_invoice.py

  • Committer: Renato Lima
  • Date: 2012-11-08 12:31:59 UTC
  • Revision ID: renatonlima@gmail.com-20121108123159-qux9m5dbenw92bnk
l10n_br_account: implementado nos documentos fiscais, as mudanças de posição fiscal nas linhas 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# -*- encoding: utf-8 -*-
2
 
#################################################################################
3
 
#                                                                               #
4
 
# Copyright (C) 2009  Renato Lima - Akretion                                    #
5
 
#                                                                               #
6
 
#This program is free software: you can redistribute it and/or modify           #
7
 
#it under the terms of the GNU Affero General Public License as published by    #
8
 
#the Free Software Foundation, either version 3 of the License, or              #
9
 
#(at your option) any later version.                                            #
10
 
#                                                                               #
11
 
#This program is distributed in the hope that it will be useful,                #
12
 
#but WITHOUT ANY WARRANTY; without even the implied warranty of                 #
13
 
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                  #
14
 
#GNU Affero General Public License for more details.                            #
15
 
#                                                                               #
16
 
#You should have received a copy of the GNU Affero General Public License       #
17
 
#along with this program.  If not, see <http://www.gnu.org/licenses/>.          #
18
 
#################################################################################
 
2
###############################################################################
 
3
#                                                                             #
 
4
# Copyright (C) 2009  Renato Lima - Akretion                                  #
 
5
#                                                                             #
 
6
#This program is free software: you can redistribute it and/or modify         #
 
7
#it under the terms of the GNU Affero General Public License as published by  #
 
8
#the Free Software Foundation, either version 3 of the License, or            #
 
9
#(at your option) any later version.                                          #
 
10
#                                                                             #
 
11
#This program is distributed in the hope that it will be useful,              #
 
12
#but WITHOUT ANY WARRANTY; without even the implied warranty of               #
 
13
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
 
14
#GNU Affero General Public License for more details.                          #
 
15
#                                                                             #
 
16
#You should have received a copy of the GNU Affero General Public License     #
 
17
#along with this program.  If not, see <http://www.gnu.org/licenses/>.        #
 
18
###############################################################################
19
19
 
20
20
from lxml import etree
21
21
import time
392
392
            cr, uid, user.company_id.id, context=context)
393
393
 
394
394
        if invoice_fiscal_type == 'product':
395
 
            fiscal_document_serie = [doc_serie for doc_serie in \
 
395
            fiscal_document_series = [doc_serie for doc_serie in \
396
396
                                     company.document_serie_product_ids if \
397
397
                                     doc_serie.fiscal_document_id.id == \
398
398
                                     company.product_invoice_id.id and \
399
 
                                     doc_serie.active][0].id
 
399
                                     doc_serie.active]
 
400
            if fiscal_document_series:
 
401
                fiscal_document_serie = fiscal_document_series[0].id
400
402
        else:
401
403
            fiscal_document_serie = company.document_serie_service_id and \
402
404
            company.document_serie_service_id.id or False
673
675
                for cfop_id in cfops:
674
676
                    cfop_id.set('invisible', '1')
675
677
                    cfop_id.set('required', '0')
 
678
            
 
679
            product_ids = eview.xpath("//field[@name='product_id']")
 
680
            for product_id in product_ids:
 
681
                product_id.set('domain', "[('fiscal_type', '=', '%s')]" % (
 
682
                    context.get('fiscal_type', 'product'))) 
676
683
        
677
684
            result['arch'] = etree.tostring(eview)
678
685
        
819
826
            }
820
827
 
821
828
            price = line.price_unit * (1-(line.discount or 0.0)/100.0)
822
 
            taxes = tax_obj.compute_all(cr, uid, line.invoice_line_tax_id, price, line.quantity, product=line.product_id, address_id=line.invoice_id.address_invoice_id, partner=line.invoice_id.partner_id, fiscal_position=line.fiscal_position_id)
 
829
            taxes = tax_obj.compute_all(cr, uid, line.invoice_line_tax_id, price, line.quantity, product=line.product_id, address_id=line.invoice_id.address_invoice_id, partner=line.invoice_id.partner_id, fiscal_position=line.fiscal_position)
823
830
 
824
831
            icms_cst = '99'
825
832
            ipi_cst = '99'
879
886
    _columns = {
880
887
        'fiscal_category_id': fields.many2one(
881
888
            'l10n_br_account.fiscal.category', 'Categoria'),
882
 
        'fiscal_position_id': fields.many2one(
 
889
        'fiscal_position': fields.many2one(
883
890
            'account.fiscal.position', u'Posição Fiscal',
884
891
            domain="[('fiscal_category_id','=',fiscal_category_id)]"),
885
892
        'cfop_id': fields.many2one('l10n_br_account.cfop', 'CFOP'),
1054
1061
        'ii_customhouse_charges': fields.float(
1055
1062
            'Depesas Atuaneiras', required=True,
1056
1063
            digits_compute=dp.get_precision('Account'))}
1057
 
    
 
1064
 
1058
1065
    _defaults = {
1059
1066
         'ii_iof': 0.0,
1060
1067
         'ii_customhouse_charges': 0.0}
1061
1068
 
1062
1069
    def _fiscal_position_map(self, cr, uid, ids, partner_id,
1063
 
                             partner_invoice_id, company_id, fiscal_category_id):
 
1070
                             partner_invoice_id, company_id,
 
1071
                             fiscal_category_id, product_id=False, 
 
1072
                             account_id=False, context=False):
1064
1073
        result = {'cfop_id': False}
1065
1074
        obj_rule = self.pool.get('account.fiscal.position.rule')
1066
 
        fiscal_result = obj_rule.fiscal_position_map(cr, uid, partner_id, partner_invoice_id, company_id, fiscal_category_id, context={'use_domain': ('use_invoice','=',True)})
 
1075
        fiscal_result = obj_rule.fiscal_position_map(
 
1076
            cr, uid, partner_id, partner_invoice_id, company_id,
 
1077
            fiscal_category_id, 
 
1078
            context={'use_domain': ('use_invoice', '=', True)})
1067
1079
        result.update(fiscal_result)
1068
1080
        if result.get('fiscal_position', False):
1069
 
            obj_fp = self.pool.get('account.fiscal.position').browse(cr, uid, result['fiscal_position'])
1070
 
            result['cfop_id'] = obj_fp.cfop_id.id
 
1081
            obj_fposition = self.pool.get('account.fiscal.position').browse(
 
1082
                cr, uid, result['fiscal_position'])
 
1083
            result['cfop_id'] = obj_fposition.cfop_id.id
 
1084
            if product_id:
 
1085
                obj_product = self.pool.get('product.product').browse(
 
1086
                cr, uid, product_id, context=context)
 
1087
                if context.get('type') in ('out_invoice', 'out_refund'):
 
1088
                    taxes = obj_product.taxes_id and obj_product.taxes_id or (account_id and self.pool.get('account.account').browse(cr, uid, account_id, context=context).tax_ids or False)
 
1089
                else:
 
1090
                    taxes = obj_product.supplier_taxes_id and obj_product.supplier_taxes_id or (account_id and self.pool.get('account.account').browse(cr, uid, account_id, context=context).tax_ids or False)
 
1091
    
 
1092
                tax_ids = self.pool.get('account.fiscal.position').map_tax(
 
1093
                    cr, uid, obj_fposition, taxes)
 
1094
    
 
1095
                result['invoice_line_tax_id'] = tax_ids
 
1096
 
1071
1097
        return result
1072
1098
 
1073
1099
    def product_id_change(self, cr, uid, ids, product, uom, qty=0, name='',
1074
 
                          type='out_invoice', partner_id=False, fposition_id=False,
1075
 
                          price_unit=False, address_invoice_id=False, currency_id=False, 
1076
 
                          context=None, company_id=False, fiscal_category_id=False, parent_fposition_id=False):
 
1100
                          type='out_invoice', partner_id=False,
 
1101
                          fposition_id=False, price_unit=False,
 
1102
                          address_invoice_id=False, currency_id=False,
 
1103
                          context=None, company_id=False,
 
1104
                          fiscal_category_id=False, parent_fposition_id=False):
 
1105
 
1077
1106
        result = super(account_invoice_line, self).product_id_change(
1078
1107
            cr, uid, ids, product, uom, qty, name, type, partner_id,
1079
1108
            fposition_id, price_unit, address_invoice_id, currency_id,
1080
1109
            context, company_id)
1081
 
        
 
1110
 
1082
1111
        if not fiscal_category_id or not product or not parent_fposition_id:
1083
1112
            return result
1084
1113
 
1086
1115
        product_fiscal_category_id = obj_fp_rule.product_fiscal_category_map(
1087
1116
            cr, uid, product, fiscal_category_id)
1088
1117
 
1089
 
        fiscal_position = fposition_id or parent_fposition_id or False
 
1118
        fiscal_position = parent_fposition_id or False
1090
1119
 
1091
1120
        if not product_fiscal_category_id:
1092
1121
            result['value']['fiscal_category_id'] = fiscal_category_id
1093
 
            result['value']['fiscal_position_id'] = fiscal_position
 
1122
            result['value']['fiscal_position'] = fiscal_position
1094
1123
            if fiscal_position:
1095
1124
                result['value']['cfop_id'] = self.pool.get('account.fiscal.position').read(cr, uid, [fiscal_position], ['cfop_id'])[0]['cfop_id']
1096
1125
        else:
1097
1126
            result['value']['fiscal_category_id'] = product_fiscal_category_id
1098
 
            fiscal_data = self._fiscal_position_map(cr, uid, ids, partner_id, address_invoice_id, company_id, product_fiscal_category_id)
 
1127
            fiscal_data = self._fiscal_position_map(
 
1128
                cr, uid, ids, partner_id, address_invoice_id, company_id,
 
1129
                product_fiscal_category_id, product,
 
1130
                result['value'].get('account_id', False), context)
1099
1131
            result['value'].update(fiscal_data)
1100
1132
 
1101
1133
        return result
 
1134
   
 
1135
    def onchange_fiscal_category_id(self, cr, uid, ids, partner_id,
 
1136
                                    address_invoice_id, company_id, product_id,
 
1137
                                    fiscal_category_id, account_id, context):
 
1138
        result = {'value': {}}
 
1139
        fiscal_data = self._fiscal_position_map(
 
1140
            cr, uid, ids, partner_id, address_invoice_id, company_id,
 
1141
            fiscal_category_id, product_id, account_id, context)
 
1142
        
 
1143
        result['value'].update(fiscal_data)
 
1144
        return result
 
1145
    
 
1146
    def onchange_fiscal_position(self, cr, uid, ids, partner_id,
 
1147
                                    address_invoice_id, company_id, product_id,
 
1148
                                    fiscal_category_id, account_id, context):
 
1149
        result = {'value': {}}
 
1150
        fiscal_data = self._fiscal_position_map(
 
1151
            cr, uid, ids, partner_id, address_invoice_id, company_id,
 
1152
            fiscal_category_id, product_id, account_id, context)
 
1153
        
 
1154
        result['value'].update(fiscal_data)
 
1155
        return result
1102
1156
 
1103
1157
account_invoice_line()