<?php
    Getsecurity("spk_covering");

    if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            case "transno":
              if(RetRstField("SELECT fnid FROM txspk_hdr WHERE ftspk_no='{$_REQUEST['fttrans_no']}'"))
              {
                  ?>
                      alert('No. SPK sudah dipakai... ');
                  <?php
              }
              else
              {
                  ?>
                      submitbutton('Simpan','','','','');
                  <?php
              }
              die("");
              break;
        }
    }

    $fntype = 1;
    
    $objAxpDB->ViewName("Daftar SPK Covering");
    $objAxpDB->ViewSQL("SELECT a.fnid, a.ftspk_no,a.fdtrans_date,
                                a.ftpor_no,a.ftnotes, c.fttrans_status_name AS ftstatus
                                FROM txspk_hdr a
                                LEFT JOIN tstransaction_status c ON a.fnstatus = c.fntrans_status_code AND c.fntrans_group_code = 2
                                WHERE a.fntype = {$fntype}");
    $objAxpDB->ViewCaption("ID, No. SPK, Tgl. Transaksi, No. POR, Keterangan, Status");

    $objAxpDB->FormName("SPK Covering");
    $objAxpDB->FormSQL("select  ftspk_no,ftpor_no,fdtrans_date,ftnotes,fnstatus,fntype
                        from txspk_hdr");
    $objAxpDB->FormCaption("No. SPK, No. POR,Tgl. SPK,Catatan, Status, Tipe SPK");
    $objAxpDB->FormPosition("TL,TL,TR,BL,BR,HD");
    $objAxpDB->ViewPeriod(TRUE);

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

    $objAxpDB->FormScript(1, "onChange=\"{$x}\"");
    $objAxpDB->FormScript(2, "onChange=\"{$ajaxGrid}\"");

    $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("ftpor_no","SELECT ftpor_no,fdtrans_date FROM txproduction_order_request_hdr WHERE fnstatus = '1'", "ftpor_no", "fdtrans_date", -1,false);
    $objAxpDB->Lookup("ftitemdetail_code", "SELECT ftitem_code, ftitem_name FROM vBomDetail", "ftitem_code", "ftitem_name", -1,false);
    $objAxpDB->Lookup("ftmachine_code", "SELECT ftmachine_code, ftmachine_name FROM tlmachine", "ftmachine_code", "ftmachine_name", -1, false);

    // Deklarasi Detail
    $objAxpDB->GridSQL(0, "SELECT fnindex,ftmachine_code,ftitemdetail_code,ftitem_name,fnqty,ftuomdetail_code,fnqty_reject,fdstart_date,fdend_date, ftdescription FROM txspk_det");
    $objAxpDB->GridCaption(0,"No.,Mesin ,Barang, Nama Barang, Qty, Satuan, Qty Waste, Jadwal Mulai Produksi, Jadwal Selesai Produksi,  Keterangan " );
    $objAxpDB->GridHidden(0, "0,0, 0, 1,0,0,0,0,0,0");
    $objAxpDB->GridAutoAdd(0, False);

    $objAxpDB->SetStatus("id, fnstatus");

    if(!$_REQUEST['id']) {
            $strAutoNumber1="  ajax_do('query2.php?code=2&ftmodule_code=produksi01&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftspk_no&no_trans=ftspk_no&table_name=txspk_hdr&proses={$proses}&fntype={$fntype}&ftbranch_code={$_SESSION['strbranch']}');";
            $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                                           {$strAutoNumber1}
                                     }
                                     OnChangeDate_fdtrans_date();
                                     document.getElementById(\"fntype\").value = {$fntype};
                                ";
            $objAxpDB->FormJS($strAutoNumber0);
    } 

    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,1");
            $objAxpDB->ButtonShow("reset, simpan, tutup");
            $objAxpDB->GridLocked(0, "1,0,1,1,0,1,0,0,0,0,0,1");
            $objAxpDB->GridAllowDelete(0, true);

    } elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
            $objAxpDB->FormLocked("1,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,1");
            $objAxpDB->ButtonShow("tutup");
            $objAxpDB->GridLocked(0, "1,1,1,1,1,1,1,1,1,1");

    } elseif ($objAxpDB->GetStatus("id") == 1) {
            $objAxpDB->FormLocked("1,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");
    }

    if($_REQUEST['ajax']) {
           switch($_REQUEST['code']) {
           case "grid":
               ob_start();

               $objAxpDB->GridSQL(0, "SELECT a.fnindex,c.ftmachine_code, a.ftitemdetail_code AS ftitemdetail_code,a.ftitemdetail_name AS ftitem_name
                                        ,(a.fnqty_estimasi-fnqty_spk) AS fnqty, a.ftuom_code AS ftuomdetail_code, 0 as fnqty_reject
                                        ,b.fdstart_date,b.fdend_date,b.ftdescription
                                        FROM txestimation_det a
                                        LEFT JOIN txspk_det b ON 0=1
                                        LEFT JOIN tlmachine c ON b.ftmachine_code = c.ftmachine_code
                                        WHERE a.ftestimation_no='{$_REQUEST['ftestimation_no']}'
                                        AND a.fnqty_estimasi<>a.fnqty_spk AND a.fnqty_estimasi > a.fnqty_spk
                                        #");

               $objAxpDB->GridLocked(0, "1,0,1,1,1,1,0,0,0,0");
               $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();
               <?php
               break;
           }
           die("");
           exit;
    }

    $objAxpDB->Process();
    $objAxpDB->WriteHtmlTable();

?>
