<?php
if(basename($_SERVER['PHP_SELF']) == 'corders.php') 
  $fn=" class='current'";
$custo=$conde=$condi='';
include'connection.php';
$dato=$datos=$Date;
$dati=$Date;
$p=$i=0;
$code=0;


	// Save new record for purchase
    if(isset($_POST['addo']))
		{
		    $dati=$_POST['dati'];
			$name=str_replace("'", "`", $_POST['name']);
			$invo=$_POST['invo'];
			$lcno=$_POST['lcno'];
			$conte=$_POST['conte'];
			$iname=str_replace("'", "`", $_POST['item']);
			$qty=str_replace(',', '', $_POST['qty']);
			$amo=str_replace(',', '', $_POST['amo']);
			$type=$_POST['type'];
			$unit=$_POST['unit'];
			$purpo=str_replace("'", "`", $_POST['purpo']);
			
$_SESSION['Base']="jblgroup_sales";
include'sales.php';
$sel =mysqli_query($cons, "SELECT `Number` FROM `items` WHERE `Itemna` = '$iname' GROUP BY `Itemna` ORDER BY `Itemna` ASC");
if($fel=mysqli_num_rows($sel)){
$row=mysqli_fetch_array($sel);
$item = $row['Number'];

	$do=mysqli_query($conn, "INSERT INTO `purchase` (`Date`, `User`, `Supplier`, `Invoice`, `Lcno`, `Container`, `Itemno`, `Itemna`, `Quantity`, `Unit`, `Status`, `Carton`, `Pcs`, `Type`, `Descri`, `Amount`) VALUES ('$dati', '$loge', '$name', '$invo', '$lcno', '$conte', '$item', '$iname', '$qty', '$unit', '0', '0', '0', '$type', '$purpo', '$amo')");
		}
		else
		header("location:corder.php?pto=10&item=$iname&dati=$dati&name=$name&invo=$invo&lcno=$lcno&conte=$conte&qty=$qty&amo=$amo&type=$type&unit=$unit&purpo=$purpo&code=$code");
		}
		
		
		// Update a record for purchase
    if(isset($_POST['updo']))
		{
			$code=$_POST['code'];
			$dati=$_POST['dati'];
			$name=str_replace("'", "`", $_POST['name']);
			$invo=$_POST['invo'];
			$lcno=$_POST['lcno'];
			$conte=$_POST['conte'];
			$iname=str_replace("'", "`", $_POST['item']);
			$qty=str_replace(',', '', $_POST['qty']);
			$amo=str_replace(',', '', $_POST['amo']);
			$type=$_POST['type'];
			$unit=$_POST['unit'];
			$purpo=str_replace("'", "`", $_POST['purpo']);
			
$_SESSION['Base']="jblgroup_sales";
include'sales.php';
$sel =mysqli_query($cons, "SELECT `Number` FROM `items` WHERE `Itemna` = '$iname' GROUP BY `Itemna` ORDER BY `Itemna` ASC");
if($fel=mysqli_num_rows($sel)){
$row=mysqli_fetch_array($sel);
$item = $row['Number'];
			
	$do=mysqli_query($conn, "UPDATE `purchase` SET `Date`='$dati', `User`='$loge', `Supplier`='$name', `Invoice`='$invo', `Lcno`='$lcno', `Container`='$conte', `Itemno`='$item', `Iname`='$iname', `Quantity`='$qty', `Unit`='$unit', `Type`='$type', `Descri`='$purpo', `Amount`='$amo' WHERE `Number`='$code'");
            }
		else
		header("location:corder.php?pto=10&item=$iname&dati=$dati&name=$name&invo=$invo&lcno=$lcno&conte=$conte&qty=$qty&amo=$amo&type=$type&unit=$unit&purpo=$purpo&code=$code");
		}
		
		

include'header.php';

        // Search for data by date
    if(isset($_POST['search']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
				$p=1;
		}
		
	
			// Delete a given purchase record
    if(isset($_POST['delo']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
			$code=$_POST['code'];
				$p=$_POST['p'];
		
	$dele=mysqli_query($conn, "DELETE FROM `purchase` WHERE `Number`='$code' ORDER BY `Number` DESC LIMIT 1");
		}
		
        if(isset($_POST['open']))
		{
			$code=$_POST['code'];
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$p=1;
		}
		
        if($dato==$datos)
			$mpri="ON $dato";
		else
			$mpri="FROM $dato TO $datos";
?>

<div class="container-fluid main-content">
        <div class="page-title hidden-xs hidden-print">
          <h2 style='margin-top:-20px; margin-bottom: 5px; color:#ffcc33'>
         Finance
          </h2>
                 </div>
     
        <div class="row">
  <div class="col-lg-2 hidden-print">
   
   <ul class="list-group">  

    <li class="list-group-item">
	  <a href="salesrepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Sales Report
                </p>
              </a></li> 

    <li class="list-group-item">
	  <a href="purcharepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Purchase Report
                </p>
              </a></li>   

    <li class="list-group-item">
	  <a href="trans.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Service Providers
                </p>
              </a></li> 

    <li class="list-group-item">
	  <a href="experepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Payout/Expenses
                </p>
              </a></li>  

    <li class="list-group-item">
	  <a href="zreport.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Daily Z-Report
                </p>
              </a></li>   

    <li class="list-group-item">
	  <a href="taxrepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;VAT/TAX Report
                </p>
              </a></li>   

    <li class="list-group-item">
	  <a href="assets.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Assets Report
                </p>
              </a></li>    

    <li class="list-group-item active">
	  <a href="corders.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Create Orders
                </p>
              </a></li>  
                       
            </ul><br><br><br>
  </div>
  
  
  <?php
 // include'transc.php';
?>
       
        <div class="col-lg-10">
                  <div class="row"><form action='' method='post'>
	<div class="col-lg-3 hidden-print" style="padding-left:50px; padding-right:50px;">

    <button class="btn btn-warning btn-block" type="button" stle="font-size:13px;" onclick="location.href='corder.php'">CREATE NEW ORDER</button>
		</div>
           <div class="col-lg-1" style="padding-left:40px;"></div>

        <form action="" method="post" class="form-horizontal ">                  
    <div class="col-lg-8 hidden-print" style="padding-right:50px;">
                           
     <div class="col-lg-4 hidden-print"><select class="form-control" name="custo">
		<option value='' selected='selected'>SELECT A SUPPLIER</option>
			 <?php
$doi=mysqli_query($conn, "SELECT `Supplier` FROM `purchase` WHERE `Status`='0' AND `Supplier`!='' GROUP BY `Supplier` ORDER BY `Supplier` ASC");
			while($roi=mysqli_fetch_assoc($doi)){
				$desto=$roi['Supplier'];
				if($desto==$custo)
					$sle="selected='selected'";
				else
					$sle='';
			echo"<option value='$desto' $sle> $desto </option>";
			}
			?>    
                            </select>
			</div>      
			
             <div class="col-lg-3"> 
           <div class="input-group date" data-provide="datepicker">
      <input class="form-control form-center" id="from" name="dato" type="text" value="<?php echo $dato ?>" onkeypress='return isNumberKey(event)' required>
	  <span class="input-group-addon"><i class="lnr lnr-calendar-full"></i></span>
			</div></div>


		  <div class="col-lg-3"> 
           <div class="input-group date" data-provide="datepicker">	
      <input class="form-control form-center" id="to" name="datos" type="text" value="<?php echo $datos ?>" onkeypress='return isNumberKey(event)' required>
	  <span class="input-group-addon"><i class="lnr lnr-calendar-full"></i></span>
              </div></div>                     
                       
                       <div class="col-lg-2"><?php echo"<input type='hidden' name='pg' value='$pg'>"; ?>
                        <button class="btn  btn-primary btn-block" type="submit" name="search"><i class="lnr lnr-magnifier"></i> Search</button>
                   
					  </div>
                         </div></form>               
            </div>
               <?php
        if($custo)
            $conde="AND (`Supplier` LIKE '%$custo%')";
            
            if($code)
                $conde="AND `Number`='$code'";
        
				if($p)
		$dok=mysqli_query($conn, "SELECT *FROM `purchase` WHERE `Status`='0' AND `Date` BETWEEN '$dato' AND '$datos' $conde ORDER BY `Date` ASC");
				else
		$dok=mysqli_query($conn, "SELECT *FROM (SELECT *FROM `purchase` WHERE `Status`='0' ORDER BY `Date` DESC LIMIT 15) SUB ORDER BY `Date` ASC");
				if($fo=mysqli_num_rows($dok)){
				?>
                 
            <div class="divFooter"><center><u><b>PURCHASE REPORT <?php echo $mpri ?></b></u></center></div>
             <div class="row">
            <div class="col-lg-12">
     <span> &nbsp;&nbsp; Total Records Found : <b><?php echo $fo ?></b></span>
			 <span class="pull-right hidden-print"> &nbsp;&nbsp;
			 <a href="#" onclick="return window.print()" class="hidden-print" title='Click to print' data-toggle='tooltip' data-placement='top'>
			 <i class="lnr lnr-printer"></i></a></span>
            <div class="widget-container fluid-height clearfix">
            <div class="widget-content padded clearfix">

		<table class="table table-striped table-hover">     
                                      <thead>
                    <tr role="row">
                     <th width="1%" style='padding:1px; text-align:center'> No </th>
			<th style="adding:1px;"><div align='center'>&nbsp;&nbsp;&nbsp;&nbsp;DATE&nbsp;&nbsp;&nbsp;&nbsp;</th>
	        <th style='padding:1px;'><div align='center'> SUPPLIER </th>
			<th style='padding:1px;'><div align='center'> INVOICE </th>
			<th style='padding:1px;'><div align='center'> LC&nbsp;No </th>
			<th style='padding:1px;'><div align='center'> ITEM </th>
	<th style='padding:1px;' colspan='2'><div align='center'> QUANTITY </th>
			<th style='padding:1px;'><div align='center'> TYPE </th>
			<th style='padding:1px;'><div align='center'> AMOUNT </th>
			<th style='padding:1px;'><div align='center'> PAID </th>
			<th style='padding:1px;'><div align='center'> BALANCE </th>
				<th colspan='4' class="hidden-print" 
	style='padding:1px; width:20px;'><div align='center'> OPTIONS </th>
                     </tr>
                    </thead>
                                        <tbody>
					<?php
					
				
			
				$i++;			$tot=0;
		while($rok=mysqli_fetch_assoc($dok)){
				$code=$rok['Number'];
				$dati=$rok['Date'];
				$name=$rok['Supplier'];
			$invo=$rok['Invoice'];
	        $amo=($rok['Amount']);			$amoo=number_format($amo, 2);
			$lcno=$rok['Lcno'];
			$item=$rok['Itemna'];
			$qty=$rok['Quantity'];           $qto=number_format($qty, 2);
			$unit=$rok['Unit'];
			$type=$rok['Type'];
			$purpo=$rok['Descri'];
					$stl="padding:1px; font-size:12px;";

	print("<tr><td style='$stl $clr'><div align='right'>&nbsp;$i&nbsp;</td>
	<td style='$stl $clr'><div align='center'> $dati </td>
	<td style='$stl $clr'> $name </td><td style='$stl $clr'> $invo </td>
	<td style='$stl $clr'> $lcno </td><td style='$stl $clr'> $item </td>
	<td style='$stl $clr'><div align='right'> $qto </td>
	<td style='$stl $clr'> $unit </td><td style='$stl $clr'> $type </td>
	<td style='$stl $clr'><div align='right'> $$amoo </td>
	<td style='$stl $clr'><div align='right'> $0.00 </td>
	<td style='$stl $clr'><div align='right'> $$amoo </td>
	<td style='$stl $clr'>");

    if($_SESSION['Cfr']){
	    $dbutn='submit';
	    $disa='';
	}
	else{
	    $dbutn='button';
	    $disa='disabled';
	}

	include'addpaid.php';                              include'transc.php';              
	echo"<div class='modal fade' id='modal-$i' tabindex='-1' role='dialog' aria-labelledby='exampleModalLabel' aria-hidden='true' 
		style='top:120px;'>
  <div class='modal-dialog' role='document'>
    <div class='modal-content text-left'>
      <div class='modal-header'>
        <h5 class='modal-title' id='exampleModalLabel'>DELETE CONFIRMATION 
		<label class='text-right pull-right'> $name &nbsp;&nbsp;&nbsp;&nbsp; $$amoo</label></h5>

      </div>
      <div class='modal-body' style='height:60px;'>
        <h5 style='color:#ff0033'>Are you sure you want to delete this purchase?</h5>
      </div><form method='post' action=''>
      <input type='hidden' name='code' value='$code'>
      <input type='hidden' name='datos' value='$datos'>
      <input type='hidden' name='p' value='$p'>
	  <input type='hidden' name='dato' value='$dato'>
	  <input type='hidden' name='custo' value='$custo'>
      <div class='modal-header' style='height:50px; padding:10px; border:0px solid blue; text-align:right'>
        <button type='button' class='btn btn-sm btn-success' data-dismiss='modal'>&nbsp;NO&nbsp;</button>
        <button type='$dbutn' name='delo' class='btn btn-sm btn-danger' $disa data-toggle='tooltip' data-placement='top'>YES</button>
      </div></form>
    </div>
  </div>
</div>";

	print("</td><form action='' method='post'>
	<td class='hidden-print' style='padding:0px; width:20px;'>
	<div align='right'><input type='hidden' name='code' value='$code'>
	<input type='hidden' name='dato' value='$dati'>
	<input type='hidden' name='datos' value='$dati'>
	<button type='submit' class='btn btn-xs btn-success hidden-print' style='height:18px; width:25px; padding:0px; margin:0px; margin-right:2px;' title='Edit a Purchase' data-toggle='tooltip' data-placement='top' name='open'> &nbsp;<i class='lnr lnr-download'></i>&nbsp; </button></td></form><form action='corder.php' method='post'>
	<td class='hidden-print' style='padding:0px; width:20px;'>
	<div align='right'><input type='hidden' name='code' value='$code'>
	<button type='submit' name='open' class='btn btn-xs btn-warning hidden-print' style='height:18px; width:25px; padding:0px; margin:0px; margin-right:2px;' title='Edit a Purchase' data-toggle='tooltip' data-placement='top'> &nbsp;<i class='lnr lnr-pencil'></i>&nbsp; </button></td></form><form action='' method='post'>
	<td class='hidden-print' style='padding:0px; width:20px;'><div title='Delete a SUPPLIERer' data-toggle='tooltip' data-placement='top'>
	<div align='right'><button type='button' class='btn btn-xs btn-danger hidden-print' style='height:18px; width:25px; padding:0px; margin:0px; margin-right:2px;' data-toggle='modal' data-target='#modal-$i'> &nbsp;<i class='lnr lnr-trush'></i>&nbsp; </button></td></form></tr>");
		        $i++;	    			$tot+=$amo;      
						}
			$tot=number_format($tot, 2);
						?>
						
                    </tbody>
            <?php
                if($code=='0'){
                    ?>
					 <thead>
					<tr><th> </th>
		<th colspan='6'><div align='center'> TOTAL AMOUNT </th>
				<th style="padding-right:1px;" colspan="3">
				<div align='right'>$<?php echo $tot ?></th>
				<th style="padding-right:1px;" colspan="3">
				<div align='right'>0.00</th>
				<th style="padding-right:1px;" colspan="3">
				<div align='right'>$<?php echo $tot ?></th>
			<th> </th><th colspan="3" class='hidden-print'>
			<div align='center'> -- </th></tr>
			    <?php
			    }
			    ?></table>

								</div></div>
								<span class="pull-right hidden-print">
			 <a href="#" onclick="return window.print()" class="hidden-print" title='Click to print' data-toggle='tooltip' data-placement='top'>
			 <i class="lnr lnr-printer"></i></a></span><br><br><br>  

			<?php
				}
				else{
						echo"<div class='row'>
            <div class='col-lg-12' style='margin-top:-20px;'>
             <span> &nbsp;&nbsp; Total Records Found : <b> 0 </b></span>
            <div class='widget-container fluid-height clearfix'>
            <div class='widget-content padded clearfix'>
			<br><br><br><br><br><br>
	<div style='text-align:center; font-size:24px; color:#ff9999'>Report not available on selected date </div><br><br><br><br><br><br>";
					}
			
					?>
                                      
                
              </div>
            </div></div>
                  </div>
   <?php
   include'footer.php';
   ?>