<?php

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

                     } ";
                }
                break;
            case "oum":
                $ssql="SELECT ftuom_code,ftstock_category_code FROM tlstock_hdr 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
                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; ?>';
                <?

                die("");
                break;
            
            case "price":
                
                $fcPrice = GetLastPriceValue("{$_SESSION['strbranch']}","{$_REQUEST['ftitem_code']}");

                if ($fcPrice == 0) {
                    $fcPrice = RetRstField("SELECT fcbuying_price FROM tlbuying_price WHERE ftcurrency_code = 'IDR' AND ftitem_code = '{$_REQUEST['ftitem_code']}' ");
                    if ($fcPrice == '') $fcPrice = 0;         
                }
                
                ?>
                document.getElementById('fcpriceD0<?  echo $_REQUEST['index']; ?>').value='<?php echo CCur($fcPrice); ?>';
                ElementCurrencyALL();   
                <?php    
                
                die("");
                break;  
        }
        die("");
    }

    Getsecurity('saldoawal_stock');

    $objAxpDB->ViewName("Daftar Saldo Awal Stock");
    $ssql="SELECT A.fnid,  A.ftstock_in_no,  A.fdtrans_date, A.fntotal_qty, A.ftnotes, B.fttrans_status_name
                        FROM txinnitial_stock_hdr
                        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=tn023&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftstock_in_no&no_trans=ftstock_in_no&table_name=txinnitial_stock_hdr');";
        $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                                       {$strAutoNumber1}
                                 }

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

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

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

    $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code, ftwarehouse_code, ftlocation_code, fnqty, ftuom_code, fcprice, fnqty_base, ffformula FROM txinnitial_stock_det");
    $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&ftitem_code='+this.value+'&ftuom_code=ftuom_codeD0_N_&ObjGudang=ftwarehouse_codeD0_N_&ObjLokasi=ftlocation_codeD0_N_');\"";
    $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_');\"";
    $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);";
    $ajaxPrice="OnChange=\"ajax_do('saldoawal.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=price&index=_N_&ftitem_code='+document.getElementById('ftitem_codeD0_N_').value);\"";
    
    $objAxpDB->GridScript(0,1,$ajaxOUM_Code);
    $objAxpDB->GridScript(0,2, $ajaxGudang);
    $objAxpDB->GridScript(0,3, $ajaxPrice);
    $objAxpDB->GridScript(0,4,"onChange=\"SaldoAwalStockRecalQty();{$ajaxFormula}\"");
    $objAxpDB->GridScript(0,6,"onChange=\"SaldoAwalStockRecalQty();{$ajaxFormula}\"");
    $objAxpDB->GridDelJS(0,"SaldoAwalStockRecalQty();");
     
    $objAxpDB->Lookup( "ftcategory_code", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 3", "fntrans_status_code", "fttrans_status_name", -1,true);

    $ftcategory_code="";
    $ssql="SELECT ftcategory_code FROM txinnitial_stock_hdr WHERE fnid='{$_GET['id']}'";
    $rs=mysql_query($ssql);
    $r=mysql_fetch_assoc($rs);
    $ftcategory_code=$r['ftcategory_code'];

    if($_GET['id']) {
        if($ftcategory_code=="0") {
            $objAxpDB->Lookup( "ftstock_in_company", "SELECT '' AS ftsupplier_code, ''  AS ftsupplier_name", "ftsupplier_code", "ftsupplier_name", -1,true);

        }
        elseif($ftcategory_code=="1") {
            $objAxpDB->Lookup("ftstock_in_company", "SELECT ftsupplier_code, ftsupplier_name FROM tlsupplier", "ftsupplier_code", "ftsupplier_name", -1,true);

        }
        elseif($ftcategory_code=="2") {
            $objAxpDB->Lookup("ftstock_in_company", "SELECT ftcustomer_code, ftcustomer_name FROM tlcustomer", "ftcustomer_code", "ftcustomer_name", -1,true);

        }
    }
    else {
        $objAxpDB->Lookup("ftstock_in_company", "SELECT '' AS ftsupplier_code, ''  AS ftsupplier_name", "ftsupplier_code", "ftsupplier_name", -1,true);
        #$m=" WHERE ftstock_category_code=\'inv\'";
    }
    $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 $m", "ftitem_code", "ftitem_name",-1,false);
    $objAxpDB->Lookup( "ftwarehouse_code", "SELECT ftwarehouse_code, ftwarehouse_name FROM tlwarehouse", "ftwarehouse_code", "ftwarehouse_name", -1,true);
    $objAxpDB->Lookup( "ftlocation_code", "SELECT ftlocation_code, ftlocation_name FROM tllocation", "ftlocation_code", "ftlocation_name", -1,true);

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

    } elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
            if($objAxpDB->GetStatus("ftcategory_code")==2) {
                $objAxpDB->ButtonShow("cetak, tutup");
            } else {
                // $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,1');
            $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
            $objAxpDB->ButtonShow("tutup");
    }

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

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

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

?>

