<?php
    Getsecurity('hasil_produksi');

    if($_REQUEST['ajax']) {
        switch($_REQUEST['code'])
        {
            case "transno":
                if(RetRstField("SELECT fnid FROM txproduction_hdr WHERE ftproduction_no='{$_REQUEST['fttrans_no']}'")) {
                ?>
                    alert('No. Produksi sudah dipakai... ');
                <?php
                }
                else
                {
                ?>
                    submitbutton('Simpan','','','','');
                <?php
                }
                break;
                die("");
            case "item":
                $ssql="SELECT ftuomdetail_code FROM txspk_det WHERE ftspk_no='{$_REQUEST['ftspk_no']}' AND ftitemdetail_code='{$_REQUEST['ftitem_code']}'";
                $ftuom_code=RetRstField($ssql);
                ?>
                    document.getElementById('ftuom_codeD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ftuom_code ; ?>';
                <?php

                $ssql="select (fnqty-fnqty_produksi) as fnqty_production from txspk_det
                        where ftspk_no='{$_REQUEST['ftspk_no']}' AND ftitemdetail_code='{$_REQUEST['ftitem_code']}'";
                $fnqty_avail=RetRstField($ssql);
                if(($fnqty_avail==""))
                {
                    $fnqty_avail="0";
                }
                ?>  
                    document.getElementById('fnqty_spkD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $fnqty_avail; ?>';
                    document.getElementById('fnqtyD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $fnqty_avail; ?>';
                <?php
                die("");
                break;
            case "gudang":
                $ssql="SELECT ftlocation_code,ftlocation_name FROM tllocation 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;
        }
    }
    $objAxpDB->ViewName("Daftar Hasil Produksi");
    $objAxpDB->ViewSQL("SELECT a.fnid, a.ftproduction_no,a.fdtrans_date,a.ftdepartment_code,
                                c.fttrans_status_name AS ftstatus, a.ftnotes
                                FROM txproduction_hdr a
                                LEFT JOIN tstransaction_status c ON a.fnstatus = c.fntrans_status_code AND c.fntrans_group_code = 2
                                ORDER BY ftproduction_no DESC ");
    $objAxpDB->ViewCaption("ID, No. Produksi, Tgl. Transaksi, Kode Departement, Status, Keterangan");

    $objAxpDB->FormName("Hasil Produksi");
    $objAxpDB->FormSQL("select  ftproduction_no, ftdepartment_code, ftshift_code, fdtrans_date, ftnotes, fnstatus
                        from txproduction_hdr");
    $objAxpDB->FormCaption("No. Produksi, Departement, Shift, Tgl. Produksi, Catatan, Status");
    $objAxpDB->FormPosition(" TL, TL, TL, TR, BL, BR");
    
     // Deklarasi Detail
    $objAxpDB->GridSQL(0, "SELECT fnindex, ftmachine_code, ftoperator_code, ftspk_no, ftitem_code,fnqty_spk,fnqty,ftwarehouse_code,ftlocation_code,ftuom_code,ftdescription FROM txproduction_det");
    $objAxpDB->GridCaption(0,"No., No. Mesin, Kode Operator, No. Spk,  Barang, Qty Spk, Qty, Gudang, Lokasi, Satuan, Keterangan " );

    $objAxpDB->Lookup("ftdepartment_code", "SELECT ftdepartment_code, ftdepartment_name FROM tldepartment_ppic", "ftdepartment_code", "ftdepartment_name", -1,true);
    $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("ftoperator_code", "select ftoperator_code, ftemployee_name from tlemployee WHERE IFNULL(ftoperator_code,'')<>'' ", "ftoperator_code", "ftemployee_name", -1, False);
    $objAxpDB->Lookup("ftshift_code", "SELECT distinct ftshift_code, ftshift_name FROM tlshift order by ftshift_code", "ftshift_code", "ftshift_name", -1, True);
    $objAxpDB->Lookup("ftwarehouse_code", "SELECT ftwarehouse_code, ftwarehouse_name FROM tlwarehouse where fnwarehouse_type in ('0','1','2') ", "ftwarehouse_code", "ftwarehouse_name", -1,true);
    $objAxpDB->Lookup("ftlocation_code", "SELECT ftlocation_code, ftlocation_name FROM tllocation a inner join tlwarehouse b on a.ftwarehouse_code = b.ftwarehouse_code and fnwarehouse_type in ('0','1','2') ", "ftlocation_code", "ftlocation_name", -1,true);
    //$objAxpDB->Lookup("ftspk_no", "SELECT ftspk_no, fdtrans_date FROM txspk_hdr where fnstatus = 1 order by fdtrans_date ", "ftspk_no", "fdtrans_date", -1,FALSE,"' AND ftdepartment_code=\''+ document.getElementById('ftdepartment_code').value + '\''");
    $objAxpDB->Lookup("ftspk_no", "SELECT ftspk_no, fdtrans_date FROM txspk_hdr where fnstatus = 1 ", "ftspk_no", "fdtrans_date", -1,FALSE,"' AND ftdepartment_code=\''+ document.getElementById('ftdepartment_code').value + '\''");
    //$objAxpDB->Lookup("ftitem_code", "SELECT a.ftitemdetail_code as ftitem_code, a.ftitem_name FROM txspk_det a INNER JOIN txspk_hdr b ON a.ftspk_no = b.ftspk_no WHERE b.fnstatus <> 9 ", "ftitem_code", "ftitem_name",0,false,"' AND a.ftspk_no=\''+ document.getElementById('ftspk_noD0_N_').value + '\''");
    $objAxpDB->Lookup("ftmachine_code", "select ftmachine_code, ftmachine_name from tlmachine ", "ftmachine_code", "ftmachine_name", -1, True);
    $objAxpDB->Lookup("fntype", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 46", "fntrans_status_code", "fttrans_status_name",0, True);

    $ajaxfnqty_avail="OnChange=\"ajax_do('produksi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=item&index=_N_&ftspk_no='+  document.getElementById('ftspk_noD0_N_').value +'&ftitem_code='+  document.getElementById('ftitem_codeD0_N_').value +'');\"";
    $objAxpDB->GridScript(0,4,"{$ajaxfnqty_avail}");

    $Clear="document.getElementById('ftitem_codeD0_N_').value='';";
    $Clear.="document.getElementById('hidden_ftitem_codeD0_N_').value='';";
    $Clear.="document.getElementById('span_ftitem_codeD0_N_').innerHTML='';";
    $Clear.="document.getElementById('fnqty_spkD0_N_').value='0';";
    $Clear.="document.getElementById('fnqtyD0_N_').value='0';";
    $Clear.="document.getElementById('ftuom_codeD0_N_').value='';";
    $objAxpDB->GridScript(0,3 , "onChange=\"{$Clear}\"");

    $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,7,"{$ajaxGudang}");

    $ajaxGrid1="ajax_do_grid0('produksi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=grid&ftdepartment_code='+ document.getElementById('ftdepartment_code').value);";
    $objAxpDB->FormScript(1, "onChange=\"{$ajaxGrid1}\"");
    
    $objAxpDB->SetStatus("id, fnstatus");
    if(!$_REQUEST['id']) {
        $strAutoNumber1="ajax_do('query2.php?code=2&ftmodule_code=produksi05&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftproduction_no&no_trans=ftproduction_no&table_name=txproduction_hdr&ftbranch_code={$_SESSION['strbranch']}');";
        $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                                       {$strAutoNumber1}
                                 }
                                 OnChangeDate_fdtrans_date();
                            ";
        $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");

            $objAxpDB->ButtonShow("reset, simpan, tutup");
            $objAxpDB->GridLocked(0, "1, 0, 0, 0,0,1, 0, 0, 0,1,0");
            $objAxpDB->GridAllowAddNew(0, TRUE);
            $objAxpDB->GridAllowDelete(0, TRUE);
            $objAxpDB->Lookup("ftitem_code", " SELECT a.ftitemdetail_code, a.ftitem_name, a.ftspk_no FROM txspk_det a inner join  txspk_hdr b  on a.ftspk_no = b.ftspk_no WHERE b.fnstatus = 1 and a.fnqty > a.fnqty_produksi  ", "ftitemdetail_code", "ftitem_name", -1, false,"' AND a.ftspk_no=\''+ document.getElementById('ftspk_noD0_N_').value + '\''");
    }
    elseif ($objAxpDB->GetStatus("fnstatus") == 1)
    {
            $objAxpDB->FormLocked("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");
            $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM vBomDetail", "ftitem_code", "ftitem_name", -1,false);
   } 
   elseif ($objAxpDB->GetStatus("fnstatus") == 9)
   {
            $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1");
            $objAxpDB->ButtonShow("tutup");
            $objAxpDB->GridLocked(0, "1, 1, 1, 1,1,1, 1, 1, 1,1,1");
            $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM vBomDetail", "ftitem_code", "ftitem_name", -1,false);
   }

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

                $objAxpDB->GridSQL(0, "SELECT fnindex, ftmachine_code, ftoperator_code, ftspk_no, ftitem_code,fnqty_spk,fnqty,ftwarehouse_code,ftlocation_code,ftuom_code,ftdescription FROM txproduction_det");


                $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;
                    RowBuild0();

                 <?php
                break;
           }
           die("");
           exit;
    }

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