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

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdir"];
$zcomp = $_SESSION["comp_" . $dbname];
$credprint = "credoth_print.php?vouchno=16";


$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
$vouch = $_GET['vouchno'];
$cond = '';
$lcond = '';
$formname = "CREDIT NOTE";
$maindb = 'credmain';
$trandb = 'credoth';

$getqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as vouchdt, DATE_FORMAT(`invdate`, '%d-%m-%Y')as invdate   FROM `$maindb` WHERE vouchno='$vouch' ORDER BY `vouchno`") or die(mysqli_error($conn));
$getdata = mysqli_fetch_array($getqry);
//
//$cmpname = mysqli_query($conn, "SELECT * From $zroot.`company` where `code`='1'") or die(mysqli_error($conn));
//$getcmpname = mysqli_fetch_array($cmpname);

$getparty = mysqli_query($conn, "SELECT `accode`,`name` From accmast WHERE `accode` = '$getdata[party]'") or die(mysqli_error($conn));
$showparty = mysqli_fetch_array($getparty);

$imgsrc = "images/kidslounge.png";
?>

<!DOCTYPE HTML>
<html lang="en-US">
    <style>
        @media print {
            .noprint {
                display: none;
            }
        }

        body {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
            /*background-color: #FAFAFA;*/
            font: 12pt "Tahoma";
        }

        * {
            box-sizing: border-box;
            -moz-box-sizing: border-box;
        }

        .page {
            width: 210mm;
            min-height: 297mm;
            padding: 4mm;
            margin: 10mm auto;
            border: 1px #D3D3D3 solid;
            border-radius: 5px;
            /*background: white;*/

        }

        .subpage {
            padding: 0cm;
            /*        border: 1px solid;*/
            height: 288mm;
            outline: 2cm #ffffff solid;
        }

        @page {
            size: A4;
            margin: 0;
        }

        @media print {

            html,
            body {
                width: 210mm;
                height: 297mm;
            }

            .page {
                margin: 0;
                border: initial;
                border-radius: initial;
                width: initial;
                min-height: initial;
                box-shadow: initial;
                background: initial;
                page-break-after: always;
            }
        }

        .numeric {
            text-align: right;
        }

        .rnone {
            border-right: none;
        }

        .lnone {
            border-left: none;
        }
    </style>
</head>

<body id="main_container">

    <div class="book">
        <button class="btn btn-primary goBack noprint" onclick="window.close()" style="float: right;width: 150px;height:35px;background-color: white;color:#A0B1BA;">Go Back</button>

        <button class="btn btn-primary noprint" onclick="window.open('<?= $credprint ?>')" style="float: right;width: 150px;height:35px;background-color: white;color:#A0B1BA;">Print</button>
        <div class="page">
            <!--<div class="subpage">-->
            <!--   ************Header************  -->
            <div class='col-md-12'>
                <table>
                    <?php
                    require("includes/dbconnect.php");
                    $comp = mysqli_query($conn, "SELECT * FROM `company`") or die(mysql_errno());
                    $fcomp = mysqli_fetch_assoc($comp);
                    ?>
                    <tr>
                        <td style='width:80%'>
                            <table>
                                <tr>
                                    <td class='text-center' style='padding-left: 105px;'><b style="font-family:Serif;font-size:32px;text-align:center">كيدز لونج لتجارة الحذية ذ.م.م</b></td>
                                </tr>
                                <tr>
                                    <td class='text-right' style='padding-left: 100px;'><b style="font-family:Serif;font-size:32px;text-align:center"><?php echo trim($fcomp['compname']) ?></b></td>
                                </tr>

                                <tr>
                                    <td class='text-right' style='padding-left: 90px;'><b style="font-family:Serif;font-size:18px;text-align:center">Importers, Exporters and Wholesaler of Footwear</b></td>
                                </tr>
                            </table>
                        </td>
                        <td style='width:20%'>
                            <table>
                                <tr>
                                    <td>
                                        <img src="images/kidslounge.png" align="center" style="width: 156px;height: 104px;">
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td style=""><b style="font-family:Arial;font-size:17px;"><center style='padding-left: 64px;background-color:black;color:white'><u><?= $formname ?></u></center></b></td>
                    </tr>
                    <tr>
                        <td style=""><b style="font-family:Arial;font-size:17px;"><center  style='padding-left: 64px;'><u>VAT TRN No. <?= $fcomp['gstno'] ?></u></center></b></td>
                    </tr>
                </table>
            </div>

            <div style="border:solid 1px;" class="col-md-12" style="padding-left: 1px ; padding-right: 1px">
                <?php
                mysqli_select_db($conn, $zdb);

                $gcust = mysqli_query($conn, "SELECT * FROM `accmast` where accode = '$getdata[party]' ") or die(mysqli_error($conn));
                $cust = mysqli_fetch_array($gcust);
                ?>
                <table class="table table-border table-hover" style="width:100%">
                    <tr>
                        <td style="text-align: left; width:5%"><b>M/s.</b></td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:50%"><?php echo $showparty['name']; ?></td>
                        <td style="text-align: right ; width:20%"><b>Credit Note No.</b></td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:20%"><?php echo $getdata['vouchno']; ?></td>
                    </tr>
                    <tr>
                        <td style="text-align: left; width:7%"><b>Shop.</b></td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:50%"><?php echo $cust['add1']; ?></td>
                        <td style="text-align: right ; width:20%"><b>Date</b></td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:20%"><?php echo $getdata['vouchdt']; ?></td>
                    </tr>
                    <tr>
                        <td style="text-align: left; width:7%"></td>
                        <td style="width:1%"></td>
                        <td style="text-align: left; width:50%"><?php echo $cust['add2'] . $cust['add3']; ?></td>
                        <td style="text-align: right ; width:20%"><b>Against Invoice No.</b></td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:20%"><?php echo $getdata["invno"] ?></td>
                    </tr>
                    <tr>
                        <td style="text-align: left; width:7%"><b>Mobile</b></td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:45%"><?php echo $cust['mobile']; ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                        <td style="width:25%;text-align: right;"><b>Invoice Date</b></td>
                        <td style="width:1%"></td>
                        <td style="text-align: left; width:20%"><?php echo $getdata['invdate']; ?></td>
                    </tr>
                </table>
            </div>
            <div>
                <table border="solid 1px" style="border-collapse: collapse; width:100%">
                    <tr>
                        <th style="width: 1%;" class="text-left"><label>Sr.</label></th>
                        <th style="width: 50%;"><label>Description</label></th>
                        <th style="width: 10%;"><label>Amount</label></th>
                        <th style="width: 10%;"><label>VAT%.</label></th>
                        <th style="width: 10%;"><label>VAT Amt.</label></th>
                        <th style="width: 15%;"><label>Amount</label></th>


                    </tr>

                    <?php
                    $ctr = 0;
                    $ctab = 0;
                    $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$vouch' order by srno") or die(mysqli_error($conn));
                    while ($showtran = mysqli_fetch_array($gettran)) {
                        $ctr++;

                        $descrip = $showtran['descrip'];
                        $amount = $showtran['amount'];
                        $vatperc = $showtran['vatperc'];
                        $vatamt = $showtran['vat'];
                        $total = $showtran['total'];


                        echo "<tr>";
                        echo "<td style='text-align:center'>$ctr</td>";
                        echo "<td style=''>$descrip</td>";
                        echo "<td style=' text-align:right;'>$amount</td>";
                        echo "<td style='text-align:center'>$vatperc</td>";
                        echo "<td style='text-align: right'>$vatamt</td>";
                        echo "<td style=' text-align:right;'>$total</td>";
                        echo "</tr>";

                        $ctab = $ctab + 1;
                    }
                    if ($ctab < 15) {
                        for ($i = $ctab; $i <= 15; $i++) {
                            $ctab = $ctab + 1;
                            ?>
                            <tr style="height:26px;">
                                <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
                        }
                    }
                    if ($getdata['rem1'] != '') {
                        ?>
                        <tr>

                            <td class=" lnone" colspan="12">
                                <? echo "REMARKS" . " : " . $getdata['rem1'] . " " . $getdata['rem2']; ?>
                            </td>
                        </tr>
                    <? } ?>

                    <tr style="">
                        <td style="text-align: right;border-right:1px solid black" colspan="5"> Subtotal </td>
                        <td style="text-align: right;border-left: none;">
                            <? echo $getdata['grossamt']; ?>
                        </td>
                    </tr>
                    <? if ($getdata['totvat'] > 0) { ?>
                        <tr style="">
                            <td style="text-align: right;border-right:1px solid black" colspan="5">Add VAT</td>
                            <td style="text-align: right;border-left: none;">
                                <? echo $getdata['totvat']; ?>
                            </td>
                        </tr>
                        <?
                    }
                    ?>
                    <tr style="">
                        <td style="text-align: right;border-right:1px solid black" colspan="5">Net Amount</td>
                        <td style="text-align: right;border-left: none;"><b>
                                <? echo $getdata['billamt']; ?></b></td>
                    </tr>

                    <tr>
                        <td colspan="9">
                            Rupees
                            <?php echo rsword(round($getdata['billamt'])); ?> Only
                        </td>

                    </tr>


                </table>
            </div>

            <div>
                <table class="border" width="100%">
                    <tr>
                        <td class="border" style="text-align:right; font-size: 14px;width:33%">
                            &nbsp;
                        </td>
                        <td class="border" style="text-align:right; font-size: 14px;width:33%">
                            &nbsp
                        </td>
                        <td class="border" style="text-align:right; font-size: 14px;width:34%">
                            <b><? echo "For $fcomp[compname]"; ?></b>
                        </td>
                    </tr>
                    <tr>
                        <td height="80px">&nbsp;</td>
                        <td height="80px"></td>
                        <td height="80px"></td>
                    </tr>
                    <tr>
                        <td >
                            <hr style=''>

                        </td>
                        <td>
                        </td>
                        <td >
                            <hr style=''>

                        </td>
                    </tr>
                    <tr>
                        <td class="border" style="text-align:left; font-size: 14px;font-weight:bold;">
                            CHECKED BY
                        </td>
                        <td class="border" style="text-align:center; font-size: 14px;">
                            <table style="width:100%">

                            </table>

                        </td>
                        <td class="border" style="text-align:right; font-size: 14px;">
                            <b>AUTHORISED SIGNATORY</b>
                        </td>
                    </tr>

                </table>
            </div>
        </div>
    </div>
</div>
<?php
include("includes/linksfoot.php");
?>
<script>
    $(function () {
        formGeneral();
    });
</script>
</body>

</html>