mirror of
https://github.com/OCA/pms.git
synced 2025-01-29 00:17:45 +02:00
[FIX]pms: create and update checkins (#53)
This commit is contained in:
@@ -202,19 +202,35 @@ class PmsCheckinPartner(models.Model):
|
||||
draft_checkins = reservation.checkin_partner_ids.filtered(
|
||||
lambda c: c.state == "draft"
|
||||
)
|
||||
if len(draft_checkins) > 0 and vals.get("partner_id"):
|
||||
if len(draft_checkins) > 0:
|
||||
draft_checkins[0].write(vals)
|
||||
return draft_checkins[0]
|
||||
if vals.get("identifier", _("New")) == _("New") or "identifier" not in vals:
|
||||
elif vals.get("identifier", _("New")) == _("New") or "identifier" not in vals:
|
||||
pms_property_id = (
|
||||
self.env.user.get_active_property_ids()[0]
|
||||
if "pms_property_id" not in vals
|
||||
else vals["pms_property_id"]
|
||||
)
|
||||
vals["identifier"] = self.env["ir.sequence"].search(
|
||||
[("pms_property_id", "=", pms_property_id)]
|
||||
).next_by_code("pms.checkin.partner") or _("New")
|
||||
return super(PmsCheckinPartner, self).create(vals)
|
||||
vals["identifier"] = (
|
||||
self.env["ir.sequence"]
|
||||
.search(
|
||||
[
|
||||
("code", "=", "pms.checkin.partner"),
|
||||
"|",
|
||||
("pms_property_id", "=", pms_property_id),
|
||||
("pms_property_id", "=", False),
|
||||
]
|
||||
)
|
||||
._next_do()
|
||||
or _("New")
|
||||
)
|
||||
return super(PmsCheckinPartner, self).create(vals)
|
||||
raise ValidationError(
|
||||
_(
|
||||
"Either the reservation is not being indicated or it is \
|
||||
not possible to create the proposed check-in in this reservation"
|
||||
)
|
||||
)
|
||||
|
||||
def write(self, vals):
|
||||
res = super(PmsCheckinPartner, self).write(vals)
|
||||
@@ -233,11 +249,13 @@ class PmsCheckinPartner(models.Model):
|
||||
field
|
||||
):
|
||||
key = True
|
||||
# REVIEW: if partner exist, we can merge?
|
||||
partner = ResPartner.search(
|
||||
[(field, "=", getattr(record, field))]
|
||||
)
|
||||
if key:
|
||||
partner = ResPartner.create(partner_vals)
|
||||
if not partner:
|
||||
partner = ResPartner.create(partner_vals)
|
||||
record.partner_id = partner
|
||||
|
||||
if any(field in vals for field in self._checkin_partner_fields()):
|
||||
@@ -251,6 +269,12 @@ class PmsCheckinPartner(models.Model):
|
||||
record.partner_id.write(partner_vals)
|
||||
return res
|
||||
|
||||
def unlink(self):
|
||||
reservations = self.mapped("reservation_id")
|
||||
res = super().unlink()
|
||||
reservations._compute_checkin_partner_ids()
|
||||
return res
|
||||
|
||||
def action_on_board(self):
|
||||
for record in self:
|
||||
if record.reservation_id.checkin > fields.Date.today():
|
||||
|
||||
@@ -186,7 +186,6 @@ class TestPmsCheckinPartner(TestHotel):
|
||||
0,
|
||||
{
|
||||
"partner_id": host4.id,
|
||||
"reservation_id": self.reservation_1.id,
|
||||
},
|
||||
)
|
||||
]
|
||||
@@ -385,6 +384,16 @@ class TestPmsCheckinPartner(TestHotel):
|
||||
# emails that should be detected as incorrect
|
||||
|
||||
# ARRANGE
|
||||
reservation = self.env["pms.reservation"].create(
|
||||
{
|
||||
"checkin": "2012-01-14",
|
||||
"checkout": "2012-01-17",
|
||||
"room_type_id": self.env.ref("pms.pms_room_type_3").id,
|
||||
"partner_id": self.env.ref("base.res_partner_12").id,
|
||||
"adults": 3,
|
||||
"pms_property_id": self.env.ref("pms.main_pms_property").id,
|
||||
}
|
||||
)
|
||||
test_cases = [
|
||||
"myemail",
|
||||
"myemail@",
|
||||
@@ -397,13 +406,36 @@ class TestPmsCheckinPartner(TestHotel):
|
||||
for mail in test_cases:
|
||||
with self.subTest(i=mail):
|
||||
with self.assertRaises(ValidationError):
|
||||
self.env["pms.checkin.partner"].create({"email": mail})
|
||||
reservation.write(
|
||||
{
|
||||
"checkin_partner_ids": [
|
||||
(
|
||||
0,
|
||||
False,
|
||||
{
|
||||
"name": "Carlos",
|
||||
"email": mail,
|
||||
},
|
||||
)
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
def test_valid_emails(self):
|
||||
# TEST CASES
|
||||
# emails that should be detected as correct
|
||||
|
||||
# ARRANGE
|
||||
reservation = self.env["pms.reservation"].create(
|
||||
{
|
||||
"checkin": "2012-01-14",
|
||||
"checkout": "2012-01-17",
|
||||
"room_type_id": self.env.ref("pms.pms_room_type_3").id,
|
||||
"partner_id": self.env.ref("base.res_partner_12").id,
|
||||
"adults": 3,
|
||||
"pms_property_id": self.env.ref("pms.main_pms_property").id,
|
||||
}
|
||||
)
|
||||
test_cases = [
|
||||
"hello@commitsun.com",
|
||||
"hi.welcome@commitsun.com",
|
||||
@@ -413,17 +445,34 @@ class TestPmsCheckinPartner(TestHotel):
|
||||
]
|
||||
for mail in test_cases:
|
||||
with self.subTest(i=mail):
|
||||
guest = self.env["pms.checkin.partner"].create({"email": mail})
|
||||
guest = self.env["pms.checkin.partner"].create(
|
||||
{
|
||||
"name": "Carlos",
|
||||
"email": mail,
|
||||
"reservation_id": reservation.id,
|
||||
}
|
||||
)
|
||||
self.assertEqual(
|
||||
mail,
|
||||
guest.email,
|
||||
)
|
||||
guest.unlink()
|
||||
|
||||
def test_not_valid_phone(self):
|
||||
# TEST CASES
|
||||
# phones that should be detected as incorrect
|
||||
|
||||
# ARRANGE
|
||||
reservation = self.env["pms.reservation"].create(
|
||||
{
|
||||
"checkin": "2012-01-14",
|
||||
"checkout": "2012-01-17",
|
||||
"room_type_id": self.env.ref("pms.pms_room_type_3").id,
|
||||
"partner_id": self.env.ref("base.res_partner_12").id,
|
||||
"adults": 3,
|
||||
"pms_property_id": self.env.ref("pms.main_pms_property").id,
|
||||
}
|
||||
)
|
||||
test_cases = [
|
||||
"phone",
|
||||
"123456789123",
|
||||
@@ -434,13 +483,29 @@ class TestPmsCheckinPartner(TestHotel):
|
||||
for phone in test_cases:
|
||||
with self.subTest(i=phone):
|
||||
with self.assertRaises(ValidationError):
|
||||
self.env["pms.checkin.partner"].create({"mobile": phone})
|
||||
self.env["pms.checkin.partner"].create(
|
||||
{
|
||||
"name": "Carlos",
|
||||
"mobile": phone,
|
||||
"reservation_id": reservation.id,
|
||||
}
|
||||
)
|
||||
|
||||
def test_valid_phones(self):
|
||||
# TEST CASES
|
||||
# emails that should be detected as incorrect
|
||||
|
||||
# ARRANGE
|
||||
reservation = self.env["pms.reservation"].create(
|
||||
{
|
||||
"checkin": "2012-01-14",
|
||||
"checkout": "2012-01-17",
|
||||
"room_type_id": self.env.ref("pms.pms_room_type_3").id,
|
||||
"partner_id": self.env.ref("base.res_partner_12").id,
|
||||
"adults": 3,
|
||||
"pms_property_id": self.env.ref("pms.main_pms_property").id,
|
||||
}
|
||||
)
|
||||
test_cases = [
|
||||
"981 981 981",
|
||||
"981981981",
|
||||
@@ -448,7 +513,13 @@ class TestPmsCheckinPartner(TestHotel):
|
||||
]
|
||||
for mobile in test_cases:
|
||||
with self.subTest(i=mobile):
|
||||
guest = self.env["pms.checkin.partner"].create({"mobile": mobile})
|
||||
guest = self.env["pms.checkin.partner"].create(
|
||||
{
|
||||
"name": "Carlos",
|
||||
"mobile": mobile,
|
||||
"reservation_id": reservation.id,
|
||||
}
|
||||
)
|
||||
self.assertEqual(
|
||||
mobile,
|
||||
guest.mobile,
|
||||
|
||||
Reference in New Issue
Block a user