# -*- coding: utf-8 -*- from openerp import models, fields, tools, api import openerp.addons.decimal_precision as dp from datetime import datetime from openerp.exceptions import ValidationError import pdb import logging _logger = logging.getLogger(__name__) class AccountInterest(models.Model): _name = 'account.interest' name = fields.Char() date = fields.Date('Date', help="Fecha de operación") comment = fields.Text('Comment', help="Información adicional") 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") ## Cliente customer_id = fields.Many2one('res.partner', string="customer") ## factura invoice_id = fields.Many2one('account.invoice', string='Invoice Reference', ondelete='restrict', index=True) reference = fields.Char(string='Invoice Reference', help="Invoice Reference") # cuota_nro = fields.Char(string='N° Cuota', help="N° Cuota") ## Currency currency_id = fields.Many2one('res.currency', string="Currency", help="Moneda de la operación") ## Line lines_ids = fields.One2many('account.interest.line', 'interest_id', string='interest line') ''' Create ''' @api.model def create(self, vals): interest = super(AccountInterest, self).create(vals) interest.write({'name':("INTEREST/%06d" % (interest.id))}) return interest ''' Unlink ''' @api.multi def unlink(self): interestLine = self.env['account.interest.line'].search([('interest_id.id', '=', self.id)]) invoice_ids = map(lambda x: x.invoice.id, interestLine) moves_ids = map(lambda x: x.move_line_id.id, interestLine) invoiceNumber = "" invoice = self.env['account.invoice'].search([('id', 'in', invoice_ids)]) if (invoice): for invoiceN in invoice: invoiceNumber += "%s, " % (invoiceN.number) raise ValidationError('Existe relacion de la lineas de interes con las facturas (%s)' % (invoiceNumber)) return False moveLine = self.env['account.move.line'].search([('id', 'in', moves_ids)]) for lineMove in moveLine: lineMove.write({'date_interest': lineMove.date_maturity}) return super(AccountInterest, self).unlink() ''' Invoice ''' class accountInvoiceInterest(models.Model): _inherit = 'account.invoice' interest_ids = fields.One2many('account.interest', 'invoice_id', string=' Account Interest') is_interest = fields.Boolean('Factura de interés', default=False, help="Indica si esta factura fue generado por un interés.") interest_line_ids = fields.One2many('account.interest.line', 'invoice', string=' Account Interest Line') ''' Unlink Invoice ''' @api.multi def unlink(self): interestLine = self.env['account.interest.line'].search([('invoice.id', '=', self.id)]) for line in interestLine: moveLine = self.env['account.move.line'].browse(line.move_line_id.id) if (moveLine): moveLine.write({'date_interest': moveLine.date_maturity}) line.write({ 'state': 'open', 'reference': '' }) return super(accountInvoiceInterest, self).unlink() @api.multi def write(self, vals): invoice = super(accountInvoiceInterest, self).write(vals) ''' Cambiar estado de lineas de interés''' interestLinesIDS = self._change_state_account_interest_line(self) ''' Cambiar estado del interés ''' interest= self._change_state_account_interest(self) return invoice ''' Cambiar estado de lineas de interés ''' def _change_state_account_interest_line(self, invoice): _logger.info('Change state account.interest.line.') linesIds = [] if (invoice.interest_line_ids): linesIds = map(lambda x: x.id, invoice.interest_line_ids) if (invoice.interest_ids): linesIds = map(lambda x: x.id, invoice.interest_ids.lines_ids) if (linesIds): interestLine = self.env['account.interest.line'].search([('id', 'in', linesIds)]) state = [] for line in interestLine: lineInvoice = self.env['account.invoice'].browse(line.invoice.id) # if (lineInvoice): # lineInvoice.write({'cuota_nro': lineInvoice.cuota_nro}) if (not lineInvoice): state = {'state': 'open'} if (lineInvoice.state == 'open'): state = {'state': 'invoiced'} if (lineInvoice.state == 'cancel'): state = {'state': 'invoice_cancel'} if (lineInvoice.state == 'draft'): state = {'state': 'invoice_draft'} if (lineInvoice.state == 'paid'): state = {'state': 'paid'} if (state): line.write(state) return True ''' Cambiar el Estado del account.interest ''' def _change_state_account_interest(self, invoice): _logger.info('Change state account.interest') interestIds = [] state = [] if (invoice.interest_line_ids): interestIds = map(lambda x: x.interest_id.id, invoice.interest_line_ids) if (invoice.interest_ids): interestIds = map(lambda x: x.id, invoice.interest_ids) if (interestIds): accountInterest = self.env['account.interest'].search([('id', 'in', interestIds)]) if (accountInterest.lines_ids): linesState = map(lambda x: x.state, accountInterest.lines_ids) if (('discount'in linesState) or ('paid' in linesState)): state ={ 'state': 'lines_paid'} if ((accountInterest.invoice_id.id == invoice.id) and (invoice.state == 'paid')): state ={ 'state': 'paid'} if (('invoiced' in linesState) or ('invoice_cancel' in linesState) or ('invoice_draft' in linesState) or ('open' in linesState)): state ={'state': 'lines_open'} if (state): accountInterest.write(state) return True ''' Verify Status Invoice and interest ''' @api.model def eiru_invoice_verify_status(self): invoices = self.env['account.invoice'].search([('type', '=', 'out_invoice')]) for invoice in invoices: interestLine = self._change_state_account_interest_line(invoice) interest = self._change_state_account_interest(invoice) return True ''' Partner ''' class ResPartnerInterest(models.Model): _inherit = 'res.partner' interest_ids = fields.One2many('account.interest', 'customer_id', string=' Account Interest') class AccountInterestLine(models.Model): _name = 'account.interest.line' ## Interest interest_id = fields.Many2one('account.interest', string='Account Interest', ondelete='cascade', index=True, required=True) move_line_id = fields.Many2one('account.move.line', string="Registros del diario", help="Registros del diario") amount = fields.Float('amount', digits_compute=dp.get_precision('Account'), required=True, help="Monto del pago") amount_residual = fields.Float('amount', digits_compute=dp.get_precision('Account'), required=True, help="Monto del pago") amount_interest = fields.Float('amount', digits_compute=dp.get_precision('Account'), required=True, help="Monto del pago") date_maturity = fields.Date() expired_daystot = fields.Integer('Expired Days Total ') expired_days = fields.Integer('Expired Days ') invoice = fields.Many2one('account.invoice', string='Invoice Reference', index=True) reference = fields.Char(string='Invoice Reference', help="Invoice Reference") state = fields.Selection([ ('discount','Descuento total'), ('open','Abierto'), ('invoiced','Facturado'), ('invoice_cancel', 'Factura Cancelada'), ('invoice_draft', 'Factura Borrador'), ('paid', 'Pagado')],'Estado del pago de la linea ', default="open") amount_dicount = fields.Float('amount disconut', digits_compute=dp.get_precision('Account'), help="Monto del descuento") ''' Move Line ''' class accountMoveLineInterest(models.Model): _inherit = 'account.move.line' interest_line_ids = fields.One2many('account.interest.line', 'move_line_id', string=' Account Interest Line') date_interest = fields.Date("Date interés", help="Fecha de la facturación de los intereses")