Custom script almost working

Hi

Goal: to auto populate the “taxes and charges” table (Sales Order), based on the “tax category” of
the customer.

My script seem to fetch the “Sales Taxes and Charges” template name correctly, but the child table
does not update.

Please help

frappe.ui.form.on('Sales Order', {
    customer: function(frm) {
        if (frm.doc.customer) {
                frappe.call({
                    method: 'frappe.client.get_value',
                    args: {
                        doctype: 'Customer',
                        filters: {
                        customer_name: frm.doc.customer
                    },
                    fieldname: ['tax_category']
                    },
                    callback: function(response) {
                        if (response.message) {
    	                    var tax_category = response.message.tax_category;
    	                    frappe.show_alert(tax_category, 5);
            	            getTaxTemplateByCategory(tax_category);
                        }
                    }
                });
        }
    }
});

function getTaxTemplateByCategory(tax_category) {
    frappe.call({
        method: 'frappe.client.get_list',
        args: {
            doctype: 'Sales Taxes and Charges Template',
            filters: { tax_category: tax_category },
            fields: ['name'],
            limit: 1
        },
        callback: function(response) {
            if (response.message && response.message.length > 0) {
                frappe.show_alert(response.message[0].name, 5);
                cur_frm.doc.taxes_and_charges = response.message[0].name;
                frm.refresh_field('taxes_and_charges');
                frm.refresh_children('taxes');
                frm.trigger('taxes_and_charges');
            }
        }
    });
}

@willspenc can you re-explain your goal here . do you want to set an automatic taxes to the sales order depending on customers ?

Hi

Based on the customer selected when creating a sales order, I want to the taxes and charges table to be populated with a particular Sales Txes and charges Template.

I use Tax category to do this. So, the Tax category field of a customer is set to e.g Category 1.
The Tax category field of a Sales Taxes and charges template is set to Category 1 and that must be
the template loaded into the table when the customer is selected .

Thank you

Hi @willspenc,

Please apply it.

frappe.ui.form.on("Sales Order", {
    refresh: function(frm) {
        frappe.db.get_value("Customer", {"name": frm.doc.customer}, "tax_category", function(cust) {
            if (cust.tax_category) {
                frappe.db.get_value("Sales Taxes and Charges Template", {"tax_category": cust.tax_category}, "name", function(tax) {
                    frm.set_value('taxes_and_charges', tax.name);
                });
            }
        });
    }
});

Then reload and check it.

Thank You!

Thank you @NCP it is working .

I appreciate your assistance. By looking at your code perhaps I can learn where I went wrong.

I am also currently searching for information on the following frappe commands :slight_smile:

frm.set_df_property
frappe.model.set_df_property("from_stock", "reqd", 1);

I want to make the fields in the taxes child table non-editable after the table is populated.
Will both of these commands work ?

Please check it.

Thank You!

I will work through the document.
Thank you for your asisstance

@willspenc there is already a features that does the same thing . tax rule . A Tax Rule automatically applies taxes to transactions based on preset rules. take a look.

1 Like

thank you @bahaou for pointing that out.

I think I am so keen to grab opportunities to write code, so that I can continue learning, that I jumped
straight into action ! I have set up the Tax-rule and it works.

I think just to finish off my effort, I am going to apply the info from @NCP anyway.

Thank you.