<?php
     if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            
            case "transno":
              if(RetRstField("SELECT fnid FROM txinnitial_stock_hdr_sparepart WHERE ftstock_in_no='{$_GET['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='{$_GET['ftwarehouse_code']}'";
                $rs=mysql_query($ssql);
                   ?>

                        var ObjSelect=document.getElementById('<?php echo $_GET['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 "oum":
                  $ssql="SELECT ftuom_code FROM tlstock_hdr_sparepart 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']; ?>';
                 
                      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
            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; ?>';
                <?

                die("");
            break;
        }
        
    }
    
    Getsecurity('saldoawal_stock_sparepart');

    $objAxpDB->ViewName("Daftar Saldo Awal Stock Sparepart");
    
    $ssql="SELECT A.fnid,  A.ftstock_in_no,  A.fdtrans_date, A.fntotal_qty, A.ftnotes, B.fttrans_status_name
                FROM txinnitial_stock_hdr_sparepart
                A INNER JOIN tstransaction_status B ON a.fnstatus=b.fntrans_status_code AND B.fntrans_group_code=2";

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

    $objAxpDB->ViewCaption("ID,No. Transaksi,  Tgl. Transaksi, Total Qty, Catatan, Status");

     if(!$_GET['id']) {
            $strAutoNumber1="ajax_do('query2.php?code=2&ftmodule_code=tn126&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftstock_in_no&no_trans=ftstock_in_no&table_name=txinnitial_stock_hdr_sparepart');";
            $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                                           {$strAutoNumber1}
                                     }

                              OnChangeDate_fdtrans_date();
                                       ";
            $objAxpDB->FormJS($strAutoNumber0);
     }

    $objAxpDB->FormName("Saldo Awal Stock Sparepart");
    $objAxpDB->FormSQL("SELECT ftstock_in_no, ftstock_type_code, ftstock_in_company, ftdocument_no, fdtrans_date, ftdelivery_no, ftchecked_user, ftnotes,  fntotal_qty, fnstatus FROM txinnitial_stock_hdr_sparepart");
    $objAxpDB->FormCaption("No. Stock Masuk, Tipe Stock , Supplier/Customer, No. Document, Tgl. Masuk, No. Surat Jalan, Pengecek Barang, Catatan,  Total Qty, Status");
    $objAxpDB->FormPosition("TL, TL, HD, HD, TR, HD, HD, BL, BR, BR");

    //$ajaxGrid1="ajax_do_grid0('saldoawal.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=grid&ftstock_type_code='+ document.getElementById('ftstock_type_code').value);";
    //$objAxpDB->FormScript(1, "onChange=\"{$ajaxGrid1}\"");
    
    $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code, ftwarehouse_code, ftlocation_code, fnqty, ftuom_code, fcprice, fnqty_base, ffformula
                           FROM txinnitial_stock_det_sparepart");
    $objAxpDB->GridCaption(0, "No., Kode Barang, Gudang, Lokasi, Qty, Satuan, Harga HPP, Qty, Formula");

    $ajaxOUM_Code="OnChange=\"ajax_do('saldoawal.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=oum&ftstock_type_code='+ document.getElementById('ftstock_type_code').value+'&ftitem_code='+this.value+'&ftuom_code=ftuom_codeD0_N_&ObjGudang=ftwarehouse_codeD0_N_&ObjLokasi=ftlocation_codeD0_N_');\"";
    $objAxpDB->GridScript(0,1,$ajaxOUM_Code);

    $ajaxGudang="OnChange=\"ajax_do('saldoawal.php?form=".$_GET['form']."&mode=".$_GET['mode']."&id=0&ajax=1&code=gudang&ftwarehouse_code='+this.value+'&ObjLokasi=ftlocation_codeD0_N_');\"";
    $objAxpDB->GridScript(0,2, $ajaxGudang);

    $ajaxFormula="ajax_do('saldoawal.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,4,"onChange=\"SaldoAwalStockRecalQtySparepart();{$ajaxFormula}\"");
    $objAxpDB->GridScript(0,6,"onChange=\"SaldoAwalStockRecalQtySparepart();{$ajaxFormula}\"");
    $objAxpDB->GridDelJS(0,"SaldoAwalStockRecalQtySparepart();");
     
    // cek stock type code
    $ftstock_type_code="";
    $ssql="SELECT ftstock_type_code FROM txinnitial_stock_hdr_sparepart WHERE fnid='{$_GET['id']}'";
    $rs=mysql_query($ssql);
    $r=mysql_fetch_assoc($rs);
    $ftstock_type_code=$r['ftstock_type_code'];
    
    $tablename = "_sparepart";
    $fnwarehouse_type = '4';

    $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{$tablename} where fnstatus = 1 ", "ftitem_code", "ftitem_name",-1,false);
    $objAxpDB->Lookup("ftwarehouse_code", "SELECT ftwarehouse_code, ftwarehouse_name FROM tlwarehouse where fnwarehouse_type=0 or fnwarehouse_type={$fnwarehouse_type} ", "ftwarehouse_code", "ftwarehouse_name",-1,true);
    $objAxpDB->Lookup("ftlocation_code", "SELECT ftlocation_code, ftlocation_name FROM tllocation where 0=0 ", "ftlocation_code", "ftlocation_name", -1,true,"' AND ftwarehouse_code=\''+ document.getElementById('ftwarehouse_code').value + '\''");
    $objAxpDB->Lookup("ftstock_type_code", "SELECT ftstock_type_code, ftstock_type_name FROM tlstock_type WHERE ftstock_type_code = 'invsp' ", "ftstock_type_code", "ftstock_type_name", 0,true);
    
    $objAxpDB->SetStatus("id, fnstatus,ftstock_type_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,1,0,0,1,1');
            $objAxpDB->ButtonShow("reset, simpan, tutup");
            $objAxpDB->FormLocked("0, 1, 0, 0, 0, 0, 0, 0, 1, 1");

    } elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
            // $objAxpDB->ButtonShow("cetak, baru, update, batal, tutup");
            $objAxpDB->ButtonShow("cetak, baru, batal, tutup");
            $objAxpDB->FormLocked("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") == 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");
            $objAxpDB->ButtonShow("tutup");
    }

    if($x==1) {
           $objAxpDB->GridLocked(0,'1,1,1,1,0,1,1,1,1');
    }

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

    if($_REQUEST['ajax'])
	{
		switch($_REQUEST['code'])
		{
			case "grid": 
                            ob_start();
                            $_REQUEST['id']=0;
                           
                            $tablename = "_sparepart";
                            $fnwarehouse_type = '4';

                            $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr{$tablename} where fnstatus = 1 ", "ftitem_code", "ftitem_name",-1,false);
                            $objAxpDB->Lookup("ftwarehouse_code", "SELECT ftwarehouse_code, ftwarehouse_name FROM tlwarehouse where fnwarehouse_type=0 or fnwarehouse_type={$fnwarehouse_type} ", "ftwarehouse_code", "ftwarehouse_name",-1,true);
                            $objAxpDB->Lookup("ftlocation_code", "SELECT ftlocation_code, ftlocation_name FROM tllocation where 0=0 ", "ftlocation_code", "ftlocation_name", -1,true,"' AND ftwarehouse_code=\''+ document.getElementById('ftwarehouse_code').value + '\''");

                            $ajaxOUM_Code="OnChange=\"ajax_do('saldoawal.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=oum&ftstock_type_code='+ document.getElementById('ftstock_type_code').value+'&ftitem_code='+this.value+'&ftuom_code=ftuom_codeD0_N_&ObjGudang=ftwarehouse_codeD0_N_&ObjLokasi=ftlocation_codeD0_N_');\"";
                            $objAxpDB->GridScript(0,1,$ajaxOUM_Code);

                            $ajaxGudang="OnChange=\"ajax_do('saldoawal.php?form=".$_GET['form']."&mode=".$_GET['mode']."&id=0&ajax=1&code=gudang&ftwarehouse_code='+this.value+'&ObjLokasi=ftlocation_codeD0_N_');\"";
                            $objAxpDB->GridScript(0,2, $ajaxGudang);

                            $objAxpDB->GridScript(0,4,"onChange='SaldoAwalStockRecalQtySparepart();'");
                            $objAxpDB->GridScript(0,6,"onChange='SaldoAwalStockRecalQtySparepart();'");
                            $objAxpDB->GridDelJS(0,"SaldoAwalStockRecalQtySparepart();");

                            $objAxpDB->GridSQL(0, " SELECT fnindex, ftitem_code, ftwarehouse_code, ftlocation_code, fnqty, ftuom_code FROM txinnitial_stock_det_sparepart ");
                            $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;
                                RowBuild0();
                            <?  
                            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();