Skip to Content
Menu
This question has been flagged

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.


Avatar
Discard
Best Answer

To transfer documents from sale.order to project.task in Odoo, you need to create a relationship between these two models. This can be achieved by creating a One2many or Many2many field in the sale.order model that references the project.task model.

Here is a basic example of how you might set up this relationship in your models:


class SaleOrder(models.Model):

    _inherit = 'sale.order'


    task_ids = fields.One2many('project.task', 'sale_order_id', string='Tasks')


class ProjectTask(models.Model):

    _inherit = 'project.task'


    sale_order_id = fields.Many2one('sale.order', string='Sale Order')

    document_ids = fields.Many2many('ir.attachment', string='Documents')


In this example, task_ids is a One2many field that gets the tasks related to the sale order, and sale_order_id is a Many2one field that gets the sale order related to the task. document_ids is a Many2many field that gets the documents related to the task.

You can then override the create and write methods in the sale.order model to update the document_ids field in the related tasks whenever a sale order is created or updated.


Avatar
Discard
Related Posts Replies Views Activity
0
Apr 24
933
0
Sep 23
1699
1
Sep 23
3997
1
Nov 22
2792
1
Mar 24
2506