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

$file = "damreg_fr.php";
$formname = 'Damaged Register';

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

$maindb = 'dammain';
$trandb = 'damtran';
//$stkdb = 'transacn';
//$accdb = 'actran';

$dtype = $_POST["dtype"];
$product = $_POST["prodname"];
//browse($product);
$productcond = "";

if (!empty($product)) {
    $productcond = "AND prodcode = '$product'";
 }
//browse($productcond);
//
//if (($_GET["fromdate"]) != '') {
//    $mfdt = $_GET["fromdate"];
//    $mtdt = $_GET["todate"];
//    $mfromdate = date("y-m-d", strtotime($mfdt));
//    $mtodate = date("y-m-d", strtotime($mtdt));
//    $dtype = $_GET["rpt"];
//} else {

    $mfromdate = trim($_POST["fromdate"]);
    $mtodate = trim($_POST["todate"]);

    if (empty($_POST["fromdate"])) {
        browse("From Date Cannot Be Empty");
        echo"<script>window.close();</script>";
    }
    if (empty($_POST["todate"])) {
        browse("To Date Cannot Be Empty");
        echo"<script>window.close();</script>";
    }
    $mfdt = date("d-m-Y", strtotime($mfromdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
//}

//if (!empty($gsttype)) {
//    if ($gsttype == 1) {
//        $gstcond = "AND `oms` = '1'";
//    } else {
//        $gstcond = "AND `oms` = ''";
//    }
//} else {
//    $gstcond = "";
//}   $suppcond $ptypecond  $gstcond $pcond 
    
    
 $getpmain2 = mysqli_query($conn, "SELECT a.*,DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,b.* FROM `$maindb` a,`$trandb` b "
                    . "where a.vouchno= b.vouchno AND a.vouchdt >= '$mfromdate' AND a.vouchdt <= '$mtodate' $productcond group by a.vouchno")or die(mysqli_error($conn));

            

            $getno2 = mysqli_num_rows($getpmain2);

if (isset($_POST["view"])) {


    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        .tr1{
           background-color: #98f9f0;
        }

    </style>
    </head>
    <body>


        <div class="container-fluid">
            <?php
            include("includes/head_rep2.php");






            if ($getno2 > 0) {
                ?>
                <div class="row">
                    <div class="col-lg-12 pad">
                        <table class="table table-condesed table-bordered">
                            <tr class="mySearchTr">
                                <!--9221292520-->
                                <th width="100%" class="h4" style="color:white"><b>Damaged Register</b></th>
                            </tr>
                            <tr>
                                <td style="font-size: 15px;padding:5px" class="HDFont" colspan="2">
                                    Total No. of Record :&nbsp;&nbsp;<b><?php echo $getno2; ?></b>
                                </td>
                            </tr>
                        </table>
                        <?php if ($ptype != "") { ?>
                            <table width="100%" class="table" style="background-color: #98F9F0">
                                <tr><td width="7%" style="font-size:14px">Pay Type : </td>
                                    <td width="93%" style="font-size:14px"><b><?php echo $paytype; ?></b></td></tr>
                            </table>
                        <?php } ?>
                        <table style="background: white;width: 100%" class="table table-condensed table-striped table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th width="3%;">Sr</th>
                                    <th width="4%;">Voucher</th>
                                    <th width="6%;">Date</th>
                                    <th width="20%;" class="text-right">Total</th>
                                    <th width="67%;">Remarks</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $pctr = 0;
                                $ptotamt = 0;
                                $ptotdiscamt = 0;
                                $ptottaxable = 0;
                                $ptotgst3 = 0;
                                $ptotgst5 = 0;
                                $ptotgst12 = 0;
                                $ptotgst18 = 0;
                                $ptotgst28 = 0;
                                $ptotpackamt = $ptotstamt = $ptotothamt = $ptotbillamt = 0;
                                $pchtypecond = "";

//                                $getpmain_p = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `rawmain` WHERE `vouchdt` >= '$mfrmdate' 
//                                        AND `vouchdt` <= '$mtodate' $suppcond $godowncond $gstcond $pcond order by vouchdt,vouchno")or die(mysqli_error($conn));


                                while ($pmain = mysqli_fetch_array($getpmain2)) {
//                                    if ($pmain['paidby'] == "C") {
//                                        $ptyped = "CASH";
//                                    } else {
//                                        $ptyped = "CREDIT";
//                                    }


//                                    $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[party]'") or die(mysqli_error($conn));
//                                    $sparty = mysqli_fetch_array($gparty);

                                    $voucher = $pmain['vouchno'];
                                    $mydt = $pmain['mydt'];
                                    $total = $pmain['totamount'];
                                    $remark1 = $pmain['remark1'];
                                    $remark2 = $pmain['remark2'];

                                    $pctr++;
                                    Echo"<tr data-href='purchase.php?mode=edit&vouchno=$voucher' class='clickable-row2' [target]='_blank'>";
                                    Echo"<td>$pctr</td>";
                                    Echo"<td>$voucher</td>";
                                    Echo"<td>$mydt</td>";
                                    Echo"<td class='text-right'>$total</td>";
                                    Echo"<td>$remark1 $remark2</td>";
                                    Echo"</tr>";



                                    $pgrtotal = $pgrtotal + $total;
                                    $pgrtotdis = $pgrtotdis + $totdisc;
                                    $pgrtotvat = $pgrtotvat + $totvat;
                                    $pgrtotbillamt = $pgrtotbillamt + $billamt;

                                    if ($_POST["dtype"] == 2) {

                                        Echo "<tr style='height:10px;'>";
                                        Echo "<td colspan='25'>";
                                        Echo "<table style='width:100%;' id='tabital' class='table table-condensed  table-bordered'>";
                                        Echo "<thead>";
                                        Echo "<tr class='' style='background-color:#BABFC2'>";
                                        Echo "<th style='width:5%;'>Sr</th>";
                                        Echo "<th style='width:15%;' class=''>Item</th>";
                                        Echo "<th style='width:15%;' class='text-right'>Qty.</th>";
                                        Echo "<th style='width:15%;' class='text-right'>Unit</th>";
                                        Echo "<th style='width:15%;' class='text-right'>Cost</th>";
                                        Echo "<th style='width:15%;' class='text-right'>Amount</th>";
                                        Echo "</tr>";
                                        Echo "<thead>";
                                        Echo "<tr>";

                                        $ctr2 = 0;
                                        $pqty = 0;
                                        $pctr2 = 0;
                                        $ptotqty = 0;
                                        $ptotrate = 0;
                                        $ptotamount = $ptotdiscount = $ptotsubtot = $ptotcgstamt = $ptotsgstamt = $ptotigstamt = 0;

                                        $gettran = mysqli_query($conn, "SELECT * FROM `damtran` WHERE `vouchno`='$pmain[vouchno]' $productcond order by srno ") or die(mysqli_error($conn));
//                                        print table($gettran);
//                                        die();
                                        while ($tran = mysqli_fetch_array($gettran)) {
                                            
                                            $ctr2++;
                                            Echo "<tr>";
                                            Echo "<td>$ctr2</td>";
                                            Echo "<td>$tran[prodname]</td>";
                                            Echo "<td class='text-right'>$tran[qty]</td>";
                                            Echo "<td class='text-right'>$tran[unit]</td>";
                                            Echo "<td class='text-right'>$tran[rate]</td>";
                                            Echo "<td class='text-right'>$tran[amount]</td>";
                                            Echo "</tr>";

                                            $pcartqty = $pcartqty + $tran['cartqty'];
                                            $pqty = $pqty + $tran['qty'];
                                            $ptotqty = $ptotqty + $pqty['totqty'];
                                            $ptotamount = $ptotamount + $tran['amount'];
                                            $ptotdiscount = $ptotdiscount + $tran['discamt'];
                                            $ptotvat = $ptotvat + $tran['vat'];
                                            $ptotal = $ptotal + $tran['total'];
                                        }

                                        Echo "<tr style='background-color:#BABFC2'>";
                                        Echo "<td></td>";
                                        Echo "<td class='text-right' style='font-size:13px'><b>Total</b></td>";
                                        Echo "<td class='text-right' style='font-size:12px'><b>$pqty</b></td>";
                                        Echo "<td></td>";
                                        Echo "<td></td>";
                                        Echo "<td class='text-right' style='font-size:12px'><b>$ptotamount</b></td>";
                                        Echo "</tr>";
                                        Echo "</table>";
                                    }
                                }
                                Echo "<tr class='tr1'>";
                                Echo "<td></td>";
                                Echo "<td></td>";
                                Echo "<td class='text-right' style='font-size:15px'><b>Total</b></td>";
                                Echo "<td class='numeric text-right' style='font-size:15px' ><b>" . number_format($pgrtotal, 2) . "</b></td>";
                                Echo "<td></td>";
                                Echo "</tr>";
            }
                                ?>
                            </tbody>
                        </table>
                    </div> 
                </div>

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




if (isset($_POST["excel"])) {




      if ($getno2 > 0) {
                
               $contents .= "Damaged Register\n"; 
                           
                                            $contents .= "Sr,Voucher,Date,Total,Remarks\n";
                                            
                                $pctr = 0;
                                $ptotamt = 0;
                                $ptotdiscamt = 0;
                                $ptottaxable = 0;
                                $ptotgst3 = 0;
                                $ptotgst5 = 0;
                                $ptotgst12 = 0;
                                $ptotgst18 = 0;
                                $ptotgst28 = 0;
                                $ptotpackamt = $ptotstamt = $ptotothamt = $ptotbillamt = 0;
                                $pchtypecond = "";



                                while ($pmain = mysqli_fetch_array($getpmain2)) {
                                    
                                    $voucher = $pmain['vouchno'];
                                    $mydt = $pmain['mydt'];
                                    $total = $pmain['totamount'];
                                    $remark1 = $pmain['remark1'];
                                    $remark2 = $pmain['remark2'];

                                    $pctr++;
                                    $contents .= "$pctr" . ",";
                                    $contents .= "$voucher" . ",";
                                    $contents .= "$mydt" . ",";
                                    $contents .= "$total" . ",";
                                    $contents .= "$remark1 $remark2" . "\n";
                                   


                                    $pgrtotal = $pgrtotal + $total;
                                    $pgrtotdis = $pgrtotdis + $totdisc;
                                    $pgrtotvat = $pgrtotvat + $totvat;
                                    $pgrtotbillamt = $pgrtotbillamt + $billamt;

                                    if ($_POST["dtype"] == 2) {

                                        $contents .= "Sr,Item,Qty.,Unit,Cost,Amount\n";
                                       
                                        $ctr2 = 0;
                                        $pqty = 0;
                                        $pctr2 = 0;
                                        $ptotqty = 0;
                                        $ptotrate = 0;
                                        $ptotamount = $ptotdiscount = $ptotsubtot = $ptotcgstamt = $ptotsgstamt = $ptotigstamt = 0;

                                        $gettran = mysqli_query($conn, "SELECT * FROM `damtran` WHERE `vouchno`='$pmain[vouchno]' $productcond order by srno ") or die(mysqli_error($conn));
//                                        print table($gettran);
//                                        die();
                                        while ($tran = mysqli_fetch_array($gettran)) {
                                            
                                            $ctr2++;
                                            
                                            $contents .= "$ctr2" . ",";
                                            $contents .= "$tran[prodname]" . ",";
                                            $contents .= "$tran[qty]" . ",";
                                            $contents .= "$tran[unit]" . ",";
                                            $contents .= "$tran[rate]" . ",";
                                            $contents .= "$tran[amount]" . "\n";
                                            

                                            $pcartqty = $pcartqty + $tran['cartqty'];
                                            $pqty = $pqty + $tran['qty'];
                                            $ptotqty = $ptotqty + $pqty['totqty'];
                                            $ptotamount = $ptotamount + $tran['amount'];
                                            $ptotdiscount = $ptotdiscount + $tran['discamt'];
                                            $ptotvat = $ptotvat + $tran['vat'];
                                            $ptotal = $ptotal + $tran['total'];
                                        }

                                            $contents .= "" . ",";
                                            $contents .= "Total" . ",";
                                            $contents .= "$pqty" . ",";
                                            $contents .= "" . ",";
                                            $contents .= "" . ",";
                                            $contents .= "$ptotamount" . "\n";
                                     
                                    }
                                }
                                            $contents .= "" . ",";
                                            $contents .= "" . ",";
                               
                                if ($ptype != "") {
                                            $contents .= "" . ",";
                                }
                                            $contents .= "Total" . ",";
                                            $contents .= "$pgrtotal" . ",";
                                            $contents .= "" . "\n";
                                
                            }
                               


    
    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1 || $_POST["dtype"] == 3) {
        header("Content-Disposition: attachment;filename = $formname" . "_Summary" . date('d-m-Y') . ".csv");
        print $contents;
    } else {
        header("Content-Disposition: attachment;filename = $formname" . "_Detail" . date('d-m-Y') . ".csv");
        print $contents;
    }
}
?>