<?php 

// Include main file
include_once('../libs/main.php');

// Check admin Login
include_once('checkLogin.php');


/*$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();

$filename=basename($_SERVER['SCRIPT_FILENAME']);

if($filename == 'adminlist.php')
{
    $module_id = $adminlogObj->getModuleId($filename);
    $page_id = $adminlogObj->getPageId($filename);
    $allpages = $modulepagesObj->fromId($page_id);
    $pagename = $allpages->page;
}*/

if (isset($_REQUEST['act']) && $_REQUEST['act']=='load'){

    // $records = $_REQUEST['length'];
    // $start=0;
    // if(isset($_GET['start']) && $_GET['start']!='0'){
    //     $page = $_GET['start'];
    // }
    // $start=($page-1)*$limit;
    // $limit = array('start'=>$_GET['start'],'record'=>$records);
    
    // $where = " 1=1 ";

    
    
    // if(isset($_REQUEST['search_input']) && $_REQUEST['search_input']!=''){
    // $where = " ( clinic_name LIKE '".$_REQUEST['search_input']."%' OR clinic_name LIKE '".$_REQUEST['search_input']."%' )";
    // }
    
    // //$orderFields = array('','admin_fname','admin_email_address','admin_type','admin_created_date','admin_last_login_date');    
    
    // // Array of order by 
    // $orderby=array();
    // if (isset($_REQUEST['order']['0']['column']) && $_REQUEST['order']['0']['column']!=""){
    //     $orderby = array(" ".$orderFields[$_REQUEST['order']['0']['column']]." ".$_REQUEST['order']['0']['dir']);
    // }else{
    //     $orderby = array(" clinic_id asc ");
    // }
    // $pointmasterObj = new pointmaster();
    $agencyclinic_pointObj = new agencyclinic_point();
    $fields =array('DISTINCT a.clinic_id as clinicId','c.clinic_name as name');
    $tables = array('tbl_clinic c');
    $jfields = array('c.clinic_id = a.clinic_id');
    $jtype   = array("INNER"); 
    $orderBy = array(" clinic_name asc ");
    $arg =array('fields'=>$fields,'joinTables' =>$tables,'joinFields' =>$jfields,'joinTypes' => $jtype,'orderBy'=>$orderby);
    $allClinic =$agencyclinic_pointObj->getAllRecords($arg);

    // //print_r($orderby);exit;
    // $data    = array('where' => $where,'limit' => $limit, 'orderBy'=>$orderby);
    // $pointmasterDetails = $pointmasterObj->getAllRecords($data);
    // //print_r($adminDetails);exit;
    $recordsTotal   =   count($allClinic);

    if ($allClinic && count($allClinic)>0){
        foreach ($allClinic as $key => $clinic){
            $clinicName  =$clinic->name;
            $clinicId =$clinic->clinicId;
            
            $agencyclinic_pointObj = new agencyclinic_point();
            $fields =array('SUM(points) as total');
            $where = array('clinic_id=' => $clinicId,'type='=>'Credited');
            $arg =array('fields'=>$fields,'where'=>$where);
            $totalCredit = $agencyclinic_pointObj->fromFields($arg);
            $totalCredit =$totalCredit->total;
                
            $where = array('clinic_id=' => $clinicId,'type='=>'Debited');
            $arg =array('fields'=>$fields,'where'=>$where);
            $totalDebit = $agencyclinic_pointObj->fromFields($arg);
            $totalDebit = $totalDebit->total;


            $loyalty_actionObj = new loyalty_action();
            $fields =array('SUM(loyalty_action_rpoints) as total');
            $where =array('loyalty_action_clinic_id='=>$clinicId);
            $arg =array('fields'=>$fields,'where'=>$where);
            $spentPoints =$loyalty_actionObj->fromFields($arg);
            $spentPoints =$spentPoints->total;

            $remainigPoints =$totalCredit-$totalDebit;

            $where =array('loyalty_action_clinic_id='=>$clinicId,'loyalty_action_status='=>"Allocated");
            $arg =array('fields'=>$fields,'where'=>$where);
            $allocatedPoints =$loyalty_actionObj->fromFields($arg);
            $allocatedPoints =$allocatedPoints->total;

            $where =array('loyalty_action_clinic_id='=>$clinicId,'loyalty_action_status='=>"Earned");
            $arg =array('fields'=>$fields,'where'=>$where);
            $earnedPoints =$loyalty_actionObj->fromFields($arg);
            $earnedPoints =$earnedPoints->total;

            $where =array('loyalty_action_clinic_id='=>$clinicId,'loyalty_action_status='=>"Redeemed");
            $arg =array('fields'=>$fields,'where'=>$where);
            $redeemedPoints =$loyalty_actionObj->fromFields($arg);
            $redeemedPoints =$redeemedPoints->total;

            $balancePoints =$allocatedPoints -$redeemedPoints;
            $marginPoints =$spentPoints - $allocatedPoints;

            $response[] = array(
                    $clinicName,
                    $totalCredit,
                    $totalDebit ,
                    $spentPoints,
                    $remainigPoints,
                    $allocatedPoints,
                    $earnedPoints,
                    $redeemedPoints,
                    $balancePoints,
                    $marginPoints
             );

        }

        $agencyclinic_pointObj = new agencyclinic_point();
        $fields =array('SUM(points) as total');
        $where = array('type='=>'Credited');
        $arg =array('fields'=>$fields,'where'=>$where);
        $totalCredit = $agencyclinic_pointObj->fromFields($arg);
        $totalCredit =$totalCredit->total;
            
        $where = array('type='=>'Debited');
        $arg =array('fields'=>$fields,'where'=>$where);
        $totalDebit = $agencyclinic_pointObj->fromFields($arg);
        $totalDebit = $totalDebit->total;


        $loyalty_actionObj = new loyalty_action();
        $fields =array('SUM(loyalty_action_rpoints) as total');
        $where ="loyalty_action_clinic_id!='0' ";
        $arg =array('fields'=>$fields,'where'=>$where);
        $spentPoints =$loyalty_actionObj->fromFields($arg);
        $spentPoints =$spentPoints->total;

        $remainigPoints =$totalCredit-$totalDebit;

        $where =array('loyalty_action_clinic_id!='=>0,'loyalty_action_status='=>"Allocated");
        $arg =array('fields'=>$fields,'where'=>$where);
        $allocatedPoints =$loyalty_actionObj->fromFields($arg);
        $allocatedPoints =$allocatedPoints->total;

        $where =array('loyalty_action_clinic_id!='=>0,'loyalty_action_status='=>"Earned");
        $arg =array('fields'=>$fields,'where'=>$where);
        $earnedPoints =$loyalty_actionObj->fromFields($arg);
        $earnedPoints =$earnedPoints->total;

        $where =array('loyalty_action_clinic_id!='=>0,'loyalty_action_status='=>"Redeemed");
        $arg =array('fields'=>$fields,'where'=>$where);
        $redeemedPoints =$loyalty_actionObj->fromFields($arg);
        $redeemedPoints =$redeemedPoints->total;

        $balancePoints =$allocatedPoints -$redeemedPoints;
        $marginPoints =$spentPoints - $allocatedPoints;

        $response[] = array(
                "<b>Totals</b>",
                "<b>".$totalCredit."</b>",
                "<b>".$totalDebit."</b>",
                "<b>".$spentPoints."</b>",
                "<b>".$remainigPoints."</b>",
                "<b>".$allocatedPoints."</b>",
                "<b>".$earnedPoints."</b>",
                "<b>".$redeemedPoints."</b>",
                "<b>".$balancePoints."</b>",
                "<b>".$marginPoints."</b>"
         );
        $response[] = array(
                "<b>Value</b>",
                ($totalCredit!=0)?"<b>$".$totalCredit/100:'0'."</b>",
                ($totalDebit!=0)?"<b>$".$totalDebit/100:'0'."</b>",
                ($spentPoints!=0)?"<b>$".$spentPoints/100:'0'."</b>",
                ($remainigPoints!=0)?"<b>$".$remainigPoints/100:'0'."</b>",
                ($allocatedPoints!=0)?"<b>$".$allocatedPoints/100:'0'."</b>",
                ($earnedPoints!=0)?"<b>$".$earnedPoints/100:'0'."</b>",
                ($redeemedPoints!=0)?"<b>$".$redeemedPoints/100:'0'."</b>",
                ($balancePoints!=0)?"<b>$".$balancePoints/100:'0'."</b>",
                ($marginPoints!=0)?"<b>$".$marginPoints/100:'0'."</b>"
         );
        $result=array('data'=>$response);
    }else{
        $result=array('data'=>array());
    }
    echo json_encode($result);
    exit;
}

// Include Template 
include_once('../libs/routs.php')
?>