~openerp-spain-team/openerp-spain/6.0

« back to all changes in this revision

Viewing changes to l10n_es_prev_tesoreria/prev_tesoreria.py

  • Committer: Ana Juaristi
  • Date: 2012-03-09 16:21:34 UTC
  • Revision ID: ajuaristio@gmail.com-20120309162134-sjlzxy4afkkuvtpn
[ADD] l10n_es_account_asset, l10n_es_prev_tesoreria: Incluidos modulos prevision tesoreria y activos/amortizaciones.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    Avanzosc - Avanced Open Source Consulting
 
5
#    Copyright (C) 2010 - 2011 Avanzosc <http://www.avanzosc.com>
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU Affero General Public License as
 
9
#    published by the Free Software Foundation, either version 3 of the
 
10
#    License, or (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU Affero General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU Affero General Public License
 
18
#    along with this program.  If not, see http://www.gnu.org/licenses/.
 
19
#
 
20
##############################################################################
 
21
 
 
22
import time
 
23
import decimal_precision as dp
 
24
 
 
25
from osv import osv
 
26
from osv import fields
 
27
 
 
28
 
 
29
class l10n_es_tesoreria_facturas(osv.osv):
 
30
    _name = 'l10n.es.tesoreria.facturas'
 
31
    _description = 'Facturas para la tesorería'
 
32
    
 
33
    _columns = {
 
34
        'factura_id': fields.many2one('account.invoice', 'Factura'),
 
35
        'fecha_vencimiento': fields.date('Fecha Vencimiento'),
 
36
        'partner_id': fields.many2one('res.partner', 'Empresa'),
 
37
        'diario': fields.many2one('account.journal', 'Diario'),
 
38
        'tipo_pago': fields.many2one('payment.type', 'Tipo de Pago', required=False),
 
39
        'estado': fields.selection([
 
40
            ('draft','Borrador'),
 
41
            ('proforma','Pro-forma'),
 
42
            ('proforma2','Pro-forma'),
 
43
            ('open','Abierto'),
 
44
            ('paid','Pagado'),
 
45
            ('cancel','Cancelado'),
 
46
        ], 'Estado'),
 
47
        'base': fields.float('Base', digits_compute=dp.get_precision('Account')),
 
48
        'impuesto': fields.float('Impuesto', digits_compute=dp.get_precision('Account')),
 
49
        'total': fields.float('Total', digits_compute=dp.get_precision('Account')),
 
50
        'pendiente': fields.float('Pendiente', digits_compute=dp.get_precision('Account')),
 
51
    }
 
52
l10n_es_tesoreria_facturas()
 
53
 
 
54
class l10n_es_tesoreria(osv.osv):
 
55
    _name = 'l10n.es.tesoreria'
 
56
    _description = 'Predicción de tesorería'
 
57
 
 
58
    def _check_fecha(self, cr, uid, ids): 
 
59
        for teso in self.browse(cr, uid, ids):
 
60
            if teso.inicio_validez > teso.fin_validez:
 
61
                return False
 
62
        return True
 
63
    
 
64
    def _check_filtro(self, cr, uid, ids):
 
65
        for teso in self.browse(cr, uid, ids):
 
66
            if not teso.check_draft and not teso.check_proforma and not teso.check_open:
 
67
                return False
 
68
        return True
 
69
    
 
70
    def _calcular_saldo(self, cr, uid, ids, name, args, context=None):
 
71
        res = {}
 
72
        saldo = 0
 
73
        for teso in self.browse(cr, uid, ids):
 
74
            for fact_emit in teso.facturas_emit:
 
75
                saldo += fact_emit.total
 
76
            for fact_rec in teso.facturas_rec:
 
77
                saldo -= fact_rec.total
 
78
            for pagoP in teso.pagos_period:
 
79
                saldo -= pagoP.importe
 
80
            for pagoV in teso.pagos_var:
 
81
                saldo -= pagoV.importe
 
82
            saldo += teso.saldo_inicial
 
83
            res[teso.id] = saldo
 
84
        return res
 
85
 
 
86
    _columns = {
 
87
        'name': fields.char('Descripción', size=64, required=True),
 
88
        'plantilla': fields.many2one('l10n.es.tesoreria.plantilla', 'Plantilla', required=True),
 
89
        'inicio_validez': fields.date('Fecha Inicio', required=True),
 
90
        'fin_validez': fields.date('Fecha Final', required=True),
 
91
        'saldo_inicial': fields.float('Saldo Inicial', digits_compute=dp.get_precision('Account')),
 
92
        'saldo_final': fields.function(_calcular_saldo, method=True, digits_compute=dp.get_precision('Account'), string='Saldo Final'),
 
93
        'check_draft': fields.boolean('Borrador'),
 
94
        'check_proforma': fields.boolean('Proforma'),
 
95
        'check_open': fields.boolean('Abierto'),
 
96
        'facturas_emit': fields.many2many('l10n.es.tesoreria.facturas', 'l10n_es_teso_fact_e_rel','teso_id','fact_e_id','Facturas Emitidas'),
 
97
        'facturas_rec': fields.many2many('l10n.es.tesoreria.facturas', 'l10n_es_teso_fact_r_rel','teso_id','fact_r_id','Facturas Recibidas'),
 
98
        'pagos_period': fields.one2many('l10n.es.tesoreria.pagos.period', 'tesoreria_id', 'Pagos Periodicos'),
 
99
        'pagos_var': fields.one2many('l10n.es.tesoreria.pagos.var', 'tesoreria_id', 'Pagos Variables'),
 
100
        'desglose_saldo': fields.one2many('l10n.es.tesoreria.saldos', 'tesoreria_id', 'Desglose de Saldo'),
 
101
    }
 
102
    
 
103
    _defaults = {  
 
104
        'check_draft': lambda *a: 1,
 
105
        'check_proforma': lambda *a: 1,
 
106
        'check_open': lambda *a: 1,
 
107
    }
 
108
    
 
109
    _constraints = [
 
110
            (_check_fecha, 'Error: Fecha final de validez', ['fin_validez']),
 
111
            (_check_filtro, 'Error: Selecciona un filtro', ['check_draft']),
 
112
    ]
 
113
    
 
114
    def restart(self, cr, uid, ids, context=None):
 
115
        pagoP_obj = self.pool.get('l10n.es.tesoreria.pagos.period')
 
116
        pagoV_obj = self.pool.get('l10n.es.tesoreria.pagos.var')
 
117
        t_factura_obj = self.pool.get('l10n.es.tesoreria.facturas')
 
118
        facturas = []
 
119
        for teso in self.browse(cr, uid, ids):
 
120
            for factura_emit in teso.facturas_emit:
 
121
                facturas.append(factura_emit.id)
 
122
            for factura_rec in teso.facturas_rec:
 
123
                facturas.append(factura_rec.id)
 
124
            t_factura_obj.unlink(cr, uid, facturas)
 
125
            for pagoP in teso.pagos_period:
 
126
                pagoP_obj.unlink(cr, uid, pagoP.id)
 
127
            for pagoV in teso.pagos_var:
 
128
                pagoV_obj.unlink(cr, uid, pagoV.id)
 
129
        return True
 
130
    
 
131
    def button_saldo(self, cr, uid, ids, context=None):
 
132
        res = {}
 
133
        saldo = 0
 
134
        saldos_obj = self.pool.get('l10n.es.tesoreria.saldos')
 
135
        for teso in self.browse(cr, uid, ids):
 
136
            for saldo in teso.desglose_saldo:
 
137
                saldos_obj.unlink(cr, uid, saldo.id)
 
138
            for fact_emit in teso.facturas_emit:
 
139
                saldo_id = saldos_obj.search(cr, uid, [('name','=',fact_emit.tipo_pago.name), ('tesoreria_id', '=', teso.id)])
 
140
                if saldo_id:
 
141
                    saldo = saldos_obj.browse(cr, uid, saldo_id[0])
 
142
                    saldos_obj.write(cr, uid, saldo.id, {'saldo': saldo.saldo + fact_emit.total})
 
143
                else:
 
144
                    saldos_obj.create(cr, uid, {'name': fact_emit.tipo_pago.name, 'saldo': fact_emit.total, 'tesoreria_id': teso.id})
 
145
            for fact_rec in teso.facturas_rec:
 
146
                saldo_id = saldos_obj.search(cr, uid, [('name','=',fact_rec.tipo_pago.name), ('tesoreria_id', '=', teso.id)])
 
147
                if saldo_id:
 
148
                    saldo = saldos_obj.browse(cr, uid, saldo_id[0])
 
149
                    saldos_obj.write(cr, uid, saldo.id, {'saldo': saldo.saldo - fact_rec.total})
 
150
                else:
 
151
                    saldos_obj.create(cr, uid, {'name': fact_rec.tipo_pago.name, 'saldo': -fact_rec.total, 'tesoreria_id': teso.id})
 
152
        return True
 
153
    
 
154
    def button_calculate(self, cr, uid, ids, context=None):
 
155
        facturas_emit = []
 
156
        facturas_rec = []
 
157
        estado = []
 
158
        pagoP_obj = self.pool.get('l10n.es.tesoreria.pagos.period')
 
159
        pagoV_obj = self.pool.get('l10n.es.tesoreria.pagos.var')
 
160
        t_factura_obj = self.pool.get('l10n.es.tesoreria.facturas')
 
161
        invoice_obj = self.pool.get('account.invoice')
 
162
        
 
163
        self.restart(cr, uid, ids)
 
164
        for teso in self.browse(cr, uid, ids):
 
165
            if teso.check_draft:
 
166
                estado.append("draft")
 
167
            if teso.check_proforma:
 
168
                estado.append("proforma")
 
169
            if teso.check_open:
 
170
                estado.append("open")
 
171
            invoices = invoice_obj.search(cr, uid, [('date_due', '>', teso.inicio_validez), ('date_due', '<', teso.fin_validez), ('state', 'in', tuple(estado))])
 
172
            for invoice in invoice_obj.browse(cr, uid, invoices):
 
173
                values = {
 
174
                    'factura_id': invoice.id,
 
175
                    'fecha_vencimiento': invoice.date_due,
 
176
                    'partner_id': invoice.partner_id.id,
 
177
                    'diario': invoice.journal_id.id,
 
178
                    'tipo_pago': invoice.payment_type.id,
 
179
                    'estado': invoice.state,
 
180
                    'base': invoice.amount_untaxed,
 
181
                    'impuesto': invoice.amount_tax,
 
182
                    'total': invoice.amount_total,
 
183
                    'pendiente': invoice.residual,
 
184
                }
 
185
                id = t_factura_obj.create(cr, uid, values)
 
186
                if invoice.type == "out_invoice":
 
187
                    facturas_emit.append(id)
 
188
                elif invoice.type == "in_invoice":
 
189
                    facturas_rec.append(id)
 
190
            self.write(cr, uid, teso.id, {'facturas_emit': [(6,0, facturas_emit)], 'facturas_rec': [(6,0, facturas_rec)]})
 
191
            for pagoP in teso.plantilla.pagos_period:
 
192
                if pagoP.fecha > teso.inicio_validez and pagoP.fecha < teso.fin_validez and not pagoP.pagado:
 
193
                    values = {
 
194
                        'name': pagoP.name,
 
195
                        'fecha': pagoP.fecha,
 
196
                        'partner_id': pagoP.partner_id.id,
 
197
                        'importe': pagoP.importe,
 
198
                        'tesoreria_id': teso.id,
 
199
                    }
 
200
                    pagoP_obj.create(cr, uid, values)
 
201
            for pagoV in teso.plantilla.pagos_var:
 
202
                if pagoV.fecha > teso.inicio_validez and pagoV.fecha < teso.fin_validez and not pagoV.pagado:
 
203
                    values = {
 
204
                        'name': pagoV.name,
 
205
                        'fecha': pagoV.fecha,
 
206
                        'partner_id': pagoV.partner_id.id,
 
207
                        'importe': pagoV.importe,
 
208
                        'tesoreria_id': teso.id,
 
209
                    }
 
210
                    pagoV_obj.create(cr, uid, values)
 
211
        return True
 
212
        
 
213
l10n_es_tesoreria()
 
214
 
 
215
class l10n_es_tesoreria_saldos(osv.osv):
 
216
    _name = 'l10n.es.tesoreria.saldos'
 
217
    _description = 'Saldos para la tesorería'
 
218
    
 
219
    _columns = {
 
220
        'name': fields.char('Tipo de Pago', size=64),
 
221
        'saldo': fields.float('Saldo', digits_compute=dp.get_precision('Account')),
 
222
        'tesoreria_id': fields.many2one('l10n.es.tesoreria', 'Tesoreria'),
 
223
    }
 
224
l10n_es_tesoreria_saldos()
 
225
 
 
226
class l10n_es_tesoreria_pagos_period(osv.osv):
 
227
    _name = 'l10n.es.tesoreria.pagos.period'
 
228
    _description = 'Pagos Periodicos para la tesorería'
 
229
    
 
230
    _columns = {
 
231
        'name': fields.char('Descripción', size=64),
 
232
        'fecha': fields.date('Fecha'),
 
233
        'partner_id': fields.many2one('res.partner', 'Empresa'),
 
234
        'importe': fields.float('Importe', digits_compute=dp.get_precision('Account')),
 
235
        'tesoreria_id': fields.many2one('l10n.es.tesoreria', 'Tesorería'),
 
236
    } 
 
237
l10n_es_tesoreria_pagos_period()
 
238
 
 
239
class l10n_es_tesoreria_pagos_var(osv.osv):
 
240
    _name = 'l10n.es.tesoreria.pagos.var'
 
241
    _description = 'Pagos Variables para la tesorería'
 
242
    
 
243
    _columns = {
 
244
        'name': fields.char('Descripción', size=64),
 
245
        'partner_id': fields.many2one('res.partner', 'Empresa'),
 
246
        'fecha': fields.date('Fecha'),
 
247
        'importe': fields.float('Importe', digits_compute=dp.get_precision('Account')),
 
248
        'tesoreria_id': fields.many2one('l10n.es.tesoreria', 'Tesorería'),
 
249
    } 
 
250
l10n_es_tesoreria_pagos_var()