public function run()
 {
     $faker = Faker::create();
     foreach (range(1, 10) as $index) {
         PropertiesChannel::create([]);
     }
 }
 /**
  * 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'));
 }
 /**
  * @param $channelId
  * @param $propertyId
  * @return \Illuminate\Database\Eloquent\Model|null|static|PropertiesChannel
  */
 public static function getSettings($channelId, $propertyId)
 {
     return PropertiesChannel::where(['channel_id' => $channelId, 'property_id' => $propertyId])->first();
 }
 /**
  * Display a listing of the resource.
  * GET /reservations
  *
  * @return Response
  */
 public function getIndex()
 {
     $execResult = ['updated' => 0, 'created' => 0, 'bookings' => 0, 'cancelled' => 0, 'not_mapped' => 0];
     $propertiesChannels = PropertiesChannel::where('property_id', Property::getLoggedId())->get();
     foreach ($propertiesChannels as $channelSettings) {
         Log::debug($channelSettings);
         $channel = ChannelFactory::create($channelSettings);
         $result = $channel->getReservations();
         Log::debug($result);
         if ($result['reservations']) {
             foreach ($result['reservations'] as $reservation) {
                 $reservation['channel_id'] = $channelSettings->channel_id;
                 $reservation['property_id'] = $channelSettings->property_id;
                 $resModel = Reservation::getByKeys($channelSettings->channel_id, $channelSettings->property_id)->where('res_id', $reservation['res_id'])->first();
                 if (isset($reservation['cc_details']) && !empty($reservation['cc_details'])) {
                     $reservation['cc_details'] = Crypt::encrypt($reservation['cc_details']);
                 }
                 switch ($reservation['status']) {
                     case 'cancelled':
                         if ($resModel) {
                             $resModel->status = 'cancelled';
                             if ($reservation['res_cancel_fee']) {
                                 $resModel->res_cancel_fee = $reservation['res_cancel_fee'];
                             }
                             $resModel->cancelled_at = $resModel->freshTimestamp();
                             $resModel->save();
                             $execResult['cancelled']++;
                             //TODO: send email about cancellation
                         }
                         break;
                     case 'booked':
                         $needAddRooms = true;
                         if ($resModel) {
                             if (isset($reservation['modified']) && $reservation['modified']) {
                                 $resModel->update($reservation);
                                 $execResult['updated']++;
                                 $resModel->bookings()->delete();
                                 //TODO: send email about modification
                             } else {
                                 $needAddRooms = false;
                             }
                         } else {
                             $resModel = Reservation::create($reservation);
                             $execResult['created']++;
                         }
                         if ($reservation['rooms'] && $needAddRooms) {
                             foreach ($reservation['rooms'] as $room) {
                                 $room['reservation_id'] = $resModel->id;
                                 $room['channel_id'] = $reservation['channel_id'];
                                 $room['property_id'] = $reservation['property_id'];
                                 $mapping = InventoryMap::getMappedRoom($channelSettings->channel_id, $channelSettings->property_id, $room['inventory'], isset($room['plan']) ? $room['plan'] : null)->first();
                                 if ($mapping) {
                                     $room['room_id'] = $mapping->room_id;
                                 } else {
                                     $execResult['not_mapped']++;
                                     //TODO: send email about NOT MAPPED ROOM
                                 }
                                 Booking::create($room);
                                 $execResult['bookings']++;
                             }
                         }
                         break;
                 }
                 if ($resModel && $resModel->id) {
                     $type = $resModel->status;
                     if ($type != 'cancelled' && $resModel->modified) {
                         $type = 'modify';
                     }
                     $channel->setReservationConfirmation($resModel->id, $resModel->res_id, $type);
                 }
             }
         }
     }
     return View::make('index', compact('execResult'));
 }
 /**
  * Remove the specified propertieschannel from storage.
  *
  * @param  int $channelId
  * @return Response
  */
 public function getDestroy($channelId)
 {
     PropertiesChannel::where(['channel_id' => $channelId, 'property_id' => Property::getLoggedId()])->delete();
     return Redirect::action('PropertiesChannelsController@getIndex');
 }
 /**
  * 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);
             }
         }
     }
 }