

 <?php  
// fungsi untuk pengecekan tampilan form
// jika form add data yang dipilih
if ($_GET['form']=='add') { ?> 
  <!-- tampilan form add data -->
	<!-- Content Header (Page header) -->
  <section class="content-header">
    <h1>
      <i class="fa fa-edit icon-title"></i> Tambah Penjualan
    </h1>
    <ol class="breadcrumb">
      <li><a href="?module=beranda"><i class="fa fa-home"></i> Beranda </a></li>
      <li><a href="?module=penjualan"> Penjualan </a></li>
      <li class="active"> Tambah </li>
    </ol>
  </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="produk" id="produk"  data-placeholder="-- Pilih --" tabindex="1">
											<?php
												$query = mysqli_query($mysqli, "Select * from product where status = 'Y' order by product_name asc")
													or die('Ada kesalahan pada query tampil: '.mysqli_error($mysqli));
												while ($data = mysqli_fetch_assoc($query)) { 
													echo"<option value='$data[product_id]'>$data[kode] $data[product_name]</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"  name="qty" placeholder="Quantity"   tabindex="2" value="1"   onkeypress='return validateQty2(event);' required>
									</div><!-- /.input group -->
								</div><!-- /.form group -->
							</div>
					
							 <div class=" col-md-4">
								<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);' 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 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>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));

										while ($r = mysqli_fetch_assoc($query1)) { 
											$id = $r['id'];
											$harga = format_rupiah($r['harga']);
											$qty = format_rupiah($r['qty']);
											$grand = format_rupiah($r['qty'] * $r['harga']);
											$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 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
									$query5 = mysqli_query($mysqli, "select sum(qty * harga) as hasil from tmpjual where kasir = '$user'") 
														  or die('Ada kesalahan pada query tampil data user : '.mysqli_error($mysqli));
									$data5 = mysqli_fetch_assoc($query5);
									$grandt = format_rupiah($data5['hasil']);
									echo"Grand Total : $grandt";
								?>
							  </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">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' 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] </option>";
                      }
                    ?>
                  </select>
                </div>
              </div>

          

              <div class="form-group">
                <label class="col-sm-2 control-label">Kode SO</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control"  name="kode" autocomplete="off" required>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label">Keterangan SO</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" name="ket" autocomplete="off"  required>
                </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=pembelian" 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>
    <ol class="breadcrumb">
      <li><a href="?module=beranda"><i class="fa fa-home"></i> Beranda </a></li>
      <li><a href="?module=penjualan"> Penjualan </a></li>
      <li class="active"> View </li>
    </ol>
  </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) 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)) { 
					$suppname = $data['supp'];
					$tanggal = tgl_ind($data['billdate']);
					$tot = format_rupiah($data['tot']);
					$kodepo = $data['kodepo'];
					$ket = $data['keterangan'];
				}
								
			echo"
			<h5>
			Tanggal : $tanggal<br>
			Customer : $suppname<br>
			Total : $tot<br>
			Kode SO : $kodepo<br>
			Keterangan : $ket<br>
			</h5>"
			?>
          <!-- 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" >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 *
											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);
				$harga = format_rupiah($data['harga']);
				$qty = $data['qty'];
				$tot = format_rupiah($data['harga'] * $data['qty']);
				
				$i=$i+1;
              // menampilkan isi tabel dari database ke tabel di aplikasi
              echo "<tr>
                      <td class='center'>$i</td>
                      <td>$data[kode] $data[product_name]</td>
					  <td  align='center'>$qty</td>
                      <td  align='right'>$harga</td>                      
                      <td  align='right' >$tot</td>
                    </tr>";
              $no++;
            }
            ?>
            </tbody>
          </table>

            </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=pembelian" class="btn btn-default btn-reset">Batal</a>
                </div>
              </div>
            </div><!-- /.box footer -->
          </form>
        </div><!-- /.box -->
      </div><!--/.col -->
    </div>   <!-- /.row -->
  </section><!-- /.content -->
<?php
}
?>