<?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";
	?>
<?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 sales (invoice,invoice_date,id_customer, inventory, user_update) values ('$kode','$datepicker','$customer','$inventory','$USERNAME')");

						if($insert)
						{
							$max = mysql_fetch_array(mysql_query("select max(id_sales) as maxsales from sales "));
							$maxsales = $max['maxsales'];
							if($inventory == "services")
							{
						?>
								<script language="javascript">
									 alert("Add Sales Success");
									document.location="add_salesdetails.php?act=3&id=<?php echo $maxsales; ?>";
								</script>
						<?php
							}
							else
							{
								?>
								<script language="javascript">
									 alert("Add Sales Success");
									document.location="add_salesdetails.php?act=4&id=<?php echo $maxsales; ?>&inventory=<?php echo $inventory; ?>";
								</script>
						<?php
							}
						}
						else
						{
							?>
								<script language="javascript">
									alert("Add Sales Failed");
									document.location="add_sales.php?act=3";
								</script>
							<?php
						}
	}
	elseif($act == 2)
	{
		$inventory = $_REQUEST['inventory'];
		$bulan  = date('m');
		$kode = mysql_fetch_array(mysql_query("select max(id_sales) as maxid from  sales"));
		$maxid = $kode['maxid'];
		$branch = mysql_fetch_array(mysql_query("select * from tbl_admin a
		inner join tbl_branch b on a.id_branch = b.id_branch where user_id='$USERNAME'"));
		$brachcode = $branch['kode_branch'];
		if($maxid == "")
		{
			$kode1 = $brachcode."/".$bulan."/1";
		
		}
		else
		{
			$total = $maxid+1;
			$kode1 = $brachcode."/".$bulan."/".$total;
		}
	?>
<body>
<?php include "include/head2.php"; ?>
<?php include "include/leftmenu.php"; ?>
<?php include "include/top.php"; ?>
  <h1><i class="icon-shopping-cart"></i>Add Sales</h1>
</div>
          <div class="main-content">
            <div class="row">
                <div class="widget">
                  <div class="widget-content-white glossed">
                    <div class="padded">
                      <form method="post" action="add_sales.php?act=1&inventory=<?php echo $inventory; ?>" role="form" class="form-horizontal">
                        <div class="form-group">
                          
                           <label class="col-md-4 control-label">Choose</label>
                          <div class="col-md-8">
                            <select name="inventory1" id="inventory1" class="form-control">
                              <option value="inventory" <?php if($inventory == "inventory") { ?> selected="selected" <?php } ?>>Inventory</option>
                             <option value="services" <?php if($inventory == "services") { ?> selected="selected" <?php } ?>>Services</option>
                            </select>
                          </div>
                        </div>
                    <div class="form-group">
                          <label class="col-md-4 control-label">Invoice No.</label>
                          <div class="col-md-8">
                            <input type="text" name="kode" id="kode" class="form-control" value="<?php echo $kode1; ?>"  readonly="readonly">
                          </div>
                        </div>
                    <div class="form-group">
                          <label class="col-md-4 control-label">Invoice 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 name="customer" id="customer" class="form-control">
                              <option value="0">None</option>
							  <?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)
	{
	?>
    <body>
<?php include "include/head2.php"; ?>
<?php include "include/leftmenu.php"; ?>
<?php include "include/top.php"; ?>
  <h1><i class="icon-shopping-cart"></i>Add Sales</h1>
</div>
          <div class="main-content">
            <div class="row">
                <div class="widget">
                  <div class="widget-content-white glossed">
                    <div class="padded">
                      <form method="post" action="add_sales.php?act=2" role="form" class="form-horizontal">
                        <div class="form-group">
                          <label class="col-md-4 control-label">Choose</label>
                          <div class="col-md-8">
                            <select name="inventory" id="inventory" class="form-control">
                               <option value="inventory" <?php if($inventory == "inventory") { ?> selected="selected" <?php } ?>>Inventory</option>
                             <option value="services" <?php if($inventory == "services") { ?> selected="selected" <?php } ?>>Services</option>
                            </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
	}
	?>
</html>