Are you wondering why the script might be using the Trim Function? I think the data might contain white spaces before or after the actual values which can be used using Trim function here. With regards to the specific code:
upper(if(left(TRIM([Ship to party]),1)='N',[Ship to party Name],'Direct_Sale')) as WH,
Checking if the first letter of [Ship to party] field is N, if it is assign the value to a new field called WH the value from the field [Ship to part Name], else call is Direct_Sale.
Do you need the same explanation for all the lines of code or do you get an idea of where I am going?