Explain core concept and working of Calculate() and recalculate() methods of calculationService in Hybris ?

During customizing order and cart there is used calculation service which is very important for customizing as well as in interview. SO today I decided to write my outcomes related to calculate() and recalculate() methods of calculation service.
As you know both Cart and Order is subtype of AbstractOrder so more or less every attributed is same for both item
FYI – During checkout finally Cart is into converted to Order means Copy all data from Cart to Order and cart number converted to order number.
Both Carts and Orders have an attribute called calculated of type Boolean that states whether they are calculated, or not. Now next what will calculate?
Boolean calculated;
In every cart or an order have price, tax, discount etc so this need to be calculate.
So on basis of Boolean attribute calculated there is call of CalulationService's method calculate() and recalculate() methods.
So whenever below action is performed on a Cart or an Order, the Cart or Order’s Boolean property (calculated) which is set to be FALSE
1. Product is be added to the cart
2. Product is be removed from the cart
3. User is changed
4. Currency is changed

So two important method call on basis of calculated=FALSE which is CalculationService.calculate() or CalculationService.recalculate() method
Before going in deep let’s see below
CART
<itemtype code="Cart" extends="AbstractOrder" jaloclass="de.hybris.platform.jalo.order.Cart" autocreate="true" generate="true">
<deployment table="Carts" typecode="43"/>
<attributes>
<attribute autocreate="true" redeclare="true" qualifier="entries" type="CartEntryCollection"/>
<attribute type="java.lang.String" qualifier="sessionId">
 <persistence type="property"/>
 <modifiers read="true" write="true"/>
 </attribute>
 </attributes>
</itemtype>

ORDER

<itemtype code="Order" extends="AbstractOrder" jaloclass="de.hybris.platform.jalo.order.Order" autocreate="true" generate="true">
<deployment table="Orders" typecode="45" propertytable="OrderProps"/>
<attributes>
<attribute autocreate="true" redeclare="true" qualifier="entries" type="OrderEntryCollection"/>
</attributes>
 </itemtype>
                               
Form above item definition every cart has cartEntry and every order has OrderEntry 
FYI- Every cartEntry and orderEntry their is also has Boolean attribute calculated
So depending on  every cartEntry's Boolean attribute value it call CalculationService.calculate() method to calculate their price, tax, discount etc of that cartEntry.
Concept: If there is something change in cartEntry then after also Cart’s calculated Boolean attribute become FALSE 
 Final Conclusion
1. If you adding any product into cart then
 CartEntry's calculated attribute = FALSE
Cart's calculated attribute = FALSE
So first there will call CalculationService.calculate to calculate cartEntry total after finish this call CartEntry's calculated attribute will set to TRUE then after will call CalculationService.recalculate()  to calculate cart TOTAL and after finish this call cart' calculated attribute will set to TRUE
   
2. If again any product is add to same cart then
 CartEntry's calculated attribute = FALSE. If it is FALSE then again Cart's calculated attribute = FALSE So there will again call CalculationService.calculate() method to calculate for these cartEntry and finally again it set to= TRUE after calculate for these newly entered cartEntry and after again call CalculationService.recalculate() to calculate Cart Total.


3 comments:

  1. Very well explained. Thanks a lot.
    There is one small doubt, how is the CartEntry's calculated attribute set to true or false? Is there any function which is called to change the values?

    ReplyDelete
    Replies
    1. Cart customer care number9883223475//7477869880=
      Cart customer care number9883223475//7477869880=

      Cart customer care number9883223475//7477869880=
      Cart customer care number9883223475//7477869880=

      Cart customer care number9883223475//7477869880=
      Cart customer care number9883223475//7477869880=


      Cart customer care number9883223475//7477869880=
      Cart customer care number9883223475//7477869880=

      Cart customer care number9883223475//7477869880=
      Cart customer care number9883223475//7477869880=


      Cart customer care number9883223475//7477869880=
      Cart customer care number9883223475//7477869880=a

      Delete
  2. Cart customer care number9883223475//7477869880Cart customer care number9883223475//7477869880==Cart customer care number9883223475//7477869880=

    ReplyDelete