Skip to Content
Menu
This question has been flagged

I have the following case: When I create an SO in company 1, a purchase order is made accordingly in the same company. Then, when I confirm the purchase order, a sales order is created in company 2. This is achieved by activating the syncing option from the company level.


But, when I create a sales order in company 2 and I confirm it, a purchase order is created in company 1 and this is not my case. I just need the first case to be done. What are the exact steps to achieve this step please?

Avatar
Discard
Best Answer

Hi,

We can easily achieve this by the following code,
First, we need to inherit a field in settings or any place you want,


from odoo import fields, models


class ResConfigSettings(models.TransientModel):
"""Inherited the model to add the fields"""
_inherit = 'res.config.settings'

sale_purchase_synchronization = fields.Boolean(string='Synchronization',
config_parameter='inter_company_synchronization.
sale_purchase_synchronization'
,
help='Boolean field is use '
'to enable the inter '
'company sale and '
'purchase '
'synchronization'
)


Then the next step we need to super the confirm button in both sales and purchases.

from odoo import models


class PurchaseOrder(models.Model):
"""Inherited this model to create corresponding SO while creating PO"""
_inherit = 'purchase.order'

def button_confirm(self):
"""Super the button confirm function to create SO to the
corresponding company when confirming the PO."""
res = super(PurchaseOrder, self).button_confirm()
res_config_settings = self.env['res.config.settings'].search([])
company_id = self.env['res.company'].search(
[('name', '=', self.partner_id.name)])
transit_locations = self.env['stock.location'].search(
[('active', '=', True), ('usage', '=', 'transit')])
if transit_locations and res_config_settings and \
res_config_settings[-1].sale_purchase_synchronization:
self.env['sale.order'].sudo().create({
'partner_id': self.env.company.partner_id.id,
'company_id': company_id.id,
'client_order_ref': self.name,
'order_line': [(0, 0, {
'product_id': rec.product_id.id,
'product_uom_qty': rec.product_qty,
'price_unit': rec.price_unit,
'tax_id': rec.taxes_id,
'price_subtotal': rec.price_subtotal
}) for rec in self.order_line],
})
return res
from odoo import models


class SaleOrder(models.Model):
"""Inherit to create PO while create SO"""
_inherit = 'sale.order'

def action_confirm(self):
"""Super the function to create PO to the corresponding company
when confirming the SO"""
res = super(SaleOrder, self).action_confirm()
res_config_settings = self.env['res.config.settings'].search([])
company_id = self.env['res.company'].search(
[('name', '=', self.partner_id.name)])
transit_locations = self.env['stock.location'].search(
[('active', '=', True), ('usage', '=', 'transit')])
if transit_locations and res_config_settings and \
res_config_settings[-1].sale_purchase_synchronization:
self.env['purchase.order'].create({
'partner_id': self.env.company.id,
'company_id': company_id.id,
'origin': self.name,
'order_line': [(0, 0, {
'product_id': record.product_id.id,
'product_qty': record.product_uom_qty,
'price_unit': record.price_unit,
'price_subtotal': record.price_subtotal,
}) for record in self.order_line],
})
return res

Hope it helps

Avatar
Discard
Related Posts Replies Views Activity
4
Jan 24
3751
2
Mar 20
3278
0
Nov 23
1878
0
Apr 21
2922
2
Dec 19
3599