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


$formname = "Daily Chart";
$file = "daychart.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];

$dtye = $_POST["dtype"];
$name = $_POST["account"];
$shift = $_POST["shift"];
$product = $_POST["product"];

$fromdate = $_POST["fromdate"];
//browse($fromdate);

$accountcond = "";
$custcodecond = "";
$shiftcond = "";
$productcond = "";
$getpdg = mysqli_query($conn, "SELECT * FROM `compyear`  where finyear= '$myyear'")or die(mysqli_error($conn));
$fetpdg = mysqli_fetch_array($getpdg);
$zprod1 = $fetpdg["prod1"];
$zprod2 = $fetpdg["prod2"];
$zprod3 = $fetpdg["prod3"];
$zprod4 = $fetpdg["prod4"];
mysqli_select_db($conn, $zdb);

if (!empty($dtype)) {
    $dtypecond = " AND `dtype`='$dtype' ";
}

if (!empty($name)) {
    $accountcond = " AND `party`='$name' ";
}

if (!empty($shift)) {
    $shiftcond = " AND `shift`='$shift' ";
}

if (!empty($product)) {
    $productcond = " AND b.`product`='$product' ";
}


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

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

    </style>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <div class="container">
                <div class="row">
                    <div class="col-lg-12 pad">
                        <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:10%">Product</th>
                                    <th style="width:10%" class="text-right">Credit Qty.</th>
                                    <th style="width:10%" class="text-right">Cash Qty.</th>
                                    <th style="width:10%" class="text-right">Testing</th>
                                    <th style="width:10%" class="text-right">Total</th>
                                    <th style="width:10%" class="text-right">A/c Sales</th>
                                    <th style="width:10%" class="text-right">Cash</th>
                                    <th style="width:10%" class="text-right">Cash Amount</th>

                                </tr>
                            </thead>
                            <?php
                            $getpms = mysqli_query($conn, "SELECT a.*,b.prodcode,sum(b.sale) as total, sum(b.testing) as testing
                                                           from prdmain a, prdtran b where a.vouchno=b.vouchno and a.vouchdt = '$fromdate'
                                                           group by b.prodcode ORDER BY b.pumpno")or die(mysqli_error($conn));


                            $tamount = $tcredsale = $tcashsale = $tcredamt = $tcashamt = $ttotal = $gamount = 0;
                            while ($fetpms = mysqli_fetch_array($getpms)) {
                                $getpdname = mysqli_query($conn, "select name from product where prodcode = '$fetpms[prodcode]'")or die(mysqli_error($conn));
                                $fetpdname = mysqli_fetch_array($getpdname);

                                $getpdgrp = mysqli_query($conn, "select sum(credsale) as credsale,sum(credamt) as credamt, sum(cashsale) as cashsale, sum(cashamt) as cashamt, rate from   pumpgrp
                                                                 where vouchno = '$fetpms[vouchno]' and prodcode = '$fetpms[prodcode]' group by prodcode")or die(mysqli_error($conn));
                                $fetpdgrp = mysqli_fetch_array($getpdgrp);
                                $tamount = $fetpdgrp['cashamt'] + $fetpdgrp['credamt'];
                                echo "<tr>";
                                echo "<td>$fetpdname[name]</td>";
                                echo "<td class='text-right'>$fetpdgrp[credsale]</td>";
                                echo "<td class='text-right'>$fetpdgrp[cashsale]</td>";
                                echo "<td class='text-right'>$fetpms[testing]</td>";
                                echo "<td class='text-right'>$fetpms[total]</td>";
                                echo "<td class='text-right'>$fetpdgrp[credamt]</td>";
                                echo "<td class='text-right'>$fetpdgrp[cashamt]</td>";
                                echo "<td class='text-right'>$tamount</td>";
                                echo "</tr>";
                                $tcashamt = $tcashamt + $fetpdgrp['cashamt'];
                                $tcredamt = $tcredamt + $fetpdgrp['credamt'];
                                $tcredsale = $tcredsale + $fetpdgrp['credsale'];
                                $tcashsale = $tcashsale + $fetpdgrp['cashsale'];
                                $ttesting = $ttesting + $fetpms['testing'];
                                $ttotal = $ttotal + $fetpms['total'];
                                $gamount = $gamount + $tamount;
//                                $mtankno = $ttankno;
                            }
                            echo "<tr >";
                            echo "<td class='text-right' style='color:blue;font-size:20px;'>Total</td>";
                            echo "<td class='text-right' style='color:blue;font-size:20px;'>$tcredsale</td>";
                            echo "<td class='text-right' style='color:blue;font-size:20px;'>$tcashsale</td>";
                            echo "<td class='text-right' style='color:blue;font-size:20px;'>$ttesting</td>";
                            echo "<td class='text-right' style='color:blue;font-size:20px;'>$ttotal</td>";
                            echo "<td class='text-right' style='color:blue;font-size:20px;'>$tcredamt</td>";
                            echo "<td class='text-right' style='color:blue;font-size:20px;'>$tcashamt</td>";
                            echo "<td class='text-right' style='color:blue;font-size:20px;'>$gamount</td>";
                            echo "</tr>";

                            echo "<tr style='height:25px'>";
                            echo "</tr>";
                            ?>
                        </table>

                        <br>

                        <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                            <h4>Stock Reading Summary    </h4>

                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:20%">Product</th>
                                    <th style="width:10%" class="text-right">A/c Sales</th>
                                    <th style="width:10%" class="text-right">Cash Sales</th>
                                    <th style="width:10%" class="text-right">Total</th>
                                    <!--<th style="width:10%" class="text-right">Rate</th>-->
                                    <th style="width:10%" class="text-right">A/c Sales</th>
                                    <th style="width:10%" class="text-right">Cash Sales</th>
                                    <th style="width:10%" class="text-right">Total</th>

                                </tr>
                            </thead>
                            <?php
                            $getsd1 = mysqli_query($conn, "select * from stkmain where vouchdt = '$fromdate' ")or die(mysqli_error($conn));
                            $fetsd1 = mysqli_fetch_array($getsd1);
                            $stkcode = $fetsd1['vouchno'];


                            $getnoprod = mysqli_query($conn, "select * from stktran where prodcode NOT IN ('$zprod1','$zprod2','$zprod3','$zprod4') and vouchno = '$stkcode' group by prodcode")or die(mysqli_error($conn));
                            $tstkamt = $acctamt = $totamt = 0;
                            $tacctsale = $tcashsale = $tsale = $tacctamt = $tcashamt = $gtotamt = 0;
                            while ($fetnoprod = mysqli_fetch_array($getnoprod)) {

                                $getpdname = mysqli_query($conn, "select name from product where prodcode = '$fetnoprod[prodcode]'")or die(mysqli_error($conn));
                                $fetpdname = mysqli_fetch_array($getpdname);

                                $acctamt = $fetnoprod['acctsale'] * $fetnoprod['rate'];
                                $totamt = $acctamt + $fetnoprod['cashamt'];
                                echo "<tr>";
                                echo "<td>$fetpdname[name]</td>";
                                echo "<td class='text-right'>$fetnoprod[acctsale]</td>";
                                echo "<td class='text-right'>$fetnoprod[cashsale]</td>";
                                echo "<td class='text-right'>$fetnoprod[sale]</td>";
                                echo "<td class='text-right'>$acctamt</td>";
                                echo "<td class='text-right'>$fetnoprod[cashamt]</td>";
                                echo "<td class='text-right'>$totamt</td>";
                                echo "</tr>";
                                $tacctsale = $tacctsale + $fetnoprod['acctsale'];
                                $tcashsale = $tcashsale + $fetnoprod['cashsale'];
                                $tsale = $tsale + $fetnoprod['sale'];
                                $tacctamt = $tacctamt + $acctamt;
                                $tcashamt = $tcashamt + $fetnoprod['cashamt'];
                                $gtotamt = $gtotamt + $totamt;
                            }

                            echo "<tr >";
                            echo "<td class='text-right' style='color:blue;font-size:20px;'>Total</td>";
                            echo "<td class='text-right' style='color:blue;font-size:20px;'>$tacctsale</td>";
                            echo "<td class='text-right' style='color:blue;font-size:20px;'>$tcashsale</td>";
                            echo "<td class='text-right' style='color:blue;font-size:20px;'>$tsale</td>";
                            echo "<td class='text-right' style='color:blue;font-size:20px;'>$tacctamt</td>";
                            echo "<td class='text-right' style='color:blue;font-size:20px;'>$tcashamt</td>";
                            echo "<td class='text-right' style='color:blue;font-size:20px;'>$gtotamt</td>";
                            echo "</tr>";

                            echo "<tr style='height:25px'>";
                            echo "</tr>";
                            ?>
                        </table>

                        <br>

                        <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                            <h4>Department Sales</h4>

                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:40%" class="">Department</th>
                                    <th style="width:10%" class="text-right">A/c Sales</th>
                                    <th style="width:10%" class="text-right">Cash Sales</th>
                                    <th style="width:10%" class="text-right">Total</th>
                                </tr>
                            </thead>
                            <?php
                            $getpdgrp = mysqli_query($conn, "select sum(cashamt) as cashamt from   pumpgrp
                                                                 where vouchno = '$fetpms[vouchno]'")or die(mysqli_error($conn));
                            $fetpdgrp = mysqli_fetch_array($getpdgrp);
                            $pcash1 = $fetpdgrp['cashamt'];

                            $getstkgrp = mysqli_query($conn, "select sum(cashamt) as cashamt  from stktran where prodcode NOT IN ('$zprod1','$zprod2','$zprod3','$zprod4') 
                                                              and vouchno = '$stkcode'")or die(mysqli_error($conn));

                            $fetstkgrp = mysqli_fetch_array($getstkgrp);
                            $pcash2 = $fetstkgrp['cashamt'];

                            $pcash = $pcash1 + $pcash2;

                            $getdrvsl = mysqli_query($conn, "SELECT Sum(CASE paidby WHEN 'A' THEN billamt ELSE 0 END) as accread, 
                                                              000000000.000 as cashread FROM salemain Where vouchdt = '$fromdate'")or die(mysqli_error($conn));
//                            print table($getdrvsl);
                            $fetdrvsl = mysqli_fetch_array($getdrvsl);

                            $cashread = $pcash;
                            $accread = $fetdrvsl['accread'];
                            $totdrv = $cashread + $accread;

                            echo "<tr>";
                            echo "<td>Drive Way</td>";
                            echo "<td class='text-right'>$accread</td>";
                            echo "<td class='text-right'>$cashread</td>";
                            echo "<td class='text-right'>$totdrv</td>";
                            echo "</tr>";



                            echo "<tr style='height:25px'>";
                            echo "</tr>";
                            ?>
                        </table>

                        <br>

                        <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                            <h4>Purchase</h4>

                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:40%" class="">Purchase</th>
                                    <th style="width:10%" class="text-right">Total</th>
                                </tr>
                            </thead>
                            <?php
                            $mprod = "";
                            for ($i = 1; $i <= 5; $i++) {


                                $db = "purfueltran";
                                if ($i == 1) {
                                    $mprod = "AND `prodcode` = '$zprod1'";
                                } elseif ($i == 2) {
                                    $mprod = "AND `prodcode` = '$zprod2'";
                                } elseif ($i == 3) {
                                    $mprod = "AND `prodcode` = '$zprod3'";
                                } elseif ($i == 4) {
                                    $mprod = "AND `prodcode` = '$zprod4'";
                                } elseif ($i == 5) {
                                    $db = "purtran";

                                    $mprod = "AND `prodcode` != '$zprod1'  AND `prodcode` != '$zprod2' AND `prodcode` != '$zprod3' AND `prodcode` != '$zprod4' ";
                                }

                                $getpdname = mysqli_query($conn, "select name from product where prodcode != '' $mprod")or die(mysqli_error($conn));
                                $fetpdname = mysqli_fetch_array($getpdname);

//                                print "SELECT SUM(amount) as accread FROM purtran WHERE vouchdt = '$fromdate'
//                                                              $mprod AND amount > 0 ";
                                $getpur = mysqli_query($conn, "SELECT SUM(amount) as accread FROM $db WHERE vouchdt = '$fromdate'
                                                              $mprod AND amount > 0 ")or die(mysqli_error($conn));

                                $fetpur = mysqli_fetch_array($getpur);
                                if ($i < 5) {

                                    echo "<tr>";
                                    echo "<td>$fetpdname[name]</td>";
                                    echo "<td class='text-right'>$fetpur[accread]</td>";
                                    echo "</tr>";
                                } else {
                                    echo "<tr>";
                                    echo "<td>Others</td>";
                                    echo "<td class='text-right'>$fetpur[accread]</td>";
                                    echo "</tr>";
                                }
                            }
                            $gettax1 = mysqli_query($conn, "SELECT (SUM(totvat)+SUM(totvatadd)+SUM(licenfee)) As amount From purmain WHERE vouchdt = '$fromdate'")or die(mysqli_error($conn));
                            $gettax2 = mysqli_query($conn, "SELECT (SUM(totvat)+SUM(totvatadd)+SUM(licenfee)) As amount From purfuelmain WHERE vouchdt = '$fromdate'")or die(mysqli_error($conn));

                            $fettax1 = mysqli_fetch_array($gettax1);
                            $fettax2 = mysqli_fetch_array($gettax2);
                            $taxamt = $fettax1['amount'] + $fettax2['amount'];
                            echo "<tr>";
                            echo "<td>Taxes + Licence Fees</td>";
                            echo "<td class='text-right'>$taxamt</td>";
                            echo "</tr>";

                            $getexp = mysqli_query($conn, "SELECT SUM(ABS(amount)) As amount From actran WHERE date = '$fromdate' AND grpcode = '012' ")or die(mysqli_error($conn));

                            $fetexp = mysqli_fetch_array($getexp);

                            echo "<tr>";
                            echo "<td>Expense</td>";
                            echo "<td class='text-right'>$fetexp[amount]</td>";
                            echo "</tr>";
                            ?>
                        </table>

                        <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                            <h4>Bank Receipt</h4>

                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:20%" class="">Bank Receipt</th>
                                    <th style="width:10%" class="">Voucher</th>
                                    <th style="width:10%" class="">Code</th>
                                    <th style="width:30%" class="">A/c. Name </th>
                                    <th style="width:10%" class="text-right">Amount</th>
                                </tr>
                            </thead>
                            <tbody>

                                <?php
                                $getbr = mysqli_query($conn, "Select a.vouchno, a.party, 'Cheque' as paytype, a.amount, b.custcode, b.name as descrip
                                                         From bankrecp a, accmast b WHERE a.vouchdt = '$fromdate' AND a.party= b.accode 
                                                         AND trnflag='' order by a.vouchno")or die(mysqli_error($conn));
                                while ($fetbr = mysqli_fetch_array($getbr)) {
                                    echo "<tr>";
                                    echo "<td></td>";
                                    echo "<td>$fetbr[vouchno]</td>";
                                    echo "<td>$fetbr[custcode]</td>";
                                    echo "<td>$fetbr[descrip]</td>";
                                    echo "<td class='text-right'>$fetbr[amount]</td>";
                                    echo "</tr>";
                                }





                                echo "<tr style='height:25px'>";
                                echo "</tr>";
                                ?>
                            </tbody>

                        </table>
                        <br>


                        <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                            <h4>Cash Receipt</h4>

                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:20%" class="">Cash Receipt</th>
                                    <th style="width:10%" class="">Voucher</th>
                                    <th style="width:10%" class="">Code</th>
                                    <th style="width:30%" class="">A/c. Name </th>
                                    <th style="width:10%" class="text-right">Amount</th>
                                </tr>
                            </thead>
                            <tbody>

                                <?php
                                $getcr = mysqli_query($conn, "Select a.vouchno, a.party, 'Cash' as paytype, a.amount, b.custcode, b.name as descrip
                                                         From cashrecp a, accmast b WHERE a.vouchdt = '$fromdate' AND a.party= b.accode 
                                                         AND trnflag='' order by a.vouchno")or die(mysqli_error($conn));
                                while ($fetcr = mysqli_fetch_array($getcr)) {
                                    echo "<tr>";
                                    echo "<td></td>";
                                    echo "<td>$fetcr[vouchno]</td>";
                                    echo "<td>$fetcr[custcode]</td>";
                                    echo "<td>$fetcr[descrip]</td>";
                                    echo "<td class='text-right'>$fetcr[amount]</td>";
                                    echo "</tr>";
                                }





                                echo "<tr style='height:25px'>";
                                echo "</tr>";
                                ?>
                            </tbody>

                        </table>
                        <br>
                        <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                            <h4></h4>

                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:20%" class="">Bank</th>
                                    <th style="width:10%" class="text-right">Op. Balance</th>
                                    <th style="width:10%" class="text-right">Deposits</th>
                                    <th style="width:30%" class="text-right">Withdrawals</th>
                                    <th style="width:10%" class="text-right">Balance</th>
                                </tr>
                            </thead>
                            <tbody>

                                <?php
                                $tbalamt = 0;
                                $tramt = $tpamt = 0;
                                $getbrsm = mysqli_query($conn, "Select accode, name, openbal, dc From accmast WHERE grpcode = '001' Order By Name")or die(mysqli_error($conn));
                                while ($fetbrsm = mysqli_fetch_array($getbrsm)) {
                                    $maccode = $fetbrsm['accode'];
                                    $openbal = $fetbrsm['openbal'];
                                    $name = $fetbrsm['name'];
                                    $dc = $fetbrsm['dc'];

                                    If ($dc == "D") {
                                        $mopen = $openbal * -1;
                                    } Else {
                                        $mopen = abs($openbal);
                                    }
                                    $getbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `actran` WHERE accode = '$maccode' AND 
                                                           `date` < '$fromdate' and `retdate` = '0000-00-00' ") or die(mysqli_error($conn));
                                    $getamt = mysqli_fetch_array($getbal);
                                    $amt = $getamt['amt'];
                                    $mopenbal = $mopen + $amt;

                                    $getrec = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `actran` WHERE accode = '$maccode' AND 
                                                           `date` = '$fromdate' and `retdate` = '0000-00-00' and amount < 0 ") or die(mysqli_error($conn));
                                    $fetrec = mysqli_fetch_array($getrec);
                                    $ramt = $fetrec['amt'];
                                    $getpa = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `actran` WHERE accode = '$maccode' AND 
                                                           `date` = '$fromdate' and `retdate` = '0000-00-00' and amount > 0 ") or die(mysqli_error($conn));
                                    $fetpa = mysqli_fetch_array($getpa);
                                    $pamt = $fetpa['amt'];

                                    $balamt = $mopenbal + $ramt - $pamt;

                                    echo "<tr>";
                                    echo "<td>$fetbrsm[name]</td>";
                                    echo "<td class='text-right'>$mopenbal</td>";
                                    echo "<td class='text-right'>$ramt</td>";
                                    echo "<td class='text-right'>$pamt</td>";
                                    echo "<td class='text-right'>$balamt</td>";
                                    echo "</tr>";
                                    $tramt = $tramt + $ramt;
                                    $tpamt = $tpamt + $pamt;
                                    $tbalamt = $tbalamt + $balamt;
                                }
                                echo "<tr>";
                                echo "<td></td>";
                                echo "<td class='text-right'>Total</td>";
                                echo "<td class='text-right'>$tramt</td>";
                                echo "<td class='text-right'>$tpamt</td>";
                                echo "<td class='text-right'>$tbalamt</td>";
                                echo "</tr>";
                                echo "<tr style='height:25px'>";
                                echo "</tr>";
                                ?>
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:20%" class="">Cash</th>
                                    <th style="width:10%" class="text-right">Op. Balance</th>
                                    <th style="width:10%" class="text-right">Deposits</th>
                                    <th style="width:30%" class="text-right">Withdrawals</th>
                                    <th style="width:10%" class="text-right">Balance</th>
                                </tr>
                            </thead>
                            <?php
                            $tbalamt = 0;
                            $tramt = $tpamt = 0;
                            $balcramt = 0;
                            $getcrsm = mysqli_query($conn, "Select accode, name, openbal, dc From accmast WHERE grpcode = '002' Order By Name")or die(mysqli_error($conn));
                            while ($fetcrsm = mysqli_fetch_array($getcrsm)) {
                                $maccode = $fetcrsm['accode'];
                                $openbal = $fetcrsm['openbal'];
                                $name = $fetcrsm['name'];
                                $dc = $fetcrsm['dc'];

                                If ($dc == "D") {
                                    $mopen = $openbal * -1;
                                } Else {
                                    $mopen = abs($openbal);
                                }
                                $getbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `actran` WHERE accode = '$maccode' AND 
                                                           `date` < '$fromdate' and `retdate` = '0000-00-00' ") or die(mysqli_error($conn));
                                $getamt = mysqli_fetch_array($getbal);
                                $amt = $getamt['amt'];
                                $mopenbal = $mopen + $amt;

                                $getrec = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `actran` WHERE accode = '$maccode' AND 
                                                           `date` = '$fromdate' and `retdate` = '0000-00-00' and amount < 0 ") or die(mysqli_error($conn));
                                $fetrec = mysqli_fetch_array($getrec);
                                $ramt = $fetrec['amt'];
                                $getpa = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `actran` WHERE accode = '$maccode' AND 
                                                           `date` = '$fromdate' and `retdate` = '0000-00-00' and amount > 0 ") or die(mysqli_error($conn));
                                $fetpa = mysqli_fetch_array($getpa);
                                $pamt = $fetpa['amt'];

                                $balcramt = $mopenbal + $ramt - $pamt;

                                echo "<tr>";
                                echo "<td>$fetcrsm[name]</td>";
                                echo "<td class='text-right'>$mopenbal</td>";
                                echo "<td class='text-right'>$ramt</td>";
                                echo "<td class='text-right'>$pamt</td>";
                                echo "<td class='text-right'>$balcramt</td>";
                                echo "</tr>";
                                $tramt = $tramt + $ramt;
                                $tpamt = $tpamt + $pamt;
                                $tbalamt = $tbalamt + $balcramt;
                            }


                            echo "<tr>";
                            echo "<td></td>";
                            echo "<td class='text-right'>Total</td>";
                            echo "<td class='text-right'>$tramt</td>";
                            echo "<td class='text-right'>$tpamt</td>";
                            echo "<td class='text-right'>$tbalamt</td>";
                            echo "</tr>";


                            echo "<tr style='height:25px'>";
                            echo "</tr>";
                            ?>
                            </tbody>

                        </table>

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