<?php
     if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            case "transno":
              if(RetRstField("SELECT fnid FROM txspb_hdr WHERE ftspb_no='{$_REQUEST['fttrans_no']}'")) {
                  ?>

                  alert('No. SPB sudah dipakai... ');
            <?php
              }
              else {
                    ?>


                  submitbutton('Simpan','','','','');

            <?php
              }
            break;

            case "gudang":
                $ssql="SELECT ftlocation_code,ftlocation_name FROM tllocation_bahan_baku WHERE ftwarehouse_code='{$_REQUEST['ftwarehouse_code']}'";
                $rs=mysql_query($ssql);
                   ?>

                        var ObjSelect=document.getElementById('<?php echo $_REQUEST['ObjLokasi']; ?>');

                            var len = ObjSelect.length;

                            for (var idx=0; idx < len; idx++)
                            {
                                    if (ObjSelect.options[ObjSelect.length-1].value!='') {
                                          ObjSelect.remove(ObjSelect.length-1);
                                    }
                            }


                    <?php
                         $i=0;
                        while($r=mysql_fetch_assoc($rs)) {
                              $i++;
                               echo " try {
                                     ObjSelect.add(new Option('{$r['ftlocation_code']}--{$r['ftlocation_name']}','{$r['ftlocation_code']}'),null,null);
                                  } catch(ex) {
                                        ObjSelect.add(new Option('{$r['ftlocation_code']}--{$r['ftlocation_name']}','{$r['ftlocation_code']}'),$i,$i);

                                   } ";

                        }
                        die("");
                        break;
          die("");
        }

    }

    $objAxpDB->ViewName("Daftar SPB");
    $objAxpDB->ViewSQL("SELECT a.fnid, a.ftspb_no,a.fdtrans_date,
                                a.ftnotes, c.fttrans_status_name AS ftstatus
                                FROM txspb_hdr a
                                LEFT JOIN tstransaction_status c ON a.fnstatus = c.fntrans_status_code AND c.fntrans_group_code = 2");
    $objAxpDB->ViewCaption("ID, No. SPB, Tgl. Transaksi,Keterangan,Status");
    $objAxpDB->ViewPeriod(TRUE);
    
    $objAxpDB->FormName("SPB");
    $objAxpDB->FormSQL("select  ftspb_no,ftpor_no,ftestimation_no,fdtrans_date,ftnotes,fnstatus from txspb_hdr");
    $objAxpDB->FormCaption("No. SPB, No. POR , No. Estimasi,Tgl. SPB,Catatan,Status");
    $objAxpDB->FormPosition(" TL, TL,TL,TR,BL,BR");

    $ajaxGrid="ajax_do_grid0('produksi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=grid&ftestimation_no='+ document.getElementById('ftestimation_no').value);";

    $Clear="document.getElementById('ftestimation_no').value='';";
    $Clear.="document.getElementById('hidden_ftestimation_no').value='';";
    $Clear.="document.getElementById('span_ftestimation_no').innerHTML='';";
    
    $objAxpDB->FormScript(1, "onChange=\"{$Clear}\"");
    $objAxpDB->FormScript(2, "onChange=\"{$ajaxGrid}\"");

    $objAxpDB->Lookup("ftpor_no","SELECT ftpor_no,fdtrans_date FROM txproduction_order_request_hdr WHERE fnstatus = '1'", "ftpor_no", "fdtrans_date", -1,false);
    $objAxpDB->Lookup("ftestimation_no", "SELECT ftestimation_no, fdtrans_date,ftpor_no FROM txestimation_hdr where fnstatus <> 9 ","ftestimation_no", "fdtrans_date", -1, false,"' AND ftpor_no=\''+ document.getElementById('ftpor_no').value + '\''");
    $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM vBomDetail", "ftitem_code", "ftitem_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', 1,true);
    $objAxpDB->Lookup("ftwarehouse_code", "SELECT ftwarehouse_code, ftwarehouse_name FROM tlwarehouse_bahan_baku", "ftwarehouse_code", "ftwarehouse_name", -1,true);
    $objAxpDB->Lookup("ftlocation_code", "SELECT ftlocation_code, ftlocation_name FROM tllocation_bahan_baku", "ftlocation_code", "ftlocation_name", -1,true);

    // Deklarasi Detail
    $objAxpDB->GridSQL(0, "SELECT fnindex,ftitemheader_code, ftitem_code,ftitem_name,ftuom_code,fnqty_estimasi,fnqty_outstanding,fnqty,ftwarehouse_code,ftlocation_code, ftdescription FROM txspb_det");
    $objAxpDB->GridCaption(0,"No., HeaderNo, Barang,Nama Barang, Satuan, Qty SPK, Qty Outstanding, Qty, Gudang, Lokasi, Keterangan " );
    $objAxpDB->GridHidden(0, "0,1, 0, 1, 0,0,0,0,0,0");
    $objAxpDB->GridAutoAdd(0, False);
    
    $ajaxGudang="OnChange=\"ajax_do('produksi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=gudang&ftwarehouse_code='+this.value+'&ObjLokasi=ftlocation_codeD0_N_');\"";
    $objAxpDB->GridScript(0,8,"{$ajaxGudang}");

    if(!$_REQUEST['id']) {
            $strAutoNumber1="  ajax_do('query2.php?code=2&ftmodule_code=produksi04&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftspb_no&no_trans=ftspb_no&table_name=txspb_hdr');";

            $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                                           {$strAutoNumber1}
                                     }
                                     OnChangeDate_fdtrans_date();
                                ";

            $objAxpDB->FormJS($strAutoNumber0);
    }
	 
    $objAxpDB->SetStatus("id, fnstatus");
    if ($objAxpDB->GetStatus("id") == "")
            $objAxpDB->ButtonShow("baru, cetak_daftar, export_excel ,  tutup");
    elseif ($objAxpDB->GetStatus("id") == 0) {
            $objAxpDB->FormLocked("0,0,0,0,0,1");
            $objAxpDB->ButtonShow("reset, simpan, tutup");
            $objAxpDB->GridLocked(0, "1,1,1,1,1,1,1,0,0,0,0");
            $objAxpDB->GridAllowDelete(0, TRUE);
			//$objAxpDB->GridAllowAddNew(0, TRUE);
    } elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
            $objAxpDB->FormLocked("1,1,1,1,1,1,1");
            $objAxpDB->ButtonShow("cetak, batal, tutup");
            $objAxpDB->GridLocked(0, "1,1,1,1,1,1,1,1,1,1,1");

    } elseif ($objAxpDB->GetStatus("fnstatus") == 9) {
            $objAxpDB->FormLocked("1,1,1,1,1,1,1");
            $objAxpDB->ButtonShow("tutup");
            $objAxpDB->GridLocked(0, "1,1,1,1,1,1,1,1,1,1,1");

    } elseif ($objAxpDB->GetStatus("id") == 1) {
            $objAxpDB->FormLocked("1,1,1,1,1,1,1");
            $objAxpDB->ButtonShow("cetak, batal, tutup");
            $objAxpDB->GridLocked(0, "1,1,1,1,1,1,1,1,1,1,1");
    }

    if($_REQUEST['ajax']) {
           switch($_REQUEST['code']) {
           case "grid":
               ob_start();
                       $objAxpDB->GridSQL(0, "SELECT a.fnindex,a.ftitem_code AS ftitemheader_code, a.ftitemdetail_code AS ftitem_code, a.ftitemdetail_name as ftitem_name, a.ftuom_code,
                                                     a.fnqty_spk,(a.fnqty_estimasi-fnqty_spk) AS fnqty_outstanding,(a.fnqty_estimasi-fnqty_spk) AS fnqty,b.ftwarehouse_code,b.ftlocation_code,b.ftdescription
                                              FROM txestimation_det a
						     LEFT JOIN txspb_det b ON 0=1
					      WHERE a.ftestimation_no='{$_REQUEST['ftestimation_no']}'
                                                    #");
                                              //AND a.fnqty_spk<>a.fnqty_avail AND a.fnqty_spk > a.fnqty_avail  
               $objAxpDB->Process();
               $objAxpDB->setGrid2(0);
               $html = ob_get_contents();
               ob_end_clean();
               ?>
                    div = document.getElementById('txtHint0');
                    var m='<?php echo js_esc($html); ?>';
                    m= m.replace(/^\s*|\s*$/g,"");

                    div.innerHTML =m;
                    RowIndex0();
                    reSize();
               <?
               break;
           }
           die("");
           exit;
    }
	
    $objAxpDB->Process();
    $objAxpDB->WriteHtmlTable();
?>
