ERPNext Client Script to generate lottery number

81 views Asked by At

I want to create lottery numbers on ERPNext with the following rules: Customers will get 3 random lottery numbers every time their total purchase reaches 100,000.

Here is a client script that I tried to make

frappe.ui.form.on('Sales Invoice', {
    after_submit: function(frm) {
        // Check if the total invoice amount is greater than or equal to 100,000
        if (frm.doc.grand_total >= 100000) {
            // Calculate the number of lottery numbers based on the invoice amount
            var numberOfLotteryNumbers = Math.floor(frm.doc.grand_total / 100000) * 3;
            // Generate lottery numbers
            var lotteryNumbers = [];
            for (var i = 0; i < numberOfLotteryNumbers; i++) {
                lotteryNumbers.push(Math.floor(Math.random() * 10000) + 1);
            }
            // Update the lottery numbers field in the document
            frappe.model.set_value(frm.doctype, frm.docname, 'lottery_number', lotteryNumbers.join(', '));
            frm.save();  // Save the document to store the generated lottery numbers
            frappe.msgprint('Lottery numbers generated: ' + lotteryNumbers.join(', '));
        }
    }
});

Im also tried to add this code to sales_invoice.py


import random
from frappe.model.document import Document


class SalesInvoice(Document):
        def on_submit(self):
                        if self.customer_group in ["Langganan Gorontalo", "Grosir Gorontalo"] and self.base_grand_total >= 100000:
                                custom_nomor_undian = [random.randint(100000, 999999) for _ in range(3)]
                                lottery_numbers_str = ', '.join(str(n) for n in custom_nomor_undian)
                                self.custom_nomor_undian = lottery_numbers_str
                                self.ignore_validate_update_after_submit = True
                                self.save()

No Error, the document can be save but still no value change on my field

0

There are 0 answers