/**
  * a function to export data to excel
  */
 public function excelDownload()
 {
     if (isset($_POST['records'])) {
         /** Include PHPExcel */
         require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
         // Create new PHPExcel object
         $objPHPExcel = new PHPExcel();
         $title = Input::get("vertical");
         $pat = false;
         $usequery = "";
         if (Input::get("horizontal") == "Year") {
             $from = Input::get('year') . "-01-01";
             $to = Input::get('year') . "-12-31";
             $patientquery = DB::table('patient');
             $visitquery = DB::table('visit');
             $query = $this->processQuery($patientquery, $visitquery);
             $title .= " " . $query[2] . " " . Input::get('Year');
             $usequery = $query[0]->whereBetween('created_at', array($from, $to))->get();
         } elseif (Input::get("horizontal") == "Years") {
             $from = Input::get('start') . "-01-01";
             $to = Input::get('end') . "-12-31";
             $patientquery = DB::table('patient');
             $visitquery = DB::table('visit');
             $query = $this->processQuery($patientquery, $visitquery);
             $title .= " " . $query[2] . " " . Input::get('Year');
             $usequery = $query[0]->whereBetween('created_at', array($from, $to))->get();
         } elseif (Input::get("horizontal") == "Age Range") {
             $patientquery = DB::table('patient');
             $visitquery = DB::table('visit');
             $query = $this->processQuery($patientquery, $visitquery);
             $usequery = $query[0]->get();
             $title .= " Age Range " . $query[2] . " ";
         }
         // Set document properties
         $objPHPExcel->getProperties()->setCreator("Cervical Cancer Prevention Program")->setLastModifiedBy(Auth::user()->first_name)->setTitle($title)->setSubject($title)->setDescription("Cervical Cancer Prevention Program Reports")->setKeywords("cancer cecap openxml php")->setCategory("Result file");
         // Tittle
         $objPHPExcel->setActiveSheetIndex(0)->mergeCells('A1:I1');
         $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $title);
         $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
         $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A2', 'Name')->setCellValue('B2', 'Age')->setCellValue('C2', 'Region')->setCellValue('D2', 'District')->setCellValue('E2', 'Facility')->setCellValue('F2', 'Marital Status')->setCellValue('G2', 'HIV Status')->setCellValue('H2', 'Contraceptive Status')->setCellValue('I2', 'Last Visit');
         $k = 3;
         foreach ($usequery as $us) {
             $patient = Patient::find($us->id);
             $objPHPExcel->setActiveSheetIndex(0)->setCellValue("A{$k}", $us->first_name . " " . $us->middle_name . " " . $us->last_name)->setCellValue("B{$k}", date('Y') - date('Y', strtotime($us->birth_date)))->setCellValue("C{$k}", $patient->report->regions->region)->setCellValue("D{$k}", $patient->report->districts->district)->setCellValue("E{$k}", Facility::find($us->facility_id)->facility_name)->setCellValue("F{$k}", $patient->report->marital_status)->setCellValue("G{$k}", $patient->report->HIV_status)->setCellValue("H{$k}", $patient->report->contraceptive_status)->setCellValue("I{$k}", date('j M Y', strtotime($patient->visit()->orderBy('created_at', 'DESC')->first()->visit_date)));
             $k++;
         }
         // Rename worksheet
         $objPHPExcel->getActiveSheet()->setTitle("Cervical Cancer Patient Report");
         // Set active sheet index to the first sheet, so Excel opens this as the first sheet
         $objPHPExcel->setActiveSheetIndex(0);
         // Redirect output to a client’s web browser (Excel2007)
         header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
         header('Content-Disposition: attachment;filename="records.xlsx"');
         header('Cache-Control: max-age=0');
         // If you're serving to IE 9, then the following may be needed
         header('Cache-Control: max-age=1');
         // If you're serving to IE over SSL, then the following may be needed
         header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
         // Date in the past
         header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
         // always modified
         header('Cache-Control: cache, must-revalidate');
         // HTTP/1.1
         header('Pragma: public');
         // HTTP/1.0
         $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
         $objWriter->save('php://output');
         exit;
     } elseif (isset($_POST['reports'])) {
         /** Include PHPExcel */
         require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
         // Create new PHPExcel object
         $objPHPExcel = new PHPExcel();
         $title = Input::get("vertical");
         $pat = false;
         $row = array();
         $column = array();
         $columntype = $this->generateArray(Input::get("show"));
         if (Input::get("vertical") == "Patients") {
             $pat = true;
         }
         if (Input::get("horizontal") == "Year") {
             $row = array("01" => "jan", "02" => "feb", "03" => "mar", "04" => "apr", "05" => "may", "06" => "jun", "07" => "jul", "08" => "aug", "09" => "sep", "10" => "oct", "11" => "nov", "12" => "dec");
             foreach ($row as $key => $value) {
                 $from = Input::get('year') . "-" . $key . "-01";
                 $to = Input::get('year') . "-" . $key . "-31";
                 if (isset($columntype)) {
                     foreach ($columntype as $key1 => $value1) {
                         $patientquery = DB::table('patient');
                         $visitquery = DB::table('visit');
                         $query = $this->processQuery($patientquery, $visitquery);
                         $que = $this->checkCondition($query, $pat, $key1)->whereBetween('created_at', array($from, $to));
                         $column[$value1][] = $que->count();
                     }
                 }
             }
             $title .= " " . $query[2] . " " . Input::get('year');
         } elseif (Input::get("horizontal") == "Years") {
             $row = range(Input::get('start'), Input::get('end'));
             foreach ($row as $value) {
                 $from = $value . "-01-01";
                 $to = $value . "-12-31";
                 if (isset($columntype)) {
                     foreach ($columntype as $key1 => $value1) {
                         $patientquery = DB::table('patient');
                         $visitquery = DB::table('visit');
                         $query = $this->processQuery($patientquery, $visitquery);
                         $que = $this->checkCondition($query, $pat, $key1)->whereBetween('created_at', array($from, $to));
                         $column[$value1][] = $que->count();
                     }
                 }
             }
             $title .= " " . $query[2] . " " . Input::get('start') . " - " . Input::get('end');
         } elseif (Input::get("horizontal") == "Age Range") {
             //setting the limits
             $agetouse = Input::get('age') == 0 ? 3 : Input::get('age');
             if (parent::maxAge() % $agetouse == 0) {
                 $limit = parent::maxAge();
             } else {
                 $limit = parent::maxAge() - parent::maxAge() % $agetouse + $agetouse;
             }
             //making a loop for values
             //year iterator
             $k = 0;
             //getting age
             $range = Input::get('age');
             $yeardate = date("Y") + 1;
             $yaerdate1 = $yeardate . "-01-01";
             //creating title
             $data = array();
             for ($i = $range; $i <= $limit; $i += $range) {
                 $row[] = $k . " - " . $i;
                 //start year
                 $time = $k * 365 * 24 * 3600;
                 $today = date("Y-m-d");
                 $timerange = strtotime($today) - $time;
                 $start = date("Y", $timerange) + 1 . "-01-01";
                 //end year
                 $time1 = $i * 365 * 24 * 3600;
                 $timerange1 = strtotime($today) - $time1;
                 $end = date("Y", $timerange1) . "-01-01";
                 if (isset($columntype)) {
                     foreach ($columntype as $key1 => $value1) {
                         $patientquery = DB::table('patient');
                         $visitquery = DB::table('visit');
                         $query = $this->processQuery($patientquery, $visitquery);
                         $que = $this->checkCondition($query, true, $key1)->whereBetween('birth_date', array($end, $start));
                         $column[$value1][] = $que->count();
                     }
                 }
                 $k = $i;
             }
             $title .= " Age Range " . $query[2];
         }
         // Set document properties
         $objPHPExcel->getProperties()->setCreator("Cervical Cancer Prevention Program")->setLastModifiedBy(Auth::user()->first_name)->setTitle($title)->setSubject($title)->setDescription("Cervical Cancer Prevention Program Reports")->setKeywords("cancer cecap openxml php")->setCategory("Result file");
         $latterArr = array("A", "B", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP", "AQ", "AR", "AS", "AT", "AU", "AV", "AW", "AX", "AY", "AZ", "BA", "BB", "BC", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BK", "BM", "BL", "BO", "BP", "BQ", "BR", "BS", "BT", "BU", "BV", "BW", "BX", "BY", "BZ");
         $ttlecont = 1;
         $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A2', Input::get("show"));
         foreach ($row as $header) {
             $objPHPExcel->setActiveSheetIndex(0)->setCellValue("{$latterArr[$ttlecont]}2", $header);
             $ttlecont++;
         }
         $objPHPExcel->setActiveSheetIndex(0)->mergeCells("A1:{$latterArr[$ttlecont - 1]}1");
         $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $title);
         $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
         $k = 3;
         $colcount = 1;
         foreach ($column as $keys => $cols) {
             $objPHPExcel->setActiveSheetIndex(0)->setCellValue("A{$k}", $keys);
             foreach ($cols as $colsval) {
                 $objPHPExcel->setActiveSheetIndex(0)->setCellValue("{$latterArr[$colcount]}{$k}", $colsval);
                 $colcount++;
             }
             $colcount = 1;
             $k++;
         }
         // Rename worksheet
         $objPHPExcel->getActiveSheet()->setTitle("Cervical Cancer Patient Report");
         // Set active sheet index to the first sheet, so Excel opens this as the first sheet
         $objPHPExcel->setActiveSheetIndex(0);
         // Redirect output to a client’s web browser (Excel2007)
         header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
         header('Content-Disposition: attachment;filename="' . $title . '.xlsx"');
         header('Cache-Control: max-age=0');
         // If you're serving to IE 9, then the following may be needed
         header('Cache-Control: max-age=1');
         // If you're serving to IE over SSL, then the following may be needed
         header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
         // Date in the past
         header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
         // always modified
         header('Cache-Control: cache, must-revalidate');
         // HTTP/1.1
         header('Pragma: public');
         // HTTP/1.0
         $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
         $objWriter->save('php://output');
         exit;
     }
 }
 public function testStoreDelete()
 {
     $this->action('GET', 'FacilityController@delete', array('id' => 1));
     $facilityDeleted = Facility::find(1);
     $this->assertEquals(null, $facilityDeleted);
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function delete($id)
 {
     //Deleting the Item
     $facility = Facility::find($id);
     //Soft delete
     $facility->delete();
     // redirect
     $url = Session::get('SOURCE_URL');
     return Redirect::to($url)->with('message', trans('messages.successfully-deleted-facility'));
 }
 public function deleteAction()
 {
     if (!$this->hasACL('edit_people')) {
         $this->doNoAccessError();
     }
     $status = ValidationContainer::instance();
     $id = $this->getSanParam('id');
     if ($id and !Facility::isReferenced($id)) {
         $fac = new Facility();
         $rows = $fac->find($id);
         $row = $rows->current();
         if ($row) {
             $fac->delete('id = ' . $row->id);
         }
         $status->setStatusMessage(t('That facility was deleted.'));
     } else {
         if (!$id) {
             $status->setStatusMessage(t('That facility could not be found.'));
         } else {
             $status->setStatusMessage(t('That facility is in use and could not be deleted.'));
         }
     }
     //validate
     $this->view->assign('status', $status);
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function destroy($id)
 {
     $facility = Facility::find($id);
     $name = $facility->facility_name;
     $facility->delete();
     Logs::create(array("user_id" => Auth::user()->id, "action" => "Delete facility named " . $name));
 }