<?php
session_start();
ob_start();
require "../model/configuration.php";
extract($_POST);


if($mechid=='1')

     {
		 
		 
		 $colours = $paperinkColor;
    $colourcount = explode(",", $colours);
  $plateset = count($colourcount);
		 
//Paper Needed
list($power, $printingsize) = explode("/", $printsize);
$neededcopy1 = $neededcopy;
list($product1, $a) = explode("|", $product);
$neededcopy1 = $neededcopy;
if($printingsize==8)
{
	$neededcopy=$neededcopy/2;
}

if($printingsize==16)
{
	$neededcopy=$neededcopy/4;
}
if($printingsize == 24)
{
	$neededcopy=$neededcopy/3;
}
if($printingsize == 10)
{
	$neededcopy=$neededcopy/5;
}
if($printingsize == 12)
{
	$neededcopy=$neededcopy/3;
}

//$plateset = 4;
$paperneeded = paperneeded($sheettoprint,$printingsize,$neededcopy1);
//Paper Coast Start
$productselect = $db->execute("select sellprice from products where productcode='$product1'");
$productresult = $productselect->fetch_assoc();
$papercoast = $paperneeded * $productresult['sellprice'];
//Paper Coast End

$plateneededforoneside = platecountforsinglesideabdick($sheettoprint,$printingsize) * $plateset;
$plateneededfortwoside = platecountfordoublesideabdick($sheettoprint,$printingsize) * $plateset;
//Basic Impression Charge
$mechinequery = $db->execute("select * from machines_table where id='1'");
$mechineresult = $mechinequery->fetch_assoc();
$additionalimpressioncharge = $mechineresult['additional_impression_charge'];
if($backprint == 'Yes')
{
	$platecount = $plateneededfortwoside;
	$basicimpression = $plateneededfortwoside * $mechineresult['impression_charge'];
	$additionalimpression = additionalimpressionchargesingleside1($sheettoprint,$printingsize,$neededcopy,$platecount,$additionalimpressioncharge);
}
else
{
	$platecount = $plateneededforoneside;
	if($dupprint=='Yes')
	  {
		  if($neededcopy <= 500)
		      {
		     $basicimpression=0;
			  }
			  else
			  {
		  $basicimpression = $plateneededforoneside * $additionalimpressioncharge;
			  }
	      $additionalimpression = additionalimpressionchargesingleside1($sheettoprint,$printingsize,$neededcopy,$platecount,$additionalimpressioncharge);
		  
	  }
	  else
	  {
	$basicimpression = $plateneededforoneside * $mechineresult['impression_charge'];
	$additionalimpression = additionalimpressionchargesingleside1($sheettoprint,$printingsize,$neededcopy,$platecount,$additionalimpressioncharge);
	  }
}

//Basic Impression Charge
//plate cost start
$mchprice = $db->execute("select * from other_settings where id='7'");
$mchqro = $mchprice->fetch_assoc();
$totalplatecost=$platecount* $mchqro['amount'];
//platecost end

//Additional Impression Charge Start
 $impressioncharge=$basicimpression+$additionalimpression;
//Additional Impression Charge End

//$samematter;
//echo $impressioncharge."-".$platecount."-".$paperneeded;
echo $platecount;
//echo $stitching."-".$platecount."-".$pasting."-".$prntlastcopy."-".$dupprint;
//echo $basicimpression;
if($prntlastcopy=='Yes')
{
	$totalplatecost=0;
	$impressioncharge=0;
}
if($dupprint=='Yes')
{
	$totalplatecost=0;
}
$_SESSION['papercoast'] = $papercoast;
$_SESSION['dummypaper'] = $paperneeded;
$_SESSION['printamt'] = $impressioncharge;
$_SESSION['platecoast'] = $totalplatecost;

		 
	 }


if($mechid=='2')

     {
		 
	
	$colours = $paperinkColor;
    $colourcount = explode(",", $colours);
  $plateset = count($colourcount);
		 
//Paper Needed
list($power, $printingsize) = explode("/", $printsize);
list($product1, $a) = explode("|", $product);

//$plateset = 4;
$paperneeded = paperneeded($sheettoprint,$printingsize,$neededcopy);
//Paper Coast Start
$productselect = $db->execute("select sellprice from products where productcode='$product1'");
$productresult = $productselect->fetch_assoc();
$papercoast = $paperneeded * $productresult['sellprice'];
//Paper Coast End

$plateneededforoneside = platecountforsingleside($sheettoprint,$printingsize) * $plateset;
$plateneededfortwoside = platecountfordoubleside($sheettoprint,$printingsize) * $plateset;
//Basic Impression Charge
$mechinequery = $db->execute("select * from machines_table where id='2'");
$mechineresult = $mechinequery->fetch_assoc();
$additionalimpressioncharge = $mechineresult['additional_impression_charge'];
if($backprint == 'Yes')
{
	$platecount = $plateneededfortwoside;
	$basicimpression = $plateneededfortwoside * $mechineresult['impression_charge'];
	$additionalimpression = additionalimpressionchargeside($sheettoprint,$printingsize,$neededcopy,$plateset,$additionalimpressioncharge);
}
else
{
	$platecount = $plateneededforoneside;
	$basicimpression = $plateneededforoneside * $mechineresult['impression_charge'];
	$additionalimpression = additionalimpressionchargesingleside($sheettoprint,$printingsize,$neededcopy,$plateset,$additionalimpressioncharge);
}

//Basic Impression Charge
//plate cost start
$mchprice = $db->execute("select * from other_settings where id='5'");
$mchqro = $mchprice->fetch_assoc();
$totalplatecost=$platecount* $mchqro['amount'];
//platecost end

//Additional Impression Charge Start
 $impressioncharge=$basicimpression+$additionalimpression;
//Additional Impression Charge End

//$samematter;
//echo $impressioncharge."-".$platecount."-".$paperneeded;
if($prntlastcopy=='Yes')
{
	$totalplatecost=0;
	$impressioncharge=0;
}
if($dupprint=='Yes')
{
	$totalplatecost=0;
}

echo $platecount;
$_SESSION['papercoast'] = $papercoast;
$_SESSION['dummypaper'] = $paperneeded;
$_SESSION['printamt'] = $impressioncharge;
$_SESSION['platecoast'] = $totalplatecost;

	 }









else if($mechid=='3')

     {
		 
		 
//Paper Needed
list($power, $printingsize) = explode("/", $printsize);
list($product1, $a) = explode("|", $product);

$plateset = 4;
$paperneeded = paperneeded($sheettoprint,$printingsize,$neededcopy);
//Paper Coast Start
$productselect = $db->execute("select sellprice from products where productcode='$product1'");
$productresult = $productselect->fetch_assoc();
$papercoast = $paperneeded * $productresult['sellprice'];
//Paper Coast End

$plateneededforoneside = platecountforsingleside($sheettoprint,$printingsize) * $plateset;
$plateneededfortwoside = platecountfordoubleside($sheettoprint,$printingsize) * $plateset;
//Basic Impression Charge
$mechinequery = $db->execute("select * from machines_table where id='3'");
$mechineresult = $mechinequery->fetch_assoc();
$additionalimpressioncharge = $mechineresult['additional_impression_charge'];
if($backprint == 'Yes')
{
	$platecount = $plateneededfortwoside;
	$basicimpression = $plateneededfortwoside * $mechineresult['impression_charge'];
	$additionalimpression = additionalimpressionchargeside($sheettoprint,$printingsize,$neededcopy,$plateset,$additionalimpressioncharge);
}
else
{
	$platecount = $plateneededforoneside;
	$basicimpression = $plateneededforoneside * $mechineresult['impression_charge'];
	$additionalimpression = additionalimpressionchargesingleside($sheettoprint,$printingsize,$neededcopy,$plateset,$additionalimpressioncharge);
}

//Basic Impression Charge

//plate cost start
$mchprice = $db->execute("select * from other_settings where id='5'");
$mchqro = $mchprice->fetch_assoc();
$totalplatecost=$platecount* $mchqro['amount'];
//platecost end

//Additional Impression Charge Start
 $impressioncharge=$basicimpression+$additionalimpression;
//Additional Impression Charge End
//$samematter;
//echo $basicimpression."-".$additionalimpression."-".$platecount;
echo $platecount;


$_SESSION['papercoast'] = $papercoast;
$_SESSION['dummypaper'] = $paperneeded;
$_SESSION['printamt'] = $impressioncharge;
$_SESSION['platecoast'] = $totalplatecost;


	 }






















function paperneeded($printsheets,$printsizes,$needcopy) 
		{
            $value = $printsheets / $printsizes;
			list($x, $y) = explode(".", $value);
			$y = "0." . $y;
			if ($y > '0.5')
   				 {
	   				 $y = ceil($y);
   				 }
				$val = $needcopy * ($x + $y);
				return $val;
         }
		 
		 
function platecountforsingleside($printsheets,$printsizes)
{
	$value = $printsheets / $printsizes;
	list($x, $y) = explode(".", $value);
	$y = "0." . $y;
	$y = ceil($y);
	return ($x + $y);
}


function platecountfordoubleside($printsheets,$printsizes)
{
	$value = $printsheets / $printsizes;
	list($x, $y) = explode(".", $value);
	$y = "0." . $y;
	if ($y <= '0.5')
   		{
	   		$y = ceil($y);
   		}
		elseif($y > '0.5')
		{
			$y = ceil($y) * 2;
		}
	return (($x * 2)+ $y);
}



function platecountforsinglesideabdick($printsheets,$printsizes)
{
	return ($printsheets);
}


function platecountfordoublesideabdick($printsheets,$printsizes)
{
	return ($printsheets*2);
}



function additionalimpressionchargesingleside($printsheets,$printsizes,$needcopy,$plateset,$additionalimpressioncharge) 
{
                       $value = $printsheets / $printsizes;
			list($x, $y) = explode(".", $value);
			$y = "0." . $y;
			if($needcopy > 1000)
			{
				$result = $needcopy/1000;
				$result= (($result-1) * $x) * $additionalimpressioncharge * $plateset;
			}
			
			$multiplevalue=$y * $needcopy;
			$res=$multiplevalue/1000;
			$res=ceil($res);
			$res=$res-1;
			if($res>0)
			{
			$res = $res * $additionalimpressioncharge * $plateset;
			}
			else
			{
				$res=0;
			}
			return $result+$res;
	    
}


function additionalimpressionchargeside($printsheets,$printsizes,$needcopy,$plateset,$additionalimpressioncharge) 
{

                   	$value = $printsheets / $printsizes;
					list($x, $y) = explode(".", $value);
					$y = "0." . $y;
					
					 if($y==0)
							     {
									$result = $needcopy/1000;
									$result = ceil($result);
							        $result= (($result-1) * $x) * $additionalimpressioncharge * $plateset;						
							        $result= $result*2;
									return($result);
								 }
					
					
					if($y<='0.5')
					   {
						   	$result = $needcopy/1000;
							$result= (($result-1) * $x) * $additionalimpressioncharge * $plateset;						
							$result= $result*2;
					   }
					   else
					   {
							if($needcopy > 1000)
								{
								$result = $needcopy/1000;
								$result1=(($result) * $x) * $additionalimpressioncharge * $plateset;
								$result= (($result-1) * $x) * $additionalimpressioncharge * $plateset;						
								$result= $result+$result1;
								}
					   }

								if ($y <= '0.5')
   									{
	   									$multiplevalue=$y * $needcopy;
										$multiplevalue=$multiplevalue*2;
										$res=$multiplevalue/1000;
											$res=ceil($res);
										//$back=$res;
										$res=$res-1;
										if($res>0)
											{
												$res = $res * $additionalimpressioncharge * $plateset;
												//$back = $back * $additionalimpressioncharge * $plateset;
												$res=$res;
											}
										else
											{
												$res=0;
											}
											}
							  else if($y > '0.5')
							   {
										$y = ceil($y);
										$multiplevalue=$y * $needcopy;
										$res=$multiplevalue/1000;
										$res=ceil($res);
										$res=$res-1;
										if($res>0)
											{
											$res = $res * $additionalimpressioncharge * $plateset;
											$res=$res*2;
											}
										else
											{
											$res=0;
											}
							   }
							   
							   
							   $finalresult=$result+$res;
							   
							  
							   if($finalresult > 0)
							      {
									  return($finalresult);
								  }
								  else
								  {
									  return 0;
								  }
						  
}


//for abdick

function additionalimpressionchargesingleside1($printsheets,$printsizes,$needcopy,$plateset,$additionalimpressioncharge) 
{
            
			if($needcopy > 1000)
			{
				$result = $needcopy/1000;
				$result = ceil($result);
				$result= ($result-1) ;
				$result= $result * $additionalimpressioncharge;
				 $result= $result * $plateset;
			}
			
			return $result;
	    
}





?>