public function add_hotel()
 {
     if (!$this->securitypolicy->validateAccessRight(2, 'add')) {
         $this->load->view('access_denied');
     }
     $this->_setValidation();
     $this->_viewData['portfolio']['default'] = $this->input->post('hotel-portfolio');
     if ($this->form_validation->run() == false || $this->_isTagOpr) {
         if (!$this->_isTagOpr) {
             $this->session->set_userdata(array('status' => 'error'));
         }
         $this->_viewData['portfolio']['dropdown'] = $this->_portfolio->getPortfolioAsDropdown();
         $this->load->view('hotel_mngt_add_hotel', $this->_viewData);
     } else {
         if ($this->Do_hotel->isRemoved($this->input->post('hotel-name'))) {
             # find id
             $nameFilter = new HtlNameFiltr();
             $nameFilter->setFilter($this->input->post('hotel-name'));
             $this->_hotel->addFilter($nameFilter);
             $this->_hotel->set();
             $this->_hotel->purgeTags();
             $this->_hotel->setIsRemoved('0');
             $this->session->set_userdata(array('action' => 'edit_hotel'));
             $this->edit_hotel();
         } else {
             $this->_hotel->setByInput();
             $this->_hotel->save();
             if (!empty($this->_hotel->errors)) {
                 $data['portfolio']['dropdown'] = $this->_portfolio->getPortfolioAsDropdown();
                 $this->session->set_userdata(array('status' => 'error'));
                 $this->load->view('hotel_mngt_add_hotel', $data);
             } else {
                 $this->_postAddUpdateHotel();
             }
         }
     }
 }
 public function add_data_csv()
 {
     if (!$this->securitypolicy->validateAccessRight(1, 'add')) {
         $this->load->view('access_denied');
     }
     $this->load->library('form_validation');
     $this->load->helper('form');
     $lineno = array();
     $hotelIds = array();
     $hotelNames = array();
     $hotelAmounts = array();
     $hotelDates = array();
     if (array_key_exists('revenue-csv', $_FILES)) {
         $this->form_validation->set_rules('category', 'Category', 'required');
         if ($this->form_validation->run() == false || $_FILES['revenue-csv']['error'] === 4) {
             $this->_setViewDataErrCsv();
             $this->_getLatestActivity();
             $this->load->view('add_financial_info_csverr', $this->_viewdata);
         } else {
             $this->load->library('Csvimport');
             $config['upload_path'] = './uploads/csv';
             $config['allowed_types'] = 'csv';
             $config['overwrite'] = true;
             $this->load->library('upload', $config);
             if ($this->upload->do_upload('revenue-csv')) {
                 $filedata = $this->upload->data();
                 $path = $config['upload_path'] . '/' . $filedata['file_name'];
                 $this->csvimport->column_headers(array('hotel id', 'hotel name', 'amount', 'date'));
                 if ($this->csvimport->get_array($path)) {
                     $csv_array = $this->csvimport->get_array($path);
                     foreach ($csv_array as $line => $row) {
                         $lineno[] = $line + 2;
                         $hotelIds[] = $row['hotel id'];
                         $hotelNames[] = $row['hotel name'];
                         $hotelAmounts[] = $row['amount'];
                         $hotelDates[] = $row['date'];
                     }
                 }
             } else {
                 $this->_setViewDataErrCsv();
                 $this->_getLatestActivity();
                 $this->load->view('add_financial_info_csverr', $this->_viewdata);
             }
         }
     } else {
         $lineno = $this->input->post('lineno[]');
         $hotelIds = $this->input->post('hotel-id[]');
         $hotelNames = $this->input->post('hotel-name[]');
         $hotelAmounts = $this->input->post('amount[]');
         $hotelDates = $this->input->post('date[]');
     }
     # revenue data collection
     $revenues = new RevenueColl();
     //valid revenue data
     $revenuesErrIdNameEmpty = new RevenueColl();
     //id and name empty
     $revenuesErrIdMatch = new RevenueColl();
     //id not match name
     $revenuesErrNameMatch = new RevenueColl();
     //name not match id
     $revenuesErrIdNotFound = new RevenueColl();
     //id not found
     $revenuesErrAmount = new RevenueColl();
     //amount error
     $revenuesErrDate = new RevenueColl();
     //date error
     $revenuesErrMulti = new RevenueColl();
     //more than input error
     # hotel name collection
     $hotelNamesErrIdMatch = array();
     // hotel names for id not match
     $hotelNamesErrMatch = array();
     // hotel names
     # line number collections
     $linenoErrIdNameEmpty = array();
     $linenoErrIdMatch = array();
     $linenoErrNameMatch = array();
     $linenoErrDate = array();
     $linenoErrAmount = array();
     $linenoErrMulti = array();
     $linenoErrIdNotFound = array();
     $hotels = new HotelColl();
     $hotels->setAll();
     foreach ($hotelIds as $index => $hotelId) {
         $errors = array();
         $isIdMatch = false;
         $isNameMatch = false;
         $revenue = new RevenueData();
         $revenue->setAmount(str_replace(',', '', $hotelAmounts[$index]));
         $revenue->setAcctId($this->input->post('category'));
         $revenue->setDateEffect($hotelDates[$index]);
         foreach ($hotels as $hotel) {
             if ($hotel->getId() == $hotelId) {
                 $isIdMatch = true;
             }
             if ($hotel->getName() == $hotelNames[$index]) {
                 $isNameMatch = true;
             }
             if ($isIdMatch || $isNameMatch) {
                 break;
             }
         }
         if (!$isIdMatch && !$isNameMatch) {
             $hotel = new Hotel();
             # saving new hotel
             if (!empty($hotelNames[$index]) && !empty($hotelId)) {
                 if (!empty($hotelId)) {
                     $hotel->setId($hotelId);
                 }
                 $hotel->setName($hotelNames[$index]);
                 $hotel->save();
             } else {
                 if (empty($hotelId) && empty($hotelNames[$index])) {
                     $errors[] = 1;
                 } else {
                     if (empty($hotelNames[$index]) && !empty($hotelId)) {
                         $errors[] = 6;
                         $revenue->setHotelId($hotelId);
                     } else {
                         $errors[] = 2;
                     }
                 }
             }
         } elseif ($isIdMatch || $isNameMatch) {
             $hotel = new Hotel();
             # id - name match validation
             if (!$isIdMatch) {
                 $errors[] = 2;
                 $hotelNamesErrIdMatch[] = $hotelNames[$index];
                 # id not match
                 if (!empty($hotelId)) {
                     $hotel->setId($hotelId);
                 } else {
                     $nameFilter = new HtlNameFiltr();
                     $nameFilter->setFilter($hotelNames[$index]);
                     $hotel->addFilter($nameFilter);
                     $hotel->set();
                     $hotel->setId($hotelId);
                 }
             } elseif (!$isNameMatch) {
                 # name not match
                 $hotel->setById($hotelId);
                 if (!empty($hotelNames[$index])) {
                     $errors[] = 3;
                     $hotelNamesErrMatch[] = $hotelNames[$index];
                 }
             } else {
                 $hotel->setById($hotelId);
             }
             $revenue->setHotelId($hotel->getId());
         }
         # validate amount
         if (empty($hotelAmounts[$index]) || !is_numeric($hotelAmounts[$index])) {
             $errors[] = 4;
         }
         # validate dates
         if (empty($hotelDates[$index]) || !validateDate($hotelDates[$index])) {
             $errors[] = 5;
         }
         if (count($errors) == 1) {
             if (in_array(1, $errors)) {
                 $revenuesErrIdNameEmpty->addRevenue($revenue);
                 $linenoErrIdNameEmpty[] = $lineno[$index];
             } elseif (in_array(2, $errors)) {
                 $revenuesErrIdMatch->addRevenue($revenue);
                 $linenoErrIdMatch[] = $lineno[$index];
             } elseif (in_array(3, $errors)) {
                 $revenuesErrNameMatch->addRevenue($revenue);
                 $linenoErrNameMatch[] = $lineno[$index];
             } elseif (in_array(4, $errors)) {
                 $revenuesErrAmount->addRevenue($revenue);
                 $linenoErrAmount[] = $lineno[$index];
             } elseif (in_array(5, $errors)) {
                 $revenuesErrDate->addRevenue($revenue);
                 $linenoErrDate[] = $lineno[$index];
             } elseif (in_array(6, $errors)) {
                 $revenuesErrIdNotFound->addRevenue($revenue);
                 $linenoErrIdNotFound[] = $lineno[$index];
             }
         } elseif (count($errors) > 1) {
             $revenuesErrMulti->addRevenue($revenue);
             $linenoErrMulti[] = $lineno[$index];
         } elseif (empty($errors)) {
             # add to valid revenue data
             $revenues->addRevenue($revenue);
         }
     }
     # End foreach hotelI$revenuesErrDate->count > 0
     if ($revenues->count() > 0) {
         $revenues->save();
     }
     if ($revenuesErrIdNameEmpty->count() > 0 || $revenuesErrIdMatch->count() > 0 || $revenuesErrNameMatch->count() > 0 || $revenuesErrAmount->count() > 0 || $revenuesErrDate->count() > 0 || $revenuesErrMulti->count() > 0 || $revenuesErrIdNotFound->count() > 0) {
         $this->_viewdata['revenuesErrIdNameEmpty'] = $revenuesErrIdNameEmpty;
         $this->_viewdata['revenuesErrIdMatch'] = $revenuesErrIdMatch;
         $this->_viewdata['revenuesErrNameMatch'] = $revenuesErrNameMatch;
         $this->_viewdata['revenuesErrAmount'] = $revenuesErrAmount;
         $this->_viewdata['revenuesErrDate'] = $revenuesErrDate;
         $this->_viewdata['revenuesErrMulti'] = $revenuesErrMulti;
         $this->_viewdata['revenuesErrIdNotFound'] = $revenuesErrIdNotFound;
         $this->_viewdata['hotelNamesErrMatch'] = $hotelNamesErrMatch;
         $this->_viewdata['hotelNamesErrIdMatch'] = $hotelNamesErrIdMatch;
         $this->_viewdata['linenoErrIdNameEmpty'] = $linenoErrIdNameEmpty;
         $this->_viewdata['linenoErrIdMatch'] = $linenoErrIdMatch;
         $this->_viewdata['linenoErrNameMatch'] = $linenoErrNameMatch;
         $this->_viewdata['linenoErrAmount'] = $linenoErrAmount;
         $this->_viewdata['linenoErrDate'] = $linenoErrDate;
         $this->_viewdata['linenoErrMulti'] = $linenoErrMulti;
         $this->_viewdata['linenoErrIdNotFound'] = $linenoErrIdNotFound;
         $this->_viewdata['category'] = $this->input->post('category');
         $this->_viewdata['hotels'] = $hotels;
         $this->_viewdata['showmsg'] = true;
         $this->_viewdata['status'] = 'error';
         $this->_viewdata['msg'] = 'Some records you tried to import had wrong or missing values. Please correct these below and try again.';
         $this->load->view('add_financial_info_csverr', $this->_viewdata);
     } else {
         $this->session->set_userdata(array('status' => 'success'));
         redirect(site_url('Admin/add_revenue_data_csv'));
     }
 }
 protected function _filterHotels(HotelColl $hotels)
 {
     if ($this->session->has_userdata('filter')) {
         foreach ($this->session->userdata('filter') as $filterGroup => $filters) {
             if ($filterGroup == 'hotel-name') {
                 foreach ($filters as $hotelName) {
                     $htlNameFiltr = new HtlNameFiltr();
                     $htlNameFiltr->setFilter($hotelName);
                     $htlNameFiltr->setFilterGrp('hotel-name');
                     $hotels->addFilter($htlNameFiltr);
                 }
             }
             if ($filterGroup == 'hotel-portfolio') {
                 $this->load->model('Do_portfolio');
                 $htlPrtfoFiltr = new HtlPrtfoFiltr();
                 foreach ($filters as $portfolioName) {
                     # get equivalent id
                     $portfolio = $this->Do_portfolio->getByName($portfolioName);
                     $portfolioId = empty($portfolio) ? 0 : $portfolio->id;
                     $htlPrtfoFiltr->setFilterGrp('hotel-portfolio');
                     $htlPrtfoFiltr->setFilter($portfolioId);
                 }
                 $hotels->addFilter($htlPrtfoFiltr);
             }
             if ($filterGroup == 'hotel-country') {
                 foreach ($filters as $tagVal) {
                     $htlTagFiltr = new HtlTagFiltr();
                     $htlTagFiltr->setFilter($tagVal);
                     $htlTagFiltr->setFilterGrp('hotel-country');
                     $hotels->addFilter($htlTagFiltr);
                 }
             }
             if ($filterGroup == 'hotel-coach') {
                 foreach ($filters as $tagVal) {
                     $htlTagFiltr = new HtlTagFiltr();
                     $htlTagFiltr->setFilter($tagVal);
                     $htlTagFiltr->setFilterGrp('hotel-coach');
                     $hotels->addFilter($htlTagFiltr);
                 }
             }
         }
     }
     return $hotels;
 }