<?
	include "UctVw.php";
	/*session_start();
	if (!isset($_SESSION['username'])) {
		header("Location: login.php");
	}*/

	$form = $_REQUEST[form];
	$objAxpDB = new UctVw();
	
	switch($form)
	{
		case "supplier":
			$objAxpDB->ViewName("Daftar Supplier"); 
			$objAxpDB->ViewSQL("SELECT a.fnID, a.ftSupplier_Code, a.ftSupplier_Name, a.ftContact_Person, a.ftAddress, a.ftTelephone1, b.ftTrans_Status_Name FROM tlsupplier a LEFT JOIN tstransaction_status b ON a.fnStatus = b.fnTrans_Status_Code AND b.fnTrans_Group_Code = 9 ORDER BY a.ftSupplier_Code");
			$objAxpDB->ViewCaption("ID, Kode Supplier, Nama Supplier, Nama Kontak, Alamat, Telepon1, Status");
			
			$objAxpDB->FormName("Supplier"); 
			$objAxpDB->FormSQL("SELECT ftSupplier_Code, ftSupplier_Name, ftContact_Person, ftAddress, ftCity, ftZip_Code, ftState, ftCountry, ftShipment_Method, ftTerm_Of_Payment, ftTelephone1, ftTelephone2, ftFax, ftEmail, ftWebsite, ftNPWP_No, ftBank_Name, ftBank_Account_Owner, ftBank_Account_No, fnStatus FROM tlsupplier");
			$objAxpDB->FormCaption("Kode Supplier, Nama Supplier, Nama Kontak, Alamat, Kota, Kode Pos, Propinsi, Negara, Metode Pengiriman, Syarat Pembayaran, Telepon 1, Telepon 2, Fax, Email, Website, No. NPWP, Nama Bank, Pemilik Rekening, No. Rekening, Aktif");
			$objAxpDB->FormPosition("TL, TL, TL, TL, TL, TL, TL, TL, TL, TR, TR, TR, TR, TR, TR, TR, TR, TR, TR, TR");
			$objAxpDB->FormScript(0, "onKeyUp=\"this.value=this.value.toUpperCase();\"");

			$objAxpDB->Lookup("fnStatus", "SELECT fnTrans_Status_Code, ftTrans_Status_Name FROM tstransaction_status WHERE fnTrans_Group_Code = 9", "fnTrans_Status_Code", "ftTrans_Status_Name", 1, True);

			$objAxpDB->SetStatus("id, fnStatus");			
			if ($objAxpDB->GetStatus("id") == "")
				$objAxpDB->ButtonShow("baru, tutup");
			elseif ($objAxpDB->GetStatus("id") == 0)
				$objAxpDB->ButtonShow("reset, simpan, tutup");
			elseif ($objAxpDB->GetStatus("id") != 0) {
				$objAxpDB->ButtonShow("update, hapus, tutup");
				$objAxpDB->FormLocked("1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0");
			}
			$objAxpDB->Process();

			$objAxpDB->WriteHtmlTable();
			break;
		case "purchase_order":			
			// Deklarasi UctVw
			$objAxpDB->ViewName("Daftar Purchase Order");
			$objAxpDB->ViewSQL("SELECT a.fnID, DATE_FORMAT(a.fdTrans_Date, '%d %b %Y') AS fdTrans_Date, a.ftPO_No, a.ftSupplier_Code, b.ftSupplier_Name, a.ftStaff, e.ffQty_Outstanding, d.ftTrans_Status_Name AS ftShipment_Status, c.ftTrans_Status_Name AS ftStatus FROM txpurchase_order_hdr a LEFT JOIN tlsupplier b ON a.ftSupplier_Code = b.ftSupplier_Code LEFT JOIN tstransaction_status c ON a.fnStatus = c.fnTrans_Status_Code AND c.fnTrans_Group_Code = 2 LEFT JOIN tstransaction_status d ON a.fnShipment_Status = d.fnTrans_Status_Code AND d.fnTrans_Group_Code = 12 LEFT JOIN ( SELECT ftPO_No, SUM(ffQty)-SUM(ffQty_In) AS ffQty_Outstanding FROM txpurchase_order_det GROUP BY ftPO_No ) e ON a.ftPO_No = e.ftPO_No ORDER BY a.fdTrans_Date DESC");
			$objAxpDB->ViewCaption("ID, Tgl. PO, No. PO, Kode Supplier, Nama Supplier, Pegawai, Qty Sisa, Status Kirim, Status");
			
			// Deklarasi Header & Footer
			$objAxpDB->FormName("Purchase Order");
			$objAxpDB->FormSQL("SELECT fnCategory, ftPO_No, ftSupplier_Code, ftDescription, fdTrans_Date, ftStaff, fnShipment_Status, ftNotes, fnStatus FROM txpurchase_order_hdr");
			$objAxpDB->FormCaption("Kategori, No. PO, Supplier, Keterangan, Tgl. PO, Pegawai, Status Pengiriman, Catatan, Status");
			$objAxpDB->FormValue(3, $_SESSION['username']);
			$objAxpDB->FormPosition("TL, TL, TL, TL, TR, TR, TR, BL, BR");
			$objAxpDB->FormScript(0, "onChange=\"ajax_do('getdata.php?code=2&obj1=' + fdTrans_Date.value + '&obj2=ftPO_No&obj3=' + fnCategory.value);\"");
			$objAxpDB->FormScript(4, "onFocus=\"ajax_do('getdata.php?code=2&obj1=' + fdTrans_Date.value + '&obj2=ftPO_No&obj3=' + fnCategory.value);\"");
	
			// Deklarasi Detail
			$objAxpDB->GridSQL(0, "SELECT fnIndex, ftItem_Code, ffQty, ffQty_In FROM txpurchase_order_det");
			$objAxpDB->GridCaption(0, "No., Kode Barang, Kuantitas, Kuantitas Diterima");			

			$objAxpDB->Lookup("fnCategory", "SELECT fnTrans_Status_Code, ftTrans_Status_Name FROM tstransaction_status WHERE fnTrans_Group_Code = 15", "fnTrans_Status_Code", "ftTrans_Status_Name", -1, True);
			$objAxpDB->Lookup("ftSupplier_Code", "SELECT ftSupplier_Code, ftSupplier_Name FROM tlsupplier ORDER BY ftSupplier_Code", "ftSupplier_Code", "ftSupplier_Name", -1, False);
			$objAxpDB->Lookup("fnStatus", "SELECT fnTrans_Status_Code, ftTrans_Status_Name FROM tstransaction_status WHERE fnTrans_Group_Code = 2", "fnTrans_Status_Code", "ftTrans_Status_Name", 0, True);
			$objAxpDB->Lookup("fnShipment_Status", "SELECT fnTrans_Status_Code, ftTrans_Status_Name FROM tstransaction_status WHERE fnTrans_Group_Code = 12", "fnTrans_Status_Code", "ftTrans_Status_Name", 0, True);
			$objAxpDB->Lookup("ftItem_Code", "SELECT ftItem_Code, ftItem_Name FROM tlstock_hdr ORDER BY ftItem_Code", "ftItem_Code", "ftItem_Name", -1, False);
			$objAxpDB->ScriptGlobal("ajax_do('getdata.php?code=2&obj1=" . date("d/m/Y") . "&obj2=ftPO_No');");
			
			$objAxpDB->SetStatus("id, fnShipment_Status, fnStatus");
			if ($objAxpDB->GetStatus("id") == "") {
				$objAxpDB->ButtonShow("baru, tutup");
			} elseif ($objAxpDB->GetStatus("id") == 0) {
				$objAxpDB->ButtonShow("reset, simpan, tutup");
				$objAxpDB->FormLocked("0, 0, 0, 0, 0, 1, 1, 0, 1");
				$objAxpDB->GridLocked(0, "1, 0, 0, 1");

				$objAxpDB->GridAllowAddNew(0, TRUE);
				$objAxpDB->GridAllowDelete(0, TRUE);
			} elseif ($objAxpDB->GetStatus("fnStatus") == 1) {
				if($objAxpDB->GetStatus("fnShipment_Status") == 3)
					$objAxpDB->ButtonShow("cetak, batal, tutup"); 
				else
					$objAxpDB->ButtonShow("cetak, final, batal, tutup");

				$objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1");
				$objAxpDB->GridLocked(0, "1, 1, 1, 1");
			} elseif ($objAxpDB->GetStatus("fnStatus") == 9) {
				$objAxpDB->ButtonShow("tutup");
				$objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1");
				$objAxpDB->GridLocked(0, "1, 1, 1, 1");
			}
			
			$objAxpDB->Process();			
			$objAxpDB->WriteHtmlTable();
			break;
		case "faktur":			
			$objAxpDB->ViewName("Daftar Faktur");
			$objAxpDB->ViewSQL("SELECT a.fnID, a.ftPI_No, DATE_FORMAT(a.fdTrans_Date, '%d %b %Y') AS fdTrans_Date, DATE_FORMAT(a.fdDue_Date, '%d %b %Y') AS fdDue_Date, a.ftSupplier_Code, b.ftSupplier_Name, a.fcAmount, c.ftTrans_Status_Name AS ftPayment_Status, d.ftTrans_Status_Name AS ftStatus FROM txpurchase_invoice_hdr a LEFT JOIN tlsupplier b ON a.ftSupplier_Code = b.ftSupplier_Code LEFT JOIN tstransaction_status c ON a.fnPayment_Status = c.fnTrans_Status_Code AND c.fnTrans_Group_Code = 1 LEFT JOIN tstransaction_status d ON a.fnStatus = d.fnTrans_Status_Code AND d.fnTrans_Group_Code = 2 ORDER BY a.fdTrans_Date DESC");
			$objAxpDB->ViewCaption("ID, No. Faktur, Tgl. Faktur, Tgl. Jatuh Tempo, Kode Supplier, Nama Supplier, Jml. Total, Status Pembayaran, Status");

			$objAxpDB->FormName("Faktur");
			$objAxpDB->FormSQL("SELECT ftPI_No, fdTrans_Date, fdReceipt_Date, fdDue_Date, ftStock_In_No, fnPayment_Status, ftSupplier_Code, ftStaff, ftNotes, fcPayment_Amount, fcOutstanding_Amount, fcReturn_Amount, fcSub_Amount, fcDiscount, fcVAT, fcOther_Cost, fcAmount, fnStatus FROM txpurchase_invoice_hdr");	
			$objAxpDB->FormCaption("No. Faktur, Tgl. Faktur, Tgl. Penerimaan, Tgl. Jatuh Tempo, No. Stock Masuk, Status Pembayaran, Supplier, Pegawai, Catatan, Jumlah Pembayaran, Sisa Pembayaran, Jumlah Retur, Total Harga, Discount, Biaya Pajak, Biaya Lain-Lain, Total Pembelian, Status");
			$objAxpDB->FormPosition("TL, TL, TL, TL, TR, TR, TR, TR, BL, BL, BL, BL, BR, BR, BR, BR, BR, BR");
			$objAxpDB->FormScript(1, "onFocus=\"ajax_do('getdata.php?code=25&obj1=' + fdTrans_Date.value + '&obj2=ftPI_No');\"");
			$objAxpDB->FormScript(13, "onChange='RecalculateGrid_PI();'");
			$objAxpDB->FormScript(14, "onChange='RecalculateGrid_PI();'");
			$objAxpDB->FormScript(15, "onChange='RecalculateGrid_PI();'");
			
			$objAxpDB->GridSQL(0, "SELECT fnIndex, ftItem_Code, ffQty, fcPrice, fcAmount FROM txpurchase_invoice_det");
			$objAxpDB->GridCaption(0, "No, Kode Barang, Kuantitas (kg), Harga, Total Harga");
			$objAxpDB->GridScript(0, 3, "onChange='RecalculateGrid_PI();'");
			
			$objAxpDB->Lookup("ftSupplier_Code", "SELECT ftSupplier_Code, ftSupplier_Name FROM tlsupplier", "ftSupplier_Code", "ftSupplier_Name", 0, False);
			$objAxpDB->Lookup("fnStatus", "SELECT fnTrans_Status_Code, ftTrans_Status_Name FROM tstransaction_status WHERE fnTrans_Group_Code = 2", "fnTrans_Status_Code", "ftTrans_Status_Name", 1, True);
			$objAxpDB->Lookup("ftItem_Code", "SELECT ftItem_Code, ftItem_Name FROM tlstock_hdr ORDER BY ftItem_Code", "ftItem_Code", "ftItem_Name", 0, False);
			$objAxpDB->Lookup("fnPayment_Status", "SELECT fnTrans_Status_Code, ftTrans_Status_Name FROM tstransaction_status WHERE fnTrans_Group_Code = 1", "fnTrans_Status_Code", "ftTrans_Status_Name", 1, True);
			
			$objAxpDB->SetStatus("id, fnStatus");
			if ($objAxpDB->GetStatus("id") == "")
				$objAxpDB->ButtonShow("tutup");
			elseif ($objAxpDB->GetStatus("fnStatus") == 0) {
				$objAxpDB->ButtonShow("update, final, tutup");
				$objAxpDB->FormLocked("0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1");
				$objAxpDB->GridLocked(0, "1, 1, 1, 0, 1");
				$objAxpDB->GridButton(0, True);
			} elseif ($objAxpDB->GetStatus("fnStatus") == 1)  {
				$objAxpDB->ButtonShow("batal, tutup");		
				$objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");		
				$objAxpDB->GridLocked(0, "1, 1, 1, 1, 1");
			} elseif ($objAxpDB->GetStatus("fnStatus") == 9) {
				$objAxpDB->ButtonShow("tutup");
				$objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");		
				$objAxpDB->GridLocked(0, "1, 1, 1, 1, 1");
			}
			$objAxpDB->Process();

			$objAxpDB->WriteHtmlTable();
			break;
		case "faktur_gabungan":			
			$objAxpDB->ViewName("Daftar Faktur Gabungan"); 
			$objAxpDB->ViewSQL("SELECT a.fnID, a.ftPU_No, a.fdTrans_Date, a.fdDue_Date, a.ftSupplier_Code, b.ftSupplier_Name, a.fcAmount, c.ftTrans_Status_Name FROM txpurchase_union_hdr a LEFT JOIN tlsupplier b ON a.ftSupplier_Code = b.ftSupplier_Code LEFT JOIN tstransaction_status c ON a.fnStatus = c.fnTrans_Status_Code AND c.fnTrans_Group_Code = 2 ORDER BY a.fdTrans_Date DESC");
			$objAxpDB->ViewCaption("ID, No. Faktur, Tgl. Faktur, Tgl. Jatuh Tempo, Kode Supplier, Nama Supplier, Jml. Total, Status");

			$objAxpDB->FormName("Faktur Gabungan"); 
			$objAxpDB->FormSQL("SELECT ftPU_No, fdTrans_Date, fdReceipt_Date, fdDue_Date, fnPayment_Status, ftSupplier_Code, ftStaff, ftNotes, fcReturn_Amount, fcInvoice_Amount, fcSub_Amount, fcDiscount, fcVAT, fcOther_Cost, fcAmount, fnStatus FROM txpurchase_union_hdr");			
			$objAxpDB->FormCaption("No. Faktur, Tgl. Faktur, Tgl Penerimaan, Tgl. Jatuh Tempo, Status Pembayaran, Supplier, Pegawai, Catatan, Jumlah Retur, Total Invoice, Total Harga, Discount, Biaya Pajak, Biaya Lain-Lain, Total Pembelian, Status");
			$objAxpDB->FormPosition("TL, TL, TL, TL, TR, TR, TR, BL, BL, BL, BR, BR, BR, BR, BR, BR");
			
			$objAxpDB->FormScript(1, "onFocus=\"ajax_do('getdata.php?code=1000&obj1=' + this.value + '&obj2=ftPU_No');\"");
			$objAxpDB->FormScript(11, "onChange=\"SetAmount_PU();\"");
			$objAxpDB->FormScript(12, "onChange=\"SetAmount_PU();\"");
			$objAxpDB->FormScript(13, "onChange=\"SetAmount_PU();\"");

			$objAxpDB->FormScript(5, "onFocus='this.initialSelect = this.selectedIndex;' onChange='this.selectedIndex = this.initialSelect;'");
			$objAxpDB->FormHidden("<input type=\"hidden\" name=\"ftPI_Link\" id=\"ftPI_Link\"");

			$objAxpDB->GridSQL(0, "SELECT fnIndex, ftItem_Code, ffQty, ftUOM_Code, fcPrice FROM txpurchase_union_det");
			$objAxpDB->GridCaption(0, "No, Keterangan Barang, Kuantitas, Satuan, Harga");
			$objAxpDB->GridScript(0, 4, "onChange=\"RecalculateGrid_PU('fcPriceD0', 'fcInvoice_Amount');\"");

			$objAxpDB->Lookup("fnPayment_Status", "SELECT fnTrans_Status_Code, ftTrans_Status_Name FROM tstransaction_status WHERE fnTrans_Group_Code = 1", "fnTrans_Status_Code", "ftTrans_Status_Name", 1, True);
			$objAxpDB->Lookup("ftSupplier_Code", "SELECT ftSupplier_Code, ftSupplier_Name FROM tlsupplier", "ftSupplier_Code", "ftSupplier_Name", 0, False);
			$objAxpDB->Lookup("fnStatus", "SELECT fnTrans_Status_Code, ftTrans_Status_Name FROM tstransaction_status WHERE fnTrans_Group_Code = 2", "fnTrans_Status_Code", "ftTrans_Status_Name", 0, True);
			
			$objAxpDB->SetStatus("id, fnShipment_Status, fnStatus");
			if ($objAxpDB->GetStatus("id") == "")
				$objAxpDB->ButtonShow("baru, tutup");
			elseif ($objAxpDB->GetStatus("id") == 0) {
				$objAxpDB->ButtonShow("reset, simpan, faktur, tutup");
				$objAxpDB->FormLocked("0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1");
				$objAxpDB->GridLocked(0, "1, 0, 0, 0, 0");

				$objAxpDB->GridAllowAddNew(0, TRUE);
				$objAxpDB->GridAllowDelete(0, TRUE);
			} elseif ($objAxpDB->GetStatus("fnStatus") == 1) {
				$objAxpDB->ButtonShow("batal, faktur, tutup"); 
				$objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
				$objAxpDB->GridLocked(0, "1, 1, 1, 1, 1");
			} elseif ($objAxpDB->GetStatus("fnStatus") == 9) {
				$objAxpDB->ButtonShow("faktur, tutup");
				$objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
				$objAxpDB->GridLocked(0, "1, 1, 1, 1, 1");
			}
			$objAxpDB->Process();
			
			
			$objAxpDB->WriteHtmlTable();
			break;
		case "laporan_pembelian":
			$objAxpDB->ViewName("Laporan Pembelian"); 
			$objAxpDB->ViewSQL("SELECT fnID, ftReport_Code, ftReport_Name FROM tlreport WHERE ftReport_Group = 'Pembelian'");
			$objAxpDB->ViewCaption("ID, Kode Laporan, Nama Laporan");
			//$objAxpDB->ViewAction("");

			$objAxpDB->FormName("Laporan Pembelian"); 
			$objAxpDB->FormSQL("SELECT ftReport_Code, ftReport_Name, ftReport_SQL, ftReport_Group FROM tlreport");
			$objAxpDB->FormCaption("Kode Laporan, Nama Laporan, SQL Laporan, Group Laporan");
			$objAxpDB->FormPosition("TL, TL, TL, TL");
			
			$objAxpDB->SetStatus("id, fnStatus");
			if ($objAxpDB->GetStatus("id") == "")
				$objAxpDB->ButtonShow("tutup");
			elseif ($objAxpDB->GetStatus("id") != 0) {
				$objAxpDB->ButtonShow("tutup"); 
				$objAxpDB->FormLocked("1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0");
			}

			$objAxpDB->ReportPeriod(TRUE);
			$objAxpDB->ReportFilter(0, "No. Perkiraan", "ftCOA_Code", "SELECT ftCOA_Code, ftCOA_Name FROM TLCOA WHERE RIGHT(ftCOA_Code, 4) <> '0000' ORDER BY ftCOA_Code", TRUE);

			$objAxpDB->Process();		
			$objAxpDB->WriteHtmlTable();
			break;
		default:
			//$objAxpDB->headerData();
			$objAxpDB->menuData("Pembelian");
			//$objAxpDB->footerData();
			break;
	}