[ADD] pms: wizard folio reservations (#33)

* [IMP] pms: wizard folio reservations

* [IMP] pms: wizard folio reservation (creates folio & reservations)

* [IMP] pms: wizard folio reservation (discount)

* [FIX] pms: remove review comments resolved
This commit is contained in:
Miguel Padin
2021-01-07 12:42:30 +01:00
committed by GitHub
parent 8b429b4da5
commit a1f5c0bce7
8 changed files with 1184 additions and 0 deletions

View File

@@ -28,3 +28,4 @@ from . import test_pms_folio
from . import test_pms_room_type_availability_rules
from . import test_pms_room_type
from . import test_pms_wizard_massive_changes
from . import test_pms_wizard_folio

View File

@@ -0,0 +1,662 @@
# import datetime
# from freezegun import freeze_time
#
import datetime
from freezegun import freeze_time
from odoo import fields
from .common import TestHotel
@freeze_time("1980-12-01")
class TestPmsWizardMassiveChanges(TestHotel):
def create_common_scenario(self):
# PRICELIST CREATION
self.test_pricelist = self.env["product.pricelist"].create(
{
"name": "test pricelist 1",
}
)
self.test_pricelist.flush()
# AVAILABILITY PLAN CREATION
self.test_availability_plan = self.env[
"pms.room.type.availability.plan"
].create(
{
"name": "Availability plan for TEST",
"pms_pricelist_ids": [(6, 0, [self.test_pricelist.id])],
}
)
self.test_availability_plan.flush()
# PROPERTY CREATION (WITH DEFAULT PRICELIST AND AVAILABILITY PLAN
self.test_property = self.env["pms.property"].create(
{
"name": "MY PMS TEST",
"company_id": self.env.ref("base.main_company").id,
"default_pricelist_id": self.test_pricelist.id,
"default_availability_plan_id": self.test_availability_plan.id,
}
)
self.test_property.flush()
# CREATION OF ROOM TYPE CLASS
self.test_room_type_class = self.env["pms.room.type.class"].create(
{"name": "Room"}
)
self.test_room_type_class.flush()
# CREATION OF ROOM TYPE (WITH ROOM TYPE CLASS)
self.test_room_type_single = self.env["pms.room.type"].create(
{
"pms_property_ids": [self.test_property.id],
"name": "Single Test",
"code_type": "SNG_Test",
"class_id": self.test_room_type_class.id,
"list_price": 25.0,
}
)
self.test_room_type_single.flush()
# CREATION OF ROOM TYPE (WITH ROOM TYPE CLASS)
self.test_room_type_double = self.env["pms.room.type"].create(
{
"pms_property_ids": [self.test_property.id],
"name": "Double Test",
"code_type": "DBL_Test",
"class_id": self.test_room_type_class.id,
"list_price": 40.0,
}
)
self.test_room_type_double.flush()
# pms.room
self.test_room1_double = self.env["pms.room"].create(
{
"pms_property_id": self.test_property.id,
"name": "Double 201 test",
"room_type_id": self.test_room_type_double.id,
"capacity": 2,
}
)
self.test_room1_double.flush()
# pms.room
self.test_room2_double = self.env["pms.room"].create(
{
"pms_property_id": self.test_property.id,
"name": "Double 202 test",
"room_type_id": self.test_room_type_double.id,
"capacity": 2,
}
)
self.test_room2_double.flush()
# pms.room
self.test_room3_double = self.env["pms.room"].create(
{
"pms_property_id": self.test_property.id,
"name": "Double 203 test",
"room_type_id": self.test_room_type_double.id,
"capacity": 2,
}
)
self.test_room3_double.flush()
# pms.room
self.test_room4_double = self.env["pms.room"].create(
{
"pms_property_id": self.test_property.id,
"name": "Double 204 test",
"room_type_id": self.test_room_type_double.id,
"capacity": 2,
}
)
self.test_room4_double.flush()
# pms.room
self.test_room1_single = self.env["pms.room"].create(
{
"pms_property_id": self.test_property.id,
"name": "Single 101 test",
"room_type_id": self.test_room_type_single.id,
"capacity": 1,
}
)
self.test_room1_single.flush()
# pms.room
self.test_room2_single = self.env["pms.room"].create(
{
"pms_property_id": self.test_property.id,
"name": "Single 102 test",
"room_type_id": self.test_room_type_single.id,
"capacity": 1,
}
)
self.test_room2_single.flush()
# res.partner
self.partner_id = self.env["res.partner"].create(
{
"name": "Miguel",
"phone": "654667733",
"email": "miguel@example.com",
}
)
self.partner_id.flush()
def test_price_wizard_correct(self):
# TEST CASE
# Set values for the wizard and the total price is correct
# Also check the discount is correctly applied to get
# the total folio price
# (no pricelist applied)
# ARRANGE
# common scenario
self.create_common_scenario()
# checkin & checkout
checkin = fields.date.today()
checkout = fields.date.today() + datetime.timedelta(days=1)
days = (checkout - checkin).days
num_double_rooms = 4
discounts = [
{
"discount": 0,
"expected_price": days
* self.test_room_type_double.list_price
* num_double_rooms,
},
{
"discount": 0.5,
"expected_price": (
days * self.test_room_type_double.list_price * num_double_rooms
)
* 0.5,
},
]
# create folio wizard with partner id => pricelist & start-end dates
wizard_folio = self.env["pms.folio.wizard"].create(
{
"start_date": checkin,
"end_date": checkout,
"partner_id": self.partner_id.id,
}
)
# force pricelist load
wizard_folio.flush()
wizard_folio.availability_results._compute_dynamic_selection()
# availability items belonging to test property
lines_availability_test = self.env["pms.folio.availability.wizard"].search(
[
("room_type_id.pms_property_ids", "in", self.test_property.id),
]
)
# set value for room type double
value = self.env["pms.num.rooms.selection"].search(
[
("room_type_id", "=", str(self.test_room_type_double.id)),
("value", "=", num_double_rooms),
]
)
lines_availability_test[0].num_rooms_selected = value
for discount in discounts:
with self.subTest(k=discount):
# ACT
wizard_folio.discount = discount["discount"]
# ASSERT
self.assertEqual(
wizard_folio.total_price_folio,
discount["expected_price"],
"The total price calculation is wrong",
)
def test_price_wizard_correct_pricelist_applied(self):
# TEST CASE
# Set values for the wizard and the total price is correct
# (pricelist applied)
# ARRANGE
# common scenario
self.create_common_scenario()
# checkin & checkout
checkin = fields.date.today()
checkout = fields.date.today() + datetime.timedelta(days=1)
days = (checkout - checkin).days
# num. rooms of type double to book
num_double_rooms = 4
# price for today
price_today = 38.0
# expected price
expected_price_total = days * price_today * num_double_rooms
# convert dates to datetimes
dates = self.env["pms.folio.wizard"].get_datetime_from_start_end(checkin)
# set pricelist item for current day
product_tmpl_id = self.test_room_type_double.product_id.product_tmpl_id.id
pricelist_item = self.env["product.pricelist.item"].create(
{
"pricelist_id": self.test_pricelist.id,
"date_start": dates[0],
"date_end": dates[1],
"compute_price": "fixed",
"applied_on": "1_product",
"product_tmpl_id": product_tmpl_id,
"fixed_price": price_today,
"min_quantity": 0,
}
)
pricelist_item.flush()
# create folio wizard with partner id => pricelist & start-end dates
wizard_folio = self.env["pms.folio.wizard"].create(
{
"start_date": checkin,
"end_date": checkout,
"partner_id": self.partner_id.id,
"pricelist_id": self.test_pricelist.id,
}
)
wizard_folio.flush()
wizard_folio.availability_results._compute_dynamic_selection()
# availability items belonging to test property
lines_availability_test = self.env["pms.folio.availability.wizard"].search(
[
("room_type_id.pms_property_ids", "in", self.test_property.id),
]
)
# set value for room type double
value = self.env["pms.num.rooms.selection"].search(
[
("room_type_id", "=", str(self.test_room_type_double.id)),
("value", "=", num_double_rooms),
]
)
# ACT
lines_availability_test[0].num_rooms_selected = value
# ASSERT
self.assertEqual(
wizard_folio.total_price_folio,
expected_price_total,
"The total price calculation is wrong",
)
def test_price_wizard_correct_pricelist_applied_min_qty_applied(self):
# TEST CASE
# Set values for the wizard and the total price is correct
# (pricelist applied)
# ARRANGE
# common scenario
self.create_common_scenario()
# checkin & checkout
checkin = fields.date.today()
checkout = fields.date.today() + datetime.timedelta(days=1)
days = (checkout - checkin).days
# convert dates to datetimes
dates = self.env["pms.folio.wizard"].get_datetime_from_start_end(checkin)
# set pricelist item for current day
product_tmpl_id = self.test_room_type_double.product_id.product_tmpl_id.id
pricelist_item = self.env["product.pricelist.item"].create(
{
"pricelist_id": self.test_pricelist.id,
"date_start": dates[0],
"date_end": dates[1],
"compute_price": "fixed",
"applied_on": "1_product",
"product_tmpl_id": product_tmpl_id,
"fixed_price": 38.0,
"min_quantity": 4,
}
)
pricelist_item.flush()
# create folio wizard with partner id => pricelist & start-end dates
wizard_folio = self.env["pms.folio.wizard"].create(
{
"start_date": checkin,
"end_date": checkout,
"partner_id": self.partner_id.id,
"pricelist_id": self.test_pricelist.id,
}
)
wizard_folio.flush()
wizard_folio.availability_results._compute_dynamic_selection()
# availability items belonging to test property
lines_availability_test = self.env["pms.folio.availability.wizard"].search(
[
("room_type_id.pms_property_ids", "in", self.test_property.id),
]
)
test_cases = [
{
"num_rooms": 3,
"expected_price": 3 * self.test_room_type_double.list_price * days,
},
{"num_rooms": 4, "expected_price": 4 * pricelist_item.fixed_price * days},
]
for tc in test_cases:
with self.subTest(k=tc):
# ARRANGE
# set value for room type double
value = self.env["pms.num.rooms.selection"].search(
[
("room_type_id", "=", str(self.test_room_type_double.id)),
("value", "=", tc["num_rooms"]),
]
)
# ACT
lines_availability_test[0].num_rooms_selected = value
# ASSERT
self.assertEqual(
wizard_folio.total_price_folio,
tc["expected_price"],
"The total price calculation is wrong",
)
def test_check_create_folio(self):
# TEST CASE
# Set values for the wizard check that folio is created
# ARRANGE
# common scenario
self.create_common_scenario()
# checkin & checkout
checkin = fields.date.today()
checkout = fields.date.today() + datetime.timedelta(days=1)
# create folio wizard with partner id => pricelist & start-end dates
wizard_folio = self.env["pms.folio.wizard"].create(
{
"start_date": checkin,
"end_date": checkout,
"partner_id": self.partner_id.id,
"pricelist_id": self.test_pricelist.id,
}
)
wizard_folio.flush()
wizard_folio.availability_results._compute_dynamic_selection()
# availability items belonging to test property
lines_availability_test = self.env["pms.folio.availability.wizard"].search(
[
("room_type_id.pms_property_ids", "in", self.test_property.id),
]
)
# set one room type double
value = self.env["pms.num.rooms.selection"].search(
[
("room_type_id", "=", str(self.test_room_type_double.id)),
("value", "=", 1),
]
)
lines_availability_test[0].num_rooms_selected = value
# ACT
wizard_folio.create_folio()
# ASSERT
folio = self.env["pms.folio"].search_count(
[("partner_id", "=", self.partner_id.id)]
)
self.assertTrue(folio, "Folio not created.")
def test_check_create_reservations(self):
# TEST CASE
# Set values for the wizard check that reservations are created
# ARRANGE
# common scenario
self.create_common_scenario()
# checkin & checkout
checkin = fields.date.today()
checkout = fields.date.today() + datetime.timedelta(days=1)
# create folio wizard with partner id => pricelist & start-end dates
wizard_folio = self.env["pms.folio.wizard"].create(
{
"start_date": checkin,
"end_date": checkout,
"partner_id": self.partner_id.id,
"pricelist_id": self.test_pricelist.id,
}
)
wizard_folio.flush()
wizard_folio.availability_results._compute_dynamic_selection()
# availability items belonging to test property
lines_availability_test = self.env["pms.folio.availability.wizard"].search(
[
("room_type_id.pms_property_ids", "in", self.test_property.id),
]
)
# set one room type double
value = self.env["pms.num.rooms.selection"].search(
[
("room_type_id", "=", str(self.test_room_type_double.id)),
("value", "=", 2),
]
)
lines_availability_test[0].num_rooms_selected = value
lines_availability_test[0].value_num_rooms_selected = 2
lines_availability_test.flush()
wizard_folio.flush()
# ACT
wizard_folio.create_folio()
folio = self.env["pms.folio"].search([("partner_id", "=", self.partner_id.id)])
folio.flush()
# ASSERT
self.assertEqual(len(folio.reservation_ids), 2, "Reservations not created.")
def test_values_folio_created(self):
# TEST CASE
# Set values for the wizard and values of folio are correct
# ARRANGE
# common scenario
self.create_common_scenario()
# checkin & checkout
checkin = fields.date.today()
checkout = fields.date.today() + datetime.timedelta(days=1)
# create folio wizard with partner id => pricelist & start-end dates
wizard_folio = self.env["pms.folio.wizard"].create(
{
"start_date": checkin,
"end_date": checkout,
"partner_id": self.partner_id.id,
"pricelist_id": self.test_pricelist.id,
}
)
wizard_folio.flush()
wizard_folio.availability_results._compute_dynamic_selection()
# availability items belonging to test property
lines_availability_test = self.env["pms.folio.availability.wizard"].search(
[
("room_type_id.pms_property_ids", "in", self.test_property.id),
]
)
# set one room type double
value = self.env["pms.num.rooms.selection"].search(
[
("room_type_id", "=", str(self.test_room_type_double.id)),
("value", "=", 1),
]
)
lines_availability_test[0].num_rooms_selected = value
lines_availability_test[0].value_num_rooms_selected = 1
# ACT
wizard_folio.create_folio()
vals = {
"partner_id": self.partner_id.id,
"pricelist_id": self.test_pricelist.id,
}
folio = self.env["pms.folio"].search([("partner_id", "=", self.partner_id.id)])
# ASSERT
for key in vals:
with self.subTest(k=key):
self.assertEqual(
folio[key].id,
vals[key],
"The value of " + key + " is not correctly established",
)
def test_values_reservation_created(self):
# TEST CASE
# Set values for the wizard and values of reservations are correct
# ARRANGE
# common scenario
self.create_common_scenario()
# checkin & checkout
checkin = fields.date.today()
checkout = fields.date.today() + datetime.timedelta(days=1)
# create folio wizard with partner id => pricelist & start-end dates
wizard_folio = self.env["pms.folio.wizard"].create(
{
"start_date": checkin,
"end_date": checkout,
"partner_id": self.partner_id.id,
"pricelist_id": self.test_pricelist.id,
}
)
wizard_folio.flush()
wizard_folio.availability_results._compute_dynamic_selection()
# availability items belonging to test property
lines_availability_test = self.env["pms.folio.availability.wizard"].search(
[
("room_type_id.pms_property_ids", "in", self.test_property.id),
]
)
# set one room type double
value = self.env["pms.num.rooms.selection"].search(
[
("room_type_id", "=", str(self.test_room_type_double.id)),
("value", "=", 1),
]
)
lines_availability_test[0].num_rooms_selected = value
lines_availability_test[0].value_num_rooms_selected = 1
# ACT
wizard_folio.create_folio()
folio = self.env["pms.folio"].search([("partner_id", "=", self.partner_id.id)])
vals = {
"folio_id": folio.id,
"checkin": checkin,
"checkout": checkout,
"room_type_id": self.test_room_type_double,
"partner_id": self.partner_id.id,
"pricelist_id": folio.pricelist_id.id,
}
# ASSERT
for reservation in folio.reservation_ids:
for key in vals:
with self.subTest(k=key):
self.assertEqual(
reservation[key].id
if key in ["folio_id", "partner_id", "pricelist_id"]
else reservation[key],
vals[key],
"The value of " + key + " is not correctly established",
)
def test_reservation_line_discounts(self):
# TEST CASE
# set a discount and its applied to the reservation line
# ARRANGE
# common scenario
self.create_common_scenario()
# checkin & checkout
checkin = fields.date.today()
checkout = fields.date.today() + datetime.timedelta(days=1)
discount = 0.5
# create folio wizard with partner id => pricelist & start-end dates
wizard_folio = self.env["pms.folio.wizard"].create(
{
"start_date": checkin,
"end_date": checkout,
"partner_id": self.partner_id.id,
"pricelist_id": self.test_pricelist.id,
"discount": discount,
}
)
wizard_folio.flush()
wizard_folio.availability_results._compute_dynamic_selection()
# availability items belonging to test property
lines_availability_test = self.env["pms.folio.availability.wizard"].search(
[
("room_type_id.pms_property_ids", "in", self.test_property.id),
]
)
# set one room type double
value = self.env["pms.num.rooms.selection"].search(
[
("room_type_id", "=", str(self.test_room_type_double.id)),
("value", "=", 1),
]
)
lines_availability_test[0].num_rooms_selected = value
lines_availability_test[0].value_num_rooms_selected = 1
# ACT
wizard_folio.create_folio()
folio = self.env["pms.folio"].search([("partner_id", "=", self.partner_id.id)])
# ASSERT
for reservation in folio.reservation_ids:
for line in reservation.reservation_line_ids:
with self.subTest(k=line):
self.assertEqual(
line.discount,
discount * 100,
"The discount is not correctly established",
)