account_interest.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # -*- coding: utf-8 -*-
  2. from openerp import models, fields, tools, api
  3. import openerp.addons.decimal_precision as dp
  4. from datetime import datetime
  5. from openerp.exceptions import ValidationError
  6. import pdb
  7. import logging
  8. _logger = logging.getLogger(__name__)
  9. class AccountInterest(models.Model):
  10. _name = 'account.interest'
  11. name = fields.Char()
  12. date = fields.Date('Date', help="Fecha de operación")
  13. comment = fields.Text('Comment', help="Información adicional")
  14. state = fields.Selection([('lines_paid', 'Sin interés'),('lines_open', 'Interés a cobrar.'),('paid', 'Factura pagada')],'Estado del pago', default="lines_open", help="Estado")
  15. ## Cliente
  16. customer_id = fields.Many2one('res.partner', string="customer")
  17. ## factura
  18. invoice_id = fields.Many2one('account.invoice', string='Invoice Reference', ondelete='restrict', index=True)
  19. reference = fields.Char(string='Invoice Reference', help="Invoice Reference")
  20. # cuota_nro = fields.Char(string='N° Cuota', help="N° Cuota")
  21. ## Currency
  22. currency_id = fields.Many2one('res.currency', string="Currency", help="Moneda de la operación")
  23. ## Line
  24. lines_ids = fields.One2many('account.interest.line', 'interest_id', string='interest line')
  25. '''
  26. Create
  27. '''
  28. @api.model
  29. def create(self, vals):
  30. interest = super(AccountInterest, self).create(vals)
  31. interest.write({'name':("INTEREST/%06d" % (interest.id))})
  32. return interest
  33. '''
  34. Unlink
  35. '''
  36. @api.multi
  37. def unlink(self):
  38. interestLine = self.env['account.interest.line'].search([('interest_id.id', '=', self.id)])
  39. invoice_ids = map(lambda x: x.invoice.id, interestLine)
  40. moves_ids = map(lambda x: x.move_line_id.id, interestLine)
  41. invoiceNumber = ""
  42. invoice = self.env['account.invoice'].search([('id', 'in', invoice_ids)])
  43. if (invoice):
  44. for invoiceN in invoice:
  45. invoiceNumber += "%s, " % (invoiceN.number)
  46. raise ValidationError('Existe relacion de la lineas de interes con las facturas (%s)' % (invoiceNumber))
  47. return False
  48. moveLine = self.env['account.move.line'].search([('id', 'in', moves_ids)])
  49. for lineMove in moveLine:
  50. lineMove.write({'date_interest': lineMove.date_maturity})
  51. return super(AccountInterest, self).unlink()
  52. ''' Invoice '''
  53. class accountInvoiceInterest(models.Model):
  54. _inherit = 'account.invoice'
  55. interest_ids = fields.One2many('account.interest', 'invoice_id', string=' Account Interest')
  56. is_interest = fields.Boolean('Factura de interés', default=False, help="Indica si esta factura fue generado por un interés.")
  57. interest_line_ids = fields.One2many('account.interest.line', 'invoice', string=' Account Interest Line')
  58. ''' Unlink Invoice '''
  59. @api.multi
  60. def unlink(self):
  61. interestLine = self.env['account.interest.line'].search([('invoice.id', '=', self.id)])
  62. for line in interestLine:
  63. moveLine = self.env['account.move.line'].browse(line.move_line_id.id)
  64. if (moveLine):
  65. moveLine.write({'date_interest': moveLine.date_maturity})
  66. line.write({
  67. 'state': 'open',
  68. 'reference': ''
  69. })
  70. return super(accountInvoiceInterest, self).unlink()
  71. @api.multi
  72. def write(self, vals):
  73. invoice = super(accountInvoiceInterest, self).write(vals)
  74. ''' Cambiar estado de lineas de interés'''
  75. interestLinesIDS = self._change_state_account_interest_line(self)
  76. ''' Cambiar estado del interés '''
  77. interest= self._change_state_account_interest(self)
  78. return invoice
  79. ''' Cambiar estado de lineas de interés '''
  80. def _change_state_account_interest_line(self, invoice):
  81. _logger.info('Change state account.interest.line.')
  82. linesIds = []
  83. if (invoice.interest_line_ids):
  84. linesIds = map(lambda x: x.id, invoice.interest_line_ids)
  85. if (invoice.interest_ids):
  86. linesIds = map(lambda x: x.id, invoice.interest_ids.lines_ids)
  87. if (linesIds):
  88. interestLine = self.env['account.interest.line'].search([('id', 'in', linesIds)])
  89. state = []
  90. for line in interestLine:
  91. lineInvoice = self.env['account.invoice'].browse(line.invoice.id)
  92. # if (lineInvoice):
  93. # lineInvoice.write({'cuota_nro': lineInvoice.cuota_nro})
  94. if (not lineInvoice):
  95. state = {'state': 'open'}
  96. if (lineInvoice.state == 'open'):
  97. state = {'state': 'invoiced'}
  98. if (lineInvoice.state == 'cancel'):
  99. state = {'state': 'invoice_cancel'}
  100. if (lineInvoice.state == 'draft'):
  101. state = {'state': 'invoice_draft'}
  102. if (lineInvoice.state == 'paid'):
  103. state = {'state': 'paid'}
  104. if (state):
  105. line.write(state)
  106. return True
  107. ''' Cambiar el Estado del account.interest '''
  108. def _change_state_account_interest(self, invoice):
  109. _logger.info('Change state account.interest')
  110. interestIds = []
  111. state = []
  112. if (invoice.interest_line_ids):
  113. interestIds = map(lambda x: x.interest_id.id, invoice.interest_line_ids)
  114. if (invoice.interest_ids):
  115. interestIds = map(lambda x: x.id, invoice.interest_ids)
  116. if (interestIds):
  117. accountInterest = self.env['account.interest'].search([('id', 'in', interestIds)])
  118. if (accountInterest.lines_ids):
  119. linesState = map(lambda x: x.state, accountInterest.lines_ids)
  120. if (('discount'in linesState) or ('paid' in linesState)):
  121. state ={ 'state': 'lines_paid'}
  122. if ((accountInterest.invoice_id.id == invoice.id) and (invoice.state == 'paid')):
  123. state ={ 'state': 'paid'}
  124. if (('invoiced' in linesState) or ('invoice_cancel' in linesState) or ('invoice_draft' in linesState) or ('open' in linesState)):
  125. state ={'state': 'lines_open'}
  126. if (state):
  127. accountInterest.write(state)
  128. return True
  129. ''' Verify Status Invoice and interest '''
  130. @api.model
  131. def eiru_invoice_verify_status(self):
  132. invoices = self.env['account.invoice'].search([('type', '=', 'out_invoice')])
  133. for invoice in invoices:
  134. interestLine = self._change_state_account_interest_line(invoice)
  135. interest = self._change_state_account_interest(invoice)
  136. return True
  137. ''' Partner '''
  138. class ResPartnerInterest(models.Model):
  139. _inherit = 'res.partner'
  140. interest_ids = fields.One2many('account.interest', 'customer_id', string=' Account Interest')
  141. class AccountInterestLine(models.Model):
  142. _name = 'account.interest.line'
  143. ## Interest
  144. interest_id = fields.Many2one('account.interest', string='Account Interest', ondelete='cascade', index=True, required=True)
  145. move_line_id = fields.Many2one('account.move.line', string="Registros del diario", help="Registros del diario")
  146. amount = fields.Float('amount', digits_compute=dp.get_precision('Account'), required=True, help="Monto del pago")
  147. amount_residual = fields.Float('amount', digits_compute=dp.get_precision('Account'), required=True, help="Monto del pago")
  148. amount_interest = fields.Float('amount', digits_compute=dp.get_precision('Account'), required=True, help="Monto del pago")
  149. date_maturity = fields.Date()
  150. expired_daystot = fields.Integer('Expired Days Total ')
  151. expired_days = fields.Integer('Expired Days ')
  152. invoice = fields.Many2one('account.invoice', string='Invoice Reference', index=True)
  153. reference = fields.Char(string='Invoice Reference', help="Invoice Reference")
  154. state = fields.Selection([ ('discount','Descuento total'),
  155. ('open','Abierto'),
  156. ('invoiced','Facturado'),
  157. ('invoice_cancel', 'Factura Cancelada'),
  158. ('invoice_draft', 'Factura Borrador'),
  159. ('paid', 'Pagado')],'Estado del pago de la linea ', default="open")
  160. amount_dicount = fields.Float('amount disconut', digits_compute=dp.get_precision('Account'), help="Monto del descuento")
  161. '''
  162. Move Line
  163. '''
  164. class accountMoveLineInterest(models.Model):
  165. _inherit = 'account.move.line'
  166. interest_line_ids = fields.One2many('account.interest.line', 'move_line_id', string=' Account Interest Line')
  167. date_interest = fields.Date("Date interés", help="Fecha de la facturación de los intereses")