From 6590bbf502cccc8fc52e979f719bce7990c22949 Mon Sep 17 00:00:00 2001 From: Carlos Dauden Date: Wed, 31 Jan 2018 12:47:36 +0100 Subject: [PATCH] [9.0][IMP] partner_financial_risk: Improve multicompany cron (#530) * [9.0][IMP] partner_financial_risk: Improve multicompany cron * [9.0][IMP] partner_financial_risk: Exec with sudo --- partner_financial_risk/models/res_partner.py | 25 +++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/partner_financial_risk/models/res_partner.py b/partner_financial_risk/models/res_partner.py index 71d0bd7e3..7918b6b1b 100644 --- a/partner_financial_risk/models/res_partner.py +++ b/partner_financial_risk/models/res_partner.py @@ -2,6 +2,7 @@ # Copyright 2016 Carlos Dauden # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from collections import defaultdict from datetime import datetime from dateutil.relativedelta import relativedelta from openerp import api, fields, models @@ -249,12 +250,24 @@ class ResPartner(models.Model): ConfigParameter = self.env['ir.config_parameter'] last_check = ConfigParameter.get_param( 'partner_financial_risk.last_check', default='2016-01-01') - move_lines = self.env['account.move.line'].search([ - ('reconciled', '=', False), - ('account_id.internal_type', '=', 'receivable'), - ('date_maturity', '>=', last_check), - ('date_maturity', '<', max_date)]) - move_lines.mapped('partner_id')._compute_risk_account_amount() + groups = self.env['account.move.line'].sudo().read_group( + [('reconciled', '=', False), + ('partner_id', '!=', False), + ('account_id.internal_type', '=', 'receivable'), + ('date_maturity', '>=', last_check), + ('date_maturity', '<', max_date)], + ['company_id', 'partner_id'], + ['company_id', 'partner_id'], + lazy=False, + ) + group_dic = defaultdict(list) + for group in groups: + group_dic[group['company_id'][0]].append(group['partner_id'][0]) + for company_id, partner_ids in group_dic.iteritems(): + partners = self.browse(partner_ids) + partners.with_context( + force_company=company_id, + )._compute_risk_account_amount() ConfigParameter.set_param( 'partner_financial_risk.last_check', max_date) return True