<?php
if(basename($_SERVER['PHP_SELF']) == 'purchase.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']);
			$wei=str_replace(',', '', $_POST['wei']);
			$rat=str_replace(',', '', $_POST['rat']);
			$type=$_POST['type'];
			$unit=$_POST['unit'];
			$purpo=str_replace("'", "`", $_POST['purpo']);
			$p=$_POST['p'];
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
			$vous=$_POST['vous'];
			$inom=0;
			
$_SESSION['Base']="jblgroup_sales";
include'sales.php';
$sel=mysqli_query($cons, "SELECT `Number`, `Uprice`, `Size` FROM `items` WHERE `Itemna` = '$iname' GROUP BY `Itemna` ORDER BY `Itemna` ASC");
if($fel=mysqli_num_rows($sel)){
$row=mysqli_fetch_assoc($sel);
$item = $row['Number'];
$prix = $row['Uprice'];
$size = $row['Size'];

			if(!$vous OR $vous=='0'){
	$rese=mysqli_query($conn, "SELECT `Voucher` FROM `purchase` WHERE `Voucher`!='0' AND `Status`='0' ORDER BY `Voucher` DESC LIMIT 1");
				$res=mysqli_fetch_assoc($rese);
					$vous=$res['Voucher']+1;
			            }

	$do=mysqli_query($conn, "INSERT INTO `purchase` (`Date`, `User`, `Supplier`, `Invoice`, `Lcno`, `Container`, `Itemno`, `Itemna`, `Quantity`, `Unit`, `Status`, `Carton`, `Pcs`, `Type`, `Descri`, `Amount`, `Voucher`, `Weight`, `Rate`, `Price`, `Size`) VALUES ('$dati', '$loge', '$name', '$invo', '$lcno', '$conte', '$item', '$iname', '$qty', '$unit', '0', '0', '0', '$type', '$purpo', '$amo', '$vous', '$wei', '$rat', '$prix', '$size')");
		}
		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&rate=$rat&vous=$vous&inom=$inom");
		}
		
		
		// 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=$_POST['item'];
			$qty=str_replace(',', '', $_POST['qty']);
			$amo=str_replace(',', '', $_POST['amo']);
			$wei=str_replace(',', '', $_POST['wei']);
			$rat=str_replace(',', '', $_POST['rat']);
			$type=$_POST['type'];
			$unit=$_POST['unit'];
			$purpo=str_replace("'", "`", $_POST['purpo']);
			$p=$_POST['p'];
			$vous=$_POST['vous'];
			$inom=$_POST['inom'];
		
$_SESSION['Base']="jblgroup_sales";
include'sales.php';
$sel=mysqli_query($cons, "SELECT `Number` FROM `items` WHERE `Number` = '$inom' GROUP BY `Itemna` ORDER BY `Itemna` ASC");
if($fel=mysqli_num_rows($sel)){
			
	$do=mysqli_query($conn, "UPDATE `purchase` SET `Date`='$dati', `User`='$loge', `Supplier`='$name', `Invoice`='$invo', `Container`='$conte', `Itemno`='$inom', `Itemna`='$iname', `Quantity`='$qty', `Unit`='$unit', `Type`='$type', `Descri`='$purpo', `Amount`='$amo', `Weight`='$wei' WHERE `Number`='$code' AND `Voucher`='$vous'");
	
	$set=mysqli_query($conn, "UPDATE `purchase` SET `Lcno`='$lcno', `Rate`='$rat', `Invoice`='$invo', `Date`='$dati', `Supplier`='$name' WHERE `Status`='0' AND `Voucher`='$vous'");
            }
		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&rate=$rat&vous=$vous&inom=$inom");
		}
		
		

include'header.php';

        // Search for data by date
    if(isset($_POST['search']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
				$p=1;
		}
		
		
		// Change purchase rate
	if(isset($_POST['change']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
				$p=$_POST['p'];
			$vous=$_POST['vous'];
			$rat=str_replace(',', '', $_POST['rat']);
	$set=mysqli_query($conn, "UPDATE `purchase` SET `Rate`='$rat' WHERE `Voucher`='$vous'");
		}
		
		
		// Add taxes
	if(isset($_POST['addta']))
		{
		$p=$_POST['p'];
		$t=$_POST['t'];
		$vous=$_POST['vous'];
		$dato=$_POST['dato'];
		$datos=$_POST['datos'];
		$custo=$_POST['custo'];
		$taxes=str_replace(',', '', $_POST['taxes']);
		$vates=str_replace(',', '', $_POST['vates']);
		$tsq=str_replace(',', '', $_POST['tsq']);
		if($taxes OR $vates){
		    for($n=1; $n<=$t; $n++){
		    $tick=$_POST["tick$n"];
		    if($tick){
			$num=$_POST["num$n"];
			$size=$_POST["size$n"];
			$tax=round($taxes/$tsq*$tick);
			$vat=round($vates/$tsq*$tick);
			
	$set=mysqli_query($conn, "UPDATE `purchase` SET `Tax`='$tax', `Vat`='$vat', `Size`='$size' WHERE `Voucher`='$vous' AND `Number`='$num'");
		        }
		    }
		}
		else{
		for($n=1; $n<=$t; $n++){
			$tax=str_replace(',', '', $_POST["tax$n"]);
			$vat=str_replace(',', '', $_POST["vat$n"]);
			$sale=str_replace(',', '', $_POST["sale$n"]);
			$size=$_POST["size$n"];
			$num=$_POST["num$n"];
	$set=mysqli_query($conn, "UPDATE `purchase` SET `Tax`='$tax', `Vat`='$vat', `Price`='$sale', `Size`='$size' WHERE `Voucher`='$vous' AND `Number`='$num'");
		        }
		    }
		}
		
		 // Back to list
    if(isset($_POST['back']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
				$p=$_POST['P'];
		}
		
	
			// Delete a given purchase record
    if(isset($_POST['delo']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
			$code=$_POST['code'];
			$vous=$_POST['vous'];
			$supli=$_POST['supli'];
				$p=$_POST['p'];
		
		if($p=='2')
	$dele=mysqli_query($conn, "DELETE FROM `purchase` WHERE `Number`='$code' ORDER BY `Number` DESC LIMIT 1");
	    else{
	$dele=mysqli_query($conn, "DELETE FROM `purchase` WHERE `Status`='0' AND `Voucher`='$vous' ORDER BY `Number` DESC LIMIT 1");
	$do=mysqli_query($cons, "DELETE FROM `loading` WHERE `Voucher`='$vous' AND `Suppli`='$supli' ORDER BY `Number` ASC LIMIT 1");
	        }
		}
		
        if(isset($_POST['open']))
		{
			$code=$_POST['code'];
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
			$vous=$_POST['vous'];
			$p=2;
		}
		
		
		
	if(isset($_POST['addco']))
		{
			$code=$_POST['code'];
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
			$invo=$_POST['invo'];
			$dati=$_POST['dati'];
			$name=$_POST['name'];
			$vous=$_POST['vous'];
			$dase=$_POST['dase'];
			$descri=str_replace("'", "`", $_POST['descri']);
			$bulk=$_POST['bulk'];
			$z=$_POST['z'];
			$p=2;
			
			if($bulk AND $z){
			    for($i=1; $i<$z; $i++){
			$amo=str_replace(',', '', $_POST["amo$i"]);
			if($amo){
			$rats=str_replace(',', '', $_POST["rats$i"]);
			$cost=$_POST["cost$i"];
			
	$doi=mysqli_query($conn, "SELECT `Code` FROM `currency` WHERE `Rate`='$rats' ORDER BY `Code` ASC");
	    $roi=mysqli_fetch_assoc($doi);
			 $curre=$roi['Code'];
			
	$do=mysqli_query($conn, "INSERT INTO `loading` (`Date`, `User`, `Voucher`, `Cost`, `Amount`, `Rate`, `Descri`, `Invoice`, `Dati`, `Suppli`, `Status`, `Currency`) VALUES ('$dase', '$loge', '$vous', '$cost', '$amo', '$rats', '$descri', '$invo', '$dati', '$name', '0', '$curre')");
			}
			    }
			}
			else{
			    
			$amo=str_replace(',', '', $_POST['amo']);
			$rats=str_replace(',', '', $_POST['rats']);
			$cost=$_POST['cost'];
			
	$doi=mysqli_query($conn, "SELECT `Code` FROM `currency` WHERE `Rate`='$rats' ORDER BY `Code` ASC");
	    $roi=mysqli_fetch_assoc($doi);
			 $curre=$roi['Code'];
			
	$do=mysqli_query($conn, "INSERT INTO `loading` (`Date`, `User`, `Voucher`, `Cost`, `Amount`, `Rate`, `Descri`, `Invoice`, `Dati`, `Suppli`, `Status`, `Currency`) VALUES ('$dase', '$loge', '$vous', '$cost', '$amo', '$rats', '$descri', '$invo', '$dati', '$name', '0', '$curre')");
			}
		}
		
	if(isset($_POST['deleco']))
		{
			$code=$_POST['code'];
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
			$invo=$_POST['invo'];
			$dati=$_POST['dati'];
			$name=$_POST['name'];
			$num=$_POST['num'];
			$vous=$_POST['vous'];
			$p=2;
			
	$do=mysqli_query($conn, "DELETE FROM `loading` WHERE `Number`='$num' ORDER BY `Number` ASC LIMIT 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 active">
	  <a href="purchase.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Purchase Orders
                </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> 
                       
            </ul><br><br><br>
  </div>
  
  
         <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;">
        <?php
        if($p==2){
        echo"
      <input type='hidden' name='p' value='$p'>
	  <input type='hidden' name='dato' value='$dato'>
	  <input type='hidden' name='custo' value='$custo'>
	  <button class='btn btn-success btn-block' type='submit' name='back' stle='font-size:13px;'><i class='lnr lnr-chevron-left-circle'></i> &nbsp; BACK TO LIST</button>";
        }
        else{
    echo"<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($p==2)
		$dok=mysqli_query($conn, "SELECT *FROM `purchase` WHERE `Status`='0' AND `Voucher`='$vous' ORDER BY `Number` ASC");
		        elseif($p==1)
		$dok=mysqli_query($conn, "SELECT *, SUM(`Tax`) AS `Tax`, SUM(`Vat`) AS `Vat`, SUM(`Weight`) AS `Weight`, SUM(`Amount`) AS `Amount`, SUM(`Quantity`) AS `Quantity`, COUNT(DISTINCT(`Itemna`)) AS `Itemna` FROM `purchase` WHERE `Status`='0' AND `Date` BETWEEN '$dato' AND '$datos' $conde GROUP BY `Voucher` ORDER BY `Date` ASC");
				else
		$dok=mysqli_query($conn, "SELECT *, SUM(`Tax`) AS `Tax`, SUM(`Vat`) AS `Vat`, SUM(`Weight`) AS `Weight`, SUM(`Amount`) AS `Amount`, SUM(`Quantity`) AS `Quantity`, COUNT(DISTINCT(`Itemna`)) AS `Itemna` FROM `purchase` WHERE `Status`='0' GROUP BY `Voucher` ORDER BY `Date` DESC LIMIT 15");
				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;' width='15%'><div align='center'> SUPPLIER </th>
			<th style='padding:1px;'><div align='center'> INVOICE </th>
			<th style='padding:1px;'><div align='center'> DMC&nbsp;No </th>
			<th style='padding:1px;'><div align='center'> WEIGHT </th>
			<th style='padding:1px;'><div align='center'> ITEMS </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'> RATE </th>
			<th style='padding:1px;'><div align='center'> VALUE </th>
			<th style='padding:1px;'><div align='center'> TAXES </th>
	<th colspan='4' class="hidden-print" style='padding:1px; width:20px;'><div align='center'> OPTIONS </th>
                     </tr>
                    </thead>
                                        <tbody>
					<?php
					
		$tqs=$ites=$pris=$rats=$numb=$taxe=$vati=$sell=$colo=$siz=array();	
			
	$i++;			$tot=$tpa=$tba=$tva=$tow=$tsq=0;          $ttax=$tvat=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'];
			$inom=$rok['Itemno'];
			$qty=$rok['Quantity'];           $qto=number_format($qty, 2);
			$unit=$rok['Unit'];
			$type=$rok['Type'];
			$size=$rok['Size'];
			$conte=$rok['Container'];
			$purpo=$rok['Descri'];
			$wei=$rok['Weight'];            $weio=number_format($wei, 2);
			$tax=$rok['Tax'];                $taxo=number_format($tax, 2);
			$vat=$rok['Vat'];                $vato=number_format($vat, 2);
			$pri=number_format($rok['Price'], 2);
					$stl="padding:1px; font-size:12px;";
					   $taxes=number_format($tax+$vat, 2);
					        $cls="color:blue;";
					   
			if($tax=='0' OR $vat=='0'){
                $cls="color:red;";
			    }
					if($p==2){
					    $dv="";
		    $disa="disabled";
        $ites[$i]=$item;        $pris[$i]=$amo;        $rats[$i]=$pay;
        $numb[$i]=$code;        $taxe[$i]=$taxo;          $vati[$i]=$vato;
        $sell[$i]=$pri;         $colo[$i]=$cls;             $siz[$i]=$size;
        $tqs[$i]=$qty;
					}
					else{
					    $dv="text-align:center;";
		    $disa="";
					}
					
					if($rok['Receive'])
					    $clr="color:blue;";
					else
					    $clr="";
					    
					    
					    if(!$rok['Cost'])
					        $clo="color:red;";
					    else
					        $clo="";
					    
		
			$vous=$rok['Voucher'];
		    $pay=$rok['Rate'];
		    
		    
					    $bal=$amo*$pay;
					$balo=number_format($bal, 2);
					$payo=number_format($pay, 2);
					
		
		if($p!=2){
	$ser=mysqli_query($conn, "SELECT `Number` FROM `services` WHERE `Item`='PURCHASE' AND `Status`='0' AND `Voucher`='$vous'");
	if($fer=mysqli_num_rows($ser)){
	$then=mysqli_query($conn, "UPDATE `services` SET `Amount`='$amo', `Rate`='$pay', `Date`='$dati', `Supplier`='$name', `Invoice`='$invo', `Vat`='$vat' WHERE `Voucher`='$vous' AND `Item`='PURCHASE' AND `Status`='0'");   
	}
	else{
	$then=mysqli_query($conn, "INSERT INTO `services` (`Date`, `User`, `Item`, `Supplier`, `Amount`, `Invoice`, `Purpose`, `Status`, `Currency`, `Rate`, `Paid`, `Voucher`, `Vat`) VALUES ('$dati', '$loge', 'PURCHASE', '$name', '$amo', '$invo', '$purpo', '0', 'USD', '$pay', '0', '$vous', '$vat')");
	}
	
		}
			

	print("<tr title='$purpo' data-toggle='tooltip' data-placement='top'>
	<td style='$stl $clr $clo'><div align='right'>&nbsp;$i&nbsp;</td>
	<td style='$stl $clr $clo'><div align='center'> $dati </td>
	<td style='$stl $clr $clo'> $name </td><td style='$stl $clr $clo'> $invo </td>
	<td style='$stl $clr $clo'> $lcno </td><td style='$stl $clr $clo'><div align='right'> $weio </td><td style='$stl $clr $clo $dv'> $item </td>
	<td style='$stl $clr $clo'><div align='right'> $qto </td>
	<td style='$stl $clr $clo'> $unit </td><td style='$stl $clr $clo'> $type </td>
	<td style='$stl $clr $clo'><div align='right'> $$amoo </td>
	<td style='$stl $clr $clo'><div align='right'>
		<a href='#' data-placement='top' data-toggle='modal' data-target='#ratepModal$i'> $payo </a></td>
	<td style='$stl $clr $clo'><div align='right'> $balo </td>
	<td style='$stl $clr $clo'><div align='right'> $taxes </td>
	<td style='$stl $clr $clo'>");

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

//	include'addpaid.php';                              include'transc.php'; 
	
	
	
	
	
	
	// *********************** Change Rate ************************************************
		echo"<div class='modal fade' id='ratepModal$i' tabindex='-1' role='dialog' aria-labelledby='exampleModalLabel' aria-hidden='true'>
  <div class='modal-dialog' role='document'><div class='modal-content'><div class='modal-header text-left'>
        <h5 class='modal-title' id='exampleModalLabel'> Change Rate <span class='pull-right'> $$amoo </span></h5>

      </div><form action='' method='post'>
      <div class='modal-body text-left' style='height:100px;'>
			<div class='row'>
		<div class='col-md-12'>
			<div class='col-md-6 text-right' style='padding-top:30px;'><b> RATE  / USD </b>
            </div>


	<div class='col-md-5'><br><div class='input-group'>
	<input name='rat' class='form-control text-center' type='text' onkeyup='format(this);' onkeypress='return isNumberKey(event)' title='Rate' data-toggle='tooltip' data-placement='top' value='$payo' required><span class='input-group-addon text-info'>$</span>
  </div>

</div>

			</div></div></div><hr>
		<input type='hidden' name='custo' value='$custo'><input type='hidden' name='vous' value='$vous'>
	  <input type='hidden' name='dato' value='$dato'><input type='hidden' name='datos' value='$datos'><input type='hidden' name='p' value='$p'>
      <div class='modal-footer' style='margin-top:-10px; height:50px; padding-top:5px; border:0px solid blue;'>
        <button type='button' class='btn btn-sm btn-danger' data-dismiss='modal' style='width:80px;'> CANCEL </button>
        <button type='submit' name='change' class='btn btn-sm btn-success' style='width:80px;'> UPDATE </button>
      </div></form>
    </div>
  </div>
</div>";



	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; $$balo</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='vous' value='$vous'>
      <input type='hidden' name='p' value='$p'>
      <input type='hidden' name='supli' value='$name'>
	  <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='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 this Record' 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-trash'></i>&nbsp; </button></td>
	<td class='hidden-print' style='padding:0px; width:20px;'>
	<div align='right'><input type='hidden' name='code' value='$code'>
	<input type='hidden' name='invo' value='$invo'>
	<input type='hidden' name='dato' value='$dato'>
	<input type='hidden' name='datos' value='$datos'>
	<input type='hidden' name='custo' value='$custo'>
      <input type='hidden' name='dati' value='$dati'>
      <input type='hidden' name='name' value='$name'>
      <input type='hidden' name='vous' value='$vous'>
	<button type='submit' class='btn btn-xs btn-success hidden-print' style='height:18px; width:25px; padding:0px; margin:0px; margin-right:2px;' title='Open this Purchase' data-toggle='tooltip' data-placement='top' name='open' $disa> &nbsp;<i class='lnr lnr-download'></i>&nbsp; </button></td></form></tr>");
	$i++;	    $tot+=$amo;             $tpa+=$pay;              $tba+=$bal; 
	            $tva+=($tax+$vat);          $ttax+=$tax;         $tvat+=$vat;
            	$tow+=$wei;                             $tsq+=$qty;
        		}
			$toto=number_format($tot, 2);
			$tbao=number_format($tba, 2);
			$tpao=number_format($tpa, 2);
			$tvao=number_format($tva, 2);
			$ttaxo=number_format($ttax, 2);
			$tvato=number_format($tvat, 2);
			$tsqo=number_format($tsq, 2);
			$towo=number_format($tow, 2);
						?>
						
                    </tbody><thead><tr><th> </th>
		<th colspan='4' style="font-size:11px; text-align:center;"><b> TOTAL AMOUNT </b></th>
				<th style="padding-right:1px; font-size:11px;">
				<div align='right'><b><?php echo $towo ?></b></div></th>
		<th style="padding-right:1px; font-size:11px;">KG</th>
		<th style="padding-right:1px; font-size:11px;" colspan='2'><b><?php echo $tsqo ?></b></th>
				<th style="padding-right:1px; font-size:11px;" colspan='2'>
				<div align='right'><b>$<?php echo $toto ?></b></div></th>
				<th style="padding-right:1px; font-size:11px;" colspan='2'>
				<div align='right'><b>RWF&nbsp;<?php echo $tbao ?></b></div></th>
				<th style="padding-right:1px; font-size:11px;" colspan='2'>
				<div align='right'><b><?php echo $tvao ?></b></div></th>
			<th> </th><th colspan="3" class='hidden-print'>
			<div align='center'> -- </th></tr>
    	</table><div class="text-right" style="padding-right:30px; padding-top:5px;">
		    
		<?php
		if($p==2){
		    $tt=1;             $cust=$name;            $_SESSION['Vous']=$vous;
	$cal=mysqli_query($conn, "UPDATE `purchase` SET `Calcul`='1', `Totwei`='$tow' WHERE `Voucher`='$vous' ORDER BY `Number` ASC LIMIT 100");
		    
    echo"<button type='button' class='btn btn-xs btn-link hidden-print pull-left' onclick=location.href='purtax.php' style='width:110px; margin-left:40px;'> TAX PAYMENT </button>";
    
		  ?>
		    
	<button type='button' class='btn btn-xs btn-danger hidden-print' onclick="window.open('calcul.php', 'yourWindowName', 'scrollbars=no, width=1500, height=1500, top=150, left=200')" title='Click to open cost calculation' style='width:110px; margin-right:20px;'> <i class='lnr lnr-dice'></i>&nbsp;Calculation</button>	    
   <?php
    
	echo"<button type='button' class='btn btn-xs btn-warning hidden-print' data-toggle='modal' data-target='#xcModalp' style='width:110px; margin-right:20px;'> <i class='lnr lnr-plus-circle'></i>&nbsp;ADD COST </button>
	
	<a class='btn btn-xs btn-info hidden-print pull-right' href='#'  data-toggle='modal' data-target='#taxModal' style='width:110px; margin-right:20px;'>&nbsp;&nbsp;<i class='lnr lnr-laptop-phone'></i>&nbsp;ADD TAXES&nbsp;</a>  
	
	<a class='btn btn-xs btn-success hidden-print pull-right' href='javascript:togglez()' id='displaysText' style='width:110px; margin-right:20px;'>&nbsp;&nbsp;<i class='lnr lnr-chart-bars'></i>&nbsp;ADD ITEMS&nbsp;</a>
	</div>";
	
	
// *********************** Add taxes modal ******************************
	echo"<div class='modal fade' id='taxModal' tabindex='-1' role='dialog' aria-labelledby='exampleModalLabel' aria-hidden='true'>
  <div class='modal-dialog' role='document'><div class='modal-content'><div class='modal-header text-left'>
        <h5 class='modal-title' id='exampleModalLabel'><b> ADD TAXES </b><span class='pull-right'> </span></h5>

      </div><form action='' method='post'>
      <div class='modal-body text-left' style='height:auto;'>
		<div class='col-md-12' style='margin-top:0px; padding-top:0px; margin-bottom:0px; padding-bottom:0px;'>";

    echo"<table class='table table-striped table-hover'>     
            <thead><tr role='row'><th width='4%'><div align='center'> No </th>
    <th><div align='center'> Item&nbsp;Name </th><th width='10%'><div align='center'> Total&nbsp;Tax </th><th width='10%'><div align='center'> Vat&nbsp;Inward </th><th width='10%'><div align='center'> Selling&nbsp;Price </th></tr>";
    
            for($t=1; $t<$i; $t++){
        $item=$ites[$t];
        $price=number_format($pris[$t]*$pay, 2);
        $rates=$rats[$t];
        $nume=$numb[$t];
        $viat=$vati[$t];
        $taxes=$taxe[$t];
        $sales=$sell[$t];
        $clr=$colo[$t];
        $sizi=$siz[$t];
        $stl="style='padding:2px;'";
echo"<tr><td class='text-right' $stl> $t&nbsp;&nbsp;</td><td $stl> $item </td>
      <td class='text-center' $stl><input name='tax$t' class='form-control' type='text' onkeyup='format(this);' onkeypress='return isNumberKey(event)' style='text-align:right; width:90px; height:25px; margin:0px 0px 0px 0px; padding:0px 10px 0px 0px;' value='$taxes'></td><td class='text-center' $stl><input name='vat$t' class='form-control' type='text' onkeyup='format(this);' onkeypress='return isNumberKey(event)' style='text-align:right; width:90px; height:25px; margin:0px 0px 0px 0px; padding:0px 10px 0px 0px;' value='$viat'></td><td class='text-center' $stl><input name='sale$t' class='form-control' type='text' onkeyup='format(this);' onkeypress='return isNumberKey(event)' style='text-align:right; width:90px; height:25px; margin:0px 0px 0px 0px; padding:0px 10px 0px 0px; $clr' value='$sales'><input type='hidden' name='num$t' value='$nume'><input type='hidden' name='size$t' value='$sizi'></td></tr>";          
            }
	echo"</table>";  
	                                            $t--
		?>
<script type="text/javascript">
function UpdateCost() {
  var sum = 0;
  var gn, elem;
  for (i=1; i<=<?php echo $t ?>; i++) {
    gn = 'sum_m_'+i;
    elem = document.getElementById(gn);
    if (elem.checked == true) { sum += Number(elem.value); }
  }
  document.getElementById('totalcost' ).value = sum.toFixed(0);
}
window.onload=UpdateCost
</script>
	
	<?php
echo"<a class='btn btn-xs btn-info hidden-print' href='javascript:togglet()' id='displaytText' style='width:120px; margin-left:60px;'><i class='lnr lnr-dice'></i> Tax Dispatch </a>
	
	<div id='toggletText' style='display:none; padding:20px; margin-top:20px; border:1px solid #D8D7D6; border-radius:5px; margin-bottom:-20px; text-align:center; height:auto;'>";
	
	 for($f=1; $f<$i; $f++){
        $item=$ites[$f];
        $nume=$numb[$f];
        $tqsi=$tqs[$f];
        $sizi=$siz[$f];
	
	echo"<div class='col-xs-1 text-right' style='padding-right:1px;'><label><input class='form-control' name='tick$f' type='checkbox' value='$tqsi' title='$item' data-toggle='tooltip' data-placement='top' style='margin:1px; width:20px; height:15px;' class='checkbox' onclick='UpdateCost()' id='sum_m_$f'> </div><div class='col-xs-5 text-left' style='padding-left:1px;'> $item&nbsp;&nbsp;<font color='blue'>$sizi</font><input type='hidden' name='size$f' value='$sizi'><input type='hidden' name='num$f' value='$nume'></label></div>";
	
	        if($f%2=='0')
	        echo"<div class='row'> </div>";
	 }
	
echo"<div class='row'> </div><hr>
	    
	    <div class='col-xs-3 text-center'><input class='form-control text-center' type='text' name='tsq' id='totalcost' value='' onchange='format(this);' style='text-align:center; height:25px; margin:0px 0px 0px 0px; padding:0px 10px 0px 0px; float:right;' readonly></div>
	    <div class='col-xs-1 text-center'> </div>
	    
	    <div class='col-xs-4 text-right'>
	   <input name='taxes' class='form-control' type='text' onkeyup='format(this);' onkeypress='return isNumberKey(event)' style='text-align:center; height:25px; margin:0px 0px 0px 0px; padding:0px 10px 0px 0px; float:right;' placeholder='Total Tax'></div>
	   
	   <div class='col-xs-4 text-right'>
	   <input name='vates' class='form-control' type='text' onkeyup='format(this);' onkeypress='return isNumberKey(event)' style='text-align:center; height:25px; margin:0px 0px 0px 0px; padding:0px 10px 0px 0px; float:right;' placeholder='Vat Inward'></div> 
	    
	    <div class='row'> </div></div></div></div><hr>
	
            <input type='hidden' name='invo' value='$invo'>  
            <input type='hidden' name='name' value='$name'>  
            <input type='hidden' name='dati' value='$dati'>    
            <input type='hidden' name='p' value='$p'>    
            <input type='hidden' name='t' value='$t'>
	    <input type='hidden' name='dato' value='$dato'>
	    <input type='hidden' name='datos' value='$datos'>
	    <input type='hidden' name='custo' value='$custo'>   
            <input type='hidden' name='vous' value='$vous'> 
            
      <div class='modal-footer' style='margin-top:-10px; height:50px; padding-top:5px; border:0px solid blue;'>
        <button type='button' class='btn btn-sm btn-danger' data-dismiss='modal' style='width:95px;'><i class='lnr lnr-cross-circle'></i> CANCEL </button>
        <button type='submit' name='addta' class='btn btn-sm btn-success' style='width:95px;'><i class='lnr lnr-checkmark-circle'></i> SAVE </button>
      </div></form>
    </div>
  </div>
</div>";

					
		
		
		
		
		echo"<div id='togglezText' style='display:none; padding:0px; margin-top:20px; border:1px solid #D8D7D6; border-radius:5px; margin-bottom:80px;'><div class='modal-header' style='margin-bottom:40px; margin-top:0px;'><h5 class='modal-title' id='exampleModalLabel'>ADD ITEMS <label class='pull-right'><b>$name  &nbsp; [$invo]</b></label></h5>

    </div><div style='text-align:center; height:auto; width:80%;'></form>
      <form action='' method='post' enctype='multipart/form-data'>";
	
	
// ********************** Add a new items *****************************	
		echo"<div class='row'> </div>
		
			<div class='form-group'><div class='col-md-2'> </div>
			<div class='col-md-3' align='right'> 
    <label class='control-label' style='padding-top:10px;'>Item Name</label>
        </div>
            <div class='col-md-4'>
<input name='item' class='form-control' id='searchis' type='text'>
            </div></div>
		<div class='row'> </div>
		
		<div class='form-group'><div class='col-md-2'> </div>
			  <div class='col-md-3' align='right'>
			  <label class='control-label' style='padding-top:10px;'>Quantity/Unit </label></div>	

	 <div class='col-md-3'>
          <input name='qty' class='form-control text-center' type='text' onkeyup='format(this);' onkeypress='return isNumberKey(event)' required>
            </div><div class='col-sm-1' style='padding-left:0px; margin-left:0px;'>
	    
	    <select class='form-control' name='unit' title='Count Unit' data-toggle='tooltip' data-placement='top' style='margin-left:0px; padding-left:2px; padding-right:2px;' required>
	    <option value='' selected='selected'>UNIT</option>
	    <option value='SQM'> SQM </option>
			<option value='PCS'> PCS </option>
			<option value='SET'> SET </option>
			<option value='SHEET'> SHEET </option>";
			
		   echo"</select></div>
		</div>
		
		<div class='row'> </div>
		
			<div class='form-group'><div class='col-md-2'> </div>
			<div class='col-md-3' align='right'> 
    <label class='control-label' style='padding-top:10px;'>Total Weight</label>
        </div>
            <div class='col-md-3'>
           <div class='input-group'>
<span class='input-group-addon' style='width:20%; text-align:center; padding-left:0px; padding-right:0px;'> KG </span>	
      <input class='form-control form-center' name='wei' class='form-control text-center' onkeyup='format(this);' onkeypress='return isNumberKey(event)' type='text' required></div>
              </div><div class='col-md-1 text-center text-warning' style='padding-top:15px; padding-bottom:0px;'><b> RATE&nbsp;&nbsp;&nbsp;&nbsp; </b></div></div>
	
		<div class='row'> </div>
		
			<div class='form-group'><div class='col-md-2'> </div>
			<div class='col-md-3' align='right'> 
    <label class='control-label' style='padding-top:10px;'>Total Amount</label>
        </div>
            <div class='col-md-3'>
           <div class='input-group'>
<span class='input-group-addon' style='width:20%; text-align:center; padding-left:0px; padding-right:0px;'>$</span>	
      <input class='form-control form-center' name='amo' class='form-control text-center' onkeyup='format(this);' onkeypress='return isNumberKey(event)' type='text' required></div>
</div><div class='col-md-1' style='padding-left:0px;'><input class='form-control form-center' name='rat' value='$payo' class='form-control text-center' onkeyup='format(this);' onkeypress='return isNumberKey(event)' type='text' style='padding-left:5px; padding-right:5px;' required></div></div>
              
              <div class='row'> </div><hr width='80%'>
            <input type='hidden' name='type' value='$type'>  
            <input type='hidden' name='purpo' value='$purpo'>  
            <input type='hidden' name='conte' value='$conte'>  
            <input type='hidden' name='lcno' value='$lcno'>  
            <input type='hidden' name='invo' value='$invo'>  
            <input type='hidden' name='name' value='$name'>  
            <input type='hidden' name='dati' value='$dati'>   
            <input type='hidden' name='vous' value='$vous'>       
            <input type='hidden' name='lcno' value='$lcno'>    
            <input type='hidden' name='p' value='$p'> 
    <div class='col-md-4'> </div>
    <div class='col-md-6'> <button type='button' class='btn btn-xs btn-danger' data-dismiss='modal' style='width:120px; height:36px;' onclick=location.href='purchase.php'><i class='lnr lnr-chevron-left-circle'></i> CANCEL </button><button type='submit' name='addo' class='btn btn-xs btn-success' style='width:120px; height:36px;'><i class='lnr lnr-checkmark-circle'></i> SAVE </button></div></form>
    
        <div class='row'> </div><br><br>";
              
         // ********************** End of add item **************************     
		
		echo"</div></div>";
		
		include'chasing.php';
		}
		else{
		    $cal=mysqli_query($conn, "UPDATE `purchase` SET `Calcul`='0' WHERE `Calcul`!='0' ORDER BY `Number` ASC LIMIT 100");
		    echo"</div>";
		    $_SESSION['Vous']='';
		}
            ?>
                    </div>
							</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';
   ?>