<?php

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

                             } ";


                        }

                die("");
            break;
            case "fdschedule_delivery_date":
              $ssql1="SELECT COUNT(*) AS jml
                    FROM txpurchase_order_schedule_delivery
                    WHERE ftpo_no='{$_REQUEST['ftpo_no']}' ";

               $ssql2="SELECT fdschedule_delivery_date
                    FROM txpurchase_order_schedule_delivery
                    WHERE ftpo_no='{$_REQUEST['ftpo_no']}' AND
                            fnlock='0'
                    ORDER BY fnindex";

                $ssql3="SELECT COUNT(*) AS jml
                    FROM txpurchase_order_schedule_delivery
                    WHERE ftpo_no='{$_REQUEST['ftpo_no']}' ";

               if(RetRstField($ssql1)==1) {
                   $ssql="SELECT fdschedule_delivery_date
                        FROM txpurchase_order_schedule_delivery
                         WHERE ftpo_no='{$_REQUEST['ftpo_no']}' ";
                   $date=RetRstField($ssql);
                   ?>

                   document.getElementById('fdschedule_delivery_date').value='<?php echo CDateRev($date); ?>';
                   <?php

               }
               elseif(RetRstField($ssql2)!="") {
                    $ssql2="SELECT fdschedule_delivery_date
                            FROM txpurchase_order_schedule_delivery
                            WHERE ftpo_no='{$_REQUEST['ftpo_no']}' AND
                                    fnlock='0'
                            ORDER BY fnindex";
                   $date=RetRstField($ssql2);
                    ?>
                   document.getElementById('fdschedule_delivery_date').value='<?php echo CDateRev($date); ?>';
                   <?php
               }
               else {
                   $ssql2="SELECT fdschedule_delivery_date
                            FROM txpurchase_order_schedule_delivery
                            WHERE ftpo_no='{$_REQUEST['ftpo_no']}'
                            ORDER BY fnindex DESC";
                   $date=RetRstField($ssql2);
                    ?>
                   document.getElementById('fdschedule_delivery_date').value='<?php echo CDateRev($date); ?>';
                   <?php
               }

              die("");
            break;

        }

    }

    Getsecurity(18);
     $objAxpDB->LookupMagicQuotes(True);
    $objAxpDB->ViewName("Daftar Penerimaan Barang");

    $ssql="SELECT A.fnid,  A.ftstock_in_no, c.fttrans_status_name as ftcategory_code, f.fttrans_status_name as fttype2, d.ftsupplier_name as ftstock_in_company, A.ftdocument_no, A.fdtrans_date, A.ftreff_no,A.ftchecked_user, A.fntotal_qty, A.ftnotes, B.fttrans_status_name
                        FROM txstock_in_hdr_bahan_baku
                        A INNER JOIN tstransaction_status B ON a.fnstatus=b.fntrans_status_code AND B.fntrans_group_code=2
                        inner join tstransaction_status c on A.ftcategory_code=c.fntrans_status_code AND c.fntrans_group_code=20
                          left JOIN tlsupplier d on a.ftstock_in_company=d.ftsupplier_code
                           left join tstransaction_status f on a.fnpo_typelpb = f.fntrans_status_code and f.fntrans_group_code = 36 
                        where a.ftcategory_code='0'  and a.fnpo_type3=1";



    $objAxpDB->ViewSQL("SELECT * FROM ({$ssql}) X ORDER BY  ftstock_in_no DESC,fdtrans_date DESC" );

    $objAxpDB->ViewCaption("ID,No. LPB, Kategori,Tipe LPB, Supplier, No. PO, Tgl. LPB, No. SJ, Pengecek Barang, Total Qty, Catatan, 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=ftstock_in_no&no_trans=ftstock_in_no&table_name=txstock_in_hdr_bahan_baku');";
             $strAutoNumber1="ajax_do('query2.php?code=2&ftmodule_code=tn107&fnpo_typelpb=' + document.getElementById(\"fnpo_typelpb\").value + '&fnpo_type3=' + document.getElementById(\"fnpo_type3\").value + '&ftcategory_code=' + document.getElementById(\"ftcategory_code\").value + '&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftstock_in_no&no_trans=ftstock_in_no&table_name=txstock_in_hdr_bahan_baku');";

           $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                                           {$strAutoNumber1}
                                     }

                              OnChangeDate_fdtrans_date();
                              function OnChangeKategory() {
                                    var m=document.getElementById('ftcategory_code').value;
                                    var result='';
                                    if((m=='0')||(m=='')) {
                                        return \"SELECT ftsupplier_code, ftsupplier_name FROM tlsupplier\";
                                    }

                              }
                                 function get_ftsupplier_code() {
                                        return  \" and ftsupplier_code='\"+ document.getElementById('ftstock_in_company').value + \"'\";
                                }
                                   function get_fnpo_typelpb() {
                                        return  \" and fnpo_typelpb='\"+ document.getElementById('fnpo_typelpb').value + \"'\";
                                }
                                 function StockMasukPembelian_warehouse_onchange() {
                                        var i=document.getElementsByName('fnindexD0[]').length;

                                        for(var j=1;j<i;j++) {
                                            document.getElementsByName('ftwarehouse_codeD0[]')[j].onchange();

                                        }
                                    }

                                var i=document.getElementsByName('fnindexD0[]').length;
                                 document.getElementById(\"TransactionDetail0\").deleteRow(2);
                                       ";
            $objAxpDB->FormJS($strAutoNumber0);
             $ajaxpo="ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=fdschedule_delivery_date&ftpo_no='+ document.getElementById('ftdocument_no').value);";


             $mm=" and fnpo_type3=1 and fnstatus=1 '+ get_ftsupplier_code() + '  '+ get_fnpo_typelpb() +' "; 
             $ajaxGrid="{$ajaxpo}ajax_do_grid0('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=grid&ftpo_no='+ document.getElementById('ftdocument_no').value);";
             $objAxpDB->FormScript(5, "onChange=\"{$ajaxGrid}\"");
             $objAxpDB->FormScript(3, "onChange=\"OnChangeDate_fdtrans_date();\"");
     }

    $objAxpDB->FormName("Penerimaan Barang");
    $objAxpDB->FormSQL("SELECT ftstock_in_no, fnpo_type3, ftcategory_code, fnpo_typelpb, ftstock_in_company, ftdocument_no,fdschedule_delivery_date, fdtrans_date, ftreff_no, ftchecked_user, ftnotes,  fntotal_qty, fnstatus FROM txstock_in_hdr_bahan_baku");
    $objAxpDB->FormCaption("No. LPB,Tipe PO, Kategori,Tipe LPB, Supplier, No. PO,Tgl. Schedule Delivery, Tgl. LPB, No. SJ, Pengecek Barang, Catatan,  Total Qty, Status");
    $objAxpDB->FormPosition("TL, TL,TL, TL, TL,  TL, TL, TR, TR, TR, BL, BR, BR");


    #$objAxpDB->FormScript(1, "onChange=\"ajax_do('query2.php?code=14&value=' + this.value + '');\"");
    $objAxpDB->FormScript(2, "onChange=\"document.getElementById('ftstock_in_company').value='';document.getElementById('hidden_ftstock_in_company').value='';\"");


    $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code, ftitem_code1 ,ftdescription, ftwarehouse_code, ftlocation_code, fnqtypo,fnqtysisapo, fnqty, ftuom_code, fnindex_po FROM txstock_in_det_bahan_baku");
    $objAxpDB->GridCaption(0, "No., Kode Barang, Kode Barang PO, Keterangan, Gudang, Lokasi, Qty PO, Qty Sisa PO, Qty, Satuan,Index PO");
    $objAxpDB->GridHidden(0,'0,0,0,0,0,0,0,0,0,0,1');
    $objAxpDB->GridAutoAdd(0, False);
    $objAxpDB->GridScript(0,6,"onChange='StockMasukRecalQty();'");
//    $objAxpDB->GridScript(0,6,"onChange='StockMasukRecalQty();'");


    $objAxpDB->Lookup( "ftcategory_code", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 20", "fntrans_status_code", "fttrans_status_name", 0,true);

    $objAxpDB->Lookup( "ftstock_in_company", "SELECT  ftsupplier_code,  ftsupplier_name from tlsupplier", "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_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('ftdocument_no').value + '\')'");

    $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( "ftdocument_no", "SELECT ftpo_no, fdtrans_date,ftsupplier_code,fnpo_type3 FROM txpurchase_order_hdr where fnstatus=1 and fnreceipt_status<>3 ", "ftpo_no", "fdtrans_date", -1,false,"' AND fnpo_type3=1  AND ftsupplier_code=\''+ document.getElementById('ftstock_in_company').value + '\''");
     $objAxpDB->Lookup("fnpo_type3", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 35", "fntrans_status_code", "fttrans_status_name", 1,true);

      $objAxpDB->Lookup("fnpo_typelpb", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 36", "fntrans_status_code", "fttrans_status_name", 0,true);

    $ajaxGudang="OnChange=\"ajax_do('pembelian.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=gudang&ftwarehouse_code='+this.value+'&ObjLokasi=ftlocation_codeD0_N_');\"";

    $objAxpDB->GridScript(0,4, $ajaxGudang);
    $js1="OnChange=\"StockMasukRecalQtyPembelian();\"";
    $objAxpDB->GridScript(0,8, $js1);

    $objAxpDB->SetStatus("id, fnstatus,ftcategory_code");
    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,0,0,0,1,0');
                 
                $objAxpDB->ButtonShow("reset, simpan, tutup");
                 $objAxpDB->FormLocked("0,1, 1, 0,0, 0,1, 0, 0, 0, 0, 1, 1");
    }

    elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
             $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');
    }

    elseif (($objAxpDB->GetStatus("fnstatus") == 9)|| ($objAxpDB->GetStatus("fnstatus") == 3)) {
             $objAxpDB->GridLocked(0,'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.5;

                         $ssql="select  a.fnindex, a.ftitem_code,a.ftitem_code as ftitem_code1,a.ftdescription, ifnull(c.ftwarehouse_default,'') as ftwarehouse_code, '' as ftlocation_code,a.fnqty as fnqtypo,a.fnqty-ifnull(a.fnqty_accept,0) as fnqtysisapo,a.fnqty-ifnull(a.fnqty_accept,0) as  fnqty, a.ftuom_code,
                                 a.fnindex as  fnindex_po 
                                from txpurchase_order_det a
                                    left join tlstock_hdr_setengah_jadi b on a.ftitem_code=b.ftitem_code
                                    left join tlstock_group_bahan_baku c on b.ftstock_group_code=c.ftstock_group_code
                               where a.ftpo_no='".$_REQUEST['ftpo_no']."' and
                                        a.fnqty-ifnull(a.fnqty_accept,0)>0
                                 order by    a.fnindex 
                                   #  ";
                   
                        $objAxpDB->GridSQL(0,$ssql);


                        $objAxpDB->GridCaption(0, "No., Kode Barang, Kode Barang PO,Keterangan, Gudang, Lokasi, Qty PO, Qty Sisa PO, Qty, Satuan,Index PO");
                        $objAxpDB->GridLocked(0, "1, 0,0,1, 0, 0, 1, 1, 0,1");
                        $objAxpDB->GridHidden(0,'0,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;
                    RowIndex0();
                    reSize();
                    StockMasukRecalQtyPembelian();
                    StockMasukPembelian_warehouse_onchange();
               <?

               break;

           }
           die("");
           exit;
    }

    $objAxpDB->Process();

    $objAxpDB->WriteHtmlTable();

?>

