#-*- coding:utf-8 -*- from openerp import models, api, _ from openerp.exceptions import Warning from datetime import datetime, date class account_invoice(models.Model): _inherit = "account.invoice" def invoice_validate(self): self.check_limit() self.check_morosidad() return super(account_invoice, self).invoice_validate() @api.one def check_limit(self): if self.contado == True: return True available_credit = self.partner_id.credit_limit - self.partner_id.credit if self.amount_total > available_credit: msg = 'No se puede confirmar la factura ya que el cliente no tiene crédito suficiente.\ Pruebe marcar la opción "Contado"' raise Warning(_(msg)) return False return True @api.multi def check_morosidad(self): now = datetime.now() hoy = datetime.strptime(now.strftime("%Y-%m-%d"),"%Y-%m-%d") domain = [('id', '=', self.partner_id.id)] partner = self.env['res.partner'].search(domain) invoices = self.env['account.invoice'].search([('partner_id', '=',self.partner_id.id),('state', '=','open'),('type', '=', 'out_invoice'),('journal_id.type','=','sale')]) for item in invoices: moveLine = self.env['account.move.line'].search([('move_id','=', item.move_id.id),('debit','>',0)]) if moveLine.date_maturity < now.strftime("%Y-%m-%d"): vencimiento = datetime.strptime(moveLine.date_maturity,"%Y-%m-%d") if (hoy-vencimiento).days > partner.morosidad: raise Warning(_("El cliente %s tiene cuotas vencidas con más de %s días de atraso") % (partner.name, partner.morosidad)) return False return True # @api.model # def save_payments_invoice(self,values): # # bank_payments_type_id = values['bankPayments']['bank_payments_type_id'] # partner = self.env['res.partner'].browse(values['partnerId']) # # if bank_payments_type_id is not None: # payment_type = self.env['res.bank.payments.type'].browse(bank_payments_type_id) # if payment_type.code == 'CH' and values['amountPayments'] > partner.check_limit: # return { # 'process': False, # 'removeModal': False, # 'message' : "El monto a pagar excede el límite de crédito concedido en cheques" # } # return self.env['account.invoice'].save_payments_invoice(values)