Getting geolocation in sales invoice

I am seeking your assistance in developing a system that can automatically capture and include GPS latitude and longitude data in our sales invoices in ERPNext. Specifically, we would like to integrate this geolocation information into the invoicing process to enhance accuracy and record-keeping.

@M.K.Raj you can take reference from Employee Checkin this
Jsfile:hrms/hrms/hr/doctype/employee_checkin/employee_checkin.js at develop · frappe/hrms · GitHub
pyfile:hrms/hrms/hr/doctype/employee_checkin/employee_checkin.py at develop · frappe/hrms · GitHub

I try following code to get the geolocation when saved the sales invoice but it is not working . any suggestion?

frappe.ui.form.on(‘Sales Invoice’, {
refresh: function(frm) {

    frm.add_custom_button(__('Get Location'), function() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(
                function(position) {
                    // Set custom_latitude and custom_longitude in the document
                    frm.set_value('custom_latitude', position.coords.latitude);
                    frm.set_value('custom_longitude', position.coords.longitude);
                    frappe.show_alert(__('Location captured successfully!'));
                },
                function(error) {
                    frappe.throw(__('Geolocation error: ') + error.message);
                }
            );
        } else {
            frappe.throw(__('Geolocation is not supported by your browser.'));
        }
    });
},
before_save: function(frm) {

    if (!frm.doc.custom_latitude || !frm.doc.custom_longitude) {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(
                function(position) {

                    frm.set_value('custom_latitude', position.coords.latitude);
                    frm.set_value('custom_longitude', position.coords.longitude);


                    frm.save();
                },
                function(error) {
                    frappe.throw(__('Geolocation error: ') + error.message);
                }
            );

            // Prevent the form from saving until the coordinates are set
            return false;
        } else {
            frappe.throw(__('Geolocation is not supported by your browser.'));
        }
    }
}

});

I managed to get the latitude and longitude into a text field called custom_geoloca. Once the sales invoice is saved, it updates in the following format: 6.9270786, 79.861243. However, I created another geolocation field called custom_geolocation and tried to display the location on the geolocation map using the data from the text field. I used the following code, but nothing happened. Can anyone help me resolve this issue?

// Assuming you are using SuiteScript 2.0
define([‘N/record’, ‘N/ui/message’], function(record, message) {
function saveRecord(context) {
var currentRecord = context.currentRecord;

    // Get the value from the custom_geoloca text field
    var geolocaValue = currentRecord.getValue({
        fieldId: 'custom_geoloca'
    });

    if (geolocaValue) {
        // Split the latitude and longitude
        var latLongArray = geolocaValue.split(',');
        var latitude = parseFloat(latLongArray[0].trim());
        var longitude = parseFloat(latLongArray[1].trim());

        // Set the values in the custom_geolocation field
        currentRecord.setValue({
            fieldId: 'custom_geolocation',
            value: {
                latitude: latitude,
                longitude: longitude
            }
        });
    }
}

return {
    saveRecord: saveRecord
};

});

1 Like

finally got it myself. thank you