<!DOCTYPE html>

<?php 
	session_start();
	date_default_timezone_set("Asia/Jakarta");
	@ob_start();
	include "../../config/koneksi.php";
	include "../../config/fungsi_rupiah.php";
	include "../headadvance.php";

	
	if ($_SESSION['nama'] == ''){
		header('Location:index.php');
	}
	else{
?>
<html>

  <body class="hold-transition skin-blue sidebar-mini layout-boxed">
    <div class="wrapper">
		<?php include "../header.php";?>
      
		<?php include "../menu.php";?>
      

      <!-- Content Wrapper. Contains page content -->
      <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1>
            Edit Penjualan CS
          </h1>
          <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
            <li><a href="data.php">Transaksi</a></li>
            <li class="active">Edit Sales Order</li>
          </ol>
        </section>

        <!-- Main content -->
        <section class="content">
          <div class="row">
            <div class="col-xs-12">
              
              <div class="box">

                <div class="box-body">
					<div class="col-md-6">
					  <!-- Horizontal Form -->
					  <div class="box box-info">
						<div class="box-header with-border">
						  <h3 class="box-title"> Sales Order Info</h3>
						</div><!-- /.box-header -->
						<!-- form start -->
						
						  <div class="box-body">
							
							
							<div class="form-group">
							  <label>Tanggal Input</label>
								<?php
								
									$page = $_GET['id'];
									$ucode = $_GET['ucode'];
									
									$billdate = mysql_result(mysql_query("select billdate from jualcstax where billno = $_GET[id] and ucode = '$ucode'"),0);
									$dt1 =date_format(date_create($billdate),"Y-m-d");
									echo"
									<form id='demoForm2' name=form method=POST action='aksisales.php?module=sales&act=edittgl&page=$page&ucode=$ucode' class='form-horizontal'>
									<input type='date' class='form-control' name = 'tgl' value='$dt1' >
									<button type='submit' class='btn btn-info'><span class='fa fa-floppy-o'></span></button>
									</form>";
								?>
							</div>
							
							<div class="form-group">
							  <label>Customer</label>		
									<?php
										echo"<form id='demoForm3' name=form method=POST action='aksisales.php?module=sales&act=editcust&page=$page&ucode=$ucode' class='form-horizontal'>";
									?>
									<select name="cust"   class="form-control select2">
									<?php
										
										$page = $_GET['id'];
										$cata = mysql_result(mysql_query("select cust from jualcstax where billno = $_GET[id] and ucode = '$ucode'"),0);
										$catb = mysql_result(mysql_query("select nama from customer where id = (select cust from jualcstax where billno = $_GET[id] and ucode = '$ucode')"),0);
										
										echo"<option value = '$cata'>$catb</option>";
										$cek = mysql_query("Select * from customer where status = 'Y' order by nama asc");
										while ($r=mysql_fetch_array($cek)){	
											$cat1 = $r['id'];
											$cat2 = $r['nama'];
											echo"<option value = '$cat1'>$cat2</option>";
										}
										echo"</select>
											<button type='submit' class='btn btn-info'><span class='fa fa-floppy-o'></span></button>
										</form>
										";
									?>
								
							</div>

						  </div><!-- /.box-body -->

						
						
						
						
						
						
						
						
					  </div><!-- /.box -->

					  
					 </div><!-- /.box -->
					 
					 <div class="col-md-6">
              <!-- Horizontal Form -->
              <div class="box box-info">
                <div class="box-header with-border">
                  <h3 class="box-title">Keterangan  Sales Order</h3>
                </div><!-- /.box-header -->
                <!-- form start -->
                <?php echo"
				<form id='demoForm' name=form method=POST action='aksisales.php?module=sales&act=editket&id=$_GET[id]' class='form-horizontal'>"; 
				?>	
                  <div class="box-body">
                    <div class="form-group">
                      <label for="inputEmail3" class="col-sm-2 control-label">Keterangan</label>
                      <div class="col-sm-10">
                        <textarea name='ket' class="form-control" rows="5" placeholder="Enter ..."><?php $ket = mysql_result(mysql_query("select ket from jualcstax where billno = $_GET[id] and ucode = '$ucode'"),0); echo "$ket"; ?></textarea>
                      </div>
                    </div>
                  </div><!-- /.box-body -->
				  
					
				
				
                </form>
              </div><!-- /.box -->
             
            </div><!--/.col (right) -->
			
			
			
					
                </div><!-- /.box-body -->
              </div><!-- /.box -->
            </div><!-- /.col -->
			
			
			
			
			<div class="col-md-12">
					  <!-- Horizontal Form -->
					  <div class="box box-info">
						<div class="box-header with-border">
						  <h3 class="box-title">Tambah Item Sales Order</h3>
						</div><!-- /.box-header -->
						<?php echo"
					<form id='demoForm2' name='form2' method=POST action='aksisales.php?module=sales&act=edittambah&id=$_GET[id]&ucode=$ucode' class='form-horizontal'>"; ?>
					<div class="col-xs-12">
						<div class=" col-md-4">
							<div class="form-group">
								<label>Produk</label>
									<select name="produk" id="produk"  class="form-control select2" tabindex="1">
										<?php
											$cek = mysql_query("Select * from produk where del = 'N'  order by produk asc");
											while ($r=mysql_fetch_array($cek)){	
												echo"<option value = $r[id] data-hrg= $r[unit_rp]>$r[sku] $r[produk]</option>";
											}	
										?>
									</select>
							</div>
						</div>
								
						<div class=" col-md-2">
							<div class="form-group">
								<label for="date">Qty</label>
								<div class="input-group">
								  <input type="number" class="form-control pull-right inp" id="date" name="qty" placeholder="Quantity"   tabindex="2" value="1"   onkeypress='return validateQty2(event);' required>
								</div><!-- /.input group -->
							</div><!-- /.form group -->
						</div>
						
						<div class=" col-md-2">
							<div class="form-group">
								<label for="date">Price</label>
								<div class="input-group">
								  <input type="number" class="form-control pull-right inp" id="hrg" name="price" placeholder="Price"   tabindex="3" value="1"  onkeypress='return validateQty2(event);' required>
								</div><!-- /.input group -->
							</div><!-- /.form group -->
						</div>
						
						<div class=" col-md-2">
							<div class="form-group">
								<label for="date">Disc (%)</label>
								<div class="input-group">
								  <input type="number" class="form-control pull-right inp" id="disc" name="disc" placeholder="Disc"   tabindex="4" value="0"  onkeypress='return validateQty(event);' required>
								</div><!-- /.input group -->
							</div><!-- /.form group -->
						</div>
							 
						 <div class="col-md-2">
							<div class="form-group">
								<label for="date"></label>
								<div class="input-group">
									<!--<button type="submit" id ="add" class="btn btn-lg btn-primary" name="addtocart" >+</button>-->
							 
									<input type="submit" id ="add" class="btn btn-lg btn-primary" value="+" name="addtocart"  tabindex="5">
								</div>
							</div>	
						</div>
						</form>	
					</div>
					
					</div>
			</div>
			
			
			
			<div class="col-xs-12">
				<div class="box box-info">
							  <div class="table-responsive">
								<table class="table no-margin">
								  <thead>
									<tr>
									  <th>Product</th>
									  <th>Qty</th>
									  <th>Harga</th>
									  <th>Diskon</th>
									  <th>Total</th>
									</tr>
								  </thead>
								  <tbody>
									<?php
									
										$user = $_SESSION['nama'];
										$cek = mysql_query("Select * from jualcstaxdetail where billno = $_GET[id] and ucode = '$ucode' order by id asc");

										while ($r=mysql_fetch_array($cek)){	
											$id = $r['id'];
											$harga = $r['harga'];
											$qty = format_rupiah($r['qty']);
											$disc = format_rupiah($r['disc']);
											$grand = format_rupiah($r['qty'] * ($r['harga'] - ($r['harga'] * $r['disc'] /100)));
											$produk = mysql_result(mysql_query("select produk from produk where id = $r[produk]"),0);
											$str	= json_encode($produk);
											echo "<tr>
												<form id='demoForm' name=form method=POST action='aksisales.php?module=sales&act=edit&id=$id&page=$page&ucode=$ucode' class='form-horizontal'>
												  <td><a class = 'btn btn-info' onclick='open_container($id,$str,$ucode,$_GET[id])'><i class = 'fa fa-times' style='font-size:24px;color:red'></i></a>&nbsp;&nbsp; $produk</td>
												  <td><input type='number' class='form-control pull-right inp' name='qty' placeholder='Quantity'   tabindex='2' value='$qty'  onkeypress='return /\d/.test(String.fromCharCode(((event||window.event).which||(event||window.event).which)));' required></td>
												  <td><input type='number' class='form-control pull-right inp' name='harga' placeholder='Harga'   tabindex='2' value='$harga'  onkeypress='return /\d/.test(String.fromCharCode(((event||window.event).which||(event||window.event).which)));' required></td>
												  <td><input type='number' class='form-control pull-right inp' name='disc' placeholder='Disc'   tabindex='2' value='$disc'  onkeypress='return /\d/.test(String.fromCharCode(((event||window.event).which||(event||window.event).which)));' required></td>
												  <td align = 'right'>$grand <button type='submit' class='btn btn-info'><span class='fa fa-floppy-o'> </span></button></td>
												  
												  </form>
												</tr>";
										}
									?>
									
								  </tbody>
								</table>
							  </div><!-- /.table-responsive -->
							</div><!-- /.box-body -->
							<div class="box-footer clearfix">
							  <a href="pages/service/data.php" class="btn btn-sm btn-default btn-flat pull-right">
								<?php
									$grandt2 = mysql_result(mysql_query("select sum(qty * (harga - (harga * disc /100))) from jualcstaxdetail where billno = $_GET[id] and ucode = '$ucode' "),0);
									$grandt = format_rupiah($grandt2);
									echo"Total : $grandt";
								?>
							  </a>
							  </div>	
							  <div class="box-footer clearfix">
							  <a  class="btn btn-sm btn-default btn-flat pull-right">
								<?php
									$diskon2 = mysql_result(mysql_query("select diskon from jualcstax where billno = $_GET[id] and ucode = '$ucode'  "),0);
									$diskon = format_rupiah($diskon2);
									echo"<form id='demoForm' name=form method=POST action='aksisales.php?module=sales&act=editdiskonn&id=$page&page=$page&ucode=$ucode' class='form-horizontal'>
									<input type='number' class='form-control pull-right inp' name='diskonn' placeholder='Disc'   tabindex='2' value='$diskon2'  onkeypress='return /\d/.test(String.fromCharCode(((event||window.event).which||(event||window.event).which)));' required>
									<br><br><button type='submit' class='btn btn-info'><span class='fa fa-floppy-o'> </span></button></form>";
								?>
							  </a>
							  </div>	
							  <div class="box-footer clearfix">
							  <a href="pages/service/data.php" class="btn btn-sm btn-default btn-flat pull-right">
								<?php
									$grandtot = format_rupiah($grandt2 - $diskon2);
									echo"Grand Total : $grandtot";
								?>
							  </a>
							</div>	
				</div>

          </div><!-- /.row -->
		  
		  
        </section><!-- /.content -->
		<!-- Modal form-->
                <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                  <div class="modal-dialog ">
                    <div class="modal-content">
                      <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                        <h4 class="modal-title" id="myModalLabel"></h4>
                      </div>
                      <div class="modal-body" id="modal-bodyku">
                      </div>
                      <div class="modal-footer" id="modal-footerq">
                      </div>
                    </div>
                  </div>
                </div>
                <!-- end of modal ------------------------------>
      </div><!-- /.content-wrapper -->
		<?php include "../footer.php";?>
		

      <!-- Control Sidebar -->
<!-- Control Sidebar -->
      <aside class="control-sidebar control-sidebar-dark">

        <!-- Tab panes -->
        <div class="tab-content">
          <!-- Home tab content -->
          <div class="tab-pane" id="control-sidebar-home-tab">

          </div><!-- /.tab-pane -->

        </div>
      </aside><!-- /.control-sidebar -->
      <!-- Add the sidebar's background. This div must be placed
           immediately after the control sidebar -->
      <div class="control-sidebar-bg"></div>
    </div><!-- ./wrapper -->

    <!-- jQuery 2.1.4 -->
    <script src="../../plugins/jQuery/jQuery-2.1.4.min.js"></script>
    <!-- Bootstrap 3.3.5 -->
    <script src="../../bootstrap/js/bootstrap.min.js"></script>
	<script src="../../plugins/select2/select2.full.min.js"></script>
    <!-- DataTables -->
    <script src="../../plugins/datatables/jquery.dataTables.min.js"></script>
    <script src="../../plugins/datatables/dataTables.bootstrap.min.js"></script>
    <!-- SlimScroll -->
    <script src="../../plugins/slimScroll/jquery.slimscroll.min.js"></script>
    <!-- FastClick -->
    <script src="../../plugins/fastclick/fastclick.min.js"></script>
    <!-- AdminLTE App -->
    <script src="../../dist/js/app.min.js"></script>
    <!-- AdminLTE for demo purposes -->
    <script src="../../dist/js/demo.js"></script>
    <!-- page script -->
	
	
    <script src="../../plugins/input-mask/jquery.inputmask.js"></script>
    <script src="../../plugins/input-mask/jquery.inputmask.date.extensions.js"></script>
<script>
      $(function () {
        //Initialize Select2 Elements


        //Datemask dd/mm/yyyy
        $("#datemask").inputmask("dd/mm/yyyy", {"placeholder": "dd/mm/yyyy"});
        //Money Euro
        $("[data-mask]").inputmask();

        

      
      });
    </script>

    <script>
      $(function () {
        $("#example1").DataTable();
        $('#example2').DataTable({
          "paging": true,
          "lengthChange": false,
          "searching": false,
          "ordering": true,
          "info": true,
          "autoWidth": false
        });
      });
    </script>
	<script>
      $(function () {
        //Initialize Select2 Elements
        $(".select2").select2();

       


        //iCheck for checkbox and radio inputs
        $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
          checkboxClass: 'icheckbox_minimal-blue',
          radioClass: 'iradio_minimal-blue'
        });
        //Red color scheme for iCheck
        $('input[type="checkbox"].minimal-red, input[type="radio"].minimal-red').iCheck({
          checkboxClass: 'icheckbox_minimal-red',
          radioClass: 'iradio_minimal-red'
        });
        //Flat red color scheme for iCheck
        $('input[type="checkbox"].flat-red, input[type="radio"].flat-red').iCheck({
          checkboxClass: 'icheckbox_flat-green',
          radioClass: 'iradio_flat-green'
        });

        //Colorpicker
        $(".my-colorpicker1").colorpicker();
        //color picker with addon
        $(".my-colorpicker2").colorpicker();

        //Timepicker
        $(".timepicker").timepicker({
          showInputs: false
        });
      });
    </script>
	
	<script type="text/javascript">
	
	function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode != 46 && charCode != 45 &&(charCode < 48 || charCode > 57)))
        return false;
    return true;
	}

function removeAllOptions(sel, removeGrp) {
    var len, groups, par;
    if (removeGrp) {
        groups = sel.getElementsByTagName('optgroup');
        len = groups.length;
        for (var i=len; i; i--) {
            sel.removeChild( groups[i-1] );
        }
    }
    
    len = sel.options.length;
    for (var i=len; i; i--) {
        par = sel.options[i-1].parentNode;
        par.removeChild( sel.options[i-1] );
    }
}

function appendDataToSelect(sel, obj) {
    var f = document.createDocumentFragment();
    var labels = [], group, opts;
    
    function addOptions(obj) {
        var f = document.createDocumentFragment();
        var o;
        
        for (var i=0, len=obj.text.length; i<len; i++) {
            o = document.createElement('option');
            o.appendChild( document.createTextNode( obj.text[i] ) );
            
            if ( obj.value ) {
                o.value = obj.value[i];
            }
            
            f.appendChild(o);
        }
        return f;
    }
    
    if ( obj.text ) {
        opts = addOptions(obj);
        f.appendChild(opts);
    } else {
        for ( var prop in obj ) {
            if ( obj.hasOwnProperty(prop) ) {
                labels.push(prop);
            }
        }
        
        for (var i=0, len=labels.length; i<len; i++) {
            group = document.createElement('optgroup');
            group.label = labels[i];
            f.appendChild(group);
            opts = addOptions(obj[ labels[i] ] );
            group.appendChild(opts);
        }
    }
    sel.appendChild(f);
}

// anonymous function assigned to onchange event of controlling select list
document.forms['demoForm'].elements['category'].onchange = function(e) {
    // name of associated select list
    var relName = 'choices[]';
	
	var relName2 = 'choices222[]';
    
    // reference to associated select list 
    var relList = this.form.elements[ relName ];
	
	var relList2 = this.form.elements[ relName2 ];
    
    // get data from object literal based on selection in controlling select list (this.value)
    var obj = Select_List_Data[ relName ][ this.value ];
	
	var obj2 = Select_List_Data2[ relName2 ][ this.value ];
    
    // remove current option elements
    removeAllOptions(relList, true);
	
	removeAllOptions(relList2, true);
    
    // call function to add optgroup/option elements
    // pass reference to associated select list and data for new options
    appendDataToSelect(relList, obj);
	appendDataToSelect(relList2, obj2);
};

// object literal holds data for optgroup/option elements
var Select_List_Data = {
    
    // name of associated select list
    'choices[]': {
        
        <?php
				$cek = mysql_query("Select * from categories  order by category asc");
				while ($r=mysql_fetch_array($cek)){	
				echo"$r[pk]: {
				
				text: [";
				
				$cek2 = mysql_query("Select * from products where category = '$r[pk]' order by code asc");
				while ($rr=mysql_fetch_array($cek2)){
					echo"'$rr[product] ',";
				}
				echo"],value: [";
				$cek3 = mysql_query("Select * from products where category = '$r[pk]' order by code asc");
				while ($rrr=mysql_fetch_array($cek3)){	
					echo"'$rrr[code]',";
				}

				echo"]
				},";
				
				}
		?>

        
    }

    
};




var Select_List_Data2 = {
    
    // name of associated select list
    'choices222[]': {
        
        <?php
				$salesman = $_SESSION['nama'];
				$cek = mysql_query("Select * from categories  order by category asc");
				while ($r=mysql_fetch_array($cek)){	
				echo"$r[pk]: {
				
				text: [";
				
				$cek2 = mysql_query("Select * from customers where sales = '$salesman' and produk = '$r[pk]'  order by customername asc");
				while ($rr=mysql_fetch_array($cek2)){
					echo"'$rr[customername] ',";
				}
				echo"],value: [";
				$cek3 = mysql_query("Select * from customers where sales = '$salesman' and produk = '$r[pk]'  order by customername asc");
				while ($rrr=mysql_fetch_array($cek3)){	
					echo"'$rrr[customerid]',";
				}

				echo"]
				},";
				
				}
		?>

        
    }

    
};

// populate associated select list when page loads
window.onload = function() {
    var form = document.forms['demoForm'];
    
    // reference to controlling select list
    var sel = form.elements['category'];
    sel.selectedIndex = 0;
	
	
	var sel2 = form.elements['category'];
    sel2.selectedIndex = 0;
    
    // name of associated select list
    var relName = 'choices[]';
    // reference to associated select list
    var rel = form.elements[ relName ];
	
	
	// name of associated select list
    var relName2 = 'choices222[]';
    // reference to associated select list
    var rel = form.elements[ relName2 ];
    
    // get data for associated select list passing its name
    // and value of selected in controlling select list
    var data = Select_List_Data[ relName ][ sel.value ];
	
	var data2= Select_List_Data2[ relName2 ][ sel2.value ];
    
    // add options to associated select list
    appendDataToSelect(rel, data);
	appendDataToSelect(rel2, data2);
};



//YM2




//YM2





function convert2() {    
 var price = 1;
 var selectedPrice = document.getElementById('choices');
 var currency = selectedPrice.options[selectedPrice.selectedIndex].text;
 var currencyValue = document.getElementById('choices').value; 



        newPrice = 0;

 //Display the new price to the user
 document.getElementById("txtValue").value = newPrice;
};
		   
function myFunction() {
	var selectedPrice = document.getElementById('choices');
	var currency = selectedPrice.options[selectedPrice.selectedIndex].text;
	var currencyValue = document.getElementById('choices').value; 
	
	var mySplitResult;
	mySplitResult = currency.split("@");
	for(i = 0; i < mySplitResult.length; i++)
           {
			newPrice =  mySplitResult[i];
           }
	newPrice = newPrice.trim();
	document.getElementById("txtValue").value = 0;
};
$('#qty').on('input', function() { 
	var UpriceOld = document.getElementById('txtValue').value;
	var Uprice;
	var Qty = document.getElementById('qty').value
	Uprice = UpriceOld.replace(".", "") ; 
	Uprice = UpriceOld.replace(".", "") ; 
	Uprice = Uprice * Qty;
    document.getElementById("txtTotal").value =  formatNumber(Uprice);
});
$('#txtValue').on('input', function() { 
	var UpriceOld = document.getElementById('txtValue').value;
	var Uprice;
	var Qty = document.getElementById('qty').value
	
	Uprice = UpriceOld.replace(".", "") ; 
	Uprice = UpriceOld.replace(".", "") ; 
	Uprice = Uprice * Qty;
    document.getElementById("txtTotal").value = formatNumber(Uprice);

});
function formatNumber(number)
{
    number = number.toFixed(0) + '';
    x = number.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

$('#cate').change(function() {
  myFunction();
});
</script>

<script language="javascript">
		function pad(n, width, z) {
		  z = z || '0';
		  n = n + '';
		  return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
		}
	 function open_container(id,id2,id3,id4)
	 {
	 var size='standart';
	 var content = '<form role="form" method=POST action="aksisales.php?module=sales&act=delitemss&id='+id+'&page='+id4+'&ucode='+id3+'" ><div class="form-group"><button type="submit" class="btn btn-default">Yakin</button></form>';
	 var title = 'Yakin untuk menghapus produk dibawah ini? <br> '+id2;
	 var footer = '<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>';
	 setModalBox(title,content,footer,size);
	 $('#myModal').modal('show');
	 }
	 function setModalBox(title,content,footer,$size)
	 {
	 document.getElementById('modal-bodyku').innerHTML=content;
	 document.getElementById('myModalLabel').innerHTML=title;
	 document.getElementById('modal-footerq').innerHTML=footer;
	 if($size == 'large')
	 {
	 $('#myModal').attr('class', 'modal fade bs-example-modal-lg')
		  .attr('aria-labelledby','myLargeModalLabel');
	 $('.modal-dialog').attr('class','modal-dialog modal-lg');
	 }
	 if($size == 'standart')
	 {
	 $('#myModal').attr('class', 'modal fade')
		  .attr('aria-labelledby','myModalLabel');
	 $('.modal-dialog').attr('class','modal-dialog');
	 }
	 if($size == 'small')
	 {
	 $('#myModal').attr('class', 'modal fade bs-example-modal-sm')
		  .attr('aria-labelledby','mySmallModalLabel');
	 $('.modal-dialog').attr('class','modal-dialog modal-sm');
	 }
	 }
 </script>
	
  </body>
</html>
<?php 
	}
?>