From 781e8989f58a7a197b4115322bc182cb59a6c344 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Thu, 20 May 2021 12:25:33 +0200 Subject: [PATCH] Add filter no_attachment on sale.order (same as on account.invoice) --- account_usability/account.py | 4 ++-- sale_usability/sale.py | 27 +++++++++++++++++++++++++++ sale_usability/sale_view.xml | 4 ++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/account_usability/account.py b/account_usability/account.py index 37a7fb2..cad842e 100644 --- a/account_usability/account.py +++ b/account_usability/account.py @@ -57,11 +57,11 @@ class AccountInvoice(models.Model): def _compute_has_attachment(self): iao = self.env['ir.attachment'] for inv in self: - if iao.search([ + if iao.search_count([ ('res_model', '=', 'account.invoice'), ('res_id', '=', inv.id), ('type', '=', 'binary'), - ('company_id', '=', inv.company_id.id)], limit=1): + ('company_id', '=', inv.company_id.id)]): inv.has_attachment = True else: inv.has_attachment = False diff --git a/sale_usability/sale.py b/sale_usability/sale.py index a94f403..b5a3285 100644 --- a/sale_usability/sale.py +++ b/sale_usability/sale.py @@ -27,6 +27,9 @@ class SaleOrder(models.Model): # for reports has_discount = fields.Boolean( compute='_compute_has_discount', readonly=True) + has_attachment = fields.Boolean( + compute='_compute_has_attachment', + search='_search_has_attachment', readonly=True) @api.multi def _compute_has_discount(self): @@ -39,6 +42,30 @@ class SaleOrder(models.Model): break order.has_discount = has_discount + def _compute_has_attachment(self): + iao = self.env['ir.attachment'] + for order in self: + if iao.search_count([ + ('res_model', '=', 'sale.order'), + ('res_id', '=', order.id), + ('type', '=', 'binary'), + ('company_id', '=', order.company_id.id)]): + order.has_attachment = True + else: + order.has_attachment = False + + def _search_has_attachment(self, operator, value): + att_order_ids = {} + if operator == '=': + search_res = self.env['ir.attachment'].search_read([ + ('res_model', '=', 'sale.order'), + ('type', '=', 'binary'), + ('res_id', '!=', False)], ['res_id']) + for att in search_res: + att_order_ids[att['res_id']] = True + res = [('id', value and 'in' or 'not in', att_order_ids.keys())] + return res + @api.multi def action_confirm(self): '''Reload view upon order confirmation to display the 3 qty cols''' diff --git a/sale_usability/sale_view.xml b/sale_usability/sale_view.xml index 6d2677b..51947c6 100644 --- a/sale_usability/sale_view.xml +++ b/sale_usability/sale_view.xml @@ -75,6 +75,10 @@ analytic.group_analytic_accounting + + + +