<?php

session_start();

include("../include/config.php");

include("../include/functions.php");

include("../include/simpleimage.php"); 

 validate_admin();

  $product_id=ucfirst(mysql_real_escape_string($_POST['product_id']));

  $size=mysql_real_escape_string($_REQUEST['size']);
  $sizetype=mysql_real_escape_string($_REQUEST['sizetype']);
  $tax=mysql_real_escape_string($_POST['tax']);
  $tax_method=mysql_real_escape_string($_POST['tax_method']);
  $actual_price=mysql_real_escape_string($_REQUEST['actual_price']);

  $mrp_price=mysql_real_escape_string($_REQUEST['mrp_price']);

  $discount=mysql_real_escape_string($_REQUEST['discount']);

  $sell_price=mysql_real_escape_string($_REQUEST['sell_price']);
  $minimum_quantity=mysql_real_escape_string($_POST['minimum_quantity']);
  $wholesale_discount=mysql_real_escape_string($_POST['wholesale_discount']);

  $in_stock=mysql_real_escape_string($_REQUEST['in_stock']);

  $video=mysql_real_escape_string($_REQUEST['video']);

  $barcode_number=mysql_real_escape_string($_REQUEST['barcode_number']);
if($sizetype=='KG'){
$weight= $size*2.20462;
}elseif($sizetype=='LTR'){
$weight= $size*2.204;
}elseif($sizetype=='OZ'){
$weight= $size*0.0625;
}elseif($sizetype=='GM'){
$weight= $size*0.00220462;
}elseif($sizetype=='ML'){
$weight= $size*.00220462;
}elseif($sizetype=='GAL'){
$weight= $size*8.35;
}elseif($sizetype=='FLOZ'){
$weight= $size*0.0625;
}elseif($sizetype=='LBS'){
$weight= $size*1;
}else{
$weight='';}
  if($tax_method=='Amount'){
	    $tax_value=$tax;
  } 
  if($tax_method=='Low Tax'){
	    $tax_value=($mrp_price*2.25)/100;
  }
  if($tax_method=='High Tax'){
	    $tax_value=($mrp_price*10.25)/100;
  }
  if($tax_method=='City of Chicago/Soda Tax'){
	    $tax_value=((($mrp_price*10.25/100)+$mrp_price)*3/100)+($mrp_price*10.25/100);
  } 
   

  if($_REQUEST['submitForm']=='yes'){

  if($_FILES['photo']['size']>0 && $_FILES['photo']['error']==''){

  $Image= new SimpleImage();

  $img=time().substr($_FILES['photo']['name'],-5);

  move_uploaded_file($_FILES['photo']['tmp_name'],"../upload_images/product/".$img);

  copy("../upload_images/product/".$img,"../upload_images/product/thumb/".$img);

  copy("../upload_images/product/".$img,"../upload_images/product/tiny/".$img);

  $Image->load("../upload_images/product/thumb/".$img);	  

  $Image->resize(150,150);  

  $Image->save("../upload_images/product/thumb/".$img);	 

  $Image->load("../upload_images/product/tiny/".$img);	  

  $Image->resize(300,300);	  

  $Image->save("../upload_images/product/tiny/".$img);	 

CompressImage('../upload_images/product/'.$img, '../upload_images/product/'.$img, 40);
CompressImage('../upload_images/product/thumb/'.$img, '../upload_images/product/thumb/'.$img, 40);
CompressImage('../upload_images/product/tiny/'.$img, '../upload_images/product/tiny/'.$img, 40);

  }

  if($_FILES['barcode']['size']>0 && $_FILES['barcode']['error']==''){

  $barcodeimg=time().substr($_FILES['barcode']['name'],-5);

  move_uploaded_file($_FILES['barcode']['tmp_name'],"../upload_images/product/barcode/".$barcodeimg);

 

  }

	  

  if($_REQUEST['id']==''){

	  $sql="insert into $wholesale_productprice set product_id='$product_id',sizes='$size',size='$size',size_type='$sizetype',weight='$weight',actual_price='$actual_price',mrp_price='$mrp_price',discount='$discount',sell_price='$sell_price',wholesale_min='$minimum_quantity',wholesale_discount='$wholesale_discount',in_stock='$in_stock' ,barcode='$barcodeimg',barcode_number='$barcode_number',status=1";

	  if($img=='' && $_REQUEST['pimage']!=''){

		 $sql.=" ,pphoto='".$_REQUEST['pimage']."' ";  

	  }

	  if($img!=''){

		 $sql.=" ,pphoto='$img' ";  

	  }

	  

	  $obj->query($sql);

	  $_SESSION['sess_msg']='Product price added sucessfully';  

	  

       }else{ 

	   $sql="update $wholesale_productprice set product_id='$product_id',sizes='$size',size='$size',size_type='$sizetype',weight='$weight',actual_price='$actual_price',mrp_price='$mrp_price',discount='$discount',sell_price='$sell_price',wholesale_min='$minimum_quantity',wholesale_discount='$wholesale_discount',in_stock='$in_stock',video='$video',barcode_number='$barcode_number' ,status=1";

	  if($img=='' && $_REQUEST['pimage']!=''){

		 $sql.=" ,pphoto='".$_REQUEST['pimage']."' ";  

	  }

	  if($img!=''){

		 $sql.=" ,pphoto='$img' ";  

	  }

	   if($barcodeimg!=''){

		 $imageArr=$obj->query("select barcode from $wholesale_productprice where id='".$_REQUEST['id']."' "); 

		 $resultImage=$obj->fetchNextObject($imageArr); 

		 @unlink("../upload_images/product/barcode/".$resultImage->barcode);

		 $sql.=" ,barcode='$barcodeimg' ";  

	   }

	   $sql.=" where id='".$_REQUEST['id']."'  ";  

	   $obj->query($sql);

	   $_SESSION['sess_msg']='Product price updated sucessfully';   

        }

   header("location: wholesale-productprice-list.php?product_id=".$_REQUEST['product_id']);

   exit();

  }      

	   

	   

if($_REQUEST['id']!=''){

$sql=$obj->query("select * from $wholesale_productprice where id=".$_REQUEST['id']);

$result=$obj->fetchNextObject($sql);

}	

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title><?php echo SITE_TITLE; ?></title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link href="css/admin.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" language="javascript">

function validate(obj)

{

if(obj.mrp_price.value=='' || obj.mrp_price.value==0){

alert("Please enter mrp price");

obj.mrp_price.focus();

return false;

}

if(obj.sell_price.value=='' || obj.sell_price.value==0){

alert("Please enter sell price");

obj.sell_price.focus();

return false;

}



}

</script>

<script type="text/javascript">

function calcPrice(){

var mrp_price=document.getElementById('mrp_price').value;	

var dis=document.getElementById('discount').value;

if(mrp_price!='' && dis!='' ){

document.getElementById('sell_price').value=mrp_price-(mrp_price*dis/100);

}

}

</script>

</head>

<body>

<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">

<?php include("header.php") ?>



<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />

<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

<script type="text/javascript">

 $(document).ready(function(){

$('#size').autocomplete({

	 search: function () {},

    source: function (request, response)

    {

        $.ajax(

        {

            url:"sizesSource.php" ,

            dataType: "json",

            data:

            {

                q: request.term,

            },

            success: function (data)

            {

                response(data);

            }

        });

    },

    minLength:1,

   select: function( event, ui ) {

		$( "#size" ).val( ui.item.value );

		return false;

	  }

});

	  

 })

</script>

<tr>

	<td align="right" class="paddRtLt70" valign="top">

		<table width="99%" border="0" cellspacing="0" cellpadding="0">

			<tr>

			

				<td align="right" valign="top">

					<table width="100%" border="0" cellspacing="0" cellpadding="0">

						<tr>

							<td align="left" valign="middle" class="headingbg bodr text14">

					<em><img src="images/arrow2.gif" width="21" height="21" hspace="10" align="absmiddle" /></em>Admin: Add Price/Size For <?php  echo getField('product_name',$wholesale_product,$_REQUEST['product_id']);?>

					<span  style="float:right; padding-right:10px;">

					<input type="button" name="back" value="Back To Products"  class="button" onclick="location.href='wholesale-product-list.php'" />

					<input type="button" name="add" value="View Price/Size"  class="button" onclick="location.href='wholesale-productprice-list.php?product_id=<?php echo $_REQUEST['product_id']; ?>'" /></span>

					</td>

						</tr>

						

						<tr>

							<td height="100" align="left" valign="top" bgcolor="#f3f4f6" class="bodr">

                            <form name="frm" method="POST" enctype="multipart/form-data" action="" onsubmit="return validate(this)">

						<input type="hidden" name="submitForm" value="yes" />

						<input type="hidden" name="id" value="<?php echo $_REQUEST['id'];?>" />

						<input type="hidden" name="product_id" value="<?php echo $_REQUEST['product_id'];?>" />

							<table width="100%" cellpadding="0" cellspacing="0">

									<tr>

										<td align="center" colspan="2" class="paddRt14 paddBot11">

										<font color="#FF0000"><strong><?php echo $_SESSION['sess_msg']; $_SESSION['sess_msg']='';?></strong></font></td>

									</tr>

									<tr>

										<td width="18%" align="right" class="paddBot11 paddRt14">&nbsp;</td>

										<td width="82%" align="left" class="paddBot11"></td>

									</tr>

									

									<tr>

                                      <td align="right" class="paddBot11 paddRt14"><strong>Size :</strong></td>

									  <td align="left" class="paddBot11"><input name="size" type="text" id="size" size="36" value="<?php echo stripslashes($result->size);?>" /> Enter digits only.</td>

							  </tr>
							  <tr>

                                      <td align="right" class="paddBot11 paddRt14"><strong>Size Type :</strong></td>

									  <td align="left" class="paddBot11"><select name="sizetype" style="width:245px;" required >
<?php if($_REQUEST['product_id']!=''){?> <option value="<?php echo $result->size_type; ?>" selected="selected"><?php echo $result->size_type; ?></option><?php } else { ?>

                                      <option value="" selected="selected">Select Size</option>

                                      <?php } $sqlsize=$obj->query("select * from tbl_psizes where status=1 order by psize asc");

while($resultsize=$obj->fetchNextObject($sqlsize)){ ?>

                                      <option value="<?php echo $resultsize->psize; ?>"><?php echo $resultsize->psize; ?></option>

                                      <?php } ?>

                                      </select></td>

							  </tr>

                               <tr>

                                      <td align="right" class="paddBot11 paddRt14"><strong>Actual Price:</strong></td>

							    <td align="left" class="paddBot11"><input name="actual_price" type="text" id="actual_price" size="20" value="<?php echo stripslashes($result->actual_price);?>"  /> <strong><?php echo $website_currency_code; ?></strong></td>

							       </tr>

                              <tr>

                                      <td align="right" class="paddBot11 paddRt14"><strong>MRP Price:</strong></td>

							    <td align="left" class="paddBot11"><input name="mrp_price" type="text" id="mrp_price" size="20" value="<?php echo stripslashes($result->mrp_price);?>"  onkeyup="return calcPrice()"/> <strong><?php echo $website_currency_code; ?></strong></td>

							       </tr>

                                   <tr>

                                      <td align="right" class="paddBot11 paddRt14"><strong>Discount:</strong></td>

								     <td align="left" class="paddBot11"><input name="discount" type="text" id="discount" size="20" value="<?php echo stripslashes($result->discount);?>"  onkeyup="return calcPrice()" />							          <strong> %</strong></td>

							       </tr> 

                                    <tr>

                                      <td align="right" class="paddBot11 paddRt14"><strong>Sell Price (After Discount) :</strong></td>

									  <td align="left" class="paddBot11"><input name="sell_price" type="text" id="sell_price" size="20" value="<?php echo stripslashes($result->sell_price);?>" />

								      <strong> <?php echo $website_currency_code; ?></strong></td>

							       </tr>
<tr>

                                      <td align="right" class="paddBot11 paddRt14"><strong>Wholesale Minimum Quantity :</strong></td>

									  <td align="left" class="paddBot11"><input name="minimum_quantity" type="text" id="sell_price" size="20" value="<?php echo stripslashes($result->wholesale_min);?>"  />

								      </td>

							       </tr>
								   <tr>

                                      <td align="right" class="paddBot11 paddRt14"><strong>Wholesale Discount :</strong></td>

									  <td align="left" class="paddBot11"><input name="wholesale_discount" type="text" id="sell_price" size="20" value="<?php echo stripslashes($result->wholesale_discount);?>" />

								      <strong> per pcs/unit etc.</strong></td>

							       </tr>
									 <tr>

                                      <td align="right" class="paddBot11 paddRt14"><strong>IN Stock:</strong></td>

									  <td align="left" class="paddBot11">

                                      <input type="checkbox" name="in_stock" value="1" <?php if($result->in_stock==1 || $_REQUEST['id']==''){ ?>checked<?php } ?> />

								      </td>

							       </tr>

                                    <tr>

                                      <td align="right" class="paddBot11 paddRt14"><strong>Use Image:</strong></td>

									  <td align="left" class="paddBot11">

                                      <?php $photo=getField('photo',$wholesale_product,$_REQUEST['product_id']);

									  if(is_file("../upload_images/product/thumb/".$photo)) {?>

									   <div style="width:25%; float:left;">

                                         <img src="../upload_images/product/thumb/<?php echo $photo; ?>" /><br/>

                                         <input type="radio" name="pimage" value="<?php echo $photo; ?>" />

                                        </div>

                                       <?php } ?>

                        <?php $imageArr=$obj->query("select distinct(pphoto) from $wholesale_productprice where  product_id='".$_REQUEST['product_id']."'  and pphoto!='' and pphoto!='$photo' ",$debug=-1);                  while($rsPhoto=$obj->fetchNextObject($imageArr)){

                                      if(is_file("../upload_images/product/thumb/".$rsPhoto->pphoto)){?>

									   <div style="width:25%; float:left;">

                                         <img src="../upload_images/product/thumb/<?php echo $rsPhoto->pphoto; ?>" /><br/>

                                         <input type="radio" name="pimage" value="<?php echo $rsPhoto->pphoto; ?>" />

                                        </div>

                                       <?php } }?>

                                      </td>

							       </tr>

                                   

                                   <tr>

                                      <td align="right" class="paddBot11 paddRt14"><strong>Or Upload New Image<br/>

                                      (300 X 300px)(Not more than 100 KB):</strong></td>

									  <td align="left" class="paddBot11"><input name="photo" type="file"  /><br/>

                                      <?php if(is_file("../upload_images/product/thumb/".$result->pphoto)) {?>

                                      <img src="../upload_images/product/thumb/<?php echo  $result->pphoto; ?>" />

                                      <?php } ?>

                                      </td>

							       </tr>

                                   

                                  <!--  <tr>

                                      <td align="right" class="paddBot11 paddRt14"><strong>Bar Code:</strong></td>

									  <td align="left" class="paddBot11"><input name="barcode" type="file"  /><br/>

                                      <?php if(is_file("../upload_images/product/barcode/".$result->barcode)) {?>

                                      <img src="../upload_images/product/barcode/<?php echo  $result->barcode; ?>" />

                                      <?php } ?>

                                      </td>

							       </tr> -->

                                   <tr>

                                      <td align="right" class="paddBot11 paddRt14"><strong>Barcode Number:</strong></td>

									  <td align="left" class="paddBot11"><input name="barcode_number" type="text" id="barcode_number" size="36" value="<?php echo stripslashes($result->barcode_number);?>" />&nbsp;(Comma Separated Values Ex: 42204084,42204084,42204084)</td>

							  </tr>

                                     <tr>

                                      <td align="right" class="paddBot11 paddRt14"><strong>Video(YouTube URL):</strong></td>

								     <td align="left" class="paddBot11">

                                     <textarea name="video"  rows="5" cols="40"  id="video"><?php echo  $result->video; ?></textarea>						         </td>

							       </tr> 

                                

									<tr>

									  <td align="right" class="paddRt14 paddBot11">&nbsp;</td>

									  <td align="left" class="paddBot11">&nbsp;</td>

							  </tr>

									<tr>

										<td width="18%" align="right" class="paddRt14 paddBot11">&nbsp;</td>

										<td width="82%" align="left" class="paddBot11">

											<input type="submit" name="submit" value="Submit"  class="submit" border="0"/> 	                  		 &nbsp;&nbsp;

											<input name="Reset" type="reset" id="Reset" value="Reset" class="submit" border="0" />									  </td>

									</tr>

								</table></form>

							</td>

						</tr>

						

					</table>

				</td>

			</tr>

		</table>

	</td>

</tr>

<?php include('footer.php'); ?>

</table>

</body>

</html>