Hi all,
I need to calculate a total value for a master entity that has four slave entity.
It’s like an sales order with more rows.
I created a field in the master edit form with where I need to write the sum of all amount exiting in all four slave entity linked to the first.
But I don’t have any idea the way to solve this problem.
Do you have an example to understand a way to solve my question?
thank you
Hi Corrado,
As I understand you have two separate screens Browser and Editor and you would like to calculate some aggregated value for the Entity in its editor. So, you can override the ‘’‘preCommit’’’ method in your editor and set the aggregated value to the corresponding field. See an example below:
public class OrderItemEdit extends AbstractEditor<OrderItem> {
@Override
protected boolean preCommit() {
BigDecimal subTotal = getItem()
.getProduct()
.getPrice()
.multiply(BigDecimal.valueOf(getItem().getQuantity()));
getItem().setSubTotal(subTotal);
return super.preCommit();
}
}
If I got you wrong, could you please elaborate the problem? A sample project, illustrating your question would be very helpful.
Regards,
Aleksey
Hi Aleksey,
thank you so much for your answer.
but I’m not sure to understand your suggestion.
Please read my document attached.
thank you
Edit form.docx (790.8K)
Corrado,
Finally, got your question. I created a sample application and attached to this post. Open it in CUBA Studio and have a look at the
OrderEdit
controller of the
order-edit.xml
screen. It sums the
amount
fields of each entity instance in two compositions.
Regards,
Aleksey
total-field.zip (42.2K)
great!
tomorrow morning I’ll try immediatly.
thank you again.
HI Aleksey,
I’m tring to add your code in my project.
But I have an issue when I try to enable the following string
payrolldetailsnew1Ds.addCollectionChangeListener(e -> getItem().setTotalAmount(calculateTotalAmount()));
this is the message when I try to execute the project.
:app-web:compileJavaC:\Users\schiavoc\studio-projects\pafootball\modules\web\src\com\palermocalcio\pafootball\web\payrollmaster_new\PayrollMaster_NewEdit.java:39: error: ‘void’ type not allowed here
payrolldetailsnew1Ds.addCollectionChangeListener(e -> getItem().setTotalAmount(calculateTotalAmount()));
^
Note: C:\Users\schiavoc\studio-projects\pafootball\modules\web\src\com\palermocalcio\pafootball\web\utilities\ImportFilePaghe.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
FAILED
Corrado,
Hm… Seems
claculateTotalAmount()
is declared as a
void
method. Make it to return the value, same type as the
totalAmount
field, it should solve the problem.
Regards,
Aleksey
your solution is working.
thank you again.
can I make other question?
Sure, just start a separate topic.
This is my actual issue.
in the totalamount field (that now is working) I need to calculate the sign.
For each of forth slave entity I have the ‘amount’ field and the ‘sign’ field has two possible value ‘D’ for debit sign and ‘A’ for the credit sign.
So, if the sign is equal to ‘D’ the amount is positive. If the sign is equal to ‘A’ the amount is negative.
So, what’s the problem? Just change the logic in the
claculateTotalAmount()
method. Modify lambdas to a simple
for
loop, check each item to have ‘D’ or ‘A’ type and do
BigDecimal#add
or
BigDecimal#substract
, depending on the check result.
This is nothing to do with CUBA, just general java. We do provide project-related support in case if you need the help of our specialists. You can find more information about consultancy service here.
ok.
thanks
Hi Alekesy,
If I need to have a support to develop a single part of calculation like a sum or a duplicate copy or other in my application is there someone that help me to find a solution and solve my issue?
thank you.