<?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'];
	$id_sales  = $_REQUEST['id_sales'];
	
	if($act == 1)
	{
		$id_sales  = $_REQUEST['id_sales'];
		$cicilan  = $_REQUEST['cicilan'];
		$payment  = $_REQUEST['payment'];
		$notes  = $_REQUEST['notes'];
		$nominal = str_replace(",","", $_REQUEST['nominal']);
		$return1 = str_replace(",","", $_REQUEST['return1']);
		list($d, $e, $f)=split ("[-]", $_REQUEST['inputDate']);
		$datepicker=$f."-".$e."-".$d;
		$customer  = $_REQUEST['customer'];
			
		$insert = mysql_query("update sales set cicilan ='$cicilan',nominal = '$nominal',status_sales=3,payment_date ='$datepicker',id_term='$payment',user_update='$USERNAME',nominalreturn='$return1',notes='$notes' where id_sales='$id_sales'");
		//$total = $cicilan - 1; //echo $total; exit;
		$insert2 = mysql_query("insert sales_payment (id_sales,nominal,user_update,date_update) values ('$id_sales','$payment','$USERNAME','$tanggal_update')");
		//if($total > 0)
//		{
//			for($i =1;$i <= $total;$i++)
//			{
//				$fetch = mysql_fetch_array(mysql_query("select DATE_ADD(payment_date, INTERVAL $i MONTH) as paymentdate from sales where id_sales= '$id_sales'"));
//				//$fetch = "select DATE_ADD(payment_date, INTERVAL $total MONTH) as paymentdate from sales where id_sales= '$id_sales'"; echo $fetch; exit;
//				$paymentdate = $fetch['paymentdate']; 
//				$insert = mysql_query("insert reminder_payment values ('$id_sales','$paymentdate')");
//				//$i++;
//			}
//		}


						if($insert)
						{
				
								?>
								<script language="javascript">
									 alert("Payment Sales Success");
									document.location="sales.php";
								</script>
						<?php

						}
						else
						{
							?>
								<script language="javascript">
									alert("Payment Sales Failed");
									document.location="payment_sales.php?id_sales=<?php echo $id_sales; ?>";
								</script>
							<?php
						}
	}
	else
	{
	?>
<body>
<?php include "include/head2.php"; ?>
<?php include "include/leftmenu.php"; ?>
<?php include "include/top.php"; ?>
   
  <h1><i class="icon-shopping-cart"></i> Payment Sales</h1>
</div>


   <div class="main-content">
   
            <div class="widget">
            <div class="widget-content-white glossed">
                    <div class="padded">
                      <form role="form" class="form-horizontal" method="post" action="payment_sales.php?act=1&id_sales=<?php echo $id_sales; ?>">
                        <div class="form-group">
                        <div class="col-md-6"> 
                            <label>Payment Method</label>
                            </div>
                                <div class="col-md-6"> 
                                 <select name="payment" id="payment" class="form-control">
							  <?php
                                              $sql3 = "select * from  tbl_term order by term";
                                              $rs3 = mysql_query($sql3);
                                              while($fetch3 = mysql_fetch_array($rs3))
                                              {
                                               ?>
                                        <option value="<?php echo $fetch3['id_term']; ?>"><?php echo  $fetch3['term'];  ?></option>
                                                      <?php
                                              }
                                             ?>
                            </select>
                                </div>
                        </div>
                        <div class="form-group">
                        <div class="col-md-6"> 
                            <label>Nominal</label>
                            </div>
                                <div class="col-md-6"> 
                                 <input type="text" class="form-control" name="nominal"   id="nominal" onKeyUp="this.value = numFormat(this.value)"  >
                                </div>
                        </div>
                           <div class="form-group">
                        <div class="col-md-6"> 
                            <label>Cicilan</label>
                            </div>
                                <div class="col-md-6"> 
                                <select name="cicilan" id="cicilan" class="form-control">
                            <option value="0" >0 x</option>
                              <option value="3" >3 x</option>
                                <option value="6" >6 x</option>
                               <option value="12" >12 x</option>
                                <option value="24" >24 x</option>
                            </select>
                                </div>
                        </div>
                          <div class="form-group">
                         <div class="col-md-6"> 
                          <label class="control-label">Payment Date</label>
                          </div>
                           <div class="col-md-6"> 
                           <input type="text" class="inputDate" name="inputDate"  value="<?php echo date('d-m-Y'); ?>" id="inputDate" >
                            </div>
                        </div>
                        <div class="form-group">
                        <div class="col-md-6"> 
                            <label>Return</label>
                            </div>
                                <div class="col-md-6"> 
                                 <input type="text" class="form-control" name="return1"   id="return1" onKeyUp="this.value = numFormat(this.value)"  >
                                </div>
                        </div>
                         <div class="form-group">
                        <div class="col-md-6"> 
                            <label>Notes</label>
                            </div>
                                <div class="col-md-6"> 
                                 <input type="text" class="form-control" name="notes"   id="notes" >
                                </div>
                        </div>
                        <button class="btn btn-primary" type="submit">Submit</button>
                        <button class="btn btn-default" type="reset">Reset</button>
                      </form>
                    </div>
             </div>
            
            </div>
          </div>
        </div>
      </div>

    </div>
  </div>
</div>       

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