<?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'];
	
	if($act == 1)
	{
		$kode  = $_REQUEST['kode'];
		$inventory  = $_REQUEST['inventory'];
		list($d, $e, $f)=split ("[-]", $_REQUEST['inputDate']);
		$datepicker=$f."-".$e."-".$d;
		$customer  = $_REQUEST['customer'];
			
		$insert = mysql_query("insert into return_sales (kode_returnsales,date_returnsales,id_customer, inventory, user_update) values ('$kode','$datepicker','$customer','$inventory','$USERNAME')");

						if($insert)
						{
							$max = mysql_fetch_array(mysql_query("select max(id_returnsales) as maxsales from return_sales "));
							$maxsales = $max['maxsales'];
								?>
								<script language="javascript">
									document.location="add_returnsales.php?act=3&id=<?php echo $maxsales; ?>&customer=<?php echo $customer; ?>";
								</script>
						<?php
						}
						else
						{
							?>
								<script language="javascript">
									alert("Add Return Sales Failed");
									document.location="add_returnsales.php?act=3";
								</script>
							<?php
						}
	}
	elseif($act == 2)
	{
		$bulan  = date('m');
		$tahun  = date('Y');
		$kode = mysql_fetch_array(mysql_query("select max(id_returnsales) as maxid from  return_sales"));
		$maxid = $kode['maxid'];
		if($maxid == "")
		{
			$kode1 = "RETURN/".$bulan.$tahun."/1";
		
		}
		else
		{
			$total = $maxid+1;
			$kode1 = "RETURN/".$bulan.$tahun."/".$total;
		
		}
	?>
<body>
<?php include "include/head2.php"; ?>
<?php include "include/leftmenu.php"; ?>
<?php include "include/top.php"; ?>
  <h1><i class="icon-bar-chart"></i> Add Return Sales</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_returnsales.php?act=1" role="form" class="form-horizontal">
                        <div class="form-group">
                          <label class="col-md-4 control-label">Kode Return Sales</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">Return Sales Date</label>
                          <div class="col-md-8">
                            <input type="text" class="inputDate" name="inputDate" id="inputDate" value="<?php echo date('d-m-Y'); ?>">
                          </div>
                        </div>
                    <div class="form-group">
                          <label class="col-md-4 control-label">Kode Customer - Nama Customer</label>
                          <div class="col-md-8">
                            <select class="form-control"  name="customer" id="customer">
                             <?php
                                              $sql3 = "select * from tbl_customer where status_customer=1 order by nama_customer";
                                              $rs3 = mysql_query($sql3);
                                              while($fetch3 = mysql_fetch_array($rs3))
                                              {
                                               ?>
                                        <option value="<?php echo $fetch3['id_customer']; ?>"><?php echo  $fetch3['kode_customer'];  ?> - <?php echo  $fetch3['nama_customer'];  ?></option>
                                                      <?php
                                              }
                                             ?>
                            </select>
                          </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>
<?php
	}
elseif($act == 3)
	{
		$id  = $_REQUEST['id'];
		$inventory  = $_REQUEST['inventory'];
		$customer  = $_REQUEST['customer'];
	?>
    <body>
<?php include "include/head2.php"; ?>
<?php include "include/leftmenu.php"; ?>
<?php include "include/top.php"; ?>
  <h1><i class="icon-bar-chart"></i> Add Return Sales</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_returnsales.php?act=5&id=<?php echo $id; ?>&inventory=<?php echo $inventory; ?>" role="form" class="form-horizontal">
                         <div class="form-group">
                          <label class="col-md-4 control-label">Barcode  - Kode Stock</label>
                          <div class="col-md-8">
                            <select name="kode" id="kode" class="form-control">
                              <option value="Lama">Barang Lama</option>
							  																			<?php
                                              $sql3 = "select * from salesdetails a
													inner join sales b on a.id_sales = b.id_sales 
													where status_sales = 1 and status = 1  and
													kode_stock not in (select kode_stock from return_sales where status_returnsales = 1)
													and id_customer = '$customer' order by kode_stock ";
																																														//echo $sql3; exit;
                                              $rs3 = mysql_query($sql3);
                                              while($fetch3 = mysql_fetch_array($rs3))
                                              {
                                               ?>
                                        <option value="<?php echo $fetch3['kode_stock']; ?>"><?php echo  $fetch3['barcode'];  ?> - <?php echo  $fetch3['kode_stock'];  ?></option>
                                                      <?php
                                              }
                                             ?>
                            </select>
                          </div>
                        </div>
                    <div class="form-group">
                          <label class="col-md-4 control-label">Harga Return Sales</label>
                          <div class="col-md-8">
                           <input type="text" name="harga" id="harga" class="form-control" 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>
                    </div>
                    </div>
                    </div>
                    </div>
                    </div>
                   

<?php include "include/foot.php"; ?>
<script src='assets/js/for_pages/dashboard.js'></script>
</body>
 <?php
	}
	elseif($act  == 5)
	{
			$id  = $_REQUEST['id'];
			$inventory  = $_REQUEST['inventory'];
			$kode  = $_REQUEST['kode'];
			$harga = str_replace(",","", $_REQUEST['harga']);
		
			if($harga == "")
			{
			?>
								<script language="javascript">
									alert("Input Harga Return Sales");
									document.location="add_returnsales.php?act=3&id=<?php echo $id; ?>&inventory=<?php echo $inventory; ?>";
								</script>
							<?php
			}
			else
			{
			$update  = mysql_query("update return_sales set kode_stock = '$kode', hargaterima = '$harga' where id_returnsales = '$id'");
			$update2  = mysql_query("update salesdetails set status = 3 where kode_stock = '$kode'");	
			if($update)
						{
								?>
								<script language="javascript">
									 alert("Add Return Sales Success");
									document.location="return_sales.php";
								</script>
						<?php
						}
						else
						{
							?>
								<script language="javascript">
									alert("Add Return Sales Failed");
									document.location="add_returnsales.php?act=3&id=<?php echo $id; ?>&inventory=<?php echo $inventory; ?>";
								</script>
							<?php
						}
						}
	}
	?>
</html>