<?php
	
	
	include "koneksi.php";
	$id=mysqli_real_escape_string($conn,$_GET["id"]);
    echo "ini".$id;

$s="SELECT * FROM data_purchasing_requisition WHERE id='". $_GET["id"] ."'";
$res=mysqli_query($conn,$s);
$dataUser=mysqli_fetch_assoc($res);
$quantity = $dataUser["quantity"];
echo "qty :".$quantity."<br>";
	
    $po_date = date('d / m / yy');
	$hargaQty=$_POST["harga"];
	$harga = $quantity * $hargaQty;
	$suplier=$_POST["suplier"];
	$dpo = $_POST["dpo"];
	$no_po = $_POST["no_po"];
	$quote = $_POST["quote"];
	$discont = $_POST["discont"];
	$ppnya = $_POST["ppnya"];
	$accode = $_POST["accode"];
	
						
	echo "suplier".$suplier."<br>";
	echo "harga".$harga."<br>";
	echo "discont".$discont."<br>";
	echo "ppn ya".$ppnya."<br>";
	echo "dpo".$dpo."<br>";
	echo "harga".number_format($harga, 0, ".", ".")."<br>";
	echo "acc code".$accode."<br>";
	
	
	
	
	
	if ($discont != '' OR $ppnya == 1) //terisi salah satu
	{
		echo "salah satu"."<br>";
		if ($ppnya == 1 AND $discont == 0) //ppn 
	    {
		   $ppn = 10 / 100;
		   $ppnHarga = $ppn * $harga;
		   $ppnTotal = $harga + $ppnHarga;
		   echo "Total Potong PPN = ".number_format($ppnTotal, 0, ".", ".");
		   
		//jadikan_satu =  1 belum dihapus, 2 sudah dihapus
        $q = "UPDATE data_purchasing_requisition 
		      SET harga='$hargaQty',id_suplier='$suplier',po_date='$po_date',
		      jadikan_satu='1',status_proses='1',delivery_po_date='$dpo',no_po='$no_po',quote='$quote',
			  harga_total_pcs='$harga',harga_ppn='$ppnTotal',harga_akhir='$ppnTotal',gabung='0',kode_acc='$accode' 
			  WHERE id=".$id;
	    $result = mysqli_query($conn, $q);

	    if($result)
	    {
		   echo "berhasillllll";
		   echo "<br>".$q;
		   header("location:prDiterimaPrc.php?p=1");
	    }
	    else
	    {
		   echo "gagal";
		   header("location:prDiterimaPrc.php?p=3");
	    }
		   
	    }
		if ($discont != '' AND $ppnya == NULL) //discont 
	    { 
		   echo "2 diskon tanpa ppn"."<br>";
		   $hitungDiscont = $discont / 100;
           $hitungHarga = $hitungDiscont * $harga;
           $hitungTotal = $harga - $hitungHarga;
		   
           echo "Total Setelah Discont = ".number_format($hitungTotal, 0, ".", ".");	

        //jadikan_satu =  1 belum dihapus, 2 sudah dihapus
        $q = "UPDATE data_purchasing_requisition 
		      SET harga='$hargaQty',id_suplier='$suplier',po_date='$po_date',
		      jadikan_satu='1',status_proses='1',delivery_po_date='$dpo',no_po='$no_po',quote='$quote',
			  harga_total_pcs='$harga',harga_discont='$hitungTotal',harga_akhir='$hitungTotal',besar_discont='$discont',kode_acc='$accode'
			  WHERE id=".$id;
	    $result = mysqli_query($conn, $q);
	    if($result)
	    {
		   echo "berhasil DISCONT";
		   header("location:prDiterimaPrc.php?p=1");
	    }
	    else
	    {
		   echo "gagal";
		   header("location:prDiterimaPrc.php?p=3");
	    }           
		   
	    }
        if ($discont != '' AND $ppnya == 1) //discont dan ppn
	    {
		   echo "3 diskon dan ppn"."<br>";
		   $ppn = 10 / 100;
		   $ppnHarga = $ppn * $harga;
		   $ppnTotal = $harga + $ppnHarga;
		 
		   $hitungDiscont = $discont / 100;
           $hitungHarga = $hitungDiscont * $harga;
           $hitungTotal = $harga - $hitungHarga;
		   
		   $hitungTotalPpn = $ppn * $hitungTotal;
		   $discontPpn = $hitungTotal + $hitungTotalPpn;
		   
		   echo "diskon :" .number_format($hitungHarga, 0, ".", ".")."<br>";
		   echo "Total Potong PPN = ".number_format($hitungTotalPpn, 0, ".", ".")."<br>";
		   echo "Total Setelah Discont = ".number_format($hitungTotal, 0, ".", ".")."<br>";
           echo "Total Discont dan PPN = ".number_format($discontPpn, 0, ".", ".");		

        //query
        //jadikan_satu =  1 belum dihapus, 2 sudah dihapus
        $q = "UPDATE data_purchasing_requisition 
		      SET harga='$hargaQty',id_suplier='$suplier',po_date='$po_date',
		      jadikan_satu='1',status_proses='1',delivery_po_date='$dpo',no_po='$no_po',quote='$quote',
			  harga_total_pcs='$harga',harga_discont='$hitungTotal',harga_ppn='$hitungTotalPpn',harga_akhir='$discontPpn',besar_discont='$discont',kode_acc='$accode' 
			  WHERE id=".$id;
	    $result = mysqli_query($conn, $q);

	    if($result)
	    {
		   echo "berhasil";
		   header("location:prDiterimaPrc.php?p=1");
	    }
	    else
	    {
		   echo "gagal";
		   ("location:prDiterimaPrc.php?p=3");
	    }		   
	    }
	}
	else
	{
		echo "4 tidak diskon dan ppn"."<br>"; //tidak terisi semua
		
		//jadikan_satu =  1 belum dihapus, 2 sudah dihapus
        $q = "UPDATE data_purchasing_requisition 
		      SET harga='$hargaQty',id_suplier='$suplier',po_date='$po_date',
		      jadikan_satu='1',status_proses='1',delivery_po_date='$dpo',no_po='$no_po',quote='$quote',
			  harga_total_pcs='$harga',harga_akhir='$harga',kode_acc='$accode' 
			  WHERE id=".$id;
	    $result = mysqli_query($conn, $q);

	    if($result)
	    {
		   echo "berhasil tidak diskon dan ppn";
		   header("location:prDiterimaPrc.php?p=1");
	    }
	    else
	    {
		   echo "gagal";
		   header("location:prDiterimaPrc.php?p=3");
	    }
	}
?>