<?php
         if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            case "transno":
              if(RetRstField("SELECT fnid FROM txstock_mutation_hdr_bbtobj WHERE ftstock_mutation_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 "gudang":
                $ssql="SELECT a.ftlocation_code,a.ftlocation_name FROM tllocation a
                        inner join tlwarehouse b on a.ftwarehouse_code = b.ftwarehouse_code
                        WHERE a.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);
                    
                                   } ";
                        }
                  if($_REQUEST['t']==1) {
                        ?>
                       document.getElementById('<?php echo $_REQUEST['objQty']; ?>').value=0; 
                    <?php  
                  }
                  
            break;

            case "formula":
                $ssql="SELECT IFNULL(SUM(ffformula),1) as ffformula
                        FROM tlstock_multi_det_bahan_baku
                        WHERE ftitem_code='{$_REQUEST['ftitem_code']}'
                        and ftuom_code_multi='{$_REQUEST['ftuom_code']}'
                        ";
                $ffformula=RetRstField($ssql);
                $ffQtyMulti=(double)$ffformula*(double)CCur($_REQUEST['fnqty']);
                ?>
                        document.getElementById('ffformulaD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ffformula; ?>';
                        document.getElementById('fnqty_baseD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ffQtyMulti; ?>';
                <?php

                die("");
            break;
        }       
        die("");
    }
    
    Getsecurity('inventorybb_stock_mutasi_barang_jadi');
    
    $objAxpDB->ViewName("Daftar Mutasi Barang Jadi");
    //$objAxpDB->ViewTable("vINStockMutasi");
    $objAxpDB->ViewSQL("SELECT A.fnid, A.ftstock_mutation_no, A.fdtrans_date, A.ftnotes, B.fttrans_status_name
                        FROM txstock_mutation_hdr_bbtobj
                        A INNER JOIN tstransaction_status B ON a.fnstatus=b.fntrans_status_code AND B.fntrans_group_code=2 ORDER BY A.ftstock_mutation_no DESC, A.fdtrans_date DESC");
    $objAxpDB->ViewCaption("ID, No. Mutasi, Tgl. Mutasi, Catatan, Status");
    $objAxpDB->ViewPeriod(TRUE);

    if(!$_REQUEST['id']) {
        $strAutoNumber1="ajax_do('query2.php?code=2&ftmodule_code=bbbj003&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftstock_mutation_no&no_trans=ftstock_mutation_no&table_name=txstock_mutation_hdr_bbtobj&ftbranch_code={$_SESSION['strbranch']}');";
        $strAutoNumber0=" function OnChangeDate_fdtrans_date() {                                                    
                                       {$strAutoNumber1} 
                                  } 
                             OnChangeDate_fdtrans_date(); 
                            ";   
   
        $objAxpDB->FormJS($strAutoNumber0);
 	$m=" fnstatus=1 AND ";
    }
    

    
    $objAxpDB->FormName("Mutasi Barang Jadi");
    $objAxpDB->FormSQL("SELECT ftstock_mutation_no, fdtrans_date, ftnotes, fnstatus FROM txstock_mutation_hdr_bbtobj");
    $objAxpDB->FormCaption("No. Mutasi, Tgl. Mutasi, Catatan, Status");
    $objAxpDB->FormPosition("TL, TR, BL, BR");
   
    $ajaxOUM_Code="ajax_do('query2.php?code=uom&obj1=' + this.value + '&obj2=ftuom_codeD0_N_');";
    $ajaxExp_Date="ajax_do('query2.php?code=exp_datebb&obj1=' + this.value + '&obj2=ftactiveD0_N_');";
    $ajaxQtyAvail=" onChange=\"ajax_do('query2.php?code=qty_bahan_baku&ftwarehouse_code=' + document.getElementById('ftwarehouse_fromD0_N_').value + '&ftlocation_code=' + document.getElementById('ftlocation_fromD0_N_').value+'&ftitem_code=' + document.getElementById('ftitem_codeD0_N_').value+ '&fdexp_date=' + document.getElementById('fdexp_dateD0_N_').value+ '&objQty=fnqty_availableD0_N_');\"";
    $ajaxOUM_CodeAndQtyAvail=" onChange=\"{$ajaxOUM_Code}{$ajaxExp_Date};ajax_do('query2.php?code=qty_bahan_baku&ftwarehouse_code=' + document.getElementById('ftwarehouse_fromD0_N_').value + '&ftlocation_code=' + document.getElementById('ftlocation_fromD0_N_').value+'&ftitem_code=' + document.getElementById('ftitem_codeD0_N_').value+ '&objQty=fnqty_availableD0_N_');\"";
    $ajaxGudang1="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_fromD0_N_&t=1&objQty=fnqty_availableD0_N_');\"";
    $objAxpDB->GridScript(0,1, $ajaxOUM_CodeAndQtyAvail);
    $objAxpDB->GridScript(0,3, $ajaxGudang1);
    $objAxpDB->GridScript(0,4, $ajaxQtyAvail);
    $ajaxGudang2="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_toD0_N_&t=2');\"";
    $objAxpDB->GridScript(0,5, $ajaxGudang2);
    $ajaxFormula="OnChange=\"ajax_do('inventory_bahan_baku.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=formula&index=_N_&index=_N_&ftitem_code='+ document.getElementById('ftitem_codeD0_N_').value+'&ftuom_code='+ document.getElementById('ftuom_codeD0_N_').value+'&fnqty='+ document.getElementById('fnqtyD0_N_').value);\"";
    $objAxpDB->GridScript(0,8, $ajaxFormula);

    $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code, fdexp_date, ftwarehouse_from, ftlocation_from, ftwarehouse_to, ftlocation_to, fnqty_available, fnqty, ftuom_code, fnqty_base, ffformula, '' as ftactive FROM txstock_mutation_det_bbtobj");
    $objAxpDB->GridCaption(0, "No., Kode Barang, Exp. Date, Gudang Asal, Lokasi Asal, Gudang Tujuan, Lokasi Tujuan, Qty Avail, Qty, Satuan, Qty Base, Formula, Exp. Date Status");
    
    $objAxpDB->Lookup("ftwarehouse_from", "SELECT ftwarehouse_code, ftwarehouse_name FROM tlwarehouse where fnwarehouse_type = 0 or fnwarehouse_type = 3 ", "ftwarehouse_code", "ftwarehouse_name", -1,true);
    $objAxpDB->Lookup("ftwarehouse_to", "SELECT ftwarehouse_code, ftwarehouse_name FROM tlwarehouse where fnwarehouse_type = 0 or fnwarehouse_type = 3 ", "ftwarehouse_code", "ftwarehouse_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 a.ftitem_code, a.ftitem_name FROM tlstock_hdr_bahan_baku a WHERE $m (SELECT COUNT(*) FROM tlstock_hdr b WHERE b.ftitem_code=a.ftitem_code) > 0", "ftitem_code", "ftitem_name", -1,false);
    $objAxpDB->Lookup("ftlocation_from", "SELECT ftlocation_code, ftlocation_name FROM tllocation where 0=0 ", "ftlocation_code", "ftlocation_name", -1,true," and ftwarehouse_code='{$_REQUEST['ftwarehouse_code']}' ");
    $objAxpDB->Lookup("ftlocation_to", "SELECT ftlocation_code, ftlocation_name FROM tllocation where 0=0 ", "ftlocation_code", "ftlocation_name", -1,true," and ftwarehouse_code='{$_REQUEST['ftwarehouse_code']}' ");
   
    $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, 0, 0, 0, 1, 0, 1, 1, 1");
            $objAxpDB->ButtonShow("reset, simpan, tutup");
            $objAxpDB->FormLocked("0, 0, 0, 1");
        
        $objAxpDB->GridSQL(1, "SELECT REPLACE(fbaccess,1,'aktif') AS fbaccess  FROM tluser_access_setting #");
        $objAxpDB->GridCaption(1, "Access");
        $objAxpDB->GridAutoAdd(1, FALSE);
	
    } elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
            $objAxpDB->ButtonShow("cetak, baru, batal, tutup");
            $objAxpDB->FormLocked("1, 1, 1, 1");
            $objAxpDB->GridLocked(0, "1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
    } elseif ($objAxpDB->GetStatus("fnstatus") == 9) {
            $objAxpDB->FormLocked("1, 1, 1, 1");
            $objAxpDB->GridLocked(0, "1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
            $objAxpDB->ButtonShow("tutup");
    }

    $objAxpDB->GridHidden(0,'0,0,0,0,0,0,0,0,0,1,1,1,1');
    $objAxpDB->GridHidden(1,'1');

    $objAxpDB->Process();

    $objAxpDB->WriteHtmlTable();