<?php
    
    if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            case "transno":
              if(RetRstField("SELECT fnid FROM txstock_adjustment_hdr WHERE ftstock_adjustment_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 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;
                       document.getElementById('fnqtyD0<?php echo $_REQUEST['index']; ?>').value='<?php echo 0-((double)$_REQUEST['objQty']); ?>';
                    <?php
                    
                die("");
            break;
            case "avail":
                 
                $ssql="SELECT ftwarehouse_code FROM tllocation 
                        WHERE ftlocation_code='{$_REQUEST['ftlocation_code']}'
                        ";
                $ftwarehouse_code=RetRstField($ssql);

                ?>
                document.getElementById('ftwarehouse_codeD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ftwarehouse_code; ?>';                
                ajax_do('query2.php?formx=<?php echo $_REQUEST['form']; ?>&code=qty&index=<?php echo $_REQUEST['index']; ?>&ftwarehouse_code=' + document.getElementById('ftwarehouse_codeD0<?php echo $_REQUEST['index']; ?>').value + '&ftlocation_code=' + document.getElementById('ftlocation_codeD0<?php echo $_REQUEST['index']; ?>').value+'&ftitem_code=' + document.getElementById('ftitem_codeD0<?php echo $_REQUEST['index']; ?>').value+ '&fdexp_date=' + document.getElementById('fdexp_dateD0<?php echo $_REQUEST['index']; ?>').value+ '&objQty=fnqty_availableD0<?php echo $_REQUEST['index']; ?>');
                <?php

                die("");
                break;

            case "formula":
                $ssql="SELECT IFNULL(SUM(ffformula),1) as ffformula
                        FROM tlstock_multi_det
                        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;
        }       
       
    }

    Getsecurity('penyesuaian_barang');
    $objAxpDB->ViewName("Daftar Penyesuaian Barang");
    //$objAxpDB->ViewTable("vIStockPenyesuaian");
    $objAxpDB->ViewSQL("SELECT A.fnid,  A.ftstock_adjustment_no, A.fdtrans_date,A.ftreference, A.ftnotes, B.fttrans_status_name
                         FROM txstock_adjustment_hdr
                        A INNER JOIN tstransaction_status B ON a.fnstatus=b.fntrans_status_code AND B.fntrans_group_code=2
                        WHERE (('{$_SESSION['strbranch']}' <> 'PUSAT' AND a.ftbranch_code = '{$_SESSION['strbranch']}') OR '{$_SESSION['strbranch']}' = 'PUSAT')
                        ORDER BY  A.ftstock_adjustment_no DESC, A.fdtrans_date DESC");
    $objAxpDB->ViewCaption("ID,  No. transaksi, Tgl. Transaksi,No. Referensi, Catatan, Status");
    $objAxpDB->ViewPeriod(TRUE);

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

        $ajaxGrid="ajax_do_grid0('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=grid&ftsubgroup_code='+ document.getElementById('ftsubgroup_code').value +'');";
        $objAxpDB->FormScript(1, "onChange=\"{$ajaxGrid}\"");
	$m=" WHERE ftstock_category_code='inv' AND fnstatus=1 ORDER BY ftitem_name,ftpart_no "; 
       
    } else {
        $m=" WHERE ftstock_category_code='inv' ORDER BY ftitem_name,ftpart_no "; 
       
    }

    $objAxpDB->FormName("Penyesuaian Barang");
    $objAxpDB->FormSQL("SELECT ftstock_adjustment_no,ftreference, fdtrans_date, ftnotes,  fnstatus FROM txstock_adjustment_hdr");
    $objAxpDB->FormCaption("No. transaksi, No. Referensi, Tgl. transaksi, Catatan,  Status");
    $objAxpDB->FormPosition("TL, 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_date&obj1=' + this.value + '&obj2=ftactiveD0_N_');ElementCurrencyALL();";
    //$ajaxQtyAvail=" onChange=\"ajax_do('query2.php?formx=".$_REQUEST['form']."&index=_N_&code=qty&ftwarehouse_code=' + document.getElementById('ftwarehouse_codeD0_N_').value + '&ftlocation_code=' + document.getElementById('ftlocation_codeD0_N_').value+'&ftitem_code=' + document.getElementById('ftitem_codeD0_N_').value+ '&fdexp_date=' + document.getElementById('fdexp_dateD0_N_').value+ '&objQty=fnqty_availableD0_N_');\"";
    //$ajaxQtyAvail=" onChange=\"ajax_do('query2.php?formx=".$_REQUEST['form']."&index=_N_&code=qty&ftwarehouse_code=' + document.getElementById('ftwarehouse_codeD0_N_').value + '&ftlocation_code=' + document.getElementById('ftlocation_codeD0_N_').value+'&ftitem_code=' + document.getElementById('ftitem_codeD0_N_').value+ '&fdexp_date=' + document.getElementById('fdexp_dateD0_N_').value+ '&objQty=fnqty_availableD0_N_');\"";
    $ajaxQtyAvail=" onChange=\"ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=avail&index=_N_&ftlocation_code=' + document.getElementById('ftlocation_codeD0_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('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=avail&index=_N_&ftlocation_code=' + document.getElementById('ftlocation_codeD0_N_').value+'&ftitem_code=' + document.getElementById('ftitem_codeD0_N_').value+ '&fdexp_date=' + document.getElementById('fdexp_dateD0_N_').value+ '&objQty=fnqty_availableD0_N_');\"";
    $ajaxGudang="OnChange=\"ajax_do('inventory.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_&index=_N_');ElementCurrencyALL();\"";
    $ajaxFormula="ajax_do('inventory.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->GridSQL(0, "SELECT fnindex, ftitem_code, fdexp_date, ftwarehouse_code, ftlocation_code, fnqty_available, fnqty_opname, fnqty, ftuom_code, fnqty_base, ffformula,ftdescription,fcprice,'' as ftactive FROM TXStock_Adjustment_Det");
    $objAxpDB->GridCaption(0, "No., Kode Barang, Exp. Date, Gudang, Lokasi, Qty Avail, Qty Opname, Qty, Satuan, Qty Base, Formula, Keterangan, Harga HPP, Exp. Date Status");
    $objAxpDB->GridHidden(0,'0,0,1,1,0,0,0,0,0,1,1,0,0,1');
    $objAxpDB->GridReindex(0, false);
    
    $objAxpDB->GridScript(0,1, $ajaxOUM_CodeAndQtyAvail);
    $objAxpDB->GridScript(0,3, $ajaxGudang);
    $objAxpDB->GridScript(0,4, $ajaxQtyAvail);    
    $objAxpDB->GridScript(0,6,"onChange=\"StockPenyesuaianRecalQty1(_N_);{$ajaxFormula}ElementCurrencyALL();\"");
    
    $objAxpDB->LookupMagicQuotes(TRUE);
    $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, CONCAT(TRIM(ftitem_name),CASE WHEN TRIM(IFNULL(ftpart_no,'')) != '' THEN ' ,PART NO: ' ELSE '' END,IFNULL(ftpart_no,'')) as ftitem_name  FROM tlstock_hdr $m ", "ftitem_code", "ftitem_name", -1,false);
    $objAxpDB->Lookup("ftwarehouse_code", "SELECT ftwarehouse_code, ftwarehouse_name FROM tlwarehouse where fnstatus = 1", "ftwarehouse_code", "ftwarehouse_name",-1,true);
    $objAxpDB->Lookup("ftlocation_code", "SELECT ftlocation_code, ftlocation_name FROM tllocation a inner join tlwarehouse b on a.ftwarehouse_code = b.ftwarehouse_code where a.fnstatus = 1", "ftlocation_code", "ftlocation_name", -1,true,"' AND a.ftwarehouse_code=\''+ document.getElementById('ftwarehouse_code').value + '\''");
    $objAxpDB->Lookup("fncategory", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 9", "fntrans_status_code", "fttrans_status_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->SetStatus("id, fnstatus");
    if ($objAxpDB->GetStatus("id") == "") {
        $objAxpDB->ButtonShow("baru");
        $html="";
        $objAxpDB->GridReplace(0,$html);
    }
    elseif ($objAxpDB->GetStatus("id") == 0) {
        $objAxpDB->GridAllowAddNew(0,true);
        $objAxpDB->GridAllowDelete(0,true);
        $objAxpDB->GridLocked(0, "1, 0, 0, 0, 0, 1, 0, 1,1,1,1,0,0");
        $objAxpDB->ButtonShow("reset, simpan, tutup");
        $objAxpDB->FormLocked("1, 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->GridLocked(0, "1, 1, 1, 1, 1, 1, 0, 1,1,1,1,0,1");
        $objAxpDB->ButtonShow("reset, update,final, tutup");
        $objAxpDB->FormLocked("1, 1, 0,0, 1");
  
    } elseif ($objAxpDB->GetStatus("fnstatus") == 2) {
        $objAxpDB->ButtonShow("cetak, batal, tutup");
        $objAxpDB->GridLocked(0, "1, 1, 1, 1, 1, 1, 1, 1,1,1,1,1,1");
        $objAxpDB->FormLocked("1, 1,1, 1, 1");
       
    } elseif ($objAxpDB->GetStatus("fnstatus") == 9) {
        $objAxpDB->FormLocked("1, 1,1, 1, 1");
        $objAxpDB->GridLocked(0, "1, 1, 1, 1, 1, 1, 1, 1,1,1,1,1,1");
        $objAxpDB->ButtonShow("tutup");
    }

    $objAxpDB->GridHidden(1,'1');
    
         if($_REQUEST['ajax']) {
           switch($_REQUEST['code']) {
           case "grid":
                /*
               ob_start();
               
                       $ssql="SELECT a.fnid as fnindex, b.ftitem_code, a.ftwarehouse_code, a.ftlocation_code,a.fnqty_on_hand as fnqty_available, ifnull(a.fnqty_on_hand,0)-ifnull(a.fnqty_on_hand,0) as fnqty_opname , 0-ifnull(a.fnqty_on_hand,0) as fnqty, b.ftuom_code,'' as ftdescription
                                from tlstock_hdr b left join tlstock_det a on a.ftitem_code=b.ftitem_code
                                where b.fnstatus=1 and  B.ftstock_category_code='inv' and b.ftstock_subgroup_code='".$_REQUEST['ftsubgroup_code']."'
                                order by  b.ftitem_code
                                   #                               ";
                        $objAxpDB->GridSQL(0,$ssql);
                       
                        $objAxpDB->GridAllowAddNew(0,true);
                        $objAxpDB->GridAllowDelete(0,true);
                      
                        $objAxpDB->GridCaption(0, "No., Kode Barang, Gudang, Lokasi, Qty Avail, Qty Opname, Qty, Satuan, Keterangan");
                        $objAxpDB->GridLocked(0, "1, 0, 0, 0, 1, 0, 1,1,0");

               $objAxpDB->Process();

               $objAxpDB->setGrid2(0);
               //pr($objAxpDB->arrGrid);
               $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();
               <?

               break;
               */
           }
           die("");
           exit;
    }

    $objAxpDB->Process();
    if($x==1) {
        ob_start();

        $objAxpDB->setGrid2(0);
        $html = ob_get_contents();
        ob_end_clean();
        $objAxpDB->GridReplace(0,$html);
    }
   
    $objAxpDB->WriteHtmlTable();

?>