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

$keycond = "";
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$usertype = $_SESSION["usertype"];

mysqli_select_db($conn,$zdb);
$success = false;
$cust = 'accmast';
$ztit = $_SESSION["tit_" . $dbname];
$maindb = 'challan';
$trandb = 'invwhtn' . $ztit;
$custdb = $cust . $ztit;
$file = 'chalmain.php';
$keyfld = "vouchno";

$series = $_REQUEST["series"];
$rangeFrom = $_REQUEST["rangeFrom"];
$rangeTo = $_REQUEST["rangeTo"];
$searchTerm = $_REQUEST["ssearch"];
$operator = $_REQUEST["soperator"];
$invoicingtype = $_REQUEST["type"];

$invoicecond = '';
if ($invoicingtype != "") {
    $invoicecond = " and ( `complete` = '' or `complete` = '0000-00-00' ) ";
}

$cond = "";
if ($_REQUEST["sdelv"] == "1") {
    $delvcond = "";
} else if ($_REQUEST["sdelv"] == "2") {
    $delvcond = "AND `delv` != 'D'";
} else if ($_REQUEST["sdelv"] == "3") {
    $delvcond = "AND `delv` = 'D'";
}
if ($operator == "=") {
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND a.`vouchdt` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND a.`delv` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND a.`delvdate` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "4") {
        $cond = "AND b.`name` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND a.`nag` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "6") {
        $cond = "AND a.`sizemm` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "7") {
        $cond = "AND a.`guagemm` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "8") {
        $cond = "AND a.`makemm` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "9") {
        $cond = "AND a.`dia` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "10") {
        $cond = "AND a.`ap` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "11") {
        $cond = "AND a.`weld` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "12") {
        $cond = "AND a.`straight` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "13") {
        $cond = "AND a.`foot` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "14") {
        $cond = "AND a.`totfeet` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "15") {
        $cond = "AND a.`weight` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "16") {
        $cond = "AND a.`rate` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "17") {
        $cond = "AND a.`amount` = '$searchTerm'";
    }
} else {

//    if ($_REQUEST["sfield"] == "1") {
//        $cond = "AND `vouchno` LIKE '%$searchTerm%'";
//    }
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND a.`vouchdt` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND a.`delv` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND a.`delvdate` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "4") {
        $cond = "AND b.`name` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND a.`nag` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "6") {
        $cond = "AND a.`sizemm` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "7") {
        $cond = "AND a.`guagemm` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "8") {
        $cond = "AND a.`makemm` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "9") {
        $cond = "AND a.`dia` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "10") {
        $cond = "AND a.`ap` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "11") {
        $cond = "AND a.`weld` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "12") {
        $cond = "AND a.`straight` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "13") {
        $cond = "AND a.`foot` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "14") {
        $cond = "AND a.`totfeet` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "15") {
        $cond = "AND a.`weight` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "16") {
        $cond = "AND a.`rate` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "17") {
        $cond = "AND a.`amount` LIKE '%$searchTerm%'";
    }
}


$sortcond = "ORDER BY `vouchno`";
if ($_REQUEST["sortBy"] == "2") {
    $sortcond = " ORDER BY `vouchno`";
} else if ($_REQUEST["sortBy"] == "1") {
    $sortcond = " ORDER BY `vouchdt`";
} else if ($_REQUEST["sortBy"] == "3") {
    $sortcond = " ORDER BY `delvdate`";
} else if ($_REQUEST["sortBy"] == "4") {
    $sortcond = " ORDER BY `party`";
}


$ordcond = "ASC";
if ($_REQUEST["orderAs"] == "desc") {
    $ordcond = " DESC";
} else {
    $ordcond = " ASC";
}

$query = "SELECT a.*, DATE_FORMAT(a.`vouchdt`,'%d-%m-%Y') as mydt, a.vouchdt as mvdate, DATE_FORMAT(a.`delvdate`,'%d-%m-%Y') as mdelvdate,  b.name  FROM `$maindb` a Left join accmast b
                         on  a.party = b.accode  where a.`vouchno`!='' $cond $delvcond $sortcond $ordcond  LIMIT $rangeFrom, $rangeTo";


$result = mysqli_query($conn,$query) or die(mysqli_error($conn));

$result2 = mysqli_query($conn,"SELECT vouchno FROM `challan`") or die(mysqli_error($conn));

$rows = array();
$ctr = 0;
while ($r = mysqli_fetch_assoc($result)) {

    $numrows = mysqli_num_rows($result2);

    if ($r['cashcred'] == 'C') {
        $cashcred = 'Cash';
    } else {
        $cashcred = 'Credit';
    }


//    $getpart = mysqli_query($conn,"Select accode,name from `accmast` where accode = '$r[party]' AND `grpcode`='010'") or die(mysqli_error($conn));
//    $fetpart = mysqli_fetch_array($getpart);
//    $getgodw = mysqli_query($conn,"Select name from `godown` where code = '$row[brancode]'") or die(mysqli_error($conn));
//    $fetgodw = mysqli_fetch_array($getgodw);
//    $getcompa = mysqli_query($conn,"Select compname from `company` where code = '$row[company]'") or die(mysqli_error($conn));
//    $fetcomp = mysqli_fetch_array($getcompa);

    $rows[] = ['success' => true,
        'numrows' => $numrows,
        'value' => $ctr,
        'srl' => $r['srno'],
        'srno' => $r['vouchno'],
        'mark' => $r['mark'],
        'vouchdt' => $r['mydt'],
        'mvdate' => $r['mvdate'],
        'valdate' => $r['valdate'],
        'delv' => $r['delv'],
        'delvdate' => $r['mdelvdate'],
        'partyname' => $r['name'],
        'accode' => $r['party'],
        'nag' => $r['nag'],
        'sizemm' => $r['sizemm'],
        'guagemm' => $r['guagemm'],
        'makemm' => $r['makemm'],
        'dia' => $r['dia'],
        'ap' => $r['ap'],
        'weld' => $r['weld'],
        'straight' => $r['straight'],
        'foot' => $r['foot'],
        'totfeet' => $r['totfeet'],
        'weight' => $r['weight'],
        'rate' => $r['rate'],
        'amount' => $r['amount'],
        'descrip' => $r['descrip'],
        'cashcred' => $r['cashcred'],
        'cashvouch' => $r['cashvouch'],
        'vouchno' => $r['vouchno'],
        'gstperc' => $r['gstperc'],
//        'action' => "$file?mode=edit&series=$series&vouchno=$r[vouchno]",
        'action' => "$file?mode=edit&$keyfld=$r[$keyfld]",
        'usertype' => $usertype,
    ];
    $ctr++;
}
//browse($rows);
header("Content-type: application\json");
echo json_encode($rows);
