<?php

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

                                   } ";

                        }
                         echo " try {
                                    //  ObjSelect.selectedIndex=1;
                                  } catch(ex) {

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

            case "formula":
                $ssql="SELECT IFNULL(SUM(ffformula),1) as ffformula
                        FROM tlstock_multi_det_sparepart
                        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("inventorysp_stock_penyesuaian");
    
    $objAxpDB->LookupMagicQuotes(True);
    $objAxpDB->ViewName("Daftar Penyesuaian Barang Penyesuaian");
    $objAxpDB->ViewSQL("SELECT A.fnid,  A.ftstock_adjustment_no, A.fdtrans_date,A.ftnotes, B.fttrans_status_name
                         FROM txstock_adjustment_hdr_sparepart
                        A INNER JOIN tstransaction_status B ON a.fnstatus=b.fntrans_status_code AND B.fntrans_group_code=2
                        ORDER BY  A.ftstock_adjustment_no DESC, A.fdtrans_date DESC");
    $objAxpDB->ViewCaption("ID,  No. transaksi, Tgl. transaksi, Catatan, Status");
    $objAxpDB->ViewPeriod(TRUE);
    if(!$_REQUEST['id']) {
        $strAutoNumber1="ajax_do('query2.php?code=2&ftmodule_code=invsp004&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftstock_adjustment_no&no_trans=ftstock_adjustment_no&table_name=txstock_adjustment_hdr_sparepart&ftbranch_code={$_SESSION['strbranch']}');";
        $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                                       {$strAutoNumber1}
                                  }
                                  function StockMasukPembelian_warehouse_onchange() {
                                        var i=document.getElementsByName('fnindexD0[]').length;

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

                                        }
                                    }
                                OnChangeDate_fdtrans_date();
                            ";

        $objAxpDB->FormJS($strAutoNumber0);

        $ajaxGrid="ajax_do_grid0('inventory_sparepart.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=grid&ftgroup_code='+ document.getElementById('ftgroup_code').value);";

        $ajax1=" OnClick=\"{$ajaxGrid}\"";
        //$button="<input type='button' id='cmd_employee_list' name='cmd_employee_list' value='LIHAT' {$ajax1}>";
        //$objAxpDB->FormLayout(1,"{$button}");
        $objAxpDB->FormScript(1, "onChange=\"{$ajaxGrid}\"");
        
        $m=" WHERE fnstatus=1 ";
     }

    $objAxpDB->FormName("Penyesuaian Barang Sparepart");
    $objAxpDB->FormSQL("SELECT ftstock_adjustment_no,ftgroup_code,fdtrans_date, ftnotes,  fnstatus FROM txstock_adjustment_hdr_sparepart");
    $objAxpDB->FormCaption("No. transaksi,Group, Tgl. transaksi, Catatan,  Status");
    $objAxpDB->FormPosition("TL,HD,TR, BL, BR");

    $ajaxOUM_Code="ajax_do('query2.php?code=uom_sparepart&obj1=' + this.value + '&obj2=ftuom_codeD0_N_');";
    $ajaxExp_Date="ajax_do('query2.php?code=exp_datesp&obj1=' + this.value + '&obj2=ftactiveD0_N_');";
    $ajaxQtyAvail=" onChange=\"ajax_do('query2.php?formx=".$_REQUEST['form']."&index=_N_&code=qty_sparepart&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_');\"";
    $ajaxOUM_CodeAndQtyAvail=" onChange=\"{$ajaxOUM_Code}{$ajaxExp_Date};ajax_do('query2.php?formx=".$_REQUEST['form']."&index=_N_&code=qty_sparepart&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('inventory_sparepart.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_');\"";
    $ajaxFormula="ajax_do('inventory_sparepart.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,1, $ajaxOUM_CodeAndQtyAvail);
    $objAxpDB->GridScript(0,3, $ajaxGudang);
    $objAxpDB->GridScript(0,4, $ajaxQtyAvail);
    $objAxpDB->GridScript(0,6,"onChange=\"StockPenyesuaianRecalQty1(_N_);{$ajaxFormula}\"");
    $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_sparepart");
    $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->Lookup( "ftwarehouse_code", "SELECT ftwarehouse_code, ftwarehouse_name FROM tlwarehouse where fnwarehouse_type = 0 or fnwarehouse_type = 4 ", "ftwarehouse_code", "ftwarehouse_name", -1,true);
    $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( "ftstock_out_company", "SELECT ftcustomer_code, ftcustomer_name FROM tlcustomer", "ftcustomer_code", "ftcustomer_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 ftitem_code, ftitem_name as ftitem_name FROM tlstock_hdr_sparepart $m ", "ftitem_code", "ftitem_name", -1,false);
    $objAxpDB->Lookup( "ftlocation_code", "SELECT ftlocation_code, ftlocation_name FROM tllocation where 0=0 ", "ftlocation_code", "ftlocation_name", -1,true);
    $objAxpDB->Lookup( "ftgroup_code", "SELECT 'SELURUH' AS  ftstock_group_code, 'SELURUH' AS ftstock_group_name UNION ALL (SELECT ftstock_group_code,ftstock_group_name FROM tlstock_group_sparepart ORDER BY ftstock_group_code) ", "ftstock_group_code", "ftstock_group_name", -1,false);

    $objAxpDB->SetStatus("id, fnstatus");
    if ($objAxpDB->GetStatus("id") == "") {
            $objAxpDB->ButtonShow("baru,  cetak_daftar, export_excel , tutup");
            $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, 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->GridAllowAddNew(0,true);
            $objAxpDB->GridAllowDelete(0,true);
            $objAxpDB->GridLocked(0, "1, 0, 0, 0, 0, 1, 0, 1,1,1,1,0,1");
            $objAxpDB->ButtonShow("cetak,batal,update,final, tutup");
            $objAxpDB->FormLocked("1, 1,1, 0,1, 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, 1");
    }
    elseif ($objAxpDB->GetStatus("fnstatus") == 9) {
              $objAxpDB->FormLocked("1, 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(0, "0, 0, 0, 0, 0, 0, 0, 0,0,1,1,0,0,1");
    $objAxpDB->GridHidden(1, "1");

    if($_REQUEST['ajax']) {
           switch($_REQUEST['code']) {
           /*case "grid":
                $_REQUEST['id']=0.5;
                $where=" 1=1 ";
                if($_REQUEST['ftgroup_code']!="SELURUH") {
                   $where.=" AND b.ftstock_group_code='".$_REQUEST['ftgroup_code']."'";
                }

                ob_start();

               $ssql="SELECT a.fnid as fnindex, b.ftitem_code,replace(a.ftwarehouse_code,a.ftwarehouse_code,'') as ftwarehouse_code, replace(a.ftlocation_code,a.ftlocation_code,'') as ftlocation_code,sum(a.fnqty_on_hand)-sum(a.fnqty_on_hand) as fnqty_available, sum(a.fnqty_on_hand)-sum(a.fnqty_on_hand) as fnqty_opname , sum(a.fnqty_on_hand)-sum(a.fnqty_on_hand)  as fnqty, b.ftuom_code,'' as ftdescription
                        from tlstock_hdr_sparepart b
                                left join tlstock_det_sparepart a on a.ftitem_code=b.ftitem_code
                                LEFT JOIN tlstock_group_sparepart c ON b.ftstock_group_code=c.ftstock_group_code
                        where {$where}
                        group 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);
               $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();
                    TransactionDetailRowID0 =document.getElementsByName('fnindexD0[]').length-1;
               <?

               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();



?>
