|
|
@@ -0,0 +1,180 @@
|
|
|
+# -*- coding: utf-8 -*-
|
|
|
+
|
|
|
+from openerp import models, fields, api, _
|
|
|
+from openerp.exceptions import Warning
|
|
|
+
|
|
|
+
|
|
|
+class AnalyticAccount(models.Model):
|
|
|
+ _inherit = 'account.analytic.account'
|
|
|
+
|
|
|
+ # ==========================================================
|
|
|
+ # CUOTAS
|
|
|
+ # ==========================================================
|
|
|
+
|
|
|
+ cuota_total = fields.Integer(
|
|
|
+ string='Total de cuotas',
|
|
|
+ default=0
|
|
|
+ )
|
|
|
+
|
|
|
+ nro_cuotas = fields.Integer(
|
|
|
+ string='Cuotas generadas',
|
|
|
+ readonly=True,
|
|
|
+ default=0
|
|
|
+ )
|
|
|
+
|
|
|
+ cuotas_restantes = fields.Integer(
|
|
|
+ string='Cuotas restantes',
|
|
|
+ compute='_compute_estado_cuotas',
|
|
|
+ store=True
|
|
|
+ )
|
|
|
+
|
|
|
+ porcentaje_cuotas = fields.Float(
|
|
|
+ string='% Cumplimiento',
|
|
|
+ compute='_compute_estado_cuotas',
|
|
|
+ store=True
|
|
|
+ )
|
|
|
+
|
|
|
+ estado_cuotas = fields.Selection([
|
|
|
+ ('curso', 'En Curso'),
|
|
|
+ ('finalizado', 'Finalizado')
|
|
|
+ ], string='Estado',
|
|
|
+ compute='_compute_estado_cuotas',
|
|
|
+ store=True)
|
|
|
+
|
|
|
+ # ==========================================================
|
|
|
+ # ESTADO DEL CONTRATO
|
|
|
+ # ==========================================================
|
|
|
+
|
|
|
+ @api.depends('cuota_total', 'nro_cuotas')
|
|
|
+ def _compute_estado_cuotas(self):
|
|
|
+
|
|
|
+ for rec in self:
|
|
|
+
|
|
|
+ restantes = rec.cuota_total - rec.nro_cuotas
|
|
|
+
|
|
|
+ if restantes < 0:
|
|
|
+ restantes = 0
|
|
|
+
|
|
|
+ rec.cuotas_restantes = restantes
|
|
|
+
|
|
|
+ if rec.cuota_total:
|
|
|
+
|
|
|
+ rec.porcentaje_cuotas = (
|
|
|
+ float(rec.nro_cuotas) * 100.0
|
|
|
+ ) / rec.cuota_total
|
|
|
+
|
|
|
+ else:
|
|
|
+ rec.porcentaje_cuotas = 0
|
|
|
+
|
|
|
+ if rec.cuota_total and rec.nro_cuotas >= rec.cuota_total:
|
|
|
+ rec.estado_cuotas = 'finalizado'
|
|
|
+ else:
|
|
|
+ rec.estado_cuotas = 'curso'
|
|
|
+
|
|
|
+ # ==========================================================
|
|
|
+ # ACTUALIZA FACTURA
|
|
|
+ # ==========================================================
|
|
|
+
|
|
|
+ def _actualizar_facturas(self, invoice_ids):
|
|
|
+
|
|
|
+ Invoice = self.env['account.invoice']
|
|
|
+
|
|
|
+ for contract in self:
|
|
|
+
|
|
|
+ nueva_cuota = contract.nro_cuotas + 1
|
|
|
+
|
|
|
+ if contract.cuota_total:
|
|
|
+
|
|
|
+ if nueva_cuota > contract.cuota_total:
|
|
|
+ raise Warning(_("Ya no puede generar más cuotas para este contrato."))
|
|
|
+
|
|
|
+ cuota = "%s/%s" % (
|
|
|
+ nueva_cuota,
|
|
|
+ contract.cuota_total
|
|
|
+ )
|
|
|
+
|
|
|
+ invoices = Invoice.browse(invoice_ids)
|
|
|
+
|
|
|
+ for invoice in invoices:
|
|
|
+
|
|
|
+ if invoice.origin != contract.code:
|
|
|
+ continue
|
|
|
+
|
|
|
+ invoice.write({
|
|
|
+ 'cuotas': cuota,
|
|
|
+ 'cuota_total': contract.cuota_total,
|
|
|
+ })
|
|
|
+
|
|
|
+ for line in invoice.invoice_line:
|
|
|
+
|
|
|
+ descripcion = "%s %s" % (
|
|
|
+ line.product_id.name,
|
|
|
+ cuota
|
|
|
+ )
|
|
|
+
|
|
|
+ line.write({
|
|
|
+ 'name': descripcion
|
|
|
+ })
|
|
|
+
|
|
|
+ contract.write({
|
|
|
+ 'nro_cuotas': nueva_cuota
|
|
|
+ })
|
|
|
+
|
|
|
+ # ==========================================================
|
|
|
+ # FACTURA MANUAL
|
|
|
+ # ==========================================================
|
|
|
+
|
|
|
+ @api.multi
|
|
|
+ def recurring_create_invoice(self):
|
|
|
+
|
|
|
+ invoice_ids = super(
|
|
|
+ AnalyticAccount,
|
|
|
+ self
|
|
|
+ ).recurring_create_invoice()
|
|
|
+
|
|
|
+ if invoice_ids:
|
|
|
+ self._actualizar_facturas(invoice_ids)
|
|
|
+
|
|
|
+ return invoice_ids
|
|
|
+
|
|
|
+ # ==========================================================
|
|
|
+ # FACTURA AUTOMATICA (CRON)
|
|
|
+ # ==========================================================
|
|
|
+
|
|
|
+ @api.model
|
|
|
+ def recurring_create_invoice_inmobiliaria(self):
|
|
|
+
|
|
|
+ invoice_ids = super(
|
|
|
+ AnalyticAccount,
|
|
|
+ self
|
|
|
+ )._cron_recurring_create_invoice()
|
|
|
+
|
|
|
+ if invoice_ids:
|
|
|
+
|
|
|
+ contratos = self.search([])
|
|
|
+
|
|
|
+ contratos._actualizar_facturas(invoice_ids)
|
|
|
+
|
|
|
+ return invoice_ids
|
|
|
+
|
|
|
+ # ==========================================================
|
|
|
+ # DUPLICAR CONTRATO
|
|
|
+ # ==========================================================
|
|
|
+
|
|
|
+ @api.multi
|
|
|
+ def copy(self, default=None):
|
|
|
+
|
|
|
+ default = dict(default or {})
|
|
|
+
|
|
|
+ default.update({
|
|
|
+
|
|
|
+ 'nro_cuotas': 0,
|
|
|
+
|
|
|
+ 'estado_cuotas': 'curso'
|
|
|
+
|
|
|
+ })
|
|
|
+
|
|
|
+ return super(
|
|
|
+ AnalyticAccount,
|
|
|
+ self
|
|
|
+ ).copy(default)
|