From eb0e7eba7de30bca9e1a5ad16c2974db677b154d Mon Sep 17 00:00:00 2001 From: benjamin <benjamin@le-filament.com> Date: Thu, 6 Jan 2022 15:26:27 +0100 Subject: [PATCH] [refact] retrieve_datas_dashboard to avoid SQL --- models/lefilament_tdb.py | 211 +++++++++++++-------------------------- 1 file changed, 67 insertions(+), 144 deletions(-) diff --git a/models/lefilament_tdb.py b/models/lefilament_tdb.py index 20ca88c..ac5a1fc 100644 --- a/models/lefilament_tdb.py +++ b/models/lefilament_tdb.py @@ -171,150 +171,73 @@ class LeFilamentTdb(models.Model): ) fiscal_year_next = "'" + Date.to_string(fiscal_date) + "'" - # Prepare values - res = { - "facture": 0, - "commandes": 0, - "pipe": 0, - "pipe_win": 0, - "pipe_to_win": 0, - "pipe_n1": 0, - "tresorerie": 0, - "entree": 0, - "sortie": 0, - "variation": 0, - "target": 0, - "cca": 0, - "capital": 0, - "date_maj": 0, - "a_encaisser": 0, - "a_payer": 0, - "fiscal_year": fiscal_year, - "fiscal_year_next": fiscal_year_next, - } - - self._cr.execute( - """ - SELECT - (SELECT COUNT(*) - FROM account_move - ) AS id, - (SELECT SUM(amount_untaxed_signed) - FROM account_move - WHERE state!='draft' - AND (move_type='out_invoice' OR move_type='out_refund') - AND date > %s AND date <= %s - ) AS facture, - (SELECT SUM(amount_residual_signed) - FROM account_move - WHERE state!='draft' AND move_type='out_invoice' - ) AS a_encaisser, - (SELECT SUM(amount_residual_signed) - FROM account_move - WHERE state!='draft' AND move_type='in_invoice' - ) AS a_payer, - (SELECT SUM(expected_revenue * probability / 100) - FROM crm_lead - WHERE active=True - AND (date_deadline <= %s OR date_deadline is NULL) - ) AS pipe, - (SELECT SUM(expected_revenue * probability / 100) - FROM crm_lead WHERE active=True AND date_deadline > %s - ) AS pipe_n1, - (SELECT SUM(expected_revenue * probability / 100) - FROM crm_lead - WHERE active=True - AND probability = 100 - AND (date_deadline <= %s OR date_deadline is NULL) - ) AS pipe_win, - (SELECT SUM(expected_revenue * probability / 100) - FROM crm_lead - WHERE active=True - AND probability != 100 - AND (date_deadline < %s OR date_deadline is NULL) - ) AS pipe_to_win, - (SELECT date - FROM account_bank_statement - ORDER BY ID DESC LIMIT 1 - ) AS date_maj, - (SELECT SUM(amount) - FROM account_bank_statement_line - ) AS tresorerie, - (SELECT SUM(amount) - FROM account_bank_statement_line - WHERE amount > 0 AND date > %s - ) AS entree, - (SELECT SUM(amount) - FROM account_bank_statement_line - WHERE amount < 0 AND date > %s - ) AS sortie, - (SELECT SUM(amount) - FROM account_bank_statement_line - WHERE date > %s - ) AS variation, - (SELECT SUM(e.total_amount) - FROM hr_expense_sheet es, hr_expense e - WHERE es.id = e.sheet_id - AND e.payment_mode = 'own_account' - AND es.state != 'done' - ) AS cca, - (SELECT SUM(price_subtotal - qty_invoiced * price_unit) - FROM sale_order_line - WHERE invoice_status = 'to invoice' - ) AS commandes; - """ - % ( - fiscal_year, - fiscal_year_next, - fiscal_year_next, - fiscal_year_next, - fiscal_year_next, - fiscal_year_next, - fiscal_year, - fiscal_year, - fiscal_year, - ) - ) - datas = self._cr.dictfetchall() - - self._cr.execute("SELECT ca_target FROM res_company;") - ca_target = self._cr.dictfetchall() - - self._cr.execute("SELECT sum(capital) AS capital FROM hr_employee;") - capital = self._cr.dictfetchall() - - if datas[0]["facture"]: - res["facture"] += datas[0]["facture"] - if datas[0]["a_encaisser"]: - res["a_encaisser"] += datas[0]["a_encaisser"] - if datas[0]["a_payer"]: - res["a_payer"] += datas[0]["a_payer"] - if datas[0]["pipe"]: - res["pipe"] += datas[0]["pipe"] - if datas[0]["pipe_win"]: - res["pipe_win"] += datas[0]["pipe_win"] - if datas[0]["pipe_to_win"]: - res["pipe_to_win"] += datas[0]["pipe_to_win"] - if datas[0]["pipe_n1"]: - res["pipe_n1"] += datas[0]["pipe_n1"] - if datas[0]["tresorerie"]: - res["tresorerie"] += datas[0]["tresorerie"] - if datas[0]["date_maj"]: - res["date_maj"] = datas[0]["date_maj"] - if datas[0]["entree"]: - res["entree"] += datas[0]["entree"] - if datas[0]["sortie"]: - res["sortie"] += datas[0]["sortie"] - if datas[0]["variation"]: - res["variation"] += datas[0]["variation"] - if datas[0]["commandes"]: - res["commandes"] += datas[0]["commandes"] - if datas[0]["cca"]: - res["cca"] += datas[0]["cca"] - if ca_target[0]["ca_target"]: - res["target"] += ca_target[0]["ca_target"] - if capital[0]["capital"]: - res["capital"] += capital[0]["capital"] + res = {} + res['fiscal_year'] = fiscal_year + res['fiscal_year_next'] = fiscal_year_next + res['target'] = self.env.user.company_id.ca_target + + res['facture'] = sum(self.env['account.move'].search([ + ('move_type', 'in', ('out_invoice', 'out_refund')), + ('state', '=', 'posted'), + ('invoice_date', '>', fiscal_year), + ('invoice_date', '<=', fiscal_year_next) + ]).mapped('amount_untaxed_signed')) + + res['commandes'] = sum(self.env['sale.order.line'].search([ + ('invoice_status', '=', 'to invoice'), + ]).mapped('untaxed_amount_to_invoice')) + + res['pipe'] = sum(self.env['crm.lead'].search([ + '|', + ('date_deadline', '<=', fiscal_year_next), + ('date_deadline', '=', False), + ]).mapped(lambda l: l.expected_revenue * l.probability / 100)) + res['pipe_n1'] = sum(self.env['crm.lead'].search([ + ('date_deadline', '>', fiscal_year_next), + ]).mapped(lambda l: l.expected_revenue * l.probability / 100)) + res['pipe_win'] = sum(self.env['crm.lead'].search([ + ('probability', '=', 100), + '|', + ('date_deadline', '<=', fiscal_year_next), + ('date_deadline', '=', False), + ]).mapped(lambda l: l.expected_revenue * l.probability / 100)) + res['pipe_to_win'] = sum(self.env['crm.lead'].search([ + ('probability', '!=', 100), + '|', + ('date_deadline', '<=', fiscal_year_next), + ('date_deadline', '=', False), + ]).mapped(lambda l: l.expected_revenue * l.probability / 100)) + + res['a_encaisser'] = sum(self.env['account.move'].search([ + ('move_type', 'in', ('out_invoice', 'out_refund')), + ('state', '=', 'posted'), + ('invoice_date', '>', fiscal_year), + ('invoice_date', '<=', fiscal_year_next) + ]).mapped('amount_residual_signed')) + res['a_payer'] = sum(self.env['account.move'].search([ + ('move_type', 'in', ('in_invoice', 'in_refund')), + ('state', '=', 'posted'), + ('invoice_date', '>', fiscal_year), + ('invoice_date', '<=', fiscal_year_next) + ]).mapped('amount_residual_signed')) + + res['date_maj'] = self.env['account.bank.statement'].search([ + ('active', 'in', (True, False))], + order='date desc', + limit=1 + ).date + res['tresorerie'] = sum(self.env['account.bank.statement.line'].search([]).mapped('amount')) + res['entree'] = sum(self.env['account.bank.statement.line'].search([ + ('amount', '>', 0), + ('date', '>', fiscal_year), + ]).mapped('amount')) + res['sortie'] = sum(self.env['account.bank.statement.line'].search([ + ('amount', '<', 0), + ('date', '>', fiscal_year), + ]).mapped('amount')) + res['variation'] = sum(self.env['account.bank.statement.line'].search([ + ('date', '>', fiscal_year), + ]).mapped('amount')) return res -- GitLab