You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
2.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Carlos Dauden <carlos.dauden@tecnativa.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from openerp import api, fields, models, _
  5. class SaleOrder(models.Model):
  6. _inherit = 'sale.order'
  7. invoice_amount = fields.Monetary(
  8. compute='_compute_invoice_amount', store=True)
  9. invoice_pending_amount = fields.Monetary(
  10. compute='_compute_invoice_amount', store=True)
  11. @api.multi
  12. @api.depends('state', 'order_line.invoice_lines.invoice_id.amount_total')
  13. def _compute_invoice_amount(self):
  14. AccountInvoice = self.env['account.invoice']
  15. for order in self.filtered(lambda x: x.state == 'sale'):
  16. invoice_ids = order.order_line.mapped(
  17. 'invoice_lines.invoice_id').ids
  18. if not invoice_ids:
  19. order.invoice_pending_amount = order.amount_total
  20. continue
  21. amount = AccountInvoice.read_group(
  22. [('id', 'in', invoice_ids),
  23. ('type', 'in', ['out_invoice', 'out_refund'])],
  24. ['amount_total'],
  25. []
  26. )[0]['amount_total']
  27. order.invoice_amount = amount
  28. if order.amount_total > amount:
  29. order.invoice_pending_amount = order.amount_total - amount
  30. @api.multi
  31. def action_confirm(self):
  32. if not self.env.context.get('bypass_risk', False):
  33. partner = self.partner_id.commercial_partner_id
  34. exception_msg = ""
  35. if partner.risk_exception:
  36. exception_msg = _("Financial risk exceeded.\n")
  37. elif partner.risk_sale_order_limit and (
  38. (partner.risk_sale_order + self.amount_total) >
  39. partner.risk_sale_order_limit):
  40. exception_msg = _(
  41. "This sale order exceeds the sales orders risk.\n")
  42. elif partner.risk_sale_order_include and (
  43. (partner.risk_total + self.amount_total) >
  44. partner.credit_limit):
  45. exception_msg = _(
  46. "This sale order exceeds the financial risk.\n")
  47. if exception_msg:
  48. return self.env['partner.risk.exceeded.wiz'].create({
  49. 'exception_msg': exception_msg,
  50. 'partner_id': partner.id,
  51. 'origin_reference': '%s,%s' % (self._model, self.id),
  52. 'continue_method': 'action_confirm',
  53. }).action_show()
  54. return super(SaleOrder, self).action_confirm()