Esempio n. 1
0
 def select_payed_amounts(self, ctx: Context) -> ResultE[Context]:
     quotation_ids = [
         x.quotation_id for x in ctx.reservations
         if x.quotation_id is not None and x.quotation_id > 0
     ]
     if not quotation_ids:
         return Success(ctx)
     for quotation_id in quotation_ids:
         try:
             invoices = self.get_rpc_api(ctx).select_invoices_for_order(
                 quotation_id)
             if not invoices:
                 continue
             ctx.payed_amounts[quotation_id] = sum([
                 cf.get_decimal_or_none(x.amount_total - x.amount_residual)
                 or Decimal(0) for x in invoices
             ])
         except Exception as err:
             return self._error(
                 f"Error select Invoices for Quotation ID={quotation_id} in House ID={ctx.house.id}",
                 ctx,
                 self._case_errors.error,
                 exc=err,
             )
     return Success(ctx)
Esempio n. 2
0
 def assign_prices_to_reservation(self, data: List[Any]) -> None:
     prices = [cf.get_decimal_or_none(x) for x in data]
     prices = [x for x in prices if x is not None]
     if len(prices) != (self._reservation.checkout -
                        self._reservation.checkin).days:
         raise AssertionError(
             'Count of prices is not equal night count in reservation')
     days = cf.get_days_for_period(self._reservation.checkin,
                                   self._reservation.checkout,
                                   exclude=True)
     for i, x in enumerate(days):
         self._reservation.prices[x] = prices[i]
 def parse_price_data(
         data: Dict[str, Any]) -> Dict[datetime.date, Dict[str, Any]]:
     result = {}
     pattern = re.compile(r'\[(\d{4}-\d{2}-\d{2})]')
     for key, value in data.items():
         if not key.startswith('room[') and not key.startswith('price['):
             continue
         match = pattern.search(key)
         if match is None:
             continue
         day = cf.get_date_or_none(match.group(1))
         if day is None:
             continue
         if day not in result:
             result[day] = {'room': None, 'price': None, 'day': day}
         if key.startswith('room['):
             result[day]['room'] = cf.get_int_or_none(value)
         elif key.startswith('price['):
             result[day]['price'] = cf.get_decimal_or_none(value)
     return result
Esempio n. 4
0
 def select_payed_amount(self, ctx: Context) -> ResultE[Context]:
     if ctx.reservation.quotation_id is None or ctx.reservation.quotation_id <= 0:
         return Success(ctx)
     try:
         invoices = self.get_rpc_api(ctx).select_invoices_for_order(
             ctx.reservation.quotation_id)
     except Exception as err:
         return self._error(
             f"Error select Invoices for Quotation ID={ctx.reservation.quotation_id} "
             f"for Reservation ID={ctx.reservation.id}",
             ctx,
             self._case_errors.error,
             exc=err,
         )
     if not invoices:
         return Success(ctx)
     ctx.payed_amount = sum([
         cf.get_decimal_or_none(x.amount_total - x.amount_residual)
         or Decimal(0) for x in invoices
     ])
     return Success(ctx)
    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)