Merge PR #339 into 15.0

Signed-off-by simahawk
This commit is contained in:
OCA-git-bot
2023-07-03 08:24:16 +00:00
2 changed files with 75 additions and 36 deletions

View File

@@ -2,12 +2,11 @@
# Copyright (C) 2021 Serpent Consulting Services # Copyright (C) 2021 Serpent Consulting Services
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta from dateutil.relativedelta import relativedelta
from odoo import api, fields, models from odoo import api, fields, models
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT
DELTA_TYPES = ("day", "week", "month", "year")
class StockProductionLot(models.Model): class StockProductionLot(models.Model):
@@ -15,34 +14,18 @@ class StockProductionLot(models.Model):
warranty_exp_date = fields.Date(string="Warranty Expiration Date") 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") @api.onchange("product_id")
def _onchange_product_id(self): def _onchange_product_id(self):
self.warranty_exp_date = False self.warranty_exp_date = self._get_warranty_exp_date()
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

View File

@@ -3,24 +3,80 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from datetime import datetime, timedelta from datetime import datetime, timedelta
import psycopg2
from odoo.tests import common from odoo.tests import common
class TestProductLotWarranty(common.TransactionCase): class TestProductLotWarranty(common.TransactionCase):
def test_productlot_warranty(self): @classmethod
company1 = self.env["res.company"].create({"name": "Test company1"}) def setUpClass(cls):
product1 = self.env["product.product"].create( super().setUpClass()
cls.company1 = cls.env["res.company"].create({"name": "Test company1"})
cls.product1 = cls.env["product.product"].create(
{ {
"name": "TestProduct", "name": "TestProduct",
"warranty_type": "day", "warranty_type": "day",
"warranty": 5, "warranty": 5,
} }
) )
def test_productlot_warranty(self):
production_lot = self.env["stock.production.lot"].create( 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() production_lot._onchange_product_id()
self.assertEqual( self.assertEqual(
production_lot.warranty_exp_date, production_lot.warranty_exp_date,
(datetime.now() + timedelta(days=5)).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(),
)