<?php
# Baca Variabel Form
$message        = array();
$Act            = isset($_REQUEST['Act']) ? $_REQUEST['Act'] : '';
$Act_Det        = isset($_REQUEST['Act_Det']) ? $_REQUEST['Act_Det'] : '';
$kv_id          = isset($_REQUEST['kv_id']) ? $_REQUEST['kv_id'] : '';
$kv_date        = isset($_REQUEST['kv_date']) ? $_REQUEST['kv_date'] : '';
$kv_prodid      = isset($_REQUEST['kv_prodid']) ? $_REQUEST['kv_prodid'] : '';
$kv_prodcode    = isset($_REQUEST['kv_prodcode']) ? $_REQUEST['kv_prodcode'] : '';
$kv_prodname    = isset($_REQUEST['kv_prodname']) ? $_REQUEST['kv_prodname'] : '';
$kv_prodsat     = isset($_REQUEST['kv_prodsat']) ? $_REQUEST['kv_prodsat'] : '';
$kv_qty         = isset($_REQUEST['kv_qty']) ? $_REQUEST['kv_qty'] : '';
$kv_pic         = isset($_REQUEST['kv_pic']) ? $_REQUEST['kv_pic'] : '';
$kv_desc        = isset($_REQUEST['kv_desc']) ? $_REQUEST['kv_desc'] : '';
$id_det         = isset($_REQUEST['id_det']) ? $_REQUEST['id_det'] : '';
$id_deth        = isset($_REQUEST['id_deth']) ? $_REQUEST['id_deth'] : '';
$kv_grade       = isset($_REQUEST['kv_grade']) ? $_REQUEST['kv_grade'] : '';
$kv_status      = isset($_REQUEST['kv_status']) ? $_REQUEST['kv_status'] : '';
if (empty($kv_id)) {
    $kv_no  = get_kvno('KONVERSI');
} else {
    $kv_no  = isset($_REQUEST['kv_no']) ? $_REQUEST['kv_no'] : '';
}
$kvd_prodid = isset($_REQUEST['prodid']) ? $_REQUEST['prodid'] : '';
$prodstock  = isset($_REQUEST['prodstock']) ? $_REQUEST['prodstock'] : '';
$kvd_qty    = isset($_REQUEST['kvd_qty']) ? $_REQUEST['kvd_qty'] : '';
$val_date     = get_validationdate($kv_date);

if ($Act == "SAVE") {
    if ($val_date == "NO") {
        echo "<font color='red'>Periode :$start_date s/d $end_date belum closing</font><br>";
        #reload
        require_once "add_konversi_barang_jadi.php";
        exit;
    } else {
        if (count($message) == 0) {
            if (empty($kv_id)) {
                $qrySave = mysql_query("INSERT INTO konversi_barangjadi SET kv_no='$kv_no', kv_date='$kv_date', kv_prodid='$kv_prodid', kv_prodsat='$kv_prodsat', kv_qty='$kv_qty', kv_grade='$kv_grade', kv_desc='$kv_desc', kv_pic='$kv_pic',	
						 kv_inputby='$user_id',kv_inputdate='$datetime', kv_status=10") or die("Gagal query" . mysql_error());
                if ($qrySave) {
                    $data = mysql_fetch_object(mysql_query("SELECT MAX(id) id FROM konversi_barangjadi"));
                    $kv_id = $data->id;
                    $qd = mysql_query("UPDATE konversi_barangjadi_detail SET kvd_status=20, kvd_hid='$kv_id' WHERE kvd_inputby='$user_id' AND kvd_status='10'");
                    $message[] = "Data Sukses tersimpan !";
                    $status = "OK";
                }
                #Insert History User
                $itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id',user_act='Menambahkan proses konversi no : $kv_no', user_date='$date', user_time='$time'");
            } else {
                $qrySave = mysql_query("UPDATE konversi_barangjadi SET kv_date='$kv_date', kv_prodid='$kv_prodid', kv_prodsat='$kv_prodsat', kv_grade='$kv_grade', kv_qty='$kv_qty', kv_desc='$kv_desc', kv_pic='$kv_pic', kv_status='$kv_status' WHERE id='$kv_id'")
                    or die("Gagal query" . mysql_error());
                if ($qrySave) {
                    $qd = mysql_query("UPDATE konversi_barangjadi_detail SET kvd_status= 20 WHERE kvd_inputby='$user_id' AND kvd_hid='$kv_id'");
                    $message[] = "Data Sukses tersimpan !";
                    $status = "OK";
                }
                #Insert History User
                $itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Mengubah proses konversi no : $kv_no', user_date='$date', user_time='$time'");
            }
        }
    }
}
if ($Act == "DETAIL") {
    $qd = mysql_query("SELECT a.*,b.prod_code,b.prod_name,b.prod_sat
					   FROM konversi_barangjadi a
					   INNER JOIN product b ON b.id=a.kv_prodid
					   WHERE a.kv_status <> 100 AND a.id='$kv_id'");
    $dd = mysql_fetch_object($qd);
    $kv_id           = $dd->id;
    $kv_no           = $dd->kv_no;
    $kv_date         = $dd->kv_date;
    $kv_prodid       = $dd->kv_prodid;
    $kv_prodcode     = $dd->prod_code;
    $kv_prodname     = $dd->prod_name;
    $kv_prodsat      = $dd->prod_sat;
    $kv_grade        = $dd->kv_grade;
    $kv_qty          = $dd->kv_qty;
    $kv_grade        = $dd->kv_grade;
    $kv_pic          = $dd->kv_pic;
    $kv_desc         = $dd->kv_desc;
    $kv_status       = $dd->kv_status;
}
if ($Act == "DELETE") {
    $qd = mysql_query("UPDATE konversi_barangjadi SET kv_status=100 WHERE id='$kv_id'");
    if ($qd) {
        #Insert History User
        $itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menghapus proses konversi no : $kv_no', 
		         user_date='$date', user_time='$time'");
        $message[] = "Data Sukses terhapus !";
        $status = "OK";
    }
}
if ($Act == "POSTING") {
    #Update Status
    $qd = mysql_query("UPDATE konversi_barangjadi SET kv_status=20 WHERE id='$kv_id'");
    #Insert History User
    $itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Posting proses konversi no. $kv_no', user_date='$date', user_time='$time'");
    $message[] = "Data Sukses diposting !";
    $status = "OK";
}
if (($Act_Det == "ADD_DET")) {
    if ($val_date == "NO") {
        echo "<font color='red'>Periode :$start_date s/d $end_date belum closing</font><br>";
        #reload
        require_once "add_konversi_barang_jadi.php";
        exit;
    } else {
        if (empty($kv_id)) {
            mysql_query("INSERT INTO konversi_barangjadi_detail SET kvd_hid = 0, kvd_prodid='$kvd_prodid', kvd_qty='$kvd_qty', kvd_inputby='$user_id', kvd_status='10'");
        } else {
            mysql_query("INSERT INTO konversi_barangjadi_detail SET kvd_hid='$kv_id', kvd_prodid='$kvd_prodid', kvd_qty='$kvd_qty', kvd_inputby='$user_id', kvd_status='10'");
        }
    }
}

if (($Act_Det == "DELETE_DET")) {
    mysql_query("DELETE FROM konversi_barangjadi_detail WHERE id='$id_deth'");
}
if ($Act == "ADD" || $Act == "DETAIL" || $Act_Det == "ADD_DET" || $Act_Det == "DELETE_DET") {
    require_once "add_konversi_barang_jadi.php";
}
if ($Act == "CLOSE") {
    echo "<meta http-equiv='refresh' content='0; url=?page=Data-Konversi-BrgJadi'>";
}
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-Konversi-BrgJadi'>";
        exit;
    }
}
?>