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

$formname = "Daily Report";
$file = "dailyrep_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);

$date = $_POST["date"];

$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(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'];
$mobal = $mopbal + $amt;

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



$getexpense = mysqli_query($conn, "SELECT SUM(amount) as amt from cashpay where vouchdt = '$date' and trnflag = '' ") or die(mysqli_error($conn));
$fexpense = mysqli_fetch_array($getexpense);
$expense = $fexpense['amt'];


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

$getamt2 = mysqli_fetch_array($getbal2);
$amt2 = $getamt2['amt'];
$mobal2 = $mopbal + $amt2;

$closecash = abs($mobal2);
if ($mobal2 < 0) {
    $mdc2 = "DR";
} else {
    $mdc2 = "CR";
}



$cpur1 = mysqli_query($conn, "SELECT SUM(netamt) AS amt FROM $purchfile WHERE vouchdt = '$date'  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'  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(netamt) 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(netamt) 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(netamt) 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"];


////////////////// contra entry

$continqry = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `contra` WHERE
                      vouchdt = '$date' and tobc = 3 group by vouchdt") or die(mysqli_error($conn));
$cntinget = mysqli_fetch_array($continqry);
$contrain = $cntinget["amt"];

$contoutqry = mysqli_query($conn, "SELECT *, SUM(amount) AS amt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `contra` WHERE
                      vouchdt = '$date' and frombc = 3 group by vouchdt") or die(mysqli_error($conn));
$cntoutget = mysqli_fetch_array($contoutqry);
$contraout = $cntoutget["amt"];


////////////////// sale return

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

$fsalret = mysqli_fetch_array($qsalret);


////////////////// purchase return

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

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

    include("includes/linkshead.php");
?>
    <style>
        .td {
            font-size: 15px;
        }

        @media print {
            .noPrint {
                display: none;
            }
        }

        .mySearchTr{
            background-color: #1abc9c;
        }
        
    </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>

            <table style="width:70%;" class="table-bordered table table-stripped table-hover" summary="Ledger - Detail">
                <tbody>
                    <tr style="height:10px;">
                        <th class='td mySearchTr' style="text-align:left;" scope="col" width="20">
                            <font size="3px" color=''><b>Opening Cash</b></font>
                        </th>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;background-color:white\" scope=\"col\" width=\"90\">$copen $mdc</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;">
                        <th class='td mySearchTr' style="text-align:left;" scope="col" width="20">
                            <font size="3px" color=''><b>Sale</b></font>
                        </th>
                        <?php
                        echo " <td class = 'td' style=\"text-align:right;background-color:white\" 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>
                        <th class='td mySearchTr' style="text-align:left;" scope="col" width="20">
                            <font size="3px" color=''><b>Purchase</b></font>
                        </th>
                        <?php
                        echo " <td class = 'td'  style=\"text-align:right;background-color:white\" scope=\"col\" width=\"90\">$cashpur</td>";
                        ?>
                    </tr>

                    <tr style="height:10px;">
                        <th class='td mySearchTr' style="text-align:left;" scope="col" width="20">
                            <font size="3px" color=''><b>Total Sale</b></font>
                        </th>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;background-color:white\" 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>
                        <th class='td mySearchTr' style="text-align:left;" scope="col" width="20">
                            <font size="3px" color=''><b>Total Purchase</b></font>
                        </th>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;background-color:white\" scope=\"col\" width=\"90\">" . number_format($totpur, 2) . "</td>";
                        ?>
                    </tr>

                    <tr style="height:10px;">
                        <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>";
                        ?>
                        <td class='td' style="text-align:left;" scope="col" width="40">
                            <font size="3px" color=''><b></b></font>
                        </td>
                        <th class='td mySearchTr' style="text-align:left;" scope="col" width="20">
                            <font size="3px" color=''><b>Expense</b></font>
                        </th>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right; background-color:white\" scope=\"col\" width=\"90\">" . number_format($expense, 2) . "</td>";
                        ?>
                    </tr>
                    
                    <tr style="height:10px;">
                        <th class='td mySearchTr' style="text-align:left;" scope="col" width="20">
                            <font size="3px" color=''><b>Sale Return</b></font>
                        </th>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right; background-color:white\" scope=\"col\" width=\"90\">" . number_format($fsalret['amt'], 2) . "</td>";
                        ?>
                        <td class='td' style="text-align:left;" scope="col" width="40">
                            <font size="3px" color=''><b></b></font>
                        </td>
                        <th class='td mySearchTr' style="text-align:left;" scope="col" width="20">
                            <font size="3px" color=''><b>Purchase Return</b></font>
                        </th>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;background-color:white\" scope=\"col\" width=\"90\">" . number_format($fpurret['amt'], 2) . "</td>";
                        ?>
                    </tr>
                    
                    <tr style="height:10px;">
                        <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>";
                        ?>
                        <td class='td' style="text-align:left;" scope="col" width="40">
                            <font size="3px" color=''><b></b></font>
                        </td>
                        <th class='td mySearchTr' style="text-align:left;" scope="col" width="20">
                            <font size="3px" color=''><b>Closing Cash</b></font>
                        </th>
                        <?php
                        echo " <td class = 'td'   style=\"text-align:right;background-color:white\" scope=\"col\" width=\"90\">$closecash  $mdc2</td>";
                        ?>
                    </tr>
            </table>
            <table style="width:70%;" class="table-bordered table table-stripped" summary="Ledger - Detail">
                <?php // if ($prntype == 2) {     
                ?>
                <tr style="height:10px;" class="mySearchTr">
                    <th class='td' style="text-align:left;" scope="col" width="10%">
                        <font size="3px" color=''><b></b></font>
                    </th>
                    <th class='td' style="text-align:left;" scope="col" width="40%">
                        <font size="3px" color=''><b>Cash Receipt</b></font>
                    </th>
                    <th class='td' style="text-align:left;" scope="col" width="12%">
                        <font size="3px" color=''><b></b></font>
                    </th>

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

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

                mysqli_select_db($conn, $zdb);
                while ($row = mysqli_fetch_array($crecp2)) {

                    $saleno = $row['remark1'] .  ' ' . $row['remark2'];

                    $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;background-color:white'>";
                    echo " <td class = 'td'  align='left' color=''>$row[vouchno]</td>";
                    echo " <td class = ''  align='left' color='' style='font-size:14px'>$name[name] ($saleno)</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 class="mySearchTr">
                    <th class='td' style="text-align:left;" scope="col" width="10">
                        <font size="3px" color=''><b></b></font>
                    </th>
                    <th class='td' style="text-align:left;" scope="col" width="20">
                        <font size="3px" color=''><b>Cash Payment</b></font>
                    </th>
                    <th class='td' style="text-align:left;" scope="col" width="20">
                        <font size="3px" color=''><b></b></font>
                    </th>
                </tr>
                <tr class="mySearchTr">
                    <th class='td' align="left" color=''><b>Voucher</b></th>
                    <th class='td' align="left" color=''><b>A/c. Name.</b></th>
                    <th class='td' align="right" color=''><b>Amount</b></th>
                </tr>
                <?php
                $totrecd = 0;
                $totdisc = 0;
                $totclaim = 0;
                $totamt = 0;
                while ($row2 = mysqli_fetch_array($cpaid2)) {
                    $remark = $row2['remark1'] . " " . $row2['remark2'];

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

                    echo "<tr style='background-color:white'>";
                    echo " <td class = 'td'  align='left' color=''>$row2[vouchno]</td>";
                    echo " <td style='font-size:14px;' align='left' color=''>$name[name] ($remark)</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>
               
                <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"])) {

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

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

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

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

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

    $contents .= "Total Sale" . ",";
    $contents .= "$totsale" . ",";
    $contents .= "Total Purchase" . ",";
    $contents .= "$totpur" . "\n";
    
    $contents .= "Perfume Sale" . ",";
    $contents .= "$perfsale" . "\n";
    
    $contents .= "Other Sale" . ",";
    $contents .= "$othersale" . "\n";
    
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Expense" . ",";
    $contents .= "$expense" . "\n";


    $contents .= "Sale Return" . ",";
    $contents .= "$fsalret[amt]" . ",";
    $contents .= "Purchase Return" . ",";
    $contents .= "$fpurret[amt]" . "\n";

    $contents .= "Cash Transfer In" . ",";
    $contents .= "$contrain" . ",";
    $contents .= "Cash Transfer Out" . ",";
    $contents .= "$contraout" . "\n";

    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Closing Cash" . ",";
    $contents .= "$closecash  $mdc2" . "\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)) {
        $saleno = $row['remark1'] .  ' ' . $row['remark2'];

        $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'] . " ($saleno)" . ",";
        $contents .= $row['amount'] . "\n";

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

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

    $contents .= "" . "\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)) {
        $remark = $row2['remark1'] . " " . $row2['remark2'];

        $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'] . " " . "($remark)" . ",";
        $contents .= $row2['amount'] . "\n";

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

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

    $contents .= " " . "\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)) {
        $saleno2 = $row3['remark1'] .  ' ' . $row3['remark2'];

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

        $contents .= $row3['vouchno'] . ",";
        $contents .= $name['name'] . " " . "($saleno2)" . ",";
        $contents .= $row3['amount'] . "\n";

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

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

    $contents .= " " . "\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)) {
        $remark2 = $row4['remark1'] . " " . $row4['remark2'];

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

        $contents .= $row4['vouchno'] . ",";
        $contents .= $name['name'] . " " . "($remark2)" . ",";
        $contents .= $row4['amount'] . "\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;
    }
}
if (isset($_POST["subpdf"])) {

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

    $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"];

    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(40, 7, "Cash Sale", 1, 0, '', 1);
    $pdf->Cell(39, 7, "$cashsale", 1, 0, '', 1);
    $pdf->Cell(35, 7, " ", 1, 0, '', 1);
    $pdf->Cell(40, 7, "Cash Purchase", 1, 0, '', 1);
    $pdf->Cell(39, 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(40, 7, "Credit Sale", 1, 0, '', 1);
    $pdf->Cell(39, 7, "$creditsale", 1, 0, '', 1);
    $pdf->Cell(35, 7, " ", 1, 0, '', 1);
    $pdf->Cell(40, 7, "Credit Purchase", 1, 0, '', 1);
    $pdf->Cell(39, 7, "$creditpur", 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(40, 7, "CC Sale", 1, 0, '', 1);
    $pdf->Cell(39, 7, "$ccsale", 1, 0, '', 1);
    $pdf->Cell(35, 7, " ", 1, 0, '', 1);
    $pdf->Cell(40, 7, " ", 1, 0, '', 1);
    $pdf->Cell(39, 7, " ", 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0);

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

    $pdf->SetFont('Arial', 'I', 10);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(255, 0, 0);
    $pdf->Cell(40, 7, "Total Sale", 1, 0, '', 1);
    $pdf->Cell(39, 7, number_format($totsale, 2), 1, 0, '', 1);
    $pdf->Cell(35, 7, " ", 1, 0, '', 1);
    $pdf->Cell(40, 7, "Total Purchase", 1, 0, '', 1);
    $pdf->Cell(39, 7, number_format($totpur, 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(47, 7, " ", 1, 0, '', 1);
    $pdf->Cell(100, 7, "Cash Receipt", 1, 0, '', 1);
    $pdf->Cell(46, 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(47, 7, "Voucher", 1, 0, '', 1);
    $pdf->Cell(100, 7, "A/c. Name", 1, 0, '', 1);
    $pdf->Cell(46, 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(47, 7, "$row[vouchno]", 1);
        $pdf->Cell(100, 7, "$name[name]", 1);
        $pdf->Cell(46, 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, 0, 0);
    $pdf->SetFillColor(255, 255, 255);
    $pdf->Cell(47, 7, " ", 1, 0, '', 1);
    $pdf->Cell(100, 7, "Total", 1, 0, '', 1);
    $pdf->Cell(46, 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(47, 7, " ", 1, 0, '', 1);
    $pdf->Cell(100, 7, "Cash Payment", 1, 0, '', 1);
    $pdf->Cell(46, 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(47, 7, "Voucher", 1, 0, '', 1);
    $pdf->Cell(100, 7, "A/c. Name", 1, 0, '', 1);
    $pdf->Cell(46, 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(47, 7, "$row2[vouchno]", 1);
        $pdf->Cell(100, 7, "$name[name]", 1);
        $pdf->Cell(46, 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, 0, 0);
    $pdf->SetFillColor(255, 255, 255);
    $pdf->Cell(47, 7, " ", 1, 0, '', 1);
    $pdf->Cell(100, 7, "Total", 1, 0, '', 1);
    $pdf->Cell(46, 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(47, 7, " ", 1, 0, '', 1);
    $pdf->Cell(100, 7, "Bank Receipt", 1, 0, '', 1);
    $pdf->Cell(46, 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(47, 7, "Voucher", 1, 0, '', 1);
    $pdf->Cell(100, 7, "A/c. Name", 1, 0, '', 1);
    $pdf->Cell(46, 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 ($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);
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(47, 7, "$row3[vouchno]", 1);
        $pdf->Cell(100, 7, "$name[name]", 1);
        $pdf->Cell(46, 7, "$row3[amount]", 1);
        $pdf->Ln();

        $totrecd = $totrecd + $row3['recdamt'];
        $totdisc = $totdisc + $row3['discount'];
        $totclaim = $totclaim + $row3['claim'];
        $totamt = $totamt + $row3['amount'];
    }
    $pdf->SetFont('Arial', 'I', 10);
    $pdf->SetTextColor(255, 0, 0);
    $pdf->SetFillColor(255, 255, 255);
    $pdf->Cell(47, 7, " ", 1, 0, '', 1);
    $pdf->Cell(100, 7, "Total", 1, 0, '', 1);
    $pdf->Cell(46, 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(47, 7, " ", 1, 0, '', 1);
    $pdf->Cell(100, 7, "Bank Payment", 1, 0, '', 1);
    $pdf->Cell(46, 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(47, 7, "Voucher", 1, 0, '', 1);
    $pdf->Cell(100, 7, "A/c. Name", 1, 0, '', 1);
    $pdf->Cell(46, 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 ($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);
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(47, 7, "$row4[vouchno]", 1);
        $pdf->Cell(100, 7, "$name[name]", 1);
        $pdf->Cell(46, 7, "$row4[amount]", 1);
        $pdf->Ln();

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

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