Exemplo n.º 1
0
        public HttpResponseMessage CancelInvoice(Guid id)
        {
            var response = Request.CreateResponse(HttpStatusCode.OK);

            try
            {
                // Get the invoice
                var invoice = _invoiceService.GetByKey(id);

                // Cancel all orders
                var orders = _orderService.GetOrdersByInvoiceKey(id);

                // TODO - This is a bit of a hack, must be a better way to create the order and invoice status??

                // Create a cancelled order status
                var cancelledOrderStatus = new OrderStatus
                {
                    Key        = Constants.OrderStatus.Cancelled,
                    Alias      = "cancelled",
                    Name       = "Cancelled",
                    Active     = true,
                    Reportable = false,
                    SortOrder  = 1,
                    CreateDate = DateTime.Now,
                    UpdateDate = DateTime.Now
                };

                // Cancel each one
                foreach (var order in orders)
                {
                    // Set the order status
                    order.OrderStatus = cancelledOrderStatus;

                    // Save the order
                    _orderService.Save(order);
                }

                // Not sure if I need to do this??
                var cancelledInvoiceStatus = new InvoiceStatus
                {
                    Key        = Constants.InvoiceStatus.Cancelled,
                    Alias      = "cancelled",
                    Name       = "Cancelled",
                    Active     = true,
                    Reportable = false,
                    SortOrder  = 1,
                    CreateDate = DateTime.Now,
                    UpdateDate = DateTime.Now
                };

                invoice.InvoiceStatus = cancelledInvoiceStatus;

                // Save the invoice
                _invoiceService.Save(invoice);

                // Set an audit log
                var message = string.Format("Order cancelled by {0}", CurrentUser != null ? CurrentUser.Name : "Unable to get user");
                _auditLogService.CreateAuditLogWithKey(invoice.Key, EntityType.Invoice, message);
            }
            catch (Exception ex)
            {
                MultiLogHelper.Error <InvoiceApiController>("Failed to cancel invoice", ex);
                response = Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }

            return(response);
        }