analytic_account.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. # -*- coding: utf-8 -*-
  2. from openerp import models, fields, api, _
  3. from openerp.exceptions import Warning
  4. class AnalyticAccount(models.Model):
  5. _inherit = 'account.analytic.account'
  6. # ==========================================================
  7. # CUOTAS
  8. # ==========================================================
  9. cuota_total = fields.Integer(
  10. string='Total de cuotas',
  11. default=0
  12. )
  13. nro_cuotas = fields.Integer(
  14. string='Cuotas generadas',
  15. readonly=True,
  16. default=0
  17. )
  18. cuotas_restantes = fields.Integer(
  19. string='Cuotas restantes',
  20. compute='_compute_estado_cuotas',
  21. store=True
  22. )
  23. porcentaje_cuotas = fields.Float(
  24. string='% Cumplimiento',
  25. compute='_compute_estado_cuotas',
  26. store=True
  27. )
  28. estado_cuotas = fields.Selection([
  29. ('curso', 'En Curso'),
  30. ('finalizado', 'Finalizado')
  31. ], string='Estado',
  32. compute='_compute_estado_cuotas',
  33. store=True)
  34. # ==========================================================
  35. # ESTADO DEL CONTRATO
  36. # ==========================================================
  37. @api.depends('cuota_total', 'nro_cuotas')
  38. def _compute_estado_cuotas(self):
  39. for rec in self:
  40. restantes = rec.cuota_total - rec.nro_cuotas
  41. if restantes < 0:
  42. restantes = 0
  43. rec.cuotas_restantes = restantes
  44. if rec.cuota_total:
  45. rec.porcentaje_cuotas = (
  46. float(rec.nro_cuotas) * 100.0
  47. ) / rec.cuota_total
  48. else:
  49. rec.porcentaje_cuotas = 0
  50. if rec.cuota_total and rec.nro_cuotas >= rec.cuota_total:
  51. rec.estado_cuotas = 'finalizado'
  52. else:
  53. rec.estado_cuotas = 'curso'
  54. # ==========================================================
  55. # ACTUALIZA FACTURA
  56. # ==========================================================
  57. def _actualizar_facturas(self, invoice_ids):
  58. Invoice = self.env['account.invoice']
  59. for contract in self:
  60. nueva_cuota = contract.nro_cuotas + 1
  61. if contract.cuota_total:
  62. if nueva_cuota > contract.cuota_total:
  63. raise Warning(_("Ya no puede generar más cuotas para este contrato."))
  64. cuota = "%s/%s" % (
  65. nueva_cuota,
  66. contract.cuota_total
  67. )
  68. invoices = Invoice.browse(invoice_ids)
  69. for invoice in invoices:
  70. if invoice.origin != contract.code:
  71. continue
  72. invoice.write({
  73. 'cuotas': cuota,
  74. 'cuota_total': contract.cuota_total,
  75. })
  76. for line in invoice.invoice_line:
  77. descripcion = "%s %s" % (
  78. line.product_id.name,
  79. cuota
  80. )
  81. line.write({
  82. 'name': descripcion
  83. })
  84. contract.write({
  85. 'nro_cuotas': nueva_cuota
  86. })
  87. # ==========================================================
  88. # FACTURA MANUAL
  89. # ==========================================================
  90. @api.multi
  91. def recurring_create_invoice(self):
  92. invoice_ids = super(
  93. AnalyticAccount,
  94. self
  95. ).recurring_create_invoice()
  96. if invoice_ids:
  97. self._actualizar_facturas(invoice_ids)
  98. return invoice_ids
  99. # ==========================================================
  100. # FACTURA AUTOMATICA (CRON)
  101. # ==========================================================
  102. @api.model
  103. def recurring_create_invoice_inmobiliaria(self):
  104. invoice_ids = super(
  105. AnalyticAccount,
  106. self
  107. )._cron_recurring_create_invoice()
  108. if invoice_ids:
  109. contratos = self.search([])
  110. contratos._actualizar_facturas(invoice_ids)
  111. return invoice_ids
  112. # ==========================================================
  113. # DUPLICAR CONTRATO
  114. # ==========================================================
  115. @api.multi
  116. def copy(self, default=None):
  117. default = dict(default or {})
  118. default.update({
  119. 'nro_cuotas': 0,
  120. 'estado_cuotas': 'curso'
  121. })
  122. return super(
  123. AnalyticAccount,
  124. self
  125. ).copy(default)