<?php
      if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            case "transno":
              if(RetRstField("SELECT fnid FROM txpengembalian_barang_hdr WHERE ftfpb_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
                     die("");
                        break;

                  case "oum":
                    
                  if($_REQUEST['fnitem_type']==1) {
                        $ssql="SELECT UCASE(ftuom_code) AS ftuom_code FROM tlstock_hdr_setengah_jadi WHERE ftitem_code='{$_REQUEST['ftitem_code']}'";
                        $ftuom_code=RetRstField($ssql);
                        #die($ftuom_code);
                          ?>
                           
                              document.getElementById('ftuom_codeD0<?php echo $_REQUEST['index'];?>').value='<?php echo $ftuom_code; ?>';
                         <?
                  }
                  else {
                        $ssql="SELECT UCASE(ftuom_code) AS ftuom_code FROM tlstock_hdr_bahan_baku WHERE ftitem_code='{$_REQUEST['ftitem_code']}'";
                        $ftuom_code=RetRstField($ssql);
                        # die($ftuom_code);
                          ?>
                           
                              document.getElementById('ftuom_codeD0<?php echo $_REQUEST['index'];?>').value='<?php echo $ftuom_code; ?>';
                         <?
                  }

                  ?>

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

                            var len = ObjSelect.length;

                            for (var idx=0; idx < len; idx++)
                            {
                                    if (ObjSelect.options[idx].value=='<?php echo $_SESSION['ftwarehouse_code']; ?>') {
                                        ObjSelect.selectedIndex=idx;
                                        break;
                                    }
                            }


                   //
                  <?php
                    $ssql="SELECT ftlocation_code,ftlocation_name FROM tllocation_bahan_baku WHERE ftwarehouse_code='{$_SESSION['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);

                                   } ";

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

                            var len = ObjSelect.length;

                            for (var idx=0; idx < len; idx++)
                            {
                                    if (ObjSelect.options[idx].value=='<?php echo $_SESSION['ftlocation_code']; ?>') {
                                        ObjSelect.selectedIndex=idx;
                                        break;
                                    }
                            }
                            var str1='query2.php?code=4&ftwarehouse_code=<?php echo $_SESSION['ftwarehouse_code']; ?>&ftlocation_code=<?php echo $_SESSION['ftlocation_code']; ?>&ftitem_code=<?php echo $_REQUEST['ftitem_code']; ?>&objQty=<?php echo $_REQUEST['objQty']; ?>';
                            ajax_do(str1);
                           // alert(str1);

                    <?php
                     die("");
            break;
             case "avail":
                 if($_REQUEST['fnitem_type']==1) {
                        $ssql="SELECT ifnull(sum(fnqty_available),0) as fnqty_available
                            FROM tlstock_det_setengah_jadi
                            WHERE ftwarehouse_code='{$_REQUEST['ftwarehouse_code']}' AND
                            ftlocation_code='{$_REQUEST['ftlocation_code']}' and
                            ftitem_code='{$_REQUEST['ftitem_code']}'
                            ";
                            $fnqty_available=RetRstField($ssql);
////                            die($ssql);
                            ?>
                            document.getElementById('fnqty_availableD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $fnqty_available; ?>';

                        <?php
                 }
                 else {
                        $ssql="SELECT ifnull(sum(fnqty_available),0) as fnqty_available
                            FROM tlstock_det_bahan_baku
                            WHERE ftwarehouse_code='{$_REQUEST['ftwarehouse_code']}' AND
                            ftlocation_code='{$_REQUEST['ftlocation_code']}' and
                            ftitem_code='{$_REQUEST['ftitem_code']}'
                            ";
//                         die($ssql);
                            $fnqty_available=RetRstField($ssql);
                            ?>
                            document.getElementById('fnqty_availableD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $fnqty_available; ?>';

                        <?php
                 }

                     die("");
                        break;

        }

    }

    Getsecurity(27);

    $objAxpDB->ViewName("Daftar Pengembalian Barang");
    $objAxpDB->ViewTable("vINPengembalianBarang");
    $objAxpDB->ViewSQL("SELECT A.fnid, A.ftfpb_no,fdtrans_date,  a.ftpos_no , A.ftspk_no,
                         b.fttrans_status_name
                       FROM txpengembalian_barang_hdr
                       A INNER JOIN tstransaction_status B ON a.fnstatus=b.fntrans_status_code AND B.fntrans_group_code=2
                       ORDER BY  A.ftfpb_no DESC ,A.fdtrans_date DESC");

    $objAxpDB->ViewCaption("ID, No. FPB, Tgl. transaksi, No. POS, No. SPK, Status ");
	$objAxpDB->ViewPeriod(TRUE);
     if(!$_REQUEST['id']) {
          $strAutoNumber1="ajax_do('query2.php?code=2&ftmodule_code=tn107&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftfpb_no&no_trans=ftfpb_no&table_name=txpengembalian_barang_hdr');";
          $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                                           {$strAutoNumber1}

                            }
                            OnChangeDate_fdtrans_date();
                                ";

             $objAxpDB->FormJS($strAutoNumber0);
             $ajaxAddress="ajax_do('query2.php?code=611&ftsupplier_code=' + document.getElementById('ftsupplier_code').value + '&objAddress=ftship_address');";


     }
    $objAxpDB->FormName("Pengembalian Barang");
    $objAxpDB->FormSQL("SELECT ftfpb_no, fnitem_type, ftpos_no, ftspk_no,
                        fdtrans_date,
                        ftnotes,
                        fftotal_qty, fnstatus FROM txpengembalian_barang_hdr");
    $objAxpDB->FormCaption("No. FPB, Tipe Barang,   No. POS, No. SPK,
                            Tgl. Transaksi,
                            Catatan,
                            Total Qty, Status");
    $objAxpDB->FormPosition("TL, TL, TL, TL,  
                            TR,
                            BL,
                            BR, BR");

    $ajaxGrid="ajax_do_grid0('inventory_bahan_baku.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=grid&fnitem_type='+ document.getElementById('fnitem_type').value);";
    $objAxpDB->FormScript(1, "onChange=\"{$ajaxGrid}\"");

    $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code, ftwarehouse_code, ftlocation_code, fnqty_available, fnqty,ftdescription, ftuom_code FROM txpengembalian_barang_det");
    $objAxpDB->GridCaption(0, "No.,  Barang, Gudang, Lokasi, Qty Avail, Qty, Alasan Pengembalian,Satuan");
    $objAxpDB->GridHidden(0, "0, 0, 0, 0, 1, 0, 0, 0,0,0");
    $ajaxGudang="OnChange=\"ajax_do('inventory_bahan_baku.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=gudang&index=_N_&fnitem_type='+ document.getElementById('fnitem_type').value +'&ftwarehouse_code='+this.value+'&ObjLokasi=ftlocation_codeD0_N_&objQty=fnqty_availableD0_N_');\"";
    $ajaxOUM_Code="OnChange=\"ajax_do('inventory_bahan_baku.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=oum&index=_N_&fnitem_type='+ document.getElementById('fnitem_type').value +'&ftitem_code='+this.value+'&ftuom_code=ftuom_codeD0_N_&ftstock_category_code=ftstock_category_codeD0_N_&ObjGudang=ftwarehouse_codeD0_N_&ObjLokasi=ftlocation_codeD0_N_&objQty=fnqty_availableD0_N_');\"";
    $ajaxQtyAvail="OnChange=\"ajax_do('inventory_bahan_baku.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=avail&index=_N_&fnitem_type='+ document.getElementById('fnitem_type').value +'&index=_N_&ftitem_code='+ document.getElementById('ftitem_codeD0_N_').value+'&ftlocation_code='+ document.getElementById('ftlocation_codeD0_N_').value+'&ftwarehouse_code='+ document.getElementById('ftwarehouse_codeD0_N_').value );\"";

    $objAxpDB->GridScript(0,1, $ajaxOUM_Code);
    $objAxpDB->GridScript(0,2, $ajaxGudang);
    $objAxpDB->GridScript(0,3, $ajaxQtyAvail);

    $objAxpDB->GridScript(0,5,"onChange='DeliveryOrderRecalQty();'");
    $objAxpDB->GridDelJS(0,"DeliveryOrderRecalQty();");

    $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);
    $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_setengah_jadi", "ftitem_code", "ftitem_name", -1,false);
    $objAxpDB->Lookup("fnitem_type", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_status_code<>0 AND fntrans_group_code = 39", "fntrans_status_code", "fttrans_status_name", 0,true);
   
    
    $objAxpDB->SetStatus("id, fnstatus");
    if ($objAxpDB->GetStatus("id") == "")
            $objAxpDB->ButtonShow("baru, cetak_daftar, export_excel , tutup");
    elseif ($objAxpDB->GetStatus("id") == 0) {
            $objAxpDB->GridAllowAddNew(0,true);
            $objAxpDB->GridAllowDelete(0,true);
              $objAxpDB->FormLocked("0, 0, 0, 0,
                            0,
                            0,
                            1, 1");
            $objAxpDB->GridLocked(0, "1,  0, 0, 0, 1, 0, 0,1");
            $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, 0, 0, 0,
                            0,
                            0,
                            1, 1");

                   $objAxpDB->GridLocked(0, "1,  0, 0, 0, 1, 0, 0,1");
                    $objAxpDB->GridAllowAddNew(0,true);
                    $objAxpDB->GridAllowDelete(0,true);
                }
                else {
                    $objAxpDB->ButtonShow("cetak, revisi, batal,tutup");
                    $objAxpDB->FormLocked("1, 1, 1, 1,
                            1,
                            1,
                            1, 1");
                    $objAxpDB->GridLocked(0, "1,  1, 1, 1, 1, 1, 1,1");
                }
   } elseif ($objAxpDB->GetStatus("fnstatus") == 3) {
            $objAxpDB->FormLocked("1, 1, 1, 1, 
                            1,
                            1,
                            1, 1");
            $objAxpDB->GridLocked(0, "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");
            $objAxpDB->GridLocked(0, "1,  1, 1, 1, 1, 1, 1,1");
            $objAxpDB->ButtonShow("tutup");
    }


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

                         if($_REQUEST['fnitem_type']=="1") {
                                 $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr_setengah_jadi $m", "ftitem_code", "ftitem_name", -1,false);
                                # $objAxpDB->Lookup("ftitem_code1", "SELECT a.ftitem_code, a.ftitem_name FROM tlstock_hdr_setengah_jadi a", "ftitem_code", "ftitem_name", -1,false,"' AND EXISTS(SELECT mm.fnid FROM txpurchase_order_det MM WHERE M.ftitem_code=MM.ftitem_code AND MM.ftpo_no=\''+ document.getElementById('ftpo_no').value + '\')'");

                         }
                         else {
                                $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr_bahan_baku  $m", "ftitem_code", "ftitem_name", -1,false);
                                #$objAxpDB->Lookup("ftitem_code1", "SELECT a.ftitem_code, a.ftitem_name FROM tlstock_hdr_bahan_baku a", "ftitem_code", "ftitem_name", -1,false,"' AND EXISTS(SELECT mm.fnid FROM txpurchase_order_det MM WHERE M.ftitem_code=MM.ftitem_code AND MM.ftpo_no=\''+ document.getElementById('ftpo_no').value + '\')'");

                         }


               $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();
                    RowIndex0();
                   DeliveryOrderRecalQty();
               <?

               break;

           }
           die("");
           exit;
    }
    if(isset($_REQUEST['id'])&&($_REQUEST['id'])!=0) {
            $ssql="SELECT fnitem_type
                         FROM txpengembalian_barang_hdr
                            WHERE fnid='{$_REQUEST['id']}'";

           $fnitem_type=RetRstField($ssql);
           if($fnitem_type=="1") {
                $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr_setengah_jadi $m", "ftitem_code", "ftitem_name", -1,false);
                #$objAxpDB->Lookup("ftitem_code1", "SELECT a.ftitem_code, a.ftitem_name FROM tlstock_hdr_setengah_jadi a", "ftitem_code", "ftitem_name", -1,false,"' AND EXISTS(SELECT mm.fnid FROM txpurchase_order_det MM WHERE M.ftitem_code=MM.ftitem_code AND MM.ftpo_no=\''+ document.getElementById('ftpo_no').value + '\')'");
           }
           else {
                $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr_bahan_baku  $m", "ftitem_code", "ftitem_name", -1,false);
                #$objAxpDB->Lookup("ftitem_code1", "SELECT a.ftitem_code, a.ftitem_name FROM tlstock_hdr_bahan_baku a", "ftitem_code", "ftitem_name", -1,false,"' AND EXISTS(SELECT mm.fnid FROM txpurchase_order_det MM WHERE M.ftitem_code=MM.ftitem_code AND MM.ftpo_no=\''+ document.getElementById('ftpo_no').value + '\')'");
           }
    }
    
    $objAxpDB->Process();

    $objAxpDB->WriteHtmlTable();



?>

