<?php
if (!$isInclude) {
    require("includes/check_auth_user.php");
    require("includes/dbconnect.php");
    include('includes/function.php');
}
mysqli_query($conn, "SET NAMES 'utf8'");
mysqli_query($conn, 'SET CHARACTER SET utf8');

$formname = 'Estimate';
$maindb = 'salemain';
$trandb = 'saletran';
$file = '';
$source = "SL";
$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdir"];
mysqli_select_db($conn, $zdb);
$mht = 'height:20px;';

$admin = $_SESSION["admin_" . $dbname];
$vouchno = $_REQUEST["vouchno"];


$fseries = $_SESSION["series"];
$keycond = "AND series = '$fseries'";
$admin = $_SESSION["admin_" . $dbname];

$vouchno = ($_REQUEST["vouchno"]) ? $_REQUEST["vouchno"] : $mvouchno;

$mainqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS mydt FROM `$maindb` WHERE vouchno='$vouchno' $keycond") or die(mysqli_error($conn));
$getmdata = mysqli_fetch_array($mainqry);
$vouchdt = $getmdata['vouchdt'];

$prnnum = $getmdata['prnnum'];
$newprnnum = $prnnum + 1;

mysqli_query($conn, "Update `$maindb` SET `prnnum`= '$newprnnum' where vouchno = '$vouchno'") or die(mysqli_error($conn));

$getpaccode = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`= '$getmdata[party]'") or die(mysqli_error($conn));
$cust = mysqli_fetch_array($getpaccode);
$add1 = $cust['add1'];
$add2 = $cust['add2'];
$add3 = $cust['add3'];
$city = $cust['city'];
$pin = $cust['pin'];
$paytype = $getmdata['paytype'];

if ($getmdata['cashparty'] != ""){
    $party = $getmdata['cashparty'];
}else{
    $party = $cust['name'];
}

?>

<!DOCTYPE HTML>
<html lang="en-US">
    <?php
    include("includes/linkshead.php");
    ?>
</head>
<style>
    body {
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
        padding-right: 5px;
        padding-left: 5px;
        padding-top: 5px;
        padding-bottom: 0px;
        font: 12pt "Tahoma";
    }

    .f12 {
        font-size: 12px;
    }

    .f11 {
        font-size: 11px;
    }

    .f13 {
        font-size: 13px;
        font-weight: bold;
        padding: 3px;
    }

    .f14 {
        font-size: 14px;
    }

    .bold {
        font-weight: bold;
    }

    .page {
        width: 140mm;
        min-height: 207mm;
        padding: 2mm;
        margin: 2mm auto;
        page-break-after: always;
    }

    .subpage {
        border: 1px solid black;
        padding: 1%;
    }

    .border {
        border: 1px solid black;
        color: black;
        padding-right: 2px;
        padding-left: 3px;
    }

    th {
        font-weight: bold;
        font-size: 13px;
    }

    @media print {
        @page {
            size: a5;
            margin: 0;
        }


        .page {
            width: 90%;
            /* width: 144mm; */
            min-height: 180mm;
            padding: 2mm;
            margin: 2mm auto;
            page-break-after: always;
        }

        .border {
            border: 1px solid black;
            color: black;
            padding-right: 2px;
            padding-left: 3px;
        }

        th {
            font-weight: bold;
            font-size: 13px;
        }

        .table2 {
            border: 2px solid black;
        }
    }
</style>
<?php
//require("includes/dbconnect.php");
$comp = mysqli_query($conn, "SELECT * FROM $zroot.`company`") or die(mysqli_errno($conn));
$zcomp = mysqli_fetch_assoc($comp);
header('Content-Type: text/html;charset=UTF-8');
ini_set('default_charset', 'utf-8');
$ht = "height:23px;";
?>

<body id="main_container" onload="window.print()">
    <div class="page">
        <div class="subpage">
            <table style="width:100%">
                <tr>
                    <td>
                <center style='color:black'>Subject to Mumbai Jurisdiction</center>
                </td>
                </tr>
                <tr>
                    <td>
                <center style='color:black'><U>CASH/CREDIT MEMO</U></center>
                </td>
                </tr>
                <tr>
                    <td>
                        <h3 style="font-family:Arial;font-weight:bolder;margin-top:5px;margin-bottom:3px">
                            <center style='color: green;'><?= $zcomp['compname']; ?></center>
                        </h3>
                    </td>
                </tr>
                <tr>
                    <td>
                        <b style="font-family:Arial;font-size:14px;">
                            <center style='color:black'>Onions, Potatoes and Garlic Wholesale & Retail Merchants</center>
                        </b>
                    </td>
                </tr>
                <tr>
                    <td>
                        <span style="font-family:Arial;font-size:14px;">
                            <center style='color:black'><?= $zcomp['hoadd1'] . $zcomp['hoadd2'] . $zcomp['hoadd3'] . $zcomp['hocity'] . " - " . $zcomp['hopin']; ?></center>
                        </span>
                    </td>
                </tr>
                <tr>
                    <td>
                        <span style="font-family:Arial;font-size:14px;">
                            <center style='color:black'><?= "Tel : " . $zcomp['htel'] . "     " . "Mobile : " . $zcomp['hmobile']; ?></center>
                        </span>
                    </td>
                </tr>
            </table>

            <br>

            <table class="" style="width:100%;">
                <thead>
                <b style="text-align: left; width:5%;font-size:15px">Customer Details</b>
                </thead>
                <tr>
                    <td style="text-align: left; width:55%;font-size:15px"><?php echo "$party"; ?></td>
                    <td style="text-align: right ; width:20%;font-size:15px"><b>Bill No. &nbsp;</b></td>
                    <td style="width:1%">:</td>
                    <td style="text-align: left; width:24%;font-size:15px">&nbsp;<?php echo " $getmdata[vouchno]"; ?></td>

                </tr>

                <tr>
                    <td style="text-align: left; width:55%;font-size:12px"><?php echo "$cust[add1]"; ?></td>
                    <td style="text-align: right ; width:20%;font-size:15px"><b>Date &nbsp;</b></td>
                    <td style="width:1%">:</td>
                    <td style="text-align: left; width:24%;font-size:15px">&nbsp; <?php echo "$getmdata[mydt]"; ?></td>
                </tr>
                <tr>
                    <td style="text-align: left; width:55%;font-size:12px"><?php echo " $cust[add2] $cust[add3]"; ?></td>
                    <td style="text-align: right ; width:20%"></td>
                    <td style="width:1%"></td>
                    <td style="text-align: left; width:24%;font-size:12px"><?php echo ""; ?></td>

                </tr>
                <tr>
                    <td style="text-align: left; font-size:12px"><?php echo "$cust[city]" ?></td>
                </tr>
                <tr>
                    <td style="text-align: left; font-size:12px"><?php echo "$cust[mobile]" ?></td>
                </tr>


            </table>

            <br>
            <table style="width:100%" class="">

                <tr class="" style="<?= $ht; ?>">
                    <th class="border" style="font-size:13px;width:30%; text-align: center;">Item</th>
                    <th class="border" style="font-size:13px;width:8%; text-align: center;">Batch</th>
                    <th class="border" style="font-size:13px;width:8%; text-align: center;">Bag</th>
                    <th class="border" style="font-size:13px;width:10%; text-align: center;">Kg</th>
                    <th class="border" style="font-size:13px;width:16%; text-align: center;">Rate/10Kg</th>
                    <th class="border" style="font-size:13px;width:14%; text-align: center;">Amount</th>
                </tr>


                <?php
                $tranpart = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$getmdata[vouchno]' order by srno") or die(mysqli_error($conn));
                $mht = '20px';
                $ctab = 0;
                $grqty = 0;
                while ($row = mysqli_fetch_array($tranpart)) {
                    $getbrand = mysqli_query($conn, "SELECT * FROM `product` WHERE code = '$row[itemcode]'") or die(mysqli_error($conn));
                    $getb = mysqli_fetch_array($getbrand);
                    ?>
                    <tr class="border" style="<?= $ht; ?>">
                        <td class="border " style="font-size:15px;"><?php echo "$getb[name]"; ?></td>
                        <td class="border " style="font-size:13px;text-align: right;"><?php echo "$row[batch]"; ?></td>
                        <td class="border " style="font-size:13px;text-align: right;"><?php echo "$row[cartqty]"; ?></td>
                        <td class="border " style="font-size:13px;text-align: right;"><?php echo "$row[qty]"; ?></td>
                        <td class="border " style="font-size:13px;text-align: right"><?php echo "$row[rate]"; ?></td>
                        <td class="border " style="font-size:13px;text-align: right"><?php echo "$row[amount]"; ?></td>
                    </tr>
                    <?php
                    $ctab = $ctab + 1;
                    $grqty = $grqty + $row['qty'];
                    $totamt = $totamt + $row['amount'];
                }
                if ($ctab < 8) {
                    for ($i = $ctab; $i <= 8; $i++) {
                        $ctab = $ctab + 1;
                        ?>
                        <tr style="<?= $ht; ?>">
                            <td class="border"></td>
                            <td class="border"></td>
                            <td class="border"></td>
                            <td class="border"></td>
                            <td class="border"></td>
                            <td class="border"></td>
                        </tr>
                        <?php
                    }
                }
                ?>

                <tr>
                    <td colspan="2" style="border:none;border-collapse: collapse; ">
                        <table border="solid 1px" style="border-collapse: collapse; width:100%">
                            <tbody style="border-collapse: collapse;font-size: 12px;">

                                <tr style="<?= $ht; ?>">
                                    <td class="border " style="width:75%;"><?= $zcomp['bankname'] ?></td>
                                </tr>

                                <tr style="<?= $ht; ?>">
                                    <td class="border " style="width:75%;"><?= $zcomp['branchname'] ?></td>
                                </tr>

                                <tr style="<?= $ht; ?>">
                                    <td class="border " style="width:75%;">A/C. NO. <?= $zcomp['acno'] ?></td>
                                </tr>

                                <tr style="<?= $ht; ?>">
                                    <td class="border " style="width:75%;">IFSC CODE <?= $zcomp['ifccode'] ?></td>
                                </tr>
                                <tr style="<?= $ht; ?>">
                                    <td class="border " style="width:75%;"></td>
                                </tr>
                                <tr style="<?= $ht; ?>">
                                    <td class="border " style="width:75%;"></td>
                                </tr>
                                <!-- <tr style="<?= $ht; ?>">
                                    <td class="border " style="width:75%;"></td>
                                </tr> -->
                            </tbody>
                        </table>
                    </td>
                    <td colspan="4" style="border:none;border-collapse: collapse; ">
                        <table border="solid 1px" style="border-collapse: collapse; width:100%">
                            <tbody style="border-collapse: collapse;font-size: 12px;">

                                <tr style="<?= $ht; ?>">
                                    <td class="border text-right  bold" colspan="2">Total</td>
                                    <td class="border text-right  bold" colspan="2"><?php echo number_format(round($totamt), 2) ?></td>
                                </tr>
                                <?php if ($getmdata['packchgs'] > 0) { ?>
                                    <tr style="height:24px;">
                                        <td class="border text-right  bold" colspan="2">Hel</td>
                                        <td class="border text-right  bold" colspan="2" ><?php echo number_format(round($getmdata['packchgs']), 2) ?></td>
                                    </tr>

                                <?php } else { ?>
                                    <tr style="<?= $ht; ?>">
                                        <td class="border text-right  bold" colspan="2"></td>
                                        <td class="border text-right  bold" colspan="2" ></td>
                                    </tr>
                                <?php } ?>
                                <tr style="<?= $ht; ?>">
                                    <td class="border text-right  bold" colspan="2">Bill Amount</td>
                                    <td class="border text-right  bold" colspan="2" ><?php echo rajnumformat(round($getmdata['billamt'])) ?></td>
                                </tr>

                                <?php
                                $getprevbal = mysqli_query($conn, "SELECT sum(amount) as amt FROM `actran` WHERE `accode`= '$getmdata[party]' and date < '$vouchdt' ") or die(mysqli_error($conn));
                                $fetprevbal = mysqli_fetch_array($getprevbal);
                                $prevbal = $fetprevbal['amt'] * -1;
//                                browse("P" . $prevbal);

                                if ($prevbal > 0) {
                                    ?>
                                    <tr style="<?= $ht; ?>">
                                        <td class="border text-right  bold" colspan="2">Previous Balance</td>
                                        <td class="border text-right  bold" colspan="2" ><?php echo number_format(round($prevbal), 2) ?></td>
                                    </tr>
                                <?php } else { ?>
                                    <tr style="<?= $ht; ?>">
                                        <td class="border text-right  bold" colspan="2"></td>
                                        <td class="border text-right  bold" colspan="2"></td>
                                    </tr>
                                <?php
                                }

                                $getlastrecd = mysqli_query($conn, "SELECT sum(amount) as amt FROM `actran` WHERE `accode`= '$getmdata[party]' and amount > 0 and date = '$vouchdt' order by amount desc") or die(mysqli_error($conn));
                                $fetlastrecd = mysqli_fetch_array($getlastrecd);
                                $lastrecd = $fetlastrecd['amt'];
//                                browse($lastrecd);

                                if ($lastrecd > 0) {
                                    ?>
                                    <tr style="<?= $ht; ?>">
                                        <td class="border text-right  bold" colspan="2">Last Received</td>
                                        <td class="border text-right  bold" colspan="2"><?php echo number_format(round($lastrecd), 2) ?></td>
                                    </tr>
                                <?php } else { ?>
                                    <tr style="<?= $ht; ?>">
                                        <td class="border text-right  bold" colspan="2"></td>
                                        <td class="border text-right  bold" colspan="2"></td>
                                    </tr>
                                <?php
                                }
                                $totbal = ($totamt + $prevbal - $lastrecd);
                                ?>
                                <tr style="<?= $ht; ?>">
                                    <td class="border text-right  bold" colspan="2">Total Balance</td>
                                    <td class="border text-right  bold" colspan="2" ><?php echo number_format(round($totbal), 2) ?></td>
                                </tr>
                            </tbody>
                        </table>
                    </td>
                </tr>

            </table>

            <table class="" style="width:100%">
                <tr>
                    <td style="width:50%">
                    </td>
                    <td style="width:50%">
                        <table style="width:100%">
                            <tr>
                                <td class="f20" style="text-align: right; width: 50%"><b><? echo "For $zcomp[compname]"; ?></b></td>
                            <tr>
                                <td class="f20" style="text-align: left; height: 50px;">&nbsp;</td>
                            </tr>
                            <tr>
                                <td class="f20" style="text-align: right; width: 50%">Authorised Signatory</td>
                            </tr>
                        </table>
                    </td>
                </tr>

            </table>
        </div>
    </div>
    <!--</div>-->
    <!--</div>-->
    <?php
    include("includes/linksfoot.php");
    ?>

</body>

</html>