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

$formname = "Daily Report";
$file = "dailyrep_fr.php";

$trandb = "actran";
$salefile = "salemain";
$saleretfile = "credmain";
$purchfile = "purmain";
$purchretfile = "debmain";
$crfile = "cashrecp";
$cpfile = "cashpay";
$brfile = "bankrecp";
$bpfile = "bankpay";
$custtokarifile = "custtokari";
$zroot = $_SESSION["zdir"];

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

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

    $date = $_POST["date"];
    $series = $_POST["series"];

    if (!empty($series)) {
        $seriescond = "AND series = '$series' ";
    }
$getcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company` where code='$zcomp'")or die(mysqli_error($conn));
$comp = mysqli_fetch_array($getcomp);

$qryac = mysqli_query($conn, "SELECT * FROM `accmast` where accode = '3' ")or die(mysqli_error($conn));
$getac = mysqli_fetch_array($qryac);

    $mopbal = $getac["openbal"];
    $mopdc = $getac["dc"];

    if ($mopdc == "D") {
        $mopbal = $mopbal * - 1;
    }

//    browse($date);
//////////////////////////////////////////
//    $csale1 = mysqli_query($conn, "SELECT SUM(netamt) AS amt FROM $salefile WHERE vouchdt = '$date' AND `cashcred` = 'C' group by vouchdt") or die(mysqli_error($conn));
//    $cssale = mysqli_fetch_array($csale1);
//
//    $cashsale = $cssale["amt"];
//    $csale2 = mysqli_query($conn, "SELECT SUM(netamt) AS amt FROM $salefile WHERE vouchdt = '$date' AND  `cashcred` != 'C' group by vouchdt") or die(mysqli_error($conn));
//    $crsale = mysqli_fetch_array($csale2);
//
//    $creditsale = $crsale["amt"];
//    
//    $totsale = $cashsale + $creditsale;
///////////////////////////////////////////// Purchase /////////////////////

    $cpur1 = mysqli_query($conn, "SELECT SUM(netamt) AS amt FROM $purchfile WHERE vouchdt = '$date' AND  `cashcred` = 'C' $seriescond  group by vouchdt") or die(mysqli_error($conn));
    $cspur = mysqli_fetch_array($cpur1);

    $cashpur = $cspur["amt"];

    $cpur2 = mysqli_query($conn, "SELECT SUM(netamt) AS amt FROM $purchfile WHERE vouchdt = '$date' AND  `cashcred` != 'C' $seriescond  group by vouchdt") or die(mysqli_error($conn));
    $cspur = mysqli_fetch_array($cpur2);

    $creditpur = $cspur["amt"];
    $totpur = $cashpur + $creditpur;

///////////////////////////////////////////// Sale Return /////////////////////
    $csaleret1 = mysqli_query($conn, "SELECT SUM(netamt) AS amt FROM $saleretfile WHERE vouchdt = '$date' AND  `cashcred` = 'C' $seriescond  group by vouchdt") or die(mysqli_error($conn));
    $csaleret = mysqli_fetch_array($csaleret1);

    $cashsaleret = $csaleret["amt"];

    $csaleret2 = mysqli_query($conn, "SELECT SUM(netamt) AS amt FROM $saleretfile WHERE vouchdt = '$date' AND  `cashcred` != 'C' $seriescond  group by vouchdt") or die(mysqli_error($conn));
    $csaleret = mysqli_fetch_array($csaleret2);

    $creditsaleret = $csaleret["amt"];
    $totsalereturn = $cashsaleret + $creditsaleret;


///////////////////////////////////////////// New Files cash sale, Credit Sale /////////////////////

    $cashsale = $credale = $ccsale = $totsale = 0;

    $csale1 = mysqli_query($conn, "SELECT SUM(cashamt) AS amt FROM $salefile WHERE vouchdt = '$date' $seriescond ") or die(mysqli_error($conn));
    $cssale = mysqli_fetch_array($csale1);
    $cashsale = $cssale['amt'];

    $csale2 = mysqli_query($conn, "SELECT SUM(credamt) AS amt FROM $salefile WHERE vouchdt = '$date' $seriescond  ") or die(mysqli_error($conn));
    $crsale = mysqli_fetch_array($csale2);
    $creditsale = $crsale["amt"];

    $csale3 = mysqli_query($conn, "SELECT SUM(ccamt) AS amt FROM $salefile WHERE vouchdt = '$date' $seriescond  ") or die(mysqli_error($conn));
    $ccsale = mysqli_fetch_array($csale3);
    $ccsale = $ccsale["amt"];

    $totsale = $cashsale + $creditsale + $ccsale;

///////////////////////////////////////////// New Files cash Purchase, Credit Purchase/////////////////////
    $cashpurret = $creditpurret = $ccpurret = $totpurret = 0;

    // $cpurret1 = mysqli_query($conn, "SELECT SUM(cashamt) AS amt FROM $purchretfile WHERE vouchdt = '$date' $seriescond ") or die(mysqli_error($conn));
    // $cspurret = mysqli_fetch_array($cpurret1);
    // $cashpurret = $cspurret['amt'];

    // $cpurret2 = mysqli_query($conn, "SELECT SUM(credamt) AS amt FROM $purchretfile WHERE vouchdt = '$date' $seriescond  ") or die(mysqli_error($conn));
    // $crpurret = mysqli_fetch_array($cpurret2);
    // $creditpurret = $crpurret["amt"];

    // $ccpurret3 = mysqli_query($conn, "SELECT SUM(ccamt) AS amt FROM $purchretfile WHERE vouchdt = '$date' $seriescond  ") or die(mysqli_error($conn));
    // $ccpurret = mysqli_fetch_array($ccpurret3);
    // $ccpurret = $ccpurret["amt"];

    $totpurret = $cashpurret + $creditpurret + $ccpurret;

/////////////////////////////////////////////
    $crecp2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $crfile WHERE
                      vouchdt = '$date' $seriescond  order by vouchno ") or die(mysqli_error($conn));

    $cpaid2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $cpfile WHERE
                      vouchdt = '$date' $seriescond  ") or die(mysqli_error($conn));

    $brecp2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $brfile WHERE
                       vouchdt = '$date' $seriescond  ") or die(mysqli_error($conn));

    $bpaid2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $bpfile WHERE
                       vouchdt = '$date' $seriescond  ") or die(mysqli_error($conn));

    $custtokari2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $custtokarifile WHERE
                       vouchdt = '$date' $seriescond  ") or die(mysqli_error($conn));

    $slrep = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $salefile WHERE
                      vouchdt = '$date' $seriescond  order by vouchno ") or die(mysqli_error($conn));

    $slretrep = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $saleretfile WHERE
                      vouchdt = '$date' $seriescond  ") or die(mysqli_error($conn));

    $purchrep = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $purchfile WHERE
                       vouchdt = '$date' $seriescond  ") or die(mysqli_error($conn));

    $purchretrep = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $purchretfile WHERE
                       vouchdt = '$date' $seriescond  ") or die(mysqli_error($conn));

//////////////

    $crecp = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $crfile WHERE
                      vouchdt = '$date' $seriescond  group by vouchdt ") or die(mysqli_error($conn));
    $crget = mysqli_fetch_array($crecp);

    $cramt = $crget["amt"];

    $cpaid = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `cashpay` WHERE
                      vouchdt = '$date' $seriescond  group by vouchdt ") or die(mysqli_error($conn));
    $cpget = mysqli_fetch_array($cpaid);

    $cpamt = $cpget["amt"];

    $brecp = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `bankrecp` WHERE
                      vouchdt = '$date' $seriescond  group by vouchdt ") or die(mysqli_error($conn));
    $brget = mysqli_fetch_array($brecp);

    $bramt = $brget["amt"];

    $bpaid = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `bankpay` WHERE
                      vouchdt = '$date' $seriescond  group by vouchdt ") or die(mysqli_error($conn));
    $bpget = mysqli_fetch_array($bpaid);
    
    $bpamt = $bpget["amt"];

    $qrycusttokari = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `custtokari` WHERE
                      vouchdt = '$date' $seriescond  group by vouchdt ") or die(mysqli_error($conn));
    $fetcusttokari = mysqli_fetch_array($qrycusttokari);

    $custtokariamt = $fetcusttokari["amt"];

    include("includes/linkshead.php");
    ?>
    <style>
        .td{
            font-size: 15px;
        }
        @media print {
            .noPrint {
                display:none;
            }
        }

    </style>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 ">

                    <?php
                    $getcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company` WHERE code='$zcomp'")or die(mysqli_error($conn));
                    $comp = mysqli_fetch_array($getcomp);
                    echo "<label style='font-size:20px;'>$comp[name]</label>"
                    ?>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-7 col-md-11 col-sm-11 col-xs-11 ">

                    <label class="label-control marg"><?php Echo $formname; ?> - </label>
                    <?= date("d-m-Y", strtotime($date)); ?>
                </div>
                <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                    <a onclick="window.close()" class="btn btn-primary noPrint">GO Back</a>
                </div>
            </div>
            <?php
//            include("includes/head_rep.php");
//            echo " ( " . date("d-m-Y", strtotime($date)) . ")";
            ?>
                                <!--<table class="table table-bordered" summary="Ledger - Detail" width='100%' border="0">-->
            <table style="width:75%;" class="table-bordered table table-stripped table-hover" summary="">
                <tbody>
                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Opening Balance</b></font></td>
                        <?php
                        $getbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `$trandb` WHERE accode = 3 AND 
                                                           `date` < '$date' and `retdate` = '0000-00-00'  ") or die(mysqli_error($conn));
                        $getamt = mysqli_fetch_array($getbal);
                        $amt = $getamt['amt'];

                        $mobal = $mopbal + $amt;

                        $mobal = $mobal * -1;

                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$mobal</td>";
                        ?>
                        <td class = 'td'   style="text-align:left;" scope="col" width="40"><font size="3px" color=''><b></b></font></td>
                    </tr>
                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;background-color:#99d98c" scope="col" width="20"><font size="3px"><b>Cash Sale</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$cashsale</td>";
                        ?>
                        <td class = 'td'   style="text-align:left;" scope="col" width="40"><font size="3px" color=''><b></b></font></td>
                        <td class = 'td'   style="text-align:left;background-color: #d8e2dc" scope="col" width="20"><font size="3px" color=''><b>Cash Purchase</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$cashpur</td>";
                        ?>
                    </tr>
                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;background-color:#a9def9" scope="col" width="20"><font size="3px" color=''><b>Credit Sale</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$creditsale</td>";
                        ?>
                        <td class = 'td'   style="text-align:left;" scope="col" width="40"><font size="3px" color=''><b></b></font></td>
                        <td class = 'td'   style="text-align:left;background-color: #fcd5ce" scope="col" width="20"><font size="3px" color=''><b>Credit Purchase</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$creditpur</td>";
                        ?>
                    </tr>
                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;background-color:#f1c0e8" scope="col" width="20"><font size="3px" color=''><b>CC Sale</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$ccsale</td>";
                        ?>
                        <td class = 'td'   style="text-align:left;" scope="col" width="40"><font size="3px" color=''><b></b></font></td>
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b></b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\"></td>";
                        ?>
                    </tr>
                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;background-color: #fcf6bd" scope="col" width="20"><font size="3px" color=''><b>Total Sale</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">" . number_format($totsale, 2) . "</td>";
                        ?>
                        <td class = 'td'   style="text-align:left;" scope="col" width="40"><font size="3px" color=''><b></b></font></td>
                        <td class = 'td'   style="text-align:left;background-color: #fde4cf" scope="col" width="20"><font size="3px" color=''><b>Total Purchase</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">" . number_format($totpur, 2) . "</td>";
                        ?>
                    </tr>
                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;background-color: #cbf7ed" scope="col" width="20"><font size="3px" color=''><b>Total Sale Return</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">" . number_format($totsalereturn, 2) . "</td>";
                        ?>
                        <td class = 'td'   style="text-align:left;" scope="col" width="40"><font size="3px" color=''><b></b></font></td>
                        <td class = 'td'   style="text-align:left;background-color: #fbf8cc" scope="col" width="20"><font size="3px" color=''><b>Total Purchase Return</b></font></td>
                            <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">" . number_format($totpurret, 2) . "</td>";
                        ?>
                    </tr>
                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;background-color: #cbf7ed" scope="col" width="20"><font size="3px" color=''><b>Customer To Karigar</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">" . number_format($custtokariamt, 2) . "</td>";
                        ?>
                        <td class = 'td'   style="text-align:left;" scope="col" width="40"><font size="3px" color=''><b></b></font></td>
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b></b></font></td>
                            <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\"></td>";
                        ?>
                    </tr>
            </table>  
            <!--<table class="table table-bordered" summary="Ledger - Detail" width='100%' border="0">-->
            <table style="width:75%;" class="table-bordered table table-stripped table-hover" summary="Ledger - Detail">    
                <?php // if ($prntype == 2) {      ?>
                <tr style="height:10px;">
                    <td class = 'td' colspan="3" style="text-align:center;background-color: #90caf9" scope="col" width="10%"><font size="3px" color=''><b>Cash Receipt</b></font></td>

                </tr>
                <tr style="height:10px;">
                    <td class = 'td'  align="left"  color=''><b>Voucher</b></td>
                    <td class = 'td'  align="left"  color=''><b>A/c. Name.</b></td>
                    <td class = 'td'  align="right" color=''><b>Amount</b></td>
                </tr>

                <?php
                $totrecd = 0;
                $totdisc = 0;
                $totclaim = 0;
                $totamt = 0;

                mysqli_select_db($conn, $zdb);
                while ($row = mysqli_fetch_array($crecp2)) {
                    $getname = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE accode='$row[party]'") or die(mysqli_error($conn));
                    $name = mysqli_fetch_array($getname);
                    echo "<tr style='height:10px;'>";
                    echo " <td class = 'td'  align='left' color=''>$row[vouchno]</td>";
                    echo " <td class = 'td'  align='left' color=''>$name[name]</td>";
                    echo " <td class = 'td'  align='right' color=''>$row[amount]</td>";
                    echo "</tr>";

                    $totrecd = $totrecd + $row['recdamt'];
                    $totdisc = $totdisc + $row['discount'];
                    $totclaim = $totclaim + $row['claim'];
                    $totamt = $totamt + $row['amount'];
                }
                ?>
                <tr>
                    <td class = 'td'  colspan="1">
                        <?php
                        echo " <td class = 'td'  align='right' color=''><b>Total</b></td>";
                        echo " <td class = 'td'  align='right' color='' style='background-color:rgba(183, 183, 183, 0.5'><b>" . number_format($totamt, 2) . "</b></td>";
                        ?>
                    </td>
                </tr>
                <tr>
                    <td class = 'td'  colspan="9">
                    </td>
                </tr>
                <tr>
                    <td class = 'td' colspan="3"  style="text-align:center;background-color: #99d98c" scope="col" width="10"><font size="3px" color=''><b>Cash Payment</b></font></td>
                </tr>
                <tr>
                    <td class = 'td'  align="left" color=''><b>Voucher</b></td>
                    <td class = 'td'  align="left" color=''><b>A/c. Name.</b></td>
                    <td class = 'td'  align="right" color=''><b>Amount</b></td>
                </tr>
                <?php
                $totrecd = 0;
                $totdisc = 0;
                $totclaim = 0;
                $totamt = 0;
                while ($row2 = mysqli_fetch_array($cpaid2)) {

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

                    echo "<tr>";
                    echo " <td class = 'td'  align='left' color=''>$row2[vouchno]</td>";
                    echo " <td class = 'td'  align='left' color=''>$name[name]</td>";
                    echo " <td class = 'td'  align='right' color=''>$row2[amount]</td>";
                    echo "</tr>";

                    $totrecd = $totrecd + $row2['recdamt'];
                    $totdisc = $totdisc + $row2['discount'];
                    $totclaim = $totclaim + $row2['claim'];
                    $totamt = $totamt + $row2['amount'];
                }
                ?>
                <tr>
                    <td class = 'td'  colspan="1">
                        <?php
                        echo " <td class = 'td'  align='right' color=''><b>Total</b></td>";
                        echo " <td class = 'td'  align='right' color='' style='background-color:rgba(183, 183, 183, 0.5'><b>" . number_format($totamt, 2) . "</b></td>";
                        ?>
                    </td>
                </tr>
                <tr>
                    <td class = 'td'  colspan="9">
                    </td>
                </tr>
                <tr>
                    <td class = 'td' colspan='3' style="text-align:center;background-color: #90dbf4" scope="col" width="10"><font size="3px" color=''><b>Bank Receipt</b></font></td>
                </tr>
                <tr>
                    <td class = 'td'  align="left"  color=''><b>Voucher</b></td>
                    <td class = 'td'  align="left"  color=''><b>A/c Name.</b></td>
                    <td class = 'td'  align="right" color=''><b>Amount</b></td>
                </tr>
                <?php
                /*                 * for bankrecp* */
                $totrecd = 0;
                $totdisc = 0;
                $totclaim = 0;
                $totamt = 0;

                while ($row3 = mysqli_fetch_array($brecp2)) {

                    $getname = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='$row3[party]'") or die(mysqli_error($conn));
                    $name = mysqli_fetch_array($getname);
                    echo "<tr>";
                    echo " <td class = 'td'  align='left' color=''>$row3[vouchno]</td>";
                    echo " <td class = 'td'  align='left' color=''>$name[name]</td>";
                    echo " <td class = 'td'  align='right' color=''>$row3[amount]</td>";
                    echo "</tr>";

                    $totrecd = $totrecd + $row3['recdamt'];
                    $totdisc = $totdisc + $row3['discount'];
                    $totclaim = $totclaim + $row3['claim'];
                    $totamt = $totamt + $row3['amount'];
                }
                ?>
                <tr>
                    <td class = 'td'  colspan="1">
                        <?php
                        echo " <td class = 'td'  align='right' color=''><b>Total</b></td>";
                        echo " <td class = 'td'  align='right' color='' style='background-color:rgba(183, 183, 183, 0.5'><b>" . number_format($totamt, 2) . "</b></td>";
                        ?>
                    </td>
                </tr>
                <tr>
                    <td class = 'td'  colspan="9">
                    </td>
                </tr>
                <tr>
                    <td class = 'td' colspan='3' style="text-align:center;background-color: #98f5e1" scope="col" width="10"><font size="3px" color=''><b>Bank Payment</b></font></td>
                </tr>
                <tr>
                    <td class = 'td'  align="left" color=''><b>Voucher</b></td>
                    <td class = 'td'  align="left" color=''><b>A/c Name.</b></td>
                    <td class = 'td'  align="right" color=''><b>Amount</b></td>
                </tr>
                <?php
                /** for bankpay* */
//                    $bpaid2 = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `bankpay` WHERE
//                                                 vouchdt <= '$date' group by vouchdt") or die(mysqli_error($conn));
                $totrecd = 0;
                $totdisc = 0;
                $totclaim = 0;
                $totamt = 0;
                while ($row4 = mysqli_fetch_array($bpaid2)) {

                    $getname = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='$row4[party]'") or die(mysqli_error($conn));
                    $name = mysqli_fetch_array($getname);
                    echo "<tr>";
                    echo " <td class = 'td'  align='left' color=''>$row4[vouchno]</td>";
                    echo " <td class = 'td'  align='left' color=''>$name[name]</td>";
                    echo " <td class = 'td'  align='right' color=''>$row4[amount]</td>";
                    echo "</tr>";

                    $totrecd = $totrecd + $row4['recdamt'];
                    $totdisc = $totdisc + $row4['discount'];
                    $totclaim = $totclaim + $row4['claim'];
                    $totamt = $totamt + $row4['amount'];
                }
                ?>
                <tr>
                    <td class = 'td'  colspan="1">
                        <?php
                        echo " <td class = 'td'  align='right' color=''><b>Total</b></td>";
                        echo " <td class = 'td'  align='right' color='' style='background-color:rgba(183, 183, 183, 0.5'><b>" . number_format($totamt, 2) . "</b></td>";
                        ?>
                    </td>
                </tr>
                <tr>
                    <td class = 'td'  colspan="9">
                    </td>
                </tr>
                <tr>
                    <td class = 'td' colspan='3' style="text-align:center;background-color: #f1c0e8" scope="col" width="10"><font size="3px" color=''><b>Sale</b></font></td>
                </tr>
                <tr>
                    <td class = 'td'  align="left" color=''><b>Voucher</b></td>
                    <td class = 'td'  align="left" color=''><b>A/c Name.</b></td>
                    <td class = 'td'  align="right" color=''><b>Amount</b></td>
                </tr>
                <?php
                /** for bankpay* */
//                    $bpaid2 = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `bankpay` WHERE
//                                                 vouchdt <= '$date' group by vouchdt") or die(mysqli_error($conn));
                $totrecd = 0;
                $totdisc = 0;
                $totclaim = 0;
                $totamt = 0;
                while ($row5 = mysqli_fetch_array($slrep)) {

                    $getname = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='$row5[party]'") or die(mysqli_error($conn));
                    $name = mysqli_fetch_array($getname);
                    echo "<tr>";
                    echo " <td class = 'td'  align='left' color=''>$row5[vouchno]</td>";
                    echo " <td class = 'td'  align='left' color=''>$name[name]</td>";
                    echo " <td class = 'td'  align='right' color=''>$row5[netamt]</td>";
                    echo "</tr>";

                    $totrecd = $totrecd + $row5['recdamt'];
                    $totdisc = $totdisc + $row5['discount'];
                    $totclaim = $totclaim + $row5['claim'];
                    $totamt = $totamt + $row5['netamt'];
                }
                ?>
                <tr>
                    <td class = 'td'  colspan="1">
                        <?php
                        echo " <td class = 'td'  align='right' color=''><b>Total</b></td>";
                        echo " <td class = 'td'  align='right' color='' style='background-color:rgba(183, 183, 183, 0.5'><b>" . number_format($totamt, 2) . "</b></td>";
                        ?>
                    </td>
                </tr>
                <tr>
                    <td class = 'td'  colspan="9">
                    </td>
                </tr>
                <tr>
                    <td class = 'td' colspan='3' style="text-align:center;background-color: #ffcfd2" scope="col" width="10"><font size="3px" color=''><b>Sale Return</b></font></td>
                </tr>
                <tr>
                    <td class = 'td'  align="left" color=''><b>Voucher</b></td>
                    <td class = 'td'  align="left" color=''><b>A/c Name.</b></td>
                    <td class = 'td'  align="right" color=''><b>Amount</b></td>
                </tr>
                <?php
                /** for bankpay* */
//                    $bpaid2 = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `bankpay` WHERE
//                                                 vouchdt <= '$date' group by vouchdt") or die(mysqli_error($conn));
                $totrecd = 0;
                $totdisc = 0;
                $totclaim = 0;
                $totamt = 0;
                while ($row6 = mysqli_fetch_array($slretrep)) {

                    $getname = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='$row6[party]'") or die(mysqli_error($conn));
                    $name = mysqli_fetch_array($getname);
                    echo "<tr>";
                    echo " <td class = 'td'  align='left' color=''>$row6[vouchno]</td>";
                    echo " <td class = 'td'  align='left' color=''>$name[name]</td>";
                    echo " <td class = 'td'  align='right' color=''>$row6[netamt]</td>";
                    echo "</tr>";

                    $totrecd = $totrecd + $row6['recdamt'];
                    $totdisc = $totdisc + $row6['discount'];
                    $totclaim = $totclaim + $row6['claim'];
                    $totamt = $totamt + $row6['netamt'];
                }
                ?>
                <tr>
                    <td class = 'td'  colspan="1">
                        <?php
                        echo " <td class = 'td'  align='right' color=''><b>Total</b></td>";
                        echo " <td class = 'td'  align='right' color='' style='background-color:rgba(183, 183, 183, 0.5'><b>" . number_format($totamt, 2) . "</b></td>";
                        ?>
                    </td>
                </tr>
                <tr>
                    <td class = 'td'  colspan="9">
                    </td>
                </tr>
                <tr>
                    <td class = 'td' colspan='3' style="text-align:center;background-color: #fde4cf" scope="col" width="10"><font size="3px" color=''><b>Purchase</b></font></td>
                </tr>
                <tr>
                    <td class = 'td'  align="left" color=''><b>Voucher</b></td>
                    <td class = 'td'  align="left" color=''><b>A/c Name.</b></td>
                    <td class = 'td'  align="right" color=''><b>Amount</b></td>
                </tr>
                <?php
                /** for bankpay* */
//                    $bpaid2 = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `bankpay` WHERE
//                                                 vouchdt <= '$date' group by vouchdt") or die(mysqli_error($conn));
                $totrecd = 0;
                $totdisc = 0;
                $totclaim = 0;
                $totamt = 0;
                while ($row7 = mysqli_fetch_array($purchrep)) {

                    $getname = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='$row7[party]'") or die(mysqli_error($conn));
                    $name = mysqli_fetch_array($getname);
                    echo "<tr>";
                    echo " <td class = 'td'  align='left' color=''>$row7[vouchno]</td>";
                    echo " <td class = 'td'  align='left' color=''>$name[name]</td>";
                    echo " <td class = 'td'  align='right' color=''>$row7[netamt]</td>";
                    echo "</tr>";

                    $totrecd = $totrecd + $row7['recdamt'];
                    $totdisc = $totdisc + $row7['discount'];
                    $totclaim = $totclaim + $row7['claim'];
                    $totamt = $totamt + $row7['netamt'];
                }
                ?>
                <tr>
                    <td class = 'td'  colspan="1">
                        <?php
                        echo " <td class = 'td'  align='right' color=''><b>Total</b></td>";
                        echo " <td class = 'td'  align='right' color='' style='background-color:rgba(183, 183, 183, 0.5'><b>" . number_format($totamt, 2) . "</b></td>";
                        ?>
                    </td>
                </tr>
                <tr>
                    <td class = 'td'  colspan="9">
                    </td>
                </tr>
                <tr>
                    <td class = 'td' colspan='3' style="text-align:center;background-color: #fbf8cc" scope="col" width="10"><font size="3px" color=''><b>Purchase Return</b></font></td>
                </tr>
                <tr>
                    <td class = 'td'  align="left" color=''><b>Voucher</b></td>
                    <td class = 'td'  align="left" color=''><b>A/c Name.</b></td>
                    <td class = 'td'  align="right" color=''><b>Amount</b></td>
                </tr>
                <?php
                /** for bankpay* */
//                    $bpaid2 = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `bankpay` WHERE
//                                                 vouchdt <= '$date' group by vouchdt") or die(mysqli_error($conn));
                $totrecd = 0;
                $totdisc = 0;
                $totclaim = 0;
                $totamt = 0;
                while ($row8 = mysqli_fetch_array($purchretrep)) {

                    $getname = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='$row8[party]'") or die(mysqli_error($conn));
                    $name = mysqli_fetch_array($getname);
                    echo "<tr>";
                    echo " <td class = 'td'  align='left' color=''>$row8[vouchno]</td>";
                    echo " <td class = 'td'  align='left' color=''>$name[name]</td>";
                    echo " <td class = 'td'  align='right' color=''>$row8[netamt]</td>";
                    echo "</tr>";

                    $totrecd = $totrecd + $row8['recdamt'];
                    $totdisc = $totdisc + $row8['discount'];
                    $totclaim = $totclaim + $row8['claim'];
                    $totamt = $totamt + $row8['netamt'];
                }
                ?>
                <tr>
                    <td class = 'td'  colspan="1">
                        <?php
                        echo " <td class = 'td'  align='right' color=''><b>Total</b></td>";
                        echo " <td class = 'td'  align='right' color='' style='background-color:rgba(183, 183, 183, 0.5'><b>" . number_format($totamt, 2) . "</b></td>";
                        ?>
                    </td>
                </tr>
                <tr>
                    <td class = 'td'  colspan="9">
                    </td>
                </tr>
                <tr>
                    <td class = 'td' colspan='3' style="text-align:center;background-color: #fbf8cc" scope="col" width="10"><font size="3px" color=''><b>Customer To Karigar</b></font></td>
                </tr>
                <tr>
                    <td class = 'td'  align="left" color=''><b>Voucher</b></td>
                    <td class='td' align="left" color=''>
                        <span style="display: inline-block; width: 50%; text-align: left;"><b>Customer</b></span>
                        <span style="display: inline-block; width: 40%; text-align: left;"><b>Karigar</b></span>
                    </td>

                    <td class = 'td'  align="right" color=''><b>Amount</b></td>
                </tr>
                <?php
                /** for bankpay* */
//                    $bpaid2 = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `bankpay` WHERE
//                                                 vouchdt <= '$date' group by vouchdt") or die(mysqli_error($conn));
              
                $totamt = 0;
                while ($row9 = mysqli_fetch_array($custtokari2)) {

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

                    $getkariname = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='$row9[karigar]'") or die(mysqli_error($conn));
                    $kariname = mysqli_fetch_array($getkariname);

                    echo "<tr>";
                    echo "<td class = 'td'  align='left' color=''>$row9[vouchno]</td>";
                    echo "<td class = 'td'  align='left' color=''>";
                    echo "<span style='display: inline-block; width: 50%; text-align: left;'>$name[name]</span>";
                    echo "<span style='display: inline-block; width: 40%; text-align: left;'>$kariname[name]</span>";
                    echo "</td>";
                    echo "<td class = 'td'  align='right' color=''>$row9[amount]</td>";
                    echo "</tr>";

                    $totamt = $totamt + $row9['amount'];
                }
                ?>
                <tr>
                    <td class = 'td'  colspan="1">
                        <?php
                        echo " <td class = 'td'  align='right' color=''><b>Total</b></td>";
                        echo " <td class = 'td'  align='right' color='' style='background-color:rgba(183, 183, 183, 0.5'><b>" . number_format($totamt, 2) . "</b></td>";
                        ?>
                    </td>
                </tr>
                <tr>
                    <td class = 'td'  colspan="9">
                    </td>
                </tr>
                <?php //}      ?>
                </tbody>
                <tfoot>
                    <tr>
                        <td class = 'td'  colspan="9">
                            <hr/>
                        </td>
                    </tr>
                </tfoot>
            </table>
            <?php include("includes/linksfoot.php"); ?>
        </div>

    </body>
    </html>
    <?php
}

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

    $date = $_POST["date"];


    $getcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company` where code='$zcomp'")or die(mysqli_error($conn));
    $comp = mysqli_fetch_array($getcomp);

    $cpur1 = mysqli_query($conn, "SELECT SUM(netamt) AS amt FROM $purchfile WHERE vouchdt = '$date' AND  `cashcred` = 'C' group by vouchdt") or die(mysqli_error($conn));
    $cspur = mysqli_fetch_array($cpur1);

    $cashpur = $cspur["amt"];

    $cpur2 = mysqli_query($conn, "SELECT SUM(netamt) AS amt FROM $purchfile WHERE vouchdt = '$date' AND  `cashcred` != 'C' group by vouchdt") or die(mysqli_error($conn));
    $cspur = mysqli_fetch_array($cpur2);

    $creditpur = $cspur["amt"];
    $totpur = $cashpur + $creditpur;

///////////////////////////////////////////// New Fildes cash sale, Credit Sale /////////////////////


    $cashsale = $credale = $ccsale = $totsale = 0;

    $csale1 = mysqli_query($conn, "SELECT SUM(cashamt) AS amt FROM $salefile WHERE vouchdt = '$date' ") or die(mysqli_error($conn));
    $cssale = mysqli_fetch_array($csale1);
    $cashsale = $cssale['amt'];

    $csale2 = mysqli_query($conn, "SELECT SUM(credamt) AS amt FROM $salefile WHERE vouchdt = '$date' ") or die(mysqli_error($conn));
    $crsale = mysqli_fetch_array($csale2);
    $creditsale = $crsale["amt"];

    $csale3 = mysqli_query($conn, "SELECT SUM(ccamt) AS amt FROM $salefile WHERE vouchdt = '$date' ") or die(mysqli_error($conn));
    $ccsale = mysqli_fetch_array($csale3);
    $ccsale = $ccsale["amt"];

    $totsale = $cashsale + $creditsale + $ccsale;


/////////////////////////////////////////////
    $crecp2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $crfile WHERE
                      vouchdt = '$date'  order by vouchno ") or die(mysqli_error($conn));

    $cpaid2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $cpfile WHERE
                      vouchdt = '$date' ") or die(mysqli_error($conn));

    $brecp2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $brfile WHERE
                       vouchdt = '$date' ") or die(mysqli_error($conn));

    $bpaid2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $bpfile WHERE
                       vouchdt = '$date' ") or die(mysqli_error($conn));

//////////////

    $crecp = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $crfile WHERE
                      vouchdt = '$date' group by vouchdt") or die(mysqli_error($conn));
    $crget = mysqli_fetch_array($crecp);

    $cramt = $crget["amt"];

    $cpaid = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `cashpay` WHERE
                      vouchdt = '$date' group by vouchdt") or die(mysqli_error($conn));
    $cpget = mysqli_fetch_array($cpaid);

    $cpamt = $cpget["amt"];

    $brecp = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `bankrecp` WHERE
                      vouchdt = '$date' group by vouchdt") or die(mysqli_error($conn));
    $brget = mysqli_fetch_array($brecp);

    $bramt = $brget["amt"];

    $bpaid = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `bankpay` WHERE
                      vouchdt = '$date' group by vouchdt") or die(mysqli_error($conn));
    $bpget = mysqli_fetch_array($bpaid);

    $bpamt = $bpget["amt"];


    $getcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company` where code='$zcomp'")or die(mysqli_error($conn));
    $comp = mysqli_fetch_array($getcomp);

    $cname = $comp["name"];

    $contents .= $cname . "\n";

    $contents .= "Cash Sale" . ",";
    $contents .= "$cashsale" . ",";
    $contents .= " " . ",";
    $contents .= "Cash Purchase" . ",";
    $contents .= $cashpur . "\n";

    $contents .= "Credit Sale" . ",";
    $contents .= $creditsale . ",";
    $contents .= " " . ",";
    $contents .= "Credit Purchase" . ",";
    $contents .= $creditpur . "\n";

    $contents .= "CC Sale" . ",";
    $contents .= $ccsale . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . "\n";

    $contents .= "Total Sale" . ",";
    $contents .= $totsale . ",";
    $contents .= " " . ",";
    $contents .= "Total Purchase" . ",";
    $contents .= $totpur . "\n";


    $contents .= " " . ",";
    $contents .= "Cash Receipt" . ",";
    $contents .= " " . "\n";

    $contents .= "Voucher" . ",";
    $contents .= "A/c Name" . ",";
    $contents .= "Amount" . "\n";


    $totrecd = 0;
    $totdisc = 0;
    $totclaim = 0;
    $totamt = 0;

    mysqli_select_db($conn, $zdb);
    while ($row = mysqli_fetch_array($crecp2)) {
        $getname = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE accode='$row[bankname]'") or die(mysqli_error($conn));
        $name = mysqli_fetch_array($getname);

        $rvouchno = $row['vouchno'];
        $nname = $name['name'];
        $ramount = $row['amount'];

        $contents .= $rvouchno . ",";
        $contents .= $nname . ",";
        $contents .= $ramount . "\n";


        $totrecd = $totrecd + $row['recdamt'];
        $totdisc = $totdisc + $row['discount'];
        $totclaim = $totclaim + $row['claim'];
        $totamt = $totamt + $row['amount'];
    }

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

    $contents .= " " . ",";
    $contents .= "Cash Payment" . ",";
    $contents .= " " . "\n";

    $contents .= "Voucher" . ",";
    $contents .= "A/c. Name." . ",";
    $contents .= "Amount" . "\n";

    $totrecd = 0;
    $totdisc = 0;
    $totclaim = 0;
    $totamt = 0;
    while ($row2 = mysqli_fetch_array($cpaid2)) {

        $getname = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='$row2[bankname]'") or die(mysqli_error($conn));
        $name = mysqli_fetch_array($getname);

        $r2vochno = $row2['vouchno'];
        $n2name = $name['name'];
        $r2amount = $row2['amount'];

        $contents .= $r2vochno . ",";
        $contents .= $n2name . ",";
        $contents .= $r2amount . "\n";

        $totrecd = $totrecd + $row2['recdamt'];
        $totdisc = $totdisc + $row2['discount'];
        $totclaim = $totclaim + $row2['claim'];
        $totamt = $totamt + $row2['amount'];
    }

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

    $contents .= " " . ",";
    $contents .= "Bank Receipt" . ",";
    $contents .= " " . "\n";

    $contents .= "Voucher" . ",";
    $contents .= "A/c Name." . ",";
    $contents .= "Amount" . "\n";

    $totrecd = 0;
    $totdisc = 0;
    $totclaim = 0;
    $totamt = 0;

    while ($row3 = mysqli_fetch_array($brecp2)) {

        $getname = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='$row3[bankname]'") or die(mysqli_error($conn));
        $name = mysqli_fetch_array($getname);

        $r3vouchno = $row3['vouchno'];
        $nname = $name['name'];
        $r3amount = $row3['amount'];

        $contents .= $r3vouchno . ",";
        $contents .= $nname . ",";
        $contents .= $r3amount . "\n";


        $totrecd = $totrecd + $row3['recdamt'];
        $totdisc = $totdisc + $row3['discount'];
        $totclaim = $totclaim + $row3['claim'];
        $totamt = $totamt + $row3['amount'];
    }


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

    $contents .= " " . ",";
    $contents .= "Bank Payment" . ",";
    $contents .= " " . "\n";

    $contents .= "Voucher" . ",";
    $contents .= "A/c Name" . ",";
    $contents .= "Amount" . "\n";


    $totrecd = 0;
    $totdisc = 0;
    $totclaim = 0;
    $totamt = 0;
    while ($row4 = mysqli_fetch_array($bpaid2)) {

        $getname = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='$row4[bankname]'") or die(mysqli_error($conn));
        $name = mysqli_fetch_array($getname);

        $rvouchno = $row4['vouchno'];
        $nname = $name['name'];
        $ramount = $row4['amount'];

        $contents .= $rvouchno . ",";
        $contents .= $nname . ",";
        $contents .= $ramount . "\n";

        $totrecd = $totrecd + $row4['recdamt'];
        $totdisc = $totdisc + $row4['discount'];
        $totclaim = $totclaim + $row4['claim'];
        $totamt = $totamt + $row4['amount'];
    }

    $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;
    }
}
?>