diff --git a/odex25_accounting/account_attachments/models/account_move.py b/odex25_accounting/account_attachments/models/account_move.py index 2967d955c..cb83ae45f 100755 --- a/odex25_accounting/account_attachments/models/account_move.py +++ b/odex25_accounting/account_attachments/models/account_move.py @@ -9,72 +9,174 @@ class AccountMove(models.Model): res_id = fields.Integer() res_model = fields.Char() + # def get_attachments(self): + # # Check if multiple records are passed, and handle them in a loop + # if len(self) > 1: + # action = self.env['ir.actions.act_window']._for_xml_id('base.action_attachment') + # action['domain'] = [ + # ('res_model', '=', 'account.move'), + # ('res_id', 'in', self.ids), + # ] + # + # # Update attachment count for all records (if necessary) + # for record in self: + # related_ids = record.ids + # related_models = ['account.move'] + # + # 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), + # ] + # + # # Context for creating new attachments for each record + # action['context'] = "{'default_res_model': '%s','default_res_id': %d}" % (record._name, record.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) + # ]) + # + # return action + # + # # If only one record is passed, use the original logic + # self.ensure_one() + # + # 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 + # self.attach_no = self.env['ir.attachment'].search_count(domain) + # + # return action + def get_attachments(self): # Check if multiple records are passed, and handle them in a loop 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), + ('res_id', 'in', self.ids), ] - + # Update attachment count for all records (if necessary) for record in self: related_ids = record.ids related_models = ['account.move'] - + 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), + ('res_id', 'in', related_ids), ] - + # Context for creating new attachments for each record action['context'] = "{'default_res_model': '%s','default_res_id': %d}" % (record._name, record.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) ]) - + return action - + # If only one record is passed, use the original logic self.ensure_one() - - 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) - + self_model = self._name + print("22222222222") + + related_pairs = [] + + # Current record + related_pairs.append((self_model, self.id)) + + # Request record + if self.purchase_id.request_id: + related_pairs.append((self.purchase_id.request_id._name, self.purchase_id.request_id.id)) + + # Requisition record (only if exact model match) + if self.purchase_id.requisition_id and self.purchase_id.requisition_id._name == 'purchase.requisition': + related_pairs.append(('purchase.requisition', self.purchase_id.requisition_id.id)) + + # Build domain with explicit pairs + domain = [] + if related_pairs: + domain = ['|'] * (len(related_pairs) - 1) + for model, res_id in related_pairs: + domain.extend([ + '&', + ('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'] = { + 'default_res_model': self_model, + 'default_res_id': self.id, + } self.attach_no = self.env['ir.attachment'].search_count(domain) - + return action