sale.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #-*- coding:utf-8 -*-
  2. from openerp import models, api, _
  3. from openerp.exceptions import Warning
  4. from datetime import datetime, date
  5. class sale_order(models.Model):
  6. _inherit = "sale.order"
  7. @api.one
  8. def action_wait(self):
  9. self.check_limit()
  10. self.check_morosidad()
  11. return super(sale_order, self).action_wait()
  12. @api.one
  13. def check_limit(self):
  14. if self.contado == True:
  15. return True
  16. available_credit = self.partner_id.credit_limit - self.partner_id.credit
  17. if self.amount_total > available_credit:
  18. msg = 'No se puede confirmar el Pedido ya que el cliente no tiene crédito suficiente.\
  19. Pruebe marcar la opción "Contado"'
  20. raise Warning(_(msg))
  21. return False
  22. return True
  23. @api.one
  24. def check_morosidad(self):
  25. now = datetime.now()
  26. hoy = datetime.strptime(now.strftime("%Y-%m-%d"),"%Y-%m-%d")
  27. domain = [('id', '=', self.partner_id.id)]
  28. partner = self.env['res.partner'].search(domain)
  29. invoices = self.env['account.invoice'].search([('partner_id', '=',self.partner_id.id),('state', '=','open'),('type', '=', 'out_invoice'),('journal_id.type','=','sale')])
  30. for item in invoices:
  31. moveLine = self.env['account.move.line'].search([('move_id','=', item.move_id.id),('debit','>',0)])
  32. if moveLine.date_maturity < now.strftime("%Y-%m-%d"):
  33. vencimiento = datetime.strptime(moveLine.date_maturity,"%Y-%m-%d")
  34. if partner.morosidad > 0 and (hoy-vencimiento).days > partner.morosidad:
  35. raise Warning(_("El cliente %s tiene cuotas vencidas con más de %s días de atraso") % (partner.name, partner.morosidad))
  36. return False
  37. return True