/**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $districts = ['Quận 1', 'Quận 2', 'Quận 3', 'Quận 4', 'Quận 5', 'Quận 6', 'Quận 7', 'Quận 8', 'Quận 9', 'Quận Thủ Đức', 'Quận Bình Thạnh', 'Quận Phú Nhuận', 'Quận Tân Phú', 'Quận Tân Bình', 'Huyện Hóc Môn', 'Huyện Bình Chánh', 'Huyện Nhà Bè', 'Huyện Củ Chi', 'Huyện Cần Giờ'];
     $provinces = Province::where('is_publish', 1)->orderBy('priority')->take(1)->get();
     $province_id = 1;
     if (is_null($provinces) || $provinces->count() == 0) {
         $province_id = $provinces[0]->id;
     }
     foreach ($districts as $key => $value) {
         $district = district::create(['key' => Common::createKeyURL($value), 'province_id' => $province_id, 'priority' => $key, 'is_publish' => 1, 'created_by' => 'vankhoe', 'updated_by' => 'vankhoe']);
         DistrictTranslation::create(['district_id' => $district->id, 'locale' => 'vi', 'name' => $value, 'meta_description' => 'Bán căn hộ ' . $value . ', sang nhượng căn hộ ' . $value . ', cho thuê căn hộ ' . $value, 'meta_keywords' => 'Bán căn hộ ' . $value . ', sang nhượng căn hộ ' . $value . ', cho thuê căn hộ ' . $value]);
     }
 }
 public function generate()
 {
     $bata_buffer = "";
     $year = $this->input->post('year_from');
     $epiweek = $this->input->post('epiweek_to');
     $province = $this->input->post('province');
     $district = $this->input->post('district');
     $display_type = $this->input->post('display_type');
     $weekending = Surveillance::getWeekEnding($year, $epiweek);
     $provinces = array();
     $districts = array();
     //Check if a province has been specified
     if ($province > 0) {
         //if so, retrieve it's details from the database
         $provinces = Province::getProvince($province);
     } else {
         //if not, retrieve all provinces
         $provinces = Province::getAll();
     }
     //Check if a district has been specified
     if ($district > 0) {
         //if so, retrieve it's details from the database
         $districts = District::getDistrict($district);
         //also, retrieve the province details for this district
         $provinces = Province::getProvince($districts[0]['Province']);
     } else {
         //if not, empty the array
         $districts = array();
     }
     //Start displaying the header of the table
     $bata_buffer .= " <table class='data-table'>\n            <tr style='background: #F5D2AE;'>\n                <th rowspan=2>Province</th>\n                <th rowspan=2>District</th>\n                <th rowspan=2>Reports Expected</th>\n                <th rowspan=2>Reports Received</th>\n                <th rowspan=2>%RR</th>";
     $diseases["reports"] = "reports";
     $diseases["submitted"] = "submitted";
     $diseases["percentage"] = "percentage";
     $disease_array = Disease::getAll();
     foreach ($disease_array as $disease) {
         if ($disease['Name'] == 'Malaria') {
             $diseases[$disease['id']] = $disease['Name'];
             $diseases["tested"] = "tested";
             $diseases["positive"] = "positive";
             $bata_buffer .= "<th rowspan=2>" . $disease['Name'] . "</th>";
             $bata_buffer .= "<th  colspan=2 style='color:green;'>" . $disease['Name'] . " Indicators</th>";
         } else {
             $diseases[$disease['id']] = $disease['Name'];
             $bata_buffer .= "<th rowspan=2>" . $disease['Name'] . "</th>";
         }
     }
     //Finish Displaying the Header
     $bata_buffer .= " </tr>\n            <tr style='background: #F5D2AE'>\n                 <th >Tested</th><th >Positive</th>\n            </tr>\n\t\t";
     //Start retrieving all the rows for the data
     foreach ($provinces as $province_object) {
         $bata_buffer .= "<tr class='even'><td style='font-weight:bold; font-size:14px'>" . $province_object->Name . "</td></tr>";
         $province_districts = array();
         //check if a district was specified
         if (count($districts) > 0) {
             $province_districts = $districts;
         } else {
             //Get all the districts for this province
             $province_districts = district::getProvinceDistrict($province_object->id);
         }
         //loop through all the districts to get their data
         foreach ($province_districts as $province_district) {
             $available_data = array();
             $surveillance_counter = 2;
             $bata_buffer .= "<tr class='even' style='background:#C4E8B7'><td></td><td>" . $province_district['Name'] . "</td>";
             $surveillance_data = Surveillance::getWeeklySummaries($year, $epiweek, $province_district['id']);
             //Check if any surveillance data exists
             if (isset($surveillance_data[0])) {
                 $available_data['reports'] = $surveillance_data[0]['Expected'];
                 $available_data['submitted'] = $surveillance_data[0]['Submitted'];
                 //Calculate the reporting
                 $available_data['percentage'] = floor($available_data['submitted'] / $available_data['reports'] * 100);
                 //Display these Parameters
                 $bata_buffer .= "<td>" . $available_data['reports'] . "</td><td>" . $available_data['submitted'] . "</td><td>" . $available_data['percentage'] . "</td>";
             } else {
                 $bata_buffer .= "<td>DNR</td><td>DNR</td><td>0</td>";
             }
             //Check if there is any surveillance data
             if (isset($surveillance_data[0])) {
                 //Loop through all the surveillance data returned
                 foreach ($surveillance_data as $disease_data) {
                     $bata_buffer .= "<td>" . $disease_data['Cases'] . "(" . $disease_data['Deaths'] . ")</td>";
                     //Check if the disease is malaria and if so, get the lab data and display it
                     if ($disease_data['Disease'] == 1) {
                         //Get malaria data
                         $lab_weekly_data = Lab_Weekly::getWeeklyLabData($year, $epiweek, $province_district['id']);
                         //Check if any data exists
                         if (isset($lab_weekly_data)) {
                             $bata_buffer .= "<td>" . $lab_weekly_data['Tested'] . "</td><td>" . $lab_weekly_data['Positive'] . "</td>";
                         } else {
                             $bata_buffer .= "<td>DNR</td><td>DNR</td>";
                         }
                     }
                 }
             } else {
                 $total_diseases = count($disease_array);
                 $total_elements = $total_diseases + 1;
                 for ($x = 0; $x <= $total_elements; $x++) {
                     $bata_buffer .= "<td>DNR</td>";
                 }
             }
             //Marks the end of data for one district
             $bata_buffer .= "</tr>";
         }
         //End districts loop
     }
     //End provinces loop
     //Finish the table
     $bata_buffer .= "</table>";
     //Start section that shows cumulative data
     $bata_buffer .= "<table class='data-table'>\n            <tr style='background: #F5D2AE;'>\n                <th rowspan='2' colspan='5'>Cumulative Summaries</th>";
     //Loop through all the diseases to display their names
     foreach ($disease_array as $disease) {
         if ($disease['Name'] == 'Malaria') {
             $diseases[$disease['id']] = $disease['Name'];
             $diseases["tested"] = "tested";
             $diseases["positive"] = "positive";
             $bata_buffer .= "<th rowspan=2>" . $disease['Name'] . "</th>";
             $bata_buffer .= "<th  colspan=2 style='color:green;'>" . $disease['Name'] . " Indicators</th>";
         } else {
             $diseases[$disease['id']] = $disease['Name'];
             $bata_buffer .= "<th rowspan=2>" . $disease['Name'] . "</th>";
         }
     }
     //end diseases loop
     $bata_buffer .= "</tr>\n            <tr style='background: #F5D2AE'>\n                <th >Tested</th><th >Positive</th>\n            </tr>";
     //Get the malaria lab data summaries
     $lab_weekly_summary = Lab_Weekly::getWeeklyLabSummaries($year, $epiweek);
     //Start Displaying this week summary
     $bata_buffer .= "<tr class='even'><td rowspan='2' colspan='5'>Week " . $epiweek . " Summary</td>";
     //Get the summary for the week. Disease cases vs. deaths
     $disease_deaths = array();
     foreach ($disease_array as $disease_object) {
         $disease_summaries = Surveillance::getWeeklyDiseaseSummaries($year, $epiweek, $disease_object['id']);
         $bata_buffer .= "<td>" . $disease_summaries['Cases'] . "</td>";
         $disease_deaths[$disease_object['id']] = $disease_summaries['Deaths'];
         //check if the disease is Malaria. If so, display lab data
         if ($disease_object['Name'] == "Malaria") {
             $bata_buffer .= "<td rowspan=2>" . $lab_weekly_summary['Tested'] . "</td>";
             $bata_buffer .= "<td rowspan=2>" . $lab_weekly_summary['Positive'] . "</td>";
         }
     }
     //Finish the cases row
     $bata_buffer .= "</tr>";
     //Start the deaths row
     $bata_buffer .= "<tr>";
     //Loop through one more time to display the total number of deaths
     foreach ($disease_array as $disease_object) {
         $bata_buffer .= "<td>(" . $disease_deaths[$disease_object['id']] . ")</td>";
     }
     //finish the deaths row
     $bata_buffer .= "</tr>";
     //Get the annual summary
     //Get the malaria lab data summaries
     $lab_weekly_summary = Lab_Weekly::getAnnualLabSummaries($year);
     //Start Displaying this week summary
     $bata_buffer .= "<tr class='even' style='background:#BB00FF'><td rowspan='2' colspan='5'>Years Cummulative Summary</td>";
     //Get the summary for the week. Disease cases vs. deaths
     $disease_deaths = array();
     foreach ($disease_array as $disease_object) {
         $disease_summaries = Surveillance::getAnnualDiseaseSummaries($year, $disease_object['id']);
         $bata_buffer .= "<td>" . $disease_summaries['Cases'] . "</td>";
         $disease_deaths[$disease_object['id']] = $disease_summaries['Deaths'];
         //check if the disease is Malaria. If so, display lab data
         if ($disease_object['Name'] == "Malaria") {
             $bata_buffer .= "<td rowspan=2>" . $lab_weekly_summary['Tested'] . "</td>";
             $bata_buffer .= "<td rowspan=2>" . $lab_weekly_summary['Positive'] . "</td>";
         }
     }
     //Finish the cases row
     $bata_buffer .= "</tr>";
     //Start the deaths row
     $bata_buffer .= "<tr class='even' style='background:#BB00FF'>";
     //Loop through one more time to display the total number of deaths
     foreach ($disease_array as $disease_object) {
         $bata_buffer .= "<td>(" . $disease_deaths[$disease_object['id']] . ")</td>";
     }
     //finish the deaths row
     $bata_buffer .= "</tr></table>";
     $this->generatePDF($year, $bata_buffer, $epiweek, $weekending, $display_type);
 }