Selecting Income Account

I wrote the following script. The idea is to select the appropriate income account based on the sales type and item code found in the sales invoice.

Have i missed some thing it is not working Please Help. Thanks

cur_frm.cscript.custom_validate = function(doc) 
{
//valid only for LITP
      if doc.company = "Labora International Trading PLC";
           {
               if doc.sales_type = "Cash Sales";
                {
                   string x = "cash";
                }
               else
                {
                   string x = "credit";
                }
           }
//looping through Sales Invoice Items table
     $.each(wn.model.get("Sales Invoice Items", {parent:doc.name}), function(i, d) 
    {
           string itemStr = d.item_code;
           string itemChar = itemStr.Substring(0,1);
      switch(doc.item_group) 
       {
        case "RE":
            if x = "cash"
                {
                   doc.income_account = "4200-001 Sales - Reagents Cash - LITP";
                }
            else
                {
                   doc.income_account = "4200-001 Sales - Reagents Credit - LITP";
                }
            break;
        case "MS":
            if x = "cash"
                {
                   doc.income_account = "4300-001 Sales-Medical Supplies Cash - LITP";
                }
            else
                {
                   doc.income_account = "4300-001 Sales-Medical Supplies Credit - LITP";
                }
            break;
        default:
            doc.item_code = "Sales - LITP";
       }
        
    });
}

In javascript, if conditions are like this:

if(doc.company == "Labora International Trading PLC")

There are some basic mistakes in terms of syntax, please check those online first.

Thanks Nabin,
I corrected all the syntax errors, but with no result.
I have two questions regarding this.

  1. can i add another script on the same file. i.e there is a already a script on the Sales Invoice_Client, and i need the effect to be on Sales Invoice?
    2 is the script function (cur_frm.cscript.custom_validate = function(doc)) proper function for achieving the desired result.

Hello
Waiting for some suggestions
Thanks

yes, you can add multiple function in sales custom script record.

If you write the logic in custom_validate, income_account will be set on saving of document. If you want to set income_account on_change of item or sales_type, you have to call the function on trigger of those field. For on_change event syntax, check https://frappe.io/docs/guide/form-client-scripting.

In above code, doc.income_account is wrong, it should be d.income_account.
Can you share your final code?

My final code…

cur_frm.cscript.custom_validate = function(doc) 
{
//valid only for LITP
      if doc.company == "Labora International Trading PLC";
           {
               if doc.sales_type == "Cash Sales";
                {
                   string x = "cash";
                }
               else
                {
                   string x = "credit";
                }
           }
//looping through Sales Invoice Items table
     $.each(wn.model.get("Sales Invoice Items", {parent:doc.name}), function(i, d) 
    {
           string itemStr = d.item_code;
           string itemChar = itemStr.Substring(0,1);
      switch(doc.item_group) 
       {
        case "RE":
            if x == "cash"
                {
                   d.income_account = "4200-001 Sales - Reagents Cash - LITP";
                }
            else
                {
                   d.income_account = "4200-001 Sales - Reagents Credit - LITP";
                }
            break;
        case "MS":
            if x == "cash"
                {
                   d.income_account = "4300-001 Sales-Medical Supplies Cash - LITP";
                }
            else
                {
                   d.income_account = "4300-001 Sales-Medical Supplies Credit - LITP";
                }
            break;
        default:
            d.income_account = "Sales - LITP";
       }

    });
}

Please format your code correctly or use gist.github,com or pastebin.com

I had already formatted this code also as the first one , i don’t know how it happens like that. Any way i posted the same code on GitHub gist with proper formatting, is that ok?.

Refresh income_account field after assigning value of income account.

refresh_field('income_account', d.name, 'entries');