Skip to Content
मेन्यू
This question has been flagged
4 Replies
859 Views

I want to be able to create a project from the sales order. Odoo seems to only allow this for a sales order with 'service' products, but I want to create a project for any sales order, regardless of the product.


The only way I can work out to achieve this is with an 'execute code' server action, as below.


This seems to achieve what I want, however as I am on an enterprise license I have to pay extra to use the code, so I am hoping there is a simpler solution to this. Also my python experience is quite limited and I am unsure if I have coded this in a reliable way...


Does anyone know of a better solution to this problem? any help is much appreciated.

if record.project_id:
    raise UserError("This Sales Order already has a linked project.")
   
# Get sequence number
seq = env['ir.sequence'].next_by_code('project.seq')

# Combine with opportunity name if available
if record.opportunity_id:
    opp_name = record.opportunity_id.name
else:
    opp_name = ""

if opp_name:
    project_name = f"{seq} | {opp_name}"
else:
    project_name = seq

# Create project
project = env['project.project'].create({
    'name': project_name,
    'partner_id': record.partner_id.id,
    'use_documents': False,
})

record.write({'project_id': project.id})


Avatar
Discard
Best Answer

Hello,

Please Refer the code:

1.Python code:

from odoo import models, fields, api


class SaleOrder(models.Model):

    _inherit = "sale.order"


    project_id = fields.Many2one("project.project", string="Project")


    def action_create_project(self):

        Project = self.env["project.project"]

        for order in self:

            if not order.project_id:

                project = Project.create({

                    "name": order.name,

                    "partner_id": project.id

        return True


2. Xml Code

<record id="view_order_form_inherit_project" model="ir.ui.view">

    <field name="name">sale.order.form.inherit.project</field>

    <field name="model">sale.order</field>

    <field name="inherit_id" ref="sale.view_order_form"/>

    <field name="arch" type="xml">

        <header position="inside">

            <button name="action_create_project"

                    type="object"

                    string="Create Project"

                    class="btn-primary"

                    attrs="{'invisible': [('project_id','!=',False)]}"/>

        </header>

        <sheet position="after">

            <field name="project_id"/>

        </sheet>

    </field>

</record>


Avatar
Discard

This isn't an answer to the question that has been asked!

Best Answer

Hi,

Please refer to the code:


from odoo import api, fields, models

from odoo.exceptions import UserError


class SaleOrder(models.Model):

    _inherit = "sale.order"


    project_id = fields.Many2one('project.project', string="Project")


    def action_create_project(self):

        for order in self:

            if order.project_id:

                raise UserError("This Sales Order already has a linked project.")


            seq = self.env['ir.sequence'].next_by_code('project.seq') or order.name

            opp_name = order.opportunity_id.name if order.opportunity_id else ""

            project_name = f"{seq} | {opp_name}" if opp_name else seq


            project = self.env['project.project'].create({

                'name': project_name,

                'partner_id': order.partner_id.id,

            })

            order.project_id = project.id


Hope it helps.

Avatar
Discard
Best Answer

In Odoo, a project is typically created from a Sales Order when you sell a service product that is configured to trigger project creation.

✅ Steps:

  1. Go to Sales > Products and open the service product.
  2. Set:
    • Product Type: Service
    • Service Invoicing Policy: Based on Milestones or Timesheets on Tasks
    • Service Tracking: Choose one of the following:
      • Create a task in an existing project
      • Create a new project but no task
      • Create a new project and task
  3. When you confirm a Sales Order containing that product, Odoo will automatically create the project or task depending on your selection.

You can then manage the project from the Projects module.

Avatar
Discard
Best Answer

Hi,

if record. project_id:

    raise UserError("This Sales Order already has a linked project.")


# Get sequence value or fallback

seq = env['ir.sequence'].next_by_code('project.seq') or 'NEW/PROJECT'


# Get opportunity name if exists

opp_name = record.opportunity_id.name if record.opportunity_id else ''


# Build project name

project_name = f"{seq} | {opp_name}" if opp_name else seq


# Create new project

project = env['project.project'].create({

    'name': project_name,

    'partner_id': project.id })

try this 
i hope it is use full

Avatar
Discard
Related Posts Replies Views Activity
1
जून 22
6469
3
फ़र॰ 20
3849
1
जुल॰ 19
15339
1
अप्रैल 24
1719
0
सित॰ 23
1637