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

                      alert('No. transaksi sudah dipakai...\n System akan mengenerate No. transaksi kembali ...\n Silahkan ulangi proses penyimpanan...  ');
                      OnChangeDate_fdtrans_date();
                <?php
                  }
                  else {
                        ?>


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

                <?php
                  }
                   die("");
                   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);

                                   } ";

                        }
                              ?>
                       document.getElementById('<?php echo $_REQUEST['objQty']; ?>').value=0;
                    <?php
                        break;

            }
       }
       
    $objAxpDB->ViewName("Daftar Surat Jalan Return (LPB Biasa)");
    $objAxpDB->ViewTable("vINSuratJalanReturn_LPB_Biasa");
    $objAxpDB->ViewSQL("SELECT a.fnid,a.fttrans_no,a.fdtrans_date,
                                a.ftpo_no,a.ftsupplier_code,
                                b.ftsupplier_name,c.fttrans_status_name AS ftstatus
                        FROM txdelivery_order_return_hdr_bahan_baku a
                        inner join txstock_in_hdr_bahan_baku d on a.ftstock_in_no=d.ftstock_in_no  and d.fnpo_type3=0   
                        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              
                        where  a.fntype=0
                        ORDER BY a.fdtrans_date DESC, a.fttrans_no desc");
    
    $objAxpDB->ViewCaption("ID, No. Return, Tgl. Return, No. PO, Kode Supplier, Nama Supplier,  Status");
    $objAxpDB->ViewPeriod(TRUE);
	
    // Deklarasi Header & Footer
    $objAxpDB->FormName("Surat Jalan Return");
    $objAxpDB->FormSQL("SELECT fttrans_no,fntype_reff, fntypesj,ftstock_in_return_no, ftstock_in_no, ftpo_no, ftsupplier_code,ftsj_no_supplier,fdtrans_date, ftnotes, fftotal_qty, fnstatus FROM txdelivery_order_return_hdr_bahan_baku");
    $objAxpDB->FormCaption("No. Return , Tipe Reff, Tipe SJ, Stock Masuk Return, No. LPB, No. PO, Supplier, No. SJ Supplier, Tgl. Return, Catatan, Total Qty,Status");
    $objAxpDB->FormPosition("TL,TL,TL,TL,TL,TL,TL,TL,TR,BL,BR,BR");
    
    
    $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code,fnqty_in,fnqty_outstanding_in,fnqty_available, fnqty, ftuom_code,ftwarehouse_code,ftlocation_code,ftdescription FROM txdelivery_order_return_det_bahan_baku");

    $objAxpDB->GridCaption(0, "No., Kode Barang, Qty In, Qty Sisa In, Qty Avail, Qty, Satuan, Gudang, Lokasi, Keterangan");
    $objAxpDB->GridAutoAdd(0, False);
     if($_REQUEST['id']=='0') {
       $m=" AND fnstatus=1 ";
    }
    $objAxpDB->Lookup("ftstock_in_no", "SELECT ftstock_in_no, fdtrans_date FROM txstock_in_hdr_bahan_baku WHERE fnpo_type3=0", "ftstock_in_no", "fdtrans_date", -1, False);
    $objAxpDB->Lookup("ftstock_in_return_no", "SELECT ftstock_in_return_no, fdtrans_date FROM txstock_in_return_hdr_bahan_baku WHERE fnpo_type3=0", "ftstock_in_return_no", "fdtrans_date", -1, False);
    $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",1, True);
    $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr_bahan_baku ORDER BY ftitem_code", "ftitem_code", "ftitem_name", -1, False);
    $objAxpDB->Lookup( "ftwarehouse_code", "SELECT ftwarehouse_code, ftwarehouse_name FROM tlwarehouse_bahan_baku", "ftwarehouse_code", "ftwarehouse_name", 0,true);
    $objAxpDB->Lookup( "ftlocation_code", "SELECT ftlocation_code, ftlocation_name FROM tllocation_bahan_baku", "ftlocation_code", "ftlocation_name", 0,true);
    $objAxpDB->Lookup("fntypesj", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 38", "fntrans_status_code", "fttrans_status_name",0, True);
    $objAxpDB->Lookup("fntype_reff", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 41", "fntrans_status_code", "fttrans_status_name",0, True);
    
    $ajaxGrid="ajax_do_grid0('inventory_bahan_baku.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=grid&fntype_reff='+ document.getElementById('fntype_reff').value +'&ftstock_in_return_no='+ document.getElementById('ftstock_in_return_no').value + '&ftstock_in_no='+ document.getElementById('ftstock_in_no').value);";
   

    $x="document.getElementById('ftstock_in_return_no').value='';";
    $x.="document.getElementById('hidden_ftstock_in_return_no').value='';";
    $x.="document.getElementById('span_ftstock_in_return_no').innerHTML='';";
    $x.="document.getElementById('ftstock_in_no').value='';";
    $x.="document.getElementById('hidden_ftstock_in_no').value='';";
    $x.="document.getElementById('span_ftstock_in_no').innerHTML='';";

    $objAxpDB->FormScript(1, "onChange=\"{$x}{$ajaxGrid}\"");
    $objAxpDB->FormScript(3, "onChange=\"{$ajaxGrid}\"");
    $objAxpDB->FormScript(4, "onChange=\"{$ajaxGrid}\"");
    
    $ajaxItem="ajax_do('inventory_bahan_baku.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=item&ftitem_code=' + this.value + '&index=_N_');";
    $objAxpDB->GridScript(0,1," onChange=\"{$ajaxItem}\"");
    $objAxpDB->GridScript(0,5," onChange=\"SuratJalanReturnRecalQty()\"");
    $ajaxGudang="OnChange=\"ajax_do('inventory_bahan_baku.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=gudang&ftwarehouse_code='+this.value+'&ObjLokasi=ftlocation_codeD0_N_&objQty=fnqty_availableD0_N_');\"";
    $objAxpDB->GridScript(0,7, $ajaxGudang);
   

       if(!$_REQUEST['id']) {
            $strAutoNumber1="ajax_do('query2.php?fnpo_type3=0&tipe=0&code=2&ftmodule_code=tn117&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=fttrans_no&no_trans=fttrans_no&table_name=txdelivery_order_return_hdr_bahan_baku');";
          
            $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,0,1,1,1,0,1,1");
            $objAxpDB->GridLocked(0, "1,1,1,1,1,0,1,0,0,0");
            $objAxpDB->ButtonShow("reset, simpan, tutup");
    } elseif (($objAxpDB->GetStatus("fnstatus") == 1)||($objAxpDB->GetStatus("fnstatus") == 2)) {
           if(isset($_REQUEST['revisi'])) {
                    $objAxpDB->ButtonShow("simpan_revisi,tutup");
                    $objAxpDB->FormLocked("1,1,1,1,1,1,1,1,1,0,1,1");
                    $objAxpDB->GridLocked(0, "1,1,1,1,1,0,1,0,0,0");
                }
                else {
                    $objAxpDB->ButtonShow("cetak, revisi, batal,tutup");
            $objAxpDB->FormLocked("1,1,1,1,1,1,1,1,1,1,1,1");
            $objAxpDB->GridLocked(0, "1,1,1,1,1,1,1,1,1,1");
                }
    } elseif ($objAxpDB->GetStatus("fnstatus") == 3) {
          $objAxpDB->FormLocked("1,1,1,1,1,1,1,1,1,1,1,1,1");
          $objAxpDB->GridLocked(0, "1,1,1,1,1,1,1,1,1,1,1");
          $objAxpDB->ButtonShow("tutup");
        } elseif ($objAxpDB->GetStatus("fnstatus") == 9) {
          $objAxpDB->FormLocked("1,1,1,1,1,1,1,1,1,1,1,1,1");
          $objAxpDB->GridLocked(0, "1,1,1,1,1,1,1,1,1,1,1");
          $objAxpDB->ButtonShow("tutup");
    }

     if($_REQUEST['ajax']) {
           switch($_REQUEST['code']) {
           case "grid":
               $objAxpDB->lengt_object=false;
               $_REQUEST['id']=0.5;
                       ob_start();
                       if($_REQUEST['fntype_reff']==1) {
                           $ssql="SELECT b.fnindex,b.ftitem_code,b.fnqty AS fnqty_in,
                                        b.fnqty-IFNULL(b.fnqty_return,0) AS fnqty_outstanding_in ,
                                        SUM(c.fnqty_on_hand) AS fnqty_available, b.fnqty-b.fnqty AS fnqty, b.ftuom_code,
                                        b.ftwarehouse_code,b.ftlocation_code,'' as ftdescription
                                    FROM
                                        txstock_in_det_bahan_baku b
                                        LEFT JOIN tlstock_det_bahan_baku c ON b.ftitem_code=c.ftitem_code
                                    WHERE   b.ftstock_in_no='".$_REQUEST['ftstock_in_no']."'
                                    GROUP BY b.ftstock_in_no,b.ftitem_code,b.fnindex
                                    order by    b.fnindex
                                                #";
                       }
                       else {
                             $ssql="SELECT b.fnindex,b.ftitem_code,b.fnqty AS fnqty_in,
                                    b.fnqty-IFNULL(b.fnqty_return,0) AS fnqty_outstanding_in ,
                                    SUM(c.fnqty_on_hand) AS fnqty_available, b.fnqty-b.fnqty AS fnqty, b.ftuom_code,
                                    b.ftwarehouse_code,b.ftlocation_code,'' as ftdescription
                                FROM
                                    txstock_in_return_det_bahan_baku b
                                    LEFT JOIN tlstock_det_setengah_jadi c ON b.ftitem_code=c.ftitem_code
                                WHERE   b.ftstock_in_return_no='".$_REQUEST['ftstock_in_return_no']."'
                                GROUP BY b.ftstock_in_return_no,b.ftitem_code,b.fnindex
                                order by    b.fnindex
                                            #";

                       }
                       
                        $objAxpDB->GridSQL(0,$ssql);

               $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
                 if($_REQUEST['fntype_reff']==1) {
                          $ftsupplier_code=RetRstField("SELECT ftstock_in_company FROM txstock_in_hdr_bahan_baku  WHERE ftstock_in_no='{$_REQUEST['ftstock_in_no']}'");
                          $ftsupplier_name=RetRstField("SELECT ftsupplier_name FROM tlsupplier  WHERE ftsupplier_code='{$ftsupplier_code}'");
                          $ftpo_no=RetRstField("SELECT ftdocument_no FROM txstock_in_hdr_bahan_baku  WHERE ftstock_in_no='{$_REQUEST['ftstock_in_no']}'");
                          $ftsj_no_supplier=RetRstField("SELECT ftreff_no FROM txstock_in_hdr_bahan_baku  WHERE ftstock_in_no='{$_REQUEST['ftstock_in_no']}'");
                     ?>
                        document.getElementById('ftsupplier_code').value='<?php echo $ftsupplier_code; ?>';
                        document.getElementById('hidden_ftsupplier_code').value='<?php echo $ftsupplier_name; ?>';
                        document.getElementById('span_ftsupplier_code').innerHTML='<?php echo $ftsupplier_name; ?>';
                        document.getElementById('ftpo_no').value='<?php echo $ftpo_no; ?>';
                        document.getElementById('ftsj_no_supplier').value='<?php echo $ftsj_no_supplier; ?>';
                        document.getElementById('ftstock_in_return_no').value='';
                        document.getElementById('hidden_ftstock_in_return_no').value='';
                        document.getElementById('span_ftstock_in_return_no').innerHTML='';
                   <?
                 }
                 else {
                      $ftstock_in_no=RetRstField("SELECT ftstock_in_no FROM txstock_in_return_hdr_bahan_baku WHERE ftstock_in_return_no='{$_REQUEST['ftstock_in_return_no']}'");
                      $ftstock_in_date=RetRstField("SELECT fdtrans_date FROM txstock_in_hdr_bahan_baku WHERE ftstock_in_no='{$ftstock_in_no}'");
                      
                      $ftsupplier_code=RetRstField("SELECT ftstock_in_company FROM txstock_in_hdr_bahan_baku  WHERE ftstock_in_no='{$ftstock_in_no}'");
                      $ftsupplier_name=RetRstField("SELECT ftsupplier_name FROM tlsupplier  WHERE ftsupplier_code='{$ftsupplier_code}'");
                      $ftpo_no=RetRstField("SELECT ftdocument_no FROM txstock_in_hdr_bahan_baku  WHERE ftstock_in_no='{$ftstock_in_no}'");
                      $ftsj_no_supplier=RetRstField("SELECT ftreff_no FROM txstock_in_return_hdr_bahan_baku  WHERE ftstock_in_return_no='{$_REQUEST['ftstock_in_return_no']}'");

                 ?>
                    
                    document.getElementById('ftstock_in_no').value='<?php echo $ftstock_in_no; ?>';
                    document.getElementById('hidden_ftstock_in_no').value='<?php echo $ftstock_in_date; ?>';
                    document.getElementById('span_ftstock_in_no').innerHTML='<?php echo $ftstock_in_date; ?>';
                  
                    document.getElementById('ftsupplier_code').value='<?php echo $ftsupplier_code; ?>';
                    document.getElementById('hidden_ftsupplier_code').value='<?php echo $ftsupplier_name; ?>';
                    document.getElementById('span_ftsupplier_code').innerHTML='<?php echo $ftsupplier_name; ?>';
                    document.getElementById('ftpo_no').value='<?php echo $ftpo_no; ?>';
                    document.getElementById('ftsj_no_supplier').value='<?php echo $ftsj_no_supplier; ?>';
                    <?
                 }
               break;

           }
           die("");
           exit;
    }
    

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