# -*- coding: utf-8 -*- from openerp import models, fields, tools, api, _ ## date from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT, DEFAULT_SERVER_DATE_FORMAT from pytz import timezone from datetime import datetime,timedelta ## logging import logging _logger = logging.getLogger(__name__) class EiruAccountInterest(models.Model): _inherit = 'account.interest' ''' timezone ''' def get_timezone(self): return timezone(self._context.get('tz') or self.env.user.tz) ''' Datetime ''' def get_datetime(self): return datetime.now(self.get_timezone()).strftime(DEFAULT_SERVER_DATETIME_FORMAT) ''' Date ''' def get_date(self): return datetime.now(self.get_timezone()).strftime(DEFAULT_SERVER_DATE_FORMAT) ''' Convert Str --> Datetime ''' def _convert_str_to_datetime(self, date): return datetime.strptime(date,DEFAULT_SERVER_DATE_FORMAT) ''' GET Account Interest ''' @api.model def get_account_interest(self, id): return [{ 'id': interest.id, 'name': interest.name, 'state': interest.state, 'invoiceId': interest.invoice_id.id, 'customer': [{ 'id': partner.id, 'name': partner.name, } for partner in self.env['res.partner'].browse(interest.customer_id.id)], 'currency': [{ 'id': currency.id, 'symbol': currency.symbol, 'rate': currency.rate, 'thousandsSeparator': currency.thousands_separator, 'decimalSeparator': currency.decimal_separator, 'decimalPlaces': currency.decimal_places, 'position': currency.position, } for currency in self.env['res.currency'].browse(interest.currency_id.id)] }for interest in self.env['account.interest'].browse(id)] ''' get Account Invoice ''' @api.model def get_account_invoice_interest(self, invoice=None, partner=None): _logger.info('Verify invoice') domain = [('state', '=', 'open')] if (invoice): domain.append(('id', '=', invoice)) if (partner): domain.append(('partner_id.id', '=', partner)) accountInvoice = self.env['account.invoice'].search(domain) for invoice in accountInvoice: self.get_move_line_in_invoice(invoice.id) ''' Get move Line. ''' def get_move_line_in_invoice(self, invoice): _logger.info('Get Move Line') accountInvoice = self.env['account.invoice'].browse(invoice) dateServer = self._convert_str_to_datetime(self.get_date()) domain = [('state','!=','draft'), ('credit','<=', 0), ('partner_id','=',accountInvoice.partner_id.id),('invoice','=',accountInvoice.id)] for line in self.env['account.move.line'].search(domain): expiredDays = dateServer - self._convert_str_to_datetime(line.date_maturity) if ((line.amount_residual <= 0) or (expiredDays.days <= 0)): continue ''' verify account.interest ''' accountInterest = self.eiru_create_account_interest(accountInvoice.id) ''' verify account.interest.line ''' interrstLine = self.eiru_create_account_interest_lines(accountInvoice.id, accountInterest.id, line.id, expiredDays.days) ''' Create Account Interest ''' def eiru_create_account_interest(self, invoiceId): _logger.info('Verify account interest') accountInvoice = self.env['account.invoice'].browse(invoiceId) domain = [('invoice_id', '=', accountInvoice.id),('customer_id.id', '=', accountInvoice.partner_id.id)] accountInterest = self.env['account.interest'].search(domain) dateServer = self.get_date() if (not accountInterest): accountInterest = self.env['account.interest'].create({ 'invoice_id': accountInvoice.id, 'customer_id': accountInvoice.partner_id.id, 'date': dateServer, 'currency_id': accountInvoice.currency_id.id, 'reference': accountInvoice.number, }) return accountInterest ''' create / write ''' def eiru_create_account_interest_lines(self, invoiceId, interestID, moveId, expiredDays): _logger.info('Verify account interest Line') decimal_precision = self.env['decimal.precision'].precision_get('Account') accountInvoice = self.env['account.invoice'].browse(invoiceId) accountInterest = self.env['account.interest'].browse(interestID) moveLine = self.env['account.move.line'].browse(moveId) interestConfig = self.env['account.interest.config'].search([('active', '=', True)]) interestLine = self.env['account.interest.line'].search([('interest_id.id', '=', accountInterest.id),('move_line_id.id', '=',moveLine.id)]) decimalPlaces = accountInvoice.currency_id.decimal_places if (not decimalPlaces): decimalPlaces = 0 amount = (moveLine.amount_currency or moveLine.debit or 0.0) amount_residual = (moveLine.amount_residual_currency or moveLine.amount_residual or 0.0) line = { 'move_line_id': moveLine.id, 'amount': round(amount, decimalPlaces), 'date_maturity': moveLine.date_maturity, 'expired_days': expiredDays, 'amount_residual': round(amount_residual,decimalPlaces), 'amount_interest': round((expiredDays * (amount_residual * (interestConfig.interest_rate / 100))),decimalPlaces), 'interest_id': accountInterest.id } if (not interestLine): return interestLine.create(line) return interestLine.write(line) ''' ____ _ ___ _ / ___|_ __ ___ __ _| |_ ___ |_ _|_ ____ _____ (_) ___ ___ | | | '__/ _ \/ _` | __/ _ \ | || '_ \ \ / / _ \| |/ __/ _ \ | |___| | | __/ (_| | || __/ | || | | \ V / (_) | | (_| __/ \____|_| \___|\__,_|\__\___| |___|_| |_|\_/ \___/|_|\___\___| ''' @api.model def create_account_invoice_interest(self, id, interestLine): _logger.info('Create invoice interest') accountInterest = self.env['account.interest'].browse(id) if (not accountInterest): return { 'state': False, 'message': 'Error en obtener el registro de interés' } interestConfig = self.env['account.interest.config'].search([('active', '=', True)]) if (not interestConfig): return { 'state': False, 'message': 'Error en obtener Configuración de interés' } decimal_precision = self.env['decimal.precision'].precision_get('Account') dateServer = self.get_date() invoice_line = [] for line in interestLine: accountInterestLine = self.env['account.interest.line'].browse(line['id']) descrip= "Deuda vencida al %s, días atrasados %d" % (accountInterestLine.date_maturity, accountInterestLine.expired_days) if (line['discount'] > 0): descrip = descrip+" Descuento %d" % (line['discount']) if (accountInterestLine): invoice_line.append([0,False, { 'name': descrip, 'account_id': interestConfig.line_account_id.id, 'quantity': 1, 'price_unit': round((accountInterestLine.amount_interest - line['discount']),decimal_precision), 'price_subtotal': round((accountInterestLine.amount_interest - line['discount']),decimal_precision), 'partner_id': accountInterest.customer_id.id, 'invoice_line_tax_id': [(6, 0,[x.id for x in interestConfig.line_tax_id])], }]) invoice = { 'partner_id': accountInterest.customer_id.id, 'currency_id': accountInterest.currency_id.id, 'date_invoice': dateServer, 'journal_id': interestConfig.invoice_journal_id.id, 'account_id': interestConfig.invoice_account_id.id, 'invoice_line': invoice_line, } accountInvoice = self.env['account.invoice'].create(invoice) accountInvoice.signal_workflow('invoice_open') for line in interestLine: accountInterestLine = self.env['account.interest.line'].browse(line['id']) if (accountInterestLine): accountInterestLine.write({ 'invoice': accountInvoice.id, 'reference': accountInvoice.number, 'state': 'invoiced' }) return { 'state': True, 'message': 'Operación exitosa' }