account_invoice.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.order_policy == 'prepaid':
  14. return True
  15. # We sum from all the sale orders that are aproved, the sale order
  16. # lines that are not yet invoiced
  17. domain = [('order_id.partner_id', '=', self.partner_id.id),
  18. ('invoiced', '=', False),
  19. ('order_id.state', 'not in', ['draft', 'cancel', 'sent'])]
  20. order_lines = self.env['sale.order.line'].search(domain)
  21. none_invoiced_amount = sum([x.price_subtotal for x in order_lines])
  22. # We sum from all the invoices that are in draft the total amount
  23. domain = [
  24. ('partner_id', '=', self.partner_id.id), ('state', '=', 'draft')]
  25. draft_invoices = self.env['account.invoice'].search(domain)
  26. draft_invoices_amount = sum([x.amount_total for x in draft_invoices])
  27. available_credit = self.partner_id.credit_limit - \
  28. self.partner_id.credit - \
  29. none_invoiced_amount - draft_invoices_amount
  30. if self.amount_total > available_credit:
  31. msg = 'No se puede confirmar el Pedido ya que el cliente no tiene credito suficiente.\
  32. Puede pasar la politica de facturación del pedido a "Pago antes de envío" en la \
  33. pestaña "Otra información"'
  34. raise Warning(_(msg))
  35. return False
  36. return True
  37. @api.multi
  38. def check_morosidad(self):
  39. now = datetime.now()
  40. hoy = datetime.strptime(now.strftime("%Y-%m-%d"),"%Y-%m-%d")
  41. domain = [('id', '=', self.partner_id.id)]
  42. partner = self.env['res.partner'].search(domain)
  43. invoices = self.env['account.invoice'].search([('partner_id', '=',self.partner_id.id),('state', '=','open'),('type', '=', 'out_invoice'),('journal_id.type','=','sale')])
  44. for item in invoices:
  45. moveLine = self.env['account.move.line'].search([('move_id','=', item.move_id.id),('debit','>',0)])
  46. if moveLine.date_maturity < now.strftime("%Y-%m-%d"):
  47. vencimiento = datetime.strptime(moveLine.date_maturity,"%Y-%m-%d")
  48. if (hoy-vencimiento).days > partner.morosidad:
  49. raise Warning(_("El cliente %s tiene cuotas vencidas con más de %s días de atraso") % (partner.name, partner.morosidad))
  50. return False
  51. return True
  52. @api.multi
  53. def save_payments_invoice(self,values):
  54. bank_payments_type_id = values['bankPayments']['bank_payments_type_id']
  55. payment_type = self.env['res.bank.payments.type'].search([('id', '=', bank_payments_type_id)])
  56. domain = [('id', '=', self.partner_id.id)]
  57. partner = self.env['res.partner'].search(domain)
  58. # import web_pdb; web_pdb.set_trace()
  59. if payment_type.code == 'CH' and values['amountPayments'] > partner.check_limit:
  60. # raise Warning(_("El monto a pagar excede el límite de crédito concedido en cheques"))
  61. return {
  62. 'process': False,
  63. 'removeModal': False,
  64. 'message' : "El monto a pagar excede el límite de crédito concedido en cheques"
  65. }
  66. return super(account_invoice, self).save_payments_invoice()