<?php
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');

//browse('Work In Progress. Please wait');

$formname = 'Sale Return / 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 mydt FROM `$maindb` WHERE vouchno='$vouchno'") or die(mysqli_error($conn));
$getmdata = mysqli_fetch_array($mainqry);
$vouchdt = $getmdata['vouchdt'];
$advance = $getmdata['totadv'];
$cashcred = $getmdata['paidby'];
$cashvouch = $getmdata['cashvouch'];

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

$gettransp = mysqli_query($conn, "SELECT `code`,`name` FROM `transport` WHERE `code`= '$getmdata[transporter]'") or die(mysqli_error($conn));
$ltrans = mysqli_fetch_array($gettransp);

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

//browse($paytype) ;

$openbal = $cust['openbal'];
$dc = $cust['dc'];

if ($dc == "D") {
    $mopbal = abs($openbal) * -1;
} else {
    $mopbal = abs($openbal);
}

mysqli_query($conn, "drop temporary table if exists baldbf ") or die(mysqli_error($conn));
mysqli_query($conn, "create temporary table baldbf 
                            select * from actran WHERE `accode`= '$accode' and date <= '$vouchdt' ") or die(mysqli_error($conn));

mysqli_query($conn, "delete from baldbf WHERE `source`= 'SL' and vouchno >= '$vouchno' ") or die(mysqli_error($conn));
mysqli_query($conn, "delete from baldbf WHERE `source`= 'CR' and vouchno = '$cashvouch' ") or die(mysqli_error($conn));

$getbal = mysqli_query($conn, "SELECT sum(amount) as amt1 FROM `baldbf` ") or die(mysqli_error($conn));
$fetbal = mysqli_fetch_array($getbal);
$topen1 = $fetbal['amt1'];
$topen = $topen1;

//browse($mopbal);
//browse($topen);

$prevbal1 = $mopbal + $topen;

//browse($mopbal) ;
//browse($topen) ;
//browse($prevbal1) ;

$prevbal = $prevbal1 * -1;

//browse('work in progress..') ;
//browse($advance);
//$actprev = ABS($prevbal) ;
if ($paytype == 'C') {
    $actprev = 0;
//} else {
//    $actprev = abs($actprev) * -1 ;
} else {
    $actprev = $prevbal;
}


//browse($actprev);
//$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: 140mm;
        min-height: 207mm;
        padding: 2mm;
        margin: 2mm auto;
        /*border: 1px #D3D3D3 solid;*/
        /*border-radius: 1px;*/
        /*background: white;*/
        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: 144mm;
            min-height: 200mm;
            padding: 2mm;
            margin: 2mm auto;
            /*padding: 1%;*/
            /*padding: 3mm;*/ 
            /* margin: 1mm auto; */
            /*border: 1px  solid black;*/
            /*border-radius: 1px;*/
            /*background: white;*/
            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');
?>
<!--<body id="main_container" onload="window.print();">-->
<body id="main_container" onload="">
    <!--<div class="book">-->
    <div class="page">
        <div class="subpage">
            <table style="width:100%">
                <? include("includes/inv_head1_print.php"); ?>
                <!--            <div>
                                <label class="label-control marg" >Mobile : +971 509786865</label>
                            </div>-->
            </table>
            <br>
            <? include("includes/inv_head2cred_print.php"); ?>
            <br>
            <!--<div style="border:1px solid black;">-->
            <table style="width:100%" class="">
                <tr>
                    <th class="border" style="width:5%;  text-align: center;">Sr</th>
                    <th class="border" style="width:44%; text-align: center;">Item</th>
                    <th class="border" style="width:10%; text-align: right;">Qty.</th>
                    <th class="border" style="width:10%; text-align: right;">Rate</th>
                    <th class="border" style="width:10%; text-align: right;">Amount</th>
                </tr>
                <!--</table>-->
                <!--</div>-->

                <?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);
                    ?>
                                                                            <!--<table style="width:100%" class="mc-table ">-->
                    <tr class="" style="height:24px;">
                        <td class="border f12" style="text-align: center;"><?php echo "$row[srno]"; ?></td>
                        <td class="border f12"><?php echo "$getb[name]"; ?></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[rate]"; ?></td>
                        <td class="border f12" style="text-align: right"><?php echo "$row[amount]"; ?></td>
                    </tr>
                    <!--</table>-->
                    <?
                    $ctab++;
                    if ($ctab > 14) {
                        ?>
                        <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>

                </div>    
            </div>

            <? include("includes/inv_foot2_print.php"); ?>
        </table >
        <br>
        <div class="subpage">
            <table style="width:100%">
                <? include("includes/inv_head1_print.php"); ?>
            </table>
            <br>
            <? include("includes/inv_head2_print.php"); ?>
            <br>
            <table style="width:100%;" class="">
                <tr>
                    <th class="border" style="width:5%;  text-align: center;">Sr</th>
                    <th class="border" style="width:44%; text-align: center;">Product</th>
                    <th class="border" style="width:10%; text-align: right;">Qty.</th>
                    <th class="border" style="width:10%; text-align: right;">Rate</th>
                    <th class="border" style="width:10%; text-align: right;">Amount</th>
                </tr>                                
                <!--                        </table>-->
                <?php
                $ctab = 0;
            }

            $grqty = $grqty + $row['qty'];
            $totamt = $totamt + $row['amount'];
        }
        if ($ctab < 14) {
            for ($i = $ctab; $i <= 14; $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>
                </tr>      
                <?php
            }
        }
        ?>  
        <? include("includes/inv_foot1cred_print.php"); ?>
        <? // include("includes/inv_foot2_print.php"); ?>
    </table>
</div>
</div>    
<!--</div>-->
<!--</div>-->
<?php
include("includes/linksfoot.php");
?>

</body>
</html>
