account_invoice.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 el Pedido ya que el cliente no tiene credito suficiente.\
  18. Puede pasar la politica de facturación del pedido a "Pago antes de envío" en la \
  19. pestaña "Otra información"'
  20. raise Warning(_(msg))
  21. return False
  22. return True
  23. @api.multi
  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 (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
  38. # @api.model
  39. # def save_payments_invoice(self,values):
  40. #
  41. # bank_payments_type_id = values['bankPayments']['bank_payments_type_id']
  42. # partner = self.env['res.partner'].browse(values['partnerId'])
  43. #
  44. # if bank_payments_type_id is not None:
  45. # payment_type = self.env['res.bank.payments.type'].browse(bank_payments_type_id)
  46. # if payment_type.code == 'CH' and values['amountPayments'] > partner.check_limit:
  47. # return {
  48. # 'process': False,
  49. # 'removeModal': False,
  50. # 'message' : "El monto a pagar excede el límite de crédito concedido en cheques"
  51. # }
  52. # return self.env['account.invoice'].save_payments_invoice(values)