Reputation: 222
I have requirement in Hybris as when user will place any order we are submitting order to SAP by cron job and SAP is processing the order and updating back to Hybris with updated order number and other details. We want to keep the original copy in Hybris how to achieve this?
Upvotes: 1
Views: 2993
Reputation: 2989
When you create a snapshot Hybris create a deep copy of the Order (creates a copy of all sub-elements), to differentiate snapshots and original orders Hybris adds a versionID and a reference to the origin version to snapshots (originalVersion, versionId). From the origin Order you can see the list of all snapshots from the Order History Tab.
From the HMC you will see all duplicated orders but from a user perspective you will see only the original versions as the Customer Account DAO only loads Orders without versionID '{" + OrderModel.VERSIONID + "} IS NULL'
Example using Groovy :
import de.hybris.platform.orderhistory.OrderHistoryService
import de.hybris.platform.servicelayer.search.FlexibleSearchService
import de.hybris.platform.store.services.BaseStoreService
import de.hybris.platform.core.model.order.OrderModel
import de.hybris.platform.core.model.user.UserModel
import de.hybris.platform.orderhistory.model.OrderHistoryEntryModel
import de.hybris.platform.servicelayer.model.ModelService
import de.hybris.platform.servicelayer.user.UserService
import java.util.Collection
import java.util.Iterator
import de.hybris.platform.store.BaseStoreModel
import de.hybris.platform.commerceservices.customer.dao.CustomerAccountDao
BaseStoreService bss = spring.getBean("baseStoreService")
UserService us = spring.getBean("userService")
FlexibleSearchService fss = spring.getBean("flexibleSearchService")
OrderHistoryService ohs =spring.getBean("orderHistoryService")
ModelService ms = spring.getBean("modelService")
CustomerAccountDao cad = spring.getBean("customerAccountDao")
BaseStoreModel baseStore = bss.getBaseStoreForUid("electronics")
UserModel user = userService.getUserForUID("customerUID")
Collection<OrderModel> orders = user.getOrders()
OrderModel order = cad.findOrderByCodeAndStore("orderCode", baseStore)
OrderModel orderSnap = ohs.createHistorySnapshot(order)
OrderHistoryEntryModel entry = modelService.create(OrderHistoryEntryModel.class)
entry.setTimestamp(new Date())
entry.setOrder(order)
entry.setDescription("Took a snap")
entry.setPreviousOrderVersion(orderSnap)
ohs.saveHistorySnapshot(orderSnap)
modelService.saveAll( order, entry, orderSnap )
OrderHistoryService :
public abstract interface OrderHistoryService
{
public abstract OrderModel createHistorySnapshot(OrderModel paramOrderModel);
public abstract void saveHistorySnapshot(OrderModel paramOrderModel);
public abstract Collection<OrderModel> getHistorySnapshots(OrderModel paramOrderModel);
public abstract Collection<OrderHistoryEntryModel> getHistoryEntries(OrderModel paramOrderModel, Date paramDate1, Date paramDate2);
public abstract Collection<String> getHistoryEntriesDescriptions(OrderModel paramOrderModel, Date paramDate1, Date paramDate2);
public abstract Collection<OrderHistoryEntryModel> getHistoryEntries(OrderModel paramOrderModel, EmployeeModel paramEmployeeModel);
public abstract Collection<OrderHistoryEntryModel> getHistoryEntries(UserModel paramUserModel, Date paramDate1, Date paramDate2);
}
Upvotes: 2
Reputation: 812
In order to create order versioning there are just two steps to accomplish:
creating a new OrderHistoryEntry
Create and attach a snapshot.
more details are here
Upvotes: 0