<?php
	include("includes/dbconfig.php");
	$pagetitle = "Staff advances";
	require('includes/applicationtop.php');
	require('includes/applicationmenu.php');//newexpanse.php
	$post = 0;
	
	
?>
			<!-- Content Wrapper. Contains page content -->
			<div class="content-wrapper">
				<!-- Content Header (Page header) -->
				<section class="content-header">
					 
					<ol class="breadcrumb">
					</ol>
				</section>

				<!-- Main content -->
				<section class="content">
					<!-- Default card -->
					<?php
						if($_SESSION["connectedusergroup"] == 3 || $_SESSION["connectedusergroup"] == 4 || $_SESSION["connectedusergroup"] == 1)
						{
					?>
					<div class="row">
						<div class="col-md-12">
							<div id="servererrormessage">
							</div>
						</div>
					</div>

                    <div class="row">
						<div class="col-md-12">
							<div style="">
                                <?php
                                if(isset($_POST['amount'])){
									$post = 1;
                                    $expanse  = trim($_POST['expanse']);
                                    $amount   = trim($_POST['amount']);
                                    $notes    = trim($_POST['notes']); 
									$branch   = trim($_POST['branch']);
									$employee = trim($_POST['employee']);
									

                                    DB::$error_handler = false;
		                            DB::$throw_exception_on_error = true;
		
                                    try
                                    {
                                       
                                        $trxnConnextn = new MeekroDB();
                                        $trxnConnextn->startTransaction();

                                        $trxnConnextn->insert
                                        ('allowances', 
                                            array
                                            (
                                                'exnotes' => $notes,
                                                'expanse' => $expanse,
                                               
												  'exbranch' => $branch,
												  'examount'=> $amount,
                                                
                                                'employee' => $employee
                                            )
                                        );


                                        $insertedrecord = $trxnConnextn->insertId();
                                        $trxnConnextn->commit();
                                    }
                                    catch(MeekroDBException $e)
		                            {
                                        $err = $err.$e->getMessage()."<br>";
                                        $err = $err.$e->getQuery()."<br>";
                                        $trxnConnextn->rollback();
                                        $errormessage = "<div class=\"alert alert-danger\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\"aria-hidden=\"true\">&times;</button>" . $err . "</div>";
                                        $server_response[] = array('issaved' => 0, 'message' => $errormessage);
                                        DB::$error_handler = 'meekrodb_error_handler';
                                        DB::$throw_exception_on_error = false;
                                        echo json_encode($server_response);
                                        exit();
                                    }
                                }
                                    
                                ?>
							</div>
						</div>
					</div>
				
					<div class="row">
						<div class="col-md-12"  id="customerdetails">
						
						 <!-- general form elements -->
            <div class="card card-primary">
              <div class="card-header">
                <h3 class="card-title"><?= $pagetitle; ?> </h3>
              </div>
			  
			  <div class="card-body">
			 
			  
			   <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
			    <div class="row"> 
			    <div class="col-md-3"> 
				 <a href='addallowances.php'> Add New</a>
				</div>
									
                <div class="col-md-6">
                  
                  
                 
                    <label for="to">Select date</label>
											<div class="input-group">
												<input type="date"   name='theclient'  class="form-control"    />
												<span class="input-group-btn">
													<button class="btn btn-primary btn-flat"  type="submit" name="getreceiptsbtn" id="getreceiptsbtn">Go!</button>
												</span>
											</div>
										</div><!-- form-group -->
										</div>
               
				</form>
				<br /><br />
			   <table class="table table-bordered">
			    <thead>
                    <tr>
                     <th>Date</th>
                      <th>Staff</th>
					  <th>Amount</th> 
					  <th>Notes</th> 
					 
                       
                    </tr>
                  </thead>
			  <?php 
										 	 
									 $i = 0;
									 
									 $shopsalesdata = array();
								if(isset($_POST['theclient'])){
									$date=date_create($_POST['theclient']);
                                    $exdate= date_format($date,"Y-m-d");
								   
                                    $shopsalesdata = DB::query("SELECT exid, examount, employee,  exnotes, full_name, exdate, first_name , last_name FROM allowances  join shop on shop.id = exbranch left JOIN personnel_details on personnel_details.id=employee WHERE date(exdate) ='".$exdate."' ");
									 	
									
								}else {
									$shopsalesdata = DB::query("SELECT exid, examount, employee,  exnotes, full_name, exdate, first_name , last_name FROM allowances  join shop on shop.id = exbranch left JOIN personnel_details on personnel_details.id=employee LIMIT 200");
										
									 
								}
										
										foreach($shopsalesdata as $row)//
										{
										 
										 
											echo ' <tr > 
											<td>'.$row['exdate'].'</td>
											<td>'.$row['first_name'].' , '.$row['last_name'].'</td>
											<td>'.$row['examount'].'</td>
											 
											
											
											 <td>'.$row['exnotes'].'</td> 
											
											 
											</tr>';
									 
										}
			  ?>
			  </table>
			  
			  </div>
               
            </div>
            <!-- /.card -->
							 
						
						
					</div>
					<?php
						}
						else
						{
					?>
					<div class="alert alert-danger alert-dismissable">
						<i class="fa fa-ban-circle"></i>
						Limited Access
					</div>
					<?php
						}
					?>
				</section><!-- /.content -->
			</div><!-- /.content-wrapper -->

			 
		</div><!-- ./wrapper -->
		
		
		
		<?php include ('includes/general_footer.php')?>
		
		<script src="plugins/bert/jquery.price_format.min.js" type="text/javascript"></script>
		<script src="plugins/bert/storepos.js" type="text/javascript"></script>
		<script src="plugins/bert/cashsalesregister.js" type="text/javascript"></script>
	</body>
</html>