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.

42 lines
1.7 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 odoo import api, fields, models
  5. class ResPartner(models.Model):
  6. _inherit = 'res.partner'
  7. risk_sale_order_include = fields.Boolean(
  8. string='Include Sales Orders', help='Full risk computation')
  9. risk_sale_order_limit = fields.Monetary(
  10. string='Limit Sales Orders', help='Set 0 if it is not locked')
  11. risk_sale_order = fields.Monetary(
  12. compute='_compute_risk_sale_order', store=True,
  13. string='Total Sales Orders Not Invoiced',
  14. help='Total not invoiced of sales orders in Sale Order state')
  15. @api.multi
  16. @api.depends('sale_order_ids', 'sale_order_ids.invoice_pending_amount',
  17. 'child_ids.sale_order_ids',
  18. 'child_ids.sale_order_ids.invoice_pending_amount')
  19. def _compute_risk_sale_order(self):
  20. customers = self.filtered('customer')
  21. partners = customers | customers.mapped('child_ids')
  22. orders_group = self.env['sale.order'].read_group(
  23. [('state', '=', 'sale'), ('partner_id', 'in', partners.ids)],
  24. ['partner_id', 'invoice_pending_amount'],
  25. ['partner_id'])
  26. for partner in customers:
  27. partner_ids = (partner | partner.child_ids).ids
  28. partner.risk_sale_order = sum(
  29. [x['invoice_pending_amount']
  30. for x in orders_group if x['partner_id'][0] in partner_ids])
  31. @api.model
  32. def _risk_field_list(self):
  33. res = super(ResPartner, self)._risk_field_list()
  34. res.append(('risk_sale_order', 'risk_sale_order_limit',
  35. 'risk_sale_order_include'))
  36. return res