cancel
Showing results for 
Search instead for 
Did you mean: 

Add ReturnSalesItem in Receipt using JAVA

haythem_jaidi
Participant
0 Kudos

Hi,

I am developping a CCO plugin to manage Returns. The purpose is to add the ReturnSalesItem directly in the Receipt following some business rules.

I tried to use ReturnSalesItemPosService but I did not succeed.

Here is my code that did not work:

CDBSession session = CDBSessionFactory.instance.createSession();
ReceiptManager receiptManager = new ReceiptManager(session);
ReceiptEntity receipt = receiptManager.findOrCreate(UserRegistry.INSTANCE.getCurrentUser(), null, false);
.
.
.
ReturnSalesItemPosService returnSalesItemPosService = ServiceFactory.INSTANCE.getOrCreateServiceInstance(ReturnSalesItemPosService.class, session);

try {
	returnSalesItemPosService.returnSalesItemInSameReceipt(receipt, sie, new BigDecimal(-1), "1", "01", true);
} catch (ValidationException e) {
	throw new RuntimeException(e);
}

receiptManager.deleteSalesItem(receipt, sie);

receiptManager.calculate(receipt, BaseEntity.EntityActions.UPDATE);
receiptManager.update(receipt);

BroadcasterHolder.INSTANCE.getBroadcaster().broadcastPluginEventForPath("RECEIPT_REFRESH", null);
View Entire Topic
R_Zieschang
Contributor
0 Kudos

Hi haythem.jaidi,

if I understand this correctly you have some kind of purchase at the POS? And this purchase is not based on a prior sale, right?
Why use the ReturnReceiptPosService? The intention of this service is to create receipts (returns) based on a sales receipt.

Use the EntityFactory to create the sales item with negative quantity (adapt to your needs, the material entity can be retrieved with the MaterialManager). Add the sales item with the ReceiptPosService.

ReceiptPosService posService = ServiceFactory.INSTANCE.getOrCreateServiceInstance(ReceiptPosService.class, session);
SalesItemEntity salesItem = EntityFactory.INSTANCE.createSalesItemEntity(material, "Sales Item Description", grossAmount, BigDecimal.valueOf(-1), "EA"); posService.addSalesItem(receipt, salesItem);

HTH
Robert

haythem_jaidi
Participant
0 Kudos

Hi robertzieschang,

Thank you for the reply.

I already tested this but unfortunately it does not work. I have the below error message "Quantity must be positive".

That's why I thought about the ReturnSalesItem.

Any hints on how to solve this error message?

Best Regards

Haythem