[FIX] purchase_stock : prevent wrong destination in PO

Steps to reproduce the bug:
- create 2 warehouses A & B
- create a new product and add a seller
- Create an Automated replenishment order of the created product for warehouse A
- click on “Automate orders”
- open PO
- in “other information” tab > change the picking type to the “warehouse B” picking type
- confirm order
- click on the receipt

Problem:
The destination location is “Warehouse A” instead of “Warehouse B”
The problem also appears after the duplication.

Solution:
Use the parent_path to check if the location that the user wants to use is below the original warehouse.

opw-2588082

closes odoo/odoo#75501

X-original-commit: bed546486aa667d735df015de8153d42a9af9a41
Signed-off-by: William Henrotin <Whenrow@users.noreply.github.com>
Signed-off-by: Djamel Touati <DjamelTouati@users.noreply.github.com>
Co-authored-by: fmdl <florent.mirieu@gmail.com>
This commit is contained in:
Touati Djamel (otd)
2021-08-25 08:03:03 +00:00
co-authored by fmdl
parent aa308e7fe7
commit c4ccee31c7
3 changed files with 25 additions and 1 deletions
@@ -228,6 +228,15 @@ msgstr ""
msgid "Exception(s):"
msgstr ""
#. module: purchase_stock
#: code:addons/purchase_stock/models/purchase.py:0
#, python-format
msgid ""
"For the product %s, the warehouse of the operation type (%s) is inconsistent"
" with the location (%s) of the reordering rule (%s). Change the operation "
"type or cancel the request for quotation."
msgstr ""
#. module: purchase_stock
#: model:ir.model.fields,field_description:purchase_stock.field_purchase_order_line__forecasted_issue
msgid "Forecasted Issue"
+8 -1
View File
@@ -274,7 +274,7 @@ class PurchaseOrderLine(models.Model):
qty_received_method = fields.Selection(selection_add=[('stock_moves', 'Stock Moves')])
move_ids = fields.One2many('stock.move', 'purchase_line_id', string='Reservation', readonly=True, copy=False)
orderpoint_id = fields.Many2one('stock.warehouse.orderpoint', 'Orderpoint')
orderpoint_id = fields.Many2one('stock.warehouse.orderpoint', 'Orderpoint', copy=False)
move_dest_ids = fields.One2many('stock.move', 'created_purchase_line_id', 'Downstream Moves')
product_description_variants = fields.Char('Custom Description')
propagate_cancel = fields.Boolean('Propagate cancellation', default=True)
@@ -468,8 +468,15 @@ class PurchaseOrderLine(models.Model):
res.append(extra_move_vals)
return res
def _check_orderpoint_picking_type(self):
warehouse_loc = self.order_id.picking_type_id.warehouse_id.view_location_id
if self.orderpoint_id and not warehouse_loc.parent_path in self.orderpoint_id.location_id.parent_path:
raise UserError(_('For the product %s, the warehouse of the operation type (%s) is inconsistent with the location (%s) of the reordering rule (%s). Change the operation type or cancel the request for quotation.',
self.product_id.display_name, self.order_id.picking_type_id.display_name, self.orderpoint_id.location_id.display_name, self.orderpoint_id.display_name))
def _prepare_stock_move_vals(self, picking, price_unit, product_uom_qty, product_uom):
self.ensure_one()
self._check_orderpoint_picking_type()
product = self.product_id.with_context(lang=self.order_id.dest_address_id.lang or self.env.user.lang)
date_planned = self.date_planned or self.order_id.date_planned
return {
@@ -7,6 +7,7 @@ from datetime import timedelta as td
from odoo import SUPERUSER_ID
from odoo.tests import Form
from odoo.tests.common import TransactionCase
from odoo.exceptions import UserError
class TestReorderingRule(TransactionCase):
@@ -38,6 +39,7 @@ class TestReorderingRule(TransactionCase):
"""
warehouse_1 = self.env['stock.warehouse'].search([('company_id', '=', self.env.user.id)], limit=1)
warehouse_1.write({'reception_steps': 'two_steps'})
warehouse_2 = self.env['stock.warehouse'].create({'name': 'WH 2', 'code': 'WH2', 'company_id': self.env.company.id, 'partner_id': self.env.company.partner_id.id, 'reception_steps': 'one_step'})
# create reordering rule
orderpoint_form = Form(self.env['stock.warehouse.orderpoint'])
@@ -64,6 +66,12 @@ class TestReorderingRule(TransactionCase):
purchase_order = self.env['purchase.order'].search([('partner_id', '=', self.partner.id)])
self.assertTrue(purchase_order, 'No purchase order created.')
# Check the picking type on the purchase order
purchase_order.picking_type_id = warehouse_2.in_type_id
with self.assertRaises(UserError):
purchase_order.button_confirm()
purchase_order.picking_type_id = warehouse_1.in_type_id
# On the po generated, the source document should be the name of the reordering rule
self.assertEqual(order_point.name, purchase_order.origin, 'Source document on purchase order should be the name of the reordering rule.')
self.assertEqual(purchase_order.order_line.product_qty, 10)