[REF] stock_dropshipping: multi company

Now that company_id is required on the picking type, make sure we create
as many dropship picking type/rules/sequence as there are companies

task-1985992
This commit is contained in:
Simon Lejeune
2019-09-03 15:05:03 +00:00
parent b6f053b25e
commit 27a4c07187
4 changed files with 131 additions and 26 deletions
+5 -26
View File
@@ -1,24 +1,9 @@
<?xml version="1.0"?>
<odoo>
<data noupdate="1">
<record id="seq_picking_type_dropship" model="ir.sequence">
<field name="name">Dropship</field>
<field name="prefix">DS</field>
<field name="padding">5</field>
<field name="company_id" eval="False"/>
</record>
<function model="res.company" name="create_missing_dropship_sequence"/>
<function model="res.company" name="create_missing_dropship_picking_type"/>
<record id="picking_type_dropship" model="stock.picking.type">
<field name="name">Dropship</field>
<field name="sequence_code">DS</field>
<field name="sequence_id" ref="seq_picking_type_dropship"/>
<field name="code">incoming</field>
<field name="warehouse_id" eval="False"/>
<field name="default_location_src_id" ref="stock.stock_location_suppliers"/>
<field name="default_location_dest_id" ref="stock.stock_location_customers"/>
</record>
<!-- Drop Shipping-->
<record id="route_drop_shipping" model='stock.location.route'>
<field name="name">Dropship</field>
<field name="sequence">3</field>
@@ -28,14 +13,8 @@
<field name="product_categ_selectable" eval="True"/>
</record>
<record id="stock_rule_drop_shipping" model="stock.rule">
<field name="name">Vendor → Customer (Dropshipping)</field>
<field name="action">buy</field>
<field name="location_id" ref="stock.stock_location_customers"/>
<field name="location_src_id" ref="stock.stock_location_suppliers"/>
<field name="procure_method">make_to_stock</field>
<field name="route_id" ref="route_drop_shipping"/>
<field name="picking_type_id" ref="picking_type_dropship"/>
</record>
<function model="res.company" name="create_missing_dropship_sequence"/>
<function model="res.company" name="create_missing_dropship_picking_type"/>
<function model="res.company" name="create_missing_dropship_rule"/>
</data>
</odoo>
@@ -2,4 +2,6 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import purchase
from . import res_company
from . import sale
from . import stock
@@ -0,0 +1,109 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models, _
class ResCompany(models.Model):
_inherit = 'res.company'
# -------------------------------------------------------------------------
# Sequences
# -------------------------------------------------------------------------
def _create_dropship_sequence(self):
dropship_vals = []
for company in self:
dropship_vals.append({
'name': 'Dropship (%s)' % company.name,
'code': 'stock.dropshipping',
'company_id': company.id,
'prefix': 'DS/',
'padding': 5,
})
if dropship_vals:
self.env['ir.sequence'].create(dropship_vals)
@api.model
def create_missing_dropship_sequence(self):
company_ids = self.env['res.company'].search([])
company_has_dropship_seq = self.env['ir.sequence'].search([('code', '=', 'stock.dropshippping')]).mapped('company_id')
company_todo_sequence = company_ids - company_has_dropship_seq
company_todo_sequence._create_dropship_sequence()
def _create_per_company_sequences(self):
super(ResCompany, self)._create_per_company_sequences()
self._create_dropship_sequence()
# -------------------------------------------------------------------------
# Picking types
# -------------------------------------------------------------------------
def _create_dropship_picking_type(self):
dropship_vals = []
for company in self:
sequence = self.env['ir.sequence'].search([
('code', '=', 'stock.dropshipping'),
('company_id', '=', company.id),
])
dropship_vals.append({
'name': 'Dropship',
'company_id': company.id,
'warehouse_id': False,
'sequence_id': sequence.id,
'code': 'incoming',
'default_location_src_id': self.env.ref('stock.stock_location_suppliers').id,
'default_location_dest_id': self.env.ref('stock.stock_location_customers').id,
'sequence_code': 'DS',
})
if dropship_vals:
self.env['stock.picking.type'].create(dropship_vals)
@api.model
def create_missing_dropship_picking_type(self):
company_ids = self.env['res.company'].search([])
company_has_dropship_picking_type = self.env['stock.picking.type'].search([('name', '=', 'Dropship')]).mapped('company_id')
company_todo_picking_type = company_ids - company_has_dropship_picking_type
company_todo_picking_type._create_dropship_picking_type()
def _create_per_company_picking_types(self):
super(ResCompany, self)._create_per_company_picking_types()
self._create_dropship_picking_type()
# -------------------------------------------------------------------------
# Stock rules
# -------------------------------------------------------------------------
def _create_dropship_rule(self):
dropship_route = self.env.ref('stock_dropshipping.route_drop_shipping')
supplier_location = self.env.ref('stock.stock_location_suppliers')
customer_location = self.env.ref('stock.stock_location_customers')
dropship_vals = []
for company in self:
dropship_picking_type = self.env['stock.picking.type'].search([
('name', '=', 'Dropship'),
('company_id', '=', company.id),
])
dropship_vals.append({
'name': '%s → %s' % (supplier_location.name, customer_location.name),
'action': 'buy',
'location_id': customer_location.id,
'location_src_id': supplier_location.id,
'procure_method': 'make_to_stock',
'route_id': dropship_route.id,
'picking_type_id': dropship_picking_type.id,
'company_id': company.id,
})
if dropship_vals:
self.env['stock.rule'].create(dropship_vals)
@api.model
def create_missing_dropship_rule(self):
dropship_route = self.env.ref('stock_dropshipping.route_drop_shipping')
company_ids = self.env['res.company'].search([])
company_has_dropship_rule = self.env['stock.rule'].search([('route_id', '=', dropship_route.id)]).mapped('company_id')
company_todo_rule = company_ids - company_has_dropship_rule
company_todo_rule._create_dropship_rule()
def _create_per_company_rules(self):
super(ResCompany, self)._create_per_company_rules()
self._create_dropship_rule()
+15
View File
@@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, models
class ProcurementGroup(models.Model):
_inherit = "procurement.group"
@api.model
def _get_rule_domain(self, location, values):
if 'sale_line_id' in values and values.get('company_id'):
return [('location_id', '=', location.id), ('action', '!=', 'push'), ('company_id', '=', values['company_id'].id)]
else:
return super(ProcurementGroup, self)._get_rule_domain(location, values)