<?php
// Include main files
include_once('../libs/main.php');
include_once('checkLogin.php');

$loyaltyObj = new loyalty_action();

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

    $where = "1=1";
    $where .= " AND pc.pointcategory_title IN ('ADD FAMILY','NEW XCARE APPOINTMENT','INSURANCE','WELCOME','REFERRAL')";
    $where .= " AND l.loyalty_action_status IN ('Allocated','Earned','Claimed')";

    $tables  = array('tbl_pointmaster pm', 'tbl_pointcategory pc');
    $jfields = array(
        'pm.pointmaster_id = l.loyalty_action_point_id',
        'pc.pointcategory_id = pm.pointmaster_category_id'
    );
    $jtype   = array('INNER', 'INNER');

    $data = array(
        'where'      => $where,
        'joinTables' => $tables,
        'joinFields' => $jfields,
        'joinTypes'  => $jtype,
        'groupBy'    => array('l.loyalty_action_status'),
        'fields'     => array('l.loyalty_action_status, SUM(l.loyalty_action_rpoints) AS total_points'),
        'orderBy'    => array("FIELD(l.loyalty_action_status, 'Allocated','Earned','Claimed')")
    );
    //$data['debug']=true;
    $records = $loyaltyObj->getAllRecords($data);
    $recordsTotal  = $loyaltyObj->getAllCount($data);

    $response = array();
    if ($records && count($records) > 0) {
        foreach ($records as $rec) {
            $response[] = array(
                'status' => $rec->loyalty_action_status,
                'total'  => number_format($rec->total_points)
            );
        }
    }

    $result = array(
        'recordsTotal'    => count($response),
        'recordsFiltered' => count($response),
        'data'            => $response
    );

    echo json_encode($result);
    exit;
}
include_once('../libs/routs.php');
?>