Bulk Sales invoice from members

Hello everyone,

Can anyone help me if there is any way in Non Profit Module- to create Bulk Sales Invoices from Multiple memberships. That is one selects the memberships and Bulk sales Invoices are generated against the selected memberships.

Thanks

Possible with a server script.

Otherwise, you can use both the client and server side.

Ok can you help with sample code please

Thanks

That for, you have to write a code on listview level:

frappe.listview_settings['Membership'] = {
    onload: function(listview) {
        listview.page.add_action_item(__('Create Bulk Sales Invoices'), function() {
            const selected_memberships = listview.get_checked_items();

            if (selected_memberships.length === 0) {
                frappe.msgprint(__('Please select at least one membership'));
                return;
            }

            const membership_ids = selected_memberships.map(m => m.name);

            frappe.call({
                method: "path.to.your.create_bulk_sales_invoices",
                args: {
                    selected_memberships: membership_ids
                },
                callback: function(response) {
                    if (response.message) {
                        frappe.msgprint(response.message);
                    }
                }
            });
        });
    }
};

Server side:

import frappe

@frappe.whitelist()
def create_bulk_sales_invoices(selected_memberships):
    selected_memberships = frappe.parse_json(selected_memberships)
    
    for membership_id in selected_memberships:
        membership = frappe.get_doc("Membership", membership_id)

        if not membership.is_invoiced:
            sales_invoice = frappe.new_doc("Sales Invoice")
            sales_invoice.customer = membership.customer
            sales_invoice.membership_id = membership.name

            sales_invoice.append("items", {
                "item_code": membership.membership_type,
                "qty": 1,
                "rate": membership.amount,
                "description": f"Membership for {membership.membership_type}"
            })

            sales_invoice.insert()
            sales_invoice.submit()

            membership.is_invoiced = 1
            membership.save()

    return "Bulk Sales Invoices Created Successfully!"

Now you can set it your according to the scenario.

1 Like

Thanks.

I am setting it now,

Hello, can i use use server side only ? The above solution did not work. Help with code

Regards,

Possibile, but you have to use the doc_events.