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

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$maindb = 'transacn';
$zroot = $_SESSION["zdbroot"];
$formname = 'Product Inward / Outward Histtory';
$file = 'prodhist.php';

if ($_GET["pcode"] != '') {
    $prodcode = $_GET["pcode"];
} else {
    $prodcode = $_POST["barcode"];
}


$godown = $_POST["godown"];

//$type = $_POST["type"];

$pcodecond = '';
$prodcond = '';
$grpcond = '';
$godwncond = '';

if (!empty($prodcode)) {
    $pcodecond = " AND `prodcode` = '$prodcode' ";
} else {
    
}

if (!empty($prodgrp)) {
    $grpcond = " AND `prodgrp` = '$prodgrp' ";
}
if (!empty($godown)) {
    $godwncond = " AND `godown` = '$godown' ";
}

if (!empty($_GET['todate'])) {
    $todate = trim($_GET["todate"]);
} else {
    $todate = $_POST["todt"];
}
$mfrmdate = $_POST['frmdt'];
$as_on_date = date("d-m-Y", strtotime($todate));

$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($todate));

$tcond = '';
mysqli_select_db($conn, $zdb);



$mainqry = mysqli_query($conn, "SELECT SUM(qty) AS `sqty`, prodcode, source, vouchno, godown, DATE_FORMAT(vouchdt , '%d-%m-%Y') as mydt FROM `transacn`
                                WHERE `prodcode`='$prodcode' AND `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$todate' $godwncond $tcond 
                                group by vouchno, source order by vouchdt, source, vouchno")or die(mysqli_error($conn));

$gproduct2 = mysqli_query($conn, "SELECT prodtype, code , prodname as name, size, quality, brand FROM `item` WHERE code = '$prodcode' ")or die(mysqli_error($conn));
$fproduct = mysqli_fetch_array($gproduct2);
if ($fproduct['prodtype'] == 'T') {
    $typename = 'Tiles';
} else {
    if ($fproduct['prodtype'] == 'S') {
        $typename = 'Sanitary';
    } else {
        $typename = 'Others';
    }
}

$header = $typename . '  -  ' . trim($fproduct['name']) . '  -  ' . trim($fproduct['brand']) . '  -  ' . trim($fproduct['size']) . '  -  ' . trim($fproduct['quality']);

$getno = mysqli_num_rows($mainqry);
if (isset($_POST["view"]) || ($_GET['view'])) {

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        th{
            background: #26A69A;
        }
        .tsize{
            font-size:18px;
            padding : 6px;
        }
        tr td{
            font-size:14px;
        }

    </style>
    </head>
    <body>
        <div class="container" style="width:70%">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table class="table table-bordered">
                        <tr class="mySearchTr">
                            <td style="color: white"><b><?php echo $header ?></b></td>
                        </tr>
                    </table>

                    <table style="background: white;width: 100%" class="table table-condensed table-hover table-striped table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:3%;">Sr</th>
                                <th style="width:9%;">Date</th>
                                <th style="width:4%;"></th>
                                <th style="width:6%;">Vouchno</th>
                                <th style="width:7%;">Description</th>
                                <th style="width:20%;">Godown</th>
                                <th style="width:7%;" class="text-right">Add</th>
                                <th style="width:7%;" class="text-right">Less</th>
                                <th style="width:8%;" class="text-right">Balance</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $add = 0;
                            $less = 0;
                            $balance = $totinwd = $totadd = $totless = 0;

                            while ($fproduct = mysqli_fetch_array($mainqry)) {
                                $prodcode = $fproduct['prodcode'];
                                $godown = $fproduct['godown'];
                                $date = $fproduct['mydt'];
                                $src = $fproduct['source'];
                                $vouchno = $fproduct['vouchno'];

                                $add = 0;
                                $less = 0;
                                $faccode = $partyname = "";

                                if ($fproduct['sqty'] > 0) {
                                    $add = $fproduct["sqty"];
                                } else if ($fproduct['sqty'] < 0) {
                                    $less = abs($fproduct['sqty']);
                                }
                                
                                $accode = 0;
                                switch ($src) {
                                    case "OP":
                                        $source = "OPENING";
                                        $accode = 0;
                                        break;

                                    case "SL":
                                        $source = "SALE";
                                        $getaccode = mysqli_query($conn, "SELECT `party` FROM `salemain` WHERE vouchno = '$vouchno'") or die(mysqli_error($conn));
                                        $faccode = mysqli_fetch_array($getaccode);
                                        $accode = $faccode['party'];
                                        break;

                                    case "IN":
                                        $source = "INWARD";
                                        $accode = 0;
                                        break;

                                    case "OU":
                                        $source = "OUTWARD";
                                        $accode = 0;
                                        break;

                                    case "DM":
                                        $source = "DAMAGED";
                                        $accode = 0;
                                        break;

                                    case "TR":
                                        $source = "TRANSFER";
                                        $accode = 0;
                                        break;
                                }

                                if (!empty($godown)) {
                                    $getgodw = mysqli_query($conn, "SELECT `name` FROM `godown` WHERE `gcode`='$godown'")or die(mysqli_error($conn));
                                    $fgodown = mysqli_fetch_array($getgodw);
                                    $godwname = $fgodown['name'];
                                } else {
                                    $godwname = '';
                                }

                                $ctr++;

                                if ($ctr == 1) {
                                    $getopening = mysqli_query($conn, "SELECT qty from transacn where source = 'OP' $pcodecond2 $godwncond ")or die(mysqli_error($conn));
                                    $fopening = mysqli_fetch_array($getopening);
                                    $openqty = $fopening['qty'];


                                    $getopen = mysqli_query($conn, "SELECT sum(qty) as sqty FROM `transacn` WHERE vouchno != ''  
                                                             and `vouchdt` < '$mfrmdate' $pcodecond2 $godwncond  ") or die(mysqli_error($conn));
                                    $iopen = mysqli_fetch_array($getopen);
                                    $openqty2 = $iopen['sqty'] + $openqty;


                                    if ($openqty2 > 0) {
                                        Echo"<tr>";
                                        Echo"<td class=''>$ctr</td>";
                                        Echo"<td class=''></td>";
                                        Echo"<td class=''>OP</td>";
                                        Echo"<td class=''></td>";
                                        Echo"<td>OPENING</td>";
                                        Echo"<td></td>";
                                        Echo"<td class='text-right'>$openqty2</td>";
                                        Echo"<td class='text-right'></td>";
                                        Echo"<td class='text-right'></td>";
                                        Echo"</tr>";
                                        $ctr++;
                                        $balance = $openqty;
                                        $totadd = $totadd + $openqty2;
                                    }
                                }

                                $balance = $balance + $add - $less;
                                Echo"<tr>";
                                Echo"<td class=''>$ctr</td>";
                                Echo"<td class=''>$date</td>";
                                Echo"<td class=''>$src</td>";
                                Echo"<td class=''>$vouchno</td>";
                                Echo"<td>$source</td>";
                                Echo"<td>$godwname</td>";
                                Echo"<td class='text-right'>$add</td>";
                                Echo"<td class='text-right'>$less</td>";
                                Echo"<td class='text-right'>" . $balance . "</td>";
                                Echo"</tr>";

                                $totadd = $totadd + $add;
                                $totless = $totless + $less;
//                                $totless2 = $totless2 + $less2;
                            }
//                            if ($openqty2 > 0) {
//                                
//                            }
//                            $totadd = $openqty2;
                            $totbal = $totadd - $totless;

                            Echo "<tr style='background-color:#26A69A; color:white'>";
                            Echo "<td class='text-right tsize' colspan = '6'>Total</td>";
                            Echo "<td class='text-right tsize'><b>$totadd</b></td>";
                            Echo "<td class='text-right tsize'><b>$totless</b></td>";
                            Echo "<td class='text-right tsize'><b>$totbal</b></td>";
                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div> 
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {
    $contents .= "$header" . "\n";
    $contents .= "Sr,Date,,Vouchno,Description,Godown,Add,Less,Balance\n";

    $ctr = 0;
    $add = 0;
    $less = 0;
    $balance = $totinwd = $totadd = $totless = 0;

    while ($fproduct = mysqli_fetch_array($mainqry)) {
        $prodcode = $fproduct['prodcode'];
        $date = $fproduct['mydt'];
        $src = $fproduct['source'];
        $vouchno = $fproduct['vouchno'];

        $add = 0;
        $less = 0;
        $faccode = $partyname = "";

        if ($fproduct['sqty'] > 0) {
            $add = $fproduct["sqty"];
        } else if ($fproduct['sqty'] < 0) {
            $less = abs($fproduct['sqty']);
//                                    $less2 = $fproduct['sqty'];
        }
        $accode = "";
        switch ($src) {
            case "OP":
                $source = "OPENING";
                $faccode = "";
                break;

            case "SL":
                $source = "SALE";
                $getaccode = mysqli_query($conn, "SELECT `party` FROM `salemain` WHERE vouchno = '$vouchno'") or die(mysqli_error($conn));
                $faccode = mysqli_fetch_array($getaccode);
                $accode = $faccode['party'];
                break;

            case "IN":
                $source = "INWARD";
                $accode = "";
                break;

            case "OU":
                $source = "OUTWARD";
                $accode = "";
                break;

            case "DM":
                $source = "DAMAGED";
                $accode = "";
                break;

            case "TR":
                $source = "TRANSFER";
                $accode = "";
                break;
        }

        if (!empty($accode)) {
            $getpartyname = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE `accode`='$accode'")or die(mysqli_error($conn));
            $fparty = mysqli_fetch_array($getpartyname);
            $partyname = $fparty['name'];
        } else {
            $partyname = '';
        }

        $ctr++;

        if ($ctr == 1) {
            $getopening = mysqli_query($conn, "SELECT qty from transacn where source = 'OP' $pcodecond2 $godwncond ")or die(mysqli_error($conn));
            $fopening = mysqli_fetch_array($getopening);
            $openqty = $fopening['qty'];


            $getopen = mysqli_query($conn, "SELECT sum(qty) as sqty FROM `transacn` WHERE vouchno != ''  
                                                             and `vouchdt` < '$mfrmdate' $pcodecond2 $godwncond  ") or die(mysqli_error($conn));
            $iopen = mysqli_fetch_array($getopen);
            $openqty2 = $iopen['sqty'] + $openqty;


            if ($openqty2 > 0) {
                $contents .= "$ctr" . ",";
                $contents .= "" . ",";
                $contents .= "OP" . ",";
                $contents .= "" . ",";
                $contents .= "OPENING" . ",";
                $contents .= "" . ",";
                $contents .= "$openqty2" . ",";
                $contents .= "" . ",";
                $contents .= "" . "\n";

                $ctr++;
                $balance = $openqty;
                $totadd = $totadd + $openqty2;
            }
        }

        $balance = $balance + $add - $less;

        $contents .= "$ctr" . ",";
        $contents .= "$date" . ",";
        $contents .= "$src" . ",";
        $contents .= "$vouchno" . ",";
        $contents .= "$source" . ",";
        $contents .= "$partyname" . ",";
        $contents .= "$add" . ",";
        $contents .= "$less" . ",";
        $contents .= "$balance" . "\n";

        $totadd = $totadd + $add;
        $totless = $totless + $less;
//                                $totless2 = $totless2 + $less2;
    }
//                            if ($openqty2 > 0) {
//                                
//                            }
//                            $totadd = $openqty2;
    $totbal = $totadd - $totless;

    $contents .= "Total" . ",";
    $contents .= "$totadd" . ",";
    $contents .= "$totless" . ",";
    $contents .= "$totbal" . "\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;
    }
}
?>
