<?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
{ $idpesanan=isset($_GET['idp']) ? cleanval($_GET['idp']) : NULL;
		$nomorpesanan=isset($_GET['oid']) ? base64_decode(cleanval($_GET['oid'])) : NULL;
		$namalengkap=isset($_POST['namalengkapf']) ? cleanval($_POST['namalengkapf']) : NULL;
		$noktp=isset($_POST['noktpf']) ? cleanval($_POST['noktpf']) : NULL;
		$fotoktp=isset($_FILES['fotoktpf']) ? $_FILES['fotoktpf'] : NULL;
		$nokk=isset($_POST['nokkf']) ? cleanval($_POST['nokkf']) : NULL;
		$fotokk=isset($_FILES['fotokkf']) ? $_FILES['fotokkf'] : NULL;
		$nonpwp=isset($_POST['nonpwpf']) ? cleanval($_POST['nonpwpf']) : NULL;
		$fotonpwp=isset($_FILES['fotonpwpf']) ? $_FILES['fotonpwpf'] : NULL;
		$script="";
		
		// cek id dan nomor pesanan
		$qord="SELECT *
									FROM $db_pesanan
									WHERE idpesanan='".$idpesanan."'
															AND nomorpesanan='".$nomorpesanan."'
															AND statusorder<4
									";
		$rord=mysqli_query($dbConnection, $qord);
		if (mysqli_num_rows($rord)>0)
		{ $dord=mysqli_fetch_array($rord);
				$qup="UPDATE $db_pfakturapproval
										SET tolaktgl='".$wktsekarangdb."',
														tolakoleh='OTOMATIS',
														tolaksebab='BATAL UBAH'
										WHERE nomorpesanan='".$dord['nomorpesanan']."'
																AND sesipesanan='".$dord['sesipesanan']."'
																AND tolaktgl IS NULL
																AND setujutgl IS NULL
										";
				$rup=mysqli_query($dbConnection, $qup);
				if ($rup)
				{ $qfa="SELECT *
												FROM $db_pfakturapproval
												WHERE nomorpesanan='".$dord['nomorpesanan']."'
																		AND sesipesanan='".$dord['sesipesanan']."'
												ORDER BY idpfakturapproval DESC
												LIMIT 1
												";
						$rfa=mysqli_query($dbConnection, $qfa);
						if (mysqli_num_rows($rfa)>0)
						{ $dfa=mysqli_fetch_array($rfa);
								if (empty($namalengkap))
								{ $namalengkap=$dfa['namalengkap'];
										}
								if (empty($noktp))
								{ $noktp=$dfa['noktp'];
										}
								if (empty($nokk))
								{ $nokk=$dfa['nokk'];
										}
								if (empty($nonpwp))
								{ $nonpwp=$dfa['nonpwp'];
										}
								$ktpfilename=$dfa['fotoktp'];
								$kkfilename=$dfa['fotokk'];
								$npwpfilename=$dfa['fotonpwp'];
								}
						else
						{ $ktpfilename="";
								$kkfilename="";
								$npwpfilename="";
								}
						
						// simpan file ktp
						$simpanktp=1;
						if (!empty($fotoktp['name']))
						{ $ktpextension=end(explode(".", $fotoktp['name']));
								$ktpfilename=strtoupper($nomorpesanan).date("YmdHis")."_ktp.".$ktpextension;
								$filepath="images/faktur/dokumen/".$ktpfilename;
								if (!move_uploaded_file($fotoktp['tmp_name'], $filepath))
								{ $simpanktp=NULL;
										}
								}
						
						// simpan file kk
						$simpankk=1;
						if (!empty($fotokk['name']))
						{ $kkextension=end(explode(".", $fotokk['name']));
								$kkfilename=strtoupper($nomorpesanan).date("YmdHis")."_kk.".$kkextension;
								$filepath="images/faktur/dokumen/".$kkfilename;
								if (!move_uploaded_file($fotokk['tmp_name'], $filepath))
								{ $simpankk=NULL;
										}
								}
						
						// simpan file npwp
						$simpannpwp=1;
						if (!empty($fotonpwp['name']))
						{ $npwpextension=end(explode(".", $fotonpwp['name']));
								$npwpfilename=strtoupper($nomorpesanan).date("YmdHis")."_npwp.".$npwpextension;
								$filepath="images/faktur/dokumen/".$npwpfilename;
								if (!move_uploaded_file($fotonpwp['tmp_name'], $filepath))
								{ $simpannpwp=NULL;
										}
								}

						if ($simpanktp==1 && $simpankk==1 && $simpannpwp==1)
						{ $qidp="INSERT INTO $db_pfakturapproval (idrekanan,
																																																idtoko,
																																																sesipesanan,
																																																nomorpesanan,
																																																namalengkap,
																																																noktp,
																																																fotoktp,
																																																nokk,
																																																fotokk,
																																																nonpwp,
																																																fotonpwp,
																																																mintatgl,
																																																mintaoleh)
																					VALUES ('".$dord['idrekanan']."',
																													'".$dord['idtoko']."',
																													'".$dord['sesipesanan']."',
																													'".$dord['nomorpesanan']."',
																													'".$namalengkap."',
																													'".$noktp."',
																													'".$ktpfilename."',
																													'".$nokk."',
																													'".$kkfilename."',
																													'".$nonpwp."',
																													'".$npwpfilename."',
																													'".$wktsekarangdb."',
																													'".$_SESSION['SES_USERNAME']."')
																					";
								$ridp=mysqli_query($dbConnection, $qidp);
								if ($ridp)
								{ $catatan="Mengubah data faktur.";
										$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 menyimpan data faktur.";
										$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 menyimpan data faktur.<br><br>".$linkback;
										// $msg.="<br><br>".$qidp.mysqli_error($dbConnection);
										}
								}
						else
						{ $msg="Gagal menyimpan file KTP / KK / NPWP.<br><br>".$linkback;
								}							
						}
				else
				{ $msg="Gagal membatalkan data faktur sebelumnya.<br><br>".$linkback;
						// $msg.="<br><br>".$qup.mysqli_error($dbConnection);
						}
				}
		else
		{ $msg="Data pesanan tidak ditemukan.<br><br>
										Biasanya terjadi karena proses konfirmasi data sudah terlewati atau pesanan sudah 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; UBAH DATA FAKTUR</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();

?>