diff --git a/odex25_accounting/account_attachments/models/account_move.py b/odex25_accounting/account_attachments/models/account_move.py index 202ddc6bc..4afd05db5 100755 --- a/odex25_accounting/account_attachments/models/account_move.py +++ b/odex25_accounting/account_attachments/models/account_move.py @@ -79,36 +79,54 @@ class AccountMove(models.Model): def get_attachments(self): # Check if multiple records are passed, and handle them in a loop + Attachment = self.env['ir.attachment'] + action = self.env['ir.actions.act_window']._for_xml_id('base.action_attachment') + if len(self) > 1: - print("ffffffffffffff") - action = self.env['ir.actions.act_window']._for_xml_id('base.action_attachment') - action['domain'] = [ - ('res_model', '=', 'account.move'), - ('res_id', 'in', self.ids), - ] + print("Multiple records mode") + all_models_ids = set() - # Update attachment count for all records (if necessary) for record in self: - related_ids = record.ids - related_models = ['account.move'] + related_pairs = set() - if record.res_id and record.res_model: - related_ids = record.ids + [record.res_id] - related_models.append(record.res_model) - action['domain'] = [ - ('res_model', 'in', related_models), - ('res_id', 'in', related_ids), - ] + # Main record + related_pairs.add((record._name, record.id)) - # Context for creating new attachments for each record - action['context'] = "{'default_res_model': '%s','default_res_id': %d}" % (record._name, record.id) + # Purchase Order + if record.purchase_id: + related_pairs.add((record.purchase_id._name, record.purchase_id.id)) - # Update attachment count for each record - record.attach_no = self.env['ir.attachment'].search_count([ - ('res_model', 'in', related_models), - ('res_id', 'in', related_ids) + # Request ID + if record.purchase_id.request_id: + related_pairs.add((record.purchase_id.request_id._name, record.purchase_id.request_id.id)) + + # Requisition ID (only if it's the expected model) + if record.purchase_id.requisition_id and record.purchase_id.requisition_id._name == 'purchase.requisition': + related_pairs.add(('purchase.requisition', record.purchase_id.requisition_id.id)) + + # Accumulate all related model-id pairs + all_models_ids.update(related_pairs) + + # Build domain from accumulated pairs + domain = [] + pairs = list(all_models_ids) + if pairs: + domain = ['|'] * (len(pairs) - 1) + for model, res_id in pairs: + domain.extend(['&', ('res_model', '=', model), ('res_id', '=', res_id)]) + + # Use the first record for context + action['domain'] = domain + action['context'] = { + 'default_res_model': self[0]._name, + 'default_res_id': self[0].id, + } + + for record in self: + record.attach_no = Attachment.search_count([ + ('res_model', '=', record._name), + ('res_id', '=', record.id) ]) - return action # If only one record is passed, use the original logic @@ -142,36 +160,6 @@ class AccountMove(models.Model): ('res_model', '=', model), ('res_id', '=', res_id) ]) - - # action = self.env['ir.actions.act_window']._for_xml_id('base.action_attachment') - # action['domain'] = [ - # ('res_model', '=', 'account.move'), - # ('res_id', 'in', self.ids), - # ] - # domain = [ - # ('res_model', '=', 'account.move'), - # ('res_id', 'in', self.ids), - # ] - # related_ids = self.ids - # related_models = ['account.move'] - # - # if self.res_id and self.res_model: - # related_ids = self.ids + [self.res_id] - # related_models.append(self.res_model) - # action['domain'] = [ - # ('res_model', 'in', related_models), - # ('res_id', 'in', related_ids), - # ] - # domain = [ - # ('res_model', 'in', related_models), - # ('res_id', 'in', related_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 - action = self.env['ir.actions.act_window']._for_xml_id('base.action_attachment') action['domain'] = domain action['context'] = {