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

$fseries = $_SESSION["series_" . $dbname];

$keycond = "";

$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
$zyear = $myyear;

$vouch = $_GET['vouchno'];

$cond = '';
$lcond = '';
$formname = "Credit Note";

$maindb = 'credmain';
$trandb = 'credtran';

$getqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as vouchdt   FROM `$maindb` WHERE vouchno='$vouch' $keycond ORDER BY `vouchno`")or die(mysqli_error($conn));
$getdata = mysqli_fetch_array($getqry);


$cmpname = mysqli_query($conn, "SELECT *    From $zroot.`company` where `code`='$zcomp' ")or die(mysqli_error($conn));
$getcmpname = mysqli_fetch_array($cmpname);


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


$oms = $getdata['oms'];
if ($getdata['prnflag'] == '1') {
    $prnf = 'P';
}
?>

<!DOCTYPE HTML>
<html lang="en-US">


    <?php
//        include("includes/files.php");
//        include("includes/tabsuf.php");
    ?>
    <style>
        body {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
            /*background-color: #FAFAFA;*/
            font: 10pt "Verdana";
        }
        .border{
            border: 1px solid black;
        }
        @media print {
            .noprint {
                display: none;
            }
        }
        * {
            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;

            page-break-inside:  auto ;
        }
        @media print {
            html, body {
                width: 210mm;
                height: 297mm;    
                page-break-before: always;
            }
            .page {
                margin: 0;
                border: initial;
                border-radius: initial;
                width: initial;
                min-height: initial;
                box-shadow: initial;
                background: initial;
                page-break-inside:  auto ;
            }
            /*            .pageBreak{
                            page-break-inside:  auto ;
            
                        }*/
        }
        .numeric{
            text-align: right;
        }
        .rnone{
            border-right:none; 
        }
        .lnone{
            border-left:none; 
        }
    </style>
</head>

<!--<link href="MyStyle/rep_format.css" rel="stylesheet">-->

<body id="main_container" >
    <!--<body id="main_container" onload="window.print();">-->
    <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>
        <div class="page" style="page-break-after:always">
            <!--<div class="subpage">-->
            <!--   ************Header************  -->

            <?php include("inv_head1_print.php") ?>

            <div>
                <table  border="solid 1px" class="pageBreak" style="border-collapse: collapse; width:100%;">
                    <tr>
                        <th style="width:3%; color:black; text-align: right;"><label>Sr.</label></th>
                        <th style="width:30%; color:black;"><label>Itemname</label></th>
                        <th style="width:10%; color:black;"><label>Hsn</label></th>
                        <th style="width:11%; color:black; text-align: right;"><label>GST %</label></th>
                        <!--<th style="width:8%; color:black; text-align: right;"><label>GST Rate</label></th>-->
                        <th style="width:9%; color:black; text-align: right;"><label>Qty.</label></th>
                        <!--<th style="width:7%; color:black;"><label>Unit</label></th>-->
                        <th style="width:9%; color:black; text-align: right;"><label>Rate</label></th>
                        <th style="width:11%; color:black; text-align: right;"><label>Amount</label></th>
                        <th style="width:9%; color:black; text-align: right;"><label>Discount</label></th>
                        <th style="width:11%; color:black; text-align: right;"><label>Taxable</label></th>

                    </tr>

                    <?php
                    $ctr = 0;
                    $mctr = 1;
                    $ctab = 0;
                    $totqty = 0;
                    $pname = "";
                    $sqty = 0;
                    $samount = 0;
                    $sdiscount = 0;
                    $ssubtot = 0;

                    $tgrp = "";
                    $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$vouch' $keycond order by srno")or die(mysqli_error($conn));
                    while ($showtran = mysqli_fetch_array($gettran)) {
                        $getitem = mysqli_query($conn, "SELECT `name`,`code`  From `product` WHERE `code` = '$showtran[itemcode]' ")or die(mysqli_error($conn));
                        $showitem = mysqli_fetch_array($getitem);

                        $gstperc = $showtran['cgstrate'] * 2;
                        $gstamt = $showtran['cgstamt'] * 2;

                        $nameprint = $showitem["nameprint"];
                        $name = $showitem["name"];

                        if ($nameprint != '') {
                            $itemname = $nameprint;
                        } else {
                            $itemname = $name;
                        }

                        $a = 0;
                        $pname = $itemname;
                        $sqty = $showtran['qty'];
                        $smrp = $showtran['rate'];

                        $fqty = number_format($sqty, 0);
                        $fmrp = number_format($smrp, 0);

                        $samount = $showtran['qty'] * $showtran['rate'];

//                        if ($showtran['mrp'] > 0 ) {
//                            $samount = $showtran['qty'] * $showtran['mrp'];
//                        } else {
//                            $samount = $showtran['qty'] * $showtran['rate'];
//                        }    
                        $sdiscount = $showtran['discount'];
                        $fdiscount = number_format($sdiscount, 2);

                        $ssubtot = $showtran['subtot'];
                        $tgrp = $grp;

                        Echo"<tr class=''>";
                        Echo"<td style=''>$mctr</td>";
                        Echo"<td style=''>$showitem[name]</td>";
                        Echo"<td style=''>$showtran[hsncode]</td>";
                        if ($oms == '0') {
                            ?>
                            <td class="text-right bor" style=' text-align:right;'><? Echo $gstperc ?></td>
                        <?php } else { ?>
                            <td class="text-right bor" style=' text-align:right;'><? Echo $showtran['igstrate']; ?></td>
                            <?php
                        }
//                        browse ($gstperc);
                        Echo"<td style='text-align: right;'>$fqty</td>";
                        Echo"<td style=' text-align:right;'>$fmrp</td>";
                        Echo"<td style=' text-align:right;'>$samount</td>";
                        Echo"<td style=' text-align:right;'>$fdiscount</td>";
                        Echo"<td style=' text-align:right;'>$ssubtot</td>";

                        Echo"</tr>";
                        $ctab++;

                        $tdisc = $samount - $ssubtot;

                        if ($ctab > 22) {
                            ?>
                            <table border="solid:1px"  style="border-collapse: collapse; width:100%">
                                <tr class="" style="height:24px;">
                                    <td></td>
                                </tr>
                                <tr class="" style="height:24px;">
                                    <td></td>
                                </tr>
        <!--                                        <tr class="" style="height:24px;">
                                    <td></td>
                                </tr>-->
                                <tr class="" style="height:24px;">
                                    <td></td>
                                </tr>
                                <tr class="" style="height:24px;">
                                    <td></td>
                                </tr>
                                <tr class="" style="height:24px;">
                                    <td></td>
                                </tr>
                                <tr class="" style="height:24px;">
                                    <td></td>
                                </tr>
                                <tr class="" style="height:24px;">
                                    <td></td>
                                </tr>
                                <tr class="" style="height:24px;">
                                    <td></td>
                                </tr>
        <!--                                <tr class="" style="height:24px;">
                                    <td></td>
                                </tr>-->
                            </table>
                            <?php include("inv_foot2_print.php"); ?>
                            <br>
                            <br>
                        </table>
                        <div class="subpage">
                            <?php include("inv_head1_print.php") ?>
                            <table  border="solid 1px" style="border-collapse: collapse; width:100%">
                                <tr>
                                    <th style="width:3%; color:black; text-align: right;"><label>Sr.</label></th>
                                    <th style="width:30%; color:black;"><label>Itemname</label></th>
                                    <th style="width:10%; color:black;"><label>Hsn</label></th>
                                    <th style="width:11%; color:black; text-align: right;"><label>GST %</label></th>
                                    <!--<th style="width:8%; color:black; text-align: right;"><label>GST Rate</label></th>-->
                                    <th style="width:9%; color:black; text-align: right;"><label>Qty.</label></th>
                                    <!--<th style="width:7%; color:black;"><label>Unit</label></th>-->
                                    <th style="width:9%; color:black; text-align: right;"><label>Rate</label></th>
                                    <th style="width:11%; color:black; text-align: right;"><label>Amount</label></th>
                                    <th style="width:9%; color:black; text-align: right;"><label>Discount</label></th>
                                    <th style="width:11%; color:black; text-align: right;"><label>Taxable</label></th>
                                </tr>                                
                                <?php
                                $ctab = 0;
                            }
                            $totqty = $totqty + $showtran['qty'];
                            $totamt = $totamt + $samount;

                            if ($tdisc > 0) {
                                $totdiscamt = $totdiscamt + $tdisc;
                            }
                            $tot = $tot + $ssubtot;
                            $ctr++;
                            $mctr++;
                        }

                        if ($ctab < 22) {
                            for ($i = $ctab; $i <= 22; $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>-->
                                    <td class="border"></td>
                                    <td class="border"></td>
                                    <td class="border"></td>
                                    <td class="border"></td>
                                </tr>      
                                <?php
                            }
                        }
                        ?>  

                        <tr style="">
                            <td colspan="2" style="" class="border " style="text-align: right;"></td>
                            <td  class="border"></td>
                            <td style="" class="border " style="text-align: right;">Total</td>
                            <td style="text-align: right;" class="border"> <? echo $totqty; ?>  </td>
                            <td  class="border"></td>
                            <td style="text-align: right;border-left: none;"> <? echo $totamt; ?>  </td>
                            <td style="text-align: right;border-left: none;"> <? echo number_format($totdiscamt, 2, '.', ''); ?> </td>
                            <td style="text-align: right;border-left: none;"> <? echo $getdata['tottaxamt']; ?> </td>

                        </tr>

                        
                        <? include("inv_foot1_print.php"); ?>
                        <? include("inv_foot2_print.php"); ?>
                </div>    
            </div>
            <?php
            include("includes/linksfoot.php");
            ?>
            </body>
            </html>
