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

// ini_set("display_errors", 1);
// error_reporting(E_ALL);

$formname = 'Sale Entry';
$maindb = 'sale';
$accdb = 'actran';

$file = 'sale.php';
$grid = '';
$source = 'SL';
$type = "S";

$keyfld = "vouchno";
$trandbexist = 0;
$focusfld = "name";
$printfile = 'sale_pdf.php';
$usertype = $_SESSION["usertype"];
$username = $_SESSION["username_" . $dbname];
$salelock = $_SESSION["salelock"];

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zyear = right($zdb, 4);
$zStartDate = getzdates($zyear, 'S');
$zEndDate = getzdates($zyear, 'E');
date_default_timezone_set('Asia/Kolkata');
$adduser = $_SESSION['username_' . $dbname];

mysqli_select_db($conn, $zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {

    $a = 1;
    $username = $_SESSION["username_" . $dbname];

    $vouchdt = $_POST["vouchdt"];
    $invno = $_POST["invno"];
    $qvouch = $_POST["vouchno"];

    $mvouch = $qvouch;

    foreach ($_POST as $key => $value) {
        $exclude_fld = ["test"];
        if (in_array($key, $exclude_fld)) {
            continue;
        }
        $$key = $value;
        // echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";
    }


    //     echo "<pre>";
// print_r($_FILES['images']);
// echo "</pre>";
// exit;

    if ($a == 1) {


        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `accmast` WRITE, `$accdb` WRITE, sale_images WRITE") or die(mysqli_error($conn));



        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$maindb` 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;

            // $geninvn = mysqli_query($conn, "SELECT MAX(CAST(SUBSTRING_INDEX(invno,'-',-1) AS UNSIGNED)) AS maxinvno FROM `$maindb` WHERE type='$type'") or die(mysqli_error($conn));
            // $row = mysqli_fetch_assoc($geninvn);
            // $qinvno = (!empty($row['maxinvno'])) ? ($row['maxinvno'] + 1) : 1;

            // $invprefix = "";
            // if ($type == "A") {
            //     $invprefix = "GG";
            // } elseif ($type == "S") {
            //     $invprefix = "GM";
            // }

            // $minvno = $invprefix . "-" . $qinvno;
        }


        ######################## ACCOUNTS PART ######################################
        mysqli_query($conn, "DELETE FROM `$accdb` WHERE vouchno='$mvouch' AND `source`='$source'") or die(mysqli_error($conn));

        if ($netamt > 0) {

            if ($type == "R") {
                $purcode = 15;
            } else {
                $purcode = 14;
            }

            $minus_amt = ($netamt * -1);

            // sale part
            mysqli_query($conn, "INSERT INTO `$accdb`(`date`, `vouchno`, `source`, `trnno`, `accode`, `grpcode`, `amount`)
                                              VALUES ('$vouchdt', '$mvouch', '$source', '001', '$purcode', '005', '$netamt')") or die(mysqli_error($conn));

            // party part
            mysqli_query($conn, "INSERT INTO `$accdb`(`date`, `vouchno`, `source`, `trnno`, `accode`, `grpcode`, `amount`)
                                              VALUES ('$vouchdt', '$mvouch', '$source', '002', '$party', '010', '$minus_amt')") or die(mysqli_error($conn));
        }


        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `invno`, `party`, `type`, 
                                                       `qty`, `descrip`, `rate`, `amount`, `unit`, `roe`, `roeamt`, 
                                                       `carryperc`, `carryamt`, `dumpqty`, `dumplabel`, `dumprate`, `dumpamt`,
                                                       `othchgs`, `netamt`, `unitlabel`, `unitqty`, `unitamt`, `unittotal`)
                                               VALUES ('$qvouch', '$vouchdt', '$invno', '$party', '$type', 
                                                       '$qty', '$descrip', '$rate', '$amount', '$unit', '$roe', '$roeamt', 
                                                       '$carryperc', '$carryamt', '$dumpqty', '$dumplabel', '$dumprate', '$dumpamt',
                                                       '$othchgs', '$netamt', '$unitlabel', '$unitqty', '$unitamt', '$unittotal')") or die(mysqli_error($conn));
        } else {
            $mvouch = $qvouch;

            mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt', `party`='$party', `type`='$type', `qty`='$qty', 
                                                      `descrip`='$descrip', `rate`='$rate', `amount`='$amount', `invno`='$invno',
                                                      `unit`='$unit', `roe`='$roe', `roeamt`='$roeamt', `carryperc`='$carryperc',
                                                      `carryamt`='$carryamt', `dumpqty`='$dumpqty', `dumplabel`='$dumplabel',
                                                      `dumprate`='$dumprate', `dumpamt`='$dumpamt', `othchgs`='$othchgs', `netamt`='$netamt', 
                                                      `unitlabel`='$unitlabel', `unitqty`='$unitqty', `unitamt`='$unitamt', `unittotal`='$unittotal'
                                                      WHERE vouchno = '$mvouch' ") or die(mysqli_error($conn));
        }




        // ==========================
        // DELETE OLD IMAGES
        // ==========================
        if (!empty($_POST['deleteImages'])) {

            foreach ($_POST['deleteImages'] as $id) {

                $q = mysqli_query($conn, "SELECT imagename FROM sale_images WHERE id='$id' AND vouchno='$mvouch'");

                if ($r = mysqli_fetch_assoc($q)) {

                    @unlink("images/Sales/" . $r['imagename']);

                    mysqli_query($conn, "DELETE FROM sale_images WHERE id='$id' AND vouchno='$mvouch'");
                }
            }
        }

        // ==========================
        // FOR REPLACED IMAGES
        // ==========================

        if (!empty($_FILES['replaceImages']['name'])) {

            foreach ($_FILES['replaceImages']['name'] as $id => $name) {

                if ($_FILES['replaceImages']['error'][$id] != 0) {
                    continue;
                }

                if ($_FILES['replaceImages']['size'][$id] > 307200) {

                    $_SESSION['img_error'] = "Replacement image exceeds 300 KB.";

                    continue;

                }



                $q = mysqli_query($conn, "SELECT imagename FROM sale_images WHERE id='$id' AND vouchno='$mvouch'");

                if (!$r = mysqli_fetch_assoc($q)) {
                    continue;
                }

                @unlink("images/Sales/" . $r['imagename']);

                $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));

                if (!in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'webp'])) {
                    continue;
                }

                $filename = uniqid($mvouch . "_") . "." . $ext;

                move_uploaded_file(
                    $_FILES['replaceImages']['tmp_name'][$id],
                    "images/Sales/" . $filename
                );

                mysqli_query(
                    $conn,
                    "UPDATE sale_images
             SET imagename='$filename'
             WHERE id='$id'
             AND vouchno='$mvouch'"
                );
            }
        }

        // ==========================
        // UPLOAD NEW IMAGES
        // ==========================

        // echo "Upload Started";
        // exit;

        if (!empty($_FILES['images']['name'][0])) {


            $path = "images/Sales/";

            if (!is_dir($path)) {
                mkdir($path, 0777, true);
            }

            $count = mysqli_num_rows(mysqli_query($conn, "SELECT id FROM sale_images WHERE vouchno='$mvouch'"));


            foreach ($_FILES['images']['tmp_name'] as $k => $tmp) {

                if ($count >= 10) {
                    break;
                }

                if ($_FILES['images']['error'][$k] != 0) {
                    continue;
                }

                if ($_FILES['images']['size'][$k] > 307200) {

                    $_SESSION['img_error'] = "Image " .
                        htmlspecialchars($_FILES['images']['name'][$k]) .
                        " exceeds 300 KB.";

                    continue;

                }


                $ext = strtolower(pathinfo($_FILES['images']['name'][$k], PATHINFO_EXTENSION));

                if (!in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'webp'])) {
                    continue;
                }

                $filename = uniqid($mvouch . "_") . "." . $ext;

                if (move_uploaded_file($tmp, $path . $filename)) {

                    mysqli_query(
                        $conn,
                        "INSERT INTO sale_images(vouchno, imagename)
                 VALUES('$mvouch','$filename')"
                    );

                    $count++;
                } else {

                    echo "Move Failed";

                }


            }


        }


        mysqli_query($conn, "UNLOCK TABLES");

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

if ($addflag != 1) {
    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }

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

    $getparty = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode` = '$get[party]'") or die(mysqli_error($conn));
    $gtpt = mysqli_fetch_array($getparty);
}

/* DELETE */
if (isset($_GET["delete"])) {
    mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `accmast` WRITE, `$accdb` WRITE, `sale_images` WRITE") or die(mysqli_error($conn));

    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' ";

    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    delnew($zdb, "$accdb", "$delcond", "$file?mode=last");


    $q = mysqli_query($conn, "SELECT imagename FROM sale_images WHERE vouchno='$_GET[vouchno]'");

    while ($r = mysqli_fetch_assoc($q)) {

        @unlink("images/Sales/" . $r['imagename']);

    }

    mysqli_query($conn, "DELETE FROM sale_images WHERE vouchno='$_GET[vouchno]'");

    mysqli_query($conn, "UNLOCK TABLES");
}

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

include("includes/linkshead.php");
?>
<script src="MyScript/sale.js?a=<?= mt_rand() ?>" type="text/javascript"></script>
<script src="MyScript/sale_search.js?b=<?= mt_rand() ?>" type="text/javascript"></script>
<script src="MyScript/shortkeys.js" type="text/javascript"></script>
<style type="text/css">
    .bgsale {
        background-color: #e3f2e1;
    }

    .bgpurch {
        background-color: #8fcccc;
    }

    .row {
        width: 100%;
        margin-right: 0px;
        margin-left: 0px;
    }

    .upload-btn-wrapper {
        position: relative;
        overflow: hidden;
        display: inline-block;
    }

    .skbtn {

        width: 170px;

        height: 42px;

        border-radius: 8px;

        border: 2px solid #4f6df5;

        background: #fff;

        color: #4f6df5;

        font-weight: 600;

        transition: .25s;

    }

    .skbtn:hover {

        background: #4f6df5;

        color: #fff;

    }

    .upload-btn-wrapper input[type=file] {
        font-size: 100px;
        position: absolute;
        left: 0;
        top: 0;
        opacity: 0;
    }

    .skimg {
        width: 100%;
        max-width: 300px;
        height: 200px;
        object-fit: cover;
        border-radius: 6px;
        box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
    }

    .skimg {
        cursor: pointer;
    }


    .row {
        width: 100%;
        margin-right: 0px;
        margin-left: 0px;
    }

    .party-flex {
        display: flex;
        align-items: center;
    }

    .party-flex .select2-container {
        flex: 1 1 auto !important;
        /* ← TAKE ALL REMAINING WIDTH */
        min-width: 0 !important;
    }

    .create-party-btn {
        white-space: nowrap !important;
        margin-left: 6px !important;
        height: 28px !important;
        border-radius: 7px;
    }

    /* ===== Create Party Modal ===== */

    #partyModal .modal-dialog {
        max-width: 420px;
        /* margin-top: 10vh; */
    }

    #partyModal .modal-content {
        border-radius: 10px;
        border: none;
        box-shadow: 0 12px 35px rgba(0, 0, 0, 0.18);
    }

    /* Header */
    #partyModal .modal-header {
        background: #f6f8fa;
        border-bottom: 1px solid #e3e6ea;
        padding: 15px 18px;
    }

    #partyModal .modal-title {
        font-weight: 600;
        font-size: 16px;
        color: #2c3e50;
    }

    #partyModal .close {
        font-size: 22px;
        opacity: 0.5;
    }

    /* Body */
    #partyModal .modal-body {
        padding: 18px;
    }

    #partyModal label {
        font-size: 13px;
        font-weight: 600;
        color: #555;
        margin-bottom: 6px;
    }

    #partyModal input[type="text"] {
        height: 38px;
        border-radius: 6px;
        border: 1px solid #ced4da;
        font-size: 14px;
    }

    #partyModal input[type="text"]:focus {
        border-color: #5cb85c;
        box-shadow: 0 0 0 2px rgba(92, 184, 92, 0.15);
    }

    /* Footer */
    #partyModal .modal-footer {
        border-top: 1px solid #e3e6ea;
        padding: 12px 18px;
    }

    #partyModal .btn-primary {
        background-color: #5cb85c;
        border-color: #4cae4c;
        border-radius: 6px;
        font-weight: 600;
        padding: 7px 18px;
    }

    #partyModal .btn-primary:hover {
        background-color: #4cae4c;
    }

    /* Small helper text (optional) */
    #partyModal .help-text {
        font-size: 12px;
        color: #888;
        margin-top: 5px;
    }

    /* Select2 same height as input */

    #partyModal .select2-container .select2-selection--single {
        height: 38px !important;
        border: 1px solid #ced4da !important;
        border-radius: 6px !important;
    }

    #partyModal .select2-container--default .select2-selection--single .select2-selection__rendered {
        line-height: 38px !important;
        padding-left: 12px !important;
        font-size: 14px;
    }

    #partyModal .select2-container--default .select2-selection--single .select2-selection__arrow {
        height: 38px !important;
        right: 6px;
    }

    #partyModal .select2-container {
        width: 100% !important;
    }

    .imageGallery {

        max-height: 650px;
        overflow: auto;

    }

    .imgCard {

        position: relative;

        margin-bottom: 15px;

    }

    .imgCard img {

        width: 100%;

        height: 150px;

        object-fit: cover;

        border-radius: 8px;

        cursor: pointer;

    }

    .imgBtns {

        justify-content: center; /* Center horizontally */
        
        align-items: center;

        display: flex;

        gap: 8px;

        margin-top: 8px;

    }

    .imgBtns button {

        /* flex: 1; */

        width: 100px;

        height: 34px;

        border-radius: 6px;

        border: none;

        font-weight: 600;

        transition: .25s;

    }

    .imageHeader {

        display: flex;

        align-items: center;

        justify-content: space-between;

        gap: 15px;

        margin-bottom: 15px;

        flex-wrap: wrap;

    }

    .imageCountBox {

        font-size: 18px;

        font-weight: 600;

    }

    .imageLimit {

        color: #888;

        font-size: 13px;

    }

    .replaceBtn {

        background: #ffb347;

        color: #fff;

    }

    .replaceBtn:hover {

        background: #f39c12;

    }

    .deleteBtn {

        background: #e74c3c;

        color: #fff;

    }

    .deleteBtn:hover {

        background: #c0392b;

    }
</style>
</head>

<body>

    <form action='' method='POST' enctype="multipart/form-data">
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title" style="color:#78909C"><a class="myPrint"><?php echo $formname; ?></a>
                        </h3>
                    </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 marg">Challan No.</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-2 col-xs-2 pad">
                                <input type="text" name="vouchno" id="vouchno"
                                    value="<?php echo ($addflag != 1) ? $get['vouchno'] : $qvouch; ?>"
                                    class="form-control form" readonly>
                                <input type="hidden" class="form-control form" name="cashvouch"
                                    value="<?php echo ($addflag != 1) ? $get["cashvouch"] : ""; ?>">
                            </div>

                            <div class="col-lg-1 col-md-1 col-sm-2 col-xs-2 pad text-right">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 pad">
                                <input type="date" name="vouchdt" id="vouchdt"
                                    value="<?php echo ($addflag != 1) ? $get['vouchdt'] : date('Y-m-d'); ?>"
                                    class="form-control form vouchdt focusfield" required autofocus>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2  text-right pad">
                                <label class="label-control marg">Customer</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <div class="party-flex">
                                    <select id="party" name="party" class="form-control form party select2" required>
                                        <option value="<?php echo ($addflag != 1) ? $gtpt['accode'] : ""; ?>">
                                            <?php echo ($addflag != 1) ? $gtpt['name'] : ""; ?>
                                        </option>
                                        <?php
                                        $mown = mysqli_query($conn, "SELECT * FROM `accmast` WHERE grpcode = '010' order by `name`") or die(mysqli_error($conn));
                                        while ($trow = mysqli_fetch_array($mown)) {
                                            echo "<option value=$trow[accode]>$trow[name]</option>";
                                        }
                                        ?>
                                    </select>
                                    <button type="button" class="btn btn-success create-party-btn form"
                                        id="createPartyBtn">
                                        + Create
                                    </button>
                                </div>

                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad ">
                                <label class="label-control marg">Type</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-4 col-xs-4 pad">
                                <select id="type" name="type" class="select2 form-control form type" required>
                                    <?php
                                    $typeOptions = [
                                        'A' => 'Air',
                                        'S' => 'Sea'
                                    ];

                                    foreach ($typeOptions as $value => $label) {
                                        $selected = ($get['type'] == $value) ? 'selected' : '';
                                        echo "<option value=\"$value\" $selected>$label</option>";
                                    }
                                    ?>
                                </select>

                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg"> Shipment No.</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-4 col-xs-4 pad">
                                <input type="text" name="invno"
                                    value="<?php echo ($addflag != 1) ? $get['invno'] : ""; ?>"
                                    class="form-control form invno">
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-center pad">
                                <label class="label-control marg"> Sr. No.</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 text-center pad">
                                <label class="label-control marg"> Description</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-center pad">
                                <label class="label-control marg"> Qty</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-center pad">
                                <label class="label-control marg"> Unit</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-center pad">
                                <label class="label-control marg"> Rate</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-1 col-xs-1 text-center pad">
                                <label class="label-control marg"> Amount</label>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="text"  value="1" class="form-control form text-right" readonly tabindex="-1">
                            </div>
                            <div class="col-lg-4 col-md-3 col-sm-4 col-xs-4 pad">
                                <input type="text" name="descrip"
                                    value="<?php echo ($addflag != 1) ? $get['descrip'] : ""; ?>"
                                    class="form-control form descrip">
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="number" name="qty" step="any"
                                    value="<?php echo ($addflag != 1) ? $get['qty'] : "0"; ?>"
                                    class="form-control form text-right qty">
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input list="units" name="unit" value="<?php echo $get['unit'] ?>"
                                    class="unit form-control form" autocomplete="off">
                                <datalist id="units">
                                    <option value="">
                                        <?php
                                        $mhsncode = mysqli_query($conn, "SELECT DISTINCT(`unit`) FROM `$maindb` WHERE unit != '' ") or die(mysqli_error($conn));
                                        while ($thsncode = mysqli_fetch_array($mhsncode)) {
                                            echo "<option value='$thsncode[unit]'>";
                                        }
                                        ?>
                                </datalist>
                            </div>
                        
                            <div class="col-lg-2 col-md-3 col-sm-2 col-xs-2 pad">
                                <input type="number" name="rate" step="any"
                                    value="<?php echo ($addflag != 1) ? $get['rate'] : "0"; ?>"
                                    class="form-control form text-right rate">
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-2 col-xs-2 pad">
                                <input type="number" name="amount" step="any"
                                    value="<?php echo ($addflag != 1) ? $get['amount'] : "0"; ?>"
                                    class="form-control form text-right amount">
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 text-right pad"></div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg"> ROE</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <div class="col-xs-9 pad">
                                    <input type="number" step="any" name="roe"
                                        value="<?php echo ($addflag != 1) ? $get['roe'] : ""; ?>"
                                        class="form-control form text-right roe">
                                </div>
                                <div class="col-xs-3 text-center pad">
                                    <label class="label-control marg"> X </label>
                                </div>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-4 col-xs-4 pad">
                                <input type="number" step="any" name="roeamt"
                                    value="<?php echo ($addflag != 1) ? $get['roeamt'] : ""; ?>"
                                    class="form-control form text-right roeamt" readonly tabindex="-1">
                            </div>
                        </div>

                        <div class="row showairchgs">
                            <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5 text-right pad"></div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg"> Carrying Charges </label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <div class="col-xs-4 pad">
                                    <input type="number" step="any" name="unitqty"
                                        value="<?php echo ($addflag != 1) ? $get['unitqty'] : ""; ?>"
                                        class="form-control form text-right unitqty">
                                </div>
                                <div class="col-xs-3 text-center pad">
                                    <input list="unitlabels" name="unitlabel" value="<?php echo $get['unitlabel'] ?>"
                                        maxlength="4" class="unitlabel form-control form" autocomplete="off">
                                    <datalist id="unitlabels">
                                        <option value=""></option>
                                        <?php
                                        $muntl = mysqli_query($conn, "SELECT DISTINCT(`unitlabel`) FROM `$maindb` WHERE unitlabel !=''") or die(mysqli_error($conn));
                                        while ($funtl = mysqli_fetch_array($muntl)) {
                                            echo "<option value='$funtl[unitlabel]'>";
                                        }
                                        ?>
                                    </datalist>
                                </div>
                                <div class="col-xs-5 pad">
                                    <input type="number" step="any" name="unitamt"
                                        value="<?php echo ($addflag != 1) ? $get['unitamt'] : ""; ?>"
                                        class="form-control form text-right unitamt">
                                </div>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-4 col-xs-4 pad">
                                <input type="number" name="unittotal"
                                    value="<?php echo ($addflag != 1) ? $get['unittotal'] : ""; ?>"
                                    class="form-control form text-right unittotal" readonly tabindex="-1">
                            </div>
                        </div>

                        <div class="row showcarrychgs">
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 text-right pad"></div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg"> Carrying Charges</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <div class="col-xs-9 pad">
                                    <input type="number" step="any" name="carryperc"
                                        value="<?php echo ($addflag != 1) ? $get['carryperc'] : ""; ?>"
                                        class="form-control form text-right carryperc">
                                </div>
                                <div class="col-xs-3 text-center pad">
                                    <label class="label-control marg"> %</label>
                                </div>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-4 col-xs-4 pad">
                                <input type="number" name="carryamt"
                                    value="<?php echo ($addflag != 1) ? $get['carryamt'] : ""; ?>"
                                    class="form-control form text-right carryamt" readonly tabindex="-1">
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5 text-right pad"></div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg"> Anti-dumping</label>
                            </div>

                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <div class="col-xs-4 pad">
                                    <input type="number" step="any" name="dumpqty"
                                        value="<?php echo ($addflag != 1) ? $get['dumpqty'] : ""; ?>"
                                        class="form-control form text-right dumpqty">
                                </div>
                                <div class="col-xs-3 text-center pad">
                                    <input list="dumplabels" name="dumplabel" value="<?php echo $get['dumplabel'] ?>"
                                        maxlength="6" class="dumplabel form-control form" autocomplete="off">
                                    <datalist id="dumplabels">
                                        <option value=""></option>
                                        <?php
                                        $muntl = mysqli_query($conn, "SELECT DISTINCT(`dumplabel`) FROM `$maindb` WHERE dumplabel !=''") or die(mysqli_error($conn));
                                        while ($funtl = mysqli_fetch_array($muntl)) {
                                            echo "<option value='$funtl[dumplabel]'>";
                                        }
                                        ?>
                                    </datalist>
                                </div>
                                <div class="col-xs-5 pad">
                                    <input type="number" step="any" name="dumprate"
                                        value="<?php echo ($addflag != 1) ? $get['dumprate'] : ""; ?>"
                                        class="form-control form text-right dumprate">
                                </div>
                            </div>

                            <div class="col-lg-2 col-md-3 col-sm-4 col-xs-4 pad">
                                <input type="number" name="dumpamt"
                                    value="<?php echo ($addflag != 1) ? $get['dumpamt'] : ""; ?>"
                                    class="form-control form text-right dumpamt" readonly tabindex="-1">
                            </div>
                        </div>

                        <!-- <div class="row">
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 text-right pad"></div>
                            <div class="col-lg-2 col-md-3 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg"> Other Charges</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-4 col-xs-4 pad">
                                <input type="number" name="othchgs"
                                    value="<?php echo ($addflag != 1) ? $get['othchgs'] : ""; ?>"
                                    class="form-control form text-right othchgs">
                            </div>
                        </div> -->

                        <div class="row">
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 text-right pad"></div>
                            <div class="col-lg-2 col-md-3 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg"> Net Amount</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-4 col-xs-4 pad">
                                <input type="number" name="netamt"
                                    value="<?php echo ($addflag != 1) ? $get['netamt'] : ""; ?>"
                                    class="form-control form text-right netamt" readonly tabindex="-1">
                            </div>
                        </div>


                        <!-- <hr> -->

                        <?php if ($addflag != 1) { ?>

                            <!-- <div class="row">
                                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 pad">
                                    <div class="col-lg-3 col-md-3 col-sm-6 col-xs-6 pad">
                                        <div class="upload-btn-wrapper">
                                            <button class="skbtn">Choose Image</button>
                                            <input type="file" class="form image" name="image" accept="image/*" />
                                        </div>
                                    </div>
                                    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
                                        <img class="view1 skimg"
                                            src="<?php echo ($addflag != 1) ? $get['image'] . "?a=" . mt_rand() : "" ?>"
                                            alt="Image" />
                                    </div>
                                </div>
                            </div> -->
                        <?php } ?>

                    </div>

                    <div class="panel-footer">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
            </div>

            <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 pad">
                <div class="panel panel-default">

                    <div class="panel-heading">
                        <b>Images</b>
                    </div>

                    <div class="panel-body">

                        <?php
                        $count = 0;

                        if ($addflag != 1) {

                            $count = mysqli_num_rows(
                                mysqli_query($conn, "SELECT id FROM sale_images WHERE vouchno='$get[vouchno]'")
                            );

                        }

                        ?>

                        <div class="imageHeader">

                            <div class="imageCountBox">
                                <b>Images (<span class="imageCount">
                                        <?= $count ?>
                                    </span>/10)</b>
                            </div>

                            <div class="upload-btn-wrapper">
                                <button class="skbtn form" type="button">
                                    Choose Images
                                </button>

                                <input type="file" id="images" name="images[]" class="form" accept="image/*" multiple>
                            </div>

                            <div class="imageLimit">
                                Maximum 10 Images
                            </div>

                        </div>


                        <hr>

                        <!-- Images -->
                        <div class="row imageGallery dbImageGallery">

                            <?php

                            if ($addflag != 1) {

                                $q = mysqli_query($conn, "SELECT * FROM sale_images WHERE vouchno='$get[vouchno]' ORDER BY id");

                                while ($r = mysqli_fetch_assoc($q)) {

                                    $imgPath = "images/Sales/" . $r['imagename'];
                                    $version = file_exists($imgPath) ? filemtime($imgPath) : time();
                                    ?>

                                    <div class="col-md-6 imgCard" data-id="<?= $r['id'] ?>" style="padding: 20px;">

                                        <img src="<?= $imgPath ?>?v=<?= $version ?>" class="skimg">

                                        <div class="imgBtns">

                                            <button type="button" class="replaceBtn form">
                                                Replace
                                            </button>

                                            <input type="file" name="replaceImages[<?= $r['id'] ?>]" class="replaceInput"
                                                accept="image/*" style="display:none;">

                                            <button type="button" class="deleteBtn deleteDbImage form"
                                                data-id="<?= $r['id'] ?>">
                                                Delete
                                            </button>

                                        </div>
                                    </div>

                                    <?php

                                }

                            }

                            ?>

                        </div>



                    </div>


                </div>

            </div>

        </div>
    </form>


    <div class="modal fade" id="partyModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content" style="width: 550px;border-radius:10px;">

                <div class="modal-header">
                    <button class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Create Party</h4>
                </div>

                <div class="modal-body">
                    <!-- <div class="form-group">
                        <label>Party Name</label>
                        <input type="text" id="party_name" class="form-control" maxlength="50">
                    </div>
                    <div class="form-group">
                        <label>Party Name</label>
                        <input type="text" id="party_name" class="form-control" maxlength="50">
                    </div> -->

                    <div class="row" style="margin-bottom:10px;">

                        <div class="col-md-3 text-right pad">
                            <label class="label-control" style="margin-top:7px;">Name</label>
                        </div>

                        <div class="col-md-8">
                            <input type="text" class="form-control party_name" id="party_name" name="party_name"
                                maxlength="40" style="border-radius:4px;" required>
                        </div>

                    </div>

                    <div class="row" style="margin-bottom:10px;">

                        <div class="col-md-3 text-right pad">
                            <label class="label-control" style="margin-top:7px;">State</label>
                        </div>

                        <div class="col-md-4">
                            <select name="mstate" class="form-control mstate select2" style="width:100%;">

                                <option value=""></option>

                                <?php
                                $mown4 = mysqli_query($conn, "SELECT `code`, `name` FROM `state` ORDER BY name") or die(mysqli_error($conn));
                                while ($trow4 = mysqli_fetch_array($mown4)) {
                                    echo "<option value='$trow4[name]'>$trow4[name]</option>";
                                }
                                ?>

                            </select>
                        </div>

                        <div class="col-md-2 text-right pad">
                            <label class="label-control" style="margin-top:7px;">Code</label>
                        </div>

                        <div class="col-md-2">
                            <input type="text" class="form-control mstatecode" name="mstatecode" readonly>
                        </div>

                    </div>

                    <div class="row">

                        <div class="col-md-3 text-right pad">
                            <label class="label-control" style="margin-top:7px;">Mobile</label>
                        </div>

                        <div class="col-md-4">
                            <input type="text" class="form-control mmobile" name="mmobile" maxlength="10"
                                style="border-radius:4px;">
                        </div>

                    </div>
                </div>

                <div class="modal-footer">
                    <button class="btn btn-primary" id="saveParty">Save</button>
                </div>

            </div>
        </div>
    </div>

    <div class="modal fade" id="imgModal" tabindex="-1">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Image Preview</h4>
                </div>
                <div class="modal-body text-center">
                    <img id="fullImage" src="" style="max-width:100%;max-height:60vh;">
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade bs-example-modal-lg searchModal" tabindex="-1" role="dialog"
        aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg">
            <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 $file ?>" class="form-control file">
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="">Sort By</option>
                                <option value="1">Voucher</option>
                                <option value="2">Date</option>
                                <option value="3">Customer</option>
                                <option value="4">Shipment No.</option>
                                <option value="5">Dsecription</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control soperator">
                                <option value="">Type of Search</option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control sortBy">
                                <option value="">Sort By</option>
                                <option value="1">Voucher</option>
                                <option value="2">Date</option>
                                <option value="3">Customer</option>
                                <option value="4">Shipment No.</option>
                                <option value="5">Dsecription</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control orderAs">
                                <option value="desc">Desc</option>
                                <option value="asc">Asc</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <input type="text" value="" class="form-control ssearch" placeholder="Search Here">
                        </div>
                        <div class="col-lg-2">
                            <input type="button" class="btn btn-primary sload" value="Search">
                        </div>
                    </div>

                    <div class="col-lg-12 sloader text-center">
                        <!--<img src="images/search.gif" alt="loading">-->
                    </div>
                    <div class="col-lg-12 snoRecord text-center">
                        <h3>NO RECORDS FOUND</h3>
                    </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 class="mySearchTr">
                                    <th width="5%">Voucher</th>
                                    <th width="10%">Date</th>
                                    <th width="25%">Customer</th>
                                    <th width="5%">Type</th>
                                    <th width="10%">Shipment No.</th>
                                    <th width="10%">Description</th>
                                    <!-- <th width="10%">Amount</th> -->
                                    <th width="2%">Action</th>
                                </tr>
                            </thead>
                            <tbody class="sTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button" class="btn btn-primary sprev" value="Previous">
                            <input type="button" class="btn btn-primary snext" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
    <?php

    if (!empty($_SESSION['img_error'])) {

        echo "<script>
            alert('" . addslashes($_SESSION['img_error']) . "');
          </script>";

        unset($_SESSION['img_error']);

    }

    ?>
</body>

</html>