Esempio n. 1
0
    public function send_system_usage_specific($county_id = NULL, $district_id = NULL, $facility_code = NULL, $listing_email_address = NULL)
    {
        //Karsan
        $time = date('M , d Y');
        $county_name = counties::get_county_name($county_id);
        $district_name = districts::get_district_name_($district_id);
        $cp_email_query = "SELECT * FROM email_listing_new WHERE county = {$county_id} AND usertype = 10";
        $cp_emails = $this->db->query($cp_email_query)->result_array();
        $scp_email_query = "SELECT * FROM email_listing_new WHERE county = {$county_id} AND usertype = 3";
        $scp_emails = $this->db->query($scp_email_query)->result_array();
        // echo "<pre>";print_r($cp_emails);exit;
        //CP
        $cp_emails_str = '';
        foreach ($cp_emails as $emails => $value) {
            $cp_emails_str = $cp_emails_str . ',' . $value['email'];
            // echo "<pre>";print_r($value['email']);exit;
        }
        $cp_emails_str = ltrim($cp_emails_str, ',');
        //SCP
        $scp_emails_str = '';
        foreach ($scp_emails as $emails => $value) {
            $scp_emails_str = $scp_emails_str . ',' . $value['email'];
            // echo "<pre>";print_r($value['email']);exit;
        }
        $scp_emails_str = ltrim($scp_emails_str, ',');
        // echo $scp_emails_str;exit;
        // echo "<pre>";print_r($scp_emails);exit;
        // echo "<pre>";print_r($district_name);
        // echo "<pre>OVER ";
        $appended = !empty($district_name) ? $district_name['district'] . " Subcounty" : $county_name['county'] . " County";
        $excel_title = 'HCMP facility activity log summary for ' . $appended;
        $email_subject = 'HCMP System usage report for ' . $appended . ' as at ' . $time;
        // echo $county_name.' '.$district_name;
        // echo $excel_title;exit;
        $active_facilities = Facilities::getAll_($county_id, $district_id);
        // echo "<pre>";print_r($active_facilities);echo "</pre>";exit;
        $last_seen = Facilities::get_facility_data_specific(NULL, $county_id, $district_id, $facility_code, 'all');
        $last_issued = Facilities::get_facility_data_specific('last_issued', $county_id, $district_id, $facility_code, 'all');
        $last_ordered = Facilities::get_facility_data_specific('last_ordered', $county_id, $district_id, $facility_code, 'all');
        $decommissioned = Facilities::get_facility_data_specific('last_decommissioned', $county_id, $district_id, $facility_code, 'all');
        $redistributed = Facilities::get_facility_data_specific('last_redistributed', $county_id, $district_id, $facility_code, 'all');
        $added_stock = Facilities::get_facility_data_specific('last_added_stock', $county_id, $district_id, $facility_code, 'all');
        // $all_faciliteis = Facilities::getAll_();
        // echo "<pre>";print_r($active_facilities);echo "</pre>";exit;
        $facility_count = empty($active_facilities) ? NULL : count($active_facilities);
        $final_array = array();
        $last_seen_count = count($last_seen);
        $last_issued_count = count($last_issued);
        $last_ordered_count = count($last_ordered);
        $decommissioned_count = count($decommissioned);
        $redistributed_count = count($redistributed);
        $added_stock_count = count($added_stock);
        foreach ($active_facilities as $a_c) {
            $final_array[] = array('Facility Name' => $a_c['facility_name'], 'Facility Code' => $a_c['facility_code'], 'County' => $a_c['county'], 'Sub-County' => $a_c['subcounty']);
        }
        //active_facilities foreach
        $final_array_count = count($final_array);
        $last_seen_time = NULL;
        $last_issued_time = NULL;
        $last_order_time = NULL;
        $last_deccommissioned_time = NULL;
        $last_redistributed_time = NULL;
        $last_added_stock_time = NULL;
        foreach ($final_array as $keyy => $value) {
            foreach ($last_seen as $key => $value) {
                if ($final_array[$keyy]['Facility Code'] == $last_seen[$key]['facility_code']) {
                    // echo "<pre>".$last_ordered[$j]['last_seen']."	".$last_ordered[$j]['facility_code'];
                    if ($last_seen[$key]['last_seen'] > $last_seen_time) {
                        $last_seen_time = $last_seen[$key]['last_seen'];
                        $days_last_seen = $last_seen[$key]['difference_in_days'];
                        // echo "<pre>".$last_seen_time;
                    }
                    $final_array[$keyy]['Date Last Seen'] = $last_seen_time;
                    $final_array[$keyy]['Days From Last Seen'] = $days_last_seen;
                    // echo "<pre>".$last_seen_time;
                    // $final_array[$i]['Days From Last Seen'] = abs($last_seen_time - $now);
                    $last_seen_time = NULL;
                }
                //end of facility code if
            }
            //end of last seen foreach
        }
        // echo "<pre>";print_r($final_array);echo "</pre>";exit;
        //last issued time
        foreach ($final_array as $keyy => $value) {
            foreach ($last_issued as $key => $value) {
                if ($final_array[$keyy]['Facility Code'] == $last_issued[$key]['facility_code']) {
                    // echo "<pre>".$last_ordered[$j]['last_seen']."	".$last_ordered[$j]['facility_code'];
                    if ($last_issued[$key]['last_seen'] > $last_issued_time) {
                        $last_issued_time = $last_issued[$key]['last_seen'];
                        $days = $last_issued[$key]['difference_in_days'];
                        // echo "<pre>".$last_order_time;
                    }
                    $final_array[$keyy]['Date Last Issued'] = $last_issued_time;
                    $final_array[$keyy]['Days From Last Issue'] = $days;
                    // $final_array[$i]['Days From Last Seen'] = abs($last_seen_time - $now);
                    $last_issued_time = NULL;
                }
                //end of facility code if
            }
            //end of last seen foreach
        }
        //last ordered
        foreach ($final_array as $keyy => $value) {
            foreach ($last_ordered as $key => $value) {
                if ($final_array[$keyy]['Facility Code'] == $last_ordered[$key]['facility_code']) {
                    // echo "<pre>".$last_ordered[$j]['last_seen']."	".$last_ordered[$j]['facility_code'];
                    if ($last_ordered[$key]['last_seen'] > $last_order_time) {
                        $last_order_time = $last_ordered[$key]['last_seen'];
                        $days_last_ordered = $last_ordered[$key]['difference_in_days'];
                        // echo "<pre>".$last_order_time;
                    }
                    $final_array[$keyy]['Date Last Ordered'] = $last_order_time;
                    $final_array[$keyy]['Days From Last Order'] = $days_last_ordered;
                    // $final_array[$i]['Days From Last Seen'] = abs($last_seen_time - $now);
                    $last_order_time = NULL;
                }
                //end of facility code if
            }
            //end of last seen foreach
        }
        foreach ($final_array as $keyy => $value) {
            foreach ($decommissioned as $key => $value) {
                if ($final_array[$keyy]['Facility Code'] == $decommissioned[$key]['facility_code']) {
                    // echo "<pre>".$last_ordered[$j]['last_seen']."	".$last_ordered[$j]['facility_code'];
                    if ($decommissioned[$key]['last_seen'] > $last_deccommissioned_time) {
                        $last_deccommissioned_time = $decommissioned[$key]['last_seen'];
                        $days_last_decommissioned = $decommissioned[$key]['difference_in_days'];
                        // echo "<pre>".$last_order_time;
                    }
                    $final_array[$keyy]['Date Last Decommissioned'] = $last_deccommissioned_time;
                    $final_array[$keyy]['Days From Last Decommissioned'] = $days_last_decommissioned;
                    // $final_array[$i]['Days From Last Seen'] = abs($last_seen_time - $now);
                    $last_deccommissioned_time = NULL;
                }
                //end of facility code if
            }
            //end of last seen foreach
        }
        foreach ($final_array as $keyy => $value) {
            foreach ($redistributed as $key => $value) {
                if ($final_array[$keyy]['Facility Code'] == $redistributed[$key]['facility_code']) {
                    // echo "<pre>".$last_ordered[$j]['last_seen']."	".$last_ordered[$j]['facility_code'];
                    if ($redistributed[$key]['last_seen'] > $last_redistributed_time) {
                        $last_redistributed_time = $redistributed[$key]['last_seen'];
                        $days_last_redistributed = $redistributed[$key]['difference_in_days'];
                        // echo "<pre>".$last_order_time;
                    }
                    $final_array[$keyy]['Date Last Redistributed'] = $last_redistributed_time;
                    $final_array[$keyy]['Days From Last Redistributed'] = $days_last_redistributed;
                    // $final_array[$i]['Days From Last Seen'] = abs($last_seen_time - $now);
                    $last_redistributed_time = NULL;
                }
                //end of facility code if
            }
            //end of last seen foreach
        }
        foreach ($final_array as $keyy => $value) {
            foreach ($added_stock as $key => $value) {
                if ($final_array[$keyy]['Facility Code'] == $added_stock[$key]['facility_code']) {
                    // echo "<pre>".$last_ordered[$j]['last_seen']."	".$last_ordered[$j]['facility_code'];
                    if ($added_stock[$key]['last_seen'] > $last_added_stock_time) {
                        $last_added_stock_time = $added_stock[$key]['last_seen'];
                        $days_last_added_stock = $added_stock[$key]['difference_in_days'];
                        // echo "<pre>".$last_order_time;
                    }
                    $final_array[$keyy]['Date Last Received Order'] = $last_added_stock_time;
                    $final_array[$keyy]['Days From Last Received Order'] = $days_last_added_stock;
                    // $final_array[$i]['Days From Last Seen'] = abs($last_seen_time - $now);
                    $last_added_stock_time = NULL;
                }
                //end of facility code if
            }
            //end of last seen foreach
        }
        // echo "<pre>";print_r($final_array);exit;
        $row_data = array();
        $counterrrr = 0;
        foreach ($final_array as $facility) {
            // echo "<pre>". $counterrrr . "</pre>";
            // $counterrrr = $counterrrr + 1;
            //random code to allow for commit
            $issue_date = isset($facility['Date Last Issued']) ? date('Y-m-d', strtotime($facility['Date Last Issued'])) : "No Data Available";
            $last_seen = isset($facility['Date Last Seen']) ? date('Y-m-d', strtotime($facility['Date Last Seen'])) : "No Data Available";
            $redistribution = isset($facility['Date Last Redistributed']) ? date('Y-m-d', strtotime($facility['Date Last Redistributed'])) : "No Data Available";
            $order_date = isset($facility['Date Last Ordered']) ? date('Y-m-d', strtotime($facility['Date Last Ordered'])) : "No Data Available";
            $decommission_date = isset($facility['Date Last Decommissioned']) ? date('Y-m-d', strtotime($facility['Date Last Decommissioned'])) : "No Data Available";
            $date_order = isset($facility['Date Last Received Order']) ? date('Y-m-d', strtotime($facility['Date Last Received Order'])) : "No Data Available";
            $days_from_last_seen = isset($facility['Days From Last Seen']) ? $facility['Days From Last Seen'] : '    -    ';
            $days_from_last_issued = isset($facility['Days From Last Issue']) ? $facility['Days From Last Issue'] : '  -    ';
            $days_from_last_redist = isset($facility['Days From Last Redistributed']) ? $facility['Days From Last Redistributed'] : '    -    ';
            $days_from_last_ordered = isset($facility['Days From Last Order']) ? $facility['Days From Last Order'] : '    -  ';
            $decomissioned_days = isset($facility['Days From Last Decommissioned']) ? $facility['Days From Last Decommissioned'] : '    -    ';
            $days_from_last_recieved = isset($facility['Days From Last Received Order']) ? $facility['Days From Last Received Order'] : '    -    ';
            array_push($row_data, array($facility['Facility Name'], $facility['Facility Code'], $facility['Sub-County'], $facility['County'], $last_seen, $days_from_last_seen, $issue_date, $days_from_last_issued, $redistribution, $days_from_last_redist, $order_date, $days_from_last_ordered, $decommission_date, $decomissioned_days, $date_order, $days_from_last_recieved));
        }
        $excel_data = array();
        // $excel_data = array('doc_creator' => 'HCMP ', 'doc_title' => 'System Usage Breakdown ', 'file_name' => 'system usage breakdown');
        $excel_data = array('doc_creator' => 'HCMP-Kenya', 'doc_title' => $excel_title, 'file_name' => 'HCMP_Facility_Activity_Log_Summary_as_at_' . $time);
        $column_data = array("Facility Name", "Facility Code", "Sub County", "County", "Date Last Logged In", "Days From Last Log In", "Date Last Issued", "Days From Last Issue", "Date Last Redistributed", "Days From Last Redistribution", "Date Last Ordered", "Days From Last Order", "Date Last Decommissioned", "Days From Last Decommission", "Date Last Received Order", "Days From Last Stock Addition");
        $excel_data['column_data'] = $column_data;
        $excel_data['row_data'] = $row_data;
        $excel_data['report_type'] = 'Log Summary';
        // echo "<pre>";print_r($excel_data);echo "</pre>";exit;
        $res = $this->hcmp_functions->create_excel($excel_data);
        // echo 'This '.$res;exit;
        $message = '';
        $message .= "<style> table {\n\t\t\tborder-collapse: collapse; \n\t\t}td,th{\n\t\t\tpadding: 12px;\n\t\t\ttext-align:center;\n\t\t}\n\n\t\t*{margin:0;padding:0}*{font-family:'Helvetica Neue',Helvetica,Helvetica,Arial,sans-serif}img{max-width:100%}.collapse{padding:0}body{-webkit-font-smoothing:antialiased;-webkit-text-size-adjust:none;width:100%!important;height:100%}a{color:#2BA6CB}.btn{text-decoration:none;color:#FFF;background-color:#666;padding:10px 16px;font-weight:700;margin-right:10px;text-align:center;cursor:pointer;display:inline-block}p.callout{padding:15px;background-color:#ECF8FF;margin-bottom:15px}.callout a{font-weight:700;color:#2BA6CB}table.social{background-color:#ebebeb}.social .soc-btn{padding:3px 7px;font-size:12px;margin-bottom:10px;text-decoration:none;color:#FFF;font-weight:700;display:block;text-align:center}a.fb{background-color:#3B5998!important}a.tw{background-color:#1daced!important}a.gp{background-color:#DB4A39!important}a.ms{background-color:#000!important}.sidebar .soc-btn{display:block;width:100%}table.head-wrap{width:100%}.header.container table td.logo{padding:15px}.header.container table td.label{padding:15px 15px 15px 0}table.body-wrap{width:100%}table.footer-wrap{width:100%;clear:both!important}.footer-wrap .container td.content p{border-top:1px solid #d7d7d7;padding-top:15px;font-size:9px;font-weight:500}h1,h2,h3,h4,h5,h6{font-family:HelveticaNeue-Light,'Helvetica Neue Light','Helvetica Neue',Helvetica,Arial,'Lucida Grande',sans-serif;line-height:1.1;margin-bottom:15px;color:#000}h1 small,h2 small,h3 small,h4 small,h5 small,h6 small{font-size:60%;color:#6f6f6f;line-height:0;text-transform:none}h1{font-weight:200;font-size:44px}h2{font-weight:200;font-size:37px}h3{font-weight:500;font-size:27px}h4{font-weight:500;font-size:23px}h5{font-weight:900;font-size:17px}h6{font-weight:900;font-size:14px;text-transform:uppercase;color:#444}.collapse{margin:0!important}p,ul{margin-bottom:10px;font-weight:400;font-size:14px;line-height:1.6}p.lead{font-size:17px}p.last{margin-bottom:0}ul li{margin-left:5px;list-style-position:inside}ul.sidebar{background:#ebebeb;display:block;list-style-type:none}ul.sidebar li{display:block;margin:0}ul.sidebar li a{text-decoration:none;color:#666;padding:10px 16px;cursor:pointer;border-bottom:1px solid #777;border-top:1px solid #FFF;display:block;margin:0}ul.sidebar li a.last{border-bottom-width:0}ul.sidebar li a h1,ul.sidebar li a h2,ul.sidebar li a h3,ul.sidebar li a h4,ul.sidebar li a h5,ul.sidebar li a h6,ul.sidebar li a p{margin-bottom:0!important}.container{display:block!important;max-width:100%!important;margin:0 auto!important;clear:both!important}.content{padding:15px;max-width:80%px;margin:0 auto;display:block}.content table{width:100%}.column{width:300px;float:left}.column tr td{padding:15px}.column-wrap{padding:0!important;margin:0 auto;max-width:600px!important}.column table{width:100%}.social .column{width:280px;min-width:279px;float:left}.clear{display:block;clear:both}@media only screen and (max-width:600px){a[class=btn]{display:block!important;margin-bottom:10px!important;background-image:none!important;margin-right:0!important}div[class=column]{width:auto!important;float:none!important}table.social div[class=column]{width:auto!important}}</style>";
        $message .= '
		<tr>
			<td colspan="12">
			</tr>
		</tbody>
		</table>';
        $message .= "<!-- BODY -->\n\t\t<table class='body-wrap'>\n\t\t\t<tr>\n\t\t\t\t<td></td>\n\t\t\t\t<td class='container' bgcolor='#FFFFFF'>\n\n\t\t\t\t\t<div class='content'>\n\t\t\t\t\t\t<table>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<td>\n\n\t\t\t\t\t\t\t\t\t<p class='lead'>Find attached a summary of Facility Activity Log, as at {$time}</p>\n\n\t\t\t\t\t\t\t\t\t<table class='social' width='100%'>\n\t\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t\t<td>\n\n\t\t\t\t\t\t\t\t\t\t\t\t<!-- column 1 -->\n\t\t\t\t\t\t\t\t\t\t\t\t<table align='left' class='column'>\n\n\t\t\t\t\t\t\t\t\t\t\t\t</table><!-- /column 1 -->\t\n\n\t\t\t\t\t\t\t\t\t\t\t\t<!-- column 2 -->\n\t\t\t\t\t\t\t\t\t\t\t\t<table align='left' class='column'>\n\t\t\t\t\t\t\t\t\t\t\t\t\t<tr>\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t\t\t\t</table><!-- /column 2 -->\n\n\t\t\t\t\t\t\t\t\t\t\t\t<span class='clear'></span>\t\n\n\t\t\t\t\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t</table><!-- /social & contact -->\n\n\t\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t</table>\n\t\t\t\t\t</div><!-- /content -->\n\n\t\t\t\t</td>\n\t\t\t\t<td></td>\n\t\t\t</tr>\n\t\t</table><!-- /BODY -->";
        $handler = "./print_docs/excel/excel_files/" . $excel_data['file_name'] . ".xls";
        // $subject = $excel_title." as at ".$time;
        // $email_address = "smutheu@clintonhealthaccess.org,sethrichard40@gmail.com,ttunduny@gmail.com,teddyodera@gmail.com";
        // $email_address = $listing_email_address;
        $email_address = $cp_emails_str;
        $cc_email = $scp_emails_str;
        $bcc_email = "smutheu@clintonhealthaccess.org,karsanrichard@gmail.com,ttunduny@gmail.com,teddyodera@gmail.com,ronohb@gmail.com,odiwuorybrian@gmail.com,mwakiojoy@gmail.com,kevgithuka@gmail.com,margie.odora@gmail.com,kiganyastephenthua@gmail.com";
        // $email_address = "*****@*****.**";
        // $bcc_email = "";
        // $cc_email = "";
        // $email_address = "karsanrichard@gmail.com,ttunduny@gmail.com";
        // $email_address = "*****@*****.**";
        //$bcc = "";
        if (!empty($facility_count) && $facility_count > 0) {
            $status = $this->hcmp_functions->send_email($email_address, $message, $email_subject, $handler, $bcc_email, $cc_email);
        }
        // echo "I work till here";exit;
        // echo "<pre>";print_r($status);exit;
        // redirect('sms/new_weekly_usage');
    }
Esempio n. 2
0
 public function monitoring($for_temporary_system_usage = null)
 {
     //being authored by Karsan as at 2015-08-04
     //pick values form the session
     // $facility_code = (!$this -> session -> userdata('facility_id')) ? null : $this -> session -> userdata('facility_id');
     // $district_id = (!$this -> session -> userdata('district_id')) ? null : $this -> session -> userdata('district_id');
     // $county_id = (!$this -> session -> userdata('county_id')) ? null : $this -> session -> userdata('county_id');
     // $category_data = $series_data = $graph_data = $series_data_ = array();
     $county_id = $this->session->userdata('county_id');
     $district_id = $this->session->userdata('district_id');
     $facility_code = $this->session->userdata('facility_id');
     $county_id = isset($county_id) && $county_id > 0 ? $county_id : NULL;
     $district_id = isset($district_id) && $district_id > 0 ? $district_id : NULL;
     $facility_code = isset($facility_code) && $facility_code > 0 ? $facility_code : NULL;
     // $facility_code = ($facility_code == "NULL") ? null : $facility_code;
     $identifier = $this->session->userdata('user_indicator');
     //the old query that causes too many locks on the mysql tables
     //$facility_data=Facilities::get_facilities_monitoring_data( $facility_code,$district_id,$county_id,$identifier);
     //get the monitoring data from the log tables
     // $facility_data = Facilities::facility_monitoring($county_id, $district_id, $facility_code);
     // $facility_data_new = Facilities::new_facility_monitoring_function($county_id, $district_id, $facility_code,'all');
     // get_facility_data_specific($report_type = NULL,$criteria = NULL,$county_id = NULL,$district_id = NULL,$facility_code = NULL)
     // echo $district_id;exit;
     $all_facilities = Facilities::getAll_($county_id, $district_id);
     // echo "<pre>";print_r($all_facilities);echo "</pre>";exit;
     $last_seen = Facilities::get_facility_data_specific(NULL, $county_id, $district_id, $facility_code);
     $last_issued = Facilities::get_facility_data_specific('last_issued', $county_id, $district_id, $facility_code);
     $last_ordered = Facilities::get_facility_data_specific('last_ordered', $county_id, $district_id, $facility_code);
     $decommissioned = Facilities::get_facility_data_specific('last_decommissioned', $county_id, $district_id, $facility_code);
     $redistributed = Facilities::get_facility_data_specific('last_redistributed', $county_id, $district_id, $facility_code);
     $added_stock = Facilities::get_facility_data_specific('last_added_stock', $county_id, $district_id, $facility_code);
     $final_array = array();
     $last_seen_count = count($last_seen);
     $last_issued_count = count($last_issued);
     $last_ordered_count = count($last_ordered);
     $decommissioned_count = count($decommissioned);
     $redistributed_count = count($redistributed);
     $added_stock_count = count($added_stock);
     /*
     		//previous system that chose which array had highest value and begun with
     $rollcall = array(
     	'last_seen' => $last_seen_count,
     	'last_issued' => $last_issued_count,
     	'last_ordered' => $last_ordered_count,
     	'decommissioned' => $decommissioned_count,
     	'redistributed' => $redistributed_count,
     	'added_stock' => $added_stock_count
     	);
     // echo "<pre>";print_r($rollcall);echo "</pre>";
     asort($rollcall);
     $rollcall = array_reverse($rollcall,true);
             $arr_key = key($rollcall);
     // echo "<pre>";print_r($arr_key);echo "</pre>";
     
     
     $highest = max($last_seen_count,$last_issued_count,$last_ordered_count,$decommissioned_count,$redistributed_count,$added_stock_count);
     $keys_na_si_alishia = array_search($highest, $rollcall);
     // echo $highest;
     switch ($keys_na_si_alishia) {
     	case 'last_seen':
     
     		foreach ($last_seen as $l_seen) { 
     		$final_array[] = array(
     			'Facility Name' => $l_seen['facility_name'], 
     			'Facility Code' => $l_seen['facility_code'],
     			'County' => $l_seen['county'],
     			'Sub-County' => $l_seen['district']
     			);
     		}//last issued foreach
     
     	case 'last_issued':
     		foreach ($last_issued as $l_seen) { 
     		$final_array[] = array(
     			'Facility Name' => $l_seen['facility_name'], 
     			'Facility Code' => $l_seen['facility_code'],
     			'County' => $l_seen['county'],
     			'Sub-County' => $l_seen['district']
     			);
     		}//last issued foreach
     
     	case 'last_ordered':
     		foreach ($last_ordered as $l_seen) { 
     		$final_array[] = array(
     			'Facility Name' => $l_seen['facility_name'], 
     			'Facility Code' => $l_seen['facility_code'],
     			'County' => $l_seen['county'],
     			'Sub-County' => $l_seen['district']
     			);
     		}//last issued foreach
     
     	case 'decommissioned':
     		foreach ($decommissioned as $l_seen) { 
     		$final_array[] = array(
     			'Facility Name' => $l_seen['facility_name'], 
     			'Facility Code' => $l_seen['facility_code'],
     			'County' => $l_seen['county'],
     			'Sub-County' => $l_seen['district']
     			);
     		}//last issued foreach
     	case 'redistributed':
     		foreach ($redistributed as $l_seen) { 
     		$final_array[] = array(
     			'Facility Name' => $l_seen['facility_name'], 
     			'Facility Code' => $l_seen['facility_code'],
     			'County' => $l_seen['county'],
     			'Sub-County' => $l_seen['district']
     			);
     		}//last issued foreach
     		break;
     	case 'added_stock':
     		foreach ($added_stock as $l_seen) { 
     		$final_array[] = array(
     			'Facility Name' => $l_seen['facility_name'], 
     			'Facility Code' => $l_seen['facility_code'],
     			'County' => $l_seen['county'],
     			'Sub-County' => $l_seen['district']
     			);
     		}//last issued foreach
     		break;
     }
     */
     //random code to allow for commit
     // echo $key;exit;
     // echo "<pre>".$highest;exit;
     // echo "<pre>";print_r($last_seen);echo "</pre>";
     // echo "END OF LAST SEEN";
     // echo "<pre>";print_r($last_issued);echo "</pre>";
     // echo "END OF LAST ISSUED";
     // echo "<pre>";print_r($last_ordered);echo "</pre>";
     // echo "END OF LAST ORDERED";
     // echo "<pre>";print_r($decommissioned);echo "</pre>";
     // echo "END OF LAST DECOMMISSSIONED";
     // echo "<pre>";print_r($redistributed);echo "</pre>";
     // echo "END OF LAST REDISTRIBUTED";
     // echo "<pre>";print_r($added_stock);echo "</pre>";
     // echo "END OF LAST ADDED STOCK";
     // exit;
     foreach ($all_facilities as $a_c) {
         $final_array[] = array('Facility Name' => $a_c['facility_name'], 'Facility Code' => $a_c['facility_code'], 'County' => $a_c['county'], 'Sub-County' => $a_c['subcounty']);
     }
     //final_array foreach
     // $final_array = array_unique($final_array,SORT_REGULAR);
     // $final_array = array_map("unserialize", array_unique(array_map("serialize", $final_array)));
     // echo "<pre>";print_r($final_array);echo "</pre>";exit;
     $final_array_count = count($final_array);
     // echo "<pre>" . $final_array_count."</pre>";
     // echo "<pre>" . $last_seen_count."</pre>";
     // echo "<pre>" . $last_issued_count."</pre>";
     // echo "<pre>" . $final_array_count."</pre>";
     // echo "<pre>" . $last_ordered_count."</pre>";
     // echo "<pre>" . $decommissioned_count."</pre>";
     // echo "<pre>" . $redistributed_count."</pre>";
     // echo "<pre>" . $added_stock_count."</pre>";
     // exit;
     $last_seen_time = NULL;
     $last_issued_time = NULL;
     $last_order_time = NULL;
     $last_deccommissioned_time = NULL;
     $last_redistributed_time = NULL;
     $last_added_stock_time = NULL;
     foreach ($final_array as $keyy => $value) {
         foreach ($last_seen as $key => $value) {
             if ($final_array[$keyy]['Facility Code'] == $last_seen[$key]['facility_code']) {
                 // echo "<pre>".$last_ordered[$j]['last_seen']."	".$last_ordered[$j]['facility_code'];
                 if ($last_seen[$key]['last_seen'] > $last_seen_time) {
                     $last_seen_time = $last_seen[$key]['last_seen'];
                     $days_last_seen = $last_seen[$key]['difference_in_days'];
                     // echo "<pre>".$last_seen_time;
                 }
                 $final_array[$keyy]['Date Last Seen'] = $last_seen_time;
                 $final_array[$keyy]['Days From Last Seen'] = $days_last_seen;
                 // echo "<pre>".$last_seen_time;
                 // $final_array[$i]['Days From Last Seen'] = abs($last_seen_time - $now);
                 $last_seen_time = NULL;
             }
             //end of facility code if
         }
         //end of last seen foreach
     }
     // echo "<pre>";print_r($final_array);echo "</pre>";exit;
     //last issued time
     foreach ($final_array as $keyy => $value) {
         foreach ($last_issued as $key => $value) {
             if ($final_array[$keyy]['Facility Code'] == $last_issued[$key]['facility_code']) {
                 // echo "<pre>".$last_ordered[$j]['last_seen']."	".$last_ordered[$j]['facility_code'];
                 if ($last_issued[$key]['last_seen'] > $last_issued_time) {
                     $last_issued_time = $last_issued[$key]['last_seen'];
                     $days = $last_issued[$key]['difference_in_days'];
                     // echo "<pre>".$last_order_time;
                 }
                 $final_array[$keyy]['Date Last Issued'] = $last_issued_time;
                 $final_array[$keyy]['Days From Last Issue'] = $days;
                 // $final_array[$i]['Days From Last Seen'] = abs($last_seen_time - $now);
                 $last_issued_time = NULL;
             }
             //end of facility code if
         }
         //end of last seen foreach
     }
     //last ordered
     foreach ($final_array as $keyy => $value) {
         foreach ($last_ordered as $key => $value) {
             if ($final_array[$keyy]['Facility Code'] == $last_ordered[$key]['facility_code']) {
                 // echo "<pre>".$last_ordered[$j]['last_seen']."	".$last_ordered[$j]['facility_code'];
                 if ($last_ordered[$key]['last_seen'] > $last_order_time) {
                     $last_order_time = $last_ordered[$key]['last_seen'];
                     $days_last_ordered = $last_ordered[$key]['difference_in_days'];
                     // echo "<pre>".$last_order_time;
                 }
                 $final_array[$keyy]['Date Last Ordered'] = $last_order_time;
                 $final_array[$keyy]['Days From Last Order'] = $days_last_ordered;
                 // $final_array[$i]['Days From Last Seen'] = abs($last_seen_time - $now);
                 $last_order_time = NULL;
             }
             //end of facility code if
         }
         //end of last seen foreach
     }
     foreach ($final_array as $keyy => $value) {
         foreach ($decommissioned as $key => $value) {
             if ($final_array[$keyy]['Facility Code'] == $decommissioned[$key]['facility_code']) {
                 // echo "<pre>".$last_ordered[$j]['last_seen']."	".$last_ordered[$j]['facility_code'];
                 if ($decommissioned[$key]['last_seen'] > $last_deccommissioned_time) {
                     $last_deccommissioned_time = $decommissioned[$key]['last_seen'];
                     $days_last_decommissioned = $decommissioned[$key]['difference_in_days'];
                     // echo "<pre>".$last_order_time;
                 }
                 $final_array[$keyy]['Date Last Decommissioned'] = $last_deccommissioned_time;
                 $final_array[$keyy]['Days From Last Decommissioned'] = $days_last_decommissioned;
                 // $final_array[$i]['Days From Last Seen'] = abs($last_seen_time - $now);
                 $last_deccommissioned_time = NULL;
             }
             //end of facility code if
         }
         //end of last seen foreach
     }
     foreach ($final_array as $keyy => $value) {
         foreach ($redistributed as $key => $value) {
             if ($final_array[$keyy]['Facility Code'] == $redistributed[$key]['facility_code']) {
                 // echo "<pre>".$last_ordered[$j]['last_seen']."	".$last_ordered[$j]['facility_code'];
                 if ($redistributed[$key]['last_seen'] > $last_redistributed_time) {
                     $last_redistributed_time = $redistributed[$key]['last_seen'];
                     $days_last_redistributed = $redistributed[$key]['difference_in_days'];
                     // echo "<pre>".$last_order_time;
                 }
                 $final_array[$keyy]['Date Last Redistributed'] = $last_redistributed_time;
                 $final_array[$keyy]['Days From Last Redistributed'] = $days_last_redistributed;
                 // $final_array[$i]['Days From Last Seen'] = abs($last_seen_time - $now);
                 $last_redistributed_time = NULL;
             }
             //end of facility code if
         }
         //end of last seen foreach
     }
     foreach ($final_array as $keyy => $value) {
         foreach ($added_stock as $key => $value) {
             if ($final_array[$keyy]['Facility Code'] == $added_stock[$key]['facility_code']) {
                 // echo "<pre>".$last_ordered[$j]['last_seen']."	".$last_ordered[$j]['facility_code'];
                 if ($added_stock[$key]['last_seen'] > $last_added_stock_time) {
                     $last_added_stock_time = $added_stock[$key]['last_seen'];
                     $days_last_added_stock = $added_stock[$key]['difference_in_days'];
                     // echo "<pre>".$last_order_time;
                 }
                 $final_array[$keyy]['Date Last Received Order'] = $last_added_stock_time;
                 $final_array[$keyy]['Days From Last Received Order'] = $days_last_added_stock;
                 // $final_array[$i]['Days From Last Seen'] = abs($last_seen_time - $now);
                 $last_added_stock_time = NULL;
             }
             //end of facility code if
         }
         //end of last seen foreach
     }
     // echo "<pre>";print_r($final_array);echo "</pre>";exit;
     // echo $last_order_time;
     // exit;
     // echo "<pre>";print_r($final_array_count);echo "</pre>";exit;
     $row_data = array();
     $counterrrr = 0;
     foreach ($final_array as $facility) {
         // echo "<pre>". $counterrrr . "</pre>";
         // $counterrrr = $counterrrr + 1;
         $issue_date = isset($facility['Date Last Issued']) ? date('j M, Y', strtotime($facility['Date Last Issued'])) : "No Data Available";
         $last_seen = isset($facility['Date Last Seen']) ? date('j M, Y', strtotime($facility['Date Last Seen'])) : "No Data Available";
         $redistribution = isset($facility['Date Last Redistributed']) ? date('j M, Y', strtotime($facility['Date Last Redistributed'])) : "No Data Available";
         $order_date = isset($facility['Date Last Ordered']) ? date('j M, Y', strtotime($facility['Date Last Ordered'])) : "No Data Available";
         $decommission_date = isset($facility['Date Last Decommissioned']) ? date('j M, Y', strtotime($facility['Date Last Decommissioned'])) : "No Data Available";
         $date_order = isset($facility['Date Last Received Order']) ? date('j M, Y', strtotime($facility['Date Last Received Order'])) : "No Data Available";
         $days_from_last_seen = isset($facility['Days From Last Seen']) ? $facility['Days From Last Seen'] : '    -    ';
         $days_from_last_issued = isset($facility['Days From Last Issue']) ? $facility['Days From Last Issue'] : '  -    ';
         $days_from_last_redist = isset($facility['Days From Last Redistributed']) ? $facility['Days From Last Redistributed'] : '    -    ';
         $days_from_last_ordered = isset($facility['Days From Last Order']) ? $facility['Days From Last Order'] : '    -  ';
         $decomissioned_days = isset($facility['Days From Last Decommissioned']) ? $facility['Days From Last Decommissioned'] : '    -    ';
         $days_from_last_recieved = isset($facility['Days From Last Received Order']) ? $facility['Days From Last Received Order'] : '    -    ';
         array_push($row_data, array($facility['Facility Name'], $facility['Facility Code'], $facility['Sub-County'], $facility['County'], $last_seen, $days_from_last_seen, $issue_date, $days_from_last_issued, $redistribution, $days_from_last_redist, $order_date, $days_from_last_ordered, $decommission_date, $decomissioned_days, $date_order, $days_from_last_recieved));
     }
     if ($for_temporary_system_usage != null) {
         return $row_data;
     } else {
         $excel_data = array();
         $excel_data = array('doc_creator' => 'HCMP ', 'doc_title' => 'System Usage Breakdown ', 'file_name' => 'System Usage Breakdown');
         $column_data = array("Facility Name", "Facility Code", "Sub County", "County", "Date Last Logged In", "Days From Last Log In", "Date Last Issued", "Days From Last Issue", "Date Last Redistributed", "Days From Last Redistribution", "Date Last Ordered", "Days From Last Order", "Date Last Decommissioned", "Days From Last Decommission", "Date Last Received Order", "Days From Last Received Order");
         $excel_data['column_data'] = $column_data;
         $excel_data['row_data'] = $row_data;
         $this->hcmp_functions->create_excel($excel_data);
     }
     // echo "<pre>";print_r($excel_data);echo "</pre>";exit;
 }
Esempio n. 3
0
    public function log_summary_weekly()
    {
        $time = date('M , d Y');
        /*PRIOR TO CONVERSION TO NEW QUERY*/
        /*Indented for nesting*/
        /*
        				$data = $q = Doctrine_Manager::getInstance()->getCurrentConnection()
        			        ->fetchAll("SELECT
        	                                *
        	                            FROM
        	                                (SELECT
        	                                    f.facility_name,
        	                                        f.facility_code,
        	                                        c.county,
        	                                        d.district,
        	                                        l.user_id,
        	                                        IF(l.issued = 0 AND l.ordered = 0
        	                                            AND l.redistribute = 0
        	                                            AND l.decommissioned = 0, MAX(l.start_time_of_event), NULL) AS login_only,
        	                                        l.issued,
        	                                        IF(l.issued = 1 AND l.redistribute = 0, MAX(l.start_time_of_event), NULL) AS issue_event,
        	                                        l.ordered,
        	                                        DATEDIFF(NOW(), IF(l.issued = 1 AND l.redistribute = 0, MAX(l.start_time_of_event), NULL)) AS issue_d,
        	                                        IF(l.ordered = 1, MAX(l.end_time_of_event), NULL) AS ordered_event,
        	                                        DATEDIFF(NOW(), IF(l.ordered = 1, MAX(l.start_time_of_event), 0)) AS ordered_d,
        	                                        l.redistribute,
        	                                        IF(l.redistribute = 1, MAX(l.start_time_of_event), NULL) AS redistribute_event,
        	                                        DATEDIFF(NOW(), IF(l.redistribute = 1, MAX(l.start_time_of_event), 0)) AS redistribute_d,
        	                                        l.decommissioned,
        	                                        IF(l.decommissioned = 1, MAX(l.start_time_of_event), NULL) AS decommissioned_event,
        	                                        DATEDIFF(NOW(), IF(l.decommissioned = 1, MAX(l.start_time_of_event), 0)) AS decommissioned_d,
        	                                        l.add_stock,
        	                                        IF(l.add_stock = 1, MAX(l.start_time_of_event), NULL) AS receive_event,
        	                                        DATEDIFF(NOW(), IF(l.add_stock = 1, MAX(l.start_time_of_event), 0)) AS receive_event_d,
        	                                        MAX(l.start_time_of_event) AS date_event,
        	                                        DATEDIFF(NOW(), MAX(l.start_time_of_event)) AS date_event_d
        	                                FROM
        	                                    log l
        	                                INNER JOIN user u ON l.user_id = u.id
        	                                RIGHT JOIN facilities f ON u.facility = f.facility_code
        	                                INNER JOIN districts d ON f.district = d.id
        	                                INNER JOIN counties c ON d.county = c.id
        	                                WHERE
        	                                    using_hcmp = 1
        	                                GROUP BY l.issued , l.ordered , l.redistribute , l.decommissioned , f.facility_code) AS t
        	                            GROUP BY issued , ordered , redistribute , decommissioned , facility_code
        	                                                        ");
        					    $mfl=array();
        					    // echo "<pre>";print_r($data);echo "</pre>";exit;
        		 
        foreach ($data as $key) {
        		
        	$mfl[]=$key['facility_code'];
        			 
        		 }
        		 $unique_mfl=array_values(array_unique($mfl));
        		 //echo '<pre>';print_r($data); echo '</pre>';exit;
        		 $temp=array();
        		 foreach ($unique_mfl as $key ) {
        		 	
        		 	array_push($temp,array('facility_code'=>$key,
        			        'facility_name'=>'',
        			        'county'=>'',
        			        'district'=>'',
        			        'issued'=>'',
        			        'issue_event'=>0,
        			        'issue_d'=>0,
        			        'login_event'=>0,
        			        'ordered'=>0,
        			        'ordered_d'=>0,
        			        'ordered_event'=>'',
        			        'redistribute'=>'',
        			        'redistribute_event'=>'',
        			        'redistribute_d'=>0,
        			        'decommissioned'=>'',
        			        'decommissioned_event'=>'',
        			        'decommissioned_d'=>0,
        			        'receive_event'=>'',
        			        'receive_event_d'=>0,
        				    'date_event'=>'',
        				    'date_event_d'=>0
        			        ));
        			 
        		 }
        		//echo '<pre>';print_r($temp); echo '</pre>';exit;						 
        		 $multi_dimenetional = array();
        				foreach ($data  as $row) {
        				    $multi_dimenetional[$row['facility_name']][] = array( 'facility_name'=>$row['facility_name'],
        				    									'facility_code'=>$row['facility_code'],
        				    									'county'=>$row['county'],
        				    									'district'=>$row['district'],
        				    									  'issued'=>$row['issued'],
        				    									  'issue_event'=>($row['issue_event']),
        				    									  'issue_d'=>$row['issue_d'],
        				    									  'login_event'=>$row['login_only'],
        				    									  'ordered'=>$row['ordered'],
        				    									  'ordered_d'=>$row['ordered_d'],
        				    									  'ordered_event'=>$row['ordered_event'],
        				    									  'redistribute'=>$row['redistribute'],
        				    									  'redistribute_event'=>$row['redistribute_event'],
        				    									  'redistribute_d'=>$row['redistribute_d'],
        				    									  'decommissioned'=>$row['decommissioned'],
        				    									  'decommissioned_event'=>$row['decommissioned_event'],
        				    									  'decommissioned_d'=>$row['decommissioned_d'],
        				    									  'receive_event'=>$row['receive_event'],
        				    									  'receive_event_d'=>$row['receive_event_d'],
        				    									  'date_event'=>$row['date_event'],
        				    									  'date_event_d'=>$row['date_event_d']
        													        );
        				}
        		 // echo '<pre>';print_r(array_values($multi_dimenetional)); echo '</pre>';exit;
        		 $clean_array=array_values($multi_dimenetional);
        		
        			$new=call_user_func_array('array_merge_recursive', $multi_dimenetional);
        
        			  $temp2 = array();
        		foreach ($clean_array as $key => $value) {
        			//echo count($value).'<br>';
        			$issue_event=array();
        			$issue_days=array();
        			$login_event=array();
        			$ordered=array();
        			$ordered_days=array();
        			$ordered_event=array();
        			$redistribute_days=array();
        			$redistribute_event=array();
        			$decommission=array();
        			$decommission_event=array();
        			$decommission_days=array();
        			
        			$receive_event=array();
        			$receive_days=array();
        			$last_event=array();
        			$lastseen_days=array();
        			foreach ($value as $key_child => $value_child) {
        				
        				foreach ($temp as $newkey => $newvalue) {
        					if ($newvalue['facility_code']==$value_child['facility_code']) {
        						$facility_name=$value_child['facility_name'];
        						$facility_code=$value_child['facility_code'];
        						$county=$value_child['county'];
        						$district=$value_child['district'];
        
        						$login_event[]=$value_child['login_only'];
        						$ordered[]=$value_child['ordered'];
        						
        						$issue_event[]=$value_child['issue_event'];
        						$issue_days[]=$value_child['issue_d'];
        						
        						$ordered_days[]=$value_child['ordered_d'];
        						$ordered_event[]=$value_child['ordered_event'];
        						$redistribute_days[]=$value_child['redistribute_d'];
        						$redistribute_event[]=$value_child['redistribute_event'];
        						$decommission[]=$value_child['decommissioned'];
        						$decommission_event[]=$value_child['decommissioned_event'];
        						
        						$decommission_days[]=$value_child['decommissioned_d'];
        						$receive_event[]=$value_child['receive_event'];
        						$receive_days[]=$value_child['receive_event_d'];
        						$last_event[]=$value_child['date_event'];
        						$lastseen_days[]=$value_child['date_event_d'];
        														
        			 
        					}
        					
        				}
        			}
        			array_push($temp2,array('facility_code'=>$facility_code,
        			        'facility_name'=>$facility_name,
        			        'county'=>$county,
        			        'district'=>$district,
        			        'issued'=>'',
        			        'issue_event'=>max($issue_event),
        			        'issue_d'=>min(array_filter($issue_days)),
        			        'login_event'=>max($login_event),
        			        'ordered'=>0,
        			        'ordered_d'=>min(array_filter($ordered_days)),
        			        'ordered_event'=>max($ordered_event),
        			        'redistribute'=>'',
        			        'redistribute_event'=>max($redistribute_event),
        			        'redistribute_d'=>min(array_filter($redistribute_days)),
        			        'decommissioned'=>'',
        			        'decommissioned_event'=>max($decommission_event),
        			        'decommissioned_d'=>min(array_filter($decommission_days)),
        			        'receive_event'=>max($receive_event),
        			        'receive_event_d'=>min(array_filter($receive_days)),
        				    'date_event'=>max($last_event),
        				    'date_event_d'=>min($lastseen_days)
        			        ));
        
        		}
        
        		$excel_data = array('doc_creator' => 'HCMP-Kenya', 'doc_title' => 'HCMP_Facility_Activity_Log_Summary ', 'file_name' => 'HCMP_Facility_Activity_Log_Summary_as_at_'.$time);
        		$row_data = array(); 
        		$column_data = array("Facility Name", "Facility Code", "County","Sub-County", "Date Last Issued", "Days from last issue",
        		"Date Last Redistributed", "Days From last Redistributed", "Date Last ordered", "Days From Last order", "Date Last Decommissioned",
        		 "Days From Last Decommissioned", "Date From Last Received Order", "Days From Last Received Order","Date Last Seen", "Days From Last Seen");
        		$excel_data['column_data'] = $column_data;
        		foreach ($temp2 as $key => $value) :
        		array_push($row_data, 
        		array($value['facility_name'],
        		$value['facility_code'],
        		$value['county'],
        		$value['district'],
        		(date('m-d-Y',strtotime($value['issue_event']))=='01-01-1970')? 'No Data Available' : $value['issue_event'],
        		($value['issue_d']==0)? 'No Data Available':$value['issue_d'],
        		(date('m-d-Y',strtotime($value['redistribute_event']))=='01-01-1970')? 'No Data Available' : $value['redistribute_event'],
        		($value['redistribute_d']=='')? 'No Data Available' :$value['redistribute_d'],
        		(date('m-d-Y',strtotime($value['ordered_event']))=='01-01-1970')? 'No Data Available' : $value['ordered_event'],
        		($value['ordered_d']=='')? 'No Data Available' :$value['ordered_d'] ,
        		(date('m-d-Y',strtotime($value['decommissioned_event']))=='01-01-1970')? 'No Data Available' : $value['decommissioned_event'],
        		($value['decommissioned_d']=='')? 'No Data Available':$value['decommissioned_d'],
        		(date('m-d-Y',strtotime($value['receive_event']))=='01-01-1970')? 'No Data Available' : $value['receive_event'],
        		($value['receive_event_d']=='')? 'No Data Available' :$value['receive_event_d'],
        		(date('m-d-Y',strtotime($value['date_event']))=='01-01-1970')? 'No Data Available' : $value['date_event'],
        		($value['date_event_d']=='')? 'No Data Available' :$value['date_event_d']
        		));
        		endforeach;
        		$excel_data['row_data'] = $row_data;
            // echo "<pre>";print_r($excel_data['row_data']);exit;
        		$excel_data['report_type']='Log Summary';
        		
        
        		$this -> hcmp_functions -> create_excel($excel_data);
        */
        /*END OF PRIOR TO CONVERSTION TO NEW QUERY*/
        $active_facilities = Facilities::getAll_();
        // echo "<pre>";print_r($active_facilities);echo "</pre>";exit;
        $last_seen = Facilities::get_facility_data_specific(NULL, $county_id, $district_id, $facility_code, 'all');
        $last_issued = Facilities::get_facility_data_specific('last_issued', $county_id, $district_id, $facility_code, 'all');
        $last_ordered = Facilities::get_facility_data_specific('last_ordered', $county_id, $district_id, $facility_code, 'all');
        $decommissioned = Facilities::get_facility_data_specific('last_decommissioned', $county_id, $district_id, $facility_code, 'all');
        $redistributed = Facilities::get_facility_data_specific('last_redistributed', $county_id, $district_id, $facility_code, 'all');
        $added_stock = Facilities::get_facility_data_specific('last_added_stock', $county_id, $district_id, $facility_code, 'all');
        // $all_faciliteis = Facilities::getAll_();
        // echo "<pre>";print_r($all_faciliteis);echo "</pre>";exit;
        $final_array = array();
        $last_seen_count = count($last_seen);
        $last_issued_count = count($last_issued);
        $last_ordered_count = count($last_ordered);
        $decommissioned_count = count($decommissioned);
        $redistributed_count = count($redistributed);
        $added_stock_count = count($added_stock);
        // echo "<pre>".$highest;exit;
        // echo "<pre>";print_r($last_seen);echo "</pre>";
        // echo "END OF LAST SEEN";
        // echo "<pre>";print_r($last_issued);echo "</pre>";
        // echo "END OF LAST ISSUED";
        // echo "<pre>";print_r($last_ordered);echo "</pre>";
        // echo "END OF LAST ORDERED";
        // echo "<pre>";print_r($decommissioned);echo "</pre>";
        // echo "END OF LAST DECOMMISSSIONED";
        // echo "<pre>";print_r($redistributed);echo "</pre>";
        // echo "END OF LAST REDISTRIBUTED";
        // echo "<pre>";print_r($added_stock);echo "</pre>";
        // echo "END OF LAST ADDED STOCK";
        // exit;
        foreach ($active_facilities as $a_c) {
            $final_array[] = array('Facility Name' => $a_c['facility_name'], 'Facility Code' => $a_c['facility_code'], 'County' => $a_c['county'], 'Sub-County' => $a_c['subcounty']);
        }
        //active_facilities foreach
        // $final_array = array_unique($final_array,SORT_REGULAR);
        // $final_array = array_map("unserialize", array_unique(array_map("serialize", $final_array)));
        // echo "<pre>";print_r($final_array);echo "</pre>";exit;
        $final_array_count = count($final_array);
        // echo "<pre>" . $final_array_count."</pre>";
        // echo "<pre>" . $last_seen_count."</pre>";
        // echo "<pre>" . $last_issued_count."</pre>";
        // echo "<pre>" . $final_array_count."</pre>";
        // echo "<pre>" . $last_ordered_count."</pre>";
        // echo "<pre>" . $decommissioned_count."</pre>";
        // echo "<pre>" . $redistributed_count."</pre>";
        // echo "<pre>" . $added_stock_count."</pre>";
        // exit;
        $last_seen_time = NULL;
        $last_issued_time = NULL;
        $last_order_time = NULL;
        $last_deccommissioned_time = NULL;
        $last_redistributed_time = NULL;
        $last_added_stock_time = NULL;
        foreach ($final_array as $keyy => $value) {
            foreach ($last_seen as $key => $value) {
                if ($final_array[$keyy]['Facility Code'] == $last_seen[$key]['facility_code']) {
                    // echo "<pre>".$last_ordered[$j]['last_seen']."	".$last_ordered[$j]['facility_code'];
                    if ($last_seen[$key]['last_seen'] > $last_seen_time) {
                        $last_seen_time = $last_seen[$key]['last_seen'];
                        $days_last_seen = $last_seen[$key]['difference_in_days'];
                        // echo "<pre>".$last_seen_time;
                    }
                    $final_array[$keyy]['Date Last Seen'] = $last_seen_time;
                    $final_array[$keyy]['Days From Last Seen'] = $days_last_seen;
                    // echo "<pre>".$last_seen_time;
                    // $final_array[$i]['Days From Last Seen'] = abs($last_seen_time - $now);
                    $last_seen_time = NULL;
                }
                //end of facility code if
            }
            //end of last seen foreach
        }
        // echo "<pre>";print_r($final_array);echo "</pre>";exit;
        //last issued time
        foreach ($final_array as $keyy => $value) {
            foreach ($last_issued as $key => $value) {
                if ($final_array[$keyy]['Facility Code'] == $last_issued[$key]['facility_code']) {
                    // echo "<pre>".$last_ordered[$j]['last_seen']."	".$last_ordered[$j]['facility_code'];
                    if ($last_issued[$key]['last_seen'] > $last_issued_time) {
                        $last_issued_time = $last_issued[$key]['last_seen'];
                        $days = $last_issued[$key]['difference_in_days'];
                        // echo "<pre>".$last_order_time;
                    }
                    $final_array[$keyy]['Date Last Issued'] = $last_issued_time;
                    $final_array[$keyy]['Days From Last Issue'] = $days;
                    // $final_array[$i]['Days From Last Seen'] = abs($last_seen_time - $now);
                    $last_issued_time = NULL;
                }
                //end of facility code if
            }
            //end of last seen foreach
        }
        //last ordered
        foreach ($final_array as $keyy => $value) {
            foreach ($last_ordered as $key => $value) {
                if ($final_array[$keyy]['Facility Code'] == $last_ordered[$key]['facility_code']) {
                    // echo "<pre>".$last_ordered[$j]['last_seen']."	".$last_ordered[$j]['facility_code'];
                    if ($last_ordered[$key]['last_seen'] > $last_order_time) {
                        $last_order_time = $last_ordered[$key]['last_seen'];
                        $days_last_ordered = $last_ordered[$key]['difference_in_days'];
                        // echo "<pre>".$last_order_time;
                    }
                    $final_array[$keyy]['Date Last Ordered'] = $last_order_time;
                    $final_array[$keyy]['Days From Last Order'] = $days_last_ordered;
                    // $final_array[$i]['Days From Last Seen'] = abs($last_seen_time - $now);
                    $last_order_time = NULL;
                }
                //end of facility code if
            }
            //end of last seen foreach
        }
        foreach ($final_array as $keyy => $value) {
            foreach ($decommissioned as $key => $value) {
                if ($final_array[$keyy]['Facility Code'] == $decommissioned[$key]['facility_code']) {
                    // echo "<pre>".$last_ordered[$j]['last_seen']."	".$last_ordered[$j]['facility_code'];
                    if ($decommissioned[$key]['last_seen'] > $last_deccommissioned_time) {
                        $last_deccommissioned_time = $decommissioned[$key]['last_seen'];
                        $days_last_decommissioned = $decommissioned[$key]['difference_in_days'];
                        // echo "<pre>".$last_order_time;
                    }
                    $final_array[$keyy]['Date Last Decommissioned'] = $last_deccommissioned_time;
                    $final_array[$keyy]['Days From Last Decommissioned'] = $days_last_decommissioned;
                    // $final_array[$i]['Days From Last Seen'] = abs($last_seen_time - $now);
                    $last_deccommissioned_time = NULL;
                }
                //end of facility code if
            }
            //end of last seen foreach
        }
        foreach ($final_array as $keyy => $value) {
            foreach ($redistributed as $key => $value) {
                if ($final_array[$keyy]['Facility Code'] == $redistributed[$key]['facility_code']) {
                    // echo "<pre>".$last_ordered[$j]['last_seen']."	".$last_ordered[$j]['facility_code'];
                    if ($redistributed[$key]['last_seen'] > $last_redistributed_time) {
                        $last_redistributed_time = $redistributed[$key]['last_seen'];
                        $days_last_redistributed = $redistributed[$key]['difference_in_days'];
                        // echo "<pre>".$last_order_time;
                    }
                    $final_array[$keyy]['Date Last Redistributed'] = $last_redistributed_time;
                    $final_array[$keyy]['Days From Last Redistributed'] = $days_last_redistributed;
                    // $final_array[$i]['Days From Last Seen'] = abs($last_seen_time - $now);
                    $last_redistributed_time = NULL;
                }
                //end of facility code if
            }
            //end of last seen foreach
        }
        foreach ($final_array as $keyy => $value) {
            foreach ($added_stock as $key => $value) {
                if ($final_array[$keyy]['Facility Code'] == $added_stock[$key]['facility_code']) {
                    // echo "<pre>".$last_ordered[$j]['last_seen']."	".$last_ordered[$j]['facility_code'];
                    if ($added_stock[$key]['last_seen'] > $last_added_stock_time) {
                        $last_added_stock_time = $added_stock[$key]['last_seen'];
                        $days_last_added_stock = $added_stock[$key]['difference_in_days'];
                        // echo "<pre>".$last_order_time;
                    }
                    $final_array[$keyy]['Date Last Received Order'] = $last_added_stock_time;
                    $final_array[$keyy]['Days From Last Received Order'] = $days_last_added_stock;
                    // $final_array[$i]['Days From Last Seen'] = abs($last_seen_time - $now);
                    $last_added_stock_time = NULL;
                }
                //end of facility code if
            }
            //end of last seen foreach
        }
        // echo "<pre>";print_r($final_array);echo "</pre>";exit;
        // echo $last_order_time;
        // exit;
        // echo "<pre>";print_r($final_array_count);echo "</pre>";exit;
        $row_data = array();
        $counterrrr = 0;
        foreach ($final_array as $facility) {
            // echo "<pre>". $counterrrr . "</pre>";
            // $counterrrr = $counterrrr + 1;
            //random code to allow for commit
            $issue_date = isset($facility['Date Last Issued']) ? date('Y-m-d', strtotime($facility['Date Last Issued'])) : "No Data Available";
            $last_seen = isset($facility['Date Last Seen']) ? date('Y-m-d', strtotime($facility['Date Last Seen'])) : "No Data Available";
            $redistribution = isset($facility['Date Last Redistributed']) ? date('Y-m-d', strtotime($facility['Date Last Redistributed'])) : "No Data Available";
            $order_date = isset($facility['Date Last Ordered']) ? date('Y-m-d', strtotime($facility['Date Last Ordered'])) : "No Data Available";
            $decommission_date = isset($facility['Date Last Decommissioned']) ? date('Y-m-d', strtotime($facility['Date Last Decommissioned'])) : "No Data Available";
            $date_order = isset($facility['Date Last Received Order']) ? date('Y-m-d', strtotime($facility['Date Last Received Order'])) : "No Data Available";
            $days_from_last_seen = isset($facility['Days From Last Seen']) ? $facility['Days From Last Seen'] : '    -    ';
            $days_from_last_issued = isset($facility['Days From Last Issue']) ? $facility['Days From Last Issue'] : '  -    ';
            $days_from_last_redist = isset($facility['Days From Last Redistributed']) ? $facility['Days From Last Redistributed'] : '    -    ';
            $days_from_last_ordered = isset($facility['Days From Last Order']) ? $facility['Days From Last Order'] : '    -  ';
            $decomissioned_days = isset($facility['Days From Last Decommissioned']) ? $facility['Days From Last Decommissioned'] : '    -    ';
            $days_from_last_recieved = isset($facility['Days From Last Received Order']) ? $facility['Days From Last Received Order'] : '    -    ';
            array_push($row_data, array($facility['Facility Name'], $facility['Facility Code'], $facility['Sub-County'], $facility['County'], $last_seen, $days_from_last_seen, $issue_date, $days_from_last_issued, $redistribution, $days_from_last_redist, $order_date, $days_from_last_ordered, $decommission_date, $decomissioned_days, $date_order, $days_from_last_recieved));
        }
        $excel_data = array();
        // $excel_data = array('doc_creator' => 'HCMP ', 'doc_title' => 'System Usage Breakdown ', 'file_name' => 'system usage breakdown');
        $excel_data = array('doc_creator' => 'HCMP-Kenya', 'doc_title' => 'HCMP_Facility_Activity_Log_Summary ', 'file_name' => 'HCMP_Facility_Activity_Log_Summary_as_at_' . $time);
        $column_data = array("Facility Name", "Facility Code", "Sub County", "County", "Date Last Logged In", "Days From Last Log In", "Date Last Issued", "Days From Last Issue", "Date Last Redistributed", "Days From Last Redistribution", "Date Last Ordered", "Days From Last Order", "Date Last Decommissioned", "Days From Last Decommission", "Date Last Received Order", "Days From Last Stock Addition");
        $excel_data['column_data'] = $column_data;
        $excel_data['row_data'] = $row_data;
        $excel_data['report_type'] = 'Log Summary';
        // echo "<pre>";print_r($excel_data);echo "</pre>";exit;
        $res = $this->hcmp_functions->create_excel($excel_data);
        // echo 'This '.$res;exit;
        $message = '';
        $message .= "<style> table {\n\t\t\t\t\t\t\t\t    border-collapse: collapse; \n\t\t\t\t\t\t\t\t}td,th{\n\t\t\t\t\t\t\t\t\tpadding: 12px;\n\t\t\t\t\t\t\t\t\ttext-align:center;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t*{margin:0;padding:0}*{font-family:'Helvetica Neue',Helvetica,Helvetica,Arial,sans-serif}img{max-width:100%}.collapse{padding:0}body{-webkit-font-smoothing:antialiased;-webkit-text-size-adjust:none;width:100%!important;height:100%}a{color:#2BA6CB}.btn{text-decoration:none;color:#FFF;background-color:#666;padding:10px 16px;font-weight:700;margin-right:10px;text-align:center;cursor:pointer;display:inline-block}p.callout{padding:15px;background-color:#ECF8FF;margin-bottom:15px}.callout a{font-weight:700;color:#2BA6CB}table.social{background-color:#ebebeb}.social .soc-btn{padding:3px 7px;font-size:12px;margin-bottom:10px;text-decoration:none;color:#FFF;font-weight:700;display:block;text-align:center}a.fb{background-color:#3B5998!important}a.tw{background-color:#1daced!important}a.gp{background-color:#DB4A39!important}a.ms{background-color:#000!important}.sidebar .soc-btn{display:block;width:100%}table.head-wrap{width:100%}.header.container table td.logo{padding:15px}.header.container table td.label{padding:15px 15px 15px 0}table.body-wrap{width:100%}table.footer-wrap{width:100%;clear:both!important}.footer-wrap .container td.content p{border-top:1px solid #d7d7d7;padding-top:15px;font-size:9px;font-weight:500}h1,h2,h3,h4,h5,h6{font-family:HelveticaNeue-Light,'Helvetica Neue Light','Helvetica Neue',Helvetica,Arial,'Lucida Grande',sans-serif;line-height:1.1;margin-bottom:15px;color:#000}h1 small,h2 small,h3 small,h4 small,h5 small,h6 small{font-size:60%;color:#6f6f6f;line-height:0;text-transform:none}h1{font-weight:200;font-size:44px}h2{font-weight:200;font-size:37px}h3{font-weight:500;font-size:27px}h4{font-weight:500;font-size:23px}h5{font-weight:900;font-size:17px}h6{font-weight:900;font-size:14px;text-transform:uppercase;color:#444}.collapse{margin:0!important}p,ul{margin-bottom:10px;font-weight:400;font-size:14px;line-height:1.6}p.lead{font-size:17px}p.last{margin-bottom:0}ul li{margin-left:5px;list-style-position:inside}ul.sidebar{background:#ebebeb;display:block;list-style-type:none}ul.sidebar li{display:block;margin:0}ul.sidebar li a{text-decoration:none;color:#666;padding:10px 16px;cursor:pointer;border-bottom:1px solid #777;border-top:1px solid #FFF;display:block;margin:0}ul.sidebar li a.last{border-bottom-width:0}ul.sidebar li a h1,ul.sidebar li a h2,ul.sidebar li a h3,ul.sidebar li a h4,ul.sidebar li a h5,ul.sidebar li a h6,ul.sidebar li a p{margin-bottom:0!important}.container{display:block!important;max-width:100%!important;margin:0 auto!important;clear:both!important}.content{padding:15px;max-width:80%px;margin:0 auto;display:block}.content table{width:100%}.column{width:300px;float:left}.column tr td{padding:15px}.column-wrap{padding:0!important;margin:0 auto;max-width:600px!important}.column table{width:100%}.social .column{width:280px;min-width:279px;float:left}.clear{display:block;clear:both}@media only screen and (max-width:600px){a[class=btn]{display:block!important;margin-bottom:10px!important;background-image:none!important;margin-right:0!important}div[class=column]{width:auto!important;float:none!important}table.social div[class=column]{width:auto!important}}</style>";
        $message .= '
										<tr>
										<td colspan="12">
										</tr>
										</tbody>
										</table>';
        $message .= "<!-- BODY -->\n\t\t\t\t\t\t\t\t<table class='body-wrap'>\n\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t\t\t\t<td class='container' bgcolor='#FFFFFF'>\n\n\t\t\t\t\t\t\t\t\t\t\t<div class='content'>\n\t\t\t\t\t\t\t\t\t\t\t<table>\n\t\t\t\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t<p class='lead'>Find attached a summary of Facility Activity Log, as at {$time}</p>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t<table class='social' width='100%'>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<!-- column 1 -->\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<table align='left' class='column'>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</table><!-- /column 1 -->\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<!-- column 2 -->\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<table align='left' class='column'>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</table><!-- /column 2 -->\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<span class='clear'></span>\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t</table><!-- /social & contact -->\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t\t\t\t\t</div><!-- /content -->\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t\t\t\t<td></td>\n\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t</table><!-- /BODY -->";
        $handler = "./print_docs/excel/excel_files/" . $excel_data['file_name'] . ".xls";
        $subject = "Weekly Log Summary as at " . $time;
        $email_address = "smutheu@clintonhealthaccess.org,jaynerawz@gmail.com,karsanrichard@gmail.com,ttunduny@gmail.com";
        // $email_address = "karsanrichard@gmail.com,ttunduny@gmail.com";
        // $email_address = "*****@*****.**";
        //$bcc = "";
        $this->hcmp_functions->send_email($email_address, $message, $subject, $handler);
        redirect('sms/log_summary_weekly_view');
        /*echo "<table class='data-table'>
        		<thead>
        		<tr>
        			<th ><b>Facility Name</b></th>
        			<th ><b>Facility Code</b></th>
        			<th ><b>County</b></th>
        			<th ><b>Sub-County</b></th>
        			<th ><b>Date Last Issued</b></th>
        			<th ><b>Days from last issue</b></th>
        			<th ><b>Date Last Redistributed</b></th>
        			<th ><b>Days From last Redistributed</b></th>
        			<th ><b>Date Last ordered</b></th>
        			<th ><b>Days From Last order</b></th>
        			<th ><b>Date Last Decommissioned</b></th>
        			<th ><b>Days From Last Decommissioned</b></th>
        			<th ><b>Date Last Seen</b></th>
        			<th ><b>Days From Last Seen</b></th>
        		</tr> 
        		</thead><tbody>";
               foreach ($finalArray as $key => $value) {
               	
                   echo '<tr><td>'.$value['facility_name'].'</td>';
        		   echo '<td>'.$value['facility_code'].'</td>';
        		   echo '<td>'.$value['county'].'</td>';
        		   echo '<td>'.$value['district'].'</td>';
        		   echo '<td>'.date('Y-m-d',strtotime($value['issue_event'])).'</td>';
        		   echo '<td>'.$value['issue_d'].'</td>';
        		   echo '<td>'.$value['redistribute_event'].'</td>';
        		   echo '<td>'.$value['redistribute_d'].'</td>';
        		   echo '<td>'.$value['ordered_event'].'</td>';
        		   echo '<td>'.$value['ordered_d'].'</td>';
        		   echo '<td>'.$value['decommissioned_event'].'</td>';
        		   echo '<td>'.$value['decommissioned_d'].'</td>';
        		   echo '<td>'.$value['date_event'].'</td>';
        		   echo '<td>'.$value['date_event_d'].'</td></tr>';
               }
               echo '</tbody></table>';*/
    }