@ApiMethod(name = "addInvoice") public void addInvoice(InvoiceEntity invoiceEntity) { if (invoiceEntity.getId() != null) { ReceivableEntity receiveByID = ofy().load().type(ReceivableEntity.class).id(invoiceEntity.getId()).now(); receiveByID.setCustomer(invoiceEntity.getCustomer()); receiveByID.setFinalTotal(invoiceEntity.getFinalTotal()); receiveByID.setInvoiceDate(invoiceEntity.getInvoiceDate()); receiveByID.setInvoiceDueDate(invoiceEntity.getInvoiceDueDate()); receiveByID.setInvoiceId(invoiceEntity.getId()); receiveByID.setBusiness(invoiceEntity.getBusiness()); receiveByID.setCreatedDate(invoiceEntity.getCreatedDate()); receiveByID.setModifiedDate(invoiceEntity.getModifiedDate()); receiveByID.setModifiedBy(invoiceEntity.getModifiedBy()); ofy().save().entity(receiveByID).now(); } else { ReceivableEntity receivableEntity = new ReceivableEntity(); receivableEntity.setCustomer(invoiceEntity.getCustomer()); receivableEntity.setFinalTotal(invoiceEntity.getFinalTotal()); receivableEntity.setInvoiceDate(invoiceEntity.getInvoiceDate()); receivableEntity.setInvoiceDueDate(invoiceEntity.getInvoiceDueDate()); receivableEntity.setInvoiceId(invoiceEntity.getId()); receivableEntity.setBusiness(invoiceEntity.getBusiness()); receivableEntity.setCreatedDate(invoiceEntity.getCreatedDate()); receivableEntity.setModifiedDate(invoiceEntity.getModifiedDate()); receivableEntity.setModifiedBy(invoiceEntity.getModifiedBy()); ofy().save().entity(receivableEntity).now(); } if (invoiceEntity.getId() == null) { invoiceEntity.setCreatedDate(new Date()); // stockItemEntity.setModifiedDate(new Date()); } else { invoiceEntity.setModifiedDate(new Date()); } ofy().save().entity(invoiceEntity).now(); System.out.println(invoiceEntity.getInvoiceLineItemList()); List<StockItemEntity> stockItemEntity = ofy().load().type(StockItemEntity.class).list(); /* For Reduce the Stock Quantity */ for (int i = 0; i < invoiceEntity.getInvoiceLineItemList().size(); i++) { for (int j = 0; j < stockItemEntity.size(); j++) { if (invoiceEntity .getInvoiceLineItemList() .get(i) .getItemName() .equals(stockItemEntity.get(j).getItemName())) { StockItemEntity a = stockItemEntity.get(j); a.setQty( (stockItemEntity.get(j).getQty()) - (Integer.valueOf((invoiceEntity.getInvoiceLineItemList().get(i).getQty())))); ofy().save().entity(a).now(); } } } /* For Add in ReceivableEntity */ }