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

$formname = 'Challan';
$maindb = 'chalmain';
$trandb = 'chaltran';

//$printfile = "challan_print.php?chalno=$qvouch&series=$zseries";
$invprintfile = "challan_inv_print.php";
$file = 'challan.php';
$trandbexist = 1;
$keyfld = "chalno";
$focusfield = "name";
$zseries = "$_GET[series]";
$keycond = " AND `series`='$zseries' ";
$keycond1 = " AND `cashcred` != 'C' and `cashflag` != 'Y' ";
$msource = "CH";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {
    $a = 1;
    /*     * Start sale exist check** */
    if ($_GET["mode"] == 'true') {
        $checkSaleExist = mysqli_query($conn, "SELECT `chalno` FROM `$maindb` WHERE `$keyfld`='$qvouch' AND `series`='$zseries'")or die(mysqli_error($conn));
        $checkSaleExt = mysqli_num_rows($checkSaleExist);
        if ($checkSaleExt > 0) {
            $a = 0;
        }
    }
    /** End sale exist check** */
    $series = $zseries;
    $id = $_POST[id];
    $qvouch = $_POST[chalno];
    $date = $_POST[date];
    $custcode = $_POST[custcode];
    $vessel = $_POST[vessel];
    $rankcode = $_POST[rankcode];
    $ppno = $_POST[ppno];
    $cdcno = $_POST[cdcno];
    $delvchgs = $_POST[delvchgs];
    $remark = $_POST[remark];
    $crew = $_POST[crew];
    $billno = $_POST[billno];
    $cashcred = "R";
    $cashflag = "N";
    $count = 0;


    if (empty($date)) {
        browse("Date Cannot Be Empty");
        $a = 0;
    }
    if (empty($custcode)) {
        browse("Custcode Cannot Be Empty");
        $a = 0;
    }
    if (empty($rankcode)) {
        browse("Rank Cannot Be Empty");
        $a = 0;
    }
    if (empty($ppno)) {
        browse("Passport No. Cannot Be Empty");
        $a = 0;
    }
    if (empty($cdcno)) {
        browse("Id No. Cannot Be Empty");
        $a = 0;
    }

    if (!empty($billno)) {
        browse('Edit Not Allowed Only View. Challan Exist In Invoice. No. ' . $billno);
        $a = 0;
    }


    $count = $_POST["items"];
//        browse($count);
    if ($count == 0) {
        browse('No Detail Part. Item / Qty. Cannot Be Empty.');
        $a = 0;
    }

    $getcomp = mysqli_query($conn, "SELECT `custcode`, `comp`, `curr`, `stopchal`, `billing`, `delvchgs`, `taxapp` FROM `customer` WHERE `custcode`='$custcode'")or die(mysqli_error($conn));
    $gcomp = mysqli_fetch_array($getcomp);
    $comp = $gcomp[comp];
    $curr = $gcomp[curr];
    $billing = $gcomp[billing];
    $delvperc = $gcomp[delvchgs];
    $mtaxapp = $gcomp[taxapp];
    $mstopchal = $gcomp[stopchal];

    if ($billing == 'S') {
        $autobill = "Y";
    } else {
        $autobill = "N";
    }

    if ($mstopchal == 1) {
        browse('Challan Stopped For This Party - By Main Authority.');
        $a = 0;
    }

    if ($a == 1) {

        $getcomp = mysqli_query($conn, "SELECT `custcode`, `comp`, `curr`, `stopchal`, `billing`, `delvchgs`, `taxapp` FROM `customer` WHERE `custcode`='$custcode'")or die(mysqli_error($conn));
        $gcomp = mysqli_fetch_array($getcomp);
        $comp = $gcomp[comp];
        $curr = $gcomp[curr];
        $billing = $gcomp[billing];
        $delvperc = $gcomp[delvchgs];
        $mtaxapp = $gcomp[taxapp];
        $mstopchal = $gcomp[stopchal];

        if ($billing == 'S') {
            $autobill = "Y";
        } else {
            $autobill = "N";
        }

        $getcvri = mysqli_query($conn, "SELECT `id` FROM `cvrimain` WHERE `custcode`='$custcode' AND `rankcode`='$rankcode'")or die(mysqli_error($conn));
        $gcvri = mysqli_fetch_array($getcvri);
        $cvricode = $gcvri[id];

        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE, `itemtran` WRITE, `nonstd` WRITE, 
                    `invmain` WRITE, `invtran` WRITE, `itemlink` WRITE, `item` WRITE") or die(mysqli_error($conn));

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(chalno+0) AS maxcode FROM `$maindb` WHERE `series`='$zseries' LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($gen1) > 0) {
                $vvouch = mysqli_fetch_assoc($gen1);
                $qvouch = $vvouch["maxcode"] + 1;
            } else {
                $qvouch = '1';
            }
        }
        $mvouch = $qvouch;


        if ($addflag == 1) {
            $geti = mysqli_query($conn, "SELECT *, max(id) as lastid FROM chalmain") or die(mysqli_error($conn));
            $getid = mysqli_fetch_array($geti);
            $maxid = $getid[lastid] + 1;
            $id = $maxid;
            mysqli_query($conn, "INSERT INTO `$maindb`(`id`, `chalno`, `series`, `date`, `custcode`, `cvricode`, `vessel`, `rankcode`, `comp`, `ppno`, `cdcno`, 
                                                            `delvchgs`, `remark`, `crew`, `cashcred`, `autobill`, `cashflag`)
                                    VALUES('$id', $qvouch, '$zseries', '$date', '$custcode', '$cvricode', '$vessel', '$rankcode', '$comp', '$ppno', '$cdcno',
                                            '$delvchgs', '$remark', '$crew', '$cashcred', '$autobill',  '$cashflag')") or die(mysqli_error($conn));
            $mvouch = $qvouch; //1
        } else {
            $mvouch = $qvouch; //1

            mysqli_query($conn, "UPDATE `$maindb` SET `id`='$id', `date`='$date', `custcode`='$custcode', `cvricode`='$cvricode', `vessel`='$vessel',
                    `rankcode`='$rankcode', `comp`='$comp', `ppno`='$ppno', `cdcno`='$cdcno', `delvchgs`='$delvchgs', `remark`='$remark',
                    `crew`='$crew', `cashcred`='$cashcred', `autobill`='$autobill', `cashflag`='$cashflag'
                     WHERE chalno = '$mvouch' AND `series` = '$zseries' ") or die(mysqli_error($conn));
        }
        ###################### TRAN PART #####################################################

        mysqli_query($conn, "DELETE FROM `$trandb` WHERE series = '$zseries' and chalno='$mvouch'") or die(mysqli_error($conn));
        mysqli_query($conn, "DELETE FROM `itemtran` WHERE series = '$zseries' and `vouchno` = '$mvouch' and source = '$msource'") or die(mysqli_error($conn));
        $tamt = 0;

        $tvat5on = 0;
        $tvaton = 0;

        $cnt = 0;
        $tvat = 0;
        $tvat5 = 0;
        for ($i = 0; $i < $count; $i++) {
            $msrno = $_POST["srno_$i"];
            $mitemname = trim($_POST["itemname_$i"]);
            $msize = trim($_POST["size_$i"]);
            $mactsize = $_POST["actsize_$i"];
            $munit = $_POST["unit_$i"];
            $mvat = $_POST["vat_$i"];
            $mqty = $_POST["qty_$i"];
            $mrate = $_POST["rate_$i"];


            if (empty($mactsize)) {
                $mactsize = $msize;
            }

            $nstd = 0;
            $mfrst = 0;
            $stdsms = 0;

            if (!empty($mitemname)) {
                $cnt = $cnt + 1;
//                $getitl = mysqli_query($conn, "SELECT * FROM `itemlink` WHERE `custcode` = '$custcode' AND
//                        `itemname` = '$mitemname' AND `size` = '$msize'") or die(mysqli_error($conn));

                $getitl = mysqli_query($conn, "SELECT * FROM `itemlink` WHERE `custcode` = '$custcode' 
                                      AND `itemname` = '$mitemname' and `size` = '$msize' ") or die(mysqli_error($conn));

                $itlgi = mysqli_num_rows($getitl);
                if ($itlgi > 0) {
                    $iget = mysqli_fetch_assoc($getitl);
                    $mitemcode = $iget[itemcode];
                    $mrate = $iget[rate];
                    $mvatperc = $iget[vat];

//                    browse($mitemname);
//                    browse($msize);
//                    browse($mrate);
                } else {
                    $dispmess = 'Non Standard Item : ' . $mitemname . '  Size : ' . $msize;
                    browse($dispmess);
                    $nstd = 1;
                    $mfrst = $mfrst + 1;
                    $stdsms = 1;
                    $mrate = 0;


//                    browse($mitemname);
//                    browse($msize);
                }

                $mamt = $mqty * $mrate;
                if ($mvatperc > 0) {
                    if ($mvatperc == 5) {
                        $mvat5 = $mamt * ($mvatperc / 100);
                        $tvat5on = $tvat5on + $mamt;
                    } else {
                        $mvat = $mamt * ($mvatperc / 100);
                        $tvaton = $tvaton + $mamt;
                    }
                } else {
                    $mvat = 0;
                    $mvat5 = 0;
                }
                $tamt = $tamt + $mamt;
                $tvat = $tvat + $mvat;
                $tvat5 = $tvat5 + $mvat5;

                if ($mitemcode == '') {
                    $getmyitem = mysqli_query($conn, "SELECT * FROM `item` WHERE `name`='$mitemname'") or die(mysqli_error($conn));
                    $gmyitem = mysqli_fetch_array($getmyitem);
                    $mitemcode = $gmyitem[code];
                }

                mysqli_query($conn, "INSERT INTO `$trandb`(`id`, `chalno`, `series`, `srno`, `itemcode`, `itemname`, `size`, `actsize`, `unit`, `rate`, `qty`, `itemamt`, `comp`, `vat` )
                                VALUES('$id', '$mvouch', '$zseries', '$cnt', '$mitemcode', '$mitemname', '$msize', '$mactsize', '$munit', '$mrate', '$mqty', '$mamt', '$comp', '$mvatperc')") or die(mysqli_error($conn));

                $stkqty = $mqty * -1;
                $group = $msource . $mvouch;
                mysqli_query($conn, "INSERT INTO `itemtran` (`group`, `series`, source, loc, vouchno, vouchdt, srno, itemcode, itemname, size, unit, rate, qty, comp, ord ) 
                             VALUES ('$group', '$zseries','$msource','$zseries','$mvouch','$date','$msrno','$mitemcode','$mitemname','$mactsize','$munit','$mrate','$stkqty','$comp','2' )") or die(mysqli_error($conn));

                if ($nstd == 1) {
                    mysqli_query($conn, "INSERT INTO `nonstd`  (`id`, `series`, `chalno`, `chaldate`, custcode, `vessel`, `rank`, `itemcode`, `itemname`, `size`, `unit`, `rate`,`qty`, `comp` ) 
                          VALUES ('$id', '$zseries', '$qvouch', '$date', '$custcode', '$vessel', '$rankcode', '$mitemcode', '$mitemname','$msize','$munit','$mrate','$mqty','$comp') ") or die(mysqli_error($conn));
                }
            }
        }

//        if (isset($_POST["name"])) {
//            $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `custcode`= '$custcode' AND chalno != '$qvouch'") or die(mysqli_error($conn));
//            if (mysqli_num_rows($result) > 0) {
//                $a = 0;
//                browse("Name Already Exist");
//                header("refresh: 1; $file?code=$qvouch&mode=last&series=$zseries");
//            }
//        }
//        
        ############################## challan part finish - invoice part start  ######################################################

        IF ($autobill == "Y") {
            $mroe = 1.00;
            IF ($billing = "S") {
                switch ($curr) {
                    CASE "I";
                        $mcurr = "INR";
                        $mroe = 1.00;
                        break;
                    CASE "U";
                        $mcurr = "USD";
                        break;
                    CASE "E";
                        $mcurr = "EUR";
                        break;
                }

//                browse($mcurr);
//                
//                browse($tvat5);
//                browse($tvat);
//                browse($tvat5on);
//                browse($tvaton);


                IF ($mtaxapp == "Y") {
                    $invtype = "T";
                } ELSE {
                    $invtype = "I";
                    $tvat = 0;
                    $tvat5 = 0;
                    $tvat5on = 0;
                    $tvaton = 0;
                }

                $frmdt = $date;
                $todt = $date;

                $mdelivery = $tamt * ($mdelvperc / 100);

                $mgross = $tamt;
                $mamount = round($mgross + $delvchgs + $mdelivery + $tvat5 + $tvat, 2);
                $mnetamt = $mamount * $mroe;

                $mbc = "1";

                $getmbill = mysqli_query($conn, "SELECT * FROM `chalmain` WHERE `chalno`='$mvouch' AND `series`='$zseries'")or die(mysqli_error($conn));
                $mbill = mysqli_fetch_array($getmbill);
                $billno = $mbill[billno];

                if ($billno == '' || $billno == '0') {
                    $billflag = "A";
                    $getinv = mysqli_query($conn, "select MAX(`invno`) as `maxbill` from `invmain` where `series` =  '$zseries' and invno != '' ") or die(mysqli_error($conn));
                    if (mysqli_num_rows($getinv) > 0) {
                        $getmaxi = mysqli_fetch_array($getinv);
                        $nbill = $getmaxi[maxbill];

                        IF (EMPTY($nbill) && $nbill == '0') {
                            $newbill = "1";
                        } else {
                            $newbill = $nbill + 1;
                        }
                    } else {
                        $newbill = 1;
                    }
                } else {
                    $billflag = "E";
                    $newbill = $billno;

                    $dqry = mysqli_query($conn, "SELECT `id` FROM invmain WHERE `series` ='$zseries' and invno='$newbill'") or die(mysqli_query($conn, ));
                    $getd = mysqli_fetch_array($dqry);
                    $invid = $getd[id];
                }


                if ($billflag == "A") {
                    mysqli_query($conn, " INSERT INTO `invmain` (`series`, `invno`, `curr`, `type`, `date`, `custcode`, `vessel`, `frmdate`, `todate`,
                                    `gross`, `delvchgs`, `delivery`, `delvperc`, `vat4`, `vat`, `vatperc`, `amount` , `roe` , `netamt` , `bcode`,`vat4on`,`vaton`, `bc`, `autobill`,`cashcred` )
                                    VALUES ('$zseries', $newbill, '$mcurr', '$invtype', '$date', '$custcode', '$vessel', '$frmdt', '$todt', '$mgross', 
                                    '$delvchgs', '$mdelivery', '$mdelvperc', '$tvat5', '$tvat', '', '$tamt',  '$mroe',  '$mnetamt', '$zseries','$tvat5on','$tvaton', '$mbc', 'Y','$cashcred')") or die(mysqli_query($conn, ));

                    $dqry = mysqli_query($conn, "SELECT MAX(id) AS max_id FROM invmain") or die(mysqli_query($conn, ));
                    $getd = mysqli_fetch_array($dqry);
                    $invid = $getd[id];
                } else {
                    mysqli_query($conn, "UPDATE `invmain` SET `curr`='$mcurr', `type`='$invtype',  `date`='$date', 
                                    `custcode`='$custcode', `vessel`='$vessel', `frmdate`='$frmdt', `todate`='$todt', `gross`='$mgross', `delvchgs`='$delvchgs', 
                                    `delivery`='$mdelivery', `delvperc`='$mdelvperc', `vat4`='$tvat5', `vat`='$tvat', `amount`='$tamt', `roe`='$mroe', 
                                    `netamt`='$mnetamt', `bcode`='$zseries',`vat4on` = '$tvat5on', `vaton` = '$tvaton', `bc` = '$mbc', `cashcred` = '$cashcred' 
                                    WHERE `invno`='$newbill' AND `series` ='$zseries'") or die(mysqli_error($conn));
                }
                ##############################################TRAN-PART(INVOICE)##############################################

                $dtqry = mysqli_query($conn, "DELETE FROM `invtran` WHERE `series` ='$zseries' AND invno ='$newbill'") or die(mysqli_error($conn));

                $cctr = 0;
                $getchaltr = mysqli_query($conn, "SELECT itemcode, itemname, size, unit, sum(qty) as tqty, rate, vat  FROM `chaltran` WHERE `chalno`='$mvouch' AND `series`='$zseries' 
                                      group by itemname, rate") or die(mysqli_error($conn));
                while ($chaltr = mysqli_fetch_array($getchaltr)) {
                    $cctr = $cctr + 1;
                    $msrno = $cctr;
                    $mitemcode = $chaltr[itemcode];
                    $mitemname = $chaltr[itemname];
                    $msize = $chaltr[size];
                    $munit = $chaltr[unit];
                    $mqty = $chaltr[tqty];
                    $mrate = $chaltr[rate];
                    $ivatperc = $chaltr[vat];
                    $mamt = $mqty * $mrate;
                    $mvatamt = $mamt * ($mvat / 100);

                    if (!empty($mitemname)) {
                        $totamt = $mqty * $mrate;
                        $mvatamt = $totamt * ($ivatperc / 100);
                        mysqli_query($conn, "INSERT INTO `invtran` (`id`, `series`, `invno`, `type`, `date`, `srno`, `itemcode`, `itemname`, `unit`, 
                                        `rate`, `qty`, `amount`, `xkey`,`vatperc`, `vatamt` )
                                VALUES ('$invid', '$zseries', '$newbill', '$invtype', '$date', '$msrno','$mitemcode','$mitemname','$munit',
                                        $mrate, $mqty, $mamt, '','$ivatperc','$mvatamt')")or die(mysqli_error($conn));
                    }
                }
                mysqli_query($conn, "UPDATE `chalmain` SET `billtype` = '$invtype', `billbran` = '', `billseries` = '$zseries', `billno` = '$newbill', 
	                    `billdate` = '$date', `billmark` = '1', `autobill` = 'Y' WHERE `series`= '$zseries' and `chalno`= '$mvouch' ") or die(mysqli_error($conn));
                ############################## invoice part end - sms start  ######################################################
//                if ($addflag == 1) {
//                    insmess();
//                    $mvouch = $qvouch; //1
//                    header("refresh: 1; $file?$keyfld=$mvouch&mode=edit&series=$zseries");
//                } else {
//                    updmess();
//                    header("refresh: 1; $file?$keyfld=$mvouch&mode=edit&series=$zseries");
//                }
            }
        }
        ############################## MAIN  - challan part ######################################################
        mysqli_query($conn, "UNLOCK TABLES");

        if ($addflag == 1) {
            insmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit&series=$zseries");
        } else {
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit&series=$zseries");
        }
    }
}


if ($addflag != 1) {
    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        if ($addflag == 1) {
            $getvouch = $qvouch;
        } else {
            $res2 = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `series`='$zseries' $keycond1 ORDER BY chalno+0 desc ") or die(mysqli_error($conn));
            $get2 = mysqli_fetch_array($res2);
            $getvouch = $get2[$keyfld];
        }
    }

    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld='$getvouch' AND `series`='$zseries' ") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);

    $getgcust = mysqli_query($conn, "SELECT `custcode`, `name` FROM `customer` WHERE `custcode`='$get[custcode]'") or die(mysqli_error($conn));
    $gcust = mysqli_fetch_array($getgcust);

    $getgrank = mysqli_query($conn, "SELECT `code`, `name` FROM `rank` WHERE `code`='$get[rankcode]'") or die(mysqli_error($conn));
    $grank = mysqli_fetch_array($getgrank);

    $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE $keyfld='$getvouch' AND `series`='$zseries' ORDER BY `srno`") or die(mysqli_error($conn));
}

$senable = "";
$cenable = "";


if (isset($_GET["delete"])) {
    $delflag = 0;
    $result1 = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld='$getvouch' AND `series`='$zseries'") or die(mysqli_error($conn));
    $getdel = mysqli_fetch_array($result1);
    $chalbill = $getdel[billno];
    if (empty($chalbill)) {
        $delflag = 1;
    } else {
        browse('This Challan Is Billed. Cannot Delete.');
        $delflag = 0;
        header("refresh: 1; $file?$keyfld=$mvouch&mode=last&series=$zseries");
    }

    if ($delflag == 1) {
        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE, `itemtran` WRITE, 
                    `chaldel` WRITE, `chaldeltran` WRITE") or die(mysqli_error($conn));

        mysqli_query($conn, "INSERT INTO `chaldel` (`chalno`, `series`, `date`, `custcode`, `cvricode`, `vessel`, `rankcode`, `comp`, `ppno`, `cdcno`, 
                `delvchgs`, `remark`, `crew`)    
                Select `chalno`, `series`, `date`, `custcode`, `cvricode`, `vessel`, `rankcode`, `comp`, `ppno`, `cdcno`, 
                                                            `delvchgs`, `remark`, `crew` from `$maindb` where $keyfld='$getvouch' AND `series`='$zseries'") or die(mysqli_error($conn));
        mysqli_query($conn, "INSERT INTO `chaldeltran` (`chalno`, `series`, `srno`, `itemcode`, `itemname`, `size`, `actsize`, `unit`, `rate`, `qty`, `comp`)
                 Select `chalno`, `series`, `srno`, `itemcode`, `itemname`, `size`, `actsize`, `unit`, `rate`, `qty`, `comp` from `$trandb` where $keyfld='$getvouch' AND `series`='$zseries' ") or die(mysqli_error($conn));

        $delcond = " chalno='$getvouch' AND series='$zseries' ";
        delnew($zdb, "$maindb", "$delcond", "$file?mode=last&series=$zseries");
        delnew($zdb, "$trandb", "$delcond", "$file?mode=last&series=$zseries");

        $delcond1 = "vouchno='$getvouch' AND series='$zseries' AND `source`='$msource' ";
        delnew($zdb, "itemtran", "$delcond1", "$file?mode=last&series=$zseries");

        mysqli_query($conn, "UNLOCK TABLES");
    }
}
$printfile = "challan_print.php?chalno=$qvouch&series=$zseries";
include("includes/linkshead.php");
?>
<script src="MyScript/challan.js" type="text/javascript"></script>
</head>
<body>

    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-6">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title" style="color :#05B2EA;"><?php Echo $formname; ?></h3>
                        <div class="actions pull-right" style="margin-right: 50px;">
                            <?php
                            $getmseries = mysqli_query($conn, "SELECT `code`, `name` FROM `series` WHERE `code`='$zseries'") or die(mysqli_error($conn));
                            $mseries = mysqli_fetch_array($getmseries);
                            ?>
                            <label class="label-control"><?php Echo $zseries; ?></label> - <label class="label-control"><?php Echo $mseries[name]; ?></label> 
                        </div>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class='row'>
                            <div class='col-lg-2 col-md-2 col-sm-2 col-xs-2 pad text-right'>
                                <label class='label-control tmarg'>Challan No.</label>
                            </div>
                            <div class='col-lg-2 col-md-2 col-sm-2 col-xs-2 pad'>
                                <input type='text' class='form-control form chalno' name='chalno' value='<?php echo ($addflag != 1) ? $get['chalno'] : "$qvouch"; ?>' readonly>
                                <input type='hidden'  name='id' value='<?php echo ($addflag != 1) ? $get['id'] : ""; ?>'>
                            </div>
                            <div class='col-lg-3 col-md-3 col-sm-3 col-xs-3 pad text-right'>
                                <label class='label-control tmarg'>Challan Date</label>
                            </div>
                            <div class='col-lg-4 col-md-4 col-sm-4 col-xs-4 pad'>
                                <input type='date' class='form-control form' name='date' value='<?php echo ($addflag != 1) ? $get['date'] : date('Y-m-d'); ?>' required>
                            </div>
                        </div>
                        <div class='row'>
                            <div class='col-lg-2 col-md-2 col-sm-2 col-xs-2 pad text-right'>
                                <label class='label-control tmarg'>Customer</label>
                            </div>
                            <div class='col-lg-2 col-md-2 col-sm-2 col-xs-2 pad'>
                                <input type='text' class='form-control form custcode' name='custcode' value='<?php echo ($addflag != 1) ? $get['custcode'] : ""; ?>'  required>
                            </div>
                            <div class='col-lg-7 col-md-7 col-sm-7 col-xs-7 pad'>
                                <select class="fetchpart form-control form custname">
                                    <option value="<?php Echo ($addflag != 1) ? $get[custcode] : ''; ?>"><?php Echo ($addflag != 1) ? $gcust[name] : ''; ?></option>
                                    <?php
                                    $getcust = mysqli_query($conn, "SELECT `custcode`, `name` FROM `customer` where `custcode` in (select `custcode` from `sercust` where `sercode` = '$zseries') order by `name` ")or die(mysqli_error($conn));
                                    while ($cust = mysqli_fetch_array($getcust)) {
                                        Echo "<option value='$cust[custcode]'>$cust[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class='row'>
                            <div class='col-lg-2 col-md-2 col-sm-2 col-xs-2 pad text-right'>
                                <label class='label-control tmarg'>Vessel</label>
                            </div>
                            <div class='col-lg-9 col-md-9 col-sm-9 col-xs-9 pad'>
                                <select class="select2 form-control form vessel" name="vessel">
                                    <option value="<?php Echo ($addflag != 1) ? $get[vessel] : ''; ?>"><?php Echo ($addflag != 1) ? $get[vessel] : ''; ?></option>
                                    <?php
                                    if ($addflag != 1) {
                                        $getmvess = mysqli_query($conn, "SELECT `name`, `custcode` FROM `vessel` WHERE `custcode`='$get[custcode]' order by name")or die(mysqli_error($conn));
                                        while ($mvess = mysqli_fetch_array($getmvess)) {
                                            Echo "<option value='$mvess[name]'>$mvess[name]</option>";
                                        }
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class='row'>
                            <div class='col-lg-2 col-md-2 col-sm-2 col-xs-2 pad text-right'>
                                <label class='label-control tmarg'>Rank</label>
                            </div>
                            <div class='col-lg-6 col-md-6 col-sm-6 col-xs-6 pad'>
                                <select class="select2 form-control form rankcode" name="rankcode"  required>
                                    <option value="<?php Echo ($addflag != 1) ? $grank[code] : ''; ?>"><?php Echo ($addflag != 1) ? $grank[name] : ''; ?></option>
                                    <?php
                                    $getrank = mysqli_query($conn, "SELECT `code`, `name` FROM `rank` order by name")or die(mysqli_error($conn));
                                    while ($rank = mysqli_fetch_array($getrank)) {
                                        Echo "<option value='$rank[code]'>$rank[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class='row'>
                            <div class='col-lg-2 col-md-2 col-sm-2 col-xs-2 pad text-right'>
                                <label class='label-control tmarg'>Crew Name</label>
                            </div>
                            <div class='col-lg-9 col-md-9 col-sm-9 col-xs-9 pad'>
                                <input type='text' class='form-control form' name='crew' value='<?php echo ($addflag != 1) ? $get['crew'] : ""; ?>' required>
                            </div>
                        </div>
                        <div class='row'>
                            <div class='col-lg-2 col-md-2 col-sm-2 col-xs-2 pad text-right'>
                                <label class='label-control tmarg'>P.P No.</label>
                            </div>
                            <div class='col-lg-6 col-md-6 col-sm-6 col-xs-6 pad'>
                                <input type='text' class='form-control form ppno' name='ppno' value='<?php echo ($addflag != 1) ? $get['ppno'] : ""; ?>'>
                            </div>
                        </div>
                        <div class='row'>
                            <div class='col-lg-2 col-md-2 col-sm-2 col-xs-2 pad text-right'>
                                <label class='label-control tmarg'>Id No.</label>
                            </div>
                            <div class='col-lg-6 col-md-6 col-sm-6 col-xs-6 pad'>
                                <input type='text' class='form-control form' name='cdcno' value='<?php echo ($addflag != 1) ? $get['cdcno'] : ""; ?>'>
                            </div>
                        </div>
                        <div class='row'>
                            <div class='col-lg-2 col-md-2 col-sm-2 col-xs-2 pad text-right'>
                                <label class='label-control tmarg'>Delv. Chgs.</label>
                            </div>
                            <div class='col-lg-3 col-md-3 col-sm-3 col-xs-3 pad'>
                                <input type='text' class='form-control form text-right' name='delvchgs' value='<?php echo ($addflag != 1) ? $get['delvchgs'] : ""; ?>'>
                            </div>
                            <? if ($addflag == 1) { ?>
                                <div class='col-lg-6 col-md-6 col-sm-6 col-xs-6 pad text-right'>
                                    <button type='button' class='btn btn-primary itemdtl form  btn-square'>Item Detail <i class="fa fa-chevron-right"></i></button>
                                </div>
                            <? } ?>
                        </div>
                        <div class='row'>
                            <div class='col-lg-2 col-md-2 col-sm-2 col-xs-2 pad text-right'>
                                <label class='label-control tmarg'>Remarks</label>
                            </div>
                            <div class='col-lg-9 col-md-9 col-sm-9 col-xs-9 pad'>
                                <input type='text' class='form-control form' name='remark' value='<?php echo ($addflag != 1) ? $get['remark'] : ""; ?>'>
                            </div>
                        </div>
                        <div class='row'>
                            <div class='col-lg-2 col-md-2 col-sm-2 col-xs-2 pad text-right'>
                                <label class='label-control tmarg'>Auto Bill</label>
                            </div>
                            <div class='col-lg-1 col-md-1 col-sm-1 col-xs-1 pad'>
                                <input type='text' class='form-control form text-right' name='autobill' value='<?php echo ($addflag != 1) ? $get['autobill'] : ""; ?>' readonly>
                            </div>
                            <div class='col-lg-2 col-md-2 col-sm-2 col-xs-2 pad'>
                                <input type='text' class='form-control form text-right' name='billno' value='<?php echo ($addflag != 1) ? $get['billno'] : ""; ?>' readonly>
                            </div>
                        </div>
                        <div class="row">
                            <?php if ($addflag != 1) { ?>
                                <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 "></div>
                                <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad text-left" style="margin-top: 25px">
                                    <button type="button" class="btn btn-primary btn-square challanupload " >
                                        Challan Upload <i class="fa fa-cloud-upload"></i>
                                    </button>
                                </div>                       
                                <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4  pad text-left" style="margin-top: 25px">
                                    <button type="button" class="btn btn-primary btn-square challanview ">
                                        Challan View <i class="fa fa-eye"></i>
                                    </button>
                                </div>
                            <?php } ?>
                            <?php if ($get["billno"] != '') { ?>
                                <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad text-left">
                                    <!--<img src="assets/img/invoice2.png" class="invoice" style="width: 75px; height: 75px;" onclick="window.open('<?= $invprintfile ?>?chalno=<?= $qvouch; ?>&series=<?= $zseries; ?>&billno=<?= $get["billno"]; ?>', 'popUponclickWindow', 'height=600,width=970,left=20,top=20, resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');">-->
                                    <img src="assets/img/invoice2.png" class="invoice" style="width: 75px; height: 75px;" onclick="window.open('<?= $invprintfile ?>?>&series=<?= $zseries; ?>&billno=<?= $get["billno"]; ?>', 'popUponclickWindow', 'height=600,width=970,left=20,top=20, resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');">
                                </div>
                            <? } ?>
                        </div>

                    </div>
                    <div class="panel-footer">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
            </div>
            <div class="col-lg-6 col-md-12 col-sm-12 col-xs-12 ">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title">Item Details</h3>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class='row table-responsive'>
                            <table class="table" width="100%">
                                <tr>
                                    <th style="width: 5%"></th>
                                    <th style="width: 7%">Sr</th>
                                    <th style="width: 33%">Item Name</th>
                                    <th style="width: 12%">Size</th>
                                    <th style="width: 12%">Act.Size</th>
                                    <th style="width: 12%">Unit</th>
                                    <th style="width: 12%;text-align: right;">Qty.</th>
                                </tr>
                                <tbody id="tabledata">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 0;
                                        while ($tran = mysqli_fetch_array($gettran)) {
                                            Echo "<tr>";
                                            Echo "<td align='center' valign='top'><span class='remove fa fa-times ' name='remove' value='remove' style='display:none;'></span></td>";
                                            Echo "<td><input type='text' value='$tran[srno]'  name='srno_$ctr'  class='form-control form text-right' readonly></td>";
                                            Echo "<td>";
                                            Echo "<select class='form form-control detailpart itemname' name='itemname_$ctr' style='width:100%'>";
                                            Echo "<option value='$tran[itemname]'>$tran[itemname]</option>";

                                            $getditem = mysqli_query($conn, "SELECT `code`, `name` FROM `item` GROUP BY `name` ORDER BY `name` ")or die(mysqli_error($conn));
                                            while ($ditem = mysqli_fetch_array($getditem)) {
                                                Echo "<option value='$ditem[name]'>$ditem[name]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "</td>";

                                            Echo "<td>";
                                            Echo "<select class='form form-control detailpart size' name='size_$ctr' style='width:100%'>";
                                            Echo "<option value='$tran[size]'>$tran[size]</option>";
                                            $getdsize = mysqli_query($conn, "SELECT `size` FROM `item` where `name`='$tran[itemname]' GROUP BY `size` ORDER BY `size` ")or die(mysqli_error($conn));
                                            while ($dsize = mysqli_fetch_array($getdsize)) {
                                                Echo "<option value='$dsize[size]'>$dsize[size]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "</td>";

                                            Echo "<td>";
                                            Echo "<select class='form form-control detailpart actsize' name='actsize_$ctr' style='width:100%'>";
                                            Echo "<option value='$tran[actsize]'>$tran[actsize]</option>";
                                            $getdsize2 = mysqli_query($conn, "SELECT `size` FROM `item` where `name`='$tran[itemname]' GROUP BY `size` ORDER BY `size` ")or die(mysqli_error($conn));
                                            while ($dsize2 = mysqli_fetch_array($getdsize2)) {
                                                Echo "<option value='$dsize2[size]'>$dsize2[size]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "</td>";

                                            Echo "<td>";
                                            Echo "<input type='text' value='$tran[unit]' name='unit_$ctr' class='form-control form unit' readonly>";
                                            Echo "</td>";

                                            Echo "<td><input type='text' value='$tran[qty]' name='qty_$ctr'  class='form-control form qty text-right'></td>";
                                            Echo "<input type='hidden' value='$tran[rate]'  name='rate_$ctr' class='form-control form rate' >";
                                            Echo "</tr>";
                                            $ctr++;
                                        }
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="row">
<!--                            <input type="button" style="height: 30px; width: 30px; padding: 0px" id="addnew" class="btn btn-success btn-lg form" accode="addnew2" value="+" />-->
                            <button type="button" class="btn btn-success btn-square form"  id="addnew">
                                <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row'></i>    
                            </button>
                            <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : 0; ?>" />  
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <div class="modal fade" id="formModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="myModalLabel">Challan Upload & Download</h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-lg-6 upload">
                            <p>To Download Right Click And Click On Save As..</p>
                            <form id="upload_form" enctype="multipart/form-data" method="post">
                                <div class="col-lg-12">
                                    <span class="btn btn-default btn-file btn-square btn-warning">
                                        Choose File <input type="file" name="file1" id="file1">
                                    </span>
                                    <input type="hidden" name="chalno" id="chalno" value='<?php Echo ($addflag != 1) ? $get['chalno'] : "$qvouch"; ?>'>
                                    <input type="hidden" name="series" id="series"  value="<?php Echo $_GET[series]; ?>">
                                    <input type="button" value="Upload File" class="btn btn-square btn-primary" id="uploadbtn">
                                </div>
                                <div class="col-lg-12">
                                    <div class="progress">
                                        <progress id="progressBar" class="progress-bar"  role="progressbar" value="0" max="100" style="width:100%;color: red;"></progress>
                                    </div>
                                    <h3 id="status"></h3>
                                    <p id="loaded_n_total"></p>
                                </div>
                            </form>
                        </div>
                        <div class="col-lg-6 upload">
                            <img id="preview" src="#" alt="your image" />
                        </div>
                        <div class="col-lg-12 mview">
                            <img  src="<?php Echo ($addflag != 1) ? $img[imgpath] : ''; ?>" alt="Challan Image" id="mpreview" />
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" >
        <div class="modal-dialog modal-lg" style="width: 100%;">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title" id="myLargeModalLabel">Search <? Echo $formname; ?></h4>
                </div>
                <div class="modal-body">
                    <input type="hidden" value="<?php Echo $zseries ?>" class="form-control series">
                    <input type="hidden" value="<?php Echo $get[chalno]; ?>" class="form-control chalno">
                    <div class="row">
                        <div class="col-lg-4">
                            <select class="form-control field">
                                <option value="2">Chalno</option>
                                <option value="1">Date</option>
                                <option value="3">Bill No.</option>
                                <!--<option value="4">Custcode</option>-->
                                <option value="4">Customer</option>
                                <option value="5">Vessel</option>
                                <option value="6">Rank</option>
                                <option value="7">CrewName</option>
                                <option value="8">PP No.</option>
                                <option value="9">Id No.</option>
                                <option value="10">Remark</option>
                            </select>
                        </div>
                        <div class="col-lg-4">
                            <input type="text" id="searchTerm" value="" class="form-control chalno">
                        </div>
                        <div class="col-lg-4">
                            <input type="button"  class="btn btn-primary load" value="Search">
                        </div>
                    </div>

                    <div class="col-lg-12 loader text-center">
                        <img src="assets/img/search.gif" alt="loading">       
                    </div>
                    <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                        <table id="" class="table table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr style="background-color: #05B2EA;">
                                    <th width="6%">Date</th>
                                    <th width="5%">Chalno</th>
                                    <th width="5%">Bill No.</th>
<!--                                    <th>Custcode</th>-->
                                    <th width="15%">Customer</th>
                                    <th width="10%">Vessel</th>
                                    <th width="7%">Rank</th>
                                    <th width="13%">Crew Name</th>
                                    <th width="5%">PP No.</th>
                                    <th width="5%">Id No.</th>
                                    <th width="15%">Remark</th>
                                    <th width="2%"></th>
                                </tr>
                            </thead>
                            <tbody class="tbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button"  class="btn btn-primary previous" value="Previous">
                            <input type="button"  class="btn btn-primary next" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
