Skip to content

Instantly share code, notes, and snippets.

@NagariaHussain
Created September 3, 2023 16:07
Show Gist options
  • Save NagariaHussain/08afb23b969c85204f556d6bbfef6d1b to your computer and use it in GitHub Desktop.
Save NagariaHussain/08afb23b969c85204f556d6bbfef6d1b to your computer and use it in GitHub Desktop.
Build With Hussain, Episode 14 Client/Server Scripts

Intro Client Script

frappe.ui.form.on('Project', {
	refresh(frm) {
	    if(frm.doc.expected_end_date == frappe.datetime.now_date())
		    frm.set_intro("<b>End date has arrived</b>","red")
		else
		    frm.set_intro("Take it east","green")
	}
})

Customer SI Client Script

frappe.ui.form.on('Customer', {
  refresh: function(frm) {
    let params = {
        'customer': frm.doc.name
    }
    frappe.call('customer_sales_invoice_details', params).then((r) => {
        let si = r.message;
        let name = si.name;
        let amount = si.grand_total;
        let size = name.length;
        
        if(size>0)
        {
            // Set up the desired HTML content with a table and borders
            var htmlContent = '<table style="border-collapse: collapse; width: 100%;">';
            htmlContent += '<thead>';
            htmlContent += '<tr>';
            htmlContent += '<th style="border: 1px solid #ddd; padding: 8px;">Sales Invoice</th>';
            htmlContent += '<th style="border: 1px solid #ddd; padding: 8px;">Amount</th>';
            htmlContent += '</tr>';
            htmlContent += '</thead>';
            htmlContent += '<tbody>';
            
            for(let i = 0; i< size; i++)
            {
                if (!frm.is_new()) {
                    htmlContent += '<tr>';
                    htmlContent += '<td style="border: 1px solid #ddd; padding: 8px;">' + name[i] + '</td>';
                    htmlContent += '<td style="border: 1px solid #ddd; padding: 8px;">' + amount[i] + '</td>';
                    htmlContent += '</tr>';
                }
            }
            htmlContent += '</tbody>';
            htmlContent += '</table>';
            frm.dashboard.add_section(htmlContent,"Open Sales Invoices");
        }
    })
  }
});

Automatic Payment Req creation Server Script

text = "This is a payment request created against Sales Invoice: " + doc.name + " for an amount of " + str(doc.grand_total) + " ."
if doc.custom_payment_request ==1:
    new_doc = frappe.get_doc({
        'doctype':'Payment Request',
        'party_type': 'Customer',
        'party': doc.customer,
        'reference_doctype': 'Sales Invoice',
        'reference_name': doc.name,
        'subject': text
    })
    new_doc.insert()
    frappe.msgprint(
    msg=f'New <a href=" https://tesla-india.frappe.cloud/app/payment-request/{new_doc.name}"> Payment Request </a> has been created',
    title='Amazing work!',
    indicator='orange'
)

Unpaid SI Server Script (API Type)

customer = frappe.form_dict.get('customer')
a = []
b = []
unpaid_si = frappe.get_all('Sales Invoice', filters ={'customer':customer,'status':'Overdue'})

#a = [frappe.get_doc('Sales Invoice', i).name for i in unpaid_si]
a = [frappe.get_doc('Sales Invoice', i).name for i in unpaid_si]
b = [frappe.get_doc('Sales Invoice', i).grand_total for i in unpaid_si]

result = {
    "name":a,
    "grand_total":b
}

frappe.response.message = result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment