What I've done is use a script to set each Landed Cost code & amount so native Sage takes over for the write back values to the Lines. I will calculate the amount in a number of ways per line, like you've done, then use standard Sage UDF logic to total all lines with a value in that UDF to a Header UDF for each type of Landed Cost. I then use a script to populate the Landed Cost code & amount associated with that cost in a Pre-Totals script.
This is what I have saved where they actually had 3 of these in play, so depending on what cost code was populated on the Lines would tally up to the Header. Here is one of the three in play. The key part is that last 5 lines; this will write the Landed Cost code for you. Hope this helps.
sReceiptType = ""
sReceiptNo = ""
nFreightCar = 0
sCostTypeCar = ""
retVal = oBusObj.GetValue("ReceiptType$",sReceiptType)
retVal = oBusObj.GetValue("ReceiptNo$",sReceiptNo)
retVal = oBusObj.GetValue("UDF_FREIGHT_FOR_LC",nFreightCar)
retVal = oBusObj.GetValue("UDF_LC_FREIGHT_CODE$",sCostTypeCar)
Set oLCReceipt = oBusObj.AsObject(oBusObj.PO_LandedCostReceiptObj)
sKeyPadded = sReceiptType & sReceiptNo & sCostTypeCar
retVal = oLCReceipt.SetKey(sKeyPadded)
retVal = oLCReceipt.SetValue("LandedCostAmt",nFreightCar)
retWrite = oLCReceipt.Write()
------------------------------
Dana Young
Lehman Wesley
Lansing, MI
------------------------------