<?php
Getsecurity("disassembly");
    
if($_REQUEST['ajax']) {
    switch($_REQUEST['code']) {
        // cek dan validasi no transaksi sebelum simpan    
        case "transno":
            if(RetRstField("SELECT fnid FROM txdisassembly_hdr WHERE ftdisassembly_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 "lokasi_hdr":
            $ssql="SELECT ftwarehouse_code FROM tllocation 
                    WHERE ftlocation_code='{$_REQUEST['ftlocation_code']}'
                    ";
            $ftwarehouse_code=RetRstField($ssql);

            ?>
                document.getElementById('ftwarehouse_code').value='<?php echo $ftwarehouse_code; ?>';
                ajax_do('query2.php?code=qty&formx=disassembly&ftwarehouse_code=<?php echo $ftwarehouse_code; ?>&ftlocation_code=<?php echo $_REQUEST['ftlocation_code']; ?>&ftitem_code=' + document.getElementById('ftitem_code').value+ '&objQty=fnqty_available');
                ElementCurrencyALL();            
            <?php

            die("");
            break;      
        
        case "lokasi_det":
            $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; ?>';
                ElementCurrencyALL();     
            <?php

            die("");
            break;  
        
        case "price_det":
            $fcnet_price = GetLastPriceValue($_SESSION['strbranch'],$_REQUEST['ftitem_code']);
            if ($fcnet_price==0) {
                $fcnet_price = RetRstField("SELECT fcbuying_price FROM tlbuying_price WHERE ftcurrency_code = 'IDR' AND ftitem_code = '{$_REQUEST['ftitem_code']}' ");
            }
            
            ?>
                document.getElementById('fcnet_priceD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $fcnet_price; ?>';
                document.getElementById('fnqtyD0<?php echo $_REQUEST['index']; ?>').value= 0;
                document.getElementById('ftlocation_codeD0<?php echo $_REQUEST['index']; ?>').value='';
                ElementCurrencyALL();            
            <?php

            die("");
            break;   
        
        case "total_netprice":
            // Apabila update harga dipilih baru dijalankan
            ?>
            var i=document.getElementsByName('fnindexD0[]').length;

            var fnqtyD=0;
            var fcpriceD=0;
            var fcamountD=0;
            
            for(var j=0; j < i; j++) {
                fnqtyD = document.getElementsByName('fnqtyD0[]')[j].value.replace(/,/g,'')*1;
                fcpriceD=document.getElementsByName('fcnet_priceD0[]')[j].value.replace(/,/g,'')*1;
                fcamountD=(fnqtyD*fcpriceD);
                document.getElementsByName('fctotal_priceD0[]')[j].value = fcamountD; 
            }

            ElementCurrencyALL();
            <?php 

            die("");
            break;  
        
        /* Tidak digunakan ?
        case "harga_modal":
            $fcnet_price = GetLastPriceValue($_SESSION['strbranch'],$_REQUEST['ftitem_code']);
            
            // Ambil harga dari HPP
            // if (document.getElementById('fnupdate_price').value == '0') { 
            ?>
                document.getElementById('fcnet_price').value='<?php echo $fcnet_price; ?>';
                ElementCurrencyALL();
            <?php
            // } 
            
            die("");
            break;       */
        
        case "lokasi_revisi":
            $ssql="SELECT ftwarehouse_code FROM tllocation 
                    WHERE ftlocation_code='{$_REQUEST['ftlocation_code']}'
                    ";
            $ftwarehouse_code=RetRstField($ssql);

            $ssql="SELECT ifnull(fnqty,0) * -1 AS fnqty FROM txdisassembly_hdr WHERE ftdisassembly_no='{$_REQUEST['ftdisassembly_no']}'  
                    ";
            $fnqty=RetRstField($ssql);
            if ($fnqty=='') { $fnqty = 0; }

            ?>
            document.getElementById('ftwarehouse_code').value='<?php echo $ftwarehouse_code; ?>';
            ajax_do('query2.php?code=qty&ftwarehouse_code=<?php echo $ftwarehouse_code; ?>&ftlocation_code=<?php echo $_REQUEST['ftlocation_code']; ?>&ftitem_code=' + document.getElementById('ftitem_code').value+ '&objQty=fnqty_available&qty_revisi=<?php echo $fnqty; ?>');
            <?php

            die("");
            break;
                
        case "qty_available_revisi":
            ?>
            ajax_do('inventory.php?form=<?php echo $_REQUEST['form']; ?>&mode=<?php echo $_REQUEST['mode']; ?>&id=0&ajax=1&code=lokasi_revisi&index=1&ftlocation_code='+document.getElementById('ftlocation_code').value + '&ftdisassembly_no=' + document.getElementById('ftdisassembly_no').value + '&ftitem_code='+ document.getElementById('ftitem_code').value);            
            <?php
            break;       
    }
    die("");
    exit;
}
     
$objAxpDB->ViewName("Daftar Disassembly");

$objAxpDB->ViewSQL("SELECT a.fnid, a.ftdisassembly_no, a.fdtrans_date,a.ftitem_code,b.ftitem_name,a.fnqty,a.ftlocation_code,  
                    (SELECT UCASE(fttrans_status_name) FROM tstransaction_status WHERE fntrans_group_code = 2 AND fntrans_status_code=a.fnstatus) AS fnstatus
                    FROM txdisassembly_hdr a LEFT JOIN tlstock_hdr b ON a.ftitem_code = b.ftitem_code ORDER BY a.ftdisassembly_no DESC");
$objAxpDB->ViewCaption("ID, No. Disassembly, Tanggal, Kode Barang, Nama Barang, Qty, Lokasi, Status");
$objAxpDB->ViewPeriod(TRUE);

$objAxpDB->FormName("Disassembly");
$objAxpDB->FormSQL("SELECT ftdisassembly_no, ftitem_code AS ftitem_code, fcnet_price, fdtrans_date, ftlocation_code, ftwarehouse_code, fnqty_available, fnqty, ftreff_no, ftnotes, fnstatus FROM txdisassembly_hdr");
$objAxpDB->FormCaption("No. Disassembly, Kode Barang, Harga Modal Satuan, Tanggal, Kode Lokasi, Kode Gudang, Qty Available, Qty, No Referensi, Catatan, Status");
$objAxpDB->FormPosition("TL, TL, TL, TR, TR, TR, TL, TR, TL, BL, BR");

$objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code, ftlocation_code, ftwarehouse_code, fnqty, ftnotes, fcnet_price, fctotal_price FROM txdisassembly_det");
$objAxpDB->GridHidden(0,'0,0,0,1,0,0,0,0');
$objAxpDB->GridCaption(0, "No., Kode Barang, Lokasi, Gudang, Qty, Keterangan, Harga Modal, Total Harga");
$objAxpDB->GridSize(0, ",,10,10,10,,10,10");

$ajaxLokasiHdr="OnChange=\"ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=lokasi_hdr&ftlocation_code='+this.value+'&ftitem_code='+document.getElementById('ftitem_code').value);\"";
$objAxpDB->FormScript(4, $ajaxLokasiHdr);

$ajaxPriceDet="OnChange=\"ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=price_det&index=_N_&ftitem_code='+this.value);\"";
// $ajaxResetQty="OnChange=\"document.getElementById('fnqtyD0_N_').value= 0;document.getElementById('ftlocation_codeD0_N_').value='';\"";
$objAxpDB->GridScript(0,1, $ajaxPriceDet);

$ajaxLokasiDet="OnChange=\"ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=lokasi_det&index=_N_&ftlocation_code='+this.value);\"";
$objAxpDB->GridScript(0,2, $ajaxLokasiDet);

$ajaxNetPrice="OnChange=\"ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=total_netprice&index=_N_');\"";
$objAxpDB->GridScript(0,4, $ajaxNetPrice);
$objAxpDB->GridScript(0,6, $ajaxNetPrice);

if(!$_REQUEST['id']) {
    $strAutoNumber1=" ajax_do('query2.php?code=2&ftmodule_code=invsp007&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftdisassembly_no&no_trans=ftdisassembly_no&table_name=txdisassembly_hdr&ftbranch_code={$_SESSION['strbranch']}');";
    $objAxpDB->FormJS($strAutoNumber1);
    $m=" WHERE ftstock_category_code='inv' AND fnstatus=1 ORDER BY ftitem_name,ftpart_no "; 
         
} else {
    if(isset($_REQUEST['revisi'])) {
        // reload qty available
        $ajax_qty="ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=qty_available_revisi');";
        $objAxpDB->FormJS($ajax_qty); 
        $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->LookupMagicQuotes(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 ORDER BY a.ftwarehouse_code, a.ftlocation_code ", "ftlocation_code", "ftlocation_name", -1,true,"' AND a.ftwarehouse_code=\''+ document.getElementById('ftwarehouse_code').value + '\''");
$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, CONCAT(TRIM(ftitem_name),' PART NO: ',ftpart_no) as ftitem_name FROM tlstock_hdr $m ", "ftitem_code", "ftitem_name", -1,false);
$objAxpDB->LookupGrid(0,"ftitem_code", "SELECT ftitem_code, CONCAT(TRIM(ftitem_name),' PART NO: ',ftpart_no) as ftitem_name  FROM tlstock_hdr $m ", "ftitem_code", "ftitem_name", -1,false);    

$objAxpDB->SetStatus("id, fnstatus");
    
if ($objAxpDB->GetStatus("id") == "")
    $objAxpDB->ButtonShow("baru");

elseif ($objAxpDB->GetStatus("id") == 0) {
    $objAxpDB->FormLocked("1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1");
    $objAxpDB->GridLocked(0,'1, 0, 0, 1, 0, 0, 0, 1');
    $objAxpDB->GridAllowAddNew(0,true);
    $objAxpDB->GridAllowDelete(0,true);
    $objAxpDB->ButtonShow("reset, simpan, tutup");
}

// Set Tombol utk transaksi dengan status 1 (posted)                    
elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
    // revisi
    if(isset($_REQUEST['revisi'])) {        
        $objAxpDB->FormLocked("1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1");
        $objAxpDB->GridLocked(0,'1, 0, 0, 1, 0, 0, 0, 1');
        $objAxpDB->GridAllowAddNew(0, TRUE);
        $objAxpDB->GridAllowDelete(0, TRUE); 

        $ajaxLokasiRevisi="OnChange=\"ajax_do('inventory.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=lokasi_revisi&index=_N_&ftlocation_code='+this.value + '&ftdisassembly_no=' + document.getElementById('ftdisassembly_no').value + '&ftitem_code='+ document.getElementById('ftitem_code').value);\"";
        $objAxpDB->FormScript(4, $ajaxLokasiRevisi);

        $objAxpDB->ButtonShow("simpan_revisi,tutup");
    } else {   
        $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
        $objAxpDB->GridLocked(0,'1, 1, 1, 1, 1, 1, 1, 1');
        $objAxpDB->ButtonShow("baru, cetak, revisi, batal, tutup");     
    }       
}

elseif (($objAxpDB->GetStatus("fnstatus") == 3) || ($objAxpDB->GetStatus("fnstatus") == 5) || ($objAxpDB->GetStatus("fnstatus") == 9)) {
    $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
    $objAxpDB->GridLocked(0,'1, 1, 1, 1, 1, 1, 1, 1');
    $objAxpDB->ButtonShow("baru, tutup");
}

$objAxpDB->Process();
$objAxpDB->WriteHtmlTable();
    
?>    