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.

65 lines
2.8 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.Float(
  8. compute='_compute_invoice_amount', store=True)
  9. invoice_pending_amount = fields.Float(
  10. compute='_compute_invoice_amount', store=True)
  11. @api.multi
  12. @api.depends('state',
  13. 'invoice_ids',
  14. 'invoice_ids.amount_total',
  15. 'order_line.invoice_lines.invoice_id.amount_total')
  16. def _compute_invoice_amount(self):
  17. AccountInvoice = self.env['account.invoice']
  18. for order in self:
  19. order.invoice_pending_amount = order.amount_total
  20. invoice_ids = order.order_line.mapped('invoice_lines').mapped(
  21. 'invoice_id').ids
  22. invoice_ids += order.mapped('invoice_ids').ids
  23. if not invoice_ids:
  24. continue
  25. amount = AccountInvoice.read_group(
  26. [('id', 'in', invoice_ids),
  27. ('type', 'in', ['out_invoice', 'out_refund'])],
  28. ['amount_total'],
  29. []
  30. )[0]['amount_total']
  31. order.invoice_amount = amount
  32. if order.amount_total > amount:
  33. order.invoice_pending_amount = order.amount_total - amount
  34. @api.multi
  35. def action_button_confirm(self):
  36. if not self.env.context.get('bypass_risk', False):
  37. for order in self:
  38. partner = order.partner_id.commercial_partner_id
  39. exception_msg = ""
  40. if partner.risk_exception:
  41. exception_msg = _("Financial risk exceeded.\n")
  42. elif partner.risk_sale_order_limit and (
  43. (partner.risk_sale_order + self.amount_total) >
  44. partner.risk_sale_order_limit):
  45. exception_msg = _(
  46. "This sale order exceeds the sales orders risk.\n")
  47. elif partner.risk_sale_order_include and (
  48. (partner.risk_total + self.amount_total) >
  49. partner.credit_limit):
  50. exception_msg = _(
  51. "This sale order exceeds the financial risk.\n")
  52. if exception_msg:
  53. return self.env['partner.risk.exceeded.wiz'].create({
  54. 'exception_msg': exception_msg,
  55. 'partner_id': partner.id,
  56. 'origin_reference': '%s,%s' % (self._model, self.id),
  57. 'continue_method': 'action_button_confirm',
  58. }).action_show()
  59. return super(SaleOrder, self).action_button_confirm()