<?php
include('modals/modalprojects.php');
$d1=$_GET['d1'];
$d2=$_GET['d2'];
?>

<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h5 class="m-0">School's Expenditure</h5>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item">Dashboard</li>
              <li class="breadcrumb-item ">Expenses</li>
              <li class="breadcrumb-item active">Other Expenses</li>
            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    
    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-md-4 col-sm-4 col-4">
            <div class="info-box">
              <span class="info-box-icon bg-info"><i class="far fa-dollar"></i></span>
              <?php
                include('../include/config.php');
                 $sql=mysqli_query($con,"SELECT * FROM Expenses where Month='$month' AND Cat='Administration' AND Year='$year'");
                $num=mysqli_fetch_array($sql);
                if($num>0)
                {
                $result = mysqli_query($con,"SELECT SUM(Amount) as tcost FROM Expenses where Month='$month' AND Cat='Administration' AND Year='$year'");
                while($row = $result->fetch_assoc()) 
                {
                  $tot=$row["tcost"];
                }
                }else
                {
                 $tot='0';
                }
                ?>
              <div class="info-box-content">
                <span class="info-box-text">Administration Cost (<small><?php echo $month;?></small>)</span>
                <h4 class="info-box-number budge"><?php echo $tot.' Ksh'; ?></h4>
              </div>
              <!-- /.info-box-content -->
            </div>
            <!-- /.info-box -->
          </div>
            <div class="col-md-4 col-sm-4 col-4">
            <div class="info-box">
              <span class="info-box-icon bg-success"><i class="far fa-dollar"></i></span>
              <?php
                include('../include/config.php');
                $sql=mysqli_query($con,"SELECT * FROM Expenses where Month='$month' AND Cat='Meals and Boarding' AND Year='$year'");
                $num=mysqli_fetch_array($sql);
                if($num>0)
                {
                $result = mysqli_query($con,"SELECT SUM(Amount) as tcostt FROM Expenses where Month='$month' AND Cat='Meals and Boarding' AND Year='$year'");
                while($row = $result->fetch_assoc()) 
                {
                  $tot=$row["tcostt"];
                }
                }else
                {
                    $tot='0';
                }
                ?>
              <div class="info-box-content">
                <span class="info-box-text">Meals & Boarding Cost (<small><?php echo $month;?></small>)</span>
                <h4 class="info-box-number budge"><?php echo $tot.' Ksh'; ?></h4>
              </div>
            </div>
          </div>
          
          </div>
         </div>
    </section>
         
    <section class="content" style="text-decoration:none;font-weight:normal;font-family:Times;font-size:12px">
           <div class="row">
              <div class="col-md-4"> 
              <h5 class="m-0 text-success">Add new Expenditure</h5>
                <div class="modal-body">
                  <form action="" method="POST" onsubmit="return false">
                      <div class="row">  
                    <div class="col-md-10 col-10"> 
                    <div class="form-group">
                    <label for="inputStatus">Expense Category <span class="requireds">*</span></label>
                     <select id="expecat" class="form-control custom-select">
                            <option selected value="">Select ---</option>
                            <?php
                            $sql = "SELECT * FROM expenses order by Item ASC";
                            $result = $con->query($sql);
                            while($row = $result->fetch_assoc()) 
                            {
                            ?>
                            <option value="<?php echo $row['Count']; ?>"><?php echo $row['Item']; ?>
                            </option>
                            <?php
                            }
                            ?>
                     </select>
                    </div>
                    </div>
                    
                    <div class="col-md-2 col-2"> 
                    <div class="form-group">
                         <label for="inputStatus">.</label>
                    <i class="fa fa-plus btn btn-primary" id="addimpitems"></i>
                    </div>
                    </div>
                     </div>
                      
                      <div class="form-group">
                      <label for="inputProjectLeader">Expenditure Date <span class="requireds">*</span></label>
                      <input type="date" style="font-weight:bold" class="form-control" name="" id="expedates" required />
                     </div>
                     
                     <div class="form-group">
                      <label for="inputProjectLeader">Item <span class="requireds">*</span></label>
                      <input type="text" style="" class="form-control" name="" id="parti" required />
                     </div>
                     
                     <div class="form-group">
                      <label for="inputProjectLeader"><Strong>Amount Spent (KES)</Strong> <span class="requireds">*</span></label>
                      <input type="text" style="font-weight:1000" class="form-control text-danger" name="" id="expeamount" required  />
                     </div>
                     
                     <div class="form-group">
                    <label for="inputStatus">Mode of Payment <span class="requireds">*</span></label>
                    <select id="paymode" class="form-control custom-select" required>
                    <option selected disabled value="">Select ---</option>
                    <option value="Cash">Cash</option>
                    <option value="Mpesa">Mpesa</option>
                    <option value="Cheque">Cheque</option>
                    </select>
                    </div>
                     
                     <div class="form-group">
                      <label for="inputProjectLeader">Cheque No./Transaction Details</label>
                      <input type="text" style="" class="form-control" name="" id="chequede"/>
                     </div>
                     
                      <div class="form-group">
                      <label for="inputProjectLeader">Account Debited</label>
                       <select id="bankdebited" class="form-control custom-select">
                            <option selected value="">Select ---</option>
                            <?php
                            $sql = "SELECT * FROM bankdata";
                            $result = $con->query($sql);
                            while($row = $result->fetch_assoc()) 
                            {
                            ?>
                            <option value="<?php echo $row['id']; ?>"> <?php echo $row['Bname'].'->'; ?> <small><?php echo $row['Account']; ?> </small>
                            </option>
                            <?php
                            }
                            ?>
                     </select>
                       </div>
                   
                      <div class="form-group">
                      <label for="inputProjectLeader">Any Description</label>
                      <textarea class="form-control" id="expemoredetails"> </textarea>
                      </div>
                  
                       <div class="form-group">
                         <label id="mes" style="color:#FFF">.</label> <br />
                        <button style="position:relative;margin-bottom:10px" class="btn btn-success btn-sm pull-right btn-block" id="saveexpenses">Save <i class="fa fa-save"></i>
                        </button>
                       </div>
                  </form>
                </div>
              </div>
               
             <div class="col-md-8">  
             <div class="table-responsive" id="divbus">
                 <table class="table">
                     <tr class="alert-warning">
                         <td>Filter By Month</td>
                         <td>
                            <div class="form-group">
                            <input type="Month" id="monn1" class="form-control" />
                            </div>
                         </td>
                         <td>
                             <button class="btn btn-success btn-sm" id="filtersasa"><i class="fa fa-calendar"></i></button>
                         </td>
                     </tr>
                 </table>
                 
                 <table class="table table-bordered table-sm table-dark" id="tableexpenditure" >
                     <thead>
                         <tr>
                             <th>#</th>
                             <th>Particular </th>
                             <th>Amount</th>
                             <th>Category</th>
                             <th>Account Debited</th>
                             <th>Expense Date</th>
                             <th>Cheque No/Transaction Details</th>
                             <th class="leavethiss">Action</th>
                         </tr>
                     </thead>
                     <tbody>
                        <?php
                        //include('../include/config.php');
                        $d1=$_SESSION['mani'];
                        if($d1=='' || $d1==null)
                        {
                        $sql = "select Expenses.*,expenses.Item as ite,bankdata.Bname as bnk,bankdata.Account as aka from
                        Expenses left join expenses ON expenses.Count=Expenses.Cat join bankdata ON bankdata.id=Expenses.Account where Expenses.Month='$month' AND Expenses.Year='$year' order by ExpDate DESC";

                        }else
                        {
                            $yrr=substr($d1, 0, 4);
                            $mm=substr($d1, 5, 2);
                            
                            if($mm=='01'){$mm='January';}
                            if($mm=='02'){$mm='February';}
                            if($mm=='03'){$mm='March';}
                            if($mm=='04'){$mm='April';}
                            if($mm=='05'){$mm='May';}
                            if($mm=='06'){$mm='June';}
                            if($mm=='07'){$mm='July';}
                            if($mm=='08'){$mm='August';}
                            if($mm=='09'){$mm='September';}
                            if($mm=='10'){$mm='October';}
                            if($mm=='11'){$mm='November';}
                            if($mm=='12'){$mm='December';}  

                        $sql = "select Expenses.*,expenses.Item as ite,bankdata.Bname as bnk,bankdata.Account as aka from
                        Expenses left join expenses ON expenses.Count=Expenses.Cat join bankdata ON bankdata.id=Expenses.Account where Expenses.Month='$mm' AND Expenses.Year='$yrr' order by ExpDate DESC";
                        }
                        $result = $con->query($sql);
                        if ($result->num_rows > 0) 
                        {
                            $cnt='1';
                            while($row = $result->fetch_assoc()) 
                            {
                            ?>	
                       <tr>
                        <td><?php echo $cnt; ?></td>
                        <td><?php echo $row["Particular"]; ?></td>
                        <td class="alert-success"><?php echo $row["Amount"]; ?>
                        </td>
                        <td><?php echo $row["ite"]; ?></td>
                         <td><?php echo $row["bnk"]; ?> (<?php echo $row["aka"]; ?>)</td>
                        <td class="bg-success"><strong><?php echo $row["ExpDate"]; ?></strong></td>
                        <td><?php echo $row["Details"]; ?>
                        </td>
                        <td class="leavethiss">
                            <input type="hidden" name="bid" class="expeinputid" value="<?php echo $row["id"]; ?>" />
                         <i class="fa fa-pen btn-xs text-info expendit" id="">
                         </i>
                        |
                        <i class="fa fa-trash btn-xs text-danger deleteexpenses" ></i>
                        </td>
                            </tr>
                            <?php
                            $cnt=$cnt+1;
                            }
                        }
                            ?>
                     </tbody>
                 </table> 
              </div>
              <button class="btn btn-sm btn-primary pull-right" id="expor">Export to Excel <i class="fa fa-cloud-download"></i></button>
              <hr />
        <form method="POST" target="_blank" action="expelink.php" >
        <div class="row">
             <div class="col-md-4">
               <div class="form-group">
                <label for="inputProjectLeader" class="pull-right">Generate Report for: </label>
              </div>
              </div>
               <div class="col-md-3">
                <div class="form-group">
                        <select id="impres" name="bid" class="form-control custom-select">
                            <option selected value="">Select ---</option>
                            <?php
                            $sql = "SELECT * FROM bankdata";
                            $result = $con->query($sql);
                            while($row = $result->fetch_assoc()) 
                            {
                            ?>
                            <option value="<?php echo $row['id']; ?>"> <?php echo $row['Bname'].'->'; ?> <small><?php echo $row['Account']; ?> </small>
                            </option>
                            <?php
                            }
                            ?>
                     </select>
                </div>
            </div>
            
             <div class="col-md-3">
                <div class="form-group">
                      <input type="month" style="" class="form-control" name="mmmm" id="" required />
                </div>
            </div>
            <div class="col-md-2"> 
             <div class="form-group">
                    <button style="position:relative;margin-bottom:10px" name='btnfilter' class="btn btn-success btn-sm pull-right" id="">Filter <i class="fa fa-filter"></i>
                    </button>
            </div>
            </div>
        </div>
        </form>
              </div>
              
            </div>
    </section>
</div>

