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

$maindb   = "invmain";
//$trandb = "saletran";
$formname = "Labels";
$file = "rpbill_fr.php";

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

if (isset($_POST["view"])) {
    
    $school    = $_POST[school];
    $medium = $_POST[medium];
    $exam    = $_POST[exam];
    $section    = $_POST[section];
    $session    = $_POST[session];
    $dtype   = $_POST[dtype];

    $schlcond    = "";
    $medmcond    = "";
    $examcond    = "";
    $sectcond    = "";
    $sesscond    = "";
    $mgrpcond    = '';

    if ($dtype == '0') {
        $mgrpcond = '';
        browse('Group Cannot Be Empty');
        header ("refresh:location " . $file);
        echo"<script>window.close();</script>";
        return false;
      
    } else {
        if ($dtype == '1') {
           $mgrpcond = " and grpcode = '010' ";
        } else {
           $mgrpcond = " and grpcode = '009' ";
        }
    
    }
 

    if (!empty($school)) {
        $schlcond = " AND `accode` = '$school' ";
    }

    if (!empty($medium)) {
        $medmcond = " AND `medmcode` = '$medium' ";
    }

    if (!empty($exam)) {
        $examcond = " AND `examcode` = '$exam' ";
    }

    if (!empty($section)) {
        $sectcond = " AND `section` = '$section' ";
    }

    if (!empty($session)) {
        $sesscond = " AND `session` = '$session' ";
    }

    $cmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE accode != '' $schlcond $medmcond $examcond $sectcond $sesscond group by accode")
            or die(mysqli_error($conn));

    include("includes/linkshead.php");
    ?>
    <!--    <script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-lg-12 pad">
                    <body>        
                        <table width="100%" border="0" cellspacing='0' cellpadding='0' style='height:30%;border: 0px solid red;'>            
                            <?php
                            $counter = 1;
                            $ctr = 0;
                            $breaker = 1;
                            while ($ctmp = mysqli_fetch_object($cmain)) {
                                $gschl = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = $ctmp->accode")
                                        or die (mysqli_error($conn));
                                $fschl = mysqli_fetch_array($gschl);
                                if ($counter == 1) {
                                    $name_1 = $fschl[name];
                                    $add1_1 = $ctmp->add1;
                                    $add2_1 = $ctmp->add2;
                                    $add3_1 = $ctmp->add3;
                                    $city_1 = $ctmp->city;
                                    $pin_1 = $ctmp->pin;
                                    $station_1 = $ctmp->station;
                                    $tel_1 = $ctmp->tel;
                                    $mobile_1 = $ctmp->mobile;
                                    echo "<td>";
                                    echo "<font size='1.5px'><b>" . $name_1 . "</b>";
                                    echo "</td>";
                                } if ($counter == 2) {
                                    $name_2 = $fschl[name];
                                    $add1_2 = $ctmp->add1;
                                    $add2_2 = $ctmp->add2;
                                    $add3_2 = $ctmp->add3;
                                    $city_2 = $ctmp->city;
                                    $pin_2 = $ctmp->pin;
                                    $station_2 = $ctmp->station;
                                    $tel_2 = $ctmp->tel;
                                    $mobile_2 = $ctmp->mobile;
                                    echo "<td>";
                                    echo "<font  size='1.5px'><b>" . $name_2 . "</b>";
                                    echo "</td>";
                                } if ($counter == 3) {
                                    $name_3 = $fschl[name];
                                    $add1_3 = $ctmp->add1;
                                    $add2_3 = $ctmp->add2;
                                    $add3_3 = $ctmp->add3;
                                    $city_3 = $ctmp->city;
                                    $pin_3 = $ctmp->pin;
                                    $station_3 = $ctmp->station;
                                    $tel_3 = $ctmp->tel;
                                    $mobile_3 = $ctmp->mobile;
                                    echo "<td>";
                                    echo "<font  size='1.5px'><b>" . $name_3 . "</b>";
                                    echo "</td>";
                                    echo "<tr>";
                                    echo "<td>";
                                    if (!empty($add1_1)) {
                                        echo "<font  size='1.5px'>" . $add1_1;
                                    } else {
                                        echo "<br/>";
                                    } echo "</td>";
                                    echo "<td>";
                                    if (!empty($add1_2)) {
                                        echo "<font  size='1.5px'>" . $add1_2;
                                    } else {
                                        echo "<br/>";
                                    } echo "</td>";
                                    echo "<td>";
                                    if (!empty($add1_3)) {
                                        echo "<font  size='1.5px'>" . $add1_3;
                                    } else {
                                        echo "<br/>";
                                    } echo "</td>";
                                    echo "</tr>";
                                    echo "<tr>";
                                    echo "<td>";
                                    if (!empty($add2_1)) {
                                        echo "<font  size='1.5px'>" . $add2_1;
                                    } else {
                                        echo "<br/>";
                                    } echo "</td>";
                                    echo "<td>";
                                    if (!empty($add2_2)) {
                                        echo "<font  size='1.5px'>" . $add2_2;
                                    } else {
                                        echo "<br/>";
                                    } echo "</td>";
                                    echo "<td>";
                                    if (!empty($add2_3)) {
                                        echo "<font  size='1.5px'>" . $add2_3;
                                    } else {
                                        echo "<br/>";
                                    } echo "</td>";
                                    echo "</tr>";
                                    echo "<tr>";
                                    echo "<td>";
                                    if (!empty($add3_1)) {
                                        echo "<font  size='1.5px'>" . $add3_1;
                                    } else {
                                        echo "<br/>";
                                    } echo "</td>";
                                    echo "<td>";
                                    if (!empty($add3_2)) {
                                        echo "<font  size='1.5px'>" . $add3_2;
                                    } else {
                                        echo "<br/>";
                                    } echo "</td>";
                                    echo "<td>";
                                    if (!empty($add1_1)) {
                                        echo "<font  size='1.5px'>" . $add3_3;
                                    } else {
                                        echo "<br/>";
                                    } echo "</td>";
                                    echo "</tr>";
                                    echo "<tr>";
                                    echo "<td>";
                                    echo "<font  size='1.5px'>" . $city_1 . " " . $pin_1;
                                    echo "</td>";
                                    echo "<td>";
                                    echo "<font  size='1.5px'>" . $city_2 . " " . $pin_2;
                                    echo "</td>";
                                    echo "<td>";
                                    echo "<font  size='1.5px'>" . $city_3 . " " . $pin_3;
                                    echo "</td>";
                                    echo "</tr>";
                                    echo "<tr>";
                                    echo "<td>";
                                    echo "<font  size='1.5px'>" . $station_1 . "</font>";
                                    echo "</td>";
                                    echo "<td>";
                                    echo "<font  size='1.5px'>" . $station_2 . "</font>";
                                    echo "</td>";
                                    echo "<td>";
                                    echo "<font  size='1.5px'>" . $station_3 . "</font>";
                                    echo "</td>";
                                    echo "</tr>";
                                    echo "<tr>";
                                    echo "<td>";
                                    echo "<font  size='1.5px'>" . $tel_1 . " " . $mobile_1 . "</font>";
                                    echo "</td>";
                                    echo "<td>";
                                    echo "<font  size='1.5px'>" . $tel_2 . " " . $mobile_2 . "</font>";
                                    echo "</td>";
                                    echo "<td>";
                                    echo "<font  size='1.5px'>" . $tel_3 . " " . $mobile_3 . "</font>";
                                    echo "</td>";
                                    echo "</tr>";
                                    echo "<tr>";
                                    echo "<td>";
                                    echo "</td>";
                                    echo "</tr>";
                                    $counter = 0;
                                } $counter = $counter + 1;
                                $ctr = $ctr + 1;
                                if ($ctr == 24) {
                                    echo "<tr>";
                                    echo "<td>";
                                    echo "</td>";
                                    echo "</tr>";
                                    echo "<tr>";
                                    echo "<td>";
                                    echo "</td>";
                                    echo "</tr>";
                                    $ctr = 0;
                                }
                            } if ($counter = 2) {
                                echo "<tr>";
                                echo "<td>";
                                if (!empty($add1_1)) {
                                    echo "<font  size='1.5px'>" . $add1_1;
                                } else {
                                    echo "<br/>";
                                } echo "</td>";
                                echo "</tr>";
                                echo "<tr>";
                                echo "<td>";
                                if (!empty($add2_1)) {
                                    echo "<font  size='1.5px'>" . $add2_1;
                                } else {
                                    echo "<br/>";
                                } echo "</td>";
                                echo "</tr>";
                                echo "<tr>";
                                echo "<td>";
                                if (!empty($add3_1)) {
                                    echo "<font  size='1.5px'>" . $add3_1;
                                } else {
                                    echo "<br/>";
                                } echo "</td>";
                                echo "</tr>";
                                echo "<tr>";
                                echo "<td>";
                                echo "<font  size='1.5px'>" . $city_1 . " " . $pin_1;
                                echo "</td>";
                                echo "</tr>";
                                echo "<tr>";
                                echo "<td>";
                                echo "<font  size='1.5px'>" . $station_1 . "</font>";
                                echo "</td>";
                                echo "</tr>";
                                echo "<tr>";
                                echo "<td>";
                                echo "<font  size='1.5px'>" . $tel_1 . " " . $mobile_1 . "</font>";
                                echo "</td>";
                                echo "</tr>";
                                echo "<tr>";
                                echo "<td>";
                                echo "</td>";
                                echo "</tr>";
                            } else {
                                echo "<tr>";
                                echo "<td>";
                                if (!empty($add1_1)) {
                                    echo "<font  size='1.5px'>" . $add1_1;
                                } else {
                                    echo "<br/>";
                                } echo "</td>";
                                echo "<td>";
                                if (!empty($add1_2)) {
                                    echo "<font  size='1.5px'>" . $add1_2;
                                } else {
                                    echo "<br/>";
                                } echo "</td>";
                                echo "</tr>";
                                echo "<tr>";
                                echo "<td>";
                                if (!empty($add2_1)) {
                                    echo "<font  size='1.5px'>" . $add2_1;
                                } else {
                                    echo "<br/>";
                                } echo "</td>";
                                echo "<td>";
                                if (!empty($add2_2)) {
                                    echo "<font  size='1.5px'>" . $add2_2;
                                } else {
                                    echo "<br/>";
                                } echo "</td>";
                                echo "</tr>";
                                echo "<tr>";
                                echo "<td>";
                                if (!empty($add3_1)) {
                                    echo "<font  size='1.5px'>" . $add3_1;
                                } else {
                                    echo "<br/>";
                                } echo "</td>";
                                echo "<td>";
                                if (!empty($add3_2)) {
                                    echo "<font  size='1.5px'>" . $add3_2;
                                } else {
                                    echo "<br/>";
                                } echo "</td>";
                                echo "</tr>";
                                echo "<tr>";
                                echo "<td>";
                                echo "<font  size='1.5px'>" . $city_1 . " " . $pin_1;
                                echo "</td>";
                                echo "<td>";
                                echo "<font  size='1.5px'>" . $city_2 . " " . $pin_2;
                                echo "</td>";
                                echo "</tr>";
                                echo "<tr>";
                                echo "<td>";
                                echo "<font  size='1.5px'>" . $station_1 . "</font>";
                                echo "</td>";
                                echo "<td>";
                                echo "<font  size='1.5px'>" . $station_2 . "</font>";
                                echo "</td>";
                                echo "</tr>";
                                echo "<tr>";
                                echo "<td>";
                                echo "<font  size='1.5px'>" . $tel_1 . " " . $mobile_1 . "</font>";
                                echo "</td>";
                                echo "<td>";
                                echo "<font  size='1.5px'>" . $tel_2 . " " . $mobile_2 . "</font>";
                                echo "</td>";
                                echo "</tr>";
                                echo "<tr>";
                                echo "<td>";
                                echo "</td>";
                                echo "</tr>";
                            }
                            ?>             
                        </table>    
                    </body>
                </div>
            </div>
        </div>

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

if (isset($_POST["excel"])) {
    $party = $_POST[party];
    $item = $_POST[item];
    $godown = $_POST[godown];
    $mtype = $_POST[mtype];
    $frmdt = $_POST[frmdt];
    $todt = $_POST[todt];

    $partycond = "";
    $brokercond = "";
    $transportcond = "";
    $itemcond = "";
    $godowncond = "";
    $mtypecond = "";
    if (!empty($party)) {
        $partycond = " AND `party`='$party' ";
    }

    if (!empty($item)) {
        $itemcond = " AND `name`='$item' ";
    }

    if (!empty($godown)) {
        $godowncond = " AND `godown`='$godown' ";
    }


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


    if (!empty($mtype)) {
        $mtypecond = " AND `type`='$mtype' ";
    }
// print "SELECT * FROM `$maindb` WHERE `vouchno`!='' $partyname $broker $transpor $item $godown $mtype";
    $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $maindb where vouchno != '' 
             $partycond $itemcond $godowncond $mtypecond")or die(mysqli_error($conn));

    $contents.="Sr,Type,Invoice No.,Date,Customer,Amount,Discount,Freight,Qty.,Net Amount\n";

    $ctr = 0;
    $totqty = 0;
    $totamt = 0;
    $totinv = 0;
    while ($pmain = mysqli_fetch_array($getpmain)) {
        $getmtype = mysqli_query($conn, "SELECT * FROM `type` WHERE `code`='$pmain[type]'")or die(mysqli_error($conn));
        $mtype = mysqli_fetch_array($getmtype);

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

        $getmqty = mysqli_query($conn, "SELECT SUM(qty)as totqty FROM `$trandb` WHERE `vouchno`='$pmain[VOUCHNO]'")or die(mysqli_error($conn));
        $mqty = mysqli_fetch_array($getmqty);
        $ctr++;
        $name = $mtype[name];
        $invno = $pmain[invno];
        $mydt = $pmain[mydt];
        $custname = $mcust[name];
        $subtot = $pmain[subtotal];
        $disc = $pmain[discount];
        $freight = $pmain[freight];
        $totqty = $pmain[totqty];
        $netamt = $pmain[netamt];

        $contents.=$ctr . ",";
        $contents.=$name . ",";
        $contents.=$invno . ",";
        $contents.=$mydt . ",";
        $contents.=$custname . ",";
        $contents.=$subtot . ",";
        $contents.=$disc . ",";
        $contents.=$freight . ",";
        $contents.=$totqty . ",";
        $contents.=$netamt . "\n";

        if ($_POST["dtype"] == 2) {

            $contents.="Sr,Brand,Item Name,Taka,Qty.,Tot. Qty.,Flag,Flag Qty.,Inv. Qty.,Rate,Amount\n";

            $ctr2 = 0;
            $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
            while ($tran = mysqli_fetch_array($gettran)) {

                $getbrand = mysqli_query($conn, "SELECT * FROM `brand` WHERE `code`='$tran[brand]'")or die(mysqli_error($conn));
                $mbrand = mysqli_fetch_array($getbrand);

                $getitem = mysqli_query($conn, "SELECT * FROM `item` WHERE `code`='$tran[itemcode]'")or die(mysqli_error($conn));
                $mitem = mysqli_fetch_array($getitem);

                $ctr2++;
                $brname = $mbrand[name];
                $itname = $mitem[name];
                $taka = $tran[taka];
                $qty = $tran[qty];
                $tqty = $tran[totqty];
                $flag = $tran[flag];
                $flagqty = $tran[flagqty];
                $invqty = $tran[invqty];
                $rate = $tran[rate];
                $amount = $tran[amount];

                $contents.=$ctr2 . ",";
                $contents.=$brname . ",";
                $contents.=$itname . ",";
                $contents.=$taka . ",";
                $contents.=$qty . ",";
                $contents.=$tqty . ",";
                $contents.=$flag . ",";
                $contents.=$flagqty . ",";
                $contents.=$invqty . ",";
                $contents.=$rate . ",";
                $contents.=$amount . "\n";

                $totqty = $totqty + $tran[totqty];
                $totinv = $totinv + $tran[invqty];
                $totamt = $totamt + $tran[amount];
            }
        }
    }
    $contents.=" " . ",";
    $contents.=" " . ",";
    $contents.=" " . ",";
    $contents.=" " . ",";
    $contents.="Total" . ",";
    $contents.=$totqty . ",";
    $contents.=$totinv . ",";
    $contents.="" . ",";
    $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;
    }
}
?>