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

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

$maindb = 'saletran';
$zroot = $_SESSION["zdbroot"];

$formname = 'Productwise + Party Register';
$file = 'tipartyreg_fr.php';


$party = $_POST["party"];
$product = $_POST["prodcode"];

if (!empty($_POST['party'])) {
    foreach ($_POST['party'] as $cucheck2) {
        $ccheck2[$ccount2++] .= "OR b.`party` = '" . $cucheck2 . "' ";
    }
    foreach ($ccheck2 as $g => $cusvalues2) {
        $cuscheck2 .= $cusvalues2;
    }
    $partycond = " AND (b.`party` = '' " . $cuscheck2 . " )";
}
if (!empty($product)) {
    $prodcond = " AND a.`itemcode` = '$product' ";
}

if (empty($_POST["frmdt"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; saleitemgrp.php");
}
if (empty($_POST["todt"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; saleitemgrp.php");
}

$mfrmdate = trim($_POST["frmdt"]);
$mtodate = trim($_POST["todt"]);

$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));




$mainqry = mysqli_query($conn, "SELECT a.itemcode,  a.vouchno, a.barcode, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as `mydt`,
                            a.qty, a.discamt, a.subtot, a.rate, a.cgstamt, a.sgstamt, a.igstamt, a.total as netamt, a.amount, 
                            b.party, b.vouchno, b.`vouchdt` FROM `saletran` a, `salemain` b WHERE a.vouchno = b.vouchno AND b.`vouchdt` >= '$mfrmdate'
                           AND b.`vouchdt` <= '$mtodate' $prodcond $partycond")or die(mysqli_error($conn));

$getno = mysqli_num_rows($mainqry);

//print table($mainqry);
$gproduct = mysqli_query($conn, "SELECT * FROM `product` WHERE `prodcode` = '$product' ")or die(mysqli_error($conn));
$fproduct = mysqli_fetch_array($gproduct);

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

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        th{
            background: #26A69A;
        }
        .dth{
            background:#BABFC2;
        }
        .colr{
            background: #26A69A;
            font-size: 17px;
        }
        table tr td{
            font-size: 14px;

        }
    </style>
    </head>
    <body>


        <div class="container">            
            <?php include("includes/head_rep.php"); ?>

            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background-color:#D5F3EA; color:black;padding-top:7px" class="table table-bordered">
                        <tr>
                            <th width='10%' style="background-color:#D5F3EA; color:black;">Code : <b><?= $fproduct['prodcode']; ?></b></th>
                            <th width='45%' style="background-color:#D5F3EA; color:black;">Name : <b><?= $fproduct['name']; ?></b></th>
                            <th width='14%' style="background-color:#D5F3EA; color:black;">Hsn Code : <b><?= $fproduct['hsncode']; ?></b></th>
                            <th width='11%' style="background-color:#D5F3EA; color:black;">Gram : <b><?= $fproduct['gram']; ?></b></th>
                            <th width='10%' style="background-color:#D5F3EA; color:black;">Unit : <b><?= $fproduct['unit']; ?></b></th>
                            <th width='10%' style="background-color:#D5F3EA; color:black;">Rate : <b><?= $fproduct['rate']; ?></b></th>
                        </tr>
                    </table>
                    <table style="background: white;width: 100%" class="table table-condensed table-striped table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th width='4%'>Sr</th>
                                <th width='8%'>Date</th>
                                <th width='7%'>Voucher</th>
                                <th width='30%'>Party</th>
                                <th width='6%' class="text-right">Qty.</th>
                                <th width='7%' class="text-right">Rate</th>
                                <th width='7%' class="text-right">Amt.</th>
                                <th width='7%' class="text-right">Discount</th>
                                <th width='9%' class="text-right">Taxable</th>
                                <th width='9%' class="text-right">GST</th>
                                <th width='9%' class="text-right">Net Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $gst = 0;
                            $totqty = $totamt = $totdisc = $totgst = $totnet = $tottax = 0;

                            while ($fmain = mysqli_fetch_array($mainqry)) {

                                $getparty = mysqli_query($conn, "SELECT accode,name from accmast where `accode` = '$fmain[party]'")or die(mysqli_error($conn));
                                $fparty = mysqli_fetch_array($getparty);


                                $gst = $fmain['cgstamt'] + $fmain['sgstamt'] + $fmain['igstamt'];

                                $ctr++;
                                Echo"<tr>";
                                Echo"<td>$ctr</td>";
                                Echo"<td>$fmain[mydt]</td>";
                                Echo"<td>$fmain[vouchno]</td>";
                                Echo"<td>$fparty[name]</td>";
                                Echo"<td class='text-right'><b>$fmain[qty]</b></td>";
                                Echo"<td class='text-right'>$fmain[rate]</td>";
                                Echo"<td class='text-right'>$fmain[amount]</td>";
                                Echo"<td class='text-right'>$fmain[discamt]</td>";
                                Echo"<td class='text-right'>$fmain[subtot]</td>";
                                Echo"<td class='text-right'>$gst</td>";
                                Echo"<td class='text-right'>$fmain[netamt]</td>";
                                Echo"</tr>";

                                $totqty = $totqty + $fmain[qty];
                                $totamt = $totamt + $fmain[amount];
                                $totdisc = $totdisc + $fmain[discamt];
                                $tottax = $tottax + $fmain[subtot];
                                $totgst = $totgst + $gst;
                                $totnet = $totnet + $fmain[netamt];
                            }


                            Echo "<tr>";
                            Echo "<td class='colr'></td>";
                            Echo "<td class='colr'></td>";
                            Echo "<td class='colr'></td>";
                            Echo "<td class='text-right colr'><b>Total</b></td>";
                            Echo "<td class='numeric text-right colr'><b>$totqty</b></td>";
                            Echo "<td class='numeric text-right colr'><b></b></td>";
                            Echo "<td class='numeric text-right colr'><b>$totamt</b></td>";
                            Echo "<td class='numeric text-right colr'><b>$totdisc</b></td>";
                            Echo "<td class='numeric text-right colr'><b>$tottax</b></td>";
                            Echo "<td class='numeric text-right colr'><b>$totgst</b></td>";
                            Echo "<td class='numeric text-right colr'><b>$totnet</b></td>";
                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div> 
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}


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


    $contents .= "Code : $fproduct[prodcode], Name : $fproduct[name], Hsn Code : $fproduct[hsncode], Gram : $fproduct[gram], Unit : $fproduct[unit], Rate : $fproduct[rate],\n";
    $contents .= ",,,,,,\n";

    $contents .= "Sr,Date,Voucher,Party,Qty.,Rate,Amt.,Discount,Taxable,GST,Net Amount,\n";

    $ctr = 0;
    $totqty = $totamt = $totdisc = $totgst = $totnet = $tottax = 0;

    while ($fmain = mysqli_fetch_array($mainqry)) {

        $getparty = mysqli_query($conn, "SELECT accode,name from accmast where `accode` = '$fmain[party]'")or die(mysqli_error($conn));
        $fparty = mysqli_fetch_array($getparty);

        $gst = $fmain['cgstamt'] + $fmain['sgstamt'] + $fmain['igstamt'];
        
        $ctr++;
        $contents .= $ctr . ",";
        $contents .= $fmain[mydt] . ",";
        $contents .= $fmain[vouchno] . ",";
        $contents .= $fparty[name] . ",";
        $contents .= $fmain[qty] . ",";
        $contents .= $fmain[rate] . ",";
        $contents .= $fmain[amount] . ",";
        $contents .= $fmain[discamt] . ",";
        $contents .= $fmain[subtot] . ",";
        $contents .= $gst . ",";
        $contents .= $fmain[netamt] . "\n";

        $totqty = $totqty + $fmain['qty'];
        $totamt = $totamt + $fmain['amount'];
        $totdisc = $totdisc + $fmain['discamt'];
        $tottax = $tottax + $fmain['subtot'];
        $totgst = $totgst + $gst;
        $totnet = $totnet + $fmain['netamt'];
    }

    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= "Total" . ",";
    $contents .= $totqty . ",";
    $contents .= " " . ",";
    $contents .= $totamt . ",";
    $contents .= $totdisc . ",";
    $contents .= $tottax . ",";
    $contents .= $totgst . ",";
    $contents .= $totnet . "\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;
    }
}
?>