account_interest.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. class AccountInterest(models.Model):
  7. _name = 'account.interest'
  8. name = fields.Char()
  9. date = fields.Date('Date', help="Fecha de operación")
  10. state = fields.Selection([('cancel', 'Cancelado'),('open', 'Abierto'),('paid', 'Pagado')],'Estado del pago', default="open", help="Estado")
  11. comment = fields.Text('Comment', help="Información adicional")
  12. ## Cliente
  13. customer_id = fields.Many2one('res.partner', string="customer")
  14. ## factura
  15. invoice_id = fields.Many2one('account.invoice', string='Invoice Reference', ondelete='restrict', index=True)
  16. reference = fields.Char(string='Invoice Reference', help="Invoice Reference")
  17. ## Currency
  18. currency_id = fields.Many2one('res.currency', string="Currency", help="Moneda de la operación")
  19. ## Line
  20. lines_ids = fields.One2many('account.interest.line', 'interest_id', string='interest line')
  21. '''
  22. Create
  23. '''
  24. @api.model
  25. def create(self, vals):
  26. interest = super(AccountInterest, self).create(vals)
  27. interest.write({'name':("INTEREST/%06d" % (interest.id))})
  28. return interest
  29. '''
  30. Unlink
  31. '''
  32. @api.multi
  33. def unlink(self):
  34. interestLine = self.env['account.interest.line'].search([('interest_id.id', '=', self.id)])
  35. invoice_ids = map(lambda x: x.invoice.id, interestLine)
  36. moves_ids = map(lambda x: x.move_line_id.id, interestLine)
  37. invoiceNumber = ""
  38. invoice = self.env['account.invoice'].search([('id', 'in', invoice_ids)])
  39. if (invoice):
  40. for invoiceN in invoice:
  41. invoiceNumber += "%s, " % (invoiceN.number)
  42. raise ValidationError('Existe relacion de la lineas de interes con las facturas (%s)' % (invoiceNumber))
  43. return False
  44. moveLine = self.env['account.move.line'].search([('id', 'in', moves_ids)])
  45. for lineMove in moveLine:
  46. lineMove.write({'date_interest': lineMove.date_maturity})
  47. return super(AccountInterest, self).unlink()
  48. ''' Invoice '''
  49. class accountInvoiceInterest(models.Model):
  50. _inherit = 'account.invoice'
  51. interest_ids = fields.One2many('account.interest', 'invoice_id', string=' Account Interest')
  52. is_interest = fields.Boolean('Factura de interés', default=False, help="Indica si esta factura fue generado por un interés.")
  53. interest_line_ids = fields.One2many('account.interest.line', 'invoice', string=' Account Interest Line')
  54. ''' Unlink Invoice '''
  55. @api.multi
  56. def unlink(self):
  57. interestLine = self.env['account.interest.line'].search([('invoice.id', '=', self.id)])
  58. for line in interestLine:
  59. moveLine = self.env['account.move.line'].browse(line.move_line_id.id)
  60. if (moveLine):
  61. moveLine.write({'date_interest': moveLine.date_maturity})
  62. line.write({
  63. 'state': 'open',
  64. 'reference': ''
  65. })
  66. return super(accountInvoiceInterest, self).unlink()
  67. @api.multi
  68. def write(self, vals):
  69. invoice = super(accountInvoiceInterest, self).write(vals)
  70. linesIds = map(lambda x: x.id, self.interest_line_ids)
  71. if (linesIds):
  72. interestLine = self.env['account.interest.line'].search([('id', 'in', linesIds)])
  73. if (interestLine):
  74. state = []
  75. if (self.state == 'open'):
  76. state = {'state': 'invoiced'}
  77. if (self.state == 'cancel'):
  78. state = {'state': 'invoice_cancel'}
  79. if (self.state == 'draft'):
  80. state = {'state': 'invoice_draft'}
  81. if (self.state == 'paid'):
  82. state = {'state': 'paid'}
  83. if (state):
  84. for line in interestLine:
  85. line.write(state)
  86. return True
  87. ''' Partner '''
  88. class ResPartnerInterest(models.Model):
  89. _inherit = 'res.partner'
  90. interest_ids = fields.One2many('account.interest', 'customer_id', string=' Account Interest')
  91. class AccountInterestLine(models.Model):
  92. _name = 'account.interest.line'
  93. ## Interest
  94. interest_id = fields.Many2one('account.interest', string='Account Interest', ondelete='cascade', index=True, required=True)
  95. move_line_id = fields.Many2one('account.move.line', string="Registros del diario", help="Registros del diario")
  96. amount = fields.Float('amount', digits_compute=dp.get_precision('Account'), required=True, help="Monto del pago")
  97. amount_residual = fields.Float('amount', digits_compute=dp.get_precision('Account'), required=True, help="Monto del pago")
  98. amount_interest = fields.Float('amount', digits_compute=dp.get_precision('Account'), required=True, help="Monto del pago")
  99. date_maturity = fields.Date()
  100. expired_days = fields.Integer('Expired Days ')
  101. invoice = fields.Many2one('account.invoice', string='Invoice Reference', index=True)
  102. reference = fields.Char(string='Invoice Reference', help="Invoice Reference")
  103. state = fields.Selection([ ('discount','Descuento total'),
  104. ('open','Abierto'),
  105. ('invoiced','Facturado'),
  106. ('invoice_cancel', 'Factura Cancelada'),
  107. ('invoice_draft', 'Factura Borrador'),
  108. ('paid', 'Pagado')],'Estado del pago de la linea ', default="open")
  109. amount_dicount = fields.Float('amount disconut', digits_compute=dp.get_precision('Account'), help="Monto del descuento")
  110. '''
  111. Move Line
  112. '''
  113. class accountMoveLineInterest(models.Model):
  114. _inherit = 'account.move.line'
  115. interest_line_ids = fields.One2many('account.interest.line', 'move_line_id', string=' Account Interest Line')
  116. date_interest = fields.Date("Date interés", help="Fecha de la facturación de os interés")