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

$maindb = "transacn";
$trandb = "";
$formname = "Pending Repair report";
$file = "pendrep_fr.php";
$srcond = "";

    $spltype = $_SESSION["spltype_" . $dbname];
    if ($spltype != 1) {
        if (!empty($_POST["series"])) {
            $branch = trim($_POST["series"]);

            $srcond = "and `series`='$branch'";
        }
    }


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



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

    $karigar = $_POST["karigar"];
    $fdate = $_POST["fdate"];
    $tdate = $_POST["tdate"];

    if (!empty($karigar)) {
        $karigarcond = " AND `accode`='$karigar' ";
    }

    $mfrmdate = trim($_POST["fdate"]);
    $mtodate = trim($_POST["tdate"]);

    // $dcond = " and `vouchdt` >= '$fdate' and `vouchdt` <= '$tdate' ";
    // if (empty($fdate)) {
    //     browse("From Date Cannot Be Empty");
    //     header("refresh:1; expenserep.php");
    // }
    // if (empty($tdate)) {
    //     browse("To Date Cannot Be Empty");
    //     header("refresh:1; expenserep.php");
    // }

    $getpmain = mysqli_query($conn, "SELECT a.* from (SELECT accode,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt,sum(qty) as tqty,itemcode,color,rate FROM `$maindb` WHERE
                           `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and (source = 'REPG' or source = 'REPT')$srcond $karigarcond group by accode,itemcode,color) a where tqty != 0")or die(mysqli_error($conn));


//    print table($getpmain);
    $mfdt = date("d-m-Y", strtotime($fdate));
    $mtdt = date("d-m-Y", strtotime($tdate));
    include("includes/linkshead.php");
    ?>
    <style>
        @media print
        {    
            .no-print, .no-print *
            {
                display: none !important;
            }
        }
    </style>
    </head>
    <body>
        <div class="container">
            <?php
            include("includes/head_rep.php");
            ?>
            <div class="row">
                <div class="col-lg-4 col-md-2 col-sm-2 col-xs-2 pad">
                    <label> <?php echo $cashsale; ?></label>
                </div>
            </div>
            <div class="row">

                <div class="col-lg-12 pad">
                    <table style="width:100%;background: white;" class="table table-condensed table-striped table-bordered table-hover">
                        <thead>
                            <tr style="" class="mySearchTr">
                                <th style="width:5%;">Sr.</th>
                                <th style="width:10%;">Date</th>
                                <th style="width:30%;">Karigar</th>
                                <th style="width:20%;">Item</th>
                                <th style="width:20%;">Color</th>
                                <th style="width:10%;" class='text-right'>Bal. Qty.</th>
                                <th style="width:10%;" class='text-right'>Rate</th>
                                <th style="width:10%;" class='text-right'>Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totamt = $totqty = 0;
                            while ($cmain = mysqli_fetch_array($getpmain)) {

                                $getparty = mysqli_query($conn, "SELECT `name`, `accode` FROM accmast WHERE `accode`='$cmain[accode]'")or die(mysqli_error($conn));
                                $showparty = mysqli_fetch_array($getparty);

                                $getitem = mysqli_query($conn, "SELECT `name`, `code` FROM item WHERE `code`='$cmain[itemcode]'")or die(mysqli_error($conn));
                                $showitem = mysqli_fetch_array($getitem);

                                $getcolor = mysqli_query($conn, "SELECT `name`, `code` FROM color WHERE `code`='$cmain[color]'")or die(mysqli_error($conn));
                                $showcolr = mysqli_fetch_array($getcolor);


                                if($cmain[tqty] < 0){
                                    $tqty = $cmain[tqty] * -1;
                                }
                                $mamt = $cmain[rate] * $tqty;
                                $ctr++;
                                Echo "<tr>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$cmain[mydt]</td>";
                                Echo "<td>$showparty[name]</td>";
                                Echo "<td>$showitem[name]</td>";
                                Echo "<td>$showcolr[name]</td>";
                                Echo "<td class='text-right'>$tqty</td>";
                                Echo "<td class='text-right'>$cmain[rate]</td>";
                                Echo "<td class='text-right'>$mamt</td>";
                                Echo "</tr>";
                                $totqty = $totqty + $tqty;
                                $totamt = $totamt + $mamt;
                            }

                            Echo "<tr>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td class='text-right' colspan='3'><b>Total</b></td>";
                            Echo "<td class='text-right'><b>$totqty</b></td>";
                            Echo "<td class='text-right' colspan='2'><b>$totamt</b></td>";
                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php
        include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}

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

    $cust = $_POST["cust"];
    $fdate = $_POST["fdate"];
    $tdate = $_POST["tdate"];
    $orderby = $_POST["orderby"];

    $gcond = '';
    $mcond = '';
    $cucond = '';
    $stcond = '';
    $fdatecond = '';
    $tdatecond = '';

    if (!empty($cust)) {
        $cucond = " AND `accode`='$cust' ";
    }


    $dcond = " and `vouchdt` >= '$fdate' and `vouchdt` <= '$tdate' ";

    if (empty($fdate)) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; expenserep.php");
    }

    if (empty($tdate)) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; expenserep.php");
    }




    if ($orderby == "1") {
        $getcash = mysqli_query($conn, "select party,count(*) as norec, sum(netamt) as tamt from `salemain` where party != '' $dcond group by party order by norec desc")or die(mysqli_error($conn));
    } else {
        $getcash = mysqli_query($conn, "select party,count(*) as norec, sum(netamt) as tamt from `salemain` where party != '' $dcond group by party order by tamt desc")or die(mysqli_error($conn));
    }
//    print table($getcash);
    $mfdt = date("d-m-Y", strtotime($fdate));
    $mtdt = date("d-m-Y", strtotime($tdate));

    $contents .= "Sr.No,Visit,Name,Amount\n";

    $ctr = 1;
    $totamt = 0;
    while ($cmain = mysqli_fetch_array($getcash)) {

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


        $mamt = $cmain[tamt];

        $cnorec = $cmain[norec];
        $spname = $showparty[name];


        $contents .= $ctr . ",";
        $contents .= $cnorec . ",";
        $contents .= $spname . ",";
        $contents .= $mamt . "\n";

        $ctr++;

        $totamt = $totamt + $mamt;
    }


    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= "Total" . ",";
    $contents .= $totamt . "\n";


    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1) {
        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;
    }
}
?>



