<?php
      if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            case "transno":
              if(RetRstField("SELECT fnid FROM txdelivery_order_hdr_bahan_baku WHERE ftdo_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":
                  if($_REQUEST['fnitem_type']=="1") {
                        $ssql="SELECT ftuom_code,ftstock_category_code
                            FROM tlstock_hdr_setengah_jadi
                            WHERE ftitem_code='{$_REQUEST['ftitem_code']}'";
                  }
                  else {
                        $ssql="SELECT ftuom_code,ftstock_category_code
                            FROM tlstock_hdr_bahan_baku 
                            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":
                  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']}'
                                      ";
                  }
                  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']}'
                                      ";
                  }
                $fnqty_available=RetRstField($ssql);
                  ?>
                      document.getElementById('fnqty_availableD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $fnqty_available; ?>';
                   
                  <?php
                     die("");
                        break;

        }
       
    }

    Getsecurity(27);
    $objAxpDB->LookupMagicQuotes(True);
    $objAxpDB->ViewName("Daftar Surat Jalan SubCon");
    $objAxpDB->ViewTable("vINSuratJalanBarang_SubCon");
    $objAxpDB->ViewSQL("SELECT A.fnid, A.ftdo_no,  c.ftsupplier_name,A.ftpo_no,g.fttrans_status_name as fttype,h.fttrans_status_name as fttype2, A.fdtrans_date,A.fftotal_qty, b.fttrans_status_name
                       FROM txdelivery_order_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                     
                       left JOIN tstransaction_status D on D.fntrans_group_code = 7 AND D.fntrans_status_code=A.ftdelivery_method
                       LEFT JOIN tstransaction_status E on E.fntrans_group_code = 30 AND E.fntrans_status_code=A.ftdo_type
                       left join tstransaction_status g on a.fnpo_type3 = g.fntrans_status_code and g.fntrans_group_code = 35
                       left join tstransaction_status h on a.fnitem_type = h.fntrans_status_code and h.fntrans_group_code = 39 
                       /*WHERE f.fnpo_type3=1 */
                       ORDER BY  A.ftdo_no DESC ,A.fdtrans_date DESC");

    $objAxpDB->ViewCaption("ID, No. DO, Supplier,No. PO,Tipe PO,Tipe Barang, Tgl. transaksi,  Total Qty ,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=ftdo_no&no_trans=ftdo_no&table_name=txdelivery_order_hdr_bahan_baku');";
          $ajaxTransDate="ajax_do('inventory_bahan_baku.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=tgl&tgl=' + document.getElementById(\"fdtrans_date\").value + '');";
          $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                                           {$strAutoNumber1}
                                            {$ajaxTransDate}
                            }
                            OnChangeDate_fdtrans_date();
                                ";

             $objAxpDB->FormJS($strAutoNumber0);
             $ajaxAddress="ajax_do('query2.php?code=611&ftsupplier_code=' + document.getElementById('ftsupplier_code').value + '&objAddress=ftship_address');";
			 $ajaxAddress_1="ajax_do('query2.php?code=bb6&ftsupplier_code=' + this.value + '&objAddress=ftship_address');";            
             $x="document.getElementById('ftpo_no').value='';";
             $x.="document.getElementById('hidden_ftpo_no').value='';";
             $x.="document.getElementById('span_ftpo_no').innerHTML='';";
             
             $objAxpDB->FormScript(3, "onChange=\"{$ajaxAddress_1}\"");
     }
	 else {
          $ajaxTransDate="ajax_do('inventory_bahan_baku.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=tgl&tgl=' + document.getElementById(\"fdtrans_date\").value + '');";
          $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                                            {$ajaxTransDate}
                            }
                            OnChangeDate_fdtrans_date();
                                ";

          $objAxpDB->FormJS($strAutoNumber0);	 
	 }
    $objAxpDB->FormName("Surat Jalan Barang (ada PO)");
    $objAxpDB->FormSQL("SELECT ftdo_no,fnpo_type3,fnitem_type, ftsupplier_code, ftpo_no,  ftship_address, fdtrans_date, fddue_date, ftdelivery_method, ftreference, ftnotes,  fftotal_qty, fnstatus FROM txdelivery_order_hdr_bahan_baku");
    $objAxpDB->FormCaption("No. DO,Tipe PO, Tipe Barang, Supplier,PO No,  Alamat, Tgl. transaksi, Tgl. Jatuh Tempo, Metode Pengiriman, Referensi, Catatan, Total Qty, Status");
    $objAxpDB->FormPosition("TL,TL,TL,TL,TL,TL,TR,HD,HD,HD,BL,BR,BR");

    $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code, ftwarehouse_code, ftlocation_code,fnqty_available, fnqty,ftdescription, ftuom_code,ftstock_category_code FROM txdelivery_order_det_bahan_baku");
    $objAxpDB->GridCaption(0, "No., Kode Barang, Gudang, Lokasi, Qty Avail, Qty SJ, Keterangan,Satuan, Kategori");
    $objAxpDB->GridHidden(0, "0,0,0,0,0,0,0,0,1");
    #$objAxpDB->GridAutoAdd(0, False);
    $ajaxGudang="OnChange=\"ajax_do('inventory_bahan_baku.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=gudang&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&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+'&ftitem_code='+ document.getElementById('ftitem_codeD0_N_').value+'&ftlocation_code='+ document.getElementById('ftlocation_codeD0_N_').value+'&ftwarehouse_code='+ document.getElementById('ftwarehouse_codeD0_N_').value );\"";
 
    $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(2, "onChange=\"{$ajaxGrid}\"");    
    $objAxpDB->GridScript(0,2, $ajaxGudang);
    $objAxpDB->GridScript(0,1, $ajaxOUM_Code);    
    $objAxpDB->GridScript(0,3, $ajaxQtyAvail);
    $objAxpDB->GridScript(0,5,"onChange='DeliveryOrderRecalQty();'");
    $objAxpDB->GridDelJS(0,"DeliveryOrderRecalQty();");

    $objAxpDB->Lookup("ftsupplier_code", "SELECT ftsupplier_code, ftsupplier_name FROM tlsupplier", "ftsupplier_code", "ftsupplier_name", -1,false);
    $objAxpDB->Lookup( "ftpo_no", "SELECT ftpo_no, fdtrans_date,ftsupplier_code,fnstatus FROM txpurchase_order_hdr  ", "ftpo_no", "fdtrans_date", -1,false,"' AND fnstatus=1 AND  ftsupplier_code=\''+ document.getElementById('ftsupplier_code').value + '\''");	
    $objAxpDB->Lookup("fnpo_type3", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_status_code<>0 AND fntrans_group_code = 35", "fntrans_status_code", "fttrans_status_name", 0,true);
    $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->Lookup("ftdelivery_method", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 7", "fntrans_status_code", "fttrans_status_name", -1,true);
    $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("ftdo_type", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 30", "fntrans_status_code", "fttrans_status_name", 0,true);

    if(!$_REQUEST['id']) {
		$m="  WHERE fnstatus=1  ORDER BY ftitem_code ";
                $where_ftitem_code=" WHERE fnstatus=1";
	}
	
    $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr_setengah_jadi $m", "ftitem_code", "ftitem_name", -1,false);

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

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

                if($_REQUEST['fnitem_type']==1) {
                    $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr_setengah_jadi {$where_ftitem_code} ORDER BY ftitem_code", "ftitem_code", "ftitem_name", -1, False);
                }
                else {
                    $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr_bahan_baku {$where_ftitem_code} ORDER BY ftitem_code", "ftitem_code", "ftitem_name", -1, False);
                }
                 $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code, ftwarehouse_code, ftlocation_code, fnqty,ftdescription, ftuom_code,ftstock_category_code FROM txdelivery_order_det_bahan_baku");

				$objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code, ftwarehouse_code, ftlocation_code,fnqty_available, fnqty,ftdescription, ftuom_code,ftstock_category_code FROM txdelivery_order_det_bahan_baku");
				$objAxpDB->GridCaption(0, "No., Kode Barang, Gudang, Lokasi, Qty Avail, Qty SJ, Keterangan,Satuan, Kategori");
				$objAxpDB->GridHidden(0, "0,0,0,0,0,0,0,0,1");
				
                $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();

                <?

                break;

           }
           die("");
           exit;
    }   

  if(isset($_REQUEST['id'])&&($_REQUEST['id'])!=0) {
        $ssql="SELECT fnitem_type 
                     FROM txdelivery_order_hdr_bahan_baku
                        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();



?>

