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

                                   } ";

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

             case "oum":
                  $ssql="SELECT ftuom_code,ftstock_category_code FROM tlstock_hdr 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 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
            break;
        }
        die("");
    }

    Getsecurity(27);

    $objAxpDB->ViewName("Daftar Delivery Order");
    $objAxpDB->ViewSQL("SELECT A.fnid, A.ftdo_no,  c.ftcustomer_name, A.fdtrans_date, A.fddue_date,D.fttrans_status_name AS ftdelivery_method,
                        E.fttrans_status_name AS ftdo_type, A.ftreference, cc.ftitem_code,cc.ftitem_name,cc.fnqty, A.fftotal_qty, b.fttrans_status_name
                       FROM txdelivery_order_hdr  A
                       inner join txdelivery_order_det cc ON  a.ftdo_no=cc.ftdo_no
                        INNER JOIN tstransaction_status B ON a.fnstatus=b.fntrans_status_code AND B.fntrans_group_code=2
                       
                       INNER JOIN tlcustomer C ON a.ftcustomer_code=c.ftcustomer_code
                       INNER 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
                       ORDER BY  A.ftdo_no DESC ,A.fdtrans_date DESC");

    $objAxpDB->ViewCaption("ID, No. DO, Customer, Tgl. transaksi, Tgl. Jatuh Tempo, Metode Pengiriman, Tipe Surat Jalan, Referensi,  Total Qty ,Status, ");
    $objAxpDB->ViewPeriod(TRUE);
    
    if(!$_REQUEST['id']) {
          $strAutoNumber1="ajax_do('query2.php?ftcustomer_code=' + document.getElementById(\"ftcustomer_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&ftdo_type=' + document.getElementById(\"ftdo_type\").value);";
          $ajaxTransDate="ajax_do('penjualan.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);
     }
    $objAxpDB->FormName("Delivery Order");
    $objAxpDB->FormSQL("SELECT ftdo_no, ftdo_type,  ftcustomer_code, ftship_address, fdtrans_date, fddue_date, ftdelivery_method, ftreference, ftnotes,  fftotal_qty, fnstatus FROM txdelivery_order_hdr");
    $objAxpDB->FormCaption("No. DO, Tipe DO, Customer, Alamat, Tgl. transaksi, Tgl. Jatuh Tempo, Metode Pengiriman, Referensi, Catatan, Total Qty, Status");
    $objAxpDB->FormPosition("TL, TL, TL, TL, TR, TR, TR, TR, BL, BR, BR");

    $ajaxAddress="ajax_do('query2.php?code=611&ftcustomer_code=' + document.getElementById('ftcustomer_code').value + '&ftdo_type=' + document.getElementById('ftdo_type').value + '&objAddress=ftship_address');";

    $objAxpDB->FormScript(1,"onChange=\"OnChangeDate_fdtrans_date()\"");
    $objAxpDB->FormScript(2,"onChange=\"{$ajaxAddress}\"");

    $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code, ftwarehouse_code, ftlocation_code, fnqty_available, fnqty,ftdescription, ftuom_code,ftstock_category_code FROM txdelivery_order_det");
    $objAxpDB->GridCaption(0, "No., Kode Barang, Gudang, Lokasi, Qty Avail,  Qty, Keterangan,Satuan, Kategori");
    $objAxpDB->GridHidden(0,"0, 0, 0, 0, 0, 0,0,0,1");
    //$objAxpDB->GridScroll(0,true,"100");
    $ajaxOUM_Code="ajax_do('query2.php?code=1&obj1=' + this.value + '&obj2=ftuom_codeD0_N_&objCategoryftstock_category_codeD0_N_');";
    $ajaxQtyAvail=" onChange=\"ajax_do('query2.php?code=4&ftwarehouse_code=' + document.getElementById('ftwarehouse_codeD0_N_').value + '&ftlocation_code=' + document.getElementById('ftlocation_codeD0_N_').value+'&ftitem_code=' + document.getElementById('ftitem_codeD0_N_').value+ '&objQty=fnqty_availableD0_N_');\"";
    $ajaxOUM_CodeAndQtyAvail=" onChange=\"{$ajaxOUM_Code};ajax_do('query2.php?code=4&ftwarehouse_code=' + document.getElementById('ftwarehouse_codeD0_N_').value + '&ftlocation_code=' + document.getElementById('ftlocation_codeD0_N_').value+'&ftitem_code=' + document.getElementById('ftitem_codeD0_N_').value+ '&objQty=fnqty_availableD0_N_');\"";

     $ajaxGudang="OnChange=\"ajax_do('penjualan.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_');\"";

     $ajaxOUM_Code="OnChange=\"ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=oum&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_');\"";

    $objAxpDB->GridScript(0,2, $ajaxGudang);
    $objAxpDB->GridScript(0,1, $ajaxOUM_Code);
    #$objAxpDB->GridScript(0,2, $ajaxQtyAvail);
    $objAxpDB->GridScript(0,3, $ajaxQtyAvail);
    $objAxpDB->GridScript(0,3, $ajaxQtyAvail);
    $objAxpDB->GridScript(0,5,"onChange='DeliveryOrderRecalQty();'");
     $objAxpDB->GridDelJS(0,"DeliveryOrderRecalQty();");

    $objAxpDB->Lookup("ftcustomer_code", "SELECT ftcustomer_code, ftcustomer_name FROM tlcustomer", "ftcustomer_code", "ftcustomer_name", -1,false);
    $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", "ftwarehouse_code", "ftwarehouse_name", -1,true);
    $objAxpDB->Lookup("ftlocation_code", "SELECT ftlocation_code, ftlocation_name FROM tllocation", "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 ";
	}
    $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr $m", "ftitem_code", "ftitem_name", -1,false);


    //$objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr", "ftitem_code", "ftitem_name", -1,false);

    $objAxpDB->SetStatus("id, fnstatus");
    if ($objAxpDB->GetStatus("id") == "")
            $objAxpDB->ButtonShow("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,1");
            $objAxpDB->ButtonShow("reset,  tutup");
             $objAxpDB->FormLocked("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("tutup");
                    $objAxpDB->FormLocked("1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1");
                    $objAxpDB->GridLocked(0, "1, 0, 0, 0, 1, 0,  0,1,1");
                    $objAxpDB->FormLocked("1, 0, 0, 0,  0,0, 0, 0, 0, 1, 1");
                    $objAxpDB->GridAllowAddNew(0,true);
                    $objAxpDB->GridAllowDelete(0,true);
                }
                else {
                    $objAxpDB->ButtonShow("tutup");
                    $objAxpDB->FormLocked("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->GridLocked(0, "1, 1, 1, 1, 1, 1, 1, 1,1");
              $objAxpDB->FormLocked("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");
              $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
            $objAxpDB->ButtonShow("tutup");
    }
    $objAxpDB->Process();

    $objAxpDB->WriteHtmlTable();



?>

