Ejemplo n.º 1
0
 def make_reservation_from_data(ctx: Context) -> ResultE[Context]:
     rooms = []
     for room in ctx.source.rooms:
         source_prices = {x.day: x for x in room.day_prices}
         prices = []
         for day in cf.get_days_for_period(ctx.start_date,
                                           ctx.end_date,
                                           exclude=True):
             if day in source_prices:
                 prices.append(
                     attr.evolve(source_prices[day],
                                 roomtype_id=ctx.room.roomtype_id,
                                 room_id=ctx.room.id))
             else:
                 prices.append(
                     ReservationDay(
                         id=None,
                         reservation_room_id=room.id,
                         day=day,
                         roomtype_id=ctx.room.roomtype_id,
                         room_id=ctx.room.id,
                     ))
         rooms.append(
             attr.evolve(room,
                         checkin=ctx.start_date,
                         checkout=ctx.end_date,
                         notes_info=ctx.notes,
                         day_prices=prices))
     ctx.reservation = attr.evolve(ctx.source,
                                   checkin=ctx.start_date,
                                   checkout=ctx.end_date,
                                   close_reason=ctx.reason,
                                   rooms=rooms)
     return Success(ctx)
def reservation(house, room):
    checkin = datetime.date.today()
    checkout = checkin + datetime.timedelta(days=2)
    return Reservation(
        id=111,
        house_id=house.id,
        connection_id=None,
        source=ReservationSources.MANUAL,
        channel=None,
        channel_id="",
        checkin=checkin,
        checkout=checkout,
        booked_at=timezone.now(),
        status=ReservationStatuses.CLOSE,
        is_verified=True,
        rooms=[
            ReservationRoom(
                id=222,
                reservation_id=111,
                channel_id="",
                channel_rate_id="",
                checkin=checkin,
                checkout=checkout,
                notes_info="XXX",
                day_prices=[
                    ReservationDay(id=1,
                                   reservation_room_id=222,
                                   day=checkin,
                                   roomtype_id=room.roomtype_id,
                                   room_id=room.id),
                    ReservationDay(
                        id=2,
                        reservation_room_id=222,
                        day=checkin + datetime.timedelta(days=1),
                        roomtype_id=room.roomtype_id,
                        room_id=room.id,
                    ),
                ],
            )
        ],
    )
def reservation(house, rate_plan, room_type):
    checkin = datetime.date.today()
    checkout = checkin + datetime.timedelta(days=1)
    return Reservation(
        id=111,
        house_id=house.id,
        connection_id=None,
        source=ReservationSources.MANUAL,
        channel=None,
        channel_id='',
        checkin=checkin,
        checkout=checkout,
        booked_at=timezone.now(),
        status=ReservationStatuses.NEW,
        is_verified=True,
        guest_name='John',
        guest_surname='Smith',
        guest_phone='+1-222-3344',
        price=Decimal(1120),
        price_accepted=Decimal(1121),
        netto_price=Decimal(1120),
        netto_price_accepted=Decimal(1121),
        quotation_id=321,
        rooms=[
            ReservationRoom(
                id=211,
                reservation_id=111,
                channel_id='',
                channel_rate_id='',
                checkin=checkin,
                checkout=checkout,
                rate_plan_id=rate_plan.id,
                adults=2,
                children=1,
                price=Decimal(1120),
                price_accepted=Decimal(1121),
                netto_price=Decimal(1120),
                netto_price_accepted=Decimal(1121),
                notes_info='XXX',
                day_prices=[
                    ReservationDay(
                        id=311,
                        reservation_room_id=211,
                        day=checkin,
                        roomtype_id=room_type.id,
                        price_original=Decimal(1120),
                        price_accepted=Decimal(1121),
                        currency=house.currency.code,
                    )
                ],
            )
        ],
    )
Ejemplo n.º 4
0
    def model_to_reservation_day(model: ReservationDayModel) -> ReservationDay:
        return ReservationDay(
            id=model.pk,
            reservation_room_id=model.reservation_room.id,
            day=model.day,
            price_changed=model.price_changed,
            price_accepted=model.price_accepted,
            tax=model.tax,
            currency=model.currency,
            roomtype_id=model.roomtype_id,
            room_id=model.room_id,  # noqa

            # Read-Only
            price_original=model.price_original,
        )
 def make_reservation_from_date(ctx: Context) -> ResultE[Context]:
     ctx.reservation = Reservation(
         id=None,
         house_id=ctx.house.id,
         connection_id=None,
         source=ReservationSources.MANUAL,
         channel=None,
         channel_id='',
         checkin=ctx.start_date,
         checkout=ctx.end_date,
         booked_at=timezone.now(),
         status=ReservationStatuses.CLOSE,
         close_reason=ctx.reason,
         room_count=1,
         currency=ctx.house.currency.code
         if ctx.house.currency is not None else None,
         is_verified=True,
         verified_at=timezone.now(),
     )
     ctx.reservation.rooms.append(
         ReservationRoom(
             id=None,
             reservation_id=None,
             channel_id='',
             channel_rate_id='',
             checkin=ctx.start_date,
             checkout=ctx.end_date,
             currency=ctx.house.currency.code
             if ctx.house.currency is not None else None,
             notes_info=ctx.notes,
         ))
     for day in cf.get_days_for_period(ctx.start_date,
                                       ctx.end_date,
                                       exclude=True):
         ctx.reservation.rooms[0].day_prices.append(
             ReservationDay(
                 id=None,
                 reservation_room_id=None,
                 day=day,
                 roomtype_id=ctx.room.roomtype_id,
                 room_id=ctx.room.id,
                 currency=ctx.house.currency.code
                 if ctx.house.currency is not None else None,
             ))
     return Success(ctx)
def reservation(house, room_type):
    checkin = datetime.date.today()
    checkout = checkin + datetime.timedelta(days=3)
    return Reservation(
        id=111,
        house_id=house.id,
        connection_id=None,
        source=ReservationSources.MANUAL,
        channel=None,
        channel_id='',
        checkin=checkin,
        checkout=checkout,
        booked_at=timezone.now(),
        status=ReservationStatuses.MODIFY,
        price=Decimal(1120),
        netto_price=Decimal(1120),
        rooms=[
            ReservationRoom(
                id=211,
                reservation_id=111,
                channel_id='',
                channel_rate_id='',
                checkin=checkin,
                checkout=checkout,
                rate_plan_id=499,
                rate_plan_id_original=499,
                adults=2,
                children=1,
                price=Decimal(1120),
                price_accepted=Decimal(1120),
                netto_price=Decimal(1120),
                netto_price_accepted=Decimal(1120),
                day_prices=[
                    ReservationDay(
                        id=311,
                        reservation_room_id=211,
                        day=checkin,
                        roomtype_id=room_type.id,
                        price_original=Decimal(1120),
                        price_accepted=Decimal(1120),
                        currency=house.currency.code,
                    ),
                    ReservationDay(
                        id=312,
                        reservation_room_id=211,
                        day=checkin + datetime.timedelta(days=1),
                        roomtype_id=room_type.id,
                        price_original=Decimal(1120),
                        price_accepted=Decimal(1120),
                        currency=house.currency.code,
                    ),
                    ReservationDay(
                        id=313,
                        reservation_room_id=211,
                        day=checkin + datetime.timedelta(days=2),
                        roomtype_id=room_type.id,
                        price_original=Decimal(1120),
                        price_accepted=Decimal(1120),
                        currency=house.currency.code,
                    ),
                ],
            )
        ],
    )
Ejemplo n.º 7
0
def reservation(house, room_type, rate_plan):
    checkin = datetime.date.today()
    checkout = checkin + datetime.timedelta(days=3)
    return Reservation(
        id=111,
        house_id=house.id,
        connection_id=None,
        source=ReservationSources.MANUAL,
        channel=None,
        channel_id='',
        checkin=checkin,
        checkout=checkout,
        booked_at=timezone.now(),
        status=ReservationStatuses.MODIFY,
        price=Decimal(3360),
        price_accepted=Decimal(3360),
        netto_price=Decimal(3360),
        netto_price_accepted=Decimal(3360),
        guest_name='John',
        guest_surname='Smith',
        guest_email='*****@*****.**',
        guest_phone='+1-234-567890',
        opportunity_id=123,
        quotation_id=321,
        rooms=[
            ReservationRoom(
                id=211,
                reservation_id=111,
                channel_id='',
                channel_rate_id='',
                checkin=checkin,
                checkout=checkout,
                rate_plan_id=rate_plan.id,
                adults=2,
                children=1,
                price=Decimal(3360),
                netto_price=Decimal(3360),
                day_prices=[
                    ReservationDay(
                        id=311,
                        reservation_room_id=211,
                        day=checkin,
                        roomtype_id=room_type.id,
                        price_original=Decimal(1120),
                        price_accepted=Decimal(1120),
                        currency=house.currency.code,
                    ),
                    ReservationDay(
                        id=312,
                        reservation_room_id=211,
                        day=checkin + datetime.timedelta(days=1),
                        roomtype_id=room_type.id,
                        price_original=Decimal(1120),
                        price_accepted=Decimal(1120),
                        currency=house.currency.code,
                    ),
                    ReservationDay(
                        id=313,
                        reservation_room_id=211,
                        day=checkin + datetime.timedelta(days=2),
                        roomtype_id=room_type.id,
                        price_original=Decimal(1120),
                        price_accepted=Decimal(1120),
                        currency=house.currency.code,
                    ),
                ],
            )
        ],
    )
    def make_reservation_from_date(ctx: Context) -> ResultE[Context]:
        netto_price = sum(
            ctx.request.prices.values()) if ctx.request.prices else Decimal(0)
        taxes = netto_price * ctx.house.tax / Decimal(
            100) if ctx.house.tax > Decimal(0) else Decimal(0)
        price = netto_price + taxes

        ctx.reservation = Reservation(
            id=None,
            house_id=ctx.house.id,
            connection_id=None,
            source=ReservationSources.MANUAL,
            channel=None,
            channel_id='',
            checkin=ctx.request.checkin,
            checkout=ctx.request.checkout,
            booked_at=timezone.now(),
            status=ReservationStatuses.HOLD,
            room_count=1,
            currency=ctx.house.currency.code
            if ctx.house.currency is not None else None,
            price=price,
            netto_price=netto_price,
            tax=taxes,
            guest_name=ctx.request.guest_name,
            guest_surname=ctx.request.guest_surname,
            guest_email=ctx.request.guest_email,
            guest_phone=ctx.request.guest_phone,
            is_verified=True,
            verified_at=timezone.now(),
        )
        ctx.reservation.rooms.append(
            ReservationRoom(
                id=None,
                reservation_id=None,
                channel_id='',
                channel_rate_id='',
                checkin=ctx.request.checkin,
                checkout=ctx.request.checkout,
                rate_plan_id=ctx.rate_plan.id,
                rate_id=ctx.rate.id if ctx.rate is not None else None,
                guest_name=' '.join(
                    [ctx.request.guest_name,
                     ctx.request.guest_surname]).strip(),
                guest_count=ctx.request.guests,
                adults=ctx.request.guests,
                currency=ctx.house.currency.code
                if ctx.house.currency is not None else None,
                price=ctx.reservation.price,
                netto_price=ctx.reservation.netto_price,
                tax=ctx.reservation.tax,
                notes_info=ctx.request.notes,
                policy=ctx.rate_plan.policy.dump()
                if ctx.rate_plan.policy is not None else {},
            ))
        for day in cf.get_days_for_period(ctx.request.checkin,
                                          ctx.request.checkout,
                                          exclude=True):
            day_price = ctx.request.prices.get(day) or Decimal(0)
            taxes = day_price * ctx.house.tax / Decimal(
                100) if ctx.house.tax > Decimal(0) else Decimal(0)
            ctx.reservation.rooms[0].day_prices.append(
                ReservationDay(
                    id=None,
                    reservation_room_id=None,
                    day=day,
                    roomtype_id=ctx.room_type.id,
                    price_changed=day_price,
                    price_original=day_price,
                    price_accepted=day_price,
                    tax=taxes,
                    currency=ctx.house.currency.code
                    if ctx.house.currency is not None else None,
                ))
        return Success(ctx)
def reservation(house, connection, room_connection, rate_plan_mapping):
    checkin = datetime.date.today()
    checkout = checkin + datetime.timedelta(days=3)
    return Reservation(
        id=111,
        house_id=house.id,
        connection_id=connection.id,
        source=ReservationSources.EXPEDIA,
        channel=Channels.EXPEDIA,
        channel_id='1697441495',
        checkin=checkin,
        checkout=checkout,
        booked_at=timezone.now(),
        status=ReservationStatuses.MODIFY,
        is_verified=False,
        guest_name='John',
        guest_surname='Smith',
        guest_phone='+1-222-3344',
        price=Decimal(1120),
        netto_price=Decimal(1120),
        rooms=[
            ReservationRoom(
                id=211,
                reservation_id=111,
                channel_id=room_connection.channel_id,
                channel_rate_id='',
                checkin=checkin,
                checkout=checkout,
                rate_plan_id=rate_plan_mapping.rate_plan_id,
                rate_plan_id_original=rate_plan_mapping.rate_plan_id,
                policy={'name': 'NEW POLICY'},
                policy_original={'name': 'OLD POLICY'},
                adults=2,
                children=1,
                price=Decimal(1120),
                netto_price=Decimal(1120),
                day_prices=[
                    ReservationDay(
                        id=311,
                        reservation_room_id=211,
                        day=checkin,
                        roomtype_id=room_connection.roomtype_id,
                        price_original=Decimal(1120),
                        price_accepted=Decimal(1120),
                        price_changed=Decimal(1220),
                        currency=house.currency.code,
                    ),
                    ReservationDay(
                        id=312,
                        reservation_room_id=211,
                        day=checkin + datetime.timedelta(days=1),
                        roomtype_id=room_connection.roomtype_id,
                        price_original=Decimal(1120),
                        price_accepted=Decimal(1120),
                        price_changed=Decimal(1220),
                        currency=house.currency.code,
                    ),
                    ReservationDay(
                        id=313,
                        reservation_room_id=211,
                        day=checkin + datetime.timedelta(days=2),
                        roomtype_id=room_connection.roomtype_id,
                        price_original=Decimal(1120),
                        price_accepted=Decimal(1120),
                        price_changed=Decimal(1220),
                        currency=house.currency.code,
                    ),
                ],
            )
        ],
    )
def test_make_cached_reservations_split(service: UpdateReservationCache, house,
                                        rate_plan, room_type):
    checkin = datetime.date.today()
    checkout = checkin + datetime.timedelta(days=2)
    room_type_2 = attr.evolve(room_type, id=101)
    reservation = Reservation(
        id=111,
        house_id=house.id,
        connection_id=None,
        source=ReservationSources.MANUAL,
        channel=None,
        channel_id='',
        checkin=checkin,
        checkout=checkout,
        booked_at=timezone.now(),
        status=ReservationStatuses.NEW,
        is_verified=True,
        guest_name='John',
        guest_surname='Smith',
        guest_phone='+1-222-3344',
        price=Decimal(1120),
        price_accepted=Decimal(1121),
        netto_price=Decimal(1120),
        netto_price_accepted=Decimal(1121),
        quotation_id=321,
        rooms=[
            ReservationRoom(
                id=211,
                reservation_id=111,
                channel_id='',
                channel_rate_id='',
                checkin=checkin,
                checkout=checkout,
                rate_plan_id=rate_plan.id,
                adults=2,
                children=1,
                price=Decimal(1120),
                price_accepted=Decimal(1121),
                netto_price=Decimal(1120),
                netto_price_accepted=Decimal(1121),
                day_prices=[
                    ReservationDay(
                        id=311,
                        reservation_room_id=211,
                        day=checkin,
                        roomtype_id=room_type.id,
                        price_original=Decimal(560),
                        price_accepted=Decimal(561),
                        currency=house.currency.code,
                    ),
                    ReservationDay(
                        id=311,
                        reservation_room_id=211,
                        day=checkin + datetime.timedelta(days=1),
                        roomtype_id=room_type_2.id,
                        price_original=Decimal(560),
                        price_accepted=Decimal(561),
                        currency=house.currency.code,
                    ),
                ],
            )
        ],
    )

    result = service.make_cached_reservations(reservation, house,
                                              {rate_plan.id: rate_plan},
                                              {321: Decimal(30)})

    assert len(result) == 2
    assert all([isinstance(x, CachedReservation) for x in result])

    assert result[0].pk == '111-211-1'
    assert result[0].reservation_id == 111
    assert result[0].grid == 'roomtype'
    assert result[0].grid_id == room_type.id
    assert result[0].checkin == datetime.datetime.combine(
        checkin, datetime.time(14))
    assert result[0].checkout == datetime.datetime.combine(
        checkin + datetime.timedelta(days=1), datetime.time(11))
    assert result[0].source is None
    assert result[0].source_code == ReservationSources.MANUAL.name
    assert result[0].status is None
    assert result[0].adults == 2
    assert result[0].children == 1
    assert result[0].meal == Foods.RO.value
    assert result[0].name == 'John Smith'
    assert result[0].phone == '+1-222-3344'
    assert result[0].money_room == '$1121.00'
    assert result[0].money_extra == '$0.00'
    assert result[0].total == '$1121.00'
    assert result[0].payed == '$30.00'
    assert result[0].balance == '$1091.00'
    assert result[0].has_balance

    assert result[1].pk == '111-211-2'
    assert result[1].reservation_id == 111
    assert result[1].grid == 'roomtype'
    assert result[1].grid_id == room_type_2.id
    assert result[1].checkin == datetime.datetime.combine(
        checkin + datetime.timedelta(days=1), datetime.time(14))
    assert result[1].checkout == datetime.datetime.combine(
        checkout, datetime.time(11))
    assert result[1].source is None
    assert result[1].source_code == ReservationSources.MANUAL.name
    assert result[0].status is None
    assert result[1].adults == 2
    assert result[1].children == 1
    assert result[1].meal == Foods.RO.value
    assert result[1].name == 'John Smith'
    assert result[1].phone == '+1-222-3344'
    assert result[1].money_room == '$1121.00'
    assert result[1].money_extra == '$0.00'
    assert result[1].total == '$1121.00'
    assert result[1].payed == '$30.00'
    assert result[1].balance == '$1091.00'
    assert result[1].has_balance
    def make_reservation_from_data(self, ctx: Context) -> ResultE[Context]:
        ctx.reservation = attr.evolve(ctx.source, rooms=[])

        for room in ctx.source.rooms:
            if room.id != ctx.reservation_room.id:
                # Other rooms copy without changes
                ctx.reservation.rooms.append(room)
                continue

            # Update selected room
            _room = attr.evolve(room, day_prices=[])
            if _room.rate_plan_id != ctx.rate_plan.id:
                _room.rate_plan_id = ctx.rate_plan.id
                _room.policy = ctx.rate_plan.policy.dump(
                ) if ctx.rate_plan.policy is not None else {}

            # Get one of used Room Types than will be used for new day prices
            failback_roomtype_id = self._room_type_for_failback(room)

            # Update daily prices
            day_prices = {x.day: x for x in room.day_prices}
            for day, data in ctx.prices.items():
                # Set price
                new_price = cf.get_decimal_or_none(
                    data.get('price')) or Decimal(0)
                if day in day_prices:
                    _price = attr.evolve(day_prices[day])
                else:
                    _price = ReservationDay(id=None,
                                            reservation_room_id=_room.id,
                                            day=day,
                                            price_changed=new_price)
                _price.price_accepted = new_price
                _price.tax = _price.price_accepted * ctx.house.tax / Decimal(
                    100)

                # Set room
                new_room = ctx.rooms.get(
                    cf.get_int_or_none(data.get('room')) or 0)
                _price.room_id = new_room.id if new_room is not None else None
                if new_room is not None and new_room.roomtype_id in ctx.room_types:
                    _price.roomtype_id = new_room.roomtype_id
                if _price.roomtype_id is None or _price.roomtype_id <= 0:
                    _price.roomtype_id = failback_roomtype_id

                _room.day_prices.append(_price)

            # Recalculate room prices
            _room.netto_price_accepted = sum(
                [x.price_accepted for x in _room.day_prices])
            _room.tax = sum([x.tax for x in _room.day_prices])
            _room.price_accepted = _room.netto_price_accepted + _room.tax
            _room.checkin = min([x.day for x in _room.day_prices])
            _room.checkout = max(
                [x.day for x in _room.day_prices]) + datetime.timedelta(
                    days=1)  # checkout next day

            ctx.reservation.rooms.append(_room)

        # Recalculate reservation total prices
        ctx.reservation.netto_price_accepted = sum(
            [x.netto_price_accepted for x in ctx.reservation.rooms])
        ctx.reservation.price_accepted = sum(
            [x.price_accepted for x in ctx.reservation.rooms])
        ctx.reservation.tax = sum([x.tax for x in ctx.reservation.rooms])
        ctx.reservation.checkin = min(
            [x.checkin for x in ctx.reservation.rooms])
        ctx.reservation.checkout = max(
            [x.checkout for x in ctx.reservation.rooms])

        return Success(ctx)