<?php
session_start();
if($_SESSION['Access']==null or $_SESSION['Loge']==null){
    ?>
<script type="text/javascript">
window.close();
</script>
<?php
}
?>
<!DOCTYPE html>
<html>
<head>
    <title> COST CALCULATION </title>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script language="JavaScript" type="text/javascript">
	function printpage()
  {  
window.print();
  } 
</script>

<style type="text/css">
@media print {
  .DONTPrint{ display:none }
  .DOCheck{ display:table}
			}
</style>

</head>
<body><center>
    <div style="width:99%; border:1px solid teal; border-radius:5px; height:25px; padding-top:10px; float:center;"><B> COST CALCULATION </B></div><br>

<?php
include'connection.php';
$vous=$_SESSION['Vous'];
            
	$dok=mysqli_query($conn, "SELECT *FROM `purchase` WHERE `Status`='0' AND `Voucher`='$vous' ORDER BY `Number` ASC");
		?>
		
	    <div style="width:99%; border:1px solid teal; border-radius:5px; height:auto; padding:5px; float:center;">
		<table class="table table-striped table-hover" width="100%">
		    <thead><tr role="row">
    <th style="padding:1px; font-size:12px; text-align:center" width='2%'>&nbsp;&nbsp;&nbsp;No&nbsp;&nbsp;</th>
    <th style="padding:1px; font-size:12px; text-align:center"> ITEM </th>
	<th style="padding:1px; font-size:12px; text-align:center"> SIZE </th>
	<th style="padding:1px; font-size:12px; text-align:center"> WEIGHT </th>
	<th style="padding:1px; font-size:12px; text-align:center"> KG/CTN </th>
	<th style="padding:1px; font-size:12px; text-align:center"> CTN </th>
	<th style="padding:1px; font-size:12px; text-align:center"> QTY </th>
	<th style="padding:1px; font-size:12px; text-align:center"> COST </th>
    <th style="padding:1px; font-size:12px; text-align:center"> TOTAL </th>
    <th style="padding:1px; font-size:12px; text-align:center"> TOT.RWF </th>
	<th style="padding:1px; font-size:12px; text-align:center"> CHARGES </th>
	<th style="padding:1px; font-size:12px; text-align:center"> TAXES </th>
	<th style="padding:1px; font-size:12px; text-align:center"> IN.VAT </th>
	<th style="padding:1px; font-size:12px; text-align:center"> OUT.VAT </th>
	<th style="padding:1px; font-size:12px; text-align:center"> OUT-IN </th>
	<th style="padding:1px; font-size:12px; text-align:center"> TOTAL </th>
	<th style="padding:1px; font-size:12px; text-align:center"> PRICE </th>
	<th style="padding:1px; font-size:12px; text-align:center"> SELLING </th>
	<th style="padding:1px; font-size:12px; text-align:center"> PROFIT </th>
	    </tr></thead><tbody>
					<?php	
			
	$i=1;			$tam=$tout=$tto=0;          $ttax=$tvat=$ttr=0;
	                $tkg=$tcn=$tqt=$twe=0;
		while($rok=mysqli_fetch_assoc($dok)){
				$code=$rok['Number'];
				$dati=$rok['Date'];
				$name=$rok['Supplier'];
				$ino=$rok['Itemno'];
			$invo=$rok['Invoice'];
	        $amo=$rok['Amount'];			$amoo=number_format($amo, 2);
			$item=$rok['Itemna'];
			$qty=$rok['Quantity'];           $qto=number_format($qty, 2);
			$unit=$rok['Unit'];
			$wei=$rok['Weight'];             $weio=number_format($wei, 2);
			$tax=$rok['Tax'];                $taxo=number_format($tax, 2);
			$vat=$rok['Vat'];                $vato=number_format($vat, 2);
			$cost=$amo/$qty;                 $costo=number_format($cost, 2);
					$stn="padding:1px; font-size:12px; text-align:right;";
					$stl="padding:1px; font-size:12px;";
			
			$vous=$rok['Voucher'];
		    $pay=$rok['Rate'];
		    $size=$rok['Size'];
		    $tow=$rok['Totwei'];
		    $prix=$rok['Price'];            $prixo=number_format($prix, 2);
			$tr=$amo*$pay;                  $tro=number_format($tr, 2);
		    
    	$char=mysqli_query($conn, "SELECT SUM(`Amount`*`Rate`) AS `Chs` FROM `loading` WHERE `Status`='0' AND `Voucher`='$vous' ORDER BY `Number` ASC");
			    $rar=mysqli_fetch_assoc($char);
				    $cha=$rar['Chs'];           $chao=number_format($cha, 2);  
				    $nha=$cha/$tow*$wei;         $nhao=number_format($nha, 2);
				    if($vat==0)
				        $out=0;
				    else
				        $out=$prix*$qty*18/118;       
				        
				$outo=number_format($out, 2);
				$diff=$out-$vat;              $diffo=number_format($diff, 2);
				$tot=$tr+$tax+$vat+$nha;
				$toto=number_format($tot, 2);
				$pri=($tot+$diff)/$qty;
				$prio=number_format($pri, 2);
				$rat=number_format(($prix-$pri)*100/$pri, 2);
		
		$_SESSION['Base']="jblgroup_sales";
        include'sales.php';		
	$sel=mysqli_query($cons, "SELECT `Weight`, `Uprice`, `Size` FROM `items` WHERE `Number` = '$ino' AND `Weight`>'0' ORDER BY `Itemna` ASC");
	if($fel=mysqli_num_rows($sel)){
            $row=mysqli_fetch_assoc($sel);
            $wct = $row['Weight'];              $wcto=number_format($wct, 2);
            $ctn = $wei/$wct;                   $ctno=number_format($ctn);
	}
	else{
	    $wcto=$ctno="--";
	}
            

	print("<tr><td style='text-align:right; $stl'> $i&nbsp;&nbsp;</td>
	<td style='$stl'> $item </td><td style='$stl'> $size </td>
	<td style='$stn'> $weio </td><td style='$stn'> $wcto </td>
	<td style='$stn'> $ctno </td><td style='$stn'> $qto$unit </td>
	<td style='$stn'> $costo </td><td style='$stn'> $amoo </td>
	<td style='$stn'> $tro </td><td style='$stn'> $nhao </td>
	<td style='$stn'> $taxo </td><td style='$stn'> $vato </td>
	<td style='$stn'> $outo </td><td style='$stn'> $diffo </td>
	<td style='$stn'> $toto </td><td style='$stn'> $prio </td>
	<td style='$stn'> $prixo </td><td style='$stn'> $rat% </td></tr>");
	
	$cal=mysqli_query($conn, "UPDATE `purchase` SET `Cost`='$pri' WHERE `Number`='$code' AND `Voucher`='$vous' ORDER BY `Number` ASC LIMIT 1");
	
	        $ttax+=$tax;            $tvat+=$vat;            $ttr+=$tr;
	        $tam+=$amo;             $tout+=$out;            $tto+=$tot;
	        $tkg+=$wct;             $tcn+=$ctn;             $tqt+=$qty;
	        $twe+=$wei;             $i++;
		}
		$tdif=$tout-$tvat;                      $tdif=number_format($tdif, 2);
	$ttr=number_format($ttr, 2);                $tvat=number_format($tvat, 2);
	$ttax=number_format($ttax, 2);              $tam=number_format($tam, 2);
	$tout=number_format($tout, 2);              $tto=number_format($tto, 2);
	$tkg=number_format($tkg, 2);                $tcn=number_format($tcn);
	$tqt=number_format($tqt, 2);                $twe=number_format($twe, 2);             
    ?>
    
    </tbody><thead><tr>
		<th colspan='3' style="font-size:12px;"><b> TOTAL AMOUNT </b></th>
				<th style="padding-right:1px; font-size:12px;">
				<div align='right'><b><?php echo $twe ?></b></div></th>
				<th style="padding-right:1px; font-size:12px;">
				<div align='right'><b><?php echo $tkg ?></b></div></th>
				<th style="padding-right:1px; font-size:12px;">
				<div align='right'><b><?php echo $tcn ?></b></div></th>
				<th style="padding-right:1px; font-size:12px;">
				<div align='center'><b><?php echo $tqt ?></b></div></th>
				<th style="padding-right:1px; font-size:12px;">
				<div align='center'><b>--</b></div></th>
				
				<th style="padding-right:1px; font-size:12px;">
				<div align='right'><b>$<?php echo $tam ?></b></div></th>
				<th style="padding-right:1px; font-size:12px;">
				<div align='right'><b><?php echo $ttr ?></b></div></th>
				<th style="padding-right:1px; font-size:12px;">
				<div align='right'><b><?php echo $chao ?></b></div></th>
				<th style="padding-right:1px; font-size:12px;">
				<div align='right'><b><?php echo $ttax ?></b></div></th>
				<th style="padding-right:1px; font-size:12px;">
				<div align='right'><b><?php echo $tvat ?></b></div></th>
				<th style="padding-right:1px; font-size:12px;">
				<div align='right'><b><?php echo $tout ?></b></div></th>
				<th style="padding-right:1px; font-size:12px;">
				<div align='right'><b><?php echo $tdif ?></b></div></th>
				<th style="padding-right:1px; font-size:12px;">
				<div align='right'><b><?php echo $tto ?></b></div></th>
				<th style="padding-right:1px; font-size:12px;">
				<div align='center'><b>--</b></div></th>
				<th style="padding-right:1px; font-size:12px;">
				<div align='center'><b>--</b></div></th>
				<th style="padding-right:1px; font-size:12px;">
				<div align='center'><b>--</b></div></th>
				
				</tr>
    	</table></div><br>
    	
    	<?php            
	$_SESSION['excell']='calcul.php';
	$_SESSION['filename']="Cost Calculation";
	
    		print("<div class='DONTPrint'><BR><HR WIDTH=92%>
				<table width=90% border=0><tr><td width=35%><div align='left'>
				<button type='button' style='background-color:Transparent; width: 90px; border: 0px; height: 20px; color: #FF0000; cursor:pointer' onclick='window.close()'> BACK </button></td>
				<td><div align='center'><a href='#' onclick=window.location.href='data.php' title='Download excel sheet' style='background-color:Transparent; width: 90px; border: 0px; height: 20px; color: #FF0000; cursor:pointer; text-decoration:none;'> MS Excel </a></td>
				<td width='35%'><div align='right'><button type='button' onclick='javascript:printpage()' style='background-color:Transparent; width: 90px; border: 0px; height: 20px; color: #FF0000; cursor:pointer;'> PRINT </button></td></tr></table><BR><BR>");
				?>
</body>
</html>

