<?php
include "../../inc/inc.koneksi.php";

session_start();

$table	="stock";

date_default_timezone_set('Asia/Jakarta');

$kodegr		=$_POST['kodegr'];
$kodest	 	=$_POST['kodest'];
$namast		=$_POST['namast'];
$barcode	=$_POST['barcode'];
$minstok 	=$_POST['minstok'];
$satuan	 	=$_POST['satuan'];
$hgbeli  	=$_POST['hgbeli'];
$hgjual  	=$_POST['hgjual'];
$hgresep 	=$_POST['hgresep'];
$hgparam 	=$_POST['hgparam'];
$kodesp 	=$_POST['kodesp'];
$namasp 	=$_POST['namasp'];
$kodesp1	=$_POST['kodesp1'];
$namasp1	=$_POST['namasp1'];
$user		=$_SESSION['username'];
$tgllap		=date('d-m-Y');
$jam		=date('H:i:s');

$sql= mysql_query("SELECT * FROM $table WHERE kode_st='$kodest'");
$row= mysql_num_rows($sql);
if($row>0){
	mysql_query("UPDATE $table SET nama_st='$namast',barcode='$barcode',satuan='$satuan',hg_beli='$hgbeli',hg_jual='$hgjual',hg_resep='$hgresep',
	hg_param='$hgparam',kode_sp='$kodesp',nama_sp='$namasp',kode_sp1='$kodesp1',nama_sp1='$namasp1',min_stokb='$minstok',lastupdate=now() WHERE kode_st='$kodest'");
	
	mysql_query("UPDATE $table SET nama_st='$namast',barcode='$barcode',satuan='$satuan',hg_beli='$hgbeli',hg_jual='$hgjual',hg_resep='$hgresep',
	hg_param='$hgparam',kode_sp='$kodesp',nama_sp='$namasp',kode_sp1='$kodesp1',nama_sp1='$namasp1',min_stok='$minstok',lastupdate=now() WHERE kode_st='$kodest'");
	
	mysql_query("INSERT INTO log_history (user,tanggal,jam,aktivitas,no_bukti,nama_st,keterangan) VALUES('$user','$tgllap','$jam','UBAH DATA BARANG','','$tnamast','Diubah $namast, hg.jual $thgjual jadi $hgjual, hg.grosir $thgresep jadi $hgresep')");
	echo "Data sukses diubah.";
}
else{
    $sql1=mysql_query("SELECT * FROM cabang WHERE 1 ORDER BY kode_cab");
    while($r1=mysql_fetch_array($sql1)){
        $kodecab=$r1['kode_cab'];
        mysql_query("INSERT INTO $table (kode_gr,kode_st,kode_cab,nama_st,barcode,satuan,hg_beli,hb_akhir,hg_jual,hg_resep,hg_param,kode_sp,nama_sp,kode_sp1,nama_sp1,
        min_stok,min_stokb,stok_tl,stok_tlb,jml_stok,jml_stokb,expired,umur,lastupdate) VALUES('$kodegr','$kodest','$kodecab','$namast','$barcode','$satuan','$hgbeli',
        '$hgbeli','$hgjual','$hgresep','$hgparam','$kodesp','$namasp','$kodesp1','$namasp1','$minstok','0','0','0','0','0','','0',now())");
    }
	echo "Data sukses disimpan";
}
?>