Need Sales Order Header Script to compute Standard Cost based Margin on ENTIRE Order, on totals tab.

I'm looking for existing script I can use to simplify our need to run a custom report during our quoting process for every order.  Can anyone help me create a script that will calculate the entire Gross Profit/Margin for the whole Sales Order and display it in a read only field either during Sales Order Detail entry or on the Totals Tab of sales order entry.  I've seen some similar type of scripting (for individual lines) or that displays message boxes (don't want message boxes popping).  I just want a calculation that I can easily write in a custom crystal report that fires as each line changes and records that calculated field in a SO HEader UDF and then displays that field in real time during order entry.  This sounds so simple, right? 

I don't script - so it seems hard to me.

I might be willing to pay someone for this - before I waste time hacking my way through it.  :)

Thanks a head of time to all the great people who contribute in these forums.  I love it.  So helpful!

Brad

  • 0
    You can create a UDF on the header that will automatically total a detail field. You need a detail UDF to calculate the margin per line, then total this in the header UDF.
  • 0 in reply to 49153
    Thank you so much for that response. I just researched and I see how I can add a UDF on SO Header that is based on Business Object Total from Sales Order Detail. This is great. Now - I just need to understand how to create a simple script to calculate the STD Cost Based Margin for each Detail line, right?
  • 0 in reply to Brad K
    Add a UDF named Margin to SO_SalesOrderDetail, this will be a numeric field.
    Add a UDF to SO_SalesOrderHeader: Business Object = SO_SalesOrder_Bus, Data Source = <Total From SO_SalesOrderDetail>, Column = UDF_MARGIN
    Assuming you got this far... next you need a script
  • 0 in reply to 49153

    In User-Defined Field and Table Maintenance right click SO Sales Order Detail and select User-Defined Scripts.
    Click the Add button:
    Event = Column - Post Validate
    Field = QuantityOrdered
    Script = *give it a name*, something like SOSalesOrderDetailPostValidate and click the Create/Edit Script button. Click Yes when prompted to create the file.

    Copy/paste the following:
    *************************
    ExtPrice = 0
    Qty = 0
    UnitCost = 0
    Margin = 0

    'Get Extended Price, Quantity Ordered, Unit Cost
    retVal = oBusObj.GetValue("ExtensionAmt", ExtPrice)
    retVal = oBusObj.GetValue("QuantityOrdered", Qty)
    retVal = oBusObj.GetValue("UnitCost", UnitCost)

    Margin = ExtPrice - (Qty * UnitCost)

    'Set Margin
    retVal = oBusObj.SetValue("UDF_MARGIN", Margin)
    **************************

    Click Save and Accept
    Close User-Defined Scripts
    Close User-Defined Field and Table Maintenance
    Click Compile on Script Compile
    Close Script Compile


    Done - now test the Margin UDF and tweak as needed.

  • 0 in reply to 49153
    I am amazed it this great help! Thank you so much! I did all as you showed and it is working just as you described. I am needing to make the following tweaks to make it work as we need - and i'm curious if you have any suggestions - I understand this may cause this project to get less cut and dried and more out in the weeds - but nonetheless - this would make it work for our needs:

    I adjusted what you said above to utilize the Standard Unit Cost field from CI_Item. We need the margin calculations to show and be based off of Standard Unit Cost - which is not naturally in the Sales Order Detail table. SO - I added UDF for that and based it on CI_ITem business object so that it populates as new items are keyed into the Sales Order. I can also then use this UDF_Standard_Unit_Cost in calculating the UDF_StdCostMargin. All is well.

    Problem: We use BOM lots and lots - but we don't maintain BOM standard unit costs in the inventory file. Also when we explode the kits on a sales order/quote, which is how we do ALL of them, it just brings in Standard Item Cost information for the exploded kit part numbers - not the components - as it should and I understand why - but I need to use the components standard costs to understand margins. Business Reason: Our BOM kits are always works in progress...depending on what the customer wants and what we currently have in stock - we may change the component items directly on the Sales ORder Detail as we go - still working with exploded kits and component items that don't have UDF_Standard_Unit_Cost info to use in the margin.

    Question: Is there an easy way I am overlooking that can be accessed from a script that can find and use the standard unit cost for these component items as they are added/subtracted/adjusted from a sales order while it is a work in progress: Each time...the chief and primary fruit that I need is to be able to show the UDF_StdCostMargin in the sales order detail and header screens as they go -so that they can stop running custom reports after all changes in order to see margin calculations.

    I'm sorry to throw all this out there - but now that I'm in this process - I'm not sure how to get this to work considering all of the BOM kit exploding that we do on most quotes and sales orders. Thanks so much in advance for any clues or help you can provide. I'm already blown away at how helpful you have been. So thanks!!!!! :)
  • 0 in reply to Brad K
    On the detail line you want to add an event script to fetch the component standard cost, you will need to create an object to open CI_Item.
  • 0 in reply to Brad K
    Look at this script, the same principal applies:
    sagecity.na.sage.com/.../97135
  • 0 in reply to 49153
    I've looked at that script. I think it makes sense. I tried something similar. As my second object to open CI_ITem, I'm opening SO_SalesOrderDetail_bus. not sure if that is right. When I use the model code and just do something simple to SetKey, it always returns 0. But my variable that I"m using to set the key (tmpItemCode$) is populated. I'm missing something. But moving further, I'm guessing I will Set the Key on the new object and then be able to use that record to find the UDF I want to populate on the SO Detail record of the current and first business object open in the script that I am trying to edit.

    (I apologize for my wrong terms and language I'm using - i'm an old school DB/SQL guy who used to do a bit of programming - but have been leading a business for 14 years now and don't remember much - but enough to cause problems... :)
  • 0 in reply to Brad K
    Add a UDF to SO_SalesOrderDetail for the standard cost and pull in the standard cost from CI_Item:
    Business Object = SO_SalesOrderDetail_Bus
    Data Source= ItemCode
    Column = your standard cost UDF

    I think that should make it easier to do your margin calculation, unless I missed something from above.
  • 0 in reply to 49153
    I currently do that - but for BOM Kit items that explode - This automatic UDF connection based on SO_SalesOrderDetail_Bus does not populate the standard cost for exploded kit component items. This is why I need to do this through Script for every item on the sales order as it is added to the SO Detail. Make Sense? I just need a few more details to get this scrip running to pull in standard cost through script on post validate events on item code in detail connecting to a second object based on item code to pull in standard unit cost from CI_Item table.