Esempio n. 1
0
 public static function CreateChannel($json)
 {
     $logger = \Swiftriver\Core\Setup::GetLogger();
     $logger->log("Core::ObjectModel::ObjectFactories::ChannelFactory::CreateChannel [Method invoked]", \PEAR_LOG_DEBUG);
     $logger->log("Core::ObjectModel::ObjectFactories::ChannelFactory::CreateChannel [Calling json_decode]", \PEAR_LOG_DEBUG);
     $data = json_decode($json);
     $logger->log("Core::ObjectModel::ObjectFactories::ChannelFactory::CreateChannel [Extracting data from the JSON objects]", \PEAR_LOG_DEBUG);
     if (!isset($data) || !$data) {
         throw new \InvalidArgumentException("There was an error in the JSON. No Channel can be constructed.");
     }
     $logger->log("Core::ObjectModel::ObjectFactories::ChannelFactory::CreateChannel [Extracting values from the data]", \PEAR_LOG_DEBUG);
     $logger->log("Core::ObjectModel::ObjectFactories::ChannelFactory::CreateChannel [Constructing Channel object]", \PEAR_LOG_DEBUG);
     $channel = new \Swiftriver\Core\ObjectModel\Channel();
     $channel->id = isset($data->id) ? $data->id : null;
     $channel->type = $data->type;
     $channel->updatePeriod = $data->updatePeriod;
     $channel->active = isset($data->active) ? $data->active : true;
     $channel->lastSucess = isset($data->lastSucess) ? $data->lastSucess : null;
     $channel->inprocess = isset($data->inprocess) ? $data->inprocess : false;
     $params = array();
     foreach ($data->parameters as $key => $value) {
         $params[$key] = $value;
     }
     $channel->parameters = $params;
     //set key values if they have not been set
     $channel = ChannelFactory::SetValuesIfNotSet($channel, array("id" => md5(uniqid(rand(), true)), "active" => true, "nextrun" => time() + $channel->updatePeriod * 60));
     $logger->log("Core::ObjectModel::ObjectFactories::ChannelFactory::CreateChannel [Method finished]", \PEAR_LOG_DEBUG);
     return $channel;
 }
 /**
  * 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'));
 }
Esempio n. 3
0
 public function run()
 {
     global $geo_cities, $geo_countries;
     $geo_cities = set_by_id(DB::f("select * from geo_cities"));
     $geo_countries = set_by_id(DB::f("select * from geo_countries"));
     Auth::run();
     if ($user = Auth::getUser()) {
         $this->user = $user;
     }
     $this->parseRequest();
     $old_CID = null;
     $is_called_common = 0;
     while ($this->CID != $old_CID) {
         $old_CID = $this->CID;
         try {
             $channel = ChannelFactory::create($this);
             $channel->run();
             $this->storeData($channel->getData());
             if ($this->CID == $old_CID && !$is_called_common && !$this->is_ajax) {
                 $is_called_common = 1;
                 $this->CID = "common";
             }
         } catch (CHException $e) {
             if ($this->is_ajax) {
                 $this->data["error"] = "[" . $e->getType() . "] " . $e->getMessage();
             } else {
                 $this->setError($e->getMessage(), $e->getType());
             }
         }
     }
     if ($this->is_ajax) {
         $this->buildAjaxOut();
     } else {
         $this->buildOut();
     }
 }
 /**
  * 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'));
 }
Esempio n. 5
0
 /**
  * sendPost
  *
  * @param   string  &$post   Param
  * @param   int     $userid  Param
  *
  * @return	boolean
  */
 protected function sendPost(&$post, $userid = null)
 {
     $this->logger->log(JLog::INFO, 'sendPost: postid = ' . $post->id);
     // Post for specific channel
     $channel = ChannelFactory::getInstance()->getChannel($post->channel_id);
     if (!$channel) {
         PostHelper::savePost(AutotweetPostHelper::POST_ERROR, 'COM_AUTOTWEET_ERR_CHANNEL_NOTFOUND', $post, $userid);
         return false;
     }
     $channel_id = $channel->getChannelId();
     $response = $this->sharePost($channel, $post, $userid);
     // Store message in log
     return PostHelper::savePost($response['state'], $response['result_msg'], $post, $userid);
 }
 /**
  * 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);
             }
         }
     }
 }
Esempio n. 7
0
 /**
  * fillUniversalRules.
  *
  * @param   array   &$rules  Params
  * @param   string  $author  Params
  *
  * @return	void
  */
 protected function fillUniversalRules(&$rules, $author)
 {
     $new_rules = array();
     $channels = ChannelFactory::getInstance()->getChannels($author);
     $logger = AutotweetLogger::getInstance();
     $channels_ids = array_keys($channels);
     $logger->log(JLog::INFO, 'fillUniversalRules author=' . $author . ' rules n=' . count($rules) . ' channels:', $channels_ids);
     foreach ($rules as $rule) {
         $logger->log(JLog::INFO, 'Universal rule id (' . $rule->id . ', ' . $rule->channel_id . ')');
         // Universal rule
         if (empty($rule->channel_id)) {
             foreach ($channels as $channel) {
                 $new_rule = clone $rule;
                 $new_rule->channel_id = $channel->getField('id');
                 $new_rules[] = $new_rule;
                 $logger->log(JLog::INFO, 'Generating rule for channelId: ' . $new_rule->channel_id);
             }
         } else {
             $new_rules[] = $rule;
         }
     }
     $rules = $new_rules;
 }