<?php

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');
header('Content-Type: text/html; charset=utf-8');

$keycond = " AND `series`='$zseries' ";
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn,$zdb);
$zroot = $_SESSION["zdir"];

$success = false;

$cust = 'accmast';

$ztit = $_SESSION["tit_" . $dbname];

$maindb = 'challan';
$trandb = 'invwhtn' . $ztit;
$custdb = $cust . $ztit;

$file = 'chalmain.php';

$keyfld = "vouchno";
if (isset($_REQUEST["party"])) {
    $party = $_REQUEST["party"];
    $sizemm = $_REQUEST["sizemm"];
    $guagemm = $_REQUEST["guagemm"];
    $makemm = $_REQUEST["makemm"];


    $zyear = right($zdb, 4);
//browse($zyear);
    $gcuryear = mysqli_query($conn,"SELECT id FROM $zroot.`compyear` WHERE finyear = '$zyear'");
    $fcuryear = mysqli_fetch_assoc($gcuryear);
    $cyid = $fcuryear['id'];
    $cyid--;

    $gpreyear = mysqli_query($conn,"SELECT finyear FROM $zroot.`compyear` WHERE `id` = '$cyid'");
    $fpreyear = mysqli_fetch_assoc($gpreyear);

    $prevyear = 'jazofaz_nspw_1_' . $fpreyear['finyear'];
    mysqli_query($conn," create temporary table search1 SELECT *, 'CYSP' as `trparty` FROM `challan` WHERE `party`='$party' AND `sizemm`='$sizemm' AND `guagemm`='$guagemm' AND `makemm`='$makemm' order by vouchno")
            or die(mysqli_error($conn));

    if ($fpreyear['finyear'] != '') {
        mysqli_query($conn,"INSERT INTO search1 SELECT *, 'PYSP' AS `trparty` FROM $prevyear.`challan` WHERE `party`='$party' AND `sizemm`='$sizemm' AND `guagemm`='$guagemm' AND `makemm`='$makemm' order by vouchno")
                or die(mysqli_error($conn));
    }
    mysqli_query($conn,"INSERT INTO search1 SELECT *,'CYDP' AS `trparty` FROM `challan` WHERE `party`!='$party' AND `size`='$sizemm' AND `guagemm`='$guagemm' AND `makemm`='$makemm' order by vouchno")
            or die(mysqli_error($conn));
    if ($fpreyear['finyear'] != '') {
        mysqli_query($conn," INSERT INTO search1 SELECT *,'PYDP' AS `trparty` FROM $prevyear.`challan` WHERE `party`!='$party' AND `sizemm`='$sizemm' AND `guagemm`='$guagemm' AND `makemm`='$makemm' order by vouchno")
                or die(mysqli_error($conn));
    }
    $gall = mysqli_query($conn,"SELECT * FROM `search1`") or die(mysqli_error($conn));
//    $fall = mysqli_fetch_assoc($gall);
    $prodArray = array();


    while ($fall = mysqli_fetch_assoc($gall)) {
        $prodArray[] = $fall;
    }
    mysqli_select_db($conn,$zdb);

    foreach ($prodArray as $key => $value) {
        $paccode = $prodArray[$key]['party'];
        $gparty = mysqli_query($conn,"SELECT `name` FROM `accmast` WHERE `accode`='$paccode' AND `grpcode`='010'") or die(mysqli_error($conn));
        $fparty = mysqli_fetch_assoc($gparty);
//      
        $prodArray[$key]['party'] = "$fparty[name]";
//        $prodArray[$key]['party'] = "ASDF";
    }
    $productJson = json_encode($prodArray);
    echo $productJson;
}