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


include'header.php';

       		// 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'");
		}
		
		
	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'];
			$account=$_POST['acco'];
			$cut = explode("?", $account);
            $acco = $cut[0]; 
            $dacco = $cut[1];
			
			$amo=str_replace(',', '', $_POST['amo']);
			$descri=str_replace("'", "`", $_POST['descri']);
			$rats=str_replace(',', '', $_POST['rats']);
			$taxes=$_POST['tax'];
			$cut = explode("?", $taxes);
            $tax = $cut[0]; 
            $part = $cut[1];
			
	$do=mysqli_query($conn, "SELECT `Code` FROM `currency` WHERE `Rate`='$rats' ORDER BY `Code` ASC");
	    $roi=mysqli_fetch_assoc($doi);
			 $curre=$roi['Code'];
			
	$doi=mysqli_query($conn, "INSERT INTO `loading` (`Date`, `User`, `Voucher`, `Cost`, `Amount`, `Rate`, `Descri`, `Invoice`, `Dati`, `Suppli`, `Status`, `Currency`, `Account`) VALUES ('$dase', '$loge', '$vous', '$tax', '$amo', '$rats', '$descri', '$invo', '$dati', '$name', '2', '$curre', '$acco')");
	
	$and=mysqli_query($conn, "INSERT INTO `deposit` (`Number`, `Date`, `Time`, `User`, `Item`, `Refer`, `Amount`, `Customer`, `Operation`, `Status`, `Valid`, `Account`, `Descri`, `Client`, `Voucher`, `Source`, `Record`, `Bank`, `Rate`, `Classy`) VALUES (NULL, '$dase', '$Time', '$loge', '$part', 'x2', '$amo', 'RRA', 'WITHDRAWAL', '0', '0', '$acco', '$descri', '$vous', '0', 'DIRECT', '0', '$dacco', '$rats', '')");
             $last_id = mysqli_insert_id($conn);
		    }
		
	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'];
			$acco=$_POST['acco'];
			$amo=$_POST['amo'];
			$ras=$_POST['ras'];
			$dte=$_POST['dte'];
			
	$do=mysqli_query($conn, "DELETE FROM `loading` WHERE `Number`='$num' ORDER BY `Number` ASC LIMIT 1");
			
	$doi=mysqli_query($conn, "DELETE FROM `deposit` WHERE `Amount`='$amo' AND `Date`='$dte' AND `Rate`='$ras' AND `Operation`='WITHDRAWAL' AND `Customer`='RRA' AND `Account`='$acco' AND `Client`='$vous' ORDER BY `Number` ASC LIMIT 1");
		}
		
		
		if(isset($_POST['upco']))
		{
			$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'];
			$acco=$_POST['acco'];
			$amo=$_POST['amo'];
			$ras=$_POST['ras'];
			$dte=$_POST['dte'];
			$account=$_POST['account'];
			
	$do=mysqli_query($conn, "UPDATE `loading` SET `Account`='$account' WHERE `Number`='$num' ORDER BY `Number` ASC LIMIT 1");
	
		$doi=mysqli_query($conn, "SELECT `Account` FROM `baccount` WHERE `Number`='$account' ORDER BY `Account` ASC");
			$roi=mysqli_fetch_assoc($doi);
				$fna=$roi['Account'];
			
	$don=mysqli_query($conn, "UPDATE `deposit` SET `Account`='$account', `Bank`='$fna' WHERE `Amount`='$amo' AND `Date`='$dte' AND `Rate`='$ras' AND `Operation`='WITHDRAWAL' AND `Customer`='RRA' AND `Account`='$acco' AND `Client`='$vous' 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="purchase.php" method="post">
	<div class="col-lg-3 hidden-print" style="padding-left:50px; padding-right:50px;">
        <?php
        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>";
        ?>
        
		</div><div class="col-lg-1" style="padding-left:40px;"></div>

        <form action="purchase.php" 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
		$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 `Voucher`='$vous' GROUP BY `Voucher` ORDER BY `Date` ASC");
				if($fo=mysqli_num_rows($dok)){
				?>
                 
            <div class="divFooter"><center><u><b>PURCHASE REPORT No <?php echo $vous ?></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;' colspan='2'><div align='center'> TAXES </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="";
					    
		
			$vous=$rok['Voucher'];
		    $pay=$rok['Rate'];
		    
		    
					    $bal=$amo*$pay;
					$balo=number_format($bal, 2);
					$payo=number_format($pay, 2);
				
	print("<tr title='$purpo' data-toggle='tooltip' data-placement='top'>
	<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'><div align='right'> $weio </td><td style='$stl $clr $dv'> $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'>
		<a href='#' data-placement='top' data-toggle='modal' data-target='#ratepModal$i'> $payo </a></td>
	<td style='$stl $clr'><div align='right'> $balo </td>
	<td style='$stl $clr'><div align='right'> $taxes </td>
	<td style='$stl $clr'>");

    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"</td></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'>
		    <div align='center'><b><?php echo $tsqo ?></b> &nbsp; </div></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>&nbsp;</div></th></tr>
    	</table><div class="text-right" style="padding-right:30px; padding-top:5px;">
		    
	    </div>
	    
	        <?php
	        include'taxing.php';
	        ?></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';
   ?>