public function getNationalCoverage($vaccine) {
		$months_of_stock = array();
		$year = date('Y');
		$now = date('U');
		$to = date("U", mktime(0, 0, 0, 1, 1, date("Y") + 1));
		$from = date("U", mktime(0, 0, 0, 1, 1, date('Y')));
		//Get National Data
		$population = regional_populations::getNationalPopulation($year);
		$population = str_replace(",", "", $population);
		$vaccine_object = Vaccines::getVaccine($vaccine);
		$yearly_requirement = ceil(($vaccine_object -> Doses_Required * $population * $vaccine_object -> Wastage_Factor));
		$stock_receipts =	Disbursements::getNationalReceiptsTotals($vaccine, $from, $to); 
		$percentage_coverage = ceil(($stock_receipts/$yearly_requirement)*100); 
		$chart = '
<chart caption="Estimated Coverage" palette="4" numberSuffix="%" decimals="0" enableSmartLabels="1" enableRotation="0"   bgAlpha="40,100" bgRatio="0,100" bgAngle="360" showBorder="0" startingAngle="70">
<set label="Covered" value="' . $percentage_coverage . '" isSliced="1"/>
<set label="Pending" value="' . (100-$percentage_coverage) . '"/>

</chart>';   
		echo $chart;
	}
 public function view_report($report = "consumption")
 {
     $data['title'] = "System Reports";
     $data['content_view'] = "reports_view";
     if ($report == "consumption") {
         redirect("consumption_management");
     }
     if ($report == "issues") {
         redirect("vaccine_issues_management");
     }
     if ($report == "district_stock_outs") {
         redirect("district_stock_out");
     }
     //Code for getting the Store summaries at the various vaccine store around the country
     if ($report == "store_summaries") {
         $data['quick_link'] = "store_summaries";
         $regions = Regions::getAllRegions();
         $vaccines = Vaccines::getAll_Minified();
         $national_values = array();
         $regional_values = array();
         $this_month = date('m');
         for ($month = 1; $month <= $this_month; $month++) {
             foreach ($vaccines as $vaccine) {
                 $timestamp = date("U", mktime(0, 0, 0, $month, 1, date("Y")));
                 $national_balance = Disbursements::getNationalPeriodBalance($vaccine->id, $timestamp);
                 $national_values[$month][$vaccine->id] = $national_balance;
                 //Get the balance at that time
                 foreach ($regions as $region) {
                     $regional_balance = Disbursements::getRegionalPeriodBalance($region->id, $vaccine->id, $timestamp);
                     $regional_values[$month][$region->id][$vaccine->id] = $regional_balance;
                     //Get the Balance at that time
                 }
             }
         }
         $data['national_values'] = $national_values;
         $data['regional_values'] = $regional_values;
         $data['regional_stores'] = $regions;
     } else {
         if ($report == "store_tallies") {
             $post = $this->input->post();
             if ($post) {
                 $login_level = $this->session->userdata('user_group');
                 $vaccines = Vaccines::getAll_Minified();
                 $from = strtotime($this->input->post('from'));
                 $to = strtotime($this->input->post('to'));
                 $this->session->set_userdata(array("store_tallies_from" => $from, "store_tallies_to" => $to));
                 $items = 20;
                 $order_by = "Quantity";
                 $order = "DESC";
                 $offset = 0;
                 $district_or_region_id = $this->session->userdata('district_province_id');
                 $tallies = null;
                 if ($login_level == 1) {
                     //National Level
                     foreach ($vaccines as $vaccine) {
                         $tallies[$vaccine->id] = Disbursements::getNationalRecipientTally($vaccine->id, $from, $to, $offset, $items, $order_by, $order);
                     }
                 }
                 if ($login_level == 2) {
                     //Regional Level
                     foreach ($vaccines as $vaccine) {
                         $tallies[$vaccine->id] = Disbursements::getRegionalRecipientTally($district_or_region_id, $vaccine->id, $from, $to, $offset, $items, $order_by, $order);
                     }
                 }
                 $data['tallies'] = $tallies;
             }
             $data['quick_link'] = "store_tallies";
         } else {
             if ($report == "vaccine_movement") {
                 $post = $this->input->post();
                 if ($post) {
                     $login_level = $this->session->userdata('user_group');
                     $vaccines = Vaccines::getAll_Minified();
                     $from = strtotime($this->input->post('from'));
                     $to = strtotime($this->input->post('to'));
                     $items = 20;
                     $order_by = "Quantity";
                     $order = "DESC";
                     $offset = 0;
                     $district_or_region_id = $this->session->userdata('district_province_id');
                     $received = null;
                     $issued = null;
                     $beginning_balance = null;
                     $current_balance = null;
                     if ($login_level == 1) {
                         //National Level
                         foreach ($vaccines as $vaccine) {
                             $received[$vaccine->id] = Disbursements::getNationalReceiptsTotals($vaccine->id, $from, $to);
                             $issued[$vaccine->id] = Disbursements::getNationalIssuesTotals($vaccine->id, $from, $to);
                             $beginning_balance[$vaccine->id] = Disbursements::getNationalPeriodBalance($vaccine->id, $from);
                             $current_balance[$vaccine->id] = Disbursements::getNationalPeriodBalance($vaccine->id, $to);
                         }
                     }
                     if ($login_level == 2) {
                         //Regional Level
                         /*foreach($vaccines as $vaccine){
                         	 $tallies[$vaccine->id] = Disbursements::getRegionalRecipientTally($district_or_region_id,$vaccine->id,$from,$to,$offset,$items,$order_by,$order);
                         	 }*/
                     }
                     $data['received'] = $received;
                     $data['issued'] = $issued;
                     $data['beginning_balance'] = $beginning_balance;
                     $data['current_balance'] = $current_balance;
                 }
                 $data['quick_link'] = "vaccine_movement";
             }
         }
     }
     $data['report'] = $report;
     $this->base_params($data);
 }
 function create_national_report()
 {
     $year = date('Y');
     $headers = "Summary Report for Vaccine Status in Kenya\n\t\nDepot: National Store\tReporting Date: " . date("d/m/Y") . "\t\n";
     $data = "Analytical Areas\t";
     $vaccines = Vaccines::getAll();
     $from = date("U", mktime(0, 0, 0, 1, 1, date('Y')));
     //This sets the begining date as the 1st of january of that particular year
     $to = date('U');
     //This sets the end date as the current time when the report is being generated
     //Loop all vaccines and append the vaccine name in the excel sheet content.
     foreach ($vaccines as $vaccine) {
         $data .= $vaccine->Name . "\t";
     }
     $data .= "\n";
     //New Line!
     //Begin adding data for the areas being analysed!
     $data .= "Annual Needs Coverage\t";
     //Loop all vaccines and append the needs coverage for that particular vaccine in that store
     foreach ($vaccines as $vaccine) {
         $population = Regional_Populations::getNationalPopulation($year);
         $yearly_requirement = $population * $vaccine->Doses_Required * $vaccine->Wastage_Factor;
         $vaccine_totals = Disbursements::getNationalReceiptsTotals($vaccine->id, $from, $to);
         $coverage = ceil($vaccine_totals / $yearly_requirement * 100);
         $data .= $coverage . "%\t";
     }
     $data .= "\n";
     //New Line
     $data .= "Stock Availability (Stock at Hand)\t";
     //Loop all vaccines and append the stock at hand for that particular vaccine in that store
     foreach ($vaccines as $vaccine) {
         $stock_at_hand = Disbursements::getNationalPeriodBalance($vaccine->id, $to);
         $data .= $stock_at_hand . "\t";
     }
     $data .= "\n";
     //New Line
     $data .= "Stock at Hand Forecast (In Months)\t";
     //Loop all vaccines and append the stock at hand forecast for that particular vaccine in that store
     foreach ($vaccines as $vaccine) {
         $population = Regional_Populations::getNationalPopulation($year);
         $population = str_replace(",", "", $population);
         $monthly_requirement = ceil($vaccine->Doses_Required * $population * $vaccine->Wastage_Factor / 12);
         $stock_at_hand = Disbursements::getNationalPeriodBalance($vaccine->id, $to);
         $forecast = $stock_at_hand / $monthly_requirement;
         $data .= $forecast . "\t";
     }
     $data .= "\n";
     //New Line
     $data .= "Shipments Expected Dates\t";
     //Loop all vaccines and append the shipments expected for that particular vaccine in that store
     foreach ($vaccines as $vaccine) {
         $plans = Provisional_Plan::getYearlyPlan($year, $vaccine->id);
         $plans_string = "";
         foreach ($plans as $plan) {
             $plans_string .= $plan->expected_date . " (" . $plan->expected_amount . ") ";
         }
         if (strlen($plans_string) < 1) {
             $plans_string = "None";
         }
         $data .= $plans_string . "\t";
     }
     $data .= "\n";
     //New Line
     $data .= "Shipments received Dates\t";
     //Loop all vaccines and append the shipments received for that particular vaccine in that store
     foreach ($vaccines as $vaccine) {
         $receipts = Batches::getYearlyReceipts($year, $vaccine->id);
         $receipts_string = "";
         foreach ($receipts as $receipt) {
             $receipts_string .= $receipt->Arrival_Date . " (" . $receipt->Total . ") ";
         }
         if (strlen($receipts_string) < 1) {
             $receipts_string = "None";
         }
         $data .= $receipts_string . "\t";
     }
     $data .= "\n";
     //New Line
     /*header("Content-type: application/vnd.ms-excel; name='excel'");
     	 header("Content-Disposition: filename=Country_Vaccine_Status_Summary.xls");
     	 // Fix for crappy IE bug in download.
     	 header("Pragma: ");
     	 header("Cache-Control: ");*/
     $result = $headers . $data;
     return $result;
 }
 function create_national_report()
 {
     $year = date('Y');
     $total_vaccines = Vaccines::getTotalNumber();
     $total_vaccines *= 2;
     $html = "<table border='2px solid black'>";
     $html .= "<tr ><th rowspan=3>Analytical Areas</th><th style='text-align: center' colspan=" . $total_vaccines . ">Summary Report for Vaccine Status in Kenya</th></tr>";
     $html .= "<tr ><th style='text-align: center' colspan=" . $total_vaccines . ">Depot: National Store Reporting Date: " . date("d/m/Y") . "</th></tr>";
     $headers = "Summary Report for Vaccine Status in Kenya\n\t\nDepot: National Store\tReporting Date: " . date("d/m/Y") . "\t\n";
     $data = "Analytical Areas\t";
     $vaccines = Vaccines::getAll();
     $from = date("U", mktime(0, 0, 0, 1, 1, date('Y')));
     //This sets the begining date as the 1st of january of that particular year
     $to = date('U');
     //This sets the end date as the current time when the report is being generated
     //Loop all vaccines and create a table data element for it
     $html .= "<tr>";
     foreach ($vaccines as $vaccine) {
         $html .= "<td colspan=2 style='background-color:#" . $vaccine->Tray_Color . "'>" . $vaccine->Name . "</td>";
     }
     $html .= "</tr>";
     //New Line!
     //Begin adding data for the areas being analysed!
     $html .= "<tr><td class='title'>Annual Needs Coverage</td>";
     //Loop all vaccines and append the needs coverage for that particular vaccine in that store
     foreach ($vaccines as $vaccine) {
         $population = Regional_Populations::getNationalPopulation($year);
         $yearly_requirement = $population * $vaccine->Doses_Required * $vaccine->Wastage_Factor;
         $vaccine_totals = Disbursements::getNationalReceiptsTotals($vaccine->id, $from, $to);
         $coverage = ceil($vaccine_totals / $yearly_requirement * 100);
         $html .= "<td colspan=2>" . $coverage . "%</td>";
     }
     $html .= "</tr>";
     $html .= "<tr><td class='title'>Number of Days of Stock Outage</td>";
     //Loop all vaccines and append the needs coverage for that particular vaccine in that store
     foreach ($vaccines as $vaccine) {
         $html .= "<td colspan=2>N/A</td>";
     }
     $html .= "</tr>";
     //New Line
     $html .= "<tr><td class='title'>Stock Availability (Stock at Hand)</td>";
     //Loop all vaccines and append the stock at hand for that particular vaccine in that store
     foreach ($vaccines as $vaccine) {
         $stock_at_hand = Disbursements::getNationalPeriodBalance($vaccine->id, $to);
         $html .= "<td colspan=2>" . $stock_at_hand . "</td>";
     }
     $html .= "</tr>";
     //New Line
     $html .= "<tr><td class='title'>Stock at Hand Forecast (In Months)</td>";
     //Loop all vaccines and append the stock at hand forecast for that particular vaccine in that store
     foreach ($vaccines as $vaccine) {
         $population = Regional_Populations::getNationalPopulation($year);
         $population = str_replace(",", "", $population);
         $monthly_requirement = ceil($vaccine->Doses_Required * $population * $vaccine->Wastage_Factor / 12);
         $stock_at_hand = Disbursements::getNationalPeriodBalance($vaccine->id, $to);
         $forecast = $stock_at_hand / $monthly_requirement;
         $forecast = number_format($forecast, 2, '.', '');
         $html .= "<td colspan=2>" . $forecast . "</td>";
     }
     $html .= "</tr>";
     //New Line
     $html .= "<tr><td class='title'>Shipments Expected/Received Dates</td>";
     //Loop all vaccines and append the shipments expected for that particular vaccine in that store
     foreach ($vaccines as $vaccine) {
         //Get and display the expected dates
         $plans = Provisional_Plan::getYearlyPlan($year, $vaccine->id);
         $plans_string = "";
         $html .= "<td><table>";
         foreach ($plans as $plan) {
             $plans_string = $plan->expected_date . " (" . $plan->expected_amount . ") ";
             $html .= "<tr><td class='no_border'>" . $plans_string . "</td></tr>";
         }
         if (strlen($plans_string) < 1) {
             $plans_string = "None";
             $html .= "<tr><td class='no_border'>" . $plans_string . "</td></tr>";
         }
         $html .= "</table></td>";
         $receipts = Batches::getYearlyReceipts($year, $vaccine->id);
         $receipts_string = "";
         $html .= "<td><table>";
         foreach ($receipts as $receipt) {
             $receipts_string = $receipt->Arrival_Date . " (" . $receipt->Total . ") ";
             $html .= "<tr><td class='no_border'>" . $receipts_string . "</td></tr>";
         }
         if (strlen($receipts_string) < 1) {
             $receipts_string = "None";
             $html .= "<tr><td class='no_border'>" . $receipts_string . "</td></tr>";
         }
         $html .= "</table></td>";
     }
     $html .= "</tr>";
     //New Line
     //New Line
     /*header("Content-type: application/vnd.ms-excel; name='excel'");
     	 header("Content-Disposition: filename=Country_Vaccine_Status_Summary.xls");
     	 // Fix for crappy IE bug in download.
     	 header("Pragma: ");
     	 header("Cache-Control: ");*/
     $result = $headers . $data;
     $html .= "</table>";
     return $html;
 }