<?php

header("X-Content-Type-Options: nosniff");
header("X-XSS-Protection: 1; mode=block");
header("X-Frame-Options: DENY");
header('Expires: Sun, 01 Jan 2000 00:00:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', FALSE);
header('Pragma: no-cache');

ini_set('session.cookie_httponly', 'on');
ini_set('session.cookie_secure', 'on');

session_name("SIGNADMAUTH");
session_start();

include "common/config/config.php";
include "common/config/dbconfig.php";
include "common/functions/funcconnect.php";
include "common/functions/funcauthen.php";
include "common/functions/funcvalidateinput.php";
openDB();

if (!authen())
{ header ("location: login.php");
		}
else
{ $idpdetail=isset($_GET['ida']) ? cleanval($_GET['ida']) : NULL;
		$idpesanan=isset($_GET['idp']) ? cleanval($_GET['idp']) : NULL;
		$nomorpesanan=isset($_GET['oid']) ? cleanval(base64_decode($_GET['oid'])) : NULL;
		$konfirmasi=isset($_GET['konfirmasi']) ? cleanval($_GET['konfirmasi']) : NULL;
		$script="";
		
		// cek id dan nomor pesanan
		$qord="SELECT a.*
									FROM $db_pesanan a,
														$db_pdetailapproval b
									WHERE a.idpesanan='".$idpesanan."'
															AND a.nomorpesanan='".$nomorpesanan."'
															AND a.statusorder<4
															AND b.nomorpesanan=a.nomorpesanan
															AND b.sesipesanan=a.sesipesanan
															AND b.tolaktgl IS NULL
															AND b.setujutgl IS NULL
															AND b.idpdetailapproval='".$idpdetail."'
									";
		$rord=mysqli_query($dbConnection, $qord);
		// echo $qord."<br><br>";
		if (mysqli_num_rows($rord)>0)
		{ $dord=mysqli_fetch_array($rord);
				if (empty($konfirmasi))
				{ $msg="Yakin ingin membatalkan perubahan data produk di pesanan ini?<br><br>
												<input type='button' value=' Ya, Batalkan ' style='cursor:pointer' onclick=\"document.location='produk_ubah_batal.php?ida=".$idpdetail."&idp=".$idpesanan."&oid=".base64_encode($nomorpesanan)."&konfirmasi=1'\"> &nbsp;
												<input type='button' value=' Tidak ' style='cursor:pointer' onclick='history.back(-1)'>
												";
						}
				else
				{ $qup="UPDATE $db_pdetailapproval
												SET tolaktgl='".$wktsekarangdb."',
																tolakoleh='".$_SESSION['SES_USERNAME']."',
																tolaksebab='BATAL UBAH'
												WHERE nomorpesanan='".$dord['nomorpesanan']."'
																		AND sesipesanan='".$dord['sesipesanan']."'
																		AND idpdetailapproval='".$idpdetail."'
																		AND tolaktgl IS NULL
																		AND setujutgl IS NULL
												";
						$rup=mysqli_query($dbConnection, $qup);
						// echo $qup;
						// exit;
						if ($rup)
						{ $catatan="Membatalkan perubahan data produk yang dipesan (ID: ".$idpdetail.").";
								$qic="INSERT INTO $db_pesanannote (nomorpesanan,
																																											sesipesanan,
																																											idrekanan,
																																											idtoko,
																																											statusorder,
																																											catatan,
																																											catattgl,
																																											catatoleh)
														VALUES ('".$dord['nomorpesanan']."',
																						'".$dord['sesipesanan']."',
																						'".$dord['idrekanan']."',
																						'".$dord['idtoko']."',
																						'".$dord['statusorder']."',
																						'".$catatan."',
																						'".$wktsekarangdb."',
																						'".$_SESSION['SES_USERNAME']."')
														";
								$ric=mysqli_query($dbConnection, $qic);
								$msg="Sukses membatalkan perubahan data produk.";
								$script.="<script>
																			var goBack = function ()
																																{ setTimeout(function()
																																													{ window.location='konfirmasidata.php?idp=".$dord['idpesanan']."&oid=".base64_encode($dord['nomorpesanan'])."';
																																															}, 
																																													1000);
																																		};
																			window.onload = goBack();
																		</script>
																		";
								}
						else
						{ $msg="Gagal membatalkan perubahan data produk<br><br>".$linkback;
								// $msg.="<br><br>".$qup.mysqli_error($dbConnection);
								}
						}
				}
		else
		{ $msg="Perubahan data produk yang belum ditolak/disetujui tidak ditemukan.<br><br>
										Biasanya terjadi karena perubahan data produk sudah ditolak/disetujui atau pesanan dibatalkan.<br><br>".$linkback;
				}
		
		$content=$script.
											"<table border='0' cellpadding='0' cellspacing='0' width='100%' height='100%'>
													<tr>
														<td align='left' valign='top' width='100%' height='100%'>
															<b class='title'>KONFIRMASI DATA &gt; BATAL UBAH PRODUK</b>
															<br><hr size='1' color='#000000'><br>
															".$msg."
														</td>
													</tr>
												</table>
												";
		}

include "webmenu.php";
include "common/include/template.php";
$temp=new template;
$temp->define_theme("themes/theme1.html");
$temp->define_tag("{PAGETITLE}", $namaaplikasi);
$temp->define_tag("{NAMAUSER}", strtoupper($_SESSION['SES_FULLNAME']));
$temp->define_tag("{APPMENU}", $webmenu);
$temp->define_tag("{APPCONTENT}", $content);
$temp->parse();
$temp->printproses();

closeDB();

?>