public async Task <ActionResult> Create([FromBody] SaveCreditMemoRequest entity)
        {
            try
            {
                var creditMemo = mapper.Map <CreditMemo>(entity);
                await context.CreditMemos.AddAsync(creditMemo);

                // Adjust the inventory quantities for items returned
                var itemsToBeReturned = creditMemo.LineItems.Where(a => a.ReturnedToInventory);
                await inventoryService.ProcessAdjustments(itemsToBeReturned, AdjustmentType.Add, Constants.AdjustmentRemarks.CreditMemoCreated, QuantityType.Both);

                // Update the order line item for quantity returned
                await orderService.ProcessReturns(creditMemo.LineItems, AdjustmentType.Add);

                await context.SaveChangesAsync();

                return(StatusCode(StatusCodes.Status201Created));
            }
            catch (QuantityReturnedException qEx)
            {
                logger.LogError(qEx.Message);
                ModelState.AddModelError(Constants.ErrorMessage, qEx.Message);

                return(BadRequest(ModelState));
            }
            catch (Exception e)
            {
                logger.LogError(e.Message);
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
        public async Task <ActionResult> Update(long id, [FromBody] SaveCreditMemoRequest updatedCreditMemo)
        {
            try
            {
                var creditMemo = context.CreditMemos
                                 .Include(c => c.Customer)
                                 .Include(c => c.LineItems).ThenInclude(detail => (detail as CreditMemoLineItem).TransactionHistory)
                                 .Include(c => c.LineItems).ThenInclude(detail => (detail as CreditMemoLineItem).OrderLineItem).ThenInclude(orderLineItem => orderLineItem.Item)
                                 .AsNoTracking()
                                 .SingleOrDefault(c => c.Id == id);

                if (creditMemo == null)
                {
                    return(NotFound());
                }

                // Adjust inventory quantities
                var itemsToBeCleared = creditMemo.LineItems.Where(a => a.ReturnedToInventory);
                await inventoryService.ProcessAdjustments(itemsToBeCleared, AdjustmentType.Deduct, Constants.AdjustmentRemarks.CreditMemoUpdated, QuantityType.Both);

                var itemsToBeReturned = updatedCreditMemo.LineItems.Where(a => a.ShouldAddBackToInventory);
                await inventoryService.ProcessAdjustments(itemsToBeReturned, AdjustmentType.Add, Constants.AdjustmentRemarks.CreditMemoUpdated, QuantityType.Both);

                // Update order line items for quantity returned
                await orderService.ProcessReturns(creditMemo.LineItems, AdjustmentType.Deduct);

                await orderService.ProcessReturns(updatedCreditMemo.LineItems, AdjustmentType.Add);

                // Process deleted line items
                entityListHelpers.CheckItemsForDeletion(creditMemo.LineItems, updatedCreditMemo.LineItems);

                // Update the credit memo values
                creditMemo = mapper.Map <CreditMemo>(updatedCreditMemo);

                context.Update(creditMemo);
                await context.SaveChangesAsync();

                return(StatusCode(StatusCodes.Status204NoContent));
            }
            catch (QuantityReturnedException qEx)
            {
                logger.LogError(qEx.Message);
                ModelState.AddModelError(Constants.ErrorMessage, qEx.Message);

                return(BadRequest(ModelState));
            }
            catch (DbUpdateConcurrencyException concurrencyEx)
            {
                logger.LogError(concurrencyEx.Message);
                return(StatusCode(StatusCodes.Status409Conflict, Constants.ErrorMessages.ConcurrencyErrorMessage));
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }