<?php
include("../public/impOpreation.php");
include("../library/uploadImages.php");

$do = $_GET['do'];
$today = date("Y-m-d H:i:s");
$date = date("Y-m-d");
if (empty($do)) {  
    $smarty->display("header.html");
    $smarty->display("transactionsReportEstateView/show.html");
    $smarty->assign("transactionsestatesjs", 1);
    $smarty->display("footer.html");
}else if ($do == 'showajax') {
  showajax(); 
}


function showajax() {
    $columns = array('id', 'id', 'transactionestate', 'transactionvalue','', '', 'addtoday','adduserid');
    $start_date = $_POST['start_date'];
    $end_date   = $_POST['end_date'];
    $del        = $_POST['del'];
    $delegate = $_POST['delegate'];
    $delegateid = $_POST['delegateid'];
    $delegatereliefid  = $_POST['delegatereliefid'];
    $delegatedataid  = $_POST['delegatedataid'];
    $delegateclearanceid  = $_POST['delegateclearanceid'];
    $saveid        = $_POST['saveid'];
    $adduserid        = $_POST['adduserid'];
    $id        = $_POST['id'];
    $incometypeid        = $_POST['incometypeid'];

    $searchQuery = " ";

    if($id != ''){
       $searchQuery .= " and transactionsestates.id = ".$id. " ";
    }

    if($incometypeid != ''){
       $searchQuery .= " and transactionsestates.incometypeid = ".$incometypeid. " ";
    }

    if($delegate != ''){
      $searchQuery .=  " and ( transactionsestates.delegateid = ".$delegate. " or  transactionsestates.delegatereliefid = ".$delegate. " or  transactionsestates.delegatedataid = ".$delegate. " or  transactionsestates.delegateclearanceid = ".$delegate. "  ) ";
    }

    // if($delegateid != ''){
    //   $searchQuery .=  " and transactionsestates.delegateid = ".$delegateid. " ";
    // }

    // if($delegatereliefid != ''){
    //   $searchQuery .=  " and transactionsestates.delegatereliefid = ".$delegatereliefid. " ";
    // }

    // if($delegatedataid != ''){
    //   $searchQuery .=  " and transactionsestates.delegatedataid = ".$delegatedataid. " ";
    // }

    // if($delegateclearanceid != ''){
    //   $searchQuery .=  " and transactionsestates.delegateclearanceid = ".$delegateclearanceid. " ";
    // }

    if($saveid != ''){
       $searchQuery .= " and transactionsestates.saveid = ".$saveid. " ";
    }

    if($adduserid != ''){
       $searchQuery .= " and transactionsestates.adduserid = ".$adduserid. " ";
    }

    if($del == 1){
       $searchQuery .= " and transactionsestates.del > 1 "; 
    }

   
    
    if($start_date != '' && $end_date != ''){
       $searchQuery .='and  transactionsestates.addtoday  >= "' . $start_date . ' 00-00-00" and transactionsestates.addtoday <= "' . $end_date . ' 23-59-55" ';
    }else{
        // $today = date("Y-m-d");
        // $searchQuery .='and  students.addtoday  >= "' . $today . ' 00-00-00" and students.addtoday <= "' . $today . ' 23-59-55" ';
    }


    if (isset($_POST['search']['value']) && $_POST['search']['value'] != "") {
    $searchQuery .= "and ( transactionsestates.id LIKE "%'.$_POST["search"]["value"].'%" 
                    OR transactionsestates.transactionestate LIKE "%'.$_POST["search"]["value"].'%"
                    OR transactionsestates.transactionvalue LIKE "%'.$_POST["search"]["value"].'%"
                    OR transactionsestates.delegateid LIKE "%'.$_POST["search"]["value"].'%"
                    OR transactionsestates.fullcommission LIKE "%'.$_POST["search"]["value"].'%"
                    OR transactionsestates.services LIKE "%'.$_POST["search"]["value"].'%"
                    OR transactionsestates.externalcommission LIKE "%'.$_POST["search"]["value"].'%"
                    OR transactionsestates.delegatereliefid LIKE "%'.$_POST["search"]["value"].'%"
                    OR transactionsestates.reliefvalue LIKE "%'.$_POST["search"]["value"].'%"
                    OR transactionsestates.delegatedataid LIKE "%'.$_POST["search"]["value"].'%"
                    OR transactionsestates.datavalue LIKE "%'.$_POST["search"]["value"].'%"
                    OR transactionsestates.delegateclearanceid LIKE "%'.$_POST["search"]["value"].'%"
                    OR transactionsestates.delegateclearanceidvalue LIKE "%'.$_POST["search"]["value"].'%"
                    OR transactionsestates.onlinevalue LIKE "%'.$_POST["search"]["value"].'%"
                    OR transactionsestates.netservices LIKE "%'.$_POST["search"]["value"].'%"
                    OR transactionsestates.representativecommission LIKE "%'.$_POST["search"]["value"].'%"
                    OR transactionsestates.officecommission LIKE "%'.$_POST["search"]["value"].'%"
                    OR transactionsestates.savevalue LIKE "%'.$_POST["search"]["value"].'%"
                    OR transactionsestates.saveid LIKE "%'.$_POST["search"]["value"].'%"
                    OR transactionsestates.incomeid LIKE "%'.$_POST["search"]["value"].'%"
                    OR transactionsestates.addtoday LIKE "%'.$_POST["search"]["value"].'%"
                    OR transactionsestates.adduserid LIKE "%'.$_POST["search"]["value"].'%"
                    OR transactionsestates.del LIKE "%'.$_POST["search"]["value"].'%"
        )
     ";
    }


    if(isset($_POST["order"]))
    {
    $searchQuery .= 'ORDER BY '.$columns[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].'  ';
    }
    else
    {
     $searchQuery .= "ORDER BY  transactionsestates.id DESC ";
    }
    
     $totals = R::count('transactionsestates','WHERE 1 '.$searchQuery.' ');
    
    if (isset($_POST['start']) && $_POST['length'] != '-1') {
       $searchQuery .= "LIMIT " . intval($_POST['start']) . ", " .
                intval($_POST['length']);
    }

    $rResult = R::getAll('SELECT transactionsestates.* ,userdelegateid.employeename as userdelegate
    ,userdelegatereliefid.employeename as userdelegaterelief,
    userdelegatedataid.employeename as userdelegatedata,
    userdelegateclearanceid.employeename as userdelegateclearance,
    useradduserid.employeename as useradduser ,savename  FROM `transactionsestates` 
        LEFT JOIN user as userdelegateid ON transactionsestates.delegateid = userdelegateid.userid
        LEFT JOIN user as userdelegatereliefid ON transactionsestates.delegatereliefid = userdelegatereliefid.userid
        LEFT JOIN user as userdelegatedataid ON transactionsestates.delegatedataid = userdelegatedataid.userid
        LEFT JOIN user as userdelegateclearanceid ON transactionsestates.delegateclearanceid = userdelegateclearanceid.userid
        LEFT JOIN user as useradduserid ON transactionsestates.adduserid = useradduserid.userid
        LEFT JOIN save ON transactionsestates.saveid = save.saveid
        WHERE 1 '.$searchQuery.' ');   
    $apps = count($rResult);
    
    
     
    $output = array(  
        "draw" => intval($_POST["draw"]),
        "recordsTotal" => intval($apps),
        "recordsFiltered" => $totals * 3,
        "data" => array()
    );
  
       foreach ($rResult as $row) {
        $sub_array = array();
        $sub_array[] = $row["id"];
        $sub_array[] = $row["transactionestate"];
        $sub_array[] = $row["transactionvalue"];
        $sub_array[] = $row["userdelegaterelief"];
        $sub_array[] = 'الفرجه';
        $sub_array[] = $row["reliefvalue"];
        $sub_array[] = $row["addtoday"];
        $sub_array[] = $row['useradduser'];     
        $output['data'][] = $sub_array;

        $sub_array2 = array();
        $sub_array2[] = $row["id"];
        $sub_array2[] = $row["transactionestate"];
        $sub_array2[] = $row["transactionvalue"];
        $sub_array2[] = $row["userdelegateclearance"];
        $sub_array2[] = 'التخليص';
        $sub_array2[] = $row["delegateclearanceidvalue"];
        $sub_array2[] = $row["addtoday"];
        $sub_array2[] = $row['useradduser'];     
        $output['data'][] = $sub_array2;


        $sub_array3 = array();
        $sub_array3[] = $row["id"];
        $sub_array3[] = $row["transactionestate"];
        $sub_array3[] = $row["transactionvalue"];
        $sub_array3[] = $row["userdelegatedata"];
        $sub_array3[] = 'الداتا';
        $sub_array3[] = $row["datavalue"];
        $sub_array3[] = $row["addtoday"];
        $sub_array3[] = $row['useradduser'];     
        $output['data'][] = $sub_array3;


        // $sub_array[] = $row["userdelegate"];
        // $sub_array[] = $row["fullcommission"];
        // $sub_array[] = $row["services"];
        // $sub_array[] = $row["netservices"];
        // $sub_array[] = $row["userdelegaterelief"];
       
        // $sub_array[] = $row["userdelegatedata"];
        // $sub_array[] = $row["datavalue"];
        // $sub_array[] = $row["userdelegateclearance"];
        // $sub_array[] = $row["delegateclearanceidvalue"];
        // $sub_array[] = $row["onlinevalue"];
        // $sub_array[] = $row["externalcommission"];
        // $sub_array[] = $row["representativecommission"];
        // $sub_array[] = $row["officecommission"];
        // $sub_array[] = $row["savevalue"];
        // $sub_array[] = $row["savename"];
        
       }
    
       echo json_encode($output, JSON_UNESCAPED_SLASHES);

}





?>