mirror of
https://github.com/OCA/rma.git
synced 2025-02-16 17:11:47 +02:00
@@ -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()
|
||||
|
||||
@@ -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(),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user