Auto update Xero XPM custom fields

Automatically update custom fields to reflect XPM defailt fields, which aren't available in reports

目前為 2021-06-12 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Auto update Xero XPM custom fields
// @namespace    https://practicemanager.xero.com/
// @version      0.2
// @description  Automatically update custom fields to reflect XPM defailt fields, which aren't available in reports
// @author       Kyle Drayton CPA
// @match        https://practicemanager.xero.com/Client/Client/Edit/*
// @match        https://practicemanager.xero.com/Client/Client/New
// @icon         https://www.google.com/s2/favicons?domain=xero.com
// @require      https://code.jquery.com/jquery-3.6.0.min.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    //Declare default bank fields
    //For the uninitiated, to find these right click on the corresponding textbox, click "inspect", then copy the ID and paste it between the quotes.

    //XPM default fields
    var srcBSB = "text-1092-inputEl";
    var srcAcc = "text-1093-inputEl";
    var srcAccName = "text-1094-inputEl";
    var srcFFR = "checkbox-1097-inputEl";

    //Your custom fields
    var destCliNum = "text-1091-inputEl"; //Save XPM ID in default "Client Code" field.
    var destBSB = "text-1135-inputEl";
    var destAcc = "text-1136-inputEl";
    var destAccName = "text-1137-inputEl";
    var destFFR = "checkbox-1134-inputEl";

    //Set script strings to keep the code clean.
    var saveCliNum = "document.getElementById(destCliNum).value = srcCliNum;"
    var saveBSB = "document.getElementById(destBSB).value = document.getElementById(srcBSB).value;"
    var saveAcc = "document.getElementById(destAcc).value = document.getElementById(srcAcc).value;"
    var saveAccName = "document.getElementById(destAccName).value = document.getElementById(srcAccName).value;"

    //Set XPM ID - will alert user for new clients, as this isn't available for them
    try {
        var tmpNum = window.location.href.toString().match(/\d+/); //Get XPM ID from URL
        var srcCliNum = tmpNum.toString(); //Set XPM ID as string
        window.onLoad = function(){eval(saveCliNum);}
    }
    catch(err){
        alert("Remember to go back into edit client and hit save.\nXPM ID won't be saved otherwise!");
        srcCliNum;
    }

    //Automatically populate on page load - for those who have an existing client database that needs to be updated.
    //To do this automatically, check out my UI.Vision script on Github:

    $(document).ready(function(){
        eval(saveBSB);
        eval(saveAcc);
        eval(saveAccName);

        //Double check boxes are synced, as I'm a JS n00b and can't figure out how to change state of this code without clicking

        var srcChk = srcFFR.replace("-inputEl","");
        srcChk = document.getElementById(srcChk).classList.toString().contains('checked');
        var destChk = destFFR.replace("-inputEl","");
        destChk = document.getElementById(destChk).classList.toString().contains('checked');

        if (srcChk != destChk || destChk == true){
            document.getElementById(srcFFR).click();}
        else if (srcChk != destChk || srcChk == true) {
            document.getElementById(destFFR).click();}
    });

    //Set event listeners to change destination fields
    document.getElementById(srcBSB).onblur = function(){eval(saveBSB)}
    document.getElementById(srcAcc).onblur = function(){eval(saveAcc)}
    document.getElementById(srcAccName).onblur = function(){eval(saveAccName)}
    document.getElementById(srcFFR).onclick = function(){document.getElementById(destFFR).click();}
})();