<?php
session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
include ('../inc/invfunctions.php');
include ('../inc/repfunctions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
    $_SESSION['cuPages']='home.php'; 
    $cuPage='home.php';
    $aid=$_SESSION['uid'];
    $uty=$_SESSION['utype'];    
    $brid=$_SESSION['abrid'];    
}else{
    header('Location:../index.php');
    exit;    
}
$mhead='home';
if(get_fild_data('tbl_setting','1','sval')==0){$menuh = 'Dashboard';}else{$menuh = 'ড্যাশবোর্ড';}
$phead='home';
if(get_fild_data('tbl_setting','1','sval')==0){$page = 'Dashboard';}else{$page = 'ড্যাশবোর্ড';}
$dtnow = date("Y-m-d h:i:s", time());

$date = new DateTime();
$today= $date->format('Y-m-d'); // Replacement code
?>
<?php


if(isset($_POST['ibrid']) && $_POST['ibrid']!=''){
    $ibrid=$_POST['ibrid'];    
}else{
    $ibrid=$brid;    
}
?> 


<?php 

$start=date("Y-m-d", time());
$end=date("Y-m-d", time());

if(isset($_POST['fdate']) && isset($_POST['tdate'])){
$start=$_POST['fdate'];    
$end=$_POST['tdate'];	
}

if(isset($_POST['month'])){
    $start=date("Y-m-d", time());
    $end=date("Y-m-d", time());
}


if(isset($_POST['month'])){
$start=date('Y-m-01');
$end=date('Y-m-t');
}

if(isset($_POST['week'])){
    $start=date('Y-m-d', strtotime('monday this week'));
    $end=date('Y-m-d', strtotime('sunday this week'));
}

if(isset($_POST['year'])){
    $buisness=get_fild_data('tbl_limitset',1,'strDate');
    $buisness = explode('-', $buisness);

    $start_month   = $buisness[1];
    $end_month = $start_month -1;
    if ($start_month == 1) {
        $end_month = 12;
    }
    
    $start_year = date('Y');
    //if current month is less than start month change start year to last year
    if (date('n') < $start_month) {
        $start_year = $start_year - 1;
    }

    $end_year = date('Y');
    //if current month is greater than end month change end year to next year
    if (date('n') > $end_month) {
        $end_year = $start_year + 1;
    }
    $start_date = $start_year . '-' . str_pad($start_month, 2, 0, STR_PAD_LEFT) . '-01';
    $end_date = $end_year . '-' . str_pad($end_month, 2, 0, STR_PAD_LEFT) . '-01';
    $end_date = date('Y-m-t', strtotime($end_date));
    $start=$start_date;
    $end=$end_date;
}
?>


<?php
include('../layout/head.php');
include('../layout/header.php');
include('../layout/side_nav.php');
$sql21="SELECT * FROM tbl_limitset WHERE id=1";
$query21=mysqli_query($con,$sql21)or die(mysqli_error($con));
$row21=mysqli_fetch_array($query21);

$expdate=$row21['expDate'];
$startTimeStamp = strtotime("$today");
$endTimeStamp = strtotime("$expdate");

$timeDiff = abs($endTimeStamp - $startTimeStamp);

$numberDays = $timeDiff/86400;  // 86400 seconds in one day

// and you might want to convert to integer
$numberDays = intval($numberDays);
?>
<?php if($adm['home']==1){?> 
<div class="content-wrapper">
    <?php include('../layout/page_head.php'); ?>
    <!-- Main content -->
    <section class="content">
        <!-- Main row -->
        <div class="row">
            <?php if($numberDays<5){ ?>
                <div class="alert alert-danger alert-dismissable" >
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                    <i class="zmdi zmdi-block pr-15 pull-left"></i><p class="pull-left">Your license will be expired in <?php echo $numberDays; ?> days.Please, renew the license before it gets expired</p> 
                    <div class="clearfix"></div>
			    </div>  


            <?php } ?>

            <div class="col-md-4 col-xs-12 " >
                <div class="box " style="height: 458px;">
                    <div class="box-header">
                        <i class="fa fa-bell"></i>

                        <h3 class="box-title">
                            Notifications
                        </h3>
                    </div>
                    <div class="box-body" >
                        <div class="col-md-12 no-padding db-notification-container" style="">
                            <table id="" class="table notification_table">
                                    <?php 
                                    if(get_fild_data('tbl_setting','12','sval')==0){
                                        if($uty=='1'){ 
                                            $sql="SELECT * FROM tbl_cheqman ORDER BY apdate DESC,id DESC limit 5"; 
                                        }else{
                                            $sql="SELECT * FROM tbl_cheqman WHERE brid='$brid'ORDER BY apdate DESC,id DESC limit 5"; 
                                        } 
                                        $query=mysqli_query($con,$sql)or die(mysqli_error($con));
                                        while ($row=mysqli_fetch_array($query)){
                                            $invno=$row['refinv'];     
                                            ?>

                                            <tr>
                                                <td width="30px" class="db-noti-icons"><i class="fa fa-shopping-cart"></i></td> 
                                                <td>Cheque No <span class="label label-success"><?php echo $row['chkno'];?></span> &nbsp; &nbsp; &nbsp;<?php if($row['type']=='SU'){echo get_fild_data('tbl_supplier',$row['clientid'],'name');}else{echo get_fild_data('tbl_customer',$row['clientid'],'name');}?></span>&nbsp;&nbsp;<strong><?php echo 'DT:  '. date("d M Y", strtotime($row['chkdt'])); ?></strong>  <strong><?php if($row['paymod']=='PAY'){echo 'Given';}else{ echo 'Receive';}?></strong> </td>                                           
                                                <td class="db-noti-action-links"><a  href="ban_checkman.php" class="btn btn-flat bg-purple btn-xs ">View</a> </td>                                          
                                            </tr>
                                        <?php }} ?>  
                                        <?php 
                                        if($uty=='1'){ 
                                            $sql="SELECT * FROM tbl_purchase ORDER BY apdate DESC,id DESC limit 5"; 
                                        }else{
                                            $sql="SELECT * FROM tbl_purchase WHERE brid='$brid'ORDER BY apdate DESC,id DESC limit 5"; 
                                        } 
                                        $query=mysqli_query($con,$sql)or die(mysqli_error($con));
                                        while ($row=mysqli_fetch_array($query)){
                                            $invno=$row['invno'];     
                                            ?>

                                            <tr>
                                                <td width="30px" class="db-noti-icons"><i class="fa fa-shopping-cart"></i></td> 
                                                <td>Purchase <span class="label label-success"><?php echo $row['invno'];?></span> &nbsp;<?php if($row['type']=='SU'){echo get_fild_data('tbl_supplier',$row['supid'],'name');}else{echo get_fild_data('tbl_customer',$row['supid'],'name');}?></span><strong><?php echo 'T: '.numtolocal($row['total'],get_fild_data('tbl_currency','1','symbol')); ?></strong><br><strong><?php //echo 'D: '.numtolocal($cashst,get_fild_data('tbl_currency','1','symbol')); ?></strong></td>                                           
                                                <td class="db-noti-action-links"><a  href="" class="btn btn-flat bg-purple btn-xs popup" data-callback="">View</a> </td>                                          
                                            </tr>
                                        <?php } ?>  
                                        <!-- sales list -->
                                        <?php 
                                        if($uty=='1'){ 
                                            $sql="SELECT * FROM tbl_sales ORDER BY apdate DESC,id DESC limit 5"; 
                                        }else{
                                            $sql="SELECT * FROM tbl_sales WHERE brid='$brid' ORDER BY apdate DESC,id DESC limit 5"; 
                                        } 
                                        $query=mysqli_query($con,$sql)or die(mysqli_error($con));
                                        while ($row=mysqli_fetch_array($query)){
                                            $invno=$row['invno'];    
                                    //$cashst=($row['total']-get_invcash($invno,'P'));    
                                            ?>

                                            <tr>
                                                <td width="30px" class="db-noti-icons"><i class="fa fa-shopping-cart"></i></td> 
                                                <td>Sales <span class="label label-success"><?php echo $row['invno'];?></span> &nbsp;<strong><?php echo 'T: '.numtolocal($row['total'],get_fild_data('tbl_currency','1','symbol')); ?></strong><br><strong><?php //echo 'D: '.numtolocal($cashst,get_fild_data('tbl_currency','1','symbol')); ?></strong></td>                                           
                                                <td class="db-noti-action-links"><a  href="" class="btn btn-flat bg-purple btn-xs popup" data-callback="">View</a> </td>                                          
                                            </tr>
                                        <?php } ?>
                                <!-- <tr>
                                    <td width="30px" class="db-noti-icons"><i class="fa fa-shopping-cart"></i></td> 
                                    <td>Sale <span class="label label-success"><?php echo $row['invno'];?></span> &nbsp;<?php if($row['type']=='SU'){echo get_fild_data('tbl_supplier',$row['supid'],'name');}else{echo get_fild_data('tbl_customer',$row['supid'],'name');}?></span><strong><?php echo 'T: '.numtolocal($row['total'],get_fild_data('tbl_currency','1','symbol')); ?></strong><br><strong><?php echo 'D: '.numtolocal($cashst,get_fild_data('tbl_currency','1','symbol')); ?></strong></td>                                           
                                    <td class="db-noti-action-links"><a  href="" class="btn btn-flat bg-purple btn-xs popup" data-callback="refresh_dashboard">View</a> <a   class="btn btn-flat bg-purple  btn-xs" onclick="delete_db_notification(77,this)"><i class="fa fa-trash" style="color:#fff"></i></a></td>                                          
                                </tr> -->
                            </table>
                        </div>
                    </div>

                </div> 
                <div class="box box-solid">
                    <div class="box-header with-border">
                      <h3 class="box-title">Due Purchase Cost</h3>
                   </div>
                    <div class="box-body">   
                        <div class="col-md-12 table-responsive">
                            <table class="table table-bordered table-striped" id="datarec">
                                <thead>
                                   <tr>
                                      <th style="width:40px; text-align:center;">SN</th>   
                                      <th>Invoice</th> 
                                      <!-- <th>Name</th>    -->
                                      <th class="text-center">Total</th>    
                                      <th>Due</th>   
                                   </tr>
                                </thead>   
                            
                                <tbody>
                                    <?php 
                                        $i=0;
                                        if($uty=='1'){ 
                                            $sql="SELECT * FROM tbl_purchase ORDER BY apdate DESC,id DESC limit 5"; 
                                        }else{
                                            $sql="SELECT * FROM tbl_purchase WHERE brid='$brid'ORDER BY apdate DESC,id DESC limit 5"; 
                                        } 
                                        $query=mysqli_query($con,$sql)or die(mysqli_error($con));
                                        while ($row=mysqli_fetch_array($query)){
                                            $i=$i+1;
                                            $invno=$row['invno'];    
                // $cashst=($row['total']-get_invcash($invno,'P'));    
                                    ?>
                                    <tr>
                                        <td><?php echo $i;?></td>
                                        <td><?php echo $row['invno'];?></td>
                                        <!-- <td>
                                            <?php if($row['type']=='SU'){echo get_fild_data('tbl_supplier',$row['supid'],'name');}else{echo get_fild_data('tbl_customer',$row['supid'],'name');}?>
                                        </td> -->
                                        <td>
                                            <?php echo numtolocal($row['total'],get_fild_data('tbl_currency','1','symbol')); ?> 
                                        </td>
                                        <td>
                                            <?php echo $cashst=($row['total']-get_invcash($invno,'P'));?>
                                        </td>
                                    </tr>
                                    <?php }?>
                                </tbody>   
                            </table>
                        </div>
                    </div>
                </div>
                <div class="box box-solid">
                    <div class="box-header with-border">
                      <h3 class="box-title">Due Sales Cost</h3>
                   </div>
                    <div class="box-body">   
                        <div class="col-md-12 table-responsive">
                            <table class="table table-bordered table-striped" id="datarec">
                                <thead>
                                   <tr>
                                      <th style="width:40px; text-align:center;">SN</th>   
                                      <th>Invoice</th>   
                                      <th class="text-center">Total</th>    
                                      <th>Due</th>   
                                   </tr>
                                </thead>    
                                <tbody>
                                    <?php 
                                    $j=0;
                                        if($uty=='1'){ 
                                            $sql="SELECT * FROM tbl_sales ORDER BY apdate DESC,id DESC limit 5"; 
                                        }else{
                                            $sql="SELECT * FROM tbl_sales WHERE brid='$brid' ORDER BY apdate DESC,id DESC limit 5"; 
                                        } 
                                        $query=mysqli_query($con,$sql)or die(mysqli_error($con));
                                        while ($row=mysqli_fetch_array($query)){
                                            $j=$j+1;
                                            $invno=$row['invno'];    
                //$cashst=($row['total']-get_invcash($invno,'P'));    
                                    ?>
                                    <tr>
                                        <td><?php echo $j;?></td>
                                        <td><?php echo $row['invno'];?></td>
                                        <td><?php echo numtolocal($row['total'],get_fild_data('tbl_currency','1','symbol')); ?></td>
                                        <td><?php echo $cashst=($row['total']-get_invcash($invno,'P')); ?></td>
                                    </tr>
                                <?php }?>
                                </tbody>   
                            </table>
                        </div>
                    </div>
                </div>


            </div>    

<div class="col-md-8 shortcuts">
<div class="row">
<div class="col-md-11">
<table >
<tr>
<td></td>
<form action="home.php"  method="post" accept-charset="utf-8">
<td style="width: 123px;">
<div class="form-group">
<label>Date From</label>
<div class="input-group date datetimepicker">
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
<input type="text" value="<?php if($start){echo $start; }else{echo get_fild_data('tbl_limitset',1,'strDate');}?>" name='fdate'  class="form-control" placeholder="FROM DATE" readonly="true"  />
</div>
</div>
</td>
<td style="width: 123px;">
<div class="form-group">
<label>Date To</label>
<div class="input-group date datetimepicker">
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
<input type="text" value="<?php if($end){echo $end;}else{echo $today;} ?>"  class="form-control"  name='tdate'  placeholder="TO DATE" readonly="true"  />
</div>
</div>
</td>
<td>
<div class="form-group" style="padding-top: 23px;">
<input type="submit" value="Submit"  class="btn btn-flat bg-purple "  />
</div>
</td>
</form>
<form action="home.php"  method="post" accept-charset="utf-8">
<td>
<div class="form-group" style="padding-top: 23px;">
<input type="hidden" value="today" name='today' class="btn btn-flat bg-purple "  />
<input type="submit" value="Today"  class="btn btn-flat bg-purple "  />
</div>
</td>
</form> 
<form action="home.php"  method="post" accept-charset="utf-8">
<td>
<div class="form-group" style="padding-top: 23px;">
<input type="hidden" value="week" name='week' class="btn btn-flat bg-purple "  />
<input type="submit" value="This Week"  class="btn btn-flat bg-purple "  />
</div>
</td>
</form>
<form action="home.php"  method="post" accept-charset="utf-8">
<td>
<div class="form-group" style="padding-top: 23px;">
<input type="hidden" value="month" name='month' class="btn btn-flat bg-purple "  />
<input type="submit" value="This Month"  class="btn btn-flat bg-purple "  />
</div>
</td>
</form>
<form action="home.php"  method="post" accept-charset="utf-8">
<td>
<div class="form-group" style="padding-top: 23px;">
<input type="hidden" value="year" name='year' class="btn btn-flat bg-purple "  />
<input type="submit" value="This Year"  class="btn btn-flat bg-purple "  />
</div>
</td>
</form>
</tr>
</table>
   
</div>
</div>
    <div class="row">
        <div class="col-md-3 col-xs-6">
            <!-- small box -->
            <a href="">
                <div class="small-box">
                    <div class="ribbon"><span>Overall</span></div>
                    <?php    
                    if($uty=='1'){ 
                        if($start && $end){
                            $result= mysqli_query($con, "SELECT SUM(total) AS value_sum FROM tbl_purchase Where apdate BETWEEN '$start' AND '$end'"); 
                        }else{
                            $result= mysqli_query($con, "SELECT SUM(total) AS value_sum FROM tbl_purchase"); 
                        }

                       
                    }else{
                        if($start && $end){
                           $result= mysqli_query($con, "SELECT SUM(total) AS value_sum FROM tbl_purchase  WHERE brid='$brid' AND apdate BETWEEN '$start' AND '$end'"); 
                        }else{
                            $result= mysqli_query($con, "SELECT SUM(total) AS value_sum FROM tbl_purchase  WHERE brid='$brid'");

                        }
                    }                

                    $row = mysqli_fetch_assoc($result); 
                    $sum = $row['value_sum'];
                    ?>
                    
                    <div class="inner">
                        <h4 id=""><?php echo $sum ?></h4>
                        <p>Purchase</p>
                    </div>
                    <div class="icon">
                        <i class="fa fa-handshake-o"></i>
                    </div>
                    

                </div>
            </a>
        </div>
        <div class="col-md-3 col-xs-6">
            <!-- small box -->
            <a href="">
                <div class="small-box ">
                    <div class="ribbon"><span>Overall</span></div>
                    <?php 
                    
                    if($uty=='1'){ 
                        if($start && $end){
                            $result1= mysqli_query($con, "SELECT SUM(total) AS value_sum FROM tbl_sales Where apdate BETWEEN '$start' AND '$end'"); 
                        }else{
                            $result1= mysqli_query($con, "SELECT SUM(total) AS value_sum FROM tbl_sales"); 
                        }

                       
                    }else{
                        if($start && $end){
                           $result1= mysqli_query($con, "SELECT SUM(total) AS value_sum FROM tbl_sales  WHERE brid='$brid' AND apdate BETWEEN '$start' AND '$end'"); 
                        }else{
                            $result1 =mysqli_query($con, "SELECT SUM(total) AS value_sum FROM tbl_sales  WHERE brid='$brid'");

                        }
                    }               

                    $row1 = mysqli_fetch_assoc($result1); 
                    $sum1 = $row1['value_sum'];
                    ?>
                    
                    <div class="inner">
                        <h4 id=""><?php echo $sum1 ?></h4>
                        <p>Sales</p>
                    </div>
                    <div class="icon">
                        <i class="fa fa-sun-o"></i>
                    </div>
                </div>
            </a>

        </div>

        <div class="col-md-3 col-xs-6">

            <a href="">
                <!-- small box -->
                <div class="small-box ">
                    <div class="ribbon"><span>Overall</span></div>
                    <div class="inner">
                        <?php

                            if($uty=='1'){ 
                                if($start && $end){
                                    $result6= mysqli_query($con, "SELECT SUM(amount) AS value_sum FROM tbl_expenses Where apdate BETWEEN '$start' AND '$end'"); 
                                }else{
                                    $result6= mysqli_query($con, "SELECT SUM(amount) AS value_sum FROM tbl_expenses"); 
                                }

                            
                            }else{
                                if($start && $end){
                                $result6= mysqli_query($con, "SELECT SUM(amount) AS value_sum FROM tbl_expenses  WHERE brid='$brid' AND apdate BETWEEN '$start' AND '$end'"); 
                                }else{
                                    $result6=  mysqli_query($con, "SELECT SUM(amount) AS value_sum FROM tbl_expenses  WHERE brid='$brid'");

                                }
                            }

                        $row6 = mysqli_fetch_assoc($result6); 
                        $sum6 = $row6['value_sum'];
                        ?>
                        <h4 id=""><?php echo $sum6 ?></h4>
                        <p>Expense</p>
                    </div>
                    <div class="icon">
                        <i class="fa fa-rocket"></i>
                    </div>
                </div>
            </a>

        </div>
        <div class="col-md-3 col-xs-6">
            <a href="">
                <!-- small box -->
                <div class="small-box">
                    <div class="ribbon"><span>Overall</span></div>
                    <div class="icon">
                        <i class="fa fa-shopping-cart"></i>
                    </div>
                    <div class="inner">
                  
                        <?php 
                            $twvt=0;      
                    
                            if($start && $end){
                                $rcdebit=get_goupvalnew('5','D','D','C',$start,$end);
                                $rccredit=get_goupvalnew('5','C','D','C',$start,$end);
                                $rcnet=($rccredit-$rcdebit);
                                $ecdebit=get_goupvalnew('4','D','D','C',$start,$end);
                                $eccredit=get_goupvalnew('4','C','D','C',$start,$end);
                                $ecnet=($ecdebit-$eccredit);

                                $twvt=($rcnet-$ecnet);
                                  
                             }else{
                                    $start=get_fild_data('tbl_limitset',1,'strDate');
                                    $end=get_fild_data('tbl_limitset',1,'expDate');

                                    $rcdebit=get_goupvalnew('5','D','D','C',$start,$end);
                                    $rccredit=get_goupvalnew('5','C','D','C',$start,$end);
                                    $rcnet=($rccredit-$rcdebit);
                                    $ecdebit=get_goupvalnew('4','D','D','C',$start,$end);
                                    $eccredit=get_goupvalnew('4','C','D','C',$start,$end);
                                    $ecnet=($ecdebit-$eccredit);
                                    $twvt=($rcnet-$ecnet);
                              
 
                             }              

                     

                        ?>
                        <?php if(get_pagesecurity('30','views','P')){?> 
                            <h4 id=""><?php echo number_format(($twvt- $sum6),2); ?></h4>
                        <?php } ?>
                        <p>Profit & Loss</p>
                    </div>
                </div>
            </a>
        </div>
        <div class="clearfix"></div>
    </div>


    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box box-solid">
                    <div class="box-header with-border">
                      <h3 class="box-title">Low Stock Product</h3>
                   </div>
                   <div class="box-body">   
                        <div class="table-responsive">
                        <div class="cart cart-sm"> 
                        <div class="invhold scrol-y" id="invhold" >     
                            <table class="table table-bordered table-striped" >
                                <thead>
                                    <tr>
                                        <th style="width:40px;" class="text-center">SN</th>
                                        <th>Name</th>
                                        <th>SKU</th>          
                                        <th class="text-center">In-Stock</th>   
                                    </tr>   
                                </thead>
    
                                <tbody id="tradata">
                                    <?php
                                    $sql="SELECT pid,name,pcode,SUM(avqty) AS avqty FROM tbl_brstock GROUP BY pid ORDER BY name ASC limit 20";    
                                    $query=mysqli_query($con,$sql)or die(mysqli_error($con));
                                    while ($row=mysqli_fetch_array($query)){
                                       
                                        if($row['avqty']<2) {  
                                        ?>
                                        
                                        <tr>
                                            <td class="text-center" ><?php echo count_id();?></td>
                                            <td><b class="prodetail" id="PI_<?php echo $row['pid'];?>" style="cursor: pointer;"><?php echo $row['name']; ?></b></td>
                                            <td><?php echo $row['pcode']; ?></td>
                                            <td class="text-center"><?php echo $row['avqty']; ?></td>     
                                        </tr>    
                                    <?php }} ?>     
                                </tbody>
                            </table>
                            </div> 
                            
                        </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <br>
        <div class="row">
            <div class="col-md-6">
                
            </div>
            <div class="col-md-6">
                
            </div>

        </div>
         
    </section>                                   
</div>


</div>
</div>
</div>
</div>
<?php } ?>
<?php include('../layout/quick.php');?>   
<style>
.cash_balance_box{
    min-height: 71px;
    margin-bottom: 1px;
}
.cash_balance_box .info-box-icon{
    height: 71px;
    width: 50px;
    line-height: 71px;
    font-size: 18px;
}
.cash_balance_box .info-box-content{
    margin-left: 50px;
    text-transform: uppercase;
}
.cash_balance_box .info-box-number{
    font-size: 15px;
}
#overview_services th{
    padding: 5px;
}
</style>

<!-- /.row (main row) -->

</section>   
<!-- /.main content -->    
</div>

<?php include('../layout/footer.php');?>
<!-- page script -->
<script src="../plugins/chart/Chart.js"></script>
<input type='hidden' id='current_page_name' value='dashboard'/>
  
<!-- /page script -->
</html>