1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# Avanzosc - Avanced Open Source Consulting
5
# Copyright (C) 2010 - 2011 Avanzosc <http://www.avanzosc.com>
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.
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.
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/.
20
##############################################################################
23
import decimal_precision as dp
26
from osv import fields
29
class l10n_es_tesoreria_facturas(osv.osv):
30
_name = 'l10n.es.tesoreria.facturas'
31
_description = 'Facturas para la tesorería'
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([
41
('proforma','Pro-forma'),
42
('proforma2','Pro-forma'),
45
('cancel','Cancelado'),
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')),
52
l10n_es_tesoreria_facturas()
54
class l10n_es_tesoreria(osv.osv):
55
_name = 'l10n.es.tesoreria'
56
_description = 'Predicción de tesorería'
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:
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:
70
def _calcular_saldo(self, cr, uid, ids, name, args, context=None):
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
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'),
104
'check_draft': lambda *a: 1,
105
'check_proforma': lambda *a: 1,
106
'check_open': lambda *a: 1,
110
(_check_fecha, 'Error: Fecha final de validez', ['fin_validez']),
111
(_check_filtro, 'Error: Selecciona un filtro', ['check_draft']),
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')
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)
131
def button_saldo(self, cr, uid, ids, context=None):
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)])
141
saldo = saldos_obj.browse(cr, uid, saldo_id[0])
142
saldos_obj.write(cr, uid, saldo.id, {'saldo': saldo.saldo + fact_emit.total})
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)])
148
saldo = saldos_obj.browse(cr, uid, saldo_id[0])
149
saldos_obj.write(cr, uid, saldo.id, {'saldo': saldo.saldo - fact_rec.total})
151
saldos_obj.create(cr, uid, {'name': fact_rec.tipo_pago.name, 'saldo': -fact_rec.total, 'tesoreria_id': teso.id})
154
def button_calculate(self, cr, uid, ids, context=None):
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')
163
self.restart(cr, uid, ids)
164
for teso in self.browse(cr, uid, ids):
166
estado.append("draft")
167
if teso.check_proforma:
168
estado.append("proforma")
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):
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,
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:
195
'fecha': pagoP.fecha,
196
'partner_id': pagoP.partner_id.id,
197
'importe': pagoP.importe,
198
'tesoreria_id': teso.id,
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:
205
'fecha': pagoV.fecha,
206
'partner_id': pagoV.partner_id.id,
207
'importe': pagoV.importe,
208
'tesoreria_id': teso.id,
210
pagoV_obj.create(cr, uid, values)
215
class l10n_es_tesoreria_saldos(osv.osv):
216
_name = 'l10n.es.tesoreria.saldos'
217
_description = 'Saldos para la tesorería'
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'),
224
l10n_es_tesoreria_saldos()
226
class l10n_es_tesoreria_pagos_period(osv.osv):
227
_name = 'l10n.es.tesoreria.pagos.period'
228
_description = 'Pagos Periodicos para la tesorería'
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'),
237
l10n_es_tesoreria_pagos_period()
239
class l10n_es_tesoreria_pagos_var(osv.osv):
240
_name = 'l10n.es.tesoreria.pagos.var'
241
_description = 'Pagos Variables para la tesorería'
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'),
250
l10n_es_tesoreria_pagos_var()