0
votes

I am trying to do a few simple calculations on both the SOLine level (document details grid) and the SOOrder Level (Order Summary Area).

Thanks to another stackoverflow user, I got the first SOLine calculation figured out (Field called Total profit (Ext Price - Ext Cost). Now, I am trying to calculate the percentage of the Total profit divided by the Ext price (on the SOLine level) but am having issues. Is this something that’s possible? The function doesn’t seem to recognize the new field. So I did it like this and it’s giving a “cannot divide by zero” error…. Is this wrong?

Here’s my first try with the new custom field (the TotalProfit field is already defined and confirmed working - but I am having issues with this GP% one):

    [PXUIField(DisplayName = "GP %", Enabled = false)]
    [PXFormula(typeof(Div<SOLine.curyTotalProfit, SOLine.curyLineAmt>))]
    [PXDefault(TypeCode.Decimal, "0.0")]

And here is the second one I tried that takes the Total profit formula and divides the result by the Ext price (curylineamt) 

    [PXUIField(DisplayName = "GP %", Enabled = false)]
    [PXFormula(typeof(Div<Sub<SOLine.curyLineAmt, SOLine.curyExtCost>,SOLine.curyLineAmt>))]
    [PXDefault(TypeCode.Decimal, "0.0")]

The second thing I need to do is to display (on the Order Summary level) 1. The sum of all the SOLines TotalProfit and 2. The percentage of each line's total profit divided by the total ext Cost. - For this one, I know that I have to define the pxparent attribute, but all of my attempts have failed. Here is what I tried:

I created two new fields -( one for the currency consideration):

    **(FIRST FIELD):**
    [PXParent ( typeof(Select<SOOrder,Where<SOLine.OrderNbr, Equal<Current<SOOrder.OrderNbr>>>>))]
    [PXDBCurrency(typeof(SOOrder.curyInfoID), typeof(SOOrder.usrOrderTotalProfit))]
    [PXUIField(DisplayName = "Total Profit", Enabled = false)]
    [PXFormula(null, typeof(SumCalc<SOLine.usrCuryTotalProfit>))]
    [PXDefault(TypeCode.Decimal, "0.0")]

    **(SECOND FIELD):**
    [PXDBDecimal(4)]
    [PXDefault(TypeCode.Decimal, "0.0")]

When I try to publish this, I get errors saying that the type name does not exist and one that says it is a property but is used like a type.

These seem like they should be built into Acumatica, but they are not. Any help is greatly appreciated.

Update:

Hi Dmitry, Thanks so much for your response. I am still stuck on the GP% one...I am trying to calculate my custom field (UsrCuryTotalProfit) divided by curyLineAmt. This is my attribute for the UsrCuryTotalProfit field:

    [PXDBCurrency(typeof(SOLine.curyInfoID), typeof(SOLineExt.usrTotalProfit))] 
    [PXUIField(DisplayName = "Total Profit", Enabled = false)]               
    [PXFormula(typeof(Sub<SOLine.curyLineAmt, SOLine.curyExtCost>))]    
    [PXDefault(TypeCode.Decimal, "0.0")] 

So, I used this for my GP% field:

    [PXUIField(DisplayName = "GP %", Enabled = false)] 
    [PXFormula(typeof(Div<SOLineExt.usrTotalProfit, SOLine.curyLineAmt>))] 
    [PXDefault(TypeCode.Decimal, "0.0")] 

And it published fine, but when I go to the Sales Order screen and try to add a line in the grid, it gives me an error: "Attempted to Divide by Zero". Am I still doing something wrong here?

I wonder if this has something to do with the row not having any cost or price info yet, but the formula tries to divide before I enter anything in...Not sure how to fix.

1

1 Answers

1
votes
[PXUIField(DisplayName = "GP %", Enabled = false)]
[PXFormula(typeof(Div<SOLineTotalProfit, SOLine.curyLineAmt>))]
[PXDefault(TypeCode.Decimal, "0.0")]

Is this actual code you are trying to publish? I think there is a typo in this code. Should be something like this:

[PXFormula(typeof(Div<SOLine.curyTotalProfit, SOLine.curyLineAmt>))]

You also have an incorrect parent attribute. You should select parent table that matches to the current record. Like that:

[PXParent(typeof(Select<SOOrder,Where<SOOrder.orderNbr, Equal<Current<SOLine.orderNbr>>>>))]

However, such attribute is already present in SOLine class, you don't need to place it again.

Your Formula

[PXFormula(null, typeof(SumCalc<SOLine.usrCuryTotalProfit>))]

should be placed on the SOLine.usrCuryTotalProfit field. In the second parameter you should specify the field of the parent DAC where result should be stored. Parent and formula attributes are covered well in T200 training Lesson 7.

So there are several errors in your code. You should also pay more attention to the case of the first letters.

Here is the quote from T100 training:

The abstract class and property have the same name that differ by the case of the first letter. By convention, the abstract class name starts with a lowercase letter, while the property name starts with the same uppercase letter. In BQL statements, the data field is referred to by the abstract class name,

Visual studio is nice tool to fix typos and case of the letters. You can easily open your customization in visual studio:

  1. Go to the Customization Projects
  2. Click on your project
  3. Click Extension Library -> Create new
  4. Browser will download *.bat file that will open your customization in visual studio.
  5. You will be able to see and fix compilation errors, use autocomplete and syntax highlighting.

Update:

I wonder if this has something to do with the row not having any cost or price info yet, but the formula tries to divide before I enter anything in...Not sure how to fix.

Looks like you are right. You can fix it like that:

 [PXFormula(typeof(Switch<Case<Where<SOLine.curyLineAmt, Equal<decimal0>>, decimal0>, Div<SOLineExt.usrTotalProfit, SOLine.curyLineAmt>>))] 

Also I think you should use SOLineExt.curyUsrTotalProfit in this case.