oobayly / KashFlow

// ==UserScript==
// @name         KashFlow
// @namespace    https://securedwebapp.com
// @version      0.2
// @description  Add reminder for advance payments
// @author       You
// @match        https://securedwebapp.com/viewInvoice.asp*
// @match        https://securedwebapp.com/viewinvoice.asp*
// @match        https://securedwebapp.com/viewReceipt.asp*
// @match        https://securedwebapp.com/viewreceipt.asp*
// @match        https://securedwebapp.com/createReceipt.asp*
// @match        https://securedwebapp.com/createreceipt.asp*
// @grant        none
// @require      https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js
// ==/UserScript==
/* jshint -W097 */
'use strict';

$(document).ready(function() {
    if ($("span:contains('Apply Advance Payment')").length) {
        var frmInvPay = $("form[name='frmInvPay']");
        var frmRecPay = $("form[name='frmRecPay']");
        
        if (frmInvPay.length) {
            var td = frmInvPay.find("table tbody tr td").first();
            td.text("An advance payment exists, please check first if this should be used.");
            td.css("font-weight", "bold");
            td.css("font-size", "150%");
            td.css("color", "red");

            frmInvPay.css("background-color", "yellow");
            frmInvPay.next().css("background-color", "yellow");
        } else if (frmRecPay.length) {
            var table = frmRecPay.find("table tbody").first();
            var tr = $("<tr>").prependTo(table);
            var td = $("<td>").prependTo(table);
            td.attr("colspan", 5);
            td.text("An advance payment exists, please check first if this should be used");
            td.css("font-weight", "bold");
            td.css("font-size", "150%");
            td.css("color", "red");

            frmRecPay.css("background-color", "yellow");
            frmRecPay.next().css("background-color", "yellow");
        }
    }
    
    // Don't show the payment details when adding a receipt as certain users ignore the fact that there's a pre-payment
    if (window.location.pathname.match(/\/createReceipt.asp/i)) {
        var table = $("td").filter(function() {
            return $(this).text().match(/You can ignore this section if you have not yet paid this receipt/);
        }).closest("table");
        table.remove();
    }
});