<?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'];
	$type  = $_REQUEST['tipe'];
	$kode_stock  = $_REQUEST['kode_stock'];
	if($act==2)
	{
			$kode_stock  = $_REQUEST['kode_stock'];
			$type  = $_REQUEST['type'];
			$id_custom  = $_REQUEST['id_custom'];
			$delete  = mysql_query("delete from inventory_custom where id_custom='$id_custom' ");
			if($delete)
			{
				?>
								<script language="javascript">
									 alert("Delete Custom Inventory Success");
									document.location="custom_inventory.php?kode_stock=<?php echo $kode_stock; ?>&type=4";
								</script>
						<?php
			}
			else
			{
				?>
								<script language="javascript">
									 alert("Delete Custom Inventory Failed");
									document.location="custom_inventory.php?kode_stock=<?php echo $kode_stock; ?>&type=<?php echo $type; ?>";
								</script>
						<?php
			}
			
			
	}
	elseif($act == 1)
	{
		$kode  = $_REQUEST['kode'];
		$type  = $_REQUEST['type'];
		$gold = $_REQUEST['gold'];
		$cost = str_replace(",","", $_REQUEST['cost']);
		$workmanship = str_replace(",","", $_REQUEST['workmanship']);
		$logistic = str_replace(",","", $_REQUEST['logistic']);
		$tipe  = $_REQUEST['tipe'];
		$weight  = $_REQUEST['weight'];
				$insert = mysql_query("insert into inventory_custom (kode_stock,id_diamonds,gold, weight, cost, logistic,workmanship,user_update) values ('$kode','$tipe','$gold','$weight','$cost','$logistic','$workmanship','$USERNAME')");
					if($insert)
						{
							if($type ==1)
							{
						?>
								<script language="javascript">
									 alert("Add Custom Inventory Success");
									document.location="inventory_diamond.php";
								</script>
						<?php
							}
							elseif($type ==2)
							{
						?>
								<script language="javascript">
									 alert("Add Custom Inventory Success");
									document.location="inventory_gold.php";
								</script>
						<?php
							}
							elseif($type ==3)
							{
						?>
								<script language="javascript">
									 alert("Add Custom Inventory Success");
									document.location="inventory_logam.php";
								</script>
						<?php
							}
							elseif($type ==4)
							{
						?>
								<script language="javascript">
									 alert("Add Custom Inventory Success");
									document.location="custom_inventory.php?kode_stock=<?php echo $kode; ?>&type=4";
								</script>
						<?php
							}
						}
						else
						{
							?>
								<script language="javascript">
									alert("Add Custom Inventory Failed");
									document.location="custom_inventory.php?kode_stock=<?php echo $kode; ?>&type=4";
								</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>Custom Inventory</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="custom_inventory.php?act=1&kode_stock=<?php echo $kode_stock; ?>&type=<?php echo $type; ?>" 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 $kode_stock; ?>"  readonly="readonly">
                          </div>
                          </div>
                           <div class="form-group">
                          <label class="col-md-4 control-label">Tipe</label>
                          <div class="col-md-8"><select name="tipe" id="tipe" class="form-control">
                               <?php
                                              $sql3 = "select * from tbl_diamonds order by diamonds";
                                              $rs3 = mysql_query($sql3);
                                              while($fetch3 = mysql_fetch_array($rs3))
                                              {
                                               ?>
                                        <option value="<?php echo $fetch3['id_diamonds']; ?>"><?php echo  $fetch3['diamonds'];  ?> - <?php echo  $fetch3['keterangan'];  ?></option>
                                                      <?php
                                              }
                                             ?>
                            </select>
                          </div>
                        </div>
                        
                          <div class="form-group">
                          <label class="col-md-4 control-label">Weight (in karat)</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">Gold</label>
                          <div class="col-md-8">
                            <input type="text" class="form-control" name="gold" id="gold" >
                          </div>
                          </div>
                           <div class="form-group">
                          <label class="col-md-4 control-label">Workmanship</label>
                          <div class="col-md-8">
                            <input type="text" class="form-control" name="workmanship" id="workmanship" onKeyUp="this.value = numFormat(this.value)" >
                          </div>
                          </div>
                            <div class="form-group">
                          <label class="col-md-4 control-label">Logistic</label>
                          <div class="col-md-8">
                            <input type="text" class="form-control" name="logistic" id="logistic" onKeyUp="this.value = numFormat(this.value)" >
                          </div>
                          </div>
                            <div class="form-group">
                          <label class="col-md-4 control-label">Addtional Cost</label>
                          <div class="col-md-8">
                            <input type="text" class="form-control" name="cost" id="cost" onKeyUp="this.value = numFormat(this.value)"   >
                          </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>
                       <table class="table table-striped table-bordered table-hover">
                  <thead>
                    <tr>
                      		 <th>Tipe</th>
                     <th>Weight</th>
                     <th>Gold</th>
                     <th>Workmanship</th>
                     <th>Logistic</th>
                      <th>Additional Cost</th>
                     <th>Details</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php
						$sql = mysql_query("select * from inventory_custom a
																									inner join tbl_diamonds b on a.id_diamonds = b.id_diamonds
																									where a.kode_stock = '$kode_stock'");
						while($fetch = mysql_fetch_array($sql))
						{
								$id_diamonds = $fetch['id_diamonds'];
								$id_custom = $fetch['id_custom'];
								$gold = $fetch['gold'];
								$weight = $fetch['weight'];
								$cost = $fetch['cost'];
								$logistic = $fetch['logistic'];
								$workmanship = $fetch['workmanship'];
								
					?>
                 <tr>
                     <td><?php echo  $fetch['diamonds'];  ?> - <?php echo  $fetch['keterangan'];  ?></td>
                     <td><?php echo $fetch['weight']; ?></td>
                      <td><?php echo $fetch['gold']; ?></td>
                      <td align="right"><?php echo number_format($fetch['workmanship'], 0, ',', ','); ?></td>
                      <td align="right"><?php echo number_format($fetch['logistic'], 0, ',', ','); ?></td>
                      <td align="right"><?php echo number_format($fetch['cost'], 0, ',', ','); ?></td>
                   <td class="actions aligncenter"><a href="custom_inventory.php?kode_stock=<?php echo $kode_stock; ?>&type=<?php echo $type; ?>&act=2&id_custom=<?php echo $id_custom; ?>">Delete</a></td>
                      
                </tr>
                <?php
						}
						?>
                  </tbody>
                </table>
                    </div>
                    </div>
                    </div>
                    </div>
                    </div>
                   

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

</html>