diff --git a/stock_demand_estimate/__manifest__.py b/stock_demand_estimate/__manifest__.py index c7dd47e01..bff07b742 100644 --- a/stock_demand_estimate/__manifest__.py +++ b/stock_demand_estimate/__manifest__.py @@ -7,9 +7,7 @@ "author": "ForgeFlow, Odoo Community Association (OCA)", "website": "https://github.com/OCA/stock-logistics-warehouse", "category": "Warehouse Management", - "depends": [ - "stock", - ], + "depends": ["stock"], "data": [ "security/ir.model.access.csv", "security/stock_security.xml", diff --git a/stock_demand_estimate/models/stock_demand_estimate.py b/stock_demand_estimate/models/stock_demand_estimate.py index 7b0aa3abb..679d18287 100644 --- a/stock_demand_estimate/models/stock_demand_estimate.py +++ b/stock_demand_estimate/models/stock_demand_estimate.py @@ -2,83 +2,61 @@ # Copyright 2016 Aleph Objects, Inc. (https://www.alephobjects.com/) # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). -from odoo import api, fields, models, _ -from odoo.addons import decimal_precision as dp +from datetime import date, timedelta + +from odoo import _, api, fields, models from odoo.exceptions import UserError -from datetime import timedelta, date +from odoo.addons import decimal_precision as dp class StockDemandEstimate(models.Model): - _name = 'stock.demand.estimate' - _description = 'Stock Demand Estimate Line' + _name = "stock.demand.estimate" + _description = "Stock Demand Estimate Line" date_from = fields.Date( - compute="_compute_dates", - string="From (computed)", - store=True - ) - date_to = fields.Date( - compute="_compute_dates", - string="To (computed)", - store=True, + compute="_compute_dates", string="From (computed)", store=True ) + date_to = fields.Date(compute="_compute_dates", string="To (computed)", store=True) manual_date_from = fields.Date(string="From") manual_date_to = fields.Date(string="To") manual_duration = fields.Integer( - string="Duration", - help="Duration (in days)", - default=1, + string="Duration", help="Duration (in days)", default=1 ) duration = fields.Integer( - compute="_compute_dates", - string="Duration (computed))", - store=True, + compute="_compute_dates", string="Duration (computed))", store=True ) product_id = fields.Many2one( - comodel_name="product.product", - string="Product", - required=True, - ) - product_uom = fields.Many2one( - comodel_name="uom.uom", - string="Unit of measure", + comodel_name="product.product", string="Product", required=True ) + product_uom = fields.Many2one(comodel_name="uom.uom", string="Unit of measure") location_id = fields.Many2one( - comodel_name="stock.location", - string="Location", - required=True, + comodel_name="stock.location", string="Location", required=True ) product_uom_qty = fields.Float( - string="Quantity", - digits=dp.get_precision('Product Unit of Measure'), + string="Quantity", digits=dp.get_precision("Product Unit of Measure") ) product_qty = fields.Float( - 'Real Quantity', - compute='_compute_product_quantity', - inverse='_inverse_product_quantity', + "Real Quantity", + compute="_compute_product_quantity", + inverse="_inverse_product_quantity", digits=0, store=True, - help='Quantity in the default UoM of the product', + help="Quantity in the default UoM of the product", readonly=True, ) - daily_qty = fields.Float( - string='Quantity / Day', - compute='_compute_daily_qty', - ) + daily_qty = fields.Float(string="Quantity / Day", compute="_compute_daily_qty") company_id = fields.Many2one( - comodel_name='res.company', - string='Company', + comodel_name="res.company", + string="Company", required=True, - default=lambda self: self.env['res.company']._company_default_get( - 'stock.demand.estimate' - ) + default=lambda self: self.env["res.company"]._company_default_get( + "stock.demand.estimate" + ), ) @api.multi - @api.depends( - "manual_duration", "manual_date_from", "manual_date_to", - ) + @api.depends("manual_duration", "manual_date_from", "manual_date_to") def _compute_dates(self): today = date.today() for rec in self: @@ -87,8 +65,7 @@ class StockDemandEstimate(models.Model): rec.date_to = rec.manual_date_to rec.duration = (rec.manual_date_to - rec.date_from).days + 1 elif rec.manual_duration: - rec.date_to = rec.date_from + timedelta( - days=rec.manual_duration - 1) + rec.date_to = rec.date_from + timedelta(days=rec.manual_duration - 1) rec.duration = rec.manual_duration else: rec.date_to = rec.date_from + timedelta(days=1) @@ -104,7 +81,7 @@ class StockDemandEstimate(models.Model): rec.daily_qty = 0.0 @api.multi - @api.depends('product_id', 'product_uom', 'product_uom_qty') + @api.depends("product_id", "product_uom", "product_uom_qty") def _compute_product_quantity(self): for rec in self: if rec.product_uom: @@ -115,20 +92,24 @@ class StockDemandEstimate(models.Model): rec.product_qty = rec.product_uom_qty def _inverse_product_quantity(self): - raise UserError(_( - 'The requested operation cannot be ' - 'processed because of a programming error ' - 'setting the `product_qty` field instead ' - 'of the `product_uom_qty`.' - )) + raise UserError( + _( + "The requested operation cannot be " + "processed because of a programming error " + "setting the `product_qty` field instead " + "of the `product_uom_qty`." + ) + ) @api.multi def name_get(self): res = [] for rec in self: - name = "%s - %s: %s - %s" % ( - rec.date_from, rec.date_to, - rec.product_id.name, rec.location_id.name, + name = "{} - {}: {} - {}".format( + rec.date_from, + rec.date_to, + rec.product_id.name, + rec.location_id.name, ) res.append((rec.id, name)) return res @@ -138,14 +119,16 @@ class StockDemandEstimate(models.Model): for rec in self: if rec.manual_date_from: rec.manual_duration = ( - rec.manual_date_to - rec.manual_date_from).days + 1 + rec.manual_date_to - rec.manual_date_from + ).days + 1 @api.onchange("manual_duration") def _onchange_manual_duration(self): for rec in self: if rec.manual_date_from: rec.manual_date_to = rec.manual_date_from + timedelta( - days=rec.manual_duration - 1) + days=rec.manual_duration - 1 + ) @api.model def get_quantity_by_date_range(self, date_start, date_end): diff --git a/stock_demand_estimate/tests/test_stock_demand_estimate.py b/stock_demand_estimate/tests/test_stock_demand_estimate.py index 9b14034e0..794073b44 100644 --- a/stock_demand_estimate/tests/test_stock_demand_estimate.py +++ b/stock_demand_estimate/tests/test_stock_demand_estimate.py @@ -1,76 +1,73 @@ # Copyright 2017-19 ForgeFlow S.L. (https://www.forgeflow.com) # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). -from odoo.tests.common import SavepointCase - from datetime import date, timedelta as td +from odoo.tests.common import SavepointCase + class TestStockDemandEstimate(SavepointCase): @classmethod def setUpClass(cls): super().setUpClass() - cls.res_users_model = cls.env['res.users'] - cls.product_model = cls.env['product.product'] - cls.stock_location_model = cls.env['stock.location'] + cls.res_users_model = cls.env["res.users"] + cls.product_model = cls.env["product.product"] + cls.stock_location_model = cls.env["stock.location"] cls.estimate_model = cls.env["stock.demand.estimate"] - cls.g_stock_manager = cls.env.ref('stock.group_stock_manager') - cls.g_stock_user = cls.env.ref('stock.group_stock_user') - cls.company = cls.env.ref('base.main_company') - cls.uom_unit = cls.env.ref('uom.product_uom_unit') - cls.uom_dozen = cls.env.ref('uom.product_uom_dozen') + cls.g_stock_manager = cls.env.ref("stock.group_stock_manager") + cls.g_stock_user = cls.env.ref("stock.group_stock_user") + cls.company = cls.env.ref("base.main_company") + cls.uom_unit = cls.env.ref("uom.product_uom_unit") + cls.uom_dozen = cls.env.ref("uom.product_uom_dozen") # Create users: - cls.manager = cls._create_user( - 'user_1', - [cls.g_stock_manager], - cls.company, - ).id - cls.user = cls._create_user( - 'user_2', - [cls.g_stock_user], - cls.company, - ).id + cls.manager = cls._create_user("user_1", [cls.g_stock_manager], cls.company).id + cls.user = cls._create_user("user_2", [cls.g_stock_user], cls.company).id # Create a product: - cls.product_1 = cls.product_model.create({ - 'name': 'Test Product 1', - 'type': 'product', - 'default_code': 'PROD1', - "uom_id": cls.uom_unit.id, - }) + cls.product_1 = cls.product_model.create( + { + "name": "Test Product 1", + "type": "product", + "default_code": "PROD1", + "uom_id": cls.uom_unit.id, + } + ) # Create a location: - cls.location = cls.stock_location_model.create({ - 'name': 'Place', - 'usage': 'production', - }) + cls.location = cls.stock_location_model.create( + {"name": "Place", "usage": "production"} + ) @classmethod def _create_user(cls, login, groups, company): group_ids = [group.id for group in groups] - user = cls.res_users_model.create({ - 'name': login, - 'login': login, - 'password': 'demo', - 'email': 'example@yourcompany.com', - 'company_id': company.id, - 'company_ids': [(4, company.id)], - 'groups_id': [(6, 0, group_ids)], - }) + user = cls.res_users_model.create( + { + "name": login, + "login": login, + "password": "demo", + "email": "example@yourcompany.com", + "company_id": company.id, + "company_ids": [(4, company.id)], + "groups_id": [(6, 0, group_ids)], + } + ) return user def test_01_create_estimate(self): """Crete an estimate entering manually the date from and date to.""" date_from = date.today() + td(days=10) date_to = date.today() + td(days=19) - estimate = self.estimate_model.create({ - "product_id": self.product_1.id, - "location_id": self.location.id, - "manual_date_from": date_from, - "manual_date_to": date_to, - "product_uom_qty": 500.0, - }) + estimate = self.estimate_model.create( + { + "product_id": self.product_1.id, + "location_id": self.location.id, + "manual_date_from": date_from, + "manual_date_to": date_to, + "product_uom_qty": 500.0, + } + ) self.assertEqual(estimate.date_from, date_from) self.assertEqual(estimate.date_to, date_to) self.assertEqual(estimate.duration, 10) @@ -81,14 +78,16 @@ class TestStockDemandEstimate(SavepointCase): """Create an estimate entering manually the date from, duration and using a different UoM than product's.""" date_from = date.today() + td(days=10) - estimate = self.estimate_model.create({ - "product_id": self.product_1.id, - "location_id": self.location.id, - "manual_date_from": date_from, - "manual_duration": 15, - "product_uom_qty": 100.0, - "product_uom": self.uom_dozen.id, - }) + estimate = self.estimate_model.create( + { + "product_id": self.product_1.id, + "location_id": self.location.id, + "manual_date_from": date_from, + "manual_duration": 15, + "product_uom_qty": 100.0, + "product_uom": self.uom_dozen.id, + } + ) self.assertEqual(estimate.date_from, date_from) expected_date_to = estimate.date_from + td(days=15 - 1) self.assertEqual(estimate.date_to, expected_date_to) @@ -100,26 +99,28 @@ class TestStockDemandEstimate(SavepointCase): def test_03_get_qty_by_range(self): date_from = date.today() + td(days=10) date_to = date.today() + td(days=19) - estimate = self.estimate_model.create({ - "product_id": self.product_1.id, - "location_id": self.location.id, - "manual_date_from": date_from, - "manual_date_to": date_to, - "product_uom_qty": 100.0, - }) + estimate = self.estimate_model.create( + { + "product_id": self.product_1.id, + "location_id": self.location.id, + "manual_date_from": date_from, + "manual_date_to": date_to, + "product_uom_qty": 100.0, + } + ) self.assertEqual(estimate.duration, 10.0) self.assertEqual(estimate.daily_qty, 10.0) res = estimate.get_quantity_by_date_range( - date_from + td(days=3), date_from + td(days=17)) + date_from + td(days=3), date_from + td(days=17) + ) self.assertEqual(res, 70) res = estimate.get_quantity_by_date_range( - date_from + td(days=3), date_from + td(days=7)) + date_from + td(days=3), date_from + td(days=7) + ) self.assertEqual(res, 50) # get full period - res = estimate.get_quantity_by_date_range( - date.today(), date_from + td(days=17)) + res = estimate.get_quantity_by_date_range(date.today(), date_from + td(days=17)) self.assertEqual(res, 100) # Get exact period: - res = estimate.get_quantity_by_date_range( - estimate.date_from, estimate.date_to) + res = estimate.get_quantity_by_date_range(estimate.date_from, estimate.date_to) self.assertEqual(res, 100)