<?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;
$sessionID= $_SESSION['current_session'];
$PaidDateSession;
    
    if($_GET["year"]!=NULL ){
      $session= $_GET["session"];
      $PaidDateSession= $yr[0];
    }
    else{
      $session= $_SESSION['current_session'];
      $PaidDateSession= date("Y");
    }

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

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

    if ($_GET['year']!=NULL) {

      $year=$_GET['year'];
      
      } else {

        $year=date("Y");

      }
  

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

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

    //echo $paidDate;

   /* $session=$_REQUEST["session"];*/
    $branch=$_SESSION['branch'];
   $qr="SELECT SUM(paid_amount) as total, SUM(paid_again_amount) as again_total, SUM(paid_again_salary_amount) as again_salary_total FROM employee_payroll where branch_id ='$branch' 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 expense_date like '$paidDate%'  /*and session_id='$sessionID'*/ ";
   //echo "$qr";
   $expense=$m->query($qr);


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

$from=date("$year-$default-01");
$to=date("$year-$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($bank);
  $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 paid_again_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 paid_again_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 paid_again_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($Cash);
  $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 paid_again_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 '$paidDate%'";

  //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">

            <a href="<?php echo $_SESSION['dashboard'];?>">Dashboard </a> &nbsp; <i style="" class="fa fa-angle-double-right"></i>  &nbsp; 
                   
          Profit & Loss Report 
          
          <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">Year</label>
                    <select name="year" class="form-control"  style="height:40px;">

                       <?php for ($i=2018; $i < 2031 ; $i++) {   ?>   

                       <option <?php if($i==$year){echo "Selected";} ?> value="<?php echo $i;?>"><?php echo $i;?></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'];?>&year=<?php echo $_GET['year'];?>" 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']+$salary[0]['again_salary_total'];

?>
                    <table id="excelexpo" 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']+$salary[0]['again_salary_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']);?><span style="float: right;">

                            <a style="padding: 3px; margin-top: 0px; font-size: 12px;" href="#" target="_blank" class="btn btn-primary search_print_topmargin"> View Details  </a>
                          </span></td>
                        </tr>

  <!-- <a style="padding: 3px; margin-top: 0px; font-size: 12px;" href="print_receive_amount_bychannel.php?<?php echo session_id().session_id().session_id().session_id().session_id(); ?>&month=<?php echo $_GET['month'];?>&session=<?php echo $_GET['session'];?>&paidBy=Cash" target="_blank" class="btn btn-primary search_print_topmargin"> View Details  </a> -->
                        <tr>

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

                            <a style="padding: 3px; margin-top: 0px; font-size: 12px;" href="#" target="_blank" class="btn btn-primary search_print_topmargin"> View Details  </a>
                          </span></td>
                          
                          
                        </tr>

                         <tr>

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

                            <a style="padding: 3px; margin-top: 0px; font-size: 12px;" href="#" target="_blank" class="btn btn-primary search_print_topmargin"> View Details  </a>
                          </span></td>
                         
                          
                        </tr>

                         <tr>

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

                            <?php echo number_format($BankAccount[0]['received']+$BankAccount[0]['total_fine']+$BankAccountAgain[0]['againreceived']);?><span style="float: right;">

                            <a style="padding: 3px; margin-top: 0px; font-size: 12px;" href="#" target="_blank" class="btn btn-primary search_print_topmargin"> View Details  </a>
                          </span>
                           </td>
                          
                          
                        </tr>


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

                           <td width="1%" class="text-center">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">Profit</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 -->
  

    <script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
   
   <script src="js/FileSaver.min.js" type="text/javascript"></script>
  <script src="js/tableexport.min.js" type="text/javascript"></script>

  <script type="text/javascript">
    $('#excelexpo').tableExport();
  </script>
  <?php include('RootMe/js_linksscript.php');?>
  </body>
</html>
