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

$formname = 'Credit Note';
$maindb = 'credmain';
$trandb = 'credtran';
$file = '';
$source = "SR";
$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdir"];
mysqli_select_db($conn, $zdb);
$mht = 'height:20px;';

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

//$compqry = mysqli_query($conn, "SELECT * FROM $zroot.`company` WHERE `cono`= '1'") or die(mysqli_error($conn));
//$fcomp = mysqli_fetch_array($compqry);


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

//$gettrans = mysqli_query($conn, "SELECT name, code FROM `transpor` WHERE `code`= '$getmdata[transpor]'") or die(mysqli_error($conn));
//$ftrans = mysqli_fetch_array($gettrans);
?>

<!DOCTYPE HTML>
<html lang="en-US">
<?php
// include("includes/files.php");
// include("includes/tabsuf.php");
include("includes/linkshead.php");
?>
</head>
<link href="css/rep_format.css" rel="stylesheet" />
<style>
    /*    table tr td{
            font-size: 12px;
        }*/
    body {
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
        padding-right: 5px;
        padding-left: 5px;
        padding-top: 5px;
        padding-bottom: 0px;

        /*background-color: #FAFAFA;*/
        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: 165mm;
        min-height: 200mm;
        padding: 1mm;
        margin: 1mm auto;
        border: 1px #D3D3D3 solid;
        border-radius: 1px;
        /*background: white;*/

    }

    .subpage {
        border: 1px solid black;


    }

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

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

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

        .page {
            width: 165mm;
            min-height: 180mm;
            /* padding: 1mm; */
            /* margin: 1mm auto; */
            /*border: 1px  solid black;*/
            border-radius: 1px;
            /*background: white;*/

        }

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

        th {
            font-weight: bold;
            font-size: 15px;
        }
    }
</style>
<?php
//require("includes/dbconnect.php");
$comp = mysqli_query($conn, "SELECT * FROM $zroot.`company`") or die(mysqli_errno($conn));
$zcomp = mysqli_fetch_assoc($comp);
if ($paytype == 6) {
    $payno = $zcomp['gpaydno'];
} else {
    $payno = $zcomp['gpayno'];
}
?>
<!--<body id="main_container" onload="window.print();">-->

<body id="main_container" onload="">
    <div class="book">
        <div class="page">
            <!-- <div class="subpage"> -->
            <div class='col-md-12'>
                <table>
                    <?php
                    require("includes/dbconnect.php");
                    $comp = mysqli_query($conn, "SELECT * FROM `company`") or die(mysqli_error($conn));
                    $fcomp = mysqli_fetch_assoc($comp);
                    ?>

                    <tr>
                        <td style='width:15%'>
                            <table>
                                <tr>
                                    <td>
                                        <img src="images/img12.png" align="center" style="width: 106px;height: 100px;">
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td style='width:70%'>
                            <table style="width:100%">
<!--                                <tr>
                                    <td class="text-center">
                                        <b style="font-family:Serif;font-size:17px;text-align:center">يريب سالب ةراجتلل
                                            ةماعلا ةكرش صخشلا دحاولا ذ.م.م</b>
                                    </td>
                                </tr>-->
                                <tr>
                                    <td class="text-center">
                                        <b
                                            style="font-family:Serif;font-size:30px;text-align:center"><?php echo trim($zcomp['compname']) ?></b>
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td style='width:15%'>
                            <table>
                                <tr>
                                    <td>
                                        <img src="images/img13.png" align="center" style="width: 146px;height: 114px;">
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="3"><b style="font-family:Arial;font-size:17px;">
                                <center style='padding-left: 4px;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>
            <?php
            mysqli_select_db($conn, $zdb);
            ?>
            <div style="border:solid 1px;" class="col-md-12" style="padding-left: 1px ; padding-right: 1px">
                <table class="table table-border table-hover" style="width:100%">
                    <tr>
                        <td style="text-align: left; width:5%"><b>BUYER</b></td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:45%"><?php echo $name . ' ' . $getmdata['mobile']; ?></td>
                        <td style="text-align: right ; width:30%"><b>Date:</b></td>
                        <td style="width:1%"></td>
                        <td style="text-align: left; width:15%"><?php echo $getmdata['vouchdt']; ?></td>
                    </tr>
                    <tr>
                        <td style="text-align: left; width:5%"><b>Add.</b></td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:45%"><?php echo $add1 . ' ' . $add2; ?></td>
                        <td style="text-align: right ; width:30%"><b>Credit Note No:</b></td>
                        <td style="width:1%"></td>
                        <td style="text-align: left; width:15%"><?php echo $getmdata['vouchno']; ?></td>
                    </tr>
                    <tr>
                        <td style="text-align: left; width:5%"><b></b></td>
                        <td style="width:1%"></td>
                        <td style="text-align: left; width:45%"><?php echo $add3; ?></td>
                        <td style="text-align: right;width:30%"><b>Against Invoice No:</b></td>
                        <td style="width:1%"></td>
                        <td style="text-align: left; width:15%"><?php echo $getmdata['invno']; ?></td>
                    </tr>
                </table>
            </div>
            <div style="border:1px solid black;">
                <table style="width:100%" class="mc-table table-bordered">
                    <tr>
                        <th class="border" style="width:5%;  text-align: center;">Sr</th>
                        <th class="border" style="width:55%; text-align: center;">Product</th>
                        <th class="border" style="width:13%; text-align: right;">Qty/CTN</th>
                        <th class="border" style="width:13%; text-align: right;">Qty In Pairs</th>
                        <th class="border" style="width:13%; text-align: right;">Rate</th>
                        <th class="border" style="width:14%; text-align: right;">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="height:24px;">
                            <td class="border f12" style="text-align: center;"><?php echo "$row[srno]"; ?></td>
                            <td class="border f12"><?= $row['itemname']; ?></td>
                            <td class="border f12" style="text-align: right;"><?php echo "$row[qty]"; ?></td>
                            <td class="border f12" style="text-align: right;"><?php echo "$row[pairs]"; ?></td>
                            <td class="border f12" style="text-align: right"><?php echo "$row[rate]"; ?></td>
                            <td class="border f12" style="text-align: right"><?php echo "$row[amount]"; ?></td>
                        </tr>
                        <?
                        $ctab = $ctab + 1;
                        $grqty = $grqty + $row['qty'];
                        $totamt = $totamt + $row['amount'];
                    }
                    if ($ctab < 13) {
                        for ($i = $ctab; $i <= 13; $i++) {
                            $ctab = $ctab + 1;
                            ?>
                            <tr style="height:24px;">
                                <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 style="height:25px;">
                        <td class="border" colspan="3"><?= $getmdata['rem1'] . ' ' . $getmdata['rem2']; ?></td>
                        <td class="border text-right f12 bold"><?php echo number_format($grqty, 2) ?></td>
                        <!--<td class="border text-right f12 bold"></td>-->
                        <td class="border text-right f12 bold">Total</td>
                        <td class="border text-right f12 bold"><?php echo number_format(round($totamt), 2) ?></td>
                        <!--<td class="border"></td>-->
                    </tr>
                    <?php if ($getmdata['totdisc'] > 0) { ?>
                        <tr style="height:25px;">
                            <td class="border" colspan="3"></td>
                            <td class="border text-right f12 bold"></td>
                            <td class="border text-right f11 bold">Discount</td>
                            <!--<td class="border text-right f11 bold"><?php // echo number_format(round($getmdata[cashdiscperc]),2) . " % &nbsp;&nbsp;" .number_format(round($getmdata[discount]),2)      ?></td>-->
                            <td class="border text-right f11 bold">
                                <?php echo number_format(round($getmdata['totdisc']), 2) ?>
                            </td>
                            <!--<td class="border"></td>-->
                        </tr>

                    <?php } ?>
                    <?php if ($getmdata['vatamt'] > 0) { ?>
                        <tr style="height:25px;">
                            <td class="border" colspan="3"></td>
                            <td class="border text-right f11 bold">Add VAT</td>
                            <td class="border text-right f11 bold">
                                <?php echo number_format($getmdata['vatperc'], 2) . '% ' ?>
                            </td>
                            <td class="border text-right f11 bold"><?php echo number_format($getmdata['vatamt'], 2) ?></td>
                        </tr>
                    <?php } ?>

                    <tr style="height:25px;">
                        <td class="border" colspan="3"><b></b></td>
                        <td class="border text-right f10 bold" colspan="2">Bill Amount</td>
                        <td class="border text-right f10 bold"><?php echo rajnumformat(round($getmdata['billamt'])) ?>
                        </td>
                    </tr>

                    <tr style="height:25px;">
                        <td class="border" colspan="6">AED <?php echo rsword(round($getmdata['billamt'])); ?> Only</td>
                    </tr>
                </table>
                <table class="cust-cal table " style="" style="width:100%">

                    <tr>
                        <td style="width:50%">

                            <!--                                <table style="width:100%">
                            <?php if ($zcomp['bank'] != '') { ?>
                                            <tr>
                                                <td style="width:25%">Bank Name</td>
                                                <td style="width:75%"><?= $zcomp['bank']; ?></td>
                                            </tr>
                                            <tr>
                                                <td style="width:25%">A/c. No.  </td>
                                                <td style="width:75%"><?= $zcomp['acno']; ?></td>
                                            </tr>
                                            <tr>
                                                <td style="width:25%">Branch</td>
                                                <td style="width:75%"><?= $zcomp['branch']; ?></td>
                                            </tr>
                                            <tr>
                                                <td style="width:25%">IFSC</td>
                                                <td style="width:75%"><?= $zcomp['bankcode']; ?></td>
                                            </tr>
                                            <tr>
                                                <td colspan="2">&nbsp;</td>
                                            </tr>
                            <?php } ?>
                                    <tr>
                                        <td colspan="2"><b>Proforma Validity for only 12 hrs.</b></td>
                                    </tr>
                                </table>-->
                        </td>
                        <td style="width:50%">
                            <table style="width:100%">
                                <tr>

                                    <td class="" style="text-align: right; width: 50%">
                                        <b><? echo "For $zcomp[compname]"; ?></b>
                                    </td>

                                <tr>
                                    <td class="" style="text-align: left; height: 50px;">&nbsp;</td>

                                </tr>
                                <tr>

                                    <td class="" style="text-align: right; width: 50%">Authorised Signatory</td>

                                </tr>
                            </table>
                        </td>
                    </tr>
                    <!--                        <tr>
                        <td colspan="2">
                            <b style="font-size:16px">Rupess <?php echo rsword(round($getmdata["billamt"])); ?> Only</b>
                        </td>
                    </tr>-->
                </table>
            </div>
        </div>
    </div>
    </div>
    <?php
    include("includes/linksfoot.php");
    ?>
    <script>
        $(function () {
            formGeneral();
        });
    </script>
</body>

</html>