<?php
session_name("AUTHENUSER");
session_start();
$USERNAME = $_SESSION['SES_USERNAME'];
if(!session_name("AUTHENUSER"))
	{
?>
						<script language="javascript">
						 alert("Waktu Login telah habis.Silahkan Login Kembali");
						document.location="index.php";
						</script>
<?php
	}
if(!$USERNAME)
	{
?>
						<script language="javascript">
						 alert("Harap Login Dahulu.");
						document.location="index.php";
						</script>
<?php
	}
	?>
	<?php 
	include "common/config.php";
	include "include/head.php";
?>
	<script language="javascript">
function intFormat(n)
{
var
regex = /(\d)((\d{3},?)+)$/;

n = n.split(',').join('');

while(regex.test(n))
{
n = n.replace(regex, '$1,$2');
}

return n;
}

function numFormat(n)
{
var
pointReg = /([\d,\.]*)\.(\d*)$/, f;

if(pointReg.test(n))
{
f = RegExp.$2;
return intFormat(RegExp.$1) + '.' + f;
}
return intFormat(n);
}
</script>
<?php
	$act  = $_REQUEST['act'];
	$kode = mysql_fetch_array(mysql_query("select max(id_stock) as maxid from inventory_diamond"));
	$maxid = $kode['maxid'];
	if($maxid == "")
	{
		$kode1 = "DIAMOND1";
	
	}
	else
	{
		$total = $maxid+1;
		$kode1 = "DIAMOND".$total;
	}
	$kode2  = mysql_fetch_array(mysql_query("select max(IFNULL(id_barcode,0))  as maxbarcode from barcode"));
	$maxbarcode = $kode2['maxbarcode'];
	if($maxbarcode == 0)
	{
		$maxbarcode = "000001";
	
	}
	else
	{
		$countbarcode = strlen($maxbarcode+1);
		$maxbarcode1 = $maxbarcode+1;
		if($countbarcode == 1)
		{
			$maxbarcode = '00000'.$maxbarcode1;
		}
		elseif($countbarcode == 2)
		{
			$maxbarcode = '0000'.$maxbarcode1;
		}
		elseif($countbarcode == 3)
		{
			$maxbarcode = '000'.$maxbarcode1;
		}
		elseif($countbarcode == 4)
		{
			$maxbarcode = '00'.$maxbarcode1;
		}
		elseif($countbarcode == 5)
		{
			$maxbarcode = '0'.$maxbarcode1;
		}
		else
		{
			$maxbarcode = $maxbarcode1;
		}
		
	}
	$kurs2 = mysql_fetch_array(mysql_query("select usd from tbl_usd order by date_update desc limit 1"));
	$usd = $kurs2['usd']; 
	if($act == 1)
	{
		$kode  = $_REQUEST['kode'];
		$cbprice = str_replace(",","", $_REQUEST['$cbprice']);
        $lbprice = str_replace(",","", $_REQUEST['lbprice']);
		$msprice = str_replace(",","", $_REQUEST['msprice']);
		$rapprice = str_replace(",","", $_REQUEST['rapprice']);
		$supplier  = $_REQUEST['supplier'];
		$barcode  = $_REQUEST['barcode'];
		$weight  = $_REQUEST['weight'];
		$certificateno  = $_REQUEST['certificateno'];
		$certificate  = $_REQUEST['certificate'];
		$shape  = $_REQUEST['shape'];
		$color  = $_REQUEST['color'];
		$clarity  = $_REQUEST['clarity'];
		$cut  = $_REQUEST['cut'];
		
		$foolish  = $_REQUEST['foolish'];
		$symetry  = $_REQUEST['symetry'];
		$fluo  = $_REQUEST['fluo'];
		$discount  = $_REQUEST['discount'];
		$description  = $_REQUEST['description'];
		$internal  = $_REQUEST['internal'];
		$printdescription  = $_REQUEST['printdescription'];
		$uploaddir = 'certificate/';
		$uploadfile = $uploaddir.$_FILES['file']['name'];
		$filename = $_FILES['file']['name']; 		//echo $_FILES['file']['name']; exit;
			if($filename != "")
			{
				if ($_FILES["file"]["type"] == "application/pdf")
				{
				  if ($_FILES["file"]["error"] > 0)
					{
						echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
					}
					else
					{
						move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile);
						$kurs = mysql_fetch_array(mysql_query("select * from tbl_usd order by date_update desc limit 1"));
						$usd = $kurs['usd'];
						$insert = mysql_query("insert into inventory_diamond (kode_stock,id_supplier,cbprice, msprice,rapprice,weight, barcodeno,user_update,id_type,id_shape,certificateno,
						color,clarity,cut,foolish,symetry,fluo,discount,certificate_name,idr,description,notes,internaldescription
				) values ('$kode','$supplier','$cbprice','$msprice','$rapprice','$weight','$barcode','$USERNAME','$certificate','$shape','$certificateno','$color','$clarity','$cut','$foolish','$symetry','$fluo','$discount','$filename','$usd','$printdescription','$description','$internal')");
							if($insert)
							{
								$insert2 = mysql_query("insert into barcode (kode_stock) values ('$kode')");
							?>
									<script language="javascript">
										 alert("Add Diamond Success");
										document.location="add_diamond.php";
									</script>
							<?php
							}
							else
							{
								?>
									<script language="javascript">
										alert("Add Diamond Failed");
										document.location="add_diamond.php";
									</script>
								<?php
							}
					}
				}
			
					else
					{
					?>
									<script language="javascript">
										alert("Format invalid");
										document.location="add_diamond.php";
									</script>
					<?php
					}
			}
				else
				{
					$kurs = mysql_fetch_array(mysql_query("select * from tbl_usd order by date_update desc limit 1"));
						$usd = $kurs['usd'];
                    
                    
                    /*
                    $insert = "insert into inventory_diamond (kode_stock,id_supplier,lbprice, msprice,rapprice,weight, barcodeno,user_update,id_type,id_shape,certificateno,
		color,clarity,cut,foolish,symetry,fluo,discount,idr,description,notes,internaldescription) values ('$kode','$supplier','$lbprice','$msprice','$rapprice','$weight','$barcode','$USERNAME','$certificate','$shape','$certificateno','$color','$clarity','$cut','$foolish','$symetry','$fluo','$discount','$usd','$printdescription','$description','$internal')";
                    
                    
                    echo $insert; exit;
                    
                    */
                    
                    
                    
	$insert = mysql_query("insert into inventory_diamond (kode_stock,id_supplier,cbprice, msprice,rapprice,weight, barcodeno,user_update,id_type,id_shape,certificateno,
		color,clarity,cut,foolish,symetry,fluo,discount,idr,description,notes,internaldescription) values ('$kode','$supplier','$cbprice','$msprice','$rapprice','$weight','$barcode','$USERNAME','$certificate','$shape','$certificateno','$color','$clarity','$cut','$foolish','$symetry','$fluo','$discount','$usd','$printdescription','$description','$internal')");
		if($insert)
							{
								$insert2 = mysql_query("insert into barcode (kode_stock) values ('$kode')");
							?>
									<script language="javascript">
										 alert("Add Diamond Success");
										document.location="add_diamond.php";
									</script>
							<?php
							}
							else
							{
								?>
									<script language="javascript">
										alert("Add Diamond Failed");
										document.location="add_diamond.php";
									</script>
								<?php
							}
				}
	}
	?>
<body>
<?php include "include/head2.php"; ?>
<?php include "include/leftmenu.php"; ?>
<?php include "include/top.php"; ?>
  <h1><i class="icon-gears (alias)"></i> Add Inventory Diamond</h1>
</div>

<!--<ol class="breadcrumb">
  <li><a href="#">Home</a></li>
  <li><a href="#">Bread</a></li>
  <li><a href="#">Crumbs</a></li>
  <li class="active">Example</li>
</ol>-->
          <div class="main-content">
            <div class="row">
                <div class="widget">
                  <div class="widget-content-white glossed">
                    <div class="padded">
                      <form method="post" action="add_diamond.php?act=1"  enctype="multipart/form-data" role="form" class="form-horizontal">
                        <div class="form-group">
                          <label class="col-md-4 control-label">Stock ID</label>
                          <div class="col-md-8">
                            <input type="text" class="form-control" name="kode" id="kode"  value="<?php echo $kode1; ?>"  readonly="readonly">
                          </div>
                          </div>
                           <div class="form-group">
                          <label class="col-md-4 control-label">Kode Supplier -  Nama Supplier</label>
                          <div class="col-md-8"><select name="supplier" id="supplier" class="form-control">
                               <?php
                                              $sql3 = "select * from tbl_supplier where status_supplier=1 order by nama_supplier,kode_supplier";
                                              $rs3 = mysql_query($sql3);
                                              while($fetch3 = mysql_fetch_array($rs3))
                                              {
                                               ?>
                                        <option value="<?php echo $fetch3['id_supplier']; ?>"><?php echo  $fetch3['kode_supplier'];  ?> - <?php echo  $fetch3['nama_supplier'];  ?></option>
                                                      <?php
                                              }
                                             ?>
                            </select>
                          </div>
                        </div>
                           <div class="form-group">
                          <label class="col-md-4 control-label">Barcode No.</label>
                          <div class="col-md-8">
                            <input type="text" class="form-control" name="barcode" id="barcode" readonly="readonly" value="<?php echo $maxbarcode; ?>">
                          </div>
                          </div>
                          <div class="form-group">
                          <label class="col-md-4 control-label">Certificate Type</label>
                          <div class="col-md-8">
                           <select name="certificate" id="certificate" class="form-control">
                               <?php
                                              $sql3 = "select * from  tbl_type order by nama_type";
                                              $rs3 = mysql_query($sql3);
                                              while($fetch3 = mysql_fetch_array($rs3))
                                              {
                                               ?>
                                        <option value="<?php echo $fetch3['id_type']; ?>"><?php echo  $fetch3['nama_type'];  ?></option>
                                                      <?php
                                              }
                                             ?>
                            </select>
                          </div>
                          </div>
                            <div class="form-group">
                          <label class="col-md-4 control-label">Cert No.</label>
                          <div class="col-md-8">
                            <input type="text" class="form-control" name="certificateno" id="certificateno" >
                          </div>
                          </div>
                            <div class="form-group">
                          <label class="col-md-4 control-label">Shape</label>
                          <div class="col-md-8">
                          <select name="shape" id="shape" class="form-control">
                               <?php
                                              $sql3 = "select * from  tbl_shape order by nama_shape";
                                              $rs3 = mysql_query($sql3);
                                              while($fetch3 = mysql_fetch_array($rs3))
                                              {
                                               ?>
                                        <option value="<?php echo $fetch3['id_shape']; ?>"><?php echo  $fetch3['nama_shape'];  ?></option>
                                                      <?php
                                              }
                                             ?>
                            </select>
                          </div>
                          </div>
                           <div class="form-group">
                          <label class="col-md-4 control-label">Kurs</label>
                          <div class="col-md-8">
                            <input type="text" class="form-control" name="usd" id="usd" value="<?php echo $usd; ?>" readonly="readonly" >
                          </div>
                          </div>
                           <div class="form-group">
                          <label class="col-md-4 control-label">Min Price</label>
                          <div class="col-md-8">
                            <input type="text" class="form-control" name="msprice" id="msprice" onKeyUp="this.value = numFormat(this.value)"  value="0">
                          </div>
                          </div>
                           <div class="form-group">
                          <label class="col-md-4 control-label">Max Price</label>
                          <div class="col-md-8">
                            <input type="text" class="form-control" name="cbprice" id="cbprice" onKeyUp="this.value = numFormat(this.value)"  value="0">
                          </div>
                          </div>
                          <div class="form-group">
                          <label class="col-md-4 control-label">RAP</label>
                          <div class="col-md-8">
                            <input type="text" class="form-control" name="rapprice" id="rapprice" onKeyUp="this.value = numFormat(this.value)"  value="0">
                          </div>
                          </div>
                          <div class="form-group">
                          <label class="col-md-4 control-label">Weight</label>
                          <div class="col-md-8">
                            <input type="text" class="form-control" name="weight" id="weight" >
                          </div>
                          </div>
                           <div class="form-group">
                          <label class="col-md-4 control-label">Color</label>
                          <div class="col-md-8">
                            <input type="text" class="form-control" name="color" id="color" >
                          </div>
                          </div>
                          <div class="form-group">
                          <label class="col-md-4 control-label">Clarity</label>
                          <div class="col-md-8">
                            <input type="text" class="form-control" name="clarity" id="clarity" >
                          </div>
                          </div>
                          <div class="form-group">
                          <label class="col-md-4 control-label">Cut</label>
                          <div class="col-md-8">
                            <input type="text" class="form-control" name="cut" id="cut" >
                          </div>
                          </div>
                           <div class="form-group">
                          <label class="col-md-4 control-label">Pol</label>
                          <div class="col-md-8">
                            <input type="text" class="form-control" name="foolish" id="foolish" >
                          </div>
                          </div>
                           <div class="form-group">
                          <label class="col-md-4 control-label">Sym</label>
                          <div class="col-md-8">
                            <input type="text" class="form-control" name="symetry" id="symetry" >
                          </div>
                          </div>
                            <div class="form-group">
                          <label class="col-md-4 control-label">Fluo</label>
                          <div class="col-md-8">
                            <input type="text" class="form-control" name="fluo" id="fluo" >
                          </div>
                          </div>
                            <div class="form-group">
                          <label class="col-md-4 control-label">Discount</label>
                          <div class="col-md-8">
                            <input type="text" class="form-control" name="discount" id="discount" >
                          </div>
                          </div>
                           <div class="form-group">
                          <label class="col-md-4 control-label">Upload Certificate (only PDF)</label>
                          <div class="col-md-8">
                           <input type="file" name="file" id="file" /> 
                          </div>
                        </div>
                         <div class="form-group">
                          <label class="col-md-4 control-label">Print Description</label>
                          <div class="col-md-8">
                          <textarea id="printdescription" name="printdescription" class="form-control"></textarea>
                          </div>
                        </div>
                         <div class="form-group">
                          <label class="col-md-4 control-label">Internal Description</label>
                          <div class="col-md-8">
                          <textarea id="internal" name="internal" class="form-control"></textarea>
                          </div>
                        </div>
                         <div class="form-group">
                          <label class="col-md-4 control-label">Description</label>
                          <div class="col-md-8">
                          <textarea id="description" name="description" rows="15" cols="80" style="width: 80%" class="form-control"></textarea>
                          </div>
                        </div>
                        <div class="form-group">
                          <div class="col-md-offset-4 col-md-8">
                            <button type="submit" class="btn btn-default">Submit</button>
                            <button type="reset" class="btn btn-default">Reset</button>
                          </div>
                        </div>
                      </form>
                    </div>
                    </div>
                    </div>
                    </div>
                    </div>
                   

<?php include "include/foot.php"; ?>
<script src='assets/js/for_pages/dashboard.js'></script>
</body>

</html>