<?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'];
	$sql2 = mysql_fetch_array(mysql_query("select sum(price) as total,currency  from salesdetails where id_sales  = '$id_sales' and status=1 group by currency"));
	$total = $sql2['total'];
	$currency = $sql2['currency'];
	$sql21 = mysql_fetch_array(mysql_query("select sum(price) as total,currency from salesservice where id_sales  = '$id_sales' group by currency"));
	$total1 = $sql21['total'];
	$currency1 = $sql21['currency'];
	$sql3 = mysql_fetch_array(mysql_query("select IFNULL(sum(nominal),0) as nominal  from sales_payment where id_sales  = '$id_sales'"));
	$nominal = $sql2['total'] - $sql3['nominal'];
	$nominal1 = $sql21['total'] - $sql3['nominal'];
	if($act == 1)
	{
		$id_sales  = $_REQUEST['id_sales'];
		//$cicilan  = $_REQUEST['cicilan'];
		$payment  = $_REQUEST['payment'];
        //echo $payment; exit;
		$notes  = $_REQUEST['notes'];
		$method  = $_REQUEST['method'];
		$nomor  = $_REQUEST['nomor'];
		$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;
		$insert = mysql_query("insert sales_payment (id_sales,nominal,user_update,date_update,paymentdate,id_term,nomorkartu) values ('$id_sales','$payment','$USERNAME','$tanggal_update','$datepicker','$method','$nomor')");
		//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="paymentsales.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>Total Invoice</label>
                            </div>
                                <div class="col-md-6"> 
                                 <input type="text" class="form-control" name="invoice"   id="invoice" value="<?php if($total <> 0) { ?><?php echo $currency;?> <?php echo $total;?><?php } ?><?php if($total1 <> 0) { ?><?php echo $currency1;?> <?php echo $total1;?><?php } ?>" readonly>
                                </div>
                        </div>
                        <div class="form-group">
                        <div class="col-md-6"> 
                            <label>Sisa Invoice</label>
                            </div>
                                <div class="col-md-6"> 
                                 <input type="text" class="form-control" name="invoice"   id="invoice"  value="<?php if($total <> 0) { ?><?php echo $currency;?> <?php echo $nominal;?><?php } ?><?php if($total1 <> 0) { ?><?php echo $currency1;?> <?php echo $nominal1;?><?php } ?>" readonly>
                                </div>
                        </div>
                        <div class="form-group">
                        <div class="col-md-6"> 
                            <label>Nominal Payment</label>
                            </div>
                                <div class="col-md-6"> 
                                 <input type="text" class="form-control" name="payment"   id="payment"   >
                                </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 class="control-label">Payment Method</label>
                           </div>
                           <div class="col-md-6"> 
                            <select class="form-control" name="method" id="method">
                              <?php
                                              $sql3 = "select * from tbl_term order by id_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>Nomor Kartu</label>
                            </div>
                                <div class="col-md-6"> 
                                 <input type="text" class="form-control" name="nomor"   id="nomor" >
                                </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>