

 <?php  
// fungsi untuk pengecekan tampilan form
// jika form add data yang dipilih
if ($_GET['form']=='add') { 

	if ($_SESSION['hak_akses'] == "Super Admin"){ $readonly = "";} else{$readonly = "readonly";} 
?> 
  <!-- tampilan form add data -->
	<!-- Content Header (Page header) -->
  <section class="content-header">
    <h1>
      <i class="fa fa-edit icon-title"></i> Tambah Penjualan
    </h1>

  </section>

  <!-- Main content -->
  <section class="content">
    <div class="row">
      <div class="col-md-12">
        <div class="box box-primary">
          <!-- form start -->
         
            <div class="box-body">
			
	
					<div class="box-header with-border">
					  <h3 class="box-title">Item Penjualan</h3>
					</div><!-- /.box-header -->
			
			
			
					<form id='demoForm' name=form method=POST action='modules/penjualan/proses.php?act=additem' class='form-horizontal'>

							<div class=" col-md-4">
								<div class="form-group">
									<label>Produk</label>
										<select class="chosen-select" name="produkz" id="produk"  data-placeholder="-- Pilih --" tabindex="1">
											<option value='' data-hrg='1' ></option>
											<?php
												$query = mysqli_query($mysqli, "Select * from product where status = 'Y' and gudang = '$_SESSION[gudang]' order by product_name asc")
													or die('Ada kesalahan pada query tampil: '.mysqli_error($mysqli));
												while ($data = mysqli_fetch_assoc($query)) { 
													$harga = number_format($data['product_price'], 0, '', '') ;	
													$namaproduk = str_replace('-',' - ',$data['product_name']);
													echo"<option value='$data[product_id]' data-hrg= $harga>$namaproduk</option>";
												}
											?>
										</select>
								</div>
							</div>
								
							<div class=" col-md-1">
								<div class="form-group">
									<label for="date">Qty</label>
									<div class="input-group">
									   <input type="number" class="form-control pull-right inp"  name="qty" placeholder="Quantity"   tabindex="2" value="1" step="0.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">Harga</label>
									<div class="input-group">
									   <input type="number" class="form-control pull-right inp" id="hrg" name="harga" placeholder="Price"   tabindex="3" value="1"  onkeypress='return validateQty2(event);'  <?php echo"$readonly"; ?>  required>
									</div><!-- /.input group -->
								</div><!-- /.form group -->
							 </div>
							 
							  <div class=" col-md-1">
								<div class="form-group">
									<label for="date">Disc(%)</label>
									<div class="input-group">
									   <input type="number" class="form-control pull-right inp"  name="disc" placeholder="Disc"   tabindex="4" value="0"  onkeypress='return validateQty2(event);' >
									</div><!-- /.input group -->
								</div><!-- /.form group -->
							 </div>
							 
							 
							 <div class=" col-md-2">
								<div class="form-group">
									<label for="date">Disc Harga</label>
									<div class="input-group">
									   <input type="number" class="form-control pull-right inp"  name="discharga" placeholder="Disc Harga"   tabindex="5" value="0"  onkeypress='return validateQty2(event);' >
									</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 class="btn btn-lg btn-primary" type="submit" tabindex="3" name="addtocart">+</button>
									</div>
								</div>	
							</div>
							
				
					</form>
					
					
					<div class="col-xs-12">
							  <div class="table-responsive">
								<table class="table no-margin">
								  <thead>
									<tr>
									  <th>Product</th>
									  <th>Qty</th>
									  <th>Harga</th>
									  <th>Disc(%)</th>
									  <th>Disc Harga</th>
									  <th>Total</th>
									</tr>
								  </thead>
								  <tbody>
									<?php
										$user = $_SESSION['username'];
										$query1 = mysqli_query($mysqli, "Select * from tmpjual where kasir = '$user' order by id desc")
													or die('Ada kesalahan pada query tampil: '.mysqli_error($mysqli));
										$grandt = 0;
										while ($r = mysqli_fetch_assoc($query1)) { 
											$id = $r['id'];
											$harga = format_rupiah($r['harga']);
											$disc = format_rupiah($r['disc']);
											$discharga = format_rupiah($r['discharga']);
											$qty = $r['qty'];
											$grand = format_rupiah((($r['qty'] * $r['harga']) * (100 - $r['disc']) / 100) - $r['discharga']);
											
											$grandt = $grandt + ((($r['qty'] * $r['harga']) * (100 - $r['disc']) / 100) - $r['discharga']);
											
											$query4 = mysqli_query($mysqli, "select concat(kode,' ' ,product_name) as product_name from product where product_id = $r[produk]") 
														  or die('Ada kesalahan pada query tampil data user : '.mysqli_error($mysqli));
											$data4 = mysqli_fetch_assoc($query4);
											$produk = $data4['product_name'];
											 
											echo "<tr>
												  <td> <a data-toggle='tooltip' data-placement='top' title='Hapus' href='modules/penjualan/proses.php?act=deleteitem&id=$r[id]' onclick=\"return confirm('Anda yakin ingin menghapus item $produk ?')\"><i class = 'fa fa-times' style='font-size:24px;color:red'></i></a>&nbsp; $produk</td>
												  <td>$qty</td>
												  <td>$harga</td>
												  <td>$disc</td>
												  <td>$discharga</td>
												  <td align = 'right'>$grand</td>
												</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 = format_rupiah($grandt);
									echo"Grand Total : $grandt";
								?>
								<div  id='jualgrand' style="display:none;"><?php echo $grandt;?></div>
							  </a>
							</div>	
							
							
							
					
					
					
					
			<div class="box-header with-border">
			  <h3 class="box-title">Penjualan Info</h3>
			</div><!-- /.box-header -->		
					
			
     <form role="form" class="form-horizontal" action="modules/penjualan/proses.php?act=insert" method="POST">
	 
				
				  <div class="form-group" >
					<label class="col-sm-2 control-label">Disc(%)</label>
					<div class="col-sm-5">
					 <input type="text" class="form-control" id="harga_d1" name="disc1" value="<?php echo $d1; ?>"  max = "3" autocomplete="off" value="0"  onkeyup="updatePenjualan()" onKeyPress="return goodchars(event,'0123456789',this)" >
					</div>
				  </div>
				  
				  
				  <div class="form-group" >
					<label class="col-sm-2 control-label">Disc Harga</label>
					<div class="col-sm-5">
					 <input type="text" class="form-control" id="harga_d2" name="disc2" value="<?php echo $d2; ?>" autocomplete="off" value="0" onkeyup="updatePenjualan()"  onKeyPress="return goodchars(event,'0123456789',this)" >
					</div>
				  </div>
				  
				  
				  <div class="form-group" >
					<label class="col-sm-2 control-label">Grand Total</label>
					<div class="col-sm-5">
					 <input type="text" class="form-control" id="jualgt"  value="<?php 
					 $klr = $grandt;
					 $klr2 = number_format($klr,0);
					 echo $klr2; ?>"  disabled>
					</div>
				  </div>
				  
				  <hr>

              <div class="form-group">
                <label class="col-sm-2 control-label">Tanggal</label>
                <div class="col-sm-5">
                 <input type="text" class="form-control date-picker" data-date-format="dd-mm-yyyy" name="tanggal_masuk" autocomplete="off" value="<?php echo date("d-m-Y"); ?>" required>
                </div>
              </div>
			  
			  <div class="form-group">
                <label class="col-sm-2 control-label">Customer</label>
                <div class="col-sm-5">
                  <select class="chosen-select" name="supplier" data-placeholder="-- Pilih Customer --"  autocomplete="off" required>
                    <option value=""></option>
                    <?php
                      $query_obat = mysqli_query($mysqli, "SELECT * FROM customer where status = 'Y' and gudang = '$_SESSION[gudang]'ORDER BY nama ASC")
                                                            or die('Ada kesalahan pada query tampil : '.mysqli_error($mysqli));
                      while ($data_obat = mysqli_fetch_assoc($query_obat)) {
                        echo"<option value=\"$data_obat[id]\"> $data_obat[nama]  -  $data_obat[alamat]</option>";
                      }
                    ?>
                  </select>
                </div>
              </div>

          

              <div class="form-group">
                <label class="col-sm-2 control-label">Keterangan</label>
                <div class="col-sm-5">
                    <textarea class="form-control" style="white-space: pre-line;" wrap="hard" name="ket" autocomplete="off"  required> </textarea>
                </div>
              </div>


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

            <div class="box-footer">
              <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                  <input type="submit" class="btn btn-primary btn-submit" name="simpan" value="Simpan">
                  <a href="?module=penjualan" class="btn btn-default btn-reset">Batal</a>
                </div>
              </div>
            </div><!-- /.box footer -->
          </form>
	
        </div><!-- /.box -->
      </div><!--/.col -->
    </div>   <!-- /.row -->
  </section><!-- /.content -->
<?php
}
// jika form edit data yang dipilih
// isset : cek data ada / tidak
elseif ($_GET['form']=='edit') { 
  if (isset($_GET['id'])) {
      // fungsi query untuk menampilkan data dari tabel obat
      $query = mysqli_query($mysqli, "SELECT * FROM customer WHERE id='$_GET[id]'") 
                                      or die('Ada kesalahan pada query tampil : '.mysqli_error($mysqli));
      $data  = mysqli_fetch_assoc($query);
    }
?>
  <!-- tampilan form edit data -->
  <!-- Content Header (Page header) -->
  <section class="content-header">
    <h1>
      <i class="fa fa-edit icon-title"></i> View Penjualan
    </h1>

  </section>

  <!-- Main content -->
  <section class="content">
    <div class="row">
      <div class="col-md-12">
        <div class="box box-primary">
          <!-- form start -->
          <form role="form" class="form-horizontal" action="modules/customer/proses.php?act=update" method="POST">
            <div class="box-body">
				
				 <?php
					$query = mysqli_query($mysqli, "select * ,(select nama from customer where id = jual.supplier) as supp
					,(select sum((qty * harga * (100-disc)/100) - discharga) from jualdetail where billno = jual.billno) as tot  
					from jual where billno=$_GET[id]")
													or die('Ada kesalahan pada query tampil Data: '.mysqli_error($mysqli));
					while ($data = mysqli_fetch_assoc($query)) { 
						$billno = $_GET['id'];
						$suppname = $data['supp'];
						$supplier = $data['supplier'];
						$billdate = $data['billdate'];
						$tanggal = tgl_ind($data['billdate']);
						$d1 = $data['d1'];
						$d2 = format_rupiah($data['d2']);
						$tota = format_rupiah($data['tot']);
						$sub =  format_rupiah(($data['tot'] * (100 - $data['d1']) /100) - $data['d2']);
						$kodepo = $data['kodepo'];
						$ket = $data['keterangan'];
						$sj = $data['sj'];
						$inv = $data['inv'];
						$ongkos = $data['ongkos'];
					}
				?>
				<div class="col-md-6">
			
				  <div class="form-group">
					<label class="col-sm-2">Tanggal</label>
					<div class="col-sm-10">
						<?php 
							if($_SESSION['hak_akses'] == "Super Admin"){
								echo" <div class='edit2 control-label'  style='text-align:left;'><b>$tanggal</b></div> <input type = 'date' class='txtedit2' id='billdate_$billno' value = '$billdate' >";
							}else{
								echo" <div class='control-label'  style='text-align:left;'><b>$tanggal</b></div>";
							}
						?>
					</div>
				  </div>
				  
				  
				  <div class="form-group">
					<label class="col-sm-2">Customer</label>
					<div class="col-sm-10">
						<?php 
							if($_SESSION['hak_akses'] == "Super Admin"){
								echo" <div class='edit2 control-label'  style='text-align:left;'><b>$suppname</b></div> 
										<select class='txtedit2' id='supplier_$billno'>
											<option value = '$supplier'>$suppname</option>";
											
											$query5 = mysqli_query($mysqli, "SELECT * FROM customer where status='Y' ORDER BY nama asc")
                                            or die('Ada kesalahan pada query tampil: '.mysqli_error($mysqli));

											while ($data5 = mysqli_fetch_assoc($query5)) { 
												echo"<option value='$data5[id]'>$data5[nama]</option>";
											}

									echo"		
										</select>";
							}else{
								echo" <div class='control-label'  style='text-align:left;'><b>$suppname</b></div> ";
							}
						?>
					</div>
				  </div>
				  
				</div>
				
				<div class="col-md-6">
			
				  <div class="form-group">
					<label class="col-sm-4 control-label">&nbsp;</label>
					<div class="col-sm-8">
						<?php 
							if($_SESSION['hak_akses'] == "Super Admin"){
								echo" <div class='edit2 control-label'  style='text-align:left;'><b>$inv</b></div> <input type = 'text' class='txtedit2' id='inv_$billno' value = '$inv' >";
							}else{
								echo" <div class='control-label'  style='text-align:left;'><b>$inv</b></div>";
							}
						?>
					</div>
				  </div>
				  
				  
				  <div class="form-group">
					<label class="col-sm-4 control-label">&nbsp;</label>
					<div class="col-sm-8">						
						<?php 
							if($_SESSION['hak_akses'] == "Super Admin"){
								echo" <div class='edit2 control-label'  style='text-align:left;'><b>$sj</b></div> <input type = 'text' class='txtedit2' id='sj_$billno' value = '$sj' >";
							}else{
								echo" <div class=' control-label'  style='text-align:left;'><b>$sj</b></div>";
							}
						?>
					</div>
				  </div>
				  
				</div>
				

          <!-- tampilan tabel obat -->
          <table id="dataTables2" class="table table-bordered table-striped table-hover">
            <!-- tampilan tabel header -->
            <thead>
              <tr>
                <th class="center">No</th>
                <th class="center">Nama Produk</th>
				<th class="center">Qty</th>
                <th class="center" >Harga</th>
				<th class="center" >Disc(%)</th>
                <th class="center" >Disc Harga</th>
                <th class="center" >Sub Total</th>
              </tr>
            </thead>
            <!-- tampilan tabel body -->
            <tbody>
            <?php  
            $no = 1;
            // fungsi query untuk menampilkan data dari tabel obat
            $query = mysqli_query($mysqli, "SELECT *   ,jualdetail.id as idd
											FROM
												`product`
												INNER JOIN `jualdetail` 
													ON (`product`.`product_id` = `jualdetail`.`code`)
												INNER JOIN `jual` 
													ON (`jual`.`billno` = `jualdetail`.`billno`) 
													WHERE jual.billno= '" . $_GET[id] . "'")
                                            or die('Ada kesalahan pada query tampil Data Obat: '.mysqli_error($mysqli));

            // tampilkan data
			$i=0;
            while ($data = mysqli_fetch_assoc($query)) { 
				$billno = str_pad($data['billdate'], 6, "0", STR_PAD_LEFT);
				$harga2 = intval($data['harga']);
				$harga = format_rupiah($data['harga']);
				$disc = format_rupiah($data['disc']);
				$discharga2 = intval($data['discharga']);
				$discharga = format_rupiah($data['discharga']);
				$qty = $data['qty'];
				$tot = format_rupiah(($data['harga'] * $data['qty'] * (100 - $data['disc']) / 100) - $data['discharga']);
				$id = $data['idd'];
				$i=$i+1;
              // menampilkan isi tabel dari database ke tabel di aplikasi
              echo "<tr>
                      <td class='center'>$i</td>
                      <td>";
					  if($_SESSION['hak_akses'] == "Super Admin"){
					  echo"<a data-toggle='tooltip' data-placement='top' title='Hapus' href='modules/penjualan/proses.php?act=deleteitemview&id=$id&billno=$_GET[id]'
					  onclick=\"return confirm('Anda yakin ingin menghapus item $data[product_name] ?')\">
					  <i class = 'fa fa-times' style='font-size:24px;color:red'></i>
					  </a>  ";
					  }
					  
					  echo"
					   $data[product_name]</td>
					  <td  align='center'>";
								if($_SESSION['hak_akses'] == "Super Admin"){
									echo"
									<div class='edit3' >$qty</div> 
									<input type = 'number' class='txtedit3' id='qty_$id' value = '$qty' >";
								}else{
									echo"$qty";
								}
					  echo"										
					  </td>
                      <td  align='right'>";
								if($_SESSION['hak_akses'] == "Super Admin"){
									echo"
									<div class='edit3' >$harga</div> 
									<input type = 'number' class='txtedit3' id='harga_$id' value = '$harga2' >";
								}else{
									echo"$harga";
								}
					  echo"	
					  </td>         
					  <td  align='center'>";
								if($_SESSION['hak_akses'] == "Super Admin"){
									echo"
									<div class='edit3' >$disc</div> 
									<input type = 'number' class='txtedit3' id='disc_$id' value = '$disc' >";
								}else{
									echo"$disc";
								}
					  echo"	
					  </td> 
					  <td  align='center'>";
								if($_SESSION['hak_akses'] == "Super Admin"){
									echo"
									<div class='edit3' >$discharga</div> 
									<input type = 'number' class='txtedit3' id='discharga_$id' value = '$discharga2' >";
								}else{
									echo"$discharga";
								}
					  echo"	
					  </td> 					  
                      <td  align='right' >$tot</td>
                    </tr>";
              $no++;
            }
            ?>
            </tbody>
          </table>
		  
		  
				<div class="col-md-6">
			
				  <div class="form-group">
					<label class="col-sm-12">Keterangan :</label>
					<div class="col-sm-12">
						<?php 
								$billno = $_GET['id']; 
							if($_SESSION['hak_akses'] == "Super Admin"){
								echo" <div class='edit2 control-label'  style='text-align:left;'><b>$ket</b></div> <input type = 'text' class='txtedit2' id='keterangan_$billno' value = '$ket' >";
							}else{
								echo" <div class=' control-label'  style='text-align:left;'><b>$ket</b></div>";
							}
						?>
					</div>
				  </div>
				  
				
				  
				</div>
				
				<div class="col-md-6">
			
				  <div class="form-group">
					<label class="col-sm-6 control-label">Jumlah</label>
					<div class="col-sm-6">
						<?php echo" <div class='control-label'  style='text-align:right;'><b>$tota</b></div>";?>
					</div>
				  </div>
				  
				  
				  <div class="form-group">
					<label class="col-sm-6 control-label">Disc(%)</label>
					<div class="col-sm-6">
						<?php 
							if($_SESSION['hak_akses'] == "Super Admin"){
								echo" <div class='edit2 control-label'  style='text-align:right;'><b>$d1</b></div> <input type = 'number' class='txtedit2' id='d1_$billno' value = '$d1' >";
							}else{
								echo" <div class=' control-label'  style='text-align:right;'><b>$d1</b></div>";
							}
						?>
					</div>
				  </div>
				  
				  <div class="form-group">
					<label class="col-sm-6 control-label">Disc Harga</label>
					<div class="col-sm-6">
						<?php 
							if($_SESSION['hak_akses'] == "Super Admin"){
								echo" <div class='edit2 control-label'  style='text-align:right;'><b>$d2</b></div> <input type = 'number' class='txtedit2' id='d2_$billno' value = '$d2' >";
							}else{
								echo" <div class=' control-label'  style='text-align:right;'><b>$d2</b></div>";
							}
						?>
					</div>
				  </div>
				  
				  <div class="form-group">
					<label class="col-sm-6 control-label">TOTAL</label>
					<div class="col-sm-6">
						<?php echo" <div class='control-label'  style='text-align:right;'><b>$sub</b></div>";?>
					</div>
				  </div>
				  
				  
				  
			
				  
				  
				  
				</div>
				
				
				

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

            <div class="box-footer">
              <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                  <!--<input type="submit" class="btn btn-primary btn-submit" name="simpan" value="Simpan"> -->
                  <a href="?module=penjualan" class="btn btn-default btn-reset">Batal</a>
                </div>
              </div>
            </div><!-- /.box footer -->
          </form>
        </div><!-- /.box -->
      </div><!--/.col -->
    </div>   <!-- /.row -->
  </section><!-- /.content -->
<?php
}
?>