public function send_emails() { $this->load->helper(array('file')); $config['protocol'] = 'smtp'; $config['smtp_host'] = 'ssl://smtp.googlemail.com'; $config['smtp_port'] = 465; $config['smtp_user'] = stripslashes('*****@*****.**'); $config['smtp_pass'] = stripslashes('projectDVI'); $this->load->library('email', $config); $this->load->database(); $period = 'Jun-12'; //First, retrieve all the districts that have recipients $sql = "select distinct district, d.name as district_name from stock_out_recipient s left join districts d on district = d.id where s.disabled = '0'"; $query = $this->db->query($sql); $district_array = $query->result_array(); //get the total number of vaccines that report dhis data $vaccine_number = Vaccines::getTotalDHIS(); $vaccine_number++; //Get the vaccines wiith dhis data $vaccines = Vaccines::getDHIS(); $months = array(); foreach ($district_array as $district) { $data_buffer = "\n\t\t\t<style>\n\t\t\ttable.data-table {\n\t\t\ttable-layout: fixed;\n\t\t\twidth: 1000px;\n\t\t\tborder-collapse:collapse;\n\t\t\tborder:1px solid black;\n\t\t\tmargin-top:20px;\n\t\t\t}\n\t\t\ttable.data-table td, th {\n\t\t\twidth: 100px;\n\t\t\tborder: 1px solid black;\n\t\t\t}\n\t\t\t.leftie{\n\t\t\t\ttext-align: left !important;\n\t\t\t}\n\t\t\t.right{\n\t\t\t\ttext-align: right !important;\n\t\t\t}\n\t\t\t.center{\n\t\t\t\ttext-align: center !important;\n\t\t\t}\n\t\t\t</style> \n\t\t\t"; $data_buffer .= "<table class='data-table'>"; $data_buffer .= $this->echoTitles(); $data_buffer .= "<tr>"; //loop through all the months in the season for ($x = 1; $x <= 12; $x++) { //get the month name $text = "2012-" . $x . "-1"; $period = date("M-y", strtotime($text)); $months[$x] = $period; $data_buffer .= "<td>" . $period . "</td>"; } $data_buffer .= "</tr>"; //Loop through the returned districts, generating a report for each $district_name = $district['district_name']; $district = $district['district']; //get all facilities for this district $facilities = Facilities::getDistrictFacilities($district); //loop through all the facilities to return immunization data foreach ($facilities as $facility) { $data_buffer .= "<tr><td rowspan='{$vaccine_number}'>" . $facility->name . "</td>"; $data_buffer .= "<td>Children Immunized</td>"; //get the total number of children immunized in this facility foreach ($months as $month) { $sql_children = "select fully_immunized_children from dhis_data where facility_code = '{$facility->facilitycode}' and reporting_period = '{$month}'"; $query_children = $this->db->query($sql_children); $immunized_children = $query_children->row_array(); if (isset($immunized_children['fully_immunized_children'])) { $data_buffer .= "<td>" . $immunized_children['fully_immunized_children'] . "</td>"; } else { $data_buffer .= "<td>N/A</td>"; } } $data_buffer .= "</tr>"; foreach ($vaccines as $vaccine) { $data_buffer .= "<tr><td>" . $vaccine->Name . "</td>"; $dhis_remaining = $vaccine->Dhis_Remaining; $dhis_received = $vaccine->Dhis_Received; $dhis_stock = $vaccine->Dhis_Stock; foreach ($months as $month) { //For each vaccine, create a query to get its dhis data $sql_remaining = "select {$dhis_remaining}, {$dhis_received}, {$dhis_stock} from dhis_data where facility_code = '{$facility->facilitycode}' and reporting_period = '{$month}'"; $query_remaining = $this->db->query($sql_remaining); $remaining_stock = $query_remaining->row_array(); //var_dump($query_remaining); if (!empty($remaining_stock)) { if (strlen($remaining_stock[$dhis_received]) > 0 || strlen($remaining_stock[$dhis_remaining]) > 0 || strlen($remaining_stock[$dhis_stock]) > 0) { if ($remaining_stock[$dhis_remaining] == "0" || $remaining_stock[$dhis_remaining] == "" || $remaining_stock[$dhis_remaining] == null) { $data_buffer .= "<td style='background-color: red;'>" . $remaining_stock[$dhis_remaining] . "</td>"; } else { $data_buffer .= "<td>" . $remaining_stock[$dhis_remaining] . "</td>"; } } else { $data_buffer .= "<td style='text-align:center'>-</td>"; } } else { $data_buffer .= "<td style='text-align:center'>-</td>"; } } } $data_buffer .= "</tr>"; } $data_buffer .= "</table>"; //echo $data_buffer; $this->generatePDF($data_buffer, $district_name); } }