Skip to content

Commit

Permalink
[MIG] stock_average_daily_sale: Backporting to 14.0 from 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
twalter-c2c committed Sep 25, 2024
1 parent 9fbb33f commit 4543eba
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 53 deletions.
1 change: 1 addition & 0 deletions stock_average_daily_sale/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"depends": [
"sale",
"stock_storage_type_putaway_abc",
"stock_location_warehouse", # not needed in Odoo 16.0+
"product_route_mto",
],
"data": [
Expand Down
20 changes: 0 additions & 20 deletions stock_average_daily_sale/migrations/16.0.1.1.0/pre-migrate.py

This file was deleted.

13 changes: 4 additions & 9 deletions stock_average_daily_sale/models/stock_average_daily_sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import logging
from contextlib import closing

from psycopg2.errors import ObjectNotInPrerequisiteState
from psycopg2.extensions import AsIs

from odoo import _, api, fields, models, registry
Expand Down Expand Up @@ -113,23 +112,19 @@ def _check_materialize_view_populated(cls, cr):
def _check_view(self):
cr = registry(self._cr.dbname).cursor()
with closing(cr):
try:
return self._check_materialize_view_populated(cr)
except ObjectNotInPrerequisiteState:
if not self._check_materialize_view_populated(cr):
_logger.warning(
_("The materialized view has not been populated. Launch the cron.")
)
return False
except Exception as e:
raise e
return self._check_materialize_view_populated(cr)

# pylint: disable=redefined-outer-name
@api.model
def search(self, domain, offset=0, limit=None, order=None, count=False):
def search(self, args, offset=0, limit=None, order=None, count=False):
if not config["test_enable"] and not self._check_view():
return self.browse()
return super().search(
domain=domain, offset=offset, limit=limit, order=order, count=count
args=args, offset=offset, limit=limit, order=order, count=count
)

@api.model
Expand Down
9 changes: 4 additions & 5 deletions stock_average_daily_sale/models/stock_warehouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class StockWarehouse(models.Model):
compute="_compute_average_daily_sale_root_location_id",
store=True,
readonly=False,
precompute=True,
check_company=True,
help="This is the root location for daily sale average stock computations",
)
Expand All @@ -29,11 +28,11 @@ def _compute_average_daily_sale_root_location_id(self):
continue
warehouse.average_daily_sale_root_location_id = warehouse.lot_stock_id

@api.model_create_multi
def create(self, vals_list):
@api.model
def create(self, vals):
# set the lot_stock_id of a newly created WH as an Average Daily Sale Root Location
warehouses = super().create(vals_list)
for warehouse, vals in zip(warehouses, vals_list):
warehouses = super().create(vals)
for warehouse in warehouses:
if vals.get("lot_stock_id") and not vals.get(
"average_daily_sale_root_location_id"
):
Expand Down
43 changes: 29 additions & 14 deletions stock_average_daily_sale/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CommonAverageSaleTest:
def setUpClass(cls):
super().setUpClass()
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
cls.inventory_obj = cls.env["stock.quant"].with_context(inventory_mode=True)
cls.inventory_obj = cls.env["stock.inventory"]
cls.customers = cls.env.ref("stock.stock_location_customers")
cls.location_obj = cls.env["stock.location"]
cls.move_obj = cls.env["stock.move"]
Expand Down Expand Up @@ -50,20 +50,34 @@ def setUpClass(cls):

@classmethod
def _create_inventory(cls):
cls.inventory_obj.create(
inventory = cls.env["stock.inventory"].create(
{
"product_id": cls.product_1.id,
"inventory_quantity": 50.0,
"location_id": cls.location_bin.id,
"line_ids": [
(
0,
0,
{
"product_id": cls.product_1.id,
"product_uom_id": cls.product_1.uom_id.id,
"product_qty": 50,
"location_id": cls.location_bin.id,
},
),
(
0,
0,
{
"product_id": cls.product_2.id,
"product_uom_id": cls.product_2.uom_id.id,
"product_qty": 60,
"location_id": cls.location_bin_2.id,
},
),
]
}
)._apply_inventory()
cls.inventory_obj.create(
{
"product_id": cls.product_2.id,
"inventory_quantity": 60.0,
"location_id": cls.location_bin_2.id,
}
)._apply_inventory()
)
inventory.action_start()
inventory.action_validate()

@classmethod
def _create_products(cls):
Expand All @@ -90,6 +104,7 @@ def _create_move(cls, product, origin_location, qty):
"warehouse_id": origin_location.warehouse_id.id,
"location_dest_id": cls.customers.id,
"product_uom_qty": qty,
"product_uom": product.uom_id.id,
"priority": "1",
}
)
Expand All @@ -100,5 +115,5 @@ def _create_move(cls, product, origin_location, qty):
@classmethod
def _refresh(cls):
# Flush to allow materialized view to be correctly populated
cls.env.flush_all()
cls.env["stock.average.daily.sale"].flush()
cls.env["stock.average.daily.sale"].refresh_view()
20 changes: 16 additions & 4 deletions stock_average_daily_sale/tests/test_average_daily_sale.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# Copyright 2022 ACSONE SA/NV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import logging

from dateutil.relativedelta import relativedelta
from freezegun import freeze_time

from odoo.fields import Date
from odoo.tests.common import TransactionCase
from odoo.tests.common import SavepointCase

from .common import CommonAverageSaleTest


class TestAverageSale(CommonAverageSaleTest, TransactionCase):
class TestAverageSale(CommonAverageSaleTest, SavepointCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
Expand Down Expand Up @@ -188,10 +190,20 @@ def test_average_sale_profile_a(self):

def test_view_refreshed(self):
self._refresh()
with self.assertNoLogs(
with self.assertLogs(
"odoo.addons.stock_average_daily_sale.models.stock_average_daily_sale",
level="DEBUG",
):
) as cm:
logging.getLogger(
"odoo.addons.stock_average_daily_sale.models.stock_average_daily_sale"
).info("Dummy warning")
self.env["stock.average.daily.sale"].search_read(
[("product_id", "=", self.product_1.id)]
)
# flake8: noqa: B950
self.assertEqual(
[
"INFO:odoo.addons.stock_average_daily_sale.models.stock_average_daily_sale:Dummy warning"
],
cm.output,
)
1 change: 0 additions & 1 deletion stock_average_daily_sale/views/stock_warehouse.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<field name="arch" type="xml">
<xpath expr="//group[1]" position="after">
<group string="Average Sales" name="average_sale_reporting">
<field name="lot_stock_id" invisible="1" />
<field
name="average_daily_sale_root_location_id"
attrs="{'required': [('lot_stock_id', '!=', False)]}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def _create_move(self, product, origin_location, qty):
"warehouse_id": suppliers.warehouse_id.id,
"location_dest_id": customers.id,
"product_uom_qty": qty,
"product_uom": product.uom_id.id,
}
)
move._action_confirm()
Expand All @@ -45,6 +46,7 @@ def _create_move(self, product, origin_location, qty):
"warehouse_id": origin_location.warehouse_id.id,
"location_dest_id": customers.id,
"product_uom_qty": qty,
"product_uom": product.uom_id.id,
"priority": "1",
}
)
Expand Down

0 comments on commit 4543eba

Please sign in to comment.