Sales Team Commission on the General Ledger

Hi:

I want to record an additional double entry on the General Ledger at the posting of every Sales Invoice in order to solve the issue of tracking and provisioning the Sales Partners’ commission. This extra double entry would be something like this:

Payable Account to the Sales Team (credit)
Cost Account to track the commission expense (debit)

The value of the debit/credit will be extracted from the “Total Commission” of the Sales Invoice and the accounts used can be custom fields for the Sales Team.

I guess an alternative to this can be creating a Journal Entry after posting the Invoice, but I understand that will impose extra tasks to allow tracking of the Sales Invoice.

I have checked the article: How to record Commission to Sales Partner in ERPNext?. Also I have foundthis Topic with a similar case with a subsequentIssue here. However @umair seems to have closed it claiming this has been added in version 11 but most of us haven’t found how this was done.

I have had a couple of success experiences implementing client side scripts. But my issue in this case is that I haven’t found how to create the related entries on the General Ledger for any kind of document. I would appreciate any pointers on this.

Thank you!

Guys, I really could use some help.

As my issue is “just” inserting those two extra records (of type GL Entry, as I understand), I have tried this:

As a test, in a Client Script for the Sales Invoice, I created an array doc.account with the info of the two extra records. I call it pressing the button before saving/posting the invoice. However its content is not reflected when I check the Ledger and I don’t see any warning or error thrown:

frappe.ui.form.on(‘Sales Invoice’, {
refresh: (frm, cdt, cdn) => {
frm.add_custom_button(“Test to append Accounts”, () => {
// Calls the function that appends the Accounts
test_creation(frm.doc);
console.log(‘This is the current content of the doc’)
console.log(frm.doc)
});
}
});

function test_creation(doc) {
doc.accounts =
[
{
“account”: ‘110505 - Caja general - ASHA’,
“doctype”: “Sales Invoice”,
“debit”: 100,
“credit”: 0,
“user_remark”: cur_frm.docname
},
{
“account”: ‘110505 - Caja general - ASHA’,
“doctype”: “Sales Invoice”,
“debit”: 0,
“credit”: 100,
“user_remark”: cur_frm.docname
}
]
}

I guess another possibility is to use the doc.insert method, but I’m afraid I don’t have clear which type of document has to be inserted a a parent of the GL records.

Again, I appreciate any help on this.

Thank you!

As I haven’t figured a solution for my original problem, I’ve managed to automate a Journal Entry for the recognition of the Commission based on this post from @dave-greeko

Here it is my code:

frappe.ui.form.on("Sales Invoice", {
    // When the from is refreshed...
    refresh: (frm, cdt, cdn) => {
        // ... it shows a Button to allow the user create a Journal for the Comission of the Sales Team
        frm.add_custom_button("Create Journal for Comission", () => {
            // If the button is pressed, we create the Journal Entry, based on the information we haven in this form
            create_journal_entry(frm);
        });
    }
});

function create_journal_entry(frm) {
    // When asked to create the Journal Entry, first we get the info about the sales partner
    frappe.call({
        method: 'frappe.client.get_value',
        args: {
            'doctype': 'Sales Partner',
            'filters': {
                'name': frm.doc.sales_partner
            },
            'fieldname': ['xpayable_account', 'xcomission_cost_account']
        },
        // After getting the Sales Partner info...
        callback: function (data) {
            // We create a temporal object with the information about the Journal Entry
            frm.temporal_journal_entry = populate_journal_entry_obj(frm, data);
            // And then, when submit it to the database.
            submit_je(frm)
        }
    });
}

function populate_journal_entry_obj(frm, data) {
    console.log(`From the Sales Partner, Payable Account: ${data.message.xpayable_account} and Comission Account: ${data.message.xcomission_cost_account}`);
    console.log(`From the Sales Invoice, comission: ${frm.doc.total_commission}`);
    let je = {};
    let accounts = [
        {
            "doctype": "Journal Entry Account",
            "account": data.message.xpayable_account,
            "party": frm.doc.sales_partner,
            "party_type": "Sales Partner",
            "debit": 0,
            "credit": frm.doc.total_commission,
            "credit_in_account_currency": frm.doc.total_commission,
            "user_remark": cur_frm.docname
        },
        {
            "doctype": "Journal Entry Account",
            "account": data.message.xcomission_cost_account,
            "debit": frm.doc.total_commission,
            "credit": 0,
            "debit_in_account_currency": frm.doc.total_commission,
            "user_remark": cur_frm.docname
        }
    ];
    je["doctype"] = "Journal Entry";
    je["posting_date"] = frappe.datetime.add_days(frm.doc.process_date, 0),
    je["accounts"] = accounts;
    je["remark"] = cur_frm.docname;
   return je;
}

function submit_je(frm) {
    frappe.db.insert(frm.temporal_journal_entry)
        .then(function (doc) {
            frappe.call({
                "method": "frappe.client.submit",
                "args": {
                    "doc": doc
                },
                "callback": (r) => {
                    console.log(r);
                }
            });
        });
}

Anyway, I would prefer to make those entries in the Invoice instead of an additional document. So if anyone happens to guide me on that, I would appreciate it.

Thank you!