<?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 . " )";
    $partycondex = " AND (b.`partyname` = '' " . $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));

mysqli_query($conn, "create temporary table mydbf 
                        SELECT a.itemcode,  a.vouchno, a.barcode, a.hsncode,a.gram,a.unit,
                        DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as `mydt`, sum(a.qty) as tqty,
                        sum(a.discamt) as tdiscamt, sum(a.subtot) as taxable, 
                        sum(a.cgstamt+a.sgstamt+a.igstamt) as gstamt,sum(a.total) as netamt, 
                        sum(a.amount) as tamt, b.party FROM `saletran` a , salemain b 
                        WHERE a.vouchno = b.vouchno and a.`vouchdt` >= '$mfrmdate' AND
                        a.`vouchdt` <= '$mtodate' $prodcond $partycond group by a.barcode 
                        union
                        SELECT a.itemcode,  a.vouchno, a.barcode, a.hsncode,a.gram, a.unit,
                        DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as `mydt`,sum(a.qty) as tqty,
                        sum(a.discamt) as tdiscamt, sum(a.subtot) as taxable, 
                        sum(a.cgstamt+a.sgstamt+a.igstamt) as gstamt,sum(a.total) as netamt, 
                        sum(a.amount) as tamt, b.party FROM `deextran` a , deexmain b 
                        WHERE a.vouchno = b.vouchno and a.`vouchdt` >= '$mfrmdate' AND
                        a.`vouchdt` <= '$mtodate' $prodcond $partycond group by a.barcode 
                        union
                        SELECT a.itemcode,  a.vouchno, a.barcode, a.hsncode,a.gram,a.unit,
                        DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as `mydt`,sum(a.qty) as tqty,
                        000000000.00 as tdiscamt, sum(a.amtcurr) as taxable, 
                        000000000.00 as gstamt, sum(a.amtcurr) as netamt, 
                        sum(a.amtcurr) as tamt, b.partyname as party FROM `saletranexp` a , `salemainexp` b 
                        WHERE a.vouchno = b.vouchno and a.`vouchdt` >= '$mfrmdate' AND
                        a.`vouchdt` <= '$mtodate' $prodcond $partycondex group by a.barcode 
                        union
                        SELECT a.itemcode,  a.vouchno, a.barcode, a.hsncode,a.gram,a.unit,
                        DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as `mydt`, sum(a.qty) as tqty,
                        sum(a.discamt) as tdiscamt, sum(a.subtot) as taxable, 
                        sum(a.cgstamt+a.sgstamt+a.igstamt) as gstamt,sum(a.total) as netamt, 
                        sum(a.amount) as tamt, b.party FROM `slonltran` a , slonlmain b 
                        WHERE a.vouchno = b.vouchno and a.`vouchdt` >= '$mfrmdate' AND
                        a.`vouchdt` <= '$mtodate' $prodcond $partycond group by a.barcode") or die(mysqli_error($conn)); 

$mainqry = mysqli_query($conn, "SELECT itemcode, vouchno, barcode, hsncode, gram, unit,
                        `mydt`, sum(tqty) as tqty, sum(tdiscamt) as tdiscamt, sum(taxable) as taxable, 
                        sum(gstamt) as gstamt, sum(netamt) as netamt, sum(tamt) as tamt, party FROM mydbf group by barcode order by barcode") or die(mysqli_error($conn));

$getno = mysqli_num_rows($mainqry);

mysqli_query($conn, "drop table mydbf ")or die(mysqli_error($conn));

$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>
    <script type="text/javascript">
        $(document).ready(function () {
            $(".dtlsale").hide();
            $(".dtlsale2").hide();
            $("body").on('dblclick', '.getdtl', getDetails);
            //            $("body").on('dblclick', '.getdtl2', getDetails2);
            $("body").on('dblclick', '.dtlhide1', hideDetails);
            //            $("body").on('dblclick', '.dtlhide2', hideDetails2);
            function getDetails() {
                var trid = $(this).closest('tr').attr('id');
                var idval = trid.split('_')[1];
                $("#dtlsale_" + idval).show();
            }
            //            function getDetails2() {
            //                var trid = $(this).closest('tr').attr('id');
            //                var idval = trid.split('_')[1];
            //                $("#dtlsale2_" + idval).show();
            //            }
            function hideDetails() {
                var trid = $(this).closest('tr').attr('id');
                var idval = trid.split('_')[1];
                $("#dtlsale_" + idval).hide();
            }
            //            function hideDetails2() {
            //                var trid = $(this).closest('tr').attr('id');
            //                var idval = trid.split('_')[1];
            //                $("#dtlsale2_" + idval).hide();
            //            }
        });
    </script>
    </head>
    <body>
        <div class="container-fluid">            
            <?php include("includes/head_rep.php"); ?>

            <div class="row">
                <div class="col-lg-12 pad">
                    <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='4%'>Code</th>
                                <th width='26%'>Name</th>
                                <th width='5%'>Hsncode</th>
                                <th width='5%'>Grammage</th>
                                <th width='4%'>Unit</th>
                                <th width='6%' class="text-right">Total Qty.</th>
                                <th width='7%' class="text-right">Amt.</th>
                                <th width='5%' class="text-right">Discount</th>
                                <!--<th width='5%' class="text-right">Post.</th>-->
                                <th width='5%' class="text-right">Taxable</th>
                                <th width='4%' class="text-right">GST</th>
                                <th width='6%' class="text-right">Net Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $mnctr = 0;
                            $gst = 0;
                            $totqty = $totamt = $totdisc = $totgst = $totnet = $tottax = $totpostamt = 0;

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

                                $getproduct = mysqli_query($conn, "SELECT name from product where `barcode` = '$fmain->barcode'")or die(mysqli_error($conn));
                                $fproduct = mysqli_fetch_object($getproduct);

                                $mnctr++;

                                    $amt = $fmain->tamt;
                                $discamt = $fmain->tdiscamt;
                                $taxable = $fmain->taxable;
                                 $gstamt = $fmain->gstamt;
                                 $netamt = $fmain->netamt;
                                
                                Echo"<tr class='getdtl' id='getdtl_$mnctr'>";
                                Echo"<td>$mnctr</td>";
                                Echo"<td>$fmain->barcode</td>";
                                Echo"<td>$fproduct->name</td>";
                                Echo"<td>$fmain->hsncode</td>";
                                Echo"<td>$fmain->gram</td>";
                                Echo"<td>$fmain->unit</td>";
                                Echo"<td class='text-right'><b>$fmain->tqty</b></td>";
                                Echo"<td class='text-right'>" . number_format($amt, 2) . "</td>";
                                Echo"<td class='text-right'>" . number_format($discamt, 2) . "</td>";
                                Echo"<td class='text-right'>" . number_format($taxable, 2) . "</td>";
                                Echo"<td class='text-right'>" . number_format($gstamt, 2) . "</td>";
                                Echo"<td class='text-right'>" . number_format($netamt, 2) . "</td>";
                                Echo"</tr>";

                                $totqty = $totqty + $fmain->tqty;
                                $totamt = $totamt + $fmain->tamt;
                                $totdisc = $totdisc + $fmain->tdiscamt;
                                $tottax = $tottax + $fmain->taxable;
                                $totgst = $totgst + $fmain->gstamt;
                                $totnet = $totnet + $fmain->netamt;
//                                $totpostamt = $totpostamt + $fmain->postamt;

                                
//                                browse($fmain->postamt);
                                
                                Echo"<tr id='dtlsale_$mnctr'  class='dtlsale'>";
                                Echo"<td colspan='25'>";
                                Echo"<table class='table table-bordered'>";
                                Echo"<tr class='mySearchTr2 dtlhide1 id='dtlsale_$mnctr'>";
                                Echo"<th width = '4%'>Sr</th>";
                                Echo"<th width = '8%'>Date</th>";
                                Echo"<th width = '7%'>Voucher</th>";
                                Echo"<th width = '30%'>Party</th>";
                                Echo"<th width = '6%' class = 'text-right'>Qty.</th>";
                                Echo"<th width = '7%' class = 'text-right'>Rate</th>";
                                Echo"<th width = '7%' class = 'text-right'>Amt.</th>";
                                Echo"<th width = '7%' class = 'text-right'>Discount</th>";
                                Echo"<th width = '9%' class = 'text-right'>Taxable</th>";
                                Echo"<th width = '9%' class = 'text-right'>GST</th>";
                                Echo"<th width = '9%' class = 'text-right'>Net Amount</th>";
                                Echo"</tr>";


                                $mainqry2 = 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' and a.barcode = '$fmain->barcode' $partycond 
                               union
                               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 `deextran` a, `deexmain` b WHERE a.vouchno = b.vouchno AND 
                               b.`vouchdt` >= '$mfrmdate' AND b.`vouchdt` <= '$mtodate' and a.barcode = '$fmain->barcode' $partycond
                               union
                               SELECT a.itemcode,  a.vouchno, a.barcode, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as `mydt`,
                               a.qty, 0000000.00 as discamt, a.amtcurr as subtot, a.rate, 0000000.00 as cgstamt, 
                               0000000.00 as sgstamt, 0000000.00 as igstamt, a.amtcurr as netamt, a.amtcurr as amount, 
                               b.partyname as party, b.vouchno, a.`vouchdt` FROM `saletranexp` a, `salemainexp` b 
                               WHERE a.vouchno = b.vouchno AND a.`vouchdt` >= '$mfrmdate'
                               AND a.`vouchdt` <= '$mtodate' and a.barcode = '$fmain->barcode' $partycond2
                               union
                               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 `slonltran` a, `slonlmain` b WHERE a.vouchno = b.vouchno AND 
                               b.`vouchdt` >= '$mfrmdate' AND b.`vouchdt` <= '$mtodate' and a.barcode = '$fmain->barcode' $partycond                                
                               order by vouchdt") or die(mysqli_error($conn));

                                while ($fmain2 = mysqli_fetch_array($mainqry2)) {

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


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

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

                                Echo"</table>";
                                Echo"</td>";
                                Echo"</tr>";
                            }
                            Echo "<tr>";
                            Echo "<td class='colr'></td>";
                            Echo "<td class='colr'></td>";
                            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>$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>";
                            
//                            Echo "<tr style='background-color:#98F9F0'>";
//                            Echo "<td class='text-right ' colspan='9'><b>TOTAL POSTAGE</b></td>";
//                            Echo "<td class='numeric text-right '><b></b>$totpostamt</td>";
//                            Echo "<td class='numeric text-right '><b></b></td>";
//                            Echo "<td class='numeric text-right '><b></b></td>";
//                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div> 
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}


if (isset($_POST["excel"])) {
    $contents.= "Sr,Code,Name,HSNCode,Grammage,Unit,Total Qty.,Amt.,Discount,Taxable,GST,Net Amount\n";
$mnctr = 0;
$gst = 0;
$totqty = $totamt = $totdisc = $totgst = $totnet = $tottax = $totpostamt = 0;

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

    $getproduct = mysqli_query($conn, "SELECT name from product where `barcode` = '$fmain->barcode'")or die(mysqli_error($conn));
    $fproduct = mysqli_fetch_object($getproduct);

    $mnctr++;
    $contents.= $mnctr . ",";
    $contents.= $fmain->barcode . ",";
    $contents.= $fproduct->name . ",";
    $contents.= $fmain->hsncode . ",";
    $contents.= $fmain->gram . ",";
    $contents.= $fmain->unit . ",";
    $contents.= $fmain->tqty . ",";
    $contents.= $fmain->tamt . ",";
    $contents.= $fmain->tdiscamt . ",";
    $contents.= $fmain->taxable . ",";
    $contents.= $fmain->gstamt . ",";
    $contents.= $fmain->netamt . "\n";

        $totqty = $totqty + $fmain->tqty;
        $totamt = $totamt + $fmain->tamt;
        $totdisc = $totdisc + $fmain->tdiscamt;
        $tottax = $tottax + $fmain->taxable;
        $totgst = $totgst + $fmain->gstamt;
        $totnet = $totnet + $fmain->netamt;

//                                browse($fmain->postamt);
$colspan = ",,,,,,,,,,,,,,,,,,,,,,,,,";

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


   $mainqry2 = 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' and a.barcode = '$fmain->barcode' $partycond 
   union
   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 `deextran` a, `deexmain` b WHERE a.vouchno = b.vouchno AND b.`vouchdt` >= '$mfrmdate'
   AND b.`vouchdt` <= '$mtodate' and a.barcode = '$fmain->barcode' $partycond
   union
   SELECT a.itemcode,  a.vouchno, a.barcode, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as `mydt`,
   a.qty, 0000000.00 as discamt, a.amtcurr as subtot, a.rate, 0000000.00 as cgstamt, 
   0000000.00 as sgstamt, 0000000.00 as igstamt, a.amtcurr as netamt, a.amtcurr as amount, 
   b.partyname as party, b.vouchno, a.`vouchdt` FROM `saletranexp` a, `salemainexp` b 
   WHERE a.vouchno = b.vouchno AND a.`vouchdt` >= '$mfrmdate'
   AND a.`vouchdt` <= '$mtodate' and a.barcode = '$fmain->barcode' $partycond2
   union
   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 `slonltran` a, `slonlmain` b WHERE a.vouchno = b.vouchno AND b.`vouchdt` >= '$mfrmdate'
   AND b.`vouchdt` <= '$mtodate' and a.barcode = '$fmain->barcode' $partycond    
   order by vouchdt")or die(mysqli_error($conn));

    while ($fmain2 = mysqli_fetch_array($mainqry2)) {

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


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

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

    }
    }
    $contents.= ",,,";
    $contents.= "Total" . ",";
    $contents.= $totqty . ",";
    $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;
    }
}
?>