DEV Community

Cover image for Creating a Seamless Warehouse Transfer Method
Imam Ali Mustofa
Imam Ali Mustofa

Posted on

Creating a Seamless Warehouse Transfer Method

Hey there, Software Freestyle Engineers!

Today, I want to share with you an exhilarating journey I recently embarked upon - a solo mission to solve the puzzling problem of "Creating a Transfer Method Between Warehouses" for Warehouse Management Applications. Picture this: multiple warehouses, each with varying quantities of goods, and the challenge of enabling smooth transfers between them.

As a Software Freestyle Engineer, I decided to take on this challenge single-handedly. Armed with my coding prowess and a sprinkle of curiosity, I dived headfirst into the task, eager to find an elegant and efficient solution.

The problem at hand was no small feat. Each warehouse contained a different quantity of goods, and devising a transfer method that could gracefully handle these variations was no walk in the park. Errors were lurking around every corner, ready to pounce on my code.

This coding adventure took place in the comfort of my home workspace, surrounded by my trusty keyboard and the warm glow of multiple monitors. Armed with my favorite snacks, I hunkered down to tackle this complex challenge.

The project unfolded over several weeks of late-night coding sessions and early morning debugging marathons. Time seemed to both fly by and stand still as I dived deep into the intricacies of the problem.

I love a challenge, and this problem presented an exciting opportunity to push the boundaries of my coding skills. Besides, enabling seamless transfers between warehouses is a valuable asset for any Warehouse Management Application, and I wanted to contribute something robust and reliable to the community.

The first hurdle I encountered was data inconsistency. Each warehouse stored goods in different formats and units, leading to discrepancies in calculations and handling. Figuring out a way to normalize the data was essential to ensure accuracy in the transfer process.

The second challenge was designing a transfer method that could adapt to various quantities of goods. Traditional approaches simply wouldn't cut it here. I needed a flexible solution that could accommodate both small transfers and massive stock movements without breaking a sweat.

I started by conducting a thorough analysis of the existing codebase and understanding the data structures. I then dived into designing a new algorithm that would handle the transfers more efficiently.

My approach involved creating a dynamic mapping system that could process warehouse data and establish a common ground for transfers. I focused on scalability, ensuring the method could handle warehouses with hundreds or thousands of unique items.

After countless trial-and-error sessions, numerous code revisions, and coffee pots emptied, I finally had a working prototype. The transfer method now flawlessly handled transfers between warehouses, gracefully accommodating varying quantities of goods.

Not only did this method save time and effort for warehouse managers, but it also eliminated the headaches caused by inconsistent data. The response from the coding community was overwhelming, with many expressing gratitude for the robust solution.

Taking on the challenge of creating a Transfer Method Between Warehouses has been an incredible journey of growth and learning. It's okay to face obstacles along the way. Embrace them, learn from them, and channel that energy into creating something meaningful. Our coding community thrives on collaboration and knowledge-sharing, so let's continue supporting each other in our coding endeavors.


And here is the code snippet metaphor look like, "It's messy but it's work!" (Note: It's just a piece of cake from a pile of cakes in the display case):

  • itemSourceDetail contains detail of item in origin warehouse.
  • formData contains the data that's collected by form

Check the item first!

const checkIsBarangExists = await findBarang(
    itemSourceDetail.item.produk.barang_nama
)

if (checkIsBarangExists) {
   // Update Data Already Exist in Dexie Database 
} else {
   // Add New Items in The Dexie Database That Want to Transfer
}
Enter fullscreen mode Exit fullscreen mode

Checks if the item to be transferred exists in the source warehouse. It does this by calling the findBarang function with the item's name (item.produk.barang_nama) as an argument. The result of this check is stored in the variable checkIsBarangExists.

Update Data Already Exist in Dexie Database

If the item exists in the source warehouse (checkIsBarangExists is truthy), the code proceeds with the transfer operation.

This code will calculates the total amount to be transferred (jumlah) by adding the sourceTransferAmout and sourceIsiJual values from the metadata of the existing item in the source warehouse.

const jumlah = checkIsBarangExists.metadata.sourceTransferAmout +
            checkIsBarangExists.metadata.sourceIsiJual
Enter fullscreen mode Exit fullscreen mode

Create unit definition between item source and destination. This operation will calculates the amount to be transferred in the destination unit by multiplying jumlah with the destinationRate. The result is rounded up to ensure that there are no fractional units.

const convertSourceUnitToDestination = Math.ceil(
    checkIsBarangExists.metadata.destinationRate * jumlah
)
Enter fullscreen mode Exit fullscreen mode

Then Get human-readable stock units using humanable function (This function also complicated like my mind 🀯)

const humanSource = await humanable({
    barangId: checkIsBarangExists.metadata.sourceBarangId,
    gudangId: Number(gudangId),
    jumlah: jumlah,
})
Enter fullscreen mode Exit fullscreen mode

β„Ή Translation:

  • barangId === itemId
  • gudangId === warehouseId
  • jumlah === amount

The data to be updated in the source and destination warehouses is prepared as an object updateData.

const updateData = {
    'metadata.sourceTransferAmout': jumlah,
    'metadata.humanSource': humanSource,
    'transfer_detail.jumlah_transfer': convertSourceUnitToDestination,
    'transfer_detail.humanDestination': humanDestination,
}
Enter fullscreen mode Exit fullscreen mode

Then calls the updateTransfer function to update the item's transfer details in the source warehouse. If the update is successful, it triggers a re-fetch of all current transfers and displays a success toast message 🟒. Otherwise, it shows an error toast message πŸ”΄.

const updated = await updateTransfer(checkIsBarangExists.id, updateData);
if (updated) {
    mutate("/dexie/get-all-current-transfer");
    addToast("success", "bottom-left", "Berhasil", `${checkIsBarangExists.metadata.sourceBarangNama} berhasil diupdate`);
} else {
    addToast("error", "bottom-left", "Gagal", `${checkIsBarangExists.metadata.sourceBarangNama} gagal diupdate`);
}
Enter fullscreen mode Exit fullscreen mode

Add New Items in The Dexie Database That Want to Transfer

If the item does not exist in the source warehouse (checkIsBarangExists is falsy), the function proceeds with creating the transfer.

Calls the getDetailItemByProdukId function to fetch the details of the item in the destination warehouse. The fetched details are stored in the variable itemDestinationDetail.

const itemDestinationDetail = await getDetailItemByProdukId(
    data.item.produk_id,
    values.transferKe
);
Enter fullscreen mode Exit fullscreen mode

Calculates the conversion rates between the source and destination units by filtering out zero values from the item details. The resulting arrays destinationKey, destinationValue, sourceKey, and sourceValue are then used to create objects destinationUnits and sourceUnits using the createObjectFromArray function.

const destinationKey = [
    itemDestinationDetail.satuan_jual_level_1 || 0,
    itemDestinationDetail.satuan_jual_level_2 || 0,
    itemDestinationDetail.satuan_jual_level_3 || 0,
    itemDestinationDetail.satuan_jual_level_4 || 0
].filter((x) => x !== 0);

const destinationValue = [
    itemDestinationDetail.isi_jual_level_1 || 0,
    itemDestinationDetail.isi_jual_level_2 || 0,
    itemDestinationDetail.isi_jual_level_3 || 0,
    itemDestinationDetail.isi_jual_level_4 || 0
].filter((x) => x !== 0);

const destinationUnits = createObjectFromArray(
    destinationKey,
    destinationValue
);

const sourceKey = [
    itemSourceDetail.item.satuan_jual_level_1 || 0,
    itemSourceDetail.item.satuan_jual_level_2 || 0,
    itemSourceDetail.item.satuan_jual_level_3 || 0,
    itemSourceDetail.item.satuan_jual_level_4 || 0
].filter((x) => x !== 0);

const sourceValue = [
    itemSourceDetail.item.isi_jual_level_1 || 0,
    itemSourceDetail.item.isi_jual_level_2 || 0,
    itemSourceDetail.item.isi_jual_level_3 || 0,
    itemSourceDetail.item.isi_jual_level_4 || 0
].filter((x) => x !== 0);

const sourceUnits = createObjectFromArray(
    sourceKey,
    sourceValue
);
Enter fullscreen mode Exit fullscreen mode

Determines the isiJualAsal (source unit value) and isiJualTujuan (destination unit value) based on the sourceUnits and destinationUnits, taking into account the unit of transfer.

var isiJualAsal, isiJualTujuan;
if (sourceUnits[itemDestinationDetail.satuan_beli_local]) {
    isiJualAsal = sourceUnits[itemSourceDetail.item.satuan_jual_level_1];
    isiJualTujuan = destinationUnits[itemSourceDetail.item.satuan_jual_level_1];
} else {
    isiJualAsal = sourceUnits[itemDestinationDetail.satuan_jual_level_1];
    isiJualTujuan = destinationUnits[itemDestinationDetail.satuan_jual_level_1];
}
Enter fullscreen mode Exit fullscreen mode

Get human-readable representations of the stock units for both the source and destination items.

const humanSource = await humanable({
    barangId: itemSourceDetail.item.barang_id,
    gudangId: Number(gudangId),
    jumlah: isiJualAsal,
});

const humanDestination = await humanable({
    barangId: itemDestinationDetail.barang_id,
    gudangId: Number(formData.transferKe),
    jumlah: isiJualTujuan,
});
Enter fullscreen mode Exit fullscreen mode

The data for the transfer is prepared as an object transferItem, containing details such as transfer numbers, warehouse IDs, item IDs, transfer status, transport details, and metadata.

const transferItem = {
    no_transfer: values.noTransfer,
    transfer_dari: gudangId,
    transfer_ke: values.transferKe,
    transfer_status: TRANSFER_STATUS.PENDING,
    transportasi_id: values.transportasiId,
    pengirim: values.pengirim,
    metadata: {
        sourceUserId: app.user.userId,
        sourceGudangId: gudangId,
        sourceBarangNama: itemSourceDetail.item.produk.barang_nama,
        sourceTransferAmout: isiJualAsal,
        sourceProdukId: itemSourceDetail.item.produk_id,
        sourceBarangId: itemSourceDetail.item.barang_id,
        sourceIsiJual: isiJualAsal,
        destinationRate: isiJualTujuan,
        humanSource,
    },
    transfer_detail: {
        no_transfer: values.noTransfer,
        barang_id: itemDestinationDetail.barang_id,
        produk_id: itemDestinationDetail.produk_id,
        jumlah_transfer: isiJualTujuan,
        humanDestination,
    },
};
Enter fullscreen mode Exit fullscreen mode

Then calls the createTransfer function to create the transfer record in the (backend) database. If the creation is successful, it triggers a re-fetch of all current transfers and displays a success toast message 🟒. Otherwise, it shows an error toast message πŸ”΄.

const created = await createTransfer(transferItem);
if (created) {
    mutate("/dexie/get-all-current-transfer");
    addToast("success", "bottom-left", "Berhasil", `${itemSourceDetail.item.produk.barang_nama} berhasil ditambahkan`);
} else {
    addToast("error", "bottom-left", "Gagal", `${itemSourceDetail.item.produk.barang_nama} gagal ditambahkan`);
}
Enter fullscreen mode Exit fullscreen mode

The Complete Mess

const checkIsBarangExists = await findBarang(itemSourceDetail.item.produk.barang_nama);

if (checkIsBarangExists) {
    const jumlah = checkIsBarangExists.metadata.sourceTransferAmout + checkIsBarangExists.metadata.sourceIsiJual;
    // Create unit definition between item source and destination
    const convertSourceUnitToDestination = Math.ceil(checkIsBarangExists.metadata.destinationRate * jumlah);

    // Get humanable stok unit
    const humanSource = await humanable({
        barangId: checkIsBarangExists.metadata.sourceBarangId,
        gudangId: Number(gudangId),
        jumlah: jumlah,
    });

    const humanDestination = await humanable({
        barangId: checkIsBarangExists.transfer_detail.barang_id,
        gudangId: Number(checkIsBarangExists.transfer_ke),
        jumlah: convertSourceUnitToDestination,
    });

    const updateData = {
        "metadata.sourceTransferAmout": jumlah,
        "metadata.humanSource": humanSource,
        "transfer_detail.jumlah_transfer": convertSourceUnitToDestination,
        "transfer_detail.humanDestination": humanDestination,
    };

    const updated = await updateTransfer(checkIsBarangExists.id, updateData);
    if (updated) {
        mutate("/dexie/get-all-current-transfer");
        addToast("success", "bottom-left", "Berhasil", `${checkIsBarangExists.metadata.sourceBarangNama} berhasil diupdate`);
    } else {
        addToast("error", "bottom-left", "Gagal", `${checkIsBarangExists.metadata.sourceBarangNama} gagal diupdate`);
    }
} else {
    const itemDestinationDetail = await getDetailItemByProdukId(data.item.produk_id, values.transferKe);

    // Create unit definition between item source and destination
    const destinationKey = [itemDestinationDetail.satuan_jual_level_1 || 0, itemDestinationDetail.satuan_jual_level_2 || 0, itemDestinationDetail.satuan_jual_level_3 || 0, itemDestinationDetail.satuan_jual_level_4 || 0].filter(
        (x) => x !== 0
    );
    const destinationValue = [itemDestinationDetail.isi_jual_level_1 || 0, itemDestinationDetail.isi_jual_level_2 || 0, itemDestinationDetail.isi_jual_level_3 || 0, itemDestinationDetail.isi_jual_level_4 || 0].filter((x) => x !== 0);
    const destinationUnits = createObjectFromArray(destinationKey, destinationValue);

    const sourceKey = [itemSourceDetail.item.satuan_jual_level_1 || 0, itemSourceDetail.item.satuan_jual_level_2 || 0, itemSourceDetail.item.satuan_jual_level_3 || 0, itemSourceDetail.item.satuan_jual_level_4 || 0].filter((x) => x !== 0);
    const sourceValue = [itemSourceDetail.item.isi_jual_level_1 || 0, itemSourceDetail.item.isi_jual_level_2 || 0, itemSourceDetail.item.isi_jual_level_3 || 0, itemSourceDetail.item.isi_jual_level_4 || 0].filter((x) => x !== 0);
    const sourceUnits = createObjectFromArray(sourceKey, sourceValue);

    // Ambil isi jual
    var isiJualAsal, isiJualTujuan;
    if (sourceUnits[itemDestinationDetail.satuan_beli_local]) {
        isiJualAsal = sourceUnits[itemSourceDetail.item.satuan_jual_level_1];
        isiJualTujuan = destinationUnits[itemSourceDetail.item.satuan_jual_level_1];
    } else {
        isiJualAsal = sourceUnits[itemDestinationDetail.satuan_jual_level_1];
        isiJualTujuan = destinationUnits[itemDestinationDetail.satuan_jual_level_1];
    }

    // Get humanable stok unit
    const humanSource = await humanable({
        barangId: itemSourceDetail.item.barang_id,
        gudangId: Number(gudangId),
        jumlah: isiJualAsal,
    });

    const humanDestination = await humanable({
        barangId: itemDestinationDetail.barang_id,
        gudangId: Number(formData.transferKe),
        jumlah: isiJualTujuan,
    });

    const transferItem = {
        no_transfer: values.noTransfer,
        transfer_dari: gudangId,
        transfer_ke: values.transferKe,
        transfer_status: TRANSFER_STATUS.PENDING,
        transportasi_id: values.transportasiId,
        pengirim: values.pengirim,
        metadata: {
            sourceUserId: app.user.userId,
            sourceGudangId: gudangId,
            sourceBarangNama: itemSourceDetail.item.produk.barang_nama,
            sourceTransferAmout: isiJualAsal,
            sourceProdukId: itemSourceDetail.item.produk_id,
            sourceBarangId: itemSourceDetail.item.barang_id,
            sourceIsiJual: isiJualAsal,
            destinationRate: isiJualTujuan,
            humanSource,
        },
        transfer_detail: {
            no_transfer: values.noTransfer,
            barang_id: itemDestinationDetail.barang_id,
            produk_id: itemDestinationDetail.produk_id,
            jumlah_transfer: isiJualTujuan,
            humanDestination,
        },
    };

    const created = await createTransfer(transferItem);
    if (created) {
        mutate("/dexie/get-all-current-transfer");
        addToast("success", "bottom-left", "Berhasil", `${itemSourceDetail.item.produk.barang_nama} berhasil ditambahkan`);
    } else {
        addToast("error", "bottom-left", "Gagal", `${itemSourceDetail.item.produk.barang_nama} gagal ditambahkan`);
    }
}
Enter fullscreen mode Exit fullscreen mode

This code is a crucial part of a system that facilitates the transfer of items between different warehouse locations. It efficiently handles item existence checks, conversion between units, and database operations to ensure a smooth and accurate item transfer process.


What do you think, this is a very impressive job-become Spongebob with his box of imagination. And this is an example from "The Art Of Mess Code"

I am a Software Freestyle Engineer, this title is not just a joke this is my career path and mindset that always thinks the opposite of what other people think.

Are you interested in strange things and not fictional stories but real ones? Follow me and get interesting things that might inspire your way of thinking and perspective on something.

Follow me

Top comments (2)

Collapse
 
ggorantala profile image
Gopi Gorantala

Clever!!

Collapse
 
darkterminal profile image
Imam Ali Mustofa

Thank you very much, that's me! 😁