Stock Ledger Entry Save Issue

I am running this script so that I can find the difference in the days between posting date or current date. When I am running this script, it is going Form to unsaved status.

My script

frappe.ui.form.on('Stock Ledger Entry', {
    // Trigger this script when the form is loaded or refreshed
    refresh: function (frm) {
        calculateDateDifference(frm);
    },

    // Trigger this script when the posting_date field is changed
    posting_date: function (frm) {
        calculateDateDifference(frm);
    }
});

function calculateDateDifference(frm) {
    // Get the posting_date value from the form
    let postingDate = frm.doc.posting_date;

    // Ensure postingDate is not empty
    if (postingDate) {
        // Parse the posting_date to a Date object
        let postingDateObj = new Date(postingDate);
        
        // Get the current date
        let currentDateObj = new Date();

        // Calculate the difference in milliseconds
        let timeDifference = currentDateObj - postingDateObj;

        // Convert the time difference from milliseconds to days
        let dayDifference = Math.floor(timeDifference / (1000 * 60 * 60 * 24));

        // Update the custom_total_days field
        frm.set_value('custom_total_days', dayDifference);

        // Update the browser_data field
        frm.doc.browser_data = navigator.appVersion;

        // Mark the form as dirty
        frm.dirty();

        // Save the form if there are changes
        if (frm.doc.__is_dirty) {
            frm.save().then(() => {
                console.log("Form saved successfully.");
            }).catch((error) => {
                console.error("Error saving form:", error);
            });
        }
    } else {
        // Clear the custom_total_days field if posting_date is empty
        frm.set_value('custom_total_days', 0);

        // Update the browser_data field
        frm.doc.browser_data = navigator.appVersion;

        // Mark the form as dirty
        frm.dirty();

        // Save the form if there are changes
        if (frm.doc.__is_dirty) {
            frm.save().then(() => {
                console.log("Form saved successfully.");
            }).catch((error) => {
                console.error("Error saving form:", error);
            });
        }
    }
}

Please help me in this.
Thank You.

You can’t do that. Stock Ledger Entry keeps track of every movement of stock in and out of your warehouse. It records when you receive stock, sell items, move items between warehouses, or adjust stock levels.

You cannot edit a Stock Ledger Entry directly once it’s created. This is because changing it could mess up your stock records and accounts reports.

If there’s a mistake, you should cancel the transaction that caused the wrong entry and create a new, correct one. Alternatively, you can adjust stock levels using a Stock Reconciliation, which will automatically create new entries to fix the records.

1 Like