How to transfer/reflect documents from sale.order to project.task model? I am unable to achieve the result with my code.
The documents should not get jumbled. If sale order S0001 contains 3 documents then the project task related to S0001 should reflect 3 documents and if sale order S0003 contains 0 documents then the project task related to S0003 should reflect 0.
Please find the xml & py code below:
# -*- coding: utf-8 -*-
from odoo import api, fields, models, _
from Odoo16_Community.odoo.odoo.exceptions import AccessError
class ProjectTask(models.Model):
_inherit = 'project.task'
_description = 'Display smart button for attachments'
sale_line_id = fields.Many2one(
'sale.order.line', 'Sales Order Item',
copy=True, tracking=True, index='btree_not_null', recursive=True, store=True, readonly=True)
sale_order_id = fields.Many2one('sale.order', 'Sales Order', store=True,
help="Sales order to which the task is linked.")
display_attachment_button = fields.Boolean(string='Display Attachments',
compute='_compute_display_attachments_button')
attachment_count = fields.Integer('Attachments Count', compute='_compute_attachment_count')
attachments = fields.Char("Attachments Name", compute='_compute_attachment_name')
attachment_ids = fields.One2many(
'ir.attachment',
'res_id',
domain=[('res_model', '=', 'sale.order')],
string='Attachments'
)
def _compute_attachment_name(self):
for rec in self:
attachments = self.env['ir.attachment'].search([('res_model', '=', 'sale.order'), ('res_id', '=', rec.id)])
rec.attachments = attachments[0].name if rec.attachment_count
def _compute_attachment_count(self):
for rec in self:
attachments = self.env['ir.attachment'].search_count(
[('res_model', '=', 'sale.order'), ('res_id', '=', rec.id)])
rec.attachment_count = attachments
@api.depends('sale_line_id')
def _compute_display_attachments_button(self):
if not self.sale_line_id:
self.display_attachment_button = False
return
try:
sale_line = self.env['sale.order.line'].search([('id', 'in', self.sale_line_id.ids)])
for task in self:
task.display_attachment_button = task.sale_line_id in sale_line
except AccessError:
self.display_attachment_button = False
def action_show_attachments(self):
return {
'name': _('Attachments'),
'view_mode': 'kanban,form',
'res_model': 'ir.attachment',
'type': 'ir.actions.act_window',
'domain': [('res_model', '=', 'sale.order'), ('res_id', '=', self.id)]
}
def action_view_so(self):
return {
"type": "ir.actions.act_window",
"res_model": "sale.order",
"name": _("Sales Order"),
"views": [[False, "tree"], [False, "kanban"], [False, "form"]],
"context": {"create": False, "show_sale": True},
}
Thank you in advance.