<?php include("parent/callMe.php"); ?>
<?php  include("sessioncheck.php"); ?>
<?php

  
    $m->where("session_id", $_REQUEST['session']);
    $dateSession=$m->get("edu_session_config");

    $yr=explode("-",$dateSession[0]['session_year']);

   

$session;
$PaidDateSession;
    
    if($_GET["session"]!=NULL ){
      $session= $_GET["session"];
      $PaidDateSession= $yr[0];
    }
    else{
      $session= $_SESSION['current_session'];
      $PaidDateSession= date("Y");
    }

$default;
    
    if($_GET["month"]!=NULL ){
      $default= $_GET["month"];   
     $default = sprintf("%02d", $default);

    }
    else{
      $default= date("m");
      $default = sprintf("%02d", $default);
    }
  

    $m->where("Month_Id", $default);
    $month_name=$m->get("eduportal_month_config");

    $paidDate=$PaidDateSession."-".$default;

    //echo $paidDate;

   /* $session=$_REQUEST["session"];*/
    $branch=$_SESSION['branch'];
   $qr="SELECT SUM(paid_amount) as total, SUM(paid_again_amount) as again_total FROM employee_payroll where branch_id ='$branch' and session_id='$session' and paid_date like '$paidDate%'";
  // echo "$qr";
   $salary=$m->query($qr);

   $qr="SELECT SUM(expense_amount) as total FROM eduportal_expense_detail where branch_id ='$branch' and status = '0' and month_id='$default'  and session_id='$session'";
   //echo "$qr";
   $expense=$m->query($qr);


$days=cal_days_in_month(CAL_GREGORIAN, $default, date("Y"));

$from=date("Y-$default-01");
$to=date("Y-$default-$days");
  

$qr1="SELECT SUM(collected_amount) as received, SUM(collected_fine) as total_fine FROM eduportal_monthly_challan WHERE branch_id='$branch' AND session_id='$session' and paid_status='1' ";

  $qr1 .= " AND paying_date BETWEEN '" .$from. "' AND '" .$to. "' ";
//print_r($qr1);
  $revenue = $m->query($qr1);



  $qr2="SELECT SUM(paid_again_amount) as againreceived FROM eduportal_monthly_challan WHERE branch_id='$branch' AND session_id='$session' and paid_status='1' ";

  $qr2 .= " AND paid_again_date BETWEEN '" .$from. "' AND '" .$to. "' ";
//print_r($qr);
  $againReceivedrevenue = $m->query($qr2);




  $bank="SELECT SUM(collected_amount) as received, SUM(collected_fine) as total_fine FROM eduportal_monthly_challan WHERE branch_id='$branch' AND paid_by='BankAccount' AND session_id='$session' and paid_status='1' ";

  $bank .= " AND paying_date BETWEEN '" .$from. "' AND '" .$to. "' ";
//print_r($qr1);
  $BankAccount = $m->query($bank);


  $bankAgain="SELECT SUM(paid_again_amount) as againreceived FROM eduportal_monthly_challan WHERE branch_id='$branch' AND paid_by='BankAccount' AND session_id='$session' and paid_status='1' ";

  $bankAgain .= " AND paying_date BETWEEN '" .$from. "' AND '" .$to. "' ";
//print_r($qr1);
  $BankAccountAgain = $m->query($bankAgain);



  $easy="SELECT SUM(collected_amount) as received, SUM(collected_fine) as total_fine FROM eduportal_monthly_challan WHERE branch_id='$branch' AND paid_by='Easypaisa' AND session_id='$session' and paid_status='1' ";

  $easy .= " AND paying_date BETWEEN '" .$from. "' AND '" .$to. "' ";
//print_r($easy);
  $easyPaisa = $m->query($easy);
 

  $easyAgain="SELECT SUM(paid_again_amount) as againreceived FROM eduportal_monthly_challan WHERE branch_id='$branch' AND paid_by='Easypaisa' AND session_id='$session' and paid_status='1' ";

  $easyAgain .= " AND paying_date BETWEEN '" .$from. "' AND '" .$to. "' ";
//print_r($qr1);
  $easyPaisaAgain = $m->query($easyAgain);




  $Jazz="SELECT SUM(collected_amount) as received, SUM(collected_fine) as total_fine FROM eduportal_monthly_challan WHERE branch_id='$branch' AND paid_by='JazzCash' AND session_id='$session' and paid_status='1' ";

  $Jazz .= " AND paying_date BETWEEN '" .$from. "' AND '" .$to. "' ";
//print_r($qr1);
  $JazzCash = $m->query($Jazz);


  $JazzAgain="SELECT SUM(paid_again_amount) as againreceived FROM eduportal_monthly_challan WHERE branch_id='$branch' AND paid_by='JazzCash' AND session_id='$session' and paid_status='1' ";

  $JazzAgain .= " AND paying_date BETWEEN '" .$from. "' AND '" .$to. "' ";
//print_r($qr1);
  $JazzCashAgain = $m->query($JazzAgain);


   $Cash="SELECT SUM(collected_amount) as received, SUM(collected_fine) as total_fine FROM eduportal_monthly_challan WHERE branch_id='$branch' AND paid_by='Cash' AND session_id='$session' and paid_status='1' ";

  $Cash .= " AND paying_date BETWEEN '" .$from. "' AND '" .$to. "' ";
//print_r($qr1);
  $CashReceived = $m->query($Cash);


  $CashAgain="SELECT SUM(paid_again_amount) as againreceived FROM eduportal_monthly_challan WHERE branch_id='$branch' AND paid_by='Cash' AND session_id='$session' and paid_status='1' ";

  $CashAgain .= " AND paying_date BETWEEN '" .$from. "' AND '" .$to. "' ";
//print_r($qr1);
  $CashReceivedAgain = $m->query($CashAgain);
 



 /*   $qr="SELECT SUM(collected_amount) as total, SUM(paid_again_amount) as againPaid, SUM(collected_fine) as total_fine FROM eduportal_monthly_challan where branch_id ='$branch' and month_id='$default'  and session_id='$session'";
  // echo "$qr";
   $revenue=$m->query($qr);*/

   $b=$_SESSION['branch'];

    $qr="SELECT SUM(amount) AS total  FROM eduportal_revenue_details WHERE branch_id='$b' and session_id='$session' and paid_date like '%-$default-%'";

  //echo $qr;
    $Revenue = $m->query($qr);

    $RevenueMonthly=$Revenue[0]['total'];


 
     ?>


<!DOCTYPE html>
<html lang="en">
  <head>
    
    <title>Monthly Profit & Loss report</title>
    <?php include('RootMe/css_links.php');?>
   
  </head>
  




  <body class="nav-md">
    <div class="container body">
      <div class="main_container">
        <div class="col-md-3 left_col">
     
            <?php include('RootMe/sidebar.php');?>

              <?php include('RootMe/header.php');?>
     
        <!-- page content -->
          <div class="right_col" role="main">
          <div class="" style="background-color: white;">
           
  <section class="add_sub_agent" id="table_sub_agent">
  <div class="container">


        

                                 
 <!-- page content -->
         <div class="title_left col-md-12 col-sm-12 col-xs-12 ">
                <h3>Profit & Loss Report List</h3>
              </div>
          <div class="">
            <div class="page-title">
             

             
<form action="profitloss_report.php?<?php echo session_id().session_id().session_id().session_id().session_id(); ?>">
        <div class="col-md-12">
                 <div class="col-md-3 col-sm-12 col-xs-12" style="padding: 8px;">
                <div class="form-group ">
                  <label class="required">Session</label>
                    <select name="session" class="form-control"  style="height:40px;">

                              
                        <?php

                       $match;
                        if(!isset($_REQUEST['session'])){
                          $match=$_SESSION['current_session'];
                        }else { $match=$_REQUEST['session']; }
                       
                                 
                        $WorkAssignList = $m->get("edu_session_config");
                        foreach($WorkAssignList as $WorkAssignListVal)
                        { ?>

                           <option <?php if($match==$WorkAssignListVal['session_id']){echo 'selected';}?> value="<?php echo $WorkAssignListVal['session_id']; ?>"><?php echo $WorkAssignListVal['session_year']; ?></option>


                                <?php
                        } ?>

                    </select>
                </div>
              </div>

               <div class="col-md-3 col-sm-12 col-xs-12" style="padding: 8px;">
                <div class="form-group ">
                <label class="required">Month</label>
                <select style="height:40px;" name="month" id="month" class="form-control"  required>

                              <option value="">Select Month</option>
                        

                        <?php
                        $WorkAssignList = $m->get("eduportal_month_config");
                        foreach($WorkAssignList as $WorkAssignListVal)
                        { ?>

                           <option <?php if($default==$WorkAssignListVal['Month_Id']){echo 'selected';}?> value="<?php echo $WorkAssignListVal['Month_Id']; ?>"><?php echo $WorkAssignListVal['Month_Name']; ?></option>


                                <?php
                        } ?>

                    </select>
                </div>
              </div>
                   
             
                  <div class="col-md-1 " style="padding:8px;">
                <div class="form-group ">
                   <input type="hidden" name="addaccountAdmin" value="1">
                <button type="submit" class="btn btn-primary search_print_topmargin" >Search</button>
                </div>
              </div> 
              
               <div class="col-md-3 " style="padding:8px;">
                <div class="form-group ">
                <a href="print_profitloss_report.php?<?php echo session_id().session_id().session_id().session_id().session_id(); ?>&month=<?php echo $_GET['month'];?>&session=<?php echo $_GET['session'];?>" target="_blank" class="btn btn-primary search_print_topmargin"> Print Report  </a>
                </div>
              </div> 

        </div>                          
 </form>
 <?php 
$expense_amount=$expense[0]['total'] + $salary[0]['total']+$salary[0]['again_total'];
?>
                    <table id="datatable" class="table table-striped table-bordered">
                      <thead>
                        <tr style="background-color: lightgray;color: black;">
                          <th width="1%">S.No</th>
                          <th width="10%">Category</th>
                          <th width="10%" class="text-center">Amount</th>
                          
                        
                        </tr>
                          <tr>
                          <td width="1%">1</td>
                          <td width="10%">Expenses</td>
                          <td width="10%" class="text-center"><?php echo number_format($expense[0]['total']);?></td>
                                                  
                        </tr>
                        <tr>
                          <td width="1%">2</td>
                          <td width="10%">Wages/Salary</td>
                          <td width="10%" class="text-center"><?php echo number_format($salary[0]['total']+$salary[0]['again_total']);?></td>
                                                  
                        </tr>
                        <tr style="background-color: lightgray;color: black;">
                          
                          <td width="10%" colspan="2" class="text-center">Total Expenses</td>
                          <td width="10%" class="text-center"><?php echo number_format($expense_amount);?></td>
                                                  
                        </tr>

                       
                        <tr>

                           <td width="1%" rowspan="5">3</td>
                          <td width="10%" rowspan="5">Revenue</td>
                          <td width="10%">Cash:<?php echo number_format($CashReceived[0]['received']+$CashReceived[0]['total_fine']+$CashReceivedAgain[0]['againreceived']);?></td>
                        </tr>


                        <tr>

                           <td width="1%">Jazz Cash: <?php echo number_format($JazzCash[0]['received']+$JazzCash[0]['total_fine']+$JazzCashAgain[0]['againreceived']);?></td>
                          
                          
                        </tr>

                         <tr>

                           <td width="1%">Easypaisa: <?php echo number_format($easyPaisa[0]['received']+$easyPaisa[0]['total_fine']+$easyPaisaAgain[0]['againreceived']);?></td>
                         
                          
                        </tr>

                         <tr>

                           <td width="1%">Bank Account:

                            <?php echo number_format($BankAccount[0]['received']+$BankAccount[0]['total_fine']+$BankAccountAgain[0]['againreceived']);?>
                           </td>
                          
                          
                        </tr>


                        <tr style="background-color:lightgray;color: black;">

                           <td width="1%" class="text-center">Total Revenue:

                            <?php echo number_format($revenue[0]['received']+$revenue[0]['total_fine']+$againReceivedrevenue[0]['againreceived']);?>
                           </td>
                          
                          
                        </tr>
                       

                        <tr>
                          <td width="1%">4</td>
                          <td width="10%">Other Revenue</td>
                          <td width="10%" class="text-center"><?php echo number_format($RevenueMonthly);?></td>
                                                  
                        </tr>

                        <tr style="background-color: lightgray;color:black;">
                          
                          <td width="10%" colspan="2" class="text-center">Total Revenue</td>
                          <td width="10%" class="text-center"><?php echo number_format($revenue[0]['received']+$revenue[0]['total_fine']+$againReceivedrevenue[0]['againreceived']+$RevenueMonthly);?></td>
                                                  
                        </tr>
                        <tr class="bg-primary">
                          
                          <td width="10%" colspan="2" class="text-center">Balance</td>
                          <td width="10%" class="text-center"><?php echo number_format($RevenueMonthly+$revenue[0]['received']+$revenue[0]['total_fine']+$againReceivedrevenue[0]['againreceived'] - $expense_amount );?></td>
                                                  
                        </tr>
                      </thead>
                      <tbody>

                        

                      

                      </tbody>
                    </table>
                  </div>
                </div>
              

            
             
           
          </div>
        </div>
        <!-- /page content -->





  </div>
  <div class="clearfix"></div>
</section>          
    

           
          </div>
        </div>

            </div>
          </div>
        </div>
        <!-- /page content -->
  

    
  <?php include('RootMe/js_linksscript.php');?>
  </body>
</html>
