<?php

 
  
      if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            case "transno":
              if(RetRstField("SELECT fnid FROM txstock_in_return_hdr_bahan_baku WHERE ftstock_in_return_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 "tgl":
                $ssql="select date_add('".CDate($_REQUEST['tgl'])."',INTERVAL 1 MONTH)";
                $date=RetRstField($ssql);
                ?>

                     document.getElementById('fddue_date').value='<?php echo CDateRev($date); ?>';
                <?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":
                  $ssql="SELECT ftuom_code,ftstock_category_code FROM tlstock_hdr_setengah_jadi WHERE ftitem_code='{$_REQUEST['ftitem_code']}'";
                  $rs=mysql_query( $ssql);
                  $r=mysql_fetch_assoc($rs);
                  ?>
                      document.getElementById('<?php echo $_REQUEST['ftuom_code']; ?>').value='<?php echo $r['ftuom_code']; ?>';
                      document.getElementById('<?php echo $_REQUEST['ftstock_category_code']; ?>').value='<?php echo $r['ftstock_category_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":
                $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);
                  ?>
                      document.getElementById('fnqty_availableD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $fnqty_available; ?>';

                  <?php
                     die("");
                        break;

        }

    }

    Getsecurity(27);

    $objAxpDB->ViewName("Daftar Stock Masuk Service");
    $objAxpDB->ViewTable("vINStockMasukService");
    $objAxpDB->ViewSQL("SELECT A.fnid, A.ftstock_in_return_no,  A.fdtrans_date,g.fttrans_status_name as fttype1,e.fttrans_status_name as fttype,A.ftpo_no, A.ftreturn_no, A.ftstock_in_no,A.ftsupplier_code,C.ftsupplier_name,a.ftnotes, b.fttrans_status_name
                        FROM txstock_in_return_hdr_bahan_baku
                        A INNER JOIN tstransaction_status B ON a.fnstatus=b.fntrans_status_code AND B.fntrans_group_code=2
                        INNER JOIN tlsupplier C ON a.ftsupplier_code=c.ftsupplier_code
                        INNER JOIN txpurchase_order_hdr f on a.ftpo_no=f.ftpo_no
                        left join tstransaction_status e on a.fnpo_type3 = e.fntrans_status_code and e.fntrans_group_code = 35
                        left join tstransaction_status g on a.fntype = e.fntrans_status_code and e.fntrans_group_code = 50
                        WHERE f.fnpo_type3=1 or f.fnpo_type3=2
                        ORDER BY  A.ftstock_in_return_no DESC ,A.fdtrans_date DESC");

    $objAxpDB->ViewCaption("ID,No. Stock Masuk Return, Tgl. Transaksi,Tipe Stock Masuk,Tipe PO, No. PO, No. Return, No. LPB, Kode Supplier, Nama Supplier, Catatan, Status");
	$objAxpDB->ViewPeriod(TRUE);
     if(!$_REQUEST['id']) {
          $strAutoNumber1="ajax_do('query2.php?ftsupplier_code=' + document.getElementById(\"ftsupplier_code\").value + '&code=2&ftmodule_code=TN005&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftstock_in_return_no&no_trans=ftstock_in_return_no&table_name=txstock_in_return_hdr_bahan_baku');";
             $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                                           {$strAutoNumber1}
                            }
                            OnChangeDate_fdtrans_date();
                                ";

             $objAxpDB->FormJS($strAutoNumber0);

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

    $objAxpDB->FormName("Stock Masuk Service");
    $objAxpDB->FormSQL("SELECT ftstock_in_return_no, fntype, ftreturn_no, ftstock_in_no,  ftpo_no,ftsupplier_code,ftsj_supplier_parent,fdtrans_date,ftreff_no,ftnotes,fntotal_qty, fnstatus, fnpo_type3 FROM txstock_in_return_hdr_bahan_baku");
    $objAxpDB->FormCaption("No. LPB Service,Type Stock Masuk, No. Return, No. LPB,  No. PO, Supplier,No. SJ Supplier *, Tgl. Transaksi,No. SJ Supplier,Catatan,Total Qty, Status");
    $objAxpDB->FormPosition("TL,TL,TL,TL,TL,TL,TL,TR,TR,BL,BR,BR,BR");

    $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code, ftwarehouse_code, ftlocation_code, fnqty_return, fnqty_sisa, fnqty, ftuom_code,ftdescription FROM txstock_in_return_det_bahan_baku");
    $objAxpDB->GridCaption(0, "No., Kode Barang, Gudang, Lokasi, Qty Return,  Qty Sisa Return, Qty, Satuan, Keterangan");
    $objAxpDB->GridAutoAdd(0, False);
    $objAxpDB->GridScript(0,6," onChange=\"StockMasukRecalQtyPembelian()\"");
    $objAxpDB->Lookup("ftsupplier_code", "SELECT ftsupplier_code, ftsupplier_name FROM tlsupplier", "ftsupplier_code", "ftsupplier_name", -1,false);
    $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("ftreturn_no", "SELECT fttrans_no, fdtrans_date FROM txdelivery_order_return_hdr_bahan_baku WHERE  fnstatus=1", "fttrans_no", "fdtrans_date", -1, False);
    $objAxpDB->Lookup("fntype", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 50", "fntrans_status_code", "fttrans_status_name",0, True);

    $ajaxOUM_Code="ajax_do('query2.php?code=1&obj1=' + this.value + '&obj2=ftuom_codeD0_N_&objCategoryftstock_category_codeD0_N_');";
    $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,2,"{$ajaxGudang}");
    if(!$_REQUEST['id']) {
		$m="  WHERE fnstatus=1   ORDER BY ftitem_code ";
	}
    $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr_setengah_jadi $m", "ftitem_code", "ftitem_name", -1,false);
//  $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code, ftwarehouse_code, ftlocation_code, fnqty_return, fnqty_sisa, fnqty, ftuom_code,ftdescription FROM txstock_in_return_det_bahan_baku");
    
	$objAxpDB->SetStatus("id, fnstatus");

    if ($objAxpDB->GetStatus("id") == "")
            $objAxpDB->ButtonShow("baru, cetak_daftar, export_excel , tutup");
    elseif ($objAxpDB->GetStatus("id") == 0) {
            $objAxpDB->GridAllowAddNew(0,false);
            $objAxpDB->GridAllowDelete(0,true);
            $objAxpDB->GridLocked(0, "1, 1, 0, 0, 1, 1, 0, 1,0");
            $objAxpDB->ButtonShow("reset, simpan, tutup");
            $objAxpDB->FormLocked("0,0,0,1,1,1,1,0,0,0,1,1");

    } elseif (($objAxpDB->GetStatus("fnstatus") == 1)||($objAxpDB->GetStatus("fnstatus") == 2)) {
           if(isset($_REQUEST['revisi'])) {
                    $objAxpDB->ButtonShow("simpan_revisi,tutup");
					$objAxpDB->GridAllowAddNew(0,false);
					$objAxpDB->GridAllowDelete(0,true);
					$objAxpDB->GridLocked(0, "1, 1, 0, 0, 1, 1, 0, 1,0");
					$objAxpDB->FormLocked("1,1,1,1,1,1,1,1,1,0,1,1");
                }
                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");
                }
    } elseif ($objAxpDB->GetStatus("fnstatus") == 3) {
              $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");
              $objAxpDB->ButtonShow("tutup");
        } elseif ($objAxpDB->GetStatus("fnstatus") == 9) {
				  $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");
                  $objAxpDB->ButtonShow("tutup");
    }
    if($_REQUEST['ajax']) {
           switch($_REQUEST['code']) {
           case "grid":
               ob_start();

				 $objAxpDB->lengt_object=false;

                         $_REQUEST['id']=0.5;
                         $ssql="SELECT a.fnindex, a.ftitem_code, '' as ftwarehouse_code, '' as ftlocation_code,
                                        a.fnqty as fnqty_return,a.fnqty- ifnull(a.fnqty_stockin_return,0) AS fnqty_sisa, a.fnqty-a.fnqty AS fnqty, a.ftuom_code,'' as ftdescription
                               FROM txdelivery_order_return_det_bahan_baku a
                               where a.fttrans_no='".$_REQUEST['ftreturn_no']."'
                               order by    a.fnindex #";

//                         die($ssql);
                        $objAxpDB->GridSQL(0,$ssql);

                $ssql="SELECT ftpo_no,ftstock_in_no,ftsupplier_code
                        FROM txdelivery_order_return_hdr_bahan_baku
                        WHERE fttrans_no='".$_REQUEST['ftreturn_no']."'";
               //               die($ssql);
               $rs=mysql_query($ssql);
               $r=mysql_fetch_assoc($rs);

               $ftpo_no=$r['ftpo_no'];
                 $ssql="SELECT fnpo_type3
                        FROM txpurchase_order_hdr
                        WHERE ftpo_no='{$ftpo_no}'  ";
               $fnpo_type3=RetRstField($ssql);
               if($fnpo_type3==1) {
                    $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr_setengah_jadi $m", "ftitem_code", "ftitem_name", -1,false);
               }
               else {
                   $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr_bahan_baku $m", "ftitem_code", "ftitem_name", -1,false);
               }

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


               $ftstock_in_no=$r['ftstock_in_no'];
               $ftsupplier_code=$r['ftsupplier_code'];
               $ssql="SELECT ftsupplier_name
                            FROM TLSupplier
                            WHERE ftsupplier_code='{$ftsupplier_code}'";
               $ftsupplier_name=RetRstField($ssql);

               $ssql="SELECT ftsj_no_supplier
                            FROM txdelivery_order_return_hdr_bahan_baku
                            WHERE fttrans_no='".$_REQUEST['ftreturn_no']."'";
               $ftsj_supplier_parent=RetRstField($ssql);


               echo "document.getElementById('ftpo_no').value='{$ftpo_no}';";
               echo "document.getElementById('ftstock_in_no').value='{$ftstock_in_no}';";
               echo "document.getElementById('ftsupplier_code').value='{$ftsupplier_code}';";
               echo "document.getElementById('hidden_ftsupplier_code').value='{$ftsupplier_name}';";
               echo "document.getElementById('span_ftsupplier_code').innerHTML='{$ftsupplier_name}';";
               echo "document.getElementById('ftsj_supplier_parent').value='{$ftsj_supplier_parent}';";
               echo "document.getElementById('fnpo_type3').value='{$fnpo_type3}';";

               break;

           }
           die("");
           exit;
    }
    if(isset($_REQUEST['id'])&&($_REQUEST['id'])!=0) {
         $ssql="SELECT fnpo_type3
                            FROM txstock_in_return_hdr_bahan_baku
                            WHERE fnid='{$_REQUEST['id']}'  ";
           $fnpo_type3=RetRstField($ssql);
           if($fnpo_type3==1) {
                $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr_setengah_jadi $m", "ftitem_code", "ftitem_name", -1,false);
           }
           else {
               $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr_bahan_baku $m", "ftitem_code", "ftitem_name", -1,false);
            }
    }
  
    $objAxpDB->Process();

    $objAxpDB->WriteHtmlTable();

?>

