<?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];

$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);

include("includes/linkshead.php");
?>
<link href="styles/inv_format.css" rel="stylesheet">
<style>
    body{
        font-size: 13px;
    }
    .table > tbody > tr > td{
        padding: 0px;
    }

    .bor{
        border-left: 1px solid black;
        border-right:  1px solid black;
    }

    .numeric{
        text-align: right
    }

    .fontnew{
        font-family: 'Verdana';
        font-size : 13px;
        font : 'bold';
    }
    .border{
        border-top-style:solid; 
        border-right-style:solid; 
        border-bottom-style:none; 
        border-left-style:solid;
        border-width:1px black;
    }
    .border2{
        border-top-style:solid; 
        border-right-style:solid; 
        border-bottom-style:none; 
        border-left-style:none;
        border-width:1px ;
    }
    .uppercase{
        text-transform:uppercase;
    }
    th{
        font-size:10px
    }
    .table-condensed > tbody > tr > td {
        padding:3px;
        font-size:10px
    }
    .padd{
        padding-left: 2px;
    }
    .padd2{
        padding-left: 4px;
    }
</style>
</head>
<body onload="">


    <div class="page">
        <div class="subpage" style="padding-top: 16px">
            <div class="row">
                <div class="col-lg-12 col-md-12 col-sm-12 text-right">
                    <button onclick="window.print();
                            return false;" class="btn btn-primary noPrint" style="text-align: right">Print</button>
                </div>
            </div><br>
            <div class="border" style="border-bottom: none">
                <table style="width:100%;height: 7%">
                    <tr>
                        <td style="width:25%">
                            <table style="width:100%">
                                <tr>
                                    <td><img src="images/logo/khlogo.jpg" style="width: 120px;height: 80px"></td> 
                                </tr>
                            </table>
                        </td>
                        <td style="width:50%">
                            <table style="width:100%">
                                <tr>
                                    <td class="uppercase" style="text-align: center;width: 100%;font-size: 35px;font-family: 'Times New Roman';">
                                        <b><? Echo $getcmpname['compname']; ?></b></td> 
                                </tr>
                            </table> 
                        </td>
                        <td style="width:25%">
                            <table style="width:100%">
                                <tr>
                                    <td style="text-align: right"><img src="images/logo/KH150.jpg" style="width: 120px;height: 80px"></td> 
                                </tr>
                            </table> 
                        </td>
                    </tr>
                </table>
                <table style="width:100%">
                    <tr style="text-align: center"><td class="text-center"><b></b>
                            <?php echo $getcmpname['hoadd1'] . ' ' . $getcmpname['hoadd2'] . ' ' . $getcmpname['hoadd3'] . ' ' . $getcmpname['hocity'] . '-' . $getcmpname['hopin'] . " " . "Tel :" . " " . $getcmpname["htel"] ?>                      
                        </td>
                    </tr>
                    <tr style="text-align: center">
                        <td>&nbsp;</td>
                    </tr>

                    <tr style="text-align: center">
                        <td style="font-size: 15px;" class="text-center">
                            <b>
                              CREDIT NOTE
                            </b>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="border">
                <div class="row">
                    <?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 width="100%">
                        <tr>
                            <td style="width:65%; border-right: 1px solid black;" class="text-left padd">
                                <label class="fontnew" style="text-align: left;"><b>M/s.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;<? Echo $showparty['name']; ?></b></label>
                            </td>
                            <td style="width:10%" class="text-left padd">
                                <label class="fontnew" style="text-align: left;"><b>Credit No. &nbsp; </b></label>
                            </td>
                            <td  class="padd" style="width:20%">
                                <label style="font-size: 12px;font-family: 'verdana'"><b>:&nbsp;
                                        <? Echo $getdata['vouchno']; ?></b></label>
                            </td>
                        </tr>
                        <tr>
                            <td style="width:65%; border-right: 1px solid black;" class="text-left padd">
                                <label class="fontnew" style="text-align: left;"><b>Shop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;</b><? Echo $cust['add1'] ?></label>
                            </td>
                            <td style="width:15%" class="text-left padd">
                                <label class="fontnew" style="text-align: left;"><b>Date&nbsp; </b></label>
                            </td>
                            <td  class="" style="width:17%">
                                <label style="font-size: 12px;font-family: 'verdana'"><b>:&nbsp;
                                        <? Echo $getdata['vouchdt']; ?></b></label>
                            </td>  
                        </tr>
                        <? if (!empty($cust['add2'])) { ?>
                            <tr>
                                <td style="width:55%; border-right: 1px solid black;" class="text-left padd">
                                    <label class="fontnew" style="text-align: left;"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><? Echo $cust['add2'] . " " . $cust['add3']; ?></label>
                                </td>
                                <td style="width:10%" class="text-left padd">
                                </td>
                                <td  class="padd" style="width:20%">
                                </td>  
                            </tr>
                        <? } ?>
                        <tr>
                            <td style="width:55%; border-right: 1px solid black;" class="text-left padd">
                                <label class="fontnew" style="text-align: left;"><b>Mobile &nbsp;&nbsp;:&nbsp;&nbsp;</b><? Echo $cust['mobile']; ?></label>
                            </td>
                            <td style="width:21%" class="text-left padd">
                                <label class="fontnew" style="text-align: left;"><b>Against Invoice No.&nbsp; </b></label>
                            </td>      
                            <td  class="padd" style="width:20%">
                                <label style="font-size: 12px;font-family: 'verdana'"><b>:&nbsp;
                                        <? Echo $getdata['invno']; ?></b></label>
                            </td>
                        </tr>
                        <tr>
                            <td style="width:55%; border-right: 1px solid black;" class="text-left padd">
                                <label class="fontnew" style="text-align: left;"><b>GST No&nbsp;&nbsp;:&nbsp;&nbsp;</b><? Echo $cust['gstno']; ?></label>
                            </td>
                            <td style="width:15%; " class="text-left padd">
                                <label class="fontnew" style="text-align: left;"><b>Invoice Date.&nbsp;</b></label>
                            </td>
                            <td  class="padd" style="width:20%">
                                <label style="font-size: 12px;font-family: 'verdana'"><b>:&nbsp;
                                        <? Echo $getdata['invdate']; ?></b></label>
                            </td> 

                        </tr>
                    </table>

                </div>
            </div>
            <!--                <div style="border-right: 1px solid black;border-left: 1px solid black;padding:0px">
                                <div class="row">
                                    <div class="col-lg-12">-->
            <table class="table-condensed table-hover" style="width: 100%">
                <tr style="background: #0098DA;color: white;border: 1px solid black; height: <?= $tdhg; ?>">
                    <th class="uppercase numeric bor" style="width:2%">Sr</th>
                    <th class="uppercase bor" style="width:46%">Description</th>
                    <th class="uppercase bor" style="width:10%; text-align: right;  ">Amount</th>
                </tr>
                <?php
                $ctr = 0;
                $gettran = mysqli_query($conn, "SELECT `srno`,`descrip`,`amount`FROM `$trandb` WHERE `vouchno`='$vouch' order by srno") or die(mysqli_error($conn));
                while ($showtran = mysqli_fetch_array($gettran)) {
                    $descrip = $showtran['descrip'];
                    $amount = $showtran['amount'];
                    $ctr++;
                    ?>
                    <tr style="; height: <?= $tdhg; ?>; ">
                        <td class="bor" style="font-size: 13px;"><? Echo $ctr; ?></td>
                        <td class="bor" style="font-size: 13px"><? Echo $descrip; ?></td>
                        <td class="bor" style="font-size: 13px;text-align: right;"><? Echo $amount; ?></td>
                    </tr>
                    <?
                    if ($checkgstzero == 0) {
                        if ($gstperc == 0) {
                            $checkgstzero = 1;
                        }
                    }
                }
                if ($ctr < 25) {
                    for ($i = 0; $i < 25; $i++) {
                        ?>
                        <tr style="height: <?= $tdhg; ?>">
                            <td class="bor padd2">&nbsp;</td>
                            <td class="bor"></td>
                            <td class="bor"></td>
                        </tr>
                        <?php
                    }
                }
                ?> 
            </table>
            <!--                        </div>
                                </div>
                            </div>-->
            <div class="border">
                <div class="row">
                    <div class="col-sm-12" style="padding: 0px">
                        <table class="table-hover pad" style="float: left;width:50%">
                            <tr>
                                <? if ($getdata['rem1'] != '') {
                                    ?>     
                                    <td><label class="pad"><? Echo $getdata['rem1'] ?></label></td>
                                <? }?>
                                </tr>
                                <tr>
                                <? if ($getdata['rem2'] != '') {
                                    ?>
                                    <td><label class="pad"><? Echo $getdata['rem2']; ?></label></td>
                                <? } ?>
                                </tr>
                                
                            </table>
                            <table class="table-hover pad" align="right" style="float:right;width:21%">
                                <tr >
                                    <td style="float: right;font-size:13px"><label class="">Sub Total</label></td>
                                    <!--<td class="numeric" style="text-align:left "><label class=""></label></td>-->  
                                    <td class="numeric"><label class="" style="text-align:right;font-size:13px"><? echo $amount; ?></label></td>
                                </tr>
                                <tr>

                                    <?
                                    if ($getdata['cgstamt'] > 0) {
                                        ?>

                                    <td style="text-align: right;font-size:13px;"><label class="">CGST&nbsp;<? echo (round($getdata['cgstperc'])) . "%" ?></label></td>
                                        <td style="text-align: right;font-size:13px;"><label class=""><? echo $getdata['cgstamt']; ?></label></td>
                                    <? } ?>    
                                </tr>

                                <tr>
                                    <? if ($getdata['sgstamt'] > 0) {
                                        ?>

                                        <td style="text-align: right;font-size:13px;"><label class="">SGST&nbsp;<? echo (round($getdata['sgstperc'])) . "%" ?></label></td>
                                        <td style="text-align: right;font-size:13px;"><label class=""><? echo $getdata['sgstamt']; ?></label></td>
                                    <? } ?>
                                </tr>
                                <tr>
                                    <td style="text-align: right; font-size:13px"><label class="">Net Amount</label></td>
                                    <td class="numeric" style="font-size:13px"><label class=""><b><? Echo $getdata['billamt']; ?></b></label></td>
                                </tr>
                            </table>
                        </div>
                    </div>



                </div>
                <?php
                $mwords = round($getdata['billamt']);
                $words = rsword($mwords);
                ?>
                <div style="border:1px solid black" style="">
                    <div class="row">
                        <div class="col-lg-12" style="padding: 0px">
                            <label class="pad fa fa-inr">&nbsp;<b><?php Echo $words . " Only"; ?></b></label>
                        </div>
                    </div>
                </div>
                <?php $content = "Certified that the particulars given above are true and correct." ?>
                <div>
                <table class="" width="100%">
                    <tr>
                        <td class="" style="text-align:right; font-size: 14px;width:33%">
                            &nbsp;
                        </td>
                        <td class="" style="text-align:right; font-size: 14px;width:33%">
                            &nbsp
                        </td>
                        <td class="" style="text-align:right; font-size: 14px;width:34%">
                            <b>For
                                <? echo $getcmpname['compname']; ?></b>
                        </td>
                    </tr>
                    <tr>
                        <td height="80px">&nbsp;</td>
                        <td height="80px"></td>
                        <td height="80px"></td>
                    </tr>
                    <tr>
                        <td class="" style="text-align:right; font-size: 14px;">
                            &nbsp;
                        </td>
                        <td class="" style="text-align:center; font-size: 14px;">
                            <table style="width:100%">
                                <tr>
                                    <td style="border-bottom:1px solid black">
                                        &nbsp;
                                    </td>
                                </tr>
                            </table>
                            <b>CHECKED BY</b>
                        </td>
                        <td class="" style="text-align:right; font-size: 14px;">
                            <b>AUTHORISED SIGNATORY</b>
                        </td>
                    </tr>
                </table>
            </div>
            </div>
        </div>
        <?php
        include('includes/linksfoot.php');
        ?>
