From ffeaa4740bbb28e74e7ed42594dd6affa2b55580 Mon Sep 17 00:00:00 2001 From: zainab2097 <149927291+zainab2097@users.noreply.github.com> Date: Mon, 9 Sep 2024 11:19:01 +0300 Subject: [PATCH] Update account_move --- .../models/account_move.py | 88 +++++++++++-------- 1 file changed, 49 insertions(+), 39 deletions(-) diff --git a/odex25_project/account_attachments/models/account_move.py b/odex25_project/account_attachments/models/account_move.py index 255ae56ea..01bd4c22e 100755 --- a/odex25_project/account_attachments/models/account_move.py +++ b/odex25_project/account_attachments/models/account_move.py @@ -5,48 +5,58 @@ class AccountMove(models.Model): attach_no = fields.Integer(compute='get_attachments') - def get_attachments(self): - action = self.env['ir.actions.act_window']._for_xml_id('base.action_attachment') - action['domain'] = str([('res_model', '=', 'account.move'), ('res_id', 'in', self.ids)]) - action['context'] = "{'default_res_model': '%s','default_res_id': %d}" % (self._name, self.id) - domain = [('res_model', '=', 'account.move'), ('res_id', '=', self.id)] - self.attach_no = self.env['ir.attachment'].search_count(domain) - return action - # def get_attachments(self): - # # Action for opening the attachment window # action = self.env['ir.actions.act_window']._for_xml_id('base.action_attachment') - # # Prepare domain for attachments related to account.move and project.invoice - # account_move_ids = self.ids # IDs of account.move - # # Get all related project.invoice records based on the invoice_id - # project_invoice_ids = self.env['project.invoice'].search([('invoice_id', 'in', account_move_ids)]).ids - # - # # Expand the domain to include both account.move and project.invoice attachments - # action['domain'] = [ - # '|', # OR operator to include attachments from both models - # '&', # AND operator for account.move - # ('res_model', '=', 'account.move'), - # ('res_id', 'in', account_move_ids), - # '&', # AND operator for project.invoice - # ('res_model', '=', 'project.invoice'), - # ('res_id', 'in', project_invoice_ids), - # ] - # - # # Context for creating new attachments + # action['domain'] = str([('res_model', '=', 'account.move'), ('res_id', 'in', self.ids)]) # action['context'] = "{'default_res_model': '%s','default_res_id': %d}" % (self._name, self.id) - # - # # Update attachment count for smart button - # domain = [ - # '|', # OR operator for counting attachments from both models - # '&', # AND operator for the first condition (account.move) - # ('res_model', '=', 'account.move'), - # ('res_id', 'in', account_move_ids), - # '&', # AND operator for the second condition (project.invoice) - # ('res_model', '=', 'project.invoice'), - # ('res_id', 'in', project_invoice_ids), - # ] - # + # domain = [('res_model', '=', 'account.move'), ('res_id', '=', self.id)] # self.attach_no = self.env['ir.attachment'].search_count(domain) - # # return action + def get_attachments(self): + # Action for opening the attachment window + action = self.env['ir.actions.act_window']._for_xml_id('base.action_attachment') + # Prepare domain for attachments related to account.move and project.invoice + account_move_ids = self.ids # IDs of account.move + # Get all related project.invoice records based on the invoice_id + project_invoice_ids = self.env['project.invoice'].search([('invoice_id', 'in', account_move_ids)]).ids + + # Expand the domain to include both account.move and project.invoice attachments + # Combine account.move and project.invoice ids + related_ids = account_move_ids + project_invoice_ids + action['domain'] = [ + ('res_model', 'in', ['account.move', 'project.invoice']), + ('res_id', 'in', related_ids),] + + # action['domain'] = [ + # '|', # OR operator to include attachments from both models + # '&', # AND operator for account.move + # ('res_model', '=', 'account.move'), + # ('res_id', 'in', account_move_ids), + # '&', # AND operator for project.invoice + # ('res_model', '=', 'project.invoice'), + # ('res_id', 'in', project_invoice_ids), + # ] + + # Context for creating new attachments + action['context'] = "{'default_res_model': '%s','default_res_id': %d}" % (self._name, self.id) + # Update attachment count for smart button + domain = [ + ('res_model', 'in', ['account.move', 'project.invoice']), + ('res_id', 'in', related_ids),] + + # Update attachment count for smart button + # domain = [ + # '|', # OR operator for counting attachments from both models + # '&', # AND operator for the first condition (account.move) + # ('res_model', '=', 'account.move'), + # ('res_id', 'in', account_move_ids), + # '&', # AND operator for the second condition (project.invoice) + # ('res_model', '=', 'project.invoice'), + # ('res_id', 'in', project_invoice_ids), + # ] + + self.attach_no = self.env['ir.attachment'].search_count(domain) + + return action +