Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
5.9 kB
5
Indexable
Never
function getVal() {
    closeActiveCell_<?php echo $grid_id_item?>();
    var sourceWarehouseId = $('#browse_item_request_source_warehouse_id').val();
    console.log(sourceWarehouseId);
    if (sourceWarehouseId !== '') {
        // Kosongkan grid sekali saja sebelum menambah data
        $(<?=$grid_id_item?>_element).jqGrid('clearGridData');
        
        var url = "{{ route('get-grid-item-outgoing-item', ['id' => ':sourceWarehouseId']) }}"
        url = url.replace(':sourceWarehouseId', sourceWarehouseId);
        $.ajax({
            url: url,
            method: "POST",
            data: {
                sourceWarehouseId: sourceWarehouseId,
                columnHead: [
                    "item_request_id",
                    "item_request_code",
                    "item_id",
                    "item_code",
                    "item_name",
                    "qty_amount",
                    "qty_unit_id",
                    "qty_unit",
                    "content_unit",
                    "content_unit_id",
                    "destination_location_name",
                    "destination_location_id",
                    "remark",
                    "destination_warehouse_id",
                    "destination_warehouse",
                    "conversion2",
                    "conversion3",
                    "unit1_temp",
                    "unit2_temp",
                    "unit3_temp"
                ],
                _token: '{{ csrf_token() }}'
            },
            success: function(response) {
                console.log('Response:', response);
                if (response.status_code == 200) {
                    console.log(response.data.rows);
                    var idGenerate = 1; // Inisialisasi idGenerate
                    for (let data of response.data.rows) {
                        $(<?=$grid_id_item?>_element).jqGrid('addRowData', idGenerate, data);
                        var rowData = $(<?=$grid_id_item?>_element).jqGrid('getRowData', idGenerate);
                        var qtyAmountItem = jQuery(<?= $grid_id_item ?>_element).jqGrid('getCell', idGenerate, 'qty_amount');
                        rowData.qty_amount_temp = qtyAmountItem;
                        $(<?= $grid_id_item ?>_element).jqGrid('setRowData', idGenerate, rowData);
                        setConversionRateAndPack(idGenerate, 'transaction_warehouse_outgoing_item');
                        checkQtyMaterialRequest(idGenerate, 'transaction_warehouse_outgoing_item', sourceWarehouseId);
                        idGenerate++;
                    }
                } else {
                    console.log(response);
                }
            },
            error: function(error) {
                console.error('Error fetching grid data item:', error);
            }
        });
    }
}

// Fungsi untuk memeriksa jumlah kuantitas permintaan material
function checkQtyMaterialRequest(rowid, grid_id, sourceWarehouseId) {
    console.log('Memeriksa qtyAmountItem...');
    var rowData = $(<?= $grid_id_item ?>_element).jqGrid('getRowData', rowid);
    var qtyAmountItem = parseInt(jQuery(<?= $grid_id_item ?>_element).jqGrid('getCell', rowid, 'qty_amount'));
    var item_id = jQuery(<?= $grid_id_item ?>_element).jqGrid('getCell', rowid, 'item_id');

    if (qtyAmountItem !== undefined && qtyAmountItem !== null) {
        console.log('Item ID:', item_id);
        console.log('Qty Requested:', qtyAmountItem);
        console.log('Source Warehouse ID:', sourceWarehouseId);

        var url = "{{ route('get-stock-outgoing-item') }}";
        $.ajax({
            url: url,
            method: "GET",
            data: { item_id: item_id, sourceWarehouseId: sourceWarehouseId },
            success: function(response) {
                console.log('Response:', response);
                const locationStock = response.data.data;
                let currentRowId = rowid;
                let qtyToAllocate = qtyAmountItem;
                let newQty = 0;

                for (let stock of locationStock) {
                    let stockQty = parseInt(stock.quantity);
                    if (qtyToAllocate > stockQty) {
                        newQty = stockQty;
                        qtyToAllocate -= stockQty;
                    } else {
                        newQty = qtyToAllocate;
                        qtyToAllocate = 0;
                    }

                    console.log('new qty : ', newQty);
                    console.log('qty to allocate : ', qtyToAllocate);

                    if (newQty > 0) {
                        let newRowData = { ...rowData };
                        newRowData.qty_amount = newQty;
                        newRowData.source_location_name = stock.name;
                        newRowData.source_location_id = stock.id;

                        $(<?= $grid_id_item ?>_element).jqGrid('addRowData', currentRowId, newRowData);
                        setConversionRateAndPack(currentRowId, 'transaction_warehouse_outgoing_item');
                    }

                    currentRowId++;
                }

                if (qtyToAllocate > 0) {
                    let newRowData = { ...rowData };
                    newRowData.qty_amount = qtyToAllocate;
                    newRowData.source_location_name = "Stock Tidak Tersedia";
                    newRowData.source_location_id = null;

                    $(<?= $grid_id_item ?>_element).jqGrid('addRowData', currentRowId, newRowData);
                    setConversionRateAndPack(currentRowId, 'transaction_warehouse_outgoing_item');
                }
            },
            error: function(error) {
                console.error('Error fetching stock data:', error);
            }
        });
    }
}
Leave a Comment