<?php
$icc=$_SESSION['incomecate'];
?>

<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">Other Sources of Income</h5>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
                  <li class="breadcrumb-item">Dashboard</li>
                  <li class="breadcrumb-item ">Income</li>
                  <li class="breadcrumb-item active">Other Earnings</li>
            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    
    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-md-5 col-sm-5 col-5">
            <div class="info-box">
              <span class="info-box-icon bg-info"><i class="far fa-dollar"></i></span>
              <?php
                include('../include/config.php');
                $result = mysqli_query($con,"SELECT SUM(Amount) as ama FROM Otherincome where Month='$month'");
                while($row = $result->fetch_assoc()) 
                {
                  $totincome=$row["ama"];
                }
                ?>
              <div class="info-box-content">
                <span class="info-box-text">Income from other sources  (<small><?php echo $month;?></small>)</span>
            <h4 class="info-box-number budge"><?php echo $totincome.' Ksh'; ?></h4>
              </div>
              <!-- /.info-box-content -->
            </div>
            <!-- /.info-box -->
          </div>
           <div class="col-md-3 col-sm-3 col-6">
            <div class="info-box">
              <div class="info-box-content">
                 <a href="index.php?page=Tour_Payment" class="btn btn-success">Tour Payment <i class="fa fa-bus" aria-hidden="true"></i></a>
              </div>
              <!-- /.info-box-content -->
            </div>
            <!-- /.info-box -->
          </div>
          
            <div class="col-md-3 col-sm-3 col-6">
            <div class="info-box">
              <div class="info-box-content">
                 <a href="index.php?page=Graduation" class="btn btn-primary">Graduation <i class="fa fa-graduation-cap" aria-hidden="true"></i></a>
              </div>
              <!-- /.info-box-content -->
            </div>
            <!-- /.info-box -->
          </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 alert-success">Add new Income</h5>
                <div class="modal-body panel-primary">
                  <form action="" method="POST" onsubmit="return false">
                     <div class="row">
                    <div class="col-md-10 col-10"> 
                    <div class="form-group">
                    <label for="inputStatus">Income Category <span class="requireds">*</span></label>
                     <select id="incomecati" class="form-control custom-select">
                            <option selected value="">Select ---</option>
                            <?php
                            $sql = "SELECT * FROM IncomeCat 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="addincomess"></i>
                    </div>
                    </div>
                   </div>
               
                  <div class="form-group">
                  <label for="inputProjectLeader">Source or Paid By? <span class="requireds">*</span></label>
                  <input type="text" class="form-control" name="" id="source" required />
                  </div>
                  
                    <div class="form-group">
                    <label for="inputStatus">Student Involved <span class="requireds"></span></label>
                     <select id="paidby" class="form-control custom-select">
                            <option selected value="">Select ---</option>
                            <?php
                            $sql = "SELECT * FROM pupils order by Names ASC";
                            $result = $con->query($sql);
                            while($row = $result->fetch_assoc()) 
                            {
                            ?>
                            <option value="<?php echo $row['count']; ?>"><?php echo $row['Names']; ?> <small><?php echo '('.$row['AdmNo'].')';?></small>
                            </option>
                            <?php
                            }
                            ?>
                     </select>
                    </div>
                      
                      <div class="form-group">
                      <label for="inputProjectLeader">Date <span class="requireds">*</span></label>
                      <input type="date" class="form-control" name="" id="otdate" required />
                      </div>
                      
                      <div class="form-group">
                      <label for="inputProjectLeader">Amount Received (Ksh) <span class="requireds">*</span></label>
                      <input type="text"  onkeypress="return /[0-9]/i.test(event.key)" class="form-control farmamount" max-length="6" name="" id="otamount" required />
                      </div>
                      
                      
                      <div class="form-group">
                      <label for="inputProjectLeader">Mode of payment <span class="requireds">*</span></label>
                      <select class="form-control" id="otmode">
                          <option value="">---</option>
                          <option value="Cash">Cash</option>
                          <option value="Mpesa">Mpesa</option>
                          <option value="Bank">Bank</option>
                      </select>
                      </div>
        
                      <div class="form-group">
                      <label for="inputProjectLeader">Account Credited</label>
                       <select id="otbank" 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">Cheque No/Receipt No (If any) </label>
                      <input type="text" class="form-control" name="" id="otdesc" />
                     </div>
                     
                      <div class="form-group">
                      <label for="inputProjectLeader">More Description </label>
                      <textarea class="form-control" id="otinfo"></textarea>
                     </div>
         
                     <div class="form-group">
                         <label id="otherincomemsg" style="color:#FFF">.</label> <br />
                         <button style="margin-right:0px;position:relative" class="btn btn-info btn-sm pull-right" id="btnsaveincomes">Submit <i class="fa fa-save"></i>
                        </button>
                     </div>
                  </form>
                </div>
              </div>
               
             <div class="col-md-8"> 
              <div class="col-md-10 col-10"> 
                    <div class="form-group">
                    <label for="inputStatus">Income Category <span class="requireds">*</span></label>
                     <select id="incomecateg" class="form-control custom-select">
                            <option selected value="">Select ---</option>
                            <?php
                            $sql = "SELECT * FROM IncomeCat 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
                            }
                            ?>
                             <option value="All">All</option>
                     </select>
                    </div>
                </div>
             <div class="table-responsive" id="divbus">
                 <table class="table table-bordered table-warning" id="tablefarmincome" >
                     <thead>
                         <tr>
                             <th>#</th>
                             <th>Particular</th>
                             <th>Source</th>
                             <th>Student Involved</th>
                             <th>Amount Earned (Ksh)</th>
                             <th>Paid Via</th>
                              <th>Bank Credited</th>
                             <th>Entry Date</th>
                             <th>Action</th>
                         </tr>
                     </thead>
                     <tbody>
                        <?php
                        if($icc=='' || $icc=='All')
                        {
                        
                        //include('../include/config.php');
                        $sql = "select Otherincome.*,bankdata.Bname as bnk, bankdata.Account as act,pupils.Names as stu,pupils.AdmNo as adm from Otherincome left join pupils ON pupils.count=Otherincome.Stud left join bankdata ON bankdata.id=Otherincome.Bank order by id DESC";
                        }else
                        {
                            
                         $sql = "select Otherincome.*,bankdata.Bname as bnk, bankdata.Account as act,pupils.Names as stu,pupils.AdmNo as adm from Otherincome left join pupils ON pupils.count=Otherincome.Stud left join bankdata ON bankdata.id=Otherincome.Bank where Otherincome.Cat='$icc' order by id 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><?php echo $row["stu"]; ?><small>(<?php echo $row["adm"]; ?>)</small></td>
                        <td class="alert-success"><?php echo $row["Date"]; ?>
                        </td>
                        <td class="alert-danger" ><strong class="bigger"><?php echo $row["Amount"]; ?></strong>
                        </td>
                        <td><strong><?php echo $row["Paymode"]; ?></strong></td>
                        <td><?php echo $row["bnk"]; ?> <small><?php echo $row["act"]; ?> </small>
                        </td>
                        <td>
                           <?php echo $row["Entrydate"]; ?>
                        </td>
                        <td>
                        <input type="hidden" name="otid" class="otid" value="<?php echo $row["id"]; ?>" />
                         <i class="fa fa-pen btn-xs text-info otheredit btn" id="otheredit"></i>
                        |
                        <i class="fa fa-trash btn-xs text-danger deleteotherincome btn" id="otherdelete">
                        </i>
                        </td>
                            </tr>
                            <?php
                            $cnt=$cnt+1;
                            }
                        }
                            ?>
                     </tbody>
                     
                     <tfooter>
                        <tr>
                        <td colspan="4"><strong>TOTAL (KES)</strong></td>
                        
                        <?php
                        if($icc=='' || $icc=='All')
                        {
                        ?>
                         <td><strong><em>...</em></strong></td>
                        <?php
                        }else
                        {
                            $query=mysqli_query($con,"select * from Otherincome where Cat='$icc'");
                            $num=mysqli_fetch_array($query);
                            if($num>0)
                            {     
                            $result = mysqli_query($con,"SELECT SUM(Amount) as ama FROM Otherincome where Cat='$icc'");
                            while($row = $result->fetch_assoc()) 
                            {
                            $totin=$row["ama"];
                            } 
                            ?>
                            <td><strong style="font-size::20px"><?php echo $totin;  ?></strong></td>
                            <?php
                            }else
                            {
                              ?>  
                               <td><strong>00</strong></td>
                              <?php  
                            }
                        }
                        ?>
                       
                        </tr>
                     </tfooter>
                 </table> 
              </div>
              </div>
              
            </div>
    </section>
</div>

