public function saveRate($details, $apartmentId) { /** * @var \DDD\Dao\Apartment\Rate $rateDao * @var \DDD\Service\Apartment\Inventory $inventoryService */ $rateDao = $this->getServiceLocator()->get('dao_apartment_rate'); $inventoryService = $this->getServiceLocator()->get('service_apartment_inventory'); $apartmentRateDao = $this->getApartmentRateDao(); $rateList = $this->getApartmentRates($apartmentId); $rateID = $details['id']; unset($details['id']); unset($details['save_button']); $editMode = $rateID; $data = []; if (!$rateList->count() || !isset($details['type']) && isset($details['is_parent']) && $details['is_parent']) { $isParent = true; $data['type'] = self::TYPE1; } else { $isParent = false; $data['type'] = $details['type']; } //parent rate if ($isParent) { $weekPrice = $details['weekday_price']; $weekEndPrice = $details['weekend_price']; } else { // not parent rate $weekPricePercent = $details['weekday_price']; $weekendPricePercent = $details['weekend_price']; $weekPlusMinus = $details['week_day_plus_minus']; $weekendPlusMinus = $details['weekend_plus_minus']; $parentRatePrices = $rateDao->getApartmentParentRatePrices($apartmentId); $weekPriceParent = $parentRatePrices['week_price']; $data['week_percent'] = $weekPricePercent * $weekPlusMinus; $weekendPriceParent = $parentRatePrices['weekend_price']; $data['weekend_percent'] = $weekendPricePercent * $weekendPlusMinus; if ($weekPlusMinus > 0) { $weekPrice = $weekPriceParent + $weekPriceParent * $weekPricePercent / 100; } else { $weekPrice = $weekPriceParent - $weekPriceParent * $weekPricePercent / 100; } if ($weekendPlusMinus > 0) { $weekEndPrice = $weekendPriceParent + $weekendPriceParent * $weekendPricePercent / 100; } else { $weekEndPrice = $weekendPriceParent - $weekendPriceParent * $weekendPricePercent / 100; } } $data['active'] = $details['active']; $data['capacity'] = $details['capacity']; $data['week_price'] = round($weekPrice, 2); $data['weekend_price'] = round($weekEndPrice, 2); $data['min_stay'] = $details['min_stay']; $data['max_stay'] = $details['max_stay']; $data['release_period_start'] = $details['release_window_start']; $data['release_period_end'] = $details['release_window_end']; $data['is_refundable'] = $details['is_refundable']; $data['penalty_type'] = $details['penalty_type']; $data['refundable_before_hours'] = $details['refundable_before_hours']; $data['penalty_percent'] = $details['penalty_percent']; $data['penalty_nights'] = $details['penalty_nights']; $data['penalty_fixed_amount'] = $details['penalty_fixed_amount']; $data['name'] = $details['rate_name']; if (!(int) $details['active']) { $data['cubilis_id'] = null; } if ($details['is_refundable'] == self::APARTMENT_RATE_NON_REFUNDABLE) { $data['penalty_type'] = 1; $data['penalty_percent'] = 100; $data['penalty_nights'] = 0; $data['penalty_fixed_amount'] = 1; } if ($editMode) { $apartmentRateDao->save($data, ['id' => $rateID]); // if change parent rate price, should change child rate price if ($isParent) { $rateDao->updateChildRatePrice($apartmentId, $data['week_price'], $data['weekend_price']); } } else { $roomDao = new ProductRoom($this->getServiceLocator()); $roomDomain = $roomDao->getById($apartmentId); $data['apartment_id'] = $roomDomain->getApartmentId(); $data['room_id'] = $roomDomain->getId(); $rateID = $apartmentRateDao->save($data); } // insert data if (!$editMode) { if (!$inventoryService->updateAvailability(true, $apartmentId)) { return ['status' => 'error', 'msg' => 'Rate was successfully updated, but availability table could not be filled.']; } } return ['status' => 'success', 'msg' => $editMode ? TextConstants::SUCCESS_UPDATE : TextConstants::SUCCESS_CREATED, 'rate_id' => $rateID]; }
/** * @param $params * @param $apartmentId * @return bool */ public function linkRoomRate($params, $apartmentId) { if (is_array($params) && isset($params['product_rates']) && isset($params['product_room'])) { if (isset($params['product_room']['room_id']) && isset($params['product_room']['cubilis_room_id'])) { $productTypeDao = new Room($this->getServiceLocator()); $productTypeDao->updateCubilisLink($params['product_room']['room_id'], $params['product_room']['cubilis_room_id']); } else { return ['status' => 'error', 'msg' => TextConstants::BAD_REQUEST]; } if (count($params['product_rates'])) { /** @var \DDD\Dao\Apartment\Rate $productRateDao */ $productRateDao = $this->getServiceLocator()->get('dao_apartment_rate'); $productRateDao->clearCubilisLinks($params['product_room']['room_id']); foreach ($params['product_rates'] as $room) { if (isset($room['rate_id']) && isset($room['cubilis_rate_id']) && (int) $room['rate_active']) { $productRateDao->updateCubilisLink($room['rate_id'], $room['cubilis_rate_id']); } } } else { return ['status' => 'error', 'msg' => TextConstants::BAD_REQUEST]; } } else { return ['status' => 'error', 'msg' => TextConstants::BAD_REQUEST]; } /** * @var \DDD\Service\Queue\InventorySynchronizationQueue $syncService * @var \DDD\Dao\Apartment\Inventory $inventoryDao */ $inventoryDao = $this->getServiceLocator()->get('dao_apartment_inventory'); $syncService = $this->getServiceLocator()->get('service_queue_inventory_synchronization_queue'); // get min max date from inventory $dates = $inventoryDao->getMinMaxDate(); // sync to queue $syncService->push($apartmentId, $dates['min_date'], $dates['max_date']); return ['status' => 'success', 'msg' => TextConstants::SUCCESS_UPDATE]; }