<?php
# Baca Variabel Form
$message = array();
$Act	 	 	= isset($_REQUEST['Act']) ? $_REQUEST['Act'] : '';
$Act_Det 	 	= isset($_REQUEST['Act_Det']) ? $_REQUEST['Act_Det'] : '';
$stoh_id 	 	= isset($_REQUEST['stoh_id']) ? $_REQUEST['stoh_id'] : '';
$stoh_no  		= isset($_REQUEST['stoh_no']) ? $_REQUEST['stoh_no'] : getstono();
$stoh_date  	= isset($_REQUEST['stoh_date']) ? $_REQUEST['stoh_date'] : '';
$stoh_cekno 	= isset($_REQUEST['stoh_cekno']) ? $_REQUEST['stoh_cekno'] : '';
$stoh_cekdate   = isset($_REQUEST['stoh_cekdate']) ? $_REQUEST['stoh_cekdate'] : '';
$stoh_pic	 	= isset($_REQUEST['stoh_pic']) ? $_REQUEST['stoh_pic'] : '';
$stoh_prodcat	= isset($_REQUEST['stoh_prodcat']) ? $_REQUEST['stoh_prodcat'] : '';
$id_det		    = isset($_REQUEST['id_det']) ? $_REQUEST['id_det'] : '';
$id_deth		= isset($_REQUEST['id_deth']) ? $_REQUEST['id_deth'] : '';
$stoh_whid		= isset($_REQUEST['stoh_whid']) ? $_REQUEST['stoh_whid'] : 0;
$pcls			= isset($_REQUEST['pcls']) ? $_REQUEST['pcls'] : 0;
$month_start 	= substr($start_date,5,2);
$year_start  	= substr($start_date,0,4); 	
#parameter for closing
$month_end	    = substr($end_date,5,2);
$year_end	    = substr($end_date,0,4);
if($month_end==12){
	$month_close = 1;
	$year_close  = $year_end+1;
}else{
	$month_close = $month_end+1;
	$year_close  = $year_end;
}
$tanggal ="$year_close-$month_close-01";
$year_sto   = substr($stoh_date,0,4);
$month_sto  = substr($stoh_date,5,2);

if($Act =="SAVE"){
	if(count($message)==0){	
		if(empty($stoh_id)){
			$qrySave=mysql_query("INSERT INTO stockopname SET stoh_no='$stoh_no', stoh_date='$stoh_date', stoh_cekno='$stoh_cekno', 
							  stoh_cekdate='$stoh_cekdate', stoh_pic='$stoh_pic', stoh_prodcat='$stoh_prodcat', stoh_inputby='$user_id',
							  stoh_inputdate='$datetime', stoh_status=10, stoh_whid='$stoh_whid'") or die ("Gagal query".mysql_error());
			if($qrySave){
				$data = mysql_fetch_object(mysql_query("SELECT MAX(id) id FROM stockopname"));
				$stoh_id = $data->id;
				foreach($id_det as $i => $id_no){
					$id					= $_REQUEST['id'][$id_no];
					$stod_qtysaldo		= $_REQUEST['stod_qtysaldo'][$id_no];
					$stod_qtysto		= $_REQUEST['stod_qtysto'][$id_no];
					$stock_price		= $_REQUEST['stock_price'][$id_no];
					$stock_doctype		= $_REQUEST['stock_doctype'][$id_no];
					$stock_docno		= $_REQUEST['stock_docno'][$id_no];
					$stock_docdate		= $_REQUEST['stock_docdate'][$id_no];
					$stock_loc			= $_REQUEST['stock_loc'][$id_no];
					$stock_currency		= isset($_REQUEST['stock_currency'][$id_no]) ? $_REQUEST['stock_currency'][$id_no] :'';
					mysql_query("INSERT INTO stockopname_detail SET stod_hid='$stoh_id', stod_prodid='$id', stod_qtysaldo='$stod_qtysaldo',stod_qtysto='$stod_qtysto'");
					#Delete data sebelumnya
					#mysql_query("DELETE FROM stock WHERE MONTH(stock_date)='$month_close' AND YEAR(stock_date)='$year_close' AND stock_status=10 AND stock_prodid='$id' AND stock_loc='$stock_loc'");
					#Insert kembali
					mysql_query("INSERT INTO stock_log SET stock_stoid='$stoh_id', stock_date='$tanggal', stock_prodid='$id', stock_qty='$stod_qtysto', stock_status=10, stock_price='$stock_price',
					stock_doctype='$stock_doctype', stock_docno='$stock_docno', stock_docdate='$stock_docdate', stock_loc='$stock_loc', stock_currency='$stock_currency', stock_whid='$stoh_whid'");
				}					
			}	
			#Insert History User
			$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan data stock opname No. $stoh_no', user_date='$date', user_time='$time'");
						  
		}elseif(!empty($stoh_id) && ($Act_Det <> "ADD_DET")){
			$qrySave=mysql_query("UPDATE stockopname SET stoh_date='$stoh_date', stoh_cekno='$stoh_cekno', 
								  stoh_cekdate='$stoh_cekdate', stoh_pic='$stoh_pic', stock_whid='$stoh_whid' WHERE id='$stoh_id'") or die ("Gagal query".mysql_error());
			if($qrySave){
				foreach($id_det as $i => $id)
				{
					$stod_qtysaldo		= $_REQUEST['stod_qtysaldo'][$id];
					$stod_qtysto		= $_REQUEST['stod_qtysto'][$id];
					mysql_query("UPDATE stockopname_detail SET stod_qtysaldo='$stod_qtysaldo',stod_qtysto='$stod_qtysto' WHERE id='$id'");
					#Delete data sebelumnya
					//mysql_query("DELETE FROM stock WHERE MONTH(stock_date)='$month_close' AND YEAR(stock_date)='$year_close' AND stock_status=10 AND stock_prodid='$id'");
					#Insert kembali
					//mysql_query("INSERT INTO stock SET stock_trid=0, stock_date='$tanggal', stock_prodid='$id', stock_qty='$stod_qtysto', stock_status=10");
				}
				#Insert History User
				$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Mengubah data stock opname No. $stoh_no', user_date='$date', user_time='$time'");
			}							  
		}				
		if($qrySave){
			$message[] = "Data Sukses tersimpan !";	
			$status = "OK";
		}
	}	
}		
if($Act =="DETAIL"){ 
	$qd = mysql_query("SELECT * FROM stockopname WHERE stoh_status <> 100 AND id='$stoh_id'");
	$dd = mysql_fetch_object($qd);		
	$stoh_id 		= $dd->id;			
	$stoh_no		= $dd->stoh_no;
	$stoh_date		= $dd->stoh_date;
	$stoh_cekno	    = $dd->stoh_cekno;
	$stoh_cekdate	= $dd->stoh_cekdate;
	$stoh_pic		= $dd->stoh_pic;
	$stoh_prodcat  	= $dd->stoh_prodcat;
}
if($Act =="DELETE"){ 
	$qd = mysql_query("UPDATE stockopname SET stoh_status=100 WHERE id='$stoh_id'");
	if($qd){
		#Update status stock
		mysql_query("UPDATE stock SET stock_status=100 WHERE stock_stoid='$stoh_id'");
		#Update status stock
		mysql_query("DELETE FROM stock_log WHERE stock_stoid='$stoh_id'");
		#Insert History User
		$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menghapus data stock opname No. $stoh_no', user_date='$date', user_time='$time'");
		$message[] = "Data Sukses terhapus !";	
		$status = "OK";
	}
}
if($Act =="POSTING"){ 
	#Update Status
	$qd = mysql_query("UPDATE stockopname SET stoh_status=20 WHERE id='$stoh_id'");
	#insert ke stock
	$qs = mysql_query("SELECT * FROM stock_log WHERE stock_stoid='$stoh_id' ORDER BY id ASC");
	while($ds=mysql_fetch_array($qs)){
		mysql_query("INSERT INTO stock SET stock_stoid='$ds[stock_stoid]', stock_date='$ds[stock_date]', stock_prodid='$ds[stock_prodid]', stock_qty='$ds[stock_qty]', stock_status=10, stock_whid='$ds[stoh_whid]',
		stock_price='$ds[stock_price]',	stock_doctype='$ds[stock_doctype]', stock_docno='$ds[stock_docno]', stock_docdate='$ds[stock_docdate]', stock_loc='$ds[stock_loc]', stock_currency='$ds[stock_currency]'");
	}
	#delete stock log
	mysql_query("DELETE FROM stock_log WHERE stock_stoid='$stoh_id'");
	#Insert History User
	$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Posting stock opname No. $stoh_no', user_date='$date', user_time='$time'");
	$message[] = "Data Sukses diposting !";	
	$status = "OK";				
}
if($Act == "ADD" || $Act == "DETAIL" || $Act_Det == "ADD_DET" || $Act_Det == "DELETE_DET"){
	require_once "add_stock_opname.php";
}
if($Act == "ADDP" || $Act == "SELP"){
	require_once "addp_stock_opname.php";
}
if($Act =="SAVEP"){
	$periode = isset($_REQUEST['periode']) ? $_REQUEST['periode'] : '';
	$sbulan  = isset($_REQUEST['sbulan']) ? $_REQUEST['sbulan'] : '';
	$ptahun	 = isset($_REQUEST['ptahun']) ? $_REQUEST['ptahun'] : '';
	if($pcls==1){
		$eday  = jumlah_hari($sbulan, $ptahun);
		$sdate = "$ptahun-$sbulan-01";
		$edate = "$ptahun-$sbulan-$eday";
	}else{
		if($periode=="per1"){
			$sdate = "$ptahun-01-01";
			$edate = "$ptahun-03-31";
		}elseif($periode=="per2"){
			$sdate = "$ptahun-04-01";
			$edate = "$ptahun-06-30";
		}elseif($periode=="per3"){
			$sdate = "$ptahun-07-01";
			$edate = "$ptahun-09-30";
		}elseif($periode=="per4"){
			$sdate = "$ptahun-10-01";
			$edate = "$ptahun-12-31";
		}
	}
	#periode
	mysql_query("UPDATE transfer_detail SET tiheula='$sdate', pandeuri='$edate' WHERE id=186");
	mysql_query("UPDATE periode SET p_startdate='$sdate', p_enddate='$edate' WHERE p_status=10");
	#history
	mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Merubah periode closing : $sdate ~ $edate', user_date='$date', user_time='$time'");
	$message[] = "Periode sukses dirubah ";	
	$status = "OK";	
}
if($Act == "CLOSE"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-Stock-Opname'>";
}
if (! count($message)==0 ){
	?>
		<script language="javascript">
			var pesan ='<?php 
							$Num=0;
							foreach ($message as $indeks=>$pesan_tampil) { 
								$Num++;
								echo $Num.'. '.$pesan_tampil.'\n';
							}
						?>';
			alert(pesan);
		</script>
	<?php  
	if ($status=="OK"){	
		echo "<meta http-equiv='refresh' content='0; url=?page=Data-Stock-Opname'>";	
		exit;
	}
}	
?>
