public async Task <ActionResult> Update(long id, [FromBody] SaveItemRequest entity)
        {
            try
            {
                var item = await context.Items
                           .AsNoTracking()
                           .SingleOrDefaultAsync(t => t.Id == id);

                if (item == null)
                {
                    return(NotFound(id));
                }

                item = mapper.Map <Item>(entity);
                context.Update(item);
                await context.SaveChangesAsync();

                return(StatusCode(StatusCodes.Status204NoContent));
            }
            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));
            }
        }
        public async Task <ActionResult> Update(long id, [FromBody] SaveUserRequest request)
        {
            try
            {
                var user = await context.Users
                           .AsNoTracking()
                           .SingleOrDefaultAsync(t => t.Id == id);

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

                var oldPassword = user.PasswordHash;
                user = mapper.Map <ApplicationUser>(request);
                user.PasswordHash = request.UpdatePassword ?
                                    passwordHasher.HashPassword(user, request.Password) :
                                    oldPassword;

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

                return(StatusCode(StatusCodes.Status204NoContent));
            }
            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));
            }
        }
        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));
            }
        }
        public async Task <ActionResult> Update(long id, [FromBody] SaveOrderRequest entity)
        {
            try
            {
                var order = await context.Orders
                            .Include(a => a.Customer)
                            .Include(a => a.LineItems).ThenInclude(detail => (detail as OrderLineItem).TransactionHistory)
                            .AsNoTracking()
                            .SingleOrDefaultAsync(t => t.Id == id);

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

                // Process inventory adjustments
                await inventoryService.ProcessAdjustments(order.LineItems, AdjustmentType.Add, Constants.AdjustmentRemarks.OrderUpdated, QuantityType.Quantity);

                await inventoryService.ProcessAdjustments(entity.LineItems, AdjustmentType.Deduct, Constants.AdjustmentRemarks.OrderUpdated, QuantityType.Quantity);

                // Process deleted line items
                entityListHelpers.CheckItemsForDeletion(order.LineItems, entity.LineItems);

                // Update the order data
                order = mapper.Map <Order>(entity);

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

                return(StatusCode(StatusCodes.Status204NoContent));
            }
            catch (QuantityBelowZeroException ex)
            {
                logger.LogError(ex.Message);

                ModelState.AddModelError(Constants.ErrorMessage, ex.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));
            }
        }
        public async Task <ActionResult> Update(long id, [FromBody] SaveInvoiceRequest entity)
        {
            try
            {
                var invoice = await context.Invoices
                              .Include(c => c.LineItems)
                              .AsNoTracking()
                              .SingleOrDefaultAsync(c => c.Id == id);

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

                // Revert the quantities
                var oldOrderIds       = invoice.LineItems.Where(a => a.OrderId != null).Select(a => a.OrderId.Value);
                var oldOrderLineItems = context.OrderLineItems.Where(a => oldOrderIds.Contains(a.OrderId));
                await inventoryService.ProcessAdjustments(oldOrderLineItems, AdjustmentType.Add, Constants.AdjustmentRemarks.InvoiceUpdated, QuantityType.StockQuantity);

                var newOrderIds       = entity.LineItems.Where(a => a.OrderId != null).Select(a => a.OrderId.Value);
                var newOrderLineItems = context.OrderLineItems.Where(a => newOrderIds.Contains(a.OrderId));
                await inventoryService.ProcessAdjustments(newOrderLineItems, AdjustmentType.Deduct, Constants.AdjustmentRemarks.InvoiceUpdated, QuantityType.StockQuantity);

                await orderService.ProcessInvoice(invoice.LineItems, false);

                await orderService.ProcessInvoice(entity.LineItems, true);

                // Process deleted line items
                entityListHelpers.CheckItemsForDeletion(invoice.LineItems, entity.LineItems);

                // Update the invoice data
                invoice = mapper.Map <Invoice>(entity);

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

                return(StatusCode(StatusCodes.Status204NoContent));
            }
            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));
            }
        }
Beispiel #6
0
        public async Task <ActionResult> Update(long id, [FromBody] SaveDeliveryRequest entity)
        {
            try
            {
                var delivery = await context.Deliveries
                               .Include(c => c.LineItems)
                               .AsNoTracking()
                               .SingleOrDefaultAsync(a => a.Id == id);

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

                // Process the deliveries
                await orderService.ProcessDeliveries(delivery.LineItems, AdjustmentType.Deduct);

                await orderService.ProcessDeliveries(entity.LineItems, AdjustmentType.Add);

                // Process deleted line items
                RemoveZeroLineItems(entity);
                entityListHelpers.CheckItemsForDeletion(delivery.LineItems, entity.LineItems);

                delivery = mapper.Map <Delivery>(entity);

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

                return(StatusCode(StatusCodes.Status204NoContent));
            }
            catch (QuantityDeliveredException dEx)
            {
                logger.LogError(dEx.Message);

                ModelState.AddModelError(Constants.ErrorMessage, dEx.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));
            }
        }
        public async Task <ActionResult> Update(long id, [FromBody] SaveSalesQuoteRequest entity)
        {
            try
            {
                var salesQuote = await context.SalesQuotes
                                 .Include(a => a.LineItems)
                                 .AsNoTracking()
                                 .SingleOrDefaultAsync(t => t.Id == id);

                if (salesQuote == null)
                {
                    return(NotFound(id));
                }

                // Process deleted line items
                entityListHelpers.CheckItemsForDeletion(salesQuote.LineItems, entity.LineItems);

                // Update the sales quotation to new values
                salesQuote = mapper.Map <SalesQuote>(entity);

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

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