
Runner.pages.PageSettings.addPageEvent( 
	"journal_entry", 
	"add", 
	"afterPageReady", 
	function(pageObj, proxy, pageid, inlineRow, inlineObject, row ) {
		/* ============================================================
   BuildLedger Journal Entry
   - Auto compute Total Debit and Total Credit preview
   - Works with inline journal details
   ============================================================ */

(function () {

    function blNumber(value) {
        if (value === null || value === undefined) {
            return 0;
        }

        value = String(value).replace(/,/g, "").replace("₱", "").trim();

        var n = parseFloat(value);
        return isNaN(n) ? 0 : n;
    }

    function blMoney(value) {
        return Number(value || 0).toFixed(2);
    }

    function blFindField($row, fieldName) {
        return $row.find(
            "input[name*='" + fieldName + "'], " +
            "select[name*='" + fieldName + "'], " +
            "textarea[name*='" + fieldName + "']"
        ).first();
    }

    function blSetHeaderAmount(fieldName, value) {
        var amount = blMoney(value);

        try {
            var ctrl = Runner.getControl(pageid, fieldName);

            if (ctrl) {
                ctrl.setValue(amount);
                return;
            }
        } catch (e) {
        }

        $("input[name*='" + fieldName + "']").val(amount);
        $("span[id*='" + fieldName + "']").text(amount);
        $("td[id*='" + fieldName + "']").text(amount);
        $("div[id*='" + fieldName + "']").text(amount);
    }

    function blRecalculateJournalPreview() {

        var totalDebit = 0;
        var totalCredit = 0;
        var hasLine = false;

        $("tr").each(function () {

            var $row = $(this);

            var $debit = blFindField($row, "debit_amount");
            var $credit = blFindField($row, "credit_amount");

            if (!$debit.length && !$credit.length) {
                return;
            }

            var debit = blNumber($debit.val());
            var credit = blNumber($credit.val());

            if (debit <= 0 && credit <= 0) {
                return;
            }

            hasLine = true;

            totalDebit += debit;
            totalCredit += credit;
        });

        blSetHeaderAmount("total_debit", totalDebit);
        blSetHeaderAmount("total_credit", totalCredit);
    }

    $(document).on("keyup change blur", "input, select, textarea", function () {
        setTimeout(blRecalculateJournalPreview, 100);
    });

    setTimeout(blRecalculateJournalPreview, 500);
    setTimeout(blRecalculateJournalPreview, 1200);

})();
});


Runner.pages.PageSettings.addPageEvent( 
	"journal_entry", 
	"edit", 
	"afterPageReady", 
	function(pageObj, proxy, pageid, inlineRow, inlineObject, row ) {
		/* ============================================================
   BuildLedger Journal Entry
   - Auto compute Total Debit and Total Credit preview
   - Works with inline journal details
   ============================================================ */

(function () {

    function blNumber(value) {
        if (value === null || value === undefined) {
            return 0;
        }

        value = String(value).replace(/,/g, "").replace("₱", "").trim();

        var n = parseFloat(value);
        return isNaN(n) ? 0 : n;
    }

    function blMoney(value) {
        return Number(value || 0).toFixed(2);
    }

    function blFindField($row, fieldName) {
        return $row.find(
            "input[name*='" + fieldName + "'], " +
            "select[name*='" + fieldName + "'], " +
            "textarea[name*='" + fieldName + "']"
        ).first();
    }

    function blSetHeaderAmount(fieldName, value) {
        var amount = blMoney(value);

        try {
            var ctrl = Runner.getControl(pageid, fieldName);

            if (ctrl) {
                ctrl.setValue(amount);
                return;
            }
        } catch (e) {
        }

        $("input[name*='" + fieldName + "']").val(amount);
        $("span[id*='" + fieldName + "']").text(amount);
        $("td[id*='" + fieldName + "']").text(amount);
        $("div[id*='" + fieldName + "']").text(amount);
    }

    function blRecalculateJournalPreview() {

        var totalDebit = 0;
        var totalCredit = 0;
        var hasLine = false;

        $("tr").each(function () {

            var $row = $(this);

            var $debit = blFindField($row, "debit_amount");
            var $credit = blFindField($row, "credit_amount");

            if (!$debit.length && !$credit.length) {
                return;
            }

            var debit = blNumber($debit.val());
            var credit = blNumber($credit.val());

            if (debit <= 0 && credit <= 0) {
                return;
            }

            hasLine = true;

            totalDebit += debit;
            totalCredit += credit;
        });

        blSetHeaderAmount("total_debit", totalDebit);
        blSetHeaderAmount("total_credit", totalCredit);
    }

    $(document).on("keyup change blur", "input, select, textarea", function () {
        setTimeout(blRecalculateJournalPreview, 100);
    });

    setTimeout(blRecalculateJournalPreview, 500);
    setTimeout(blRecalculateJournalPreview, 1200);

})();
});







