/**
  * Show the form for editing the specified room.
  *
  * @param int $id
  * @param int $channelId
  * @return Response
  */
 public function getMap($id, $channelId)
 {
     $room = Room::find($id);
     $channelSettings = PropertiesChannel::getSettings($channelId, Property::getLoggedId());
     $channel = ChannelFactory::create($channelSettings);
     $result = $channel->getInventoryList();
     //todo temp
     //        file_put_contents('1.txt', serialize($result));
     //        $result = unserialize(file_get_contents('1.txt'));
     //add Inventories and Plans to DB//TODO move to another place
     //delete exist maps
     Inventory::where(['channel_id' => $channelId, 'property_id' => $channelSettings->property_id])->delete();
     //delete exist plan maps
     InventoryPlan::where(['channel_id' => $channelId, 'property_id' => $channelSettings->property_id])->delete();
     if ($result) {
         foreach ($result as $inventory) {
             Inventory::create(['code' => $inventory['code'], 'name' => $inventory['name'], 'channel_id' => $channelId, 'property_id' => $channelSettings->property_id]);
             if ($inventory['plans']) {
                 foreach ($inventory['plans'] as $plan) {
                     InventoryPlan::create(['code' => $plan['code'], 'name' => $plan['name'], 'channel_id' => $channelId, 'inventory_code' => $inventory['code'], 'property_id' => $channelSettings->property_id]);
                 }
             }
         }
     }
     $existMapping = [];
     //        $mapCollection = InventoryMap::where(
     //            [
     //                'channel_id' => $channelId,
     //                'property_id' => $channelSettings->property_id
     //            ]
     //        )
     ////            ->where('room_id', '<>', $id)
     //            ->lists('inventory_code', 'room_id');
     //        if ($mapCollection) {
     //            foreach ($mapCollection as $map) {
     //                $existMapping[] = $map;
     //            }
     //        }>F
     $inventories = Channel::find($channelId)->inventory()->where('property_id', Property::getLoggedId());
     $inventoryList = [];
     $inventoryPlans = [];
     foreach ($inventories->get() as $inventory) {
         //            if (in_array($inventory->code, $existMapping)) {
         //                continue;
         //            }
         $inventoryList[$inventory->code] = $inventory->name;
         $plans = $inventory->plans()->get(['name', 'code']);
         for ($i = 0; $i < count($plans); $i++) {
             //TODO rewrite to ONE query
             $plans[$i]->selected = InventoryMap::getByKeys($channelId, $channelSettings->property_id, $id, $plans[$i]['code'])->first() ? true : false;
         }
         $inventoryPlans[$inventory->code] = $plans;
     }
     $inventoryPlans = json_encode($inventoryPlans);
     $mapping = InventoryMap::getByKeys($channelId, $channelSettings->property_id, $id)->first();
     return View::make('rooms.map', compact('room', 'channel', 'inventoryList', 'inventoryPlans', 'channelId', 'mapping'));
 }
Пример #2
0
 /**
  * Recursive function
  * TODO: move to another place
  * @param Room $room
  * @param Property $property
  * @param $data
  * @param $rate - rate value for update chanel
  * @param $weekDays
  * @param $errors
  * @param $depth
  */
 function updateChannelRate($room, $property, $data, $rate, $weekDays, &$errors, &$depth)
 {
     if ($depth > 5) {
         //infinity loop protection
         return;
     }
     //get plan mapping
     $maps = InventoryMap::getByKeys(null, $property->id, $room->id)->get();
     foreach ($maps as $mapping) {
         //get channel
         $channelSettings = PropertiesChannel::getSettings($mapping->channel_id, $mapping->property_id);
         if (!$channelSettings) {
             continue;
         }
         $channel = ChannelFactory::create($channelSettings);
         $channel->setCurrency($property->currency);
         //updating rates
         $result = $channel->setRate($mapping->inventory_code, $mapping->plan_code, $data['from_date'], $data['to_date'], $weekDays, $rate, isset($data['single_rate']) ? $data['single_rate'] : null);
         if (is_array($result)) {
             $formattedErrors = [];
             foreach ($result as $error) {
                 $formattedErrors[] = $channelSettings->channel()->name . ': ' . $error;
             }
             $errors += $formattedErrors;
         }
     }
     //check if children rooms exist
     if ($children = $room->children()->get()) {
         if (!$children->isEmpty()) {
             $depth++;
             //so we go deep so lets do rate of current ROOM as default rate,
             //like if we directly set this rate in form
             $data['rate'] = $rate;
             foreach ($children as $child) {
                 switch ($child->formula_type) {
                     case 'x':
                         $rate = $data['rate'] * $child->formula_value;
                         break;
                     case '+':
                         $rate = $data['rate'] + $child->formula_value;
                         break;
                     case '-':
                         $rate = $data['rate'] - $child->formula_value;
                         break;
                 }
                 $this->updateChannelRate($child, $property, $data, $rate, $weekDays, $errors, $depth);
             }
         }
     }
 }