public function book() { //if we don't have a hotel OR we moved to another hotel if ($this->getCurrent() == null || $this->getCurrent()->hotel->id != $this->hotel->getId()) { //if we don't have a hotel AND we moved to another hotel if ($this->getCurrent() != null and $this->getCurrent()->hotel->id != $this->hotel->getId()) { Yii::trace('Trying to restore hotelBooker from db', 'HotelBookerComponent.book'); $this->hotelBooker = HotelBooker::model()->findByAttributes(array('hotelResultKey' => $this->hotel->getId())); if ($this->hotelBooker) { $this->hotelBooker->setHotelBookerComponent($this); Yii::trace('Done', 'HotelBookerComponent.book'); } else { Yii::trace('No such record', 'HotelBookerComponent.book'); } } if ($this->hotelBooker == null) { Yii::trace('New hotelBooker to db', 'HotelBookerComponent.book'); $this->hotelBooker = new HotelBooker(); $this->hotelBooker->hotelResultKey = $this->hotel->getId(); $this->hotelBooker->hotel = $this->hotel; $this->hotelBooker->status = 'enterCredentials'; $this->hotelBooker->setHotelBookerComponent($this); $this->hotelBooker->save(); } } // Yii::trace(CVarDumper::dumpAsString($this->hotelBooker->getErrors()), 'HotelBookerComponent.book'); if (!$this->hotelBooker->id) { $this->hotelBooker->id = $this->hotelBooker->primaryKey; } Yii::app()->user->setState('hotelResultKey', $this->hotelBooker->hotel->id); }
public function getGroupId() { if ($this->hotel) { return $this->hotel->getId(); } if (!$this->groupId) { $this->groupId = uniqid(); } return $this->groupId; }
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')); } }