<?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];
mysqli_select_db($conn, $zdb);
$maindb = 'transacn';
$zroot = $_SESSION["zdbroot"];
$formname = 'Product Inward / Outward History';
$file = 'prodhist.php';

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

$product = $_POST["product"];
$godown = $_POST["godown"];
$type = $_POST["type"];

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

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

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



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 = '';

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

$gproduct2 = mysqli_query($conn, "SELECT code , prodcode, name FROM `product` WHERE code = '$prodcode' ") or die(mysqli_error($conn));
$fproduct = mysqli_fetch_array($gproduct2);
$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;
        }

        .colr {
            background: #26A69A;
            font-size: 17px;
        }
        .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">
                    <table class="table table-bordered">
                        <tr class="mySearchTr">
                            <td style="color: white"><b>Product : <?php echo $fproduct['name']; ?></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%;">Party</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['itemcode'];
                                $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 = "";
                                $source = "";
                                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 "SLVT":
                                        $source = "TAX INVOICE";
                                        $getaccode = mysqli_query($conn, "SELECT `party` FROM `vatmain` WHERE vouchno = '$vouchno'") or die(mysqli_error($conn));
                                        $faccode = mysqli_fetch_array($getaccode);
                                        $accode = $faccode['party'];
                                        break;

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

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

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

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

                                    case "EX":
                                        $source = "EXCESS";
                                        $accode = "";
                                        break;

                                    case "IN":
                                        $source = "INWARD";
                                        $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) {

                                    $getopen = mysqli_query($conn, "SELECT sum(qty) as sqty FROM `transacn` WHERE `vouchdt` < '$mfrmdate' $pcodecond2 ") or die(mysqli_error($conn));

                                    $iopen = mysqli_fetch_array($getopen);
                                    $openqty2 = $iopen['sqty'];

                                    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>";
                                    if ($openqty2 > 0) {
                                        echo "<td class='text-right'>$openqty2</td>";
                                        echo "<td class='text-right'></td>";
                                        $totadd = $totadd + $openqty2;
                                    } else {
                                        echo "<td class='text-right'></td>";
                                        echo "<td class='text-right'>" . abs($openqty2) . "</td>";
                                        $totless = $totless + abs($openqty2);
                                    }

                                    echo "<td class='text-right'></td>";
                                    echo "</tr>";
                                    $ctr++;
                                    $balance = $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 style='font-size:12px'>$source</td>";
                                echo "<td style='font-size:12px'>$partyname</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;
                            }
                            $totbal = $totadd - $totless;

                            echo "<tr class='colr'>";
                            echo "<td class='colr text-right tsize' colspan = '6'>Total</td>";
                            echo "<td class='colr text-right tsize'><b>$totadd</b></td>";
                            echo "<td class='colr text-right tsize'><b>$totless</b></td>";
                            echo "<td class='colr 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"]) || ($_GET['excel'])) {


    $contents .= "Product : $fproduct[name] \n";

    $contents .= "Sr, Date, , Vouchno, Description, Party, Add, Less, Balance \n";

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

    while ($fproduct = mysqli_fetch_array($mainqry)) {
        $prodcode = $fproduct['itemcode'];
        $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 = "";
        $source = "";
        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 "SLVT":
                $source = "TAX INVOICE";
                $getaccode = mysqli_query($conn, "SELECT `party` FROM `vatmain` WHERE vouchno = '$vouchno'") or die(mysqli_error($conn));
                $faccode = mysqli_fetch_array($getaccode);
                $accode = $faccode['party'];
                break;

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

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

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

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

            case "EX":
                $source = "EXCESS";
                $accode = "";
                break;

            case "IN":
                $source = "INWARD";
                $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) {

            $getopen = mysqli_query($conn, "SELECT sum(qty) as sqty FROM `transacn` WHERE `vouchdt` < '$mfrmdate' $pcodecond2 ") or die(mysqli_error($conn));

            $iopen = mysqli_fetch_array($getopen);
            $openqty2 = $iopen['sqty'];

            $contents .= "$ctr" . ",";
            $contents .= "" . ",";
            $contents .= "OP" . ",";
            $contents .= "" . ",";
            $contents .= "OPENING" . ",";
            $contents .= "" . ",";
            if ($openqty2 > 0) {
                $contents .= "$openqty2" . ",";
                $contents .= "" . ",";
                $totadd = $totadd + $openqty2;
            } else {
                $contents .= "" . ",";
                $contents .= "$openqty2" . ",";
                $totless = $totless + abs($openqty2);
            }
            $contents .= "" . ",";
            $contents .= "\n";

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