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