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

$formname = "Daily Cash Report";
$file = "dailyrepcash_fr.php";

$salefile = "salemain";
$purchfile = "purmain";
$crfile = "cashrecp";
$cpfile = "cashpay";
$brfile = "bankrecp";
$bpfile = "bankpay";
$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"];
    $reptype = $_POST["reptype"];


    $getcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company` where code='$zcomp'")or die(mysqli_error($conn));
    $comp = mysqli_fetch_array($getcomp);
//    browse($date);
//////////////////////////////////////////
//    $csale1 = mysqli_query($conn, "SELECT SUM(netamt) AS amt FROM $salefile WHERE vouchdt = '$date' AND `paidby` = '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  `paidby` != 'C' group by vouchdt") or die(mysqli_error($conn));
//    $crsale = mysqli_fetch_array($csale2);
//
//    $creditsale = $crsale["amt"];
//    
//    $totsale = $cashsale + $creditsale;


    $acc = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='3'") or die(mysqli_error($conn));
    $getac = mysqli_fetch_array($acc);
    $mopbal = $getac["openbal"];
    $mopdc = $getac["dc"];

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

    $getbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `actran` WHERE  accode='3' and
                                                           `date` < '$date' and `retdate` = '0000-00-00' ") or die(mysqli_error($conn));

    $getamt = mysqli_fetch_array($getbal);
    $amt = $getamt['amt'];
//browse($mopbal);
//browse($amt);
    $mobal = $mopbal + $amt;

    $copen = abs($mobal);
    if ($mobal < 0) {
        $mdc = "DR";
    } else {
        $mdc = "CR";
    }


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

    $cashpur = $cspur["amt"];

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

    $creditpur = $cspur["amt"];
    $totpur = $cashpur;
//    $totpur = $cashpur + $creditpur;
///////////////////////////////////////////// New cash sale, Credit Sale /////////////////////


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

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



    $csale2 = mysqli_query($conn, "SELECT SUM(billamt) AS amt FROM $salefile WHERE vouchdt = '$date' AND  `paidby` != 'C'") 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;

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


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

//    $csale1 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $salefile WHERE
//                      vouchdt = '$date' and cashamt > 0 and prodtype = 'O' order by vouchno ") or die(mysqli_error($conn));

    $crecp2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $crfile WHERE
                      vouchdt = '$date' and `trnflag`!='S'  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'  and `trnflag`!='S' ") or die(mysqli_error($conn));

    $brecp2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $brfile WHERE
                       vouchdt = '$date'  and `trnflag`!='S' ") 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' and `trnflag`!='S' 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' and `trnflag`!='S' 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' and `trnflag`!='S' 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"];

    $totadd = $totsale + $cramt;
    $totless = $cashpur + $cpamt;

    $closeb = $mobal - $totadd + $totless;
    $closebal = abs($closeb);

    if ($closeb < 0) {
        $cdc = "DR";
    } else {
        $cdc = "CR";
    }

    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:70%;border: 1px solid black" class="table table-stripped table-bordered" summary="Ledger - Detail">
                <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
//                        if ($mobal < 0) {
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\"><b>$copen  $mdc</b></td>";
//                        } else {
//                            echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$ctot    $mdc</td>";
//                        }
                        ?>
                    </tr>

                    <tr style ="height:25px;"></tr>
                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;" scope="col" width="25"><font size="3px" color=''><b>Cash Sale</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$cashsale</td>";
                        ?>
                    </tr>
<!--                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;" scope="col" width="25"><font size="3px" color=''><b>Cash Sale (Others)</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$cashsale1</td>";
                        ?>
                    </tr>-->
                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Cash Received</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$cramt</td>";
                        ?>
                    </tr>
                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color='blue'><b>Total In</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;color:blue\" scope=\"col\" width=\"90\">" . number_format($totadd, 2) . "</td>";
                        ?>
                    </tr>

                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;" 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;" scope="col" width="20"><font size="3px" color=''><b>Cash Paid</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;\" scope=\"col\" width=\"90\">$cpamt</td>";
                        ?>
                    </tr>
                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color='red'><b>Total Out</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;color:red\" scope=\"col\" width=\"90\">" . number_format($totless, 2) . "</td>";
                        ?>
                    </tr>
                    <tr style ="height:30px;"></tr>
                    <tr style ="height:10px;">
                        <td class = 'td'   style="text-align:left;" scope="col" width="20"><font size="3px" color='green'><b>Closing Balance</b></font></td>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;color:green\" scope=\"col\" width=\"90\"><b>" . number_format($closebal, 2) . '   ' . $cdc . "</b></td>";
                        ?>
                    </tr>
            </table>  
            <!--<table class="table table-bordered" summary="Ledger - Detail" width='100%' border="0">-->
            <?php
            if ($reptype == 2) {
                ?>
                <table style="width:70%;" class="table-bordered table table-stripped table-hover" summary="Ledger - Detail">    
                    <?php // if ($prntype == 2) {       ?>
                    <tr style="height:10px;">
                        <td class = 'td'  style="text-align:left;" scope="col" width="10%"><font size="3px" color=''><b></b></font></td>
                        <td class = 'td'  style="text-align:left;" scope="col" width="40%"><font size="3px" color=''><b>Cash Sale</b></font></td>
                        <td class = 'td'  style="text-align:left;" scope="col" width="12%"><font size="3px" color=''><b></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($csale)) {
//                    browse('1');
                        $camt = $row[billamt];
                        $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=''>$camt</td>";
                        echo "</tr>";

                        $totrecd = $totrecd + $row[recdamt];
                        $totdisc = $totdisc + $row[discount];
                        $totclaim = $totclaim + $row[claim];
                        $totamt = $totamt + $camt;
                    }
                    ?>
                    <tr>
                        <td class = 'td'  colspan="1">
                            <?php
                            echo " <td class = 'td'  align='right' color=''>Total</td>";
                            echo " <td class = 'td'  align='right' color=''>" . number_format($totamt, 2) . "</td>";
                            ?>
                        </td>
                    </tr>

                    <tr style="height:10px;">
                        <td class = 'td'  style="text-align:left;" scope="col" width="10%"><font size="3px" color=''><b></b></font></td>
                        <td class = 'td'  style="text-align:left;" scope="col" width="40%"><font size="3px" color=''><b>Cash Received</b></font></td>
                        <td class = 'td'  style="text-align:left;" scope="col" width="12%"><font size="3px" color=''><b></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=''>Total</td>";
                            echo " <td class = 'td'  align='right' color=''>" . number_format($totamt, 2) . "</td>";
                            ?>
                        </td>
                    </tr>
                    <tr>
                        <td class = 'td'  colspan="9">
                        </td>
                    </tr>
                    <tr>
                        <td class = 'td'  style="text-align:left;" scope="col" width="10"><font size="3px" color=''><b></b></font></td>
                        <td class = 'td'  style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Cash Paid</b></font></td>
                        <td class = 'td'  style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b></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=''>Total</td>";
                            echo " <td class = 'td'  align='right' color=''>" . number_format($totamt, 2) . "</td>";
                            ?>
                        </td>
                    </tr>
                    <tr>
                        <td class = 'td'  colspan="9">
                        </td>
                    </tr>
                    <br><br>

                    <?php
                    $cashadd = $cashsale + $cashsale1 + $cramt;
                    $cashout = $cpamt + $cashpur;
                    ?>
                    <tr>
                        <td class = 'td'  style="text-align:left;" scope="col" width="10"><font size="3px" color=''><b></b></font></td>
                        <td class = 'td'  style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Cash Total(Inward)</b></font></td>
                        <td class = 'td'  style="text-align:right;" scope="col" width="20"><font size="3px" color=''><b><?= $cashadd ?></b></font></td>
                    </tr>
                    <tr>
                        <td class = 'td'  style="text-align:left;" scope="col" width="10"><font size="3px" color=''><b></b></font></td>
                        <td class = 'td'  style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Cash Total(Outward)</b></font></td>
                        <td class = 'td'  style="text-align:right;" scope="col" width="20"><font size="3px" color=''><b><?= $cashout ?></b></font></td>
                    </tr>
                    <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='green'><b>Closing Balance</b></font></td>
                    <?php
                    if ($mobal < 0) {
                        $mopbal = $dtot;
                    } else {
                        $mopbal = $ctot;
                    }
//                    $closebal = $mobal + $cashadd - $cashout;
                    echo " <td class = 'td'   style=\"text-align:right;color:green\" scope=\"col\" width=\"90\"><b>$closebal   $cdc</b></td>";
                    ?>
                    <br><br>
        <!--                <tr>
                        <td class = 'td'  style="text-align:left;" scope="col" width="10"><font size="3px" color=''><b></b></font></td>
                        <td class = 'td'  style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Bank Receipt</b></font></td>
                        <td class = 'td'  style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b></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=''>Total</td>";
//                        echo " <td class = 'td'  align='right' color=''>" . number_format($totamt, 2) . "</td>";
                    ?>
                        </td>
                    </tr>
                    <tr>
                        <td class = 'td'  colspan="9">
                        </td>
                    </tr>
                    <tr>
                        <td class = 'td'  style="text-align:left;" scope="col" width="10"><font size="3px" color=''><b></b></font></td>
                        <td class = 'td'  style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b>Bank Payment</b></font></td>
                        <td class = 'td'  style="text-align:left;" scope="col" width="20"><font size="3px" color=''><b></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=''>Total</td>";
//                        echo " <td class = 'td'  align='right' color=''>" . number_format($totamt, 2) . "</td>";
                    ?>
                        </td>
                    </tr>
                    <tr>
                        <td class = 'td'  colspan="9">
                        </td>
                    </tr>-->
                    <?php //}        ?>
                    </tbody>
                    <tfoot>
                        <tr>
                            <td class = 'td'  colspan="9">
                                <hr style=""/>
                            </td>
                        </tr>
                    </tfoot>
                </table>

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

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

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

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


    $getcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company` where code='$zcomp'")or die(mysqli_error($conn));
    $comp = mysqli_fetch_array($getcomp);
//    browse($date);
//////////////////////////////////////////
//    $csale1 = mysqli_query($conn, "SELECT SUM(netamt) AS amt FROM $salefile WHERE vouchdt = '$date' AND `paidby` = '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  `paidby` != 'C' group by vouchdt") or die(mysqli_error($conn));
//    $crsale = mysqli_fetch_array($csale2);
//
//    $creditsale = $crsale["amt"];
//    
//    $totsale = $cashsale + $creditsale;


    $acc = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='3'") or die(mysqli_error($conn));
    $getac = mysqli_fetch_array($acc);
    $mopbal = $getac["openbal"];
    $mopdc = $getac["dc"];

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

    $getbal = mysqli_query($conn, "SELECT SUM(actamt) as amt FROM `actran` 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;

    $copen = abs($mobal);
    if ($mobal < 0) {
        $mdc = "DR";
    } else {
        $mdc = "CR";
    }


    $cpur1 = mysqli_query($conn, "SELECT SUM(netamt) AS amt FROM $purchfile WHERE vouchdt = '$date' AND  `paidby` = '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  `paidby` != 'C' group by vouchdt") or die(mysqli_error($conn));
    $cspur = mysqli_fetch_array($cpur2);

    $creditpur = $cspur["amt"];
    $totpur = $cashpur;
//    $totpur = $cashpur + $creditpur;
///////////////////////////////////////////// New cash sale, Credit Sale /////////////////////


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

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

    $csale2 = mysqli_query($conn, "SELECT *,SUM(cashamt) AS amt FROM $salefile WHERE vouchdt = '$date' and prodtype = 'O'") or die(mysqli_error($conn));
    $cssale = mysqli_fetch_array($csale2);
    $cashsale1 = $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 + $cashsale1;

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


    $csale = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $salefile WHERE
                      vouchdt = '$date' and cashamt > 0 and prodtype = 'P' order by vouchno ") or die(mysqli_error($conn));

    $csale1 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $salefile WHERE
                      vouchdt = '$date' and cashamt > 0 and prodtype = 'O' order by vouchno ") or die(mysqli_error($conn));

    $crecp2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $crfile WHERE
                      vouchdt = '$date' and `trnflag`!='S'  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'  and `trnflag`!='S' ") or die(mysqli_error($conn));

    $brecp2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $brfile WHERE
                       vouchdt = '$date'  and `trnflag`!='S' ") 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' and `trnflag`!='S' 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' and `trnflag`!='S' 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' and `trnflag`!='S' 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"];

    $totadd = $totsale + $cramt;
    $totless = $cashpur + $cpamt;

    $closeb = $mobal - $totadd + $totless;
    $closebal = abs($closeb);

    if ($closeb < 0) {
        $cdc = "DR";
    } else {
        $cdc = "CR";
    }

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

    $contents .= $comp['name'] . "\n";

    $contents .= $formname . "-" . date("d-m-Y", strtotime($date)) . "\n";

    $contents .= "Opening Balance" . ",";
    $contents .= $copen . " " . $mdc . "\n";

    $contents .= "Cash Sale(Perfumes)" . ",";
    $contents .= $cashsale . "\n";

    $contents .= "Cash Sale(Others)" . ",";
    $contents .= $cashsale1 . "\n";

    $contents .= "Cash Received" . ",";
    $contents .= $cramt . "\n";

    $contents .= "Total In" . ",";
    $contents .= number_format($totadd, 2, ".", " ") . "\n";

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

    $contents .= "Cash Paid" . ",";
    $contents .= $cpamt . "\n";

    $contents .= "Total Out" . ",";
    $contents .= number_format($totless, 2, ".", " ") . "\n";

    $contents .= "Closing Balance" . ",";
    $contents .= number_format($closebal, 2, ".", " ") . "\n";

    if ($reptype == 2) {
        $contents .= " " . ",";
        $contents .= "Cash Sale (Perfumes)" . ",";
        $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($csale)) {
            $camt = $row[cashamt] * 10;
            $getname = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE accode='$row[party]'") or die(mysqli_error($conn));
            $name = mysqli_fetch_array($getname);

            $contents .= $row[vouchno] . ",";
            $contents .= $name[name] . ",";
            $contents .= $camt . "\n";


            $totrecd = $totrecd + $row[recdamt];
            $totdisc = $totdisc + $row[discount];
            $totclaim = $totclaim + $row[claim];
            $totamt = $totamt + $camt;
        }
        $contents .= " " . ",";
        $contents .= "Total" . ",";
        $contents .= number_format($totamt, 2, ".", " ") . "\n";

        $contents .= " " . ",";
        $contents .= "Cash Sale (Others) " . ",";
        $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($csale1)) {

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

            $contents .= $row[vouchno] . ",";
            $contents .= $name[name] . ",";
            $contents .= $row[cashamt] . "\n";


            $totrecd = $totrecd + $row[recdamt];
            $totdisc = $totdisc + $row[discount];
            $totclaim = $totclaim + $row[claim];
            $totamt = $totamt + $row[cashamt];
        }
        $contents .= " " . ",";
        $contents .= "Total" . ",";
        $contents .= number_format($totamt, 2, ".", " ") . "\n";

        $contents .= " " . ",";
        $contents .= "Cash Received" . ",";
        $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[party]'") or die(mysqli_error($conn));
            $name = mysqli_fetch_array($getname);

            $contents .= $row[vouchno] . ",";
            $contents .= $name[name] . ",";
            $contents .= $row[amount] . "\n";


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

        $contents .= " " . ",";
        $contents .= "Total" . ",";
        $contents .= number_format($totamt, 2, ".", " ") . "\n";

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

        $contents .= " " . ",";
        $contents .= "Cash Paid" . ",";
        $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[party]'") or die(mysqli_error($conn));
            $name = mysqli_fetch_array($getname);

            $contents .= $row2[vouchno] . ",";
            $contents .= $name[name] . ",";
            $contents .= $row2[amount] . "\n";


            $totrecd = $totrecd + $row2[recdamt];
            $totdisc = $totdisc + $row2[discount];
            $totclaim = $totclaim + $row2[claim];
            $totamt = $totamt + $row2[amount];
        }
        $contents .= " " . ",";
        $contents .= "Total" . ",";
        $contents .= number_format($totamt, 2, ".", " ") . "\n";

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

        $cashadd = $cashsale + $cashsale1 + $cramt;
        $cashout = $cpamt + $cashpur;

        $contents .= " " . ",";
        $contents .= "Cash Total (Inward)" . ",";
        $contents .= $cashadd . "\n";

        $contents .= " " . ",";
        $contents .= "Cash Total (Outward)" . ",";
        $contents .= $cashout . "\n";

        $contents .= " " . ",";
        $contents .= "Closing Balance" . ",";

        if ($mobal < 0) {
            $mopbal = $dtot;
        } else {
            $mopbal = $ctot;
        }
        $contents .= $closebal . " " . $cdc . "\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;
    }
}
if (isset($_POST["subpdf"])) {

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

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

    $acc = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='3'") or die(mysqli_error($conn));
    $getac = mysqli_fetch_array($acc);
    $mopbal = $getac["openbal"];
    $mopdc = $getac["dc"];

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

    $getbal = mysqli_query($conn, "SELECT SUM(actamt) as amt FROM `actran` 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;

    $copen = abs($mobal);
    if ($mobal < 0) {
        $mdc = "DR";
    } else {
        $mdc = "CR";
    }

    $cpur1 = mysqli_query($conn, "SELECT SUM(netamt) AS amt FROM $purchfile WHERE vouchdt = '$date' AND  `paidby` = '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  `paidby` != 'C' group by vouchdt") or die(mysqli_error($conn));
    $cspur = mysqli_fetch_array($cpur2);

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

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

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

    $csale2 = mysqli_query($conn, "SELECT *,SUM(cashamt) AS amt FROM $salefile WHERE vouchdt = '$date' and prodtype = 'O'") or die(mysqli_error($conn));
    $cssale = mysqli_fetch_array($csale2);
    $cashsale1 = $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 + $cashsale1;

    $csale = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $salefile WHERE
                      vouchdt = '$date' and cashamt > 0 and prodtype = 'P' order by vouchno ") or die(mysqli_error($conn));

    $csale1 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $salefile WHERE
                      vouchdt = '$date' and cashamt > 0 and prodtype = 'O' order by vouchno ") or die(mysqli_error($conn));

    $crecp2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $crfile WHERE
                      vouchdt = '$date' and `trnflag`!='S'  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'  and `trnflag`!='S' ") or die(mysqli_error($conn));

    $brecp2 = mysqli_query($conn, "SELECT *,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $brfile WHERE
                       vouchdt = '$date'  and `trnflag`!='S' ") 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' and `trnflag`!='S' 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' and `trnflag`!='S' 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' and `trnflag`!='S' 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"];

    $totadd = $totsale + $cramt;
    $totless = $cashpur + $cpamt;

    $closeb = $mobal - $totadd + $totless;
    $closebal = abs($closeb);

    if ($closeb < 0) {
        $cdc = "DR";
    } else {
        $cdc = "CR";
    }
    require("library/fpdf/fpdf.php");

    class pdf extends fpdf {

        function Footer() {



//Position at 1.5 cm from bottom

            $this->SetY(-15);

//Arial italic 8

            $this->SetFont('Arial', 'I', 8);

//Page number

            $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
        }

    }

    $pdf = new pdf('P', 'mm', 'A4');

    $pdf->AliasNbPages();

    $pdf->AddPage();
    $getcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company` where code='$zcomp'")or die(mysqli_error($conn));
    $comp = mysqli_fetch_array($getcomp);
    $pdf->SetFont('Arial', 'I', 10);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(96, 130, 191);
    $pdf->Cell(193, 7, "$comp[name]", 1, 0, '', 1);
    $pdf->Ln();
    $pdf->Cell(193, 7, "$formname" . date("d-m-Y", strtotime($date)), 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    $pdf->SetFont('Arial', 'I', 10);
    $pdf->SetTextColor(96, 130, 191);
    $pdf->SetFillColor(255, 255, 255);
    $pdf->Cell(97, 7, "Opening Balance", 1, 0, '', 1);
    $pdf->Cell(96, 7, "$copen" . $mdc, 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    $pdf->SetFont('Arial', 'I', 10);
    $pdf->SetTextColor(96, 130, 191);
    $pdf->SetFillColor(255, 255, 255);
    $pdf->Cell(97, 7, "Cash Sale (Perfumes)", 1, 0, '', 1);
    $pdf->Cell(96, 7, "$cashsale", 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    $pdf->SetFont('Arial', 'I', 10);
    $pdf->SetTextColor(96, 130, 191);
    $pdf->SetFillColor(255, 255, 255);
    $pdf->Cell(97, 7, "Cash Sale (Others)", 1, 0, '', 1);
    $pdf->Cell(96, 7, "$cashsale1", 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    $pdf->SetFont('Arial', 'I', 10);
    $pdf->SetTextColor(96, 130, 191);
    $pdf->SetFillColor(255, 255, 255);
    $pdf->Cell(97, 7, "Cash Received", 1, 0, '', 1);
    $pdf->Cell(96, 7, "$cramt", 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    $pdf->SetFont('Arial', 'I', 10);
    $pdf->SetTextColor(2, 94, 242);
    $pdf->SetFillColor(255, 255, 255);
    $pdf->Cell(97, 7, "Total In", 1, 0, '', 1);
    $pdf->Cell(96, 7, number_format($totadd, 2), 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    $pdf->SetFont('Arial', 'I', 10);
    $pdf->SetTextColor(96, 130, 191);
    $pdf->SetFillColor(255, 255, 255);
    $pdf->Cell(97, 7, "Cash Purchase", 1, 0, '', 1);
    $pdf->Cell(96, 7, "$cashpur", 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    $pdf->SetFont('Arial', 'I', 10);
    $pdf->SetTextColor(96, 130, 191);
    $pdf->SetFillColor(255, 255, 255);
    $pdf->Cell(97, 7, "Cash Paid", 1, 0, '', 1);
    $pdf->Cell(96, 7, "$cpamt", 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    $pdf->SetFont('Arial', 'I', 10);
    $pdf->SetTextColor(239, 2, 10);
    $pdf->SetFillColor(255, 255, 255);
    $pdf->Cell(97, 7, "Total Out", 1, 0, '', 1);
    $pdf->Cell(96, 7, number_format($totless, 2), 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    $pdf->SetFont('Arial', 'I', 10);
    $pdf->SetTextColor(2, 237, 45);
    $pdf->SetFillColor(255, 255, 255);
    $pdf->Cell(97, 7, "Closing Balance", 1, 0, '', 1);
    $pdf->Cell(96, 7, number_format($closebal, 2), 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    if ($reptype == 2) {
        $pdf->SetFont('Arial', 'I', 10);
        $pdf->SetTextColor(96, 130, 191);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->Cell(64, 7, " ", 1, 0, '', 1);
        $pdf->Cell(65, 7, "Cash Sale (Perfumes)", 1, 0, '', 1);
        $pdf->Cell(64, 7, " ", 1, 0, '', 1);
        $pdf->SetFont('Arial', '', 1, 0);

        $pdf->SetTextColor(0, 0, 0);
        $pdf->Ln();

        $pdf->SetFont('Arial', 'I', 10);
        $pdf->SetTextColor(96, 130, 191);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->Cell(64, 7, "Voucher", 1, 0, '', 1);
        $pdf->Cell(65, 7, "A/c. Name", 1, 0, '', 1);
        $pdf->Cell(64, 7, "Amount", 1, 0, '', 1);
        $pdf->SetFont('Arial', '', 1, 0);

        $pdf->SetTextColor(0, 0, 0);
        $pdf->Ln();

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

        mysqli_select_db($conn, $zdb);
        while ($row = mysqli_fetch_array($csale)) {
            $camt = $row[cashamt] * 10;
            $getname = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE accode='$row[party]'") or die(mysqli_error($conn));
            $name = mysqli_fetch_array($getname);
            $pdf->SetFont('Arial', '', 8);
            $pdf->Cell(64, 7, "$row[vouchno]", 1);
            $pdf->Cell(65, 7, "$name[name]", 1);
            $pdf->Cell(64, 7, "$camt", 1);
            $pdf->Ln();

            $totrecd = $totrecd + $row[recdamt];
            $totdisc = $totdisc + $row[discount];
            $totclaim = $totclaim + $row[claim];
            $totamt = $totamt + $camt;
        }
        $pdf->SetFont('Arial', 'I', 10);
        $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFillColor(239, 2, 10);
        $pdf->Cell(64, 7, " ", 1, 0, '', 1);
        $pdf->Cell(65, 7, "Total", 1, 0, '', 1);
        $pdf->Cell(64, 7, number_format($totamt, 2), 1, 0, '', 1);
        $pdf->SetFont('Arial', '', 1, 0);

        $pdf->SetTextColor(0, 0, 0);
        $pdf->Ln();

        $pdf->SetFont('Arial', 'I', 10);
        $pdf->SetTextColor(96, 130, 191);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->Cell(64, 7, " ", 1, 0, '', 1);
        $pdf->Cell(65, 7, "Cash Sale (Others)", 1, 0, '', 1);
        $pdf->Cell(64, 7, " ", 1, 0, '', 1);
        $pdf->SetFont('Arial', '', 1, 0);

        $pdf->SetTextColor(0, 0, 0);
        $pdf->Ln();

        $pdf->SetFont('Arial', 'I', 10);
        $pdf->SetTextColor(96, 130, 191);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->Cell(64, 7, "Voucher", 1, 0, '', 1);
        $pdf->Cell(65, 7, "A/c. Name", 1, 0, '', 1);
        $pdf->Cell(64, 7, "Amount", 1, 0, '', 1);
        $pdf->SetFont('Arial', '', 1, 0);

        $pdf->SetTextColor(0, 0, 0);
        $pdf->Ln();

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

        mysqli_select_db($conn, $zdb);
        while ($row = mysqli_fetch_array($csale1)) {
            $getname = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE accode='$row[party]'") or die(mysqli_error($conn));
            $name = mysqli_fetch_array($getname);
            $pdf->SetFont('Arial', '', 8);
            $pdf->Cell(64, 7, "$row[vouchno]", 1);
            $pdf->Cell(65, 7, "$name[name]", 1);
            $pdf->Cell(64, 7, "$row[cashamt]", 1);
            $pdf->Ln();

            $totrecd = $totrecd + $row[recdamt];
            $totdisc = $totdisc + $row[discount];
            $totclaim = $totclaim + $row[claim];
            $totamt = $totamt + $row[cashamt];
        }
        $pdf->SetFont('Arial', 'I', 10);
        $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFillColor(239, 2, 10);
        $pdf->Cell(64, 7, " ", 1, 0, '', 1);
        $pdf->Cell(65, 7, "Total", 1, 0, '', 1);
        $pdf->Cell(64, 7, number_format($totamt, 2), 1, 0, '', 1);
        $pdf->SetFont('Arial', '', 1, 0);

        $pdf->SetTextColor(0, 0, 0);
        $pdf->Ln();

        $pdf->SetFont('Arial', 'I', 10);
        $pdf->SetTextColor(96, 130, 191);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->Cell(64, 7, " ", 1, 0, '', 1);
        $pdf->Cell(65, 7, "Cash Received", 1, 0, '', 1);
        $pdf->Cell(64, 7, " ", 1, 0, '', 1);
        $pdf->SetFont('Arial', '', 1, 0);

        $pdf->SetTextColor(0, 0, 0);
        $pdf->Ln();

        $pdf->SetFont('Arial', 'I', 10);
        $pdf->SetTextColor(96, 130, 191);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->Cell(64, 7, "Voucher", 1, 0, '', 1);
        $pdf->Cell(65, 7, "A/c. Name", 1, 0, '', 1);
        $pdf->Cell(64, 7, "Amount", 1, 0, '', 1);
        $pdf->SetFont('Arial', '', 1, 0);

        $pdf->SetTextColor(0, 0, 0);
        $pdf->Ln();

        $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);
            $pdf->SetFont('Arial', '', 8);
            $pdf->Cell(64, 7, "$row[vouchno]", 1);
            $pdf->Cell(65, 7, "$name[name]", 1);
            $pdf->Cell(64, 7, "$row[amount]", 1);
            $pdf->Ln();

            $totrecd = $totrecd + $row[recdamt];
            $totdisc = $totdisc + $row[discount];
            $totclaim = $totclaim + $row[claim];
            $totamt = $totamt + $row[amount];
        }
        $pdf->SetFont('Arial', 'I', 10);
        $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFillColor(239, 2, 10);
        $pdf->Cell(64, 7, " ", 1, 0, '', 1);
        $pdf->Cell(65, 7, "Total", 1, 0, '', 1);
        $pdf->Cell(64, 7, number_format($totamt, 2), 1, 0, '', 1);
        $pdf->SetFont('Arial', '', 1, 0);

        $pdf->SetTextColor(0, 0, 0);
        $pdf->Ln();

        $pdf->SetFont('Arial', 'I', 10);
        $pdf->SetTextColor(96, 130, 191);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->Cell(64, 7, " ", 1, 0, '', 1);
        $pdf->Cell(65, 7, "Cash Paid", 1, 0, '', 1);
        $pdf->Cell(64, 7, " ", 1, 0, '', 1);
        $pdf->SetFont('Arial', '', 1, 0);

        $pdf->SetTextColor(0, 0, 0);
        $pdf->Ln();

        $pdf->SetFont('Arial', 'I', 10);
        $pdf->SetTextColor(96, 130, 191);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->Cell(64, 7, "Voucher", 1, 0, '', 1);
        $pdf->Cell(65, 7, "A/c. Name", 1, 0, '', 1);
        $pdf->Cell(64, 7, "Amount", 1, 0, '', 1);
        $pdf->SetFont('Arial', '', 1, 0);

        $pdf->SetTextColor(0, 0, 0);
        $pdf->Ln();

        $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);

            $pdf->SetFont('Arial', '', 8);
            $pdf->Cell(64, 7, "$row2[vouchno]", 1);
            $pdf->Cell(65, 7, "$name[name]", 1);
            $pdf->Cell(64, 7, "$row2[amount]", 1);
            $pdf->Ln();

            $totrecd = $totrecd + $row2[recdamt];
            $totdisc = $totdisc + $row2[discount];
            $totclaim = $totclaim + $row2[claim];
            $totamt = $totamt + $row2[amount];
        }
        $pdf->SetFont('Arial', 'I', 10);
        $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFillColor(239, 2, 10);
        $pdf->Cell(64, 7, " ", 1, 0, '', 1);
        $pdf->Cell(65, 7, "Total", 1, 0, '', 1);
        $pdf->Cell(64, 7, number_format($totamt, 2), 1, 0, '', 1);
        $pdf->SetFont('Arial', '', 1, 0);

        $pdf->SetTextColor(0, 0, 0);
        $pdf->Ln();

        $cashadd = $cashsale + $cashsale1 + $cramt;
        $cashout = $cpamt + $cashpur;

        $pdf->SetFont('Arial', 'I', 10);
        $pdf->SetTextColor(96, 130, 191);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->Cell(64, 7, " ", 1, 0, '', 1);
        $pdf->Cell(65, 7, "Cash Total (Inward)", 1, 0, '', 1);
        $pdf->Cell(64, 7, "$cashadd", 1, 0, '', 1);
        $pdf->SetFont('Arial', '', 1, 0);

        $pdf->SetTextColor(0, 0, 0);
        $pdf->Ln();

        $pdf->SetFont('Arial', 'I', 10);
        $pdf->SetTextColor(96, 130, 191);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->Cell(64, 7, " ", 1, 0, '', 1);
        $pdf->Cell(65, 7, "Cash Total (Outward)", 1, 0, '', 1);
        $pdf->Cell(64, 7, "$cashout", 1, 0, '', 1);
        $pdf->SetFont('Arial', '', 1, 0);

        $pdf->SetTextColor(0, 0, 0);
        $pdf->Ln();

        $pdf->SetFont('Arial', 'I', 10);
        $pdf->SetTextColor(2, 237, 45);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->Cell(64, 7, " ", 1, 0, '', 1);
        $pdf->Cell(65, 7, "Closing Balance", 1, 0, '', 1);
        if ($mobal < 0) {
            $mopbal = $dtot;
        } else {
            $mopbal = $ctot;
        }
        $pdf->Cell(64, 7, $closebal . $cdc, 1, 0, '', 1);
        $pdf->SetFont('Arial', '', 1, 0);

        $pdf->SetTextColor(0, 0, 0);
        $pdf->Ln();
    }
    $pdf->Output("Daily Cash.pdf", "D");
}
?>