[IMP] sale_stock : add warehouse_id field on user form
We had a default warehouse_id on the users to allow a better stock managment based on the sale person. The warehouse_id on a sales order is now computed from the property_warehouse_id field on a user. If the user doesn't have a property_warehouse_id set the warehouse_id on the sales order is the one with the smallest sequence number in the company of the SO. Task-2166382
This commit is contained in:
@@ -16,12 +16,6 @@
|
||||
<h1 position="replace"/>
|
||||
<widget name="notification_alert" position="replace"/>
|
||||
<xpath expr="//field[@name='image_1920']" position="replace"/>
|
||||
<field name="signature" position="replace" />
|
||||
<xpath expr="//group[@name='preference_contact']" position="before">
|
||||
<group>
|
||||
<field name="signature" />
|
||||
</group>
|
||||
</xpath>
|
||||
<xpath expr="//field[@name='company_id']" position="attributes">
|
||||
<attribute name="invisible">1</attribute>
|
||||
</xpath>
|
||||
|
||||
@@ -29,6 +29,7 @@ Preferences
|
||||
'views/sale_stock_portal_template.xml',
|
||||
'views/stock_production_lot_views.xml',
|
||||
'views/report_invoice.xml',
|
||||
'views/res_users_views.xml',
|
||||
'report/sale_order_report_templates.xml',
|
||||
'report/stock_report_deliveryslip.xml',
|
||||
'data/sale_stock_data.xml',
|
||||
|
||||
@@ -7,3 +7,4 @@ from . import sale_order
|
||||
from . import res_config_settings
|
||||
from . import stock
|
||||
from . import product_template
|
||||
from . import res_users
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import models, fields
|
||||
|
||||
|
||||
class Users(models.Model):
|
||||
_inherit = ['res.users']
|
||||
|
||||
property_warehouse_id = fields.Many2one('stock.warehouse', string='Default Warehouse', company_dependent=True, check_company=True)
|
||||
|
||||
def _get_default_warehouse_id(self):
|
||||
if self.property_warehouse_id:
|
||||
return self.property_warehouse_id
|
||||
return self.env['stock.warehouse'].search([('company_id', '=', self.env.company.id)], limit=1)
|
||||
|
||||
def __init__(self, pool, cr):
|
||||
""" Override of __init__ to add access rights.
|
||||
Access rights are disabled by default, but allowed
|
||||
on some specific fields defined in self.SELF_{READ/WRITE}ABLE_FIELDS.
|
||||
"""
|
||||
|
||||
sale_stock_writeable_fields = [
|
||||
'property_warehouse_id',
|
||||
]
|
||||
|
||||
init_res = super().__init__(pool, cr)
|
||||
# duplicate list to avoid modifying the original reference
|
||||
type(self).SELF_READABLE_FIELDS = type(self).SELF_READABLE_FIELDS + sale_stock_writeable_fields
|
||||
type(self).SELF_WRITEABLE_FIELDS = type(self).SELF_WRITEABLE_FIELDS + sale_stock_writeable_fields
|
||||
return init_res
|
||||
@@ -15,9 +15,7 @@ class SaleOrder(models.Model):
|
||||
|
||||
@api.model
|
||||
def _default_warehouse_id(self):
|
||||
company = self.env.company.id
|
||||
warehouse_ids = self.env['stock.warehouse'].search([('company_id', '=', company)], limit=1)
|
||||
return warehouse_ids
|
||||
return self.user_id._get_default_warehouse_id()
|
||||
|
||||
incoterm = fields.Many2one(
|
||||
'account.incoterms', 'Incoterm',
|
||||
@@ -66,9 +64,10 @@ class SaleOrder(models.Model):
|
||||
|
||||
@api.model
|
||||
def create(self, vals):
|
||||
if 'warehouse_id' not in vals and 'company_id' in vals and vals.get('company_id') != self.env.company.id:
|
||||
vals['warehouse_id'] = self.env['stock.warehouse'].search([('company_id', '=', vals.get('company_id'))], limit=1).id
|
||||
return super(SaleOrder, self).create(vals)
|
||||
if 'warehouse_id' not in vals and 'company_id' in vals:
|
||||
user = self.env['res.users'].browse(vals.get('user_id', False))
|
||||
vals['warehouse_id'] = user.with_company(vals.get('company_id'))._get_default_warehouse_id().id
|
||||
return super().create(vals)
|
||||
|
||||
def write(self, values):
|
||||
if values.get('order_line') and self.state == 'sale':
|
||||
@@ -123,7 +122,12 @@ class SaleOrder(models.Model):
|
||||
@api.onchange('company_id')
|
||||
def _onchange_company_id(self):
|
||||
if self.company_id:
|
||||
self.warehouse_id = self.env['stock.warehouse'].search([('company_id', '=', self.company_id.id)], limit=1)
|
||||
self.warehouse_id = self.user_id.with_company(self.company_id.id)._get_default_warehouse_id().id
|
||||
|
||||
@api.onchange('user_id')
|
||||
def onchange_user_id(self):
|
||||
super().onchange_user_id()
|
||||
self.warehouse_id = self.user_id.with_company(self.company_id.id)._get_default_warehouse_id().id
|
||||
|
||||
@api.onchange('partner_shipping_id')
|
||||
def _onchange_partner_shipping_id(self):
|
||||
|
||||
@@ -6,3 +6,4 @@ from . import test_anglo_saxon_valuation_reconciliation
|
||||
from . import test_sale_stock
|
||||
from . import test_sale_stock_lead_time
|
||||
from . import test_sale_order_dates
|
||||
from . import test_sale_stock_multicompany
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from odoo.addons.sale.tests.test_sale_common import TestSaleCommon
|
||||
from odoo.tests import tagged
|
||||
from odoo.tests.common import new_test_user
|
||||
|
||||
|
||||
@tagged('post_install', '-at_install')
|
||||
class TestSaleStockMultiCompany(TestSaleCommon):
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super().setUpClass()
|
||||
cls.company_B = cls.env['res.company'].create({'name': 'Company B'})
|
||||
cls.warehouse_A = cls.env['stock.warehouse'].create({'name': 'WH A', 'code': 'WHA', 'company_id': cls.env.company.id, 'partner_id': cls.env.company.partner_id.id})
|
||||
cls.warehouse_A2 = cls.env['stock.warehouse'].create({'name': 'WH A 2', 'code': 'WHA2', 'company_id': cls.env.company.id, 'partner_id': cls.env.company.partner_id.id, 'sequence': 5})
|
||||
cls.warehouse_B = cls.env['stock.warehouse'].create({'name': 'WH B', 'code': 'WHB', 'company_id': cls.company_B.id, 'partner_id': cls.company_B.partner_id.id})
|
||||
cls.warehouse_user = new_test_user(cls.env, 'WarehouseUser', groups='base.group_user,stock.group_stock_user,stock.group_stock_multi_locations,sales_team.group_sale_salesman', company_ids=[(6, 0, (cls.env.company | cls.company_B).ids)])
|
||||
cls.warehouse_user.with_company(cls.env.company).property_warehouse_id = cls.warehouse_A.id
|
||||
cls.warehouse_user.with_company(cls.company_B).property_warehouse_id = cls.warehouse_B.id
|
||||
|
||||
def test_warehouse_definition_on_so(self):
|
||||
|
||||
partner = self.partner
|
||||
product = self.products['prod_del']
|
||||
|
||||
sale_order_vals = {
|
||||
'partner_id': partner.id,
|
||||
'partner_invoice_id': partner.id,
|
||||
'partner_shipping_id': partner.id,
|
||||
'user_id': False,
|
||||
'company_id': self.env.company.id,
|
||||
'order_line': [(0, 0, {
|
||||
'name': product.name,
|
||||
'product_id': product.id,
|
||||
'product_uom_qty': 10,
|
||||
'product_uom': product.uom_id.id,
|
||||
'price_unit': product.list_price})],
|
||||
'pricelist_id': self.env.ref('product.list0').id,
|
||||
}
|
||||
sale_order = self.env['sale.order']
|
||||
|
||||
so_no_user = sale_order.create(sale_order_vals)
|
||||
self.assertFalse(so_no_user.user_id.property_warehouse_id)
|
||||
self.assertEqual(so_no_user.warehouse_id.id, self.warehouse_A2.id)
|
||||
|
||||
sale_order_vals2 = {
|
||||
'partner_id': partner.id,
|
||||
'partner_invoice_id': partner.id,
|
||||
'partner_shipping_id': partner.id,
|
||||
'user_id': self.warehouse_user.id,
|
||||
'company_id': self.env.company.id,
|
||||
'order_line': [(0, 0, {
|
||||
'name': product.name,
|
||||
'product_id': product.id,
|
||||
'product_uom_qty': 10,
|
||||
'product_uom': product.uom_id.id,
|
||||
'price_unit': product.list_price})],
|
||||
'pricelist_id': self.env.ref('product.list0').id,
|
||||
}
|
||||
so_company_A = sale_order.with_company(self.env.company).create(sale_order_vals2)
|
||||
self.assertEqual(so_company_A.warehouse_id.id, self.warehouse_A.id)
|
||||
|
||||
sale_order_vals3 = {
|
||||
'partner_id': partner.id,
|
||||
'partner_invoice_id': partner.id,
|
||||
'partner_shipping_id': partner.id,
|
||||
'user_id': self.warehouse_user.id,
|
||||
'company_id': self.company_B.id,
|
||||
'order_line': [(0, 0, {
|
||||
'name': product.name,
|
||||
'product_id': product.id,
|
||||
'product_uom_qty': 10,
|
||||
'product_uom': product.uom_id.id,
|
||||
'price_unit': product.list_price})],
|
||||
'pricelist_id': self.env.ref('product.list0').id,
|
||||
}
|
||||
so_company_B = sale_order.with_company(self.company_B).create(sale_order_vals3)
|
||||
self.assertEqual(so_company_B.warehouse_id.id, self.warehouse_B.id)
|
||||
@@ -0,0 +1,44 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<odoo>
|
||||
<data>
|
||||
<record id="res_users_view_form_preferences" model="ir.ui.view">
|
||||
<field name="name">res.users.preferences.form.inherit</field>
|
||||
<field name="model">res.users</field>
|
||||
<field name="inherit_id" ref="base.view_users_form_simple_modif"/>
|
||||
<field name="arch" type="xml">
|
||||
<group name="signature" position="after">
|
||||
<group name="Warehouses">
|
||||
<field name="property_warehouse_id" groups="stock.group_stock_multi_warehouses"/>
|
||||
</group>
|
||||
</group>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record id="res_users_view_simple_form" model="ir.ui.view">
|
||||
<field name="name">res.users.simple.form.inherit</field>
|
||||
<field name="model">res.users</field>
|
||||
<field name="inherit_id" ref="base.view_users_simple_form"/>
|
||||
<field name="arch" type="xml">
|
||||
<group name="phone_numbers" position="after">
|
||||
<group name="Warehouses">
|
||||
<field name="property_warehouse_id" groups="stock.group_stock_multi_warehouses"/>
|
||||
</group>
|
||||
</group>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record id="res_users_view_form" model="ir.ui.view">
|
||||
<field name="name">res.users.form.inherit</field>
|
||||
<field name="model">res.users</field>
|
||||
<field name="inherit_id" ref="base.view_users_form"/>
|
||||
<field name="arch" type="xml">
|
||||
<group name="messaging" position="after">
|
||||
<group name="Warehouses" string="Inventory" groups="stock.group_stock_multi_warehouses">
|
||||
<field name="property_warehouse_id" groups="stock.group_stock_multi_warehouses"/>
|
||||
</group>
|
||||
</group>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
</data>
|
||||
</odoo>
|
||||
Reference in New Issue
Block a user