From 0cd53bf745ccc20e9c0423334d3010c043a71aa7 Mon Sep 17 00:00:00 2001 From: Florent Xicluna Date: Wed, 22 Feb 2023 16:34:41 +0100 Subject: [PATCH] [IMP] stock_production_lot_warranty: re-usable _get_warranty_exp_date --- .../models/stock_production_lot.py | 47 +++++--------- .../tests/test_product_lot_warranty.py | 64 +++++++++++++++++-- 2 files changed, 75 insertions(+), 36 deletions(-) diff --git a/stock_production_lot_warranty/models/stock_production_lot.py b/stock_production_lot_warranty/models/stock_production_lot.py index 2d2c4644..1e1262df 100644 --- a/stock_production_lot_warranty/models/stock_production_lot.py +++ b/stock_production_lot_warranty/models/stock_production_lot.py @@ -2,12 +2,11 @@ # Copyright (C) 2021 Serpent Consulting Services # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from datetime import datetime, timedelta - from dateutil.relativedelta import relativedelta from odoo import api, fields, models -from odoo.tools import DEFAULT_SERVER_DATE_FORMAT + +DELTA_TYPES = ("day", "week", "month", "year") class StockProductionLot(models.Model): @@ -15,34 +14,18 @@ class StockProductionLot(models.Model): warranty_exp_date = fields.Date(string="Warranty Expiration Date") + def _get_warranty_exp_date(self, start_date=None): + if not start_date: + start_date = fields.Date.context_today(self) + elif hasattr(start_date, "astimezone"): + # Datetime object, convert to date + start_date = fields.Date.context_today(self, timestamp=start_date) + delta_type = self.product_id.product_tmpl_id.warranty_type + duration = self.product_id.product_tmpl_id.warranty + if not duration or delta_type not in DELTA_TYPES: + return False + return start_date + relativedelta(**{f"{delta_type}s": duration}) + @api.onchange("product_id") def _onchange_product_id(self): - self.warranty_exp_date = False - if ( - self.product_id - and self.product_id.product_tmpl_id.warranty_type - and self.product_id.product_tmpl_id.warranty - ): - warranty_type = self.product_id.product_tmpl_id.warranty_type - time = False - if warranty_type == "day": - time = ( - datetime.now() - + timedelta(days=self.product_id.product_tmpl_id.warranty) - ).strftime(DEFAULT_SERVER_DATE_FORMAT) - elif warranty_type == "week": - time = ( - datetime.now() - + timedelta(weeks=self.product_id.product_tmpl_id.warranty) - ).strftime(DEFAULT_SERVER_DATE_FORMAT) - elif warranty_type == "month": - time = ( - datetime.now() - + relativedelta(months=+self.product_id.product_tmpl_id.warranty) - ).strftime(DEFAULT_SERVER_DATE_FORMAT) - elif warranty_type == "year": - time = ( - datetime.now() - + relativedelta(years=+self.product_id.product_tmpl_id.warranty) - ).strftime(DEFAULT_SERVER_DATE_FORMAT) - self.warranty_exp_date = time + self.warranty_exp_date = self._get_warranty_exp_date() diff --git a/stock_production_lot_warranty/tests/test_product_lot_warranty.py b/stock_production_lot_warranty/tests/test_product_lot_warranty.py index fa665cf3..620c0b1d 100644 --- a/stock_production_lot_warranty/tests/test_product_lot_warranty.py +++ b/stock_production_lot_warranty/tests/test_product_lot_warranty.py @@ -3,24 +3,80 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from datetime import datetime, timedelta +import psycopg2 + from odoo.tests import common class TestProductLotWarranty(common.TransactionCase): - def test_productlot_warranty(self): - company1 = self.env["res.company"].create({"name": "Test company1"}) - product1 = self.env["product.product"].create( + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.company1 = cls.env["res.company"].create({"name": "Test company1"}) + cls.product1 = cls.env["product.product"].create( { "name": "TestProduct", "warranty_type": "day", "warranty": 5, } ) + + def test_productlot_warranty(self): production_lot = self.env["stock.production.lot"].create( - {"product_id": product1.id, "company_id": company1.id} + {"product_id": self.product1.id, "company_id": self.company1.id} ) production_lot._onchange_product_id() self.assertEqual( production_lot.warranty_exp_date, (datetime.now() + timedelta(days=5)).date(), ) + + def test_productlot_no_product(self): + # s.p.lot "product_id" is required=True + with self.assertRaises(psycopg2.IntegrityError): + self.env["stock.production.lot"].create( + {"product_id": False, "company_id": self.company1.id} + ) + + def test_productlot_no_warranty_type(self): + # product.template "warranty_type" is required=True + with self.assertRaises(psycopg2.IntegrityError): + self.env["product.product"].create( + { + "name": "TestProduct", + "warranty_type": False, + "warranty": 5, + } + ) + + def test_productlot_no_warranty(self): + product2 = self.env["product.product"].create( + { + "name": "TestProduct", + "warranty_type": "week", + "warranty": 0, + } + ) + production_lot = self.env["stock.production.lot"].create( + {"product_id": product2.id, "company_id": self.company1.id} + ) + production_lot._onchange_product_id() + self.assertFalse(production_lot.warranty_exp_date) + + def test_get_warranty_exp_date(self): + production_lot = self.env["stock.production.lot"].create( + {"product_id": self.product1.id, "company_id": self.company1.id} + ) + timestamp = datetime.now() - timedelta(days=3) + self.assertEqual( + production_lot._get_warranty_exp_date(), + (datetime.now() + timedelta(days=5)).date(), + ) + self.assertEqual( + production_lot._get_warranty_exp_date(timestamp), + (datetime.now() + timedelta(days=2)).date(), + ) + self.assertEqual( + production_lot._get_warranty_exp_date(timestamp.date()), + (datetime.now() + timedelta(days=2)).date(), + )