<?php

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');

$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdbroot"];

mysqli_select_db($conn, $zdb);
$admin = $_SESSION["admin_" . $dbname];
$fseries2 = $_SESSION["society_" . $dbname];
$success = false;
$maindb = 'cashrecp';

$fseries = $_SESSION["series_" . $dbname];
$keycond = " and a.`series`='$fseries'";

$file = "cashrecp.php";

$keyfld = "vouchno";
$rangeFrom = $_REQUEST["rangeFrom"];
$rangeTo = $_REQUEST["rangeTo"];
$searchTerm = $_REQUEST["ssearch"];
$operator = $_REQUEST["soperator"];
$cond = "";
if ($operator == "=") {
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND `vouchno` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND `mydt` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND `cashname` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "4") {
        $cond = "AND `partyname` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND `amount` = '$searchTerm'";
    } 
} else {
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND `vouchno` LIKE '%$searchTerm%'";
    }  else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND `mydt` LIKE '%$searchTerm%'";
    }  else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND `cashname` LIKE '%$searchTerm%'";
    }  else if ($_REQUEST["sfield"] == "4") {
        $cond = "AND `partyname` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND `amount` LIKE '%$searchTerm%'";
    } 
}

$sortcond = "ORDER BY `vouchno`";
if ($_REQUEST["sortBy"] == "1") {
    $sortcond = " ORDER BY `vouchno`";
}  else if ($_REQUEST["sortBy"] == "2") {
    $sortcond = " ORDER BY `mydt`";
}  else if ($_REQUEST["sortBy"] == "3") {
    $sortcond = " ORDER BY `cashname`";
}  else if ($_REQUEST["sortBy"] == "4") {
    $sortcond = " ORDER BY `partyname`";
} else if ($_REQUEST["sortBy"] == "5") {
    $sortcond = " ORDER BY `amount`";
}
 
$ordcond = "DESC";
if ($_REQUEST["orderAs"] == "desc") {
    $ordcond = " DESC";
} else {
    $ordcond = " ASC";
}

//mysqli_query($conn,  "Create temporary table `bp` SELECT a.`vouchno`, b.`name` as bankname,
//                            Date_Format(`vouchdt`, '%d/%m/%Y') as mydt, c.`name`as partyname,a.amount  
//                            FROM $maindb a,accmast b,accmast c WHERE a.bankname = b.accode AND a.party = c.accode $keycond") 
//                            or die(mysqli_error($conn));
//    
//    $srchresult = "SELECT * FROM `bp` WHERE vouchno != '' $cond  $sortcond $ordcond  LIMIT $rangeFrom,$rangeTo"or die(mysqli_error($conn));
//    
//    $result = mysqli_query($conn, $srchresult)or die(mysqli_error($conn));

    
mysqli_query($conn,  "Create temporary table `cr` SELECT a.`vouchno`, b.`name` as cashname,
                            Date_Format(`vouchdt`, '%d/%m/%Y') as mydt, c.`name`as partyname,a.amount  
                            FROM $maindb a,accmast b,$zroot.`member` c WHERE a.bankname = b.accode AND a.memname = c.accode 
                            $keycond ")or die(mysqli_error($conn));
    
    $srchresult = "SELECT * FROM `cr` WHERE vouchno != '' $cond  $sortcond $ordcond  LIMIT $rangeFrom,$rangeTo"or die(mysqli_error($conn));
    
    $result = mysqli_query($conn, $srchresult)or die(mysqli_error($conn));

$rows = array();
$ctr = 0;
while ($r = mysqli_fetch_assoc($result)) {
 
    $rows[] = [ 'success' => true,
        'value'     => $ctr,
        'keyfld'    => $keyfld,
        'vouchno'   => $r['vouchno'],
        'mydt'      => $r['mydt'],
        'cashname'  => $r['cashname'],
        'partyname' => $r['partyname'],
        'amount'    => $r['amount'],
        'action'    => "$file?mode=edit&vouchno=$r[vouchno]&seies=$fseries",
    ];
    $ctr++;
}

header("Content-type: application\json");
echo json_encode($rows);
