account_invoice.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #-*- coding:utf-8 -*-
  2. from openerp import models, api, _
  3. from openerp.exceptions import Warning
  4. from datetime import datetime, date
  5. class account_invoice(models.Model):
  6. _inherit = "account.invoice"
  7. def invoice_validate(self):
  8. self.check_limit()
  9. self.check_morosidad()
  10. return super(account_invoice, self).invoice_validate()
  11. @api.one
  12. def check_limit(self):
  13. if self.contado == True:
  14. return True
  15. available_credit = self.partner_id.credit_limit - self.partner_id.credit
  16. if self.amount_total > available_credit:
  17. msg = 'No se puede confirmar la factura ya que el cliente no tiene crédito suficiente.\
  18. Pruebe marcar la opción "Contado"'
  19. raise Warning(_(msg))
  20. return False
  21. return True
  22. @api.multi
  23. def check_morosidad(self):
  24. now = datetime.now()
  25. hoy = datetime.strptime(now.strftime("%Y-%m-%d"),"%Y-%m-%d")
  26. domain = [('id', '=', self.partner_id.id)]
  27. partner = self.env['res.partner'].search(domain)
  28. invoices = self.env['account.invoice'].search([('partner_id', '=',self.partner_id.id),('state', '=','open'),('type', '=', 'out_invoice'),('journal_id.type','=','sale')])
  29. for item in invoices:
  30. moveLine = self.env['account.move.line'].search([('move_id','=', item.move_id.id),('debit','>',0)])
  31. if moveLine.date_maturity < now.strftime("%Y-%m-%d"):
  32. vencimiento = datetime.strptime(moveLine.date_maturity,"%Y-%m-%d")
  33. if (hoy-vencimiento).days > partner.morosidad:
  34. raise Warning(_("El cliente %s tiene cuotas vencidas con más de %s días de atraso") % (partner.name, partner.morosidad))
  35. return False
  36. return True
  37. # @api.model
  38. # def save_payments_invoice(self,values):
  39. #
  40. # bank_payments_type_id = values['bankPayments']['bank_payments_type_id']
  41. # partner = self.env['res.partner'].browse(values['partnerId'])
  42. #
  43. # if bank_payments_type_id is not None:
  44. # payment_type = self.env['res.bank.payments.type'].browse(bank_payments_type_id)
  45. # if payment_type.code == 'CH' and values['amountPayments'] > partner.check_limit:
  46. # return {
  47. # 'process': False,
  48. # 'removeModal': False,
  49. # 'message' : "El monto a pagar excede el límite de crédito concedido en cheques"
  50. # }
  51. # return self.env['account.invoice'].save_payments_invoice(values)