<?php
/**
 * Created by PhpStorm.
 * User: sunusi
 * Date: 2019-02-09
 * Time: 12:48
 */

//fetching the MONTHLY patientS record
$currentYear = date("Y");
$currentMonth = date("m");
$currentMonthFull = date("F");
$currentDay = date("d");

//TODO: delete after testing
//$currentYear = "2018";
//$currentMonth = "01";
//$currentMonthFull = "02";
//$currentDay = "01";

//fetching the patients all records
$patients = $query->getAll("
  SELECT udut_patients.*,
         udut_patients_types.*
  FROM udut_patients
  JOIN udut_patients_types
  ON udut_patients.patientTypeId = udut_patients_types.patientTypeId
");

//fetching patients this month
$patientsThisMonth = $query->getAll("
  SELECT udut_patients.*,
         udut_patients_types.*
  FROM udut_patients
  JOIN udut_patients_types
  ON udut_patients.patientTypeId = udut_patients_types.patientTypeId
  WHERE udut_patients.patientDateAdded LIKE '%" . $currentYear . "-" . $currentMonth . "%' 
");

// -------------------------------------------------------
//generating payments and services
$payments = $query->getAll("
    SELECT udut_payments.*,
           udut_payments_types.*,
           udut_payments_status.*,
           udut_visits.*,
           udut_scan_types.*
    FROM udut_payments
    JOIN udut_payments_types
    ON udut_payments.paymentTypeId = udut_payments_types.paymentTypeId  
    JOIN udut_payments_status
    ON udut_payments.paymentStatusId = udut_payments_status.paymentStatusId
    JOIN udut_visits
    ON udut_visits.paymentId = udut_payments.paymentId
    JOIN udut_scan_types
    ON udut_payments.typeId = udut_scan_types.typeId
");

//generating payments THIS MONTH
$paymentsThisMonth = $query->getAll("
    SELECT udut_payments.*,
           udut_payments_types.*,
           udut_payments_status.*,
           udut_visits.*,
           udut_scan_types.*
    FROM udut_payments
    JOIN udut_payments_types
    ON udut_payments.paymentTypeId = udut_payments_types.paymentTypeId  
    JOIN udut_payments_status
    ON udut_payments.paymentStatusId = udut_payments_status.paymentStatusId
    JOIN udut_visits
    ON udut_visits.paymentId = udut_payments.paymentId
    JOIN udut_scan_types
    ON udut_payments.typeId = udut_scan_types.typeId
    WHERE udut_payments.paymentDateAdded LIKE '%" . $currentYear . "-" . $currentMonth . "%' 
");
// -------------------------------------------------------

//total amount of money generated per month
$amountThisMonth = 0;
foreach ($paymentsThisMonth as $amount) {
    $amountThisMonth += $amount["typeAmount"];
}

$amountThisMonth = number_format($amountThisMonth);


$PJan = $query->getAll("SELECT * FROM udut_patients WHERE patientDateAdded LIKE '%" . $currentYear . "-01%' ");
$PFeb = $query->getAll("SELECT * FROM udut_patients WHERE patientDateAdded LIKE '%" . $currentYear . "-02%' ");
$PMar = $query->getAll("SELECT * FROM udut_patients WHERE patientDateAdded LIKE '%" . $currentYear . "-03%' ");
$PApr = $query->getAll("SELECT * FROM udut_patients WHERE patientDateAdded LIKE '%" . $currentYear . "-04%' ");
$PMay = $query->getAll("SELECT * FROM udut_patients WHERE patientDateAdded LIKE '%" . $currentYear . "-05%' ");
$PJun = $query->getAll("SELECT * FROM udut_patients WHERE patientDateAdded LIKE '%" . $currentYear . "-06%' ");
$PJul = $query->getAll("SELECT * FROM udut_patients WHERE patientDateAdded LIKE '%" . $currentYear . "-07%' ");
$PAug = $query->getAll("SELECT * FROM udut_patients WHERE patientDateAdded LIKE '%" . $currentYear . "-08%' ");
$PSep = $query->getAll("SELECT * FROM udut_patients WHERE patientDateAdded LIKE '%" . $currentYear . "-09%' ");
$POct = $query->getAll("SELECT * FROM udut_patients WHERE patientDateAdded LIKE '%" . $currentYear . "-10%' ");
$PNov = $query->getAll("SELECT * FROM udut_patients WHERE patientDateAdded LIKE '%" . $currentYear . "-11%' ");
$PDec = $query->getAll("SELECT * FROM udut_patients WHERE patientDateAdded LIKE '%" . $currentYear . "-12%' ");

//statistics
//patients stats data
$patientStatsData = "
         [
            {
                y: 'Jan',
                a: " . count($PJan) . "
            }, {
                y: 'Feb',
                a: " . count($PFeb) . "
            }, {
                y: 'Mar',
                a: " . count($PMar) . "
            }, {
                y: 'Apr',
                a: " . count($PApr) . "
            }, {
                y: 'May',
                a: " . count($PMay) . "
            }, {
                y: 'Jun',
                a: " . count($PJun) . "
            }, {
                y: 'Jul',
                a: " . count($PJul) . "
            }, {
                y: 'Aug',
                a: " . count($PAug) . "
            }, {
                y: 'Sep',
                a: " . count($PSep) . "
            }, {
                y: 'Oct',
                a: " . count($POct) . "
            }, {
                y: 'Nov',
                a: " . count($PNov) . "
            }, {
                y: 'Dec',
                a: " . count($PDec) . "
            }
            
         ]";

$RJan = $query->getAll("
  SELECT udut_payments.*,
         udut_scan_types.*
  FROM udut_payments 
  JOIN udut_scan_types
  ON udut_payments.typeId = udut_scan_types.typeId
  WHERE udut_payments.paymentDateAdded LIKE '%" . $currentYear . "-01%' 
");
$RFeb = $query->getAll("
  SELECT udut_payments.*,
         udut_scan_types.*
  FROM udut_payments 
  JOIN udut_scan_types
  ON udut_payments.typeId = udut_scan_types.typeId
  WHERE udut_payments.paymentDateAdded LIKE '%" . $currentYear . "-02%' 
");
$RMar = $query->getAll("
  SELECT udut_payments.*,
         udut_scan_types.*
  FROM udut_payments 
  JOIN udut_scan_types
  ON udut_payments.typeId = udut_scan_types.typeId
  WHERE udut_payments.paymentDateAdded LIKE '%" . $currentYear . "-03%' 
");
$RApr = $query->getAll("
  SELECT udut_payments.*,
         udut_scan_types.*
  FROM udut_payments 
  JOIN udut_scan_types
  ON udut_payments.typeId = udut_scan_types.typeId
  WHERE udut_payments.paymentDateAdded LIKE '%" . $currentYear . "-04%' 
");
$RMay = $query->getAll("
  SELECT udut_payments.*,
         udut_scan_types.*
  FROM udut_payments 
  JOIN udut_scan_types
  ON udut_payments.typeId = udut_scan_types.typeId
  WHERE udut_payments.paymentDateAdded LIKE '%" . $currentYear . "-05%' 
");
$RJun = $query->getAll("
  SELECT udut_payments.*,
         udut_scan_types.*
  FROM udut_payments 
  JOIN udut_scan_types
  ON udut_payments.typeId = udut_scan_types.typeId
  WHERE udut_payments.paymentDateAdded LIKE '%" . $currentYear . "-06%' 
");
$RJul = $query->getAll("
  SELECT udut_payments.*,
         udut_scan_types.*
  FROM udut_payments 
  JOIN udut_scan_types
  ON udut_payments.typeId = udut_scan_types.typeId
  WHERE udut_payments.paymentDateAdded LIKE '%" . $currentYear . "-07%' 
");
$RAug = $query->getAll("
  SELECT udut_payments.*,
         udut_scan_types.*
  FROM udut_payments 
  JOIN udut_scan_types
  ON udut_payments.typeId = udut_scan_types.typeId
  WHERE udut_payments.paymentDateAdded LIKE '%" . $currentYear . "-08%' 
");
$RSep = $query->getAll("
  SELECT udut_payments.*,
         udut_scan_types.*
  FROM udut_payments 
  JOIN udut_scan_types
  ON udut_payments.typeId = udut_scan_types.typeId
  WHERE udut_payments.paymentDateAdded LIKE '%" . $currentYear . "-09%' 
");
$ROct = $query->getAll("
  SELECT udut_payments.*,
         udut_scan_types.*
  FROM udut_payments 
  JOIN udut_scan_types
  ON udut_payments.typeId = udut_scan_types.typeId
  WHERE udut_payments.paymentDateAdded LIKE '%" . $currentYear . "-10%' 
");
$RNov = $query->getAll("
  SELECT udut_payments.*,
         udut_scan_types.*
  FROM udut_payments 
  JOIN udut_scan_types
  ON udut_payments.typeId = udut_scan_types.typeId
  WHERE udut_payments.paymentDateAdded LIKE '%" . $currentYear . "-11%' 
");
$RDec = $query->getAll("
  SELECT udut_payments.*,
         udut_scan_types.*
  FROM udut_payments 
  JOIN udut_scan_types
  ON udut_payments.typeId = udut_scan_types.typeId
  WHERE udut_payments.paymentDateAdded LIKE '%" . $currentYear . "-12%' 
");

$JanAmount = 0;
$FebAmount = 0;
$MarAmount = 0;
$AprAmount = 0;
$MayAmount = 0;
$JunAmount = 0;
$JulAmount = 0;
$AugAmount = 0;
$SepAmount = 0;
$OctAmount = 0;
$NovAmount = 0;
$DecAmount = 0;

foreach($RJan as $jan){  $JanAmount += $jan['typeAmount']; }
foreach($RFeb as $feb){  $FebAmount += $feb['typeAmount']; }
foreach($RMar as $mar){  $MarAmount += $mar['typeAmount']; }
foreach($RApr as $apr){  $AprAmount += $apr['typeAmount']; }
foreach($RMay as $may){  $MayAmount += $may['typeAmount']; }
foreach($RJun as $jun){  $JunAmount += $jun['typeAmount']; }
foreach($RJul as $jul){  $JulAmount += $jul['typeAmount']; }
foreach($RAug as $aug){  $AugAmount += $aug['typeAmount']; }
foreach($RSep as $sep){  $SepAmount += $sep['typeAmount']; }
foreach($ROct as $oct){  $OctAmount += $oct['typeAmount']; }
foreach($RNov as $nov){  $NovAmount += $nov['typeAmount']; }
foreach($RDec as $dec){  $DecAmount += $dec['typeAmount']; }



$revenueStatData = "
         [
            { Month: 'Jan', m: " . $JanAmount . " },
            { Month: 'Feb', m: " . $FebAmount . " },
            { Month: 'Mar', m: " . $MarAmount . " },
            { Month: 'Apr', m: " . $AprAmount . " },
            { Month: 'May', m: " . $MayAmount . " },
            { Month: 'Jun', m: " . $JunAmount . " },
            { Month: 'Jul', m: " . $JulAmount . " },
            { Month: 'Aug', m: " . $AugAmount . " },
            { Month: 'Sep', m: " . $SepAmount . " },
            { Month: 'Oct', m: " . $OctAmount . " },
            { Month: 'Nov', m: " . $NovAmount . " },
            { Month: 'Dec', m: " . $DecAmount . " }
         ]
";

//print("<pre>");
//print_r($revenueStatData);
//print("</pre>");