<?php
# Baca Variabel Form
$message 	 = array();
$Act		 = isset($_REQUEST['Act']) ? $_REQUEST['Act'] :'';
$smonth 	 = isset($_REQUEST['smonth']) ? $_REQUEST['smonth'] :substr(get_date(),5,2);
$syear		 = isset($_REQUEST['syear']) ? $_REQUEST['syear'] :substr(get_date(),0,4);
$fromdate 	 = isset($_REQUEST['fromdate']) ? $_REQUEST['fromdate'] :$start_date;
$todate 	 = isset($_REQUEST['todate']) ? $_REQUEST['todate'] :$end_date;
$pilih		 = isset($_REQUEST['pilih']) ? $_REQUEST['pilih'] : 0;
$p_tahun	 = isset($_REQUEST['p_tahun']) ? $_REQUEST['p_tahun'] : '';
$bln 		 = substr($todate,5,2);
$thn 		 = substr($todate,0,4);
if($bln==12){$bulan='01'; $tahun=$thn+1;}else{$bulan=$bln+1; $tahun=$thn;}
$lbln        = strlen($bulan);
if($lbln==1){$bulan="0$bulan";}else{$bulan=$bulan;}
$closingdate = "$tahun-$bulan-01";
#Proses simpan dan edit
if($Act=="SAVE"){
  	/*$qcoa = mysql_query("SELECT j_acctno, CONCAT_WS(' - ', j_acctno, j_name1) codename, j_rumusbb FROM jurnal_master ORDER BY j_acctno ASC");
	while($dcoa=mysql_fetch_array($qcoa)){
		$typ = substr($dcoa['j_acctno'],0,1);
		$sa=mysql_fetch_array(mysql_query("SELECT id, sa_price, sa_price_usd FROM stock_acct WHERE sa_date='$fromdate' AND sa_status=10 AND sa_acctno='$dcoa[j_acctno]'
			ORDER BY id DESC LIMIT 0,1"));		
		$saldo =$sa['sa_price']; $t_amountin=0; $t_amountout=0; $saldo_usd =$sa['sa_price_usd']; $t_amountin_usd=0; $t_amountout_usd=0;
		$qd=mysql_query("SELECT a.tanggal, a.jurnal_no, a.keterangan, a.jurnal_nofaktur, a.custsup_name, a.amountin, a.amountin_usd, a.amountout, a.amountout_usd, a.flag FROM
				(SELECT a.jurnal_date tanggal, a.jurnal_no, b.jurnald_desc keterangan, a.jurnal_nofaktur, 
				CASE WHEN a.jurnal_custname<>'' THEN a.jurnal_custname ELSE a.jurnal_suppname END AS custsup_name, b.jurnald_amount amountin, b.jurnald_amount_usd amountin_usd, 
				0 amountout, 0 amountout_usd, 1 flag FROM jurnal a INNER JOIN jurnal_detail b ON b.jurnal_no=a.jurnal_no
				WHERE a.jurnal_status=20 AND a.jurnal_date BETWEEN '$fromdate' AND '$todate' AND jurnald_acctno='$dcoa[j_acctno]' AND b.jurnald_status=10		
				UNION ALL
				SELECT a.jurnal_date tanggal, a.jurnal_no, b.jurnald_desc keterangan, a.jurnal_nofaktur, 
				CASE WHEN a.jurnal_custname<>'' THEN a.jurnal_custname ELSE a.jurnal_suppname END AS custsup_name, 0 amountin, 0 amountin_usd,
				b.jurnald_amount amountout, b.jurnald_amount_usd amountout_usd, 2 flag FROM jurnal a INNER JOIN jurnal_detail b ON b.jurnal_no=a.jurnal_no
				WHERE a.jurnal_status=20 AND a.jurnal_date BETWEEN '$fromdate' AND '$todate' AND jurnald_acctno='$dcoa[j_acctno]' AND b.jurnald_status=20) a
				ORDER BY a.tanggal ASC");
		$num = mysql_num_rows($qd);		
		if($num==0){
			$t_saldo_usd = $saldo_usd;
			$t_saldo     = $saldo;
		}else{		
		while($dd=mysql_fetch_array($qd)){	
			if($dcoa['j_rumusbb']=='A'){
				$saldo_usd 	= $saldo_usd+$dd['amountin_usd']-$dd['amountout_usd'];
				$saldo 		= $saldo+$dd['amountin']-$dd['amountout'];
			}else{
				$saldo_usd 	= $saldo_usd-$dd['amountin_usd']+$dd['amountout_usd'];
				$saldo 		= $saldo-$dd['amountin']+$dd['amountout'];
			}
			$t_saldo_usd = $saldo_usd;
			$t_saldo	 = $saldo;
			$t_amountin_usd  = $t_amountin_usd+$dd['amountin_usd'];
			$t_amountin  	 = $t_amountin+$dd['amountin'];
			$t_amountout_usd = $t_amountout_usd+$dd['amountout_usd'];
			$t_amountout 	 = $t_amountout+$dd['amountout'];
		}
	 } */
		#delete
		mysql_query("UPDATE stock_acct SET sa_status=100 WHERE sa_status=10 AND sa_date='$closingdate'");
		/*foreach($pilih as $i => $id){
			$sa_acctno		= $_REQUEST['j_acctno'][$id];
			$sa_price		= $_REQUEST['sa_price'][$id];
			$sa_price_usd	= $_REQUEST['sa_price_usd'][$id];
			#if($sa_price != 0){
				mysql_query("INSERT INTO stock_acct SET sa_status=10, sa_date='$closingdate', sa_acctno='$sa_acctno', sa_price='$sa_price', sa_currency='IDR', sa_price_usd='$sa_price_usd'");
			#}
		} */
		$qh = mysql_query("SELECT * FROM jurnal_master WHERE j_pclosing='Y' AND j_status=10 ORDER BY j_acctno ASC");
		while($dh=mysql_fetch_object($qh)){	
			$lastmonth1   	  = get_nr_lastmonth($fromdate,$todate,$dh->j_acctno);
			$curmonth1        = get_nr_curmonth($fromdate,$todate,$dh->j_acctno);
			$curyear1         = $lastmonth1+$curmonth1;
			
			$lastmonth1_usd   = get_nr_lastmonth_usd($fromdate,$todate,$dh->j_acctno);
			$curmonth1_usd    = get_nr_curmonth_usd($fromdate,$todate,$dh->j_acctno);
			$curyear1_usd     = $lastmonth1_usd+$curmonth1_usd;
			
			mysql_query("INSERT INTO stock_acct SET sa_status=10, sa_date='$closingdate', sa_acctno='$dh->j_acctno', sa_price='$curyear1', sa_currency='IDR', sa_price_usd='$curyear1_usd'");
		}	
	#insert history	
	mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Closing data akuntansi. $fromdate ~ $todate', user_date='$date', user_time='$time'"); 
	$message[] = "Data Sukses diclosing.";	
	$status    = "OK";
}
if($Act=="SAVEP"){
	#update periode tahun akuntansi
	mysql_query("UPDATE periode SET p_tahun='$p_tahun' WHERE p_status=90");
	#Insert History User
	mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Mengubah periode akuntansi. $p_tahun', user_date='$date', user_time='$time'");
		
	$message[] = "Data periode sukses diubah.";	
	$status    = "OK";
}

if($Act=="ADD" || $Act=="VIEW" || $Act=="ADDP"){
	require_once "add_closing_acct.php";
}
if($Act=="CLOSE"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-Closing-Acct'>";
}
# Popup pesan
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-Closing-Acct'>";	
	}
}
?>