public async Task <ActionResult> SendOrder(ExactOrderVM vm)
        {
            // Check if user exists
            var user = _dbHelper.GetUser(_context, User.Identity.Name);

            if (user == null)
            {
                return(RedirectToAction("LogOut", "Account"));
            }
            // DB order
            var dbOrder = _context.ExactOrders
                          .Include(nameof(ExactOrder.Lines) + "." + nameof(ExactOrderLine.Item))
                          .Include(nameof(ExactOrder.Supplier))
                          .Include(nameof(ExactOrder.DBUser))
                          .FirstOrDefault(x => x.Id == vm.Id);

            dbOrder.Description      = vm.Description;
            dbOrder.Created          = DateTime.Now;
            dbOrder.Currency         = vm.Currency;
            dbOrder.OrderNumber      = vm.OrderNumber;
            dbOrder.YourRef          = vm.YourRef;
            dbOrder.Document         = vm.Document;
            dbOrder.OrderDate        = vm.OrderDate;
            dbOrder.PaymentCondition = vm.PaymentConditionId;
            dbOrder.CreatorId        = user.CreatorId;
            dbOrder.Project          = vm.Project;
            dbOrder.TimeSend         = DateTime.Now;
            dbOrder.DBUser           = _context.Users.FirstOrDefault(x => x.UserName == user.UserName);
            dbOrder.IsStoredInExact  = true;

            foreach (var x in vm.Lines)
            {
                var line = _context.ExactOrderLines.FirstOrDefault(y => y.Id == x.Id);
                line.Description   = x.Description;
                line.AmountDC      = x.AmountDC;
                line.Unit          = x.Unit;
                line.NetPrice      = x.NetPrice;
                line.ReceiptDate   = x.ReceiptDate;
                line.VATAmount     = x.VATAmount;
                line.VATCode       = x.VATCode;
                line.VATPercentage = x.VATPercentage;
                line.Quantity      = x.Quantity;
                line.Project       = x.Project;
            }

            // Exact Order
            var exactOrder = new PurchaseOrder
            {
                Description = vm.Description,
                Created     = DateTime.Now,
                Supplier    = dbOrder.Supplier.ExactId,
                // Currency = vm.Currency,
                YourRef            = vm.YourRef,
                Document           = vm.Document,
                OrderDate          = DateTime.Now,
                PaymentCondition   = vm.PaymentConditionId,
                Creator            = new Guid(user.CreatorId),
                PurchaseOrderLines = vm.Lines
                                     .Select(x => new PurchaseOrderLine
                {
                    Item        = new Guid(x.ItemID),
                    Description = x.Description,
                    AmountDC    = x.AmountDC,
                    Unit        = x.Unit,
                    NetPrice    = x.NetPrice,
                    ReceiptDate = DateTime.Now,
                    // VATAmount = x.VATAmount,
                    // VATCode = x.VATCode,
                    // VATPercentage = x.VATPercentage,
                    Quantity = x.Quantity,
                    QuantityInPurchaseUnits = x.Quantity,
                    Project   = x.Project,
                    UnitPrice = 2,
                })
                                     .ToList()
            };

            ExactOnlineConnect.data.Context     = _context;
            ExactOnlineConnect.data.AccessToken = user.Token;

            var message = await ExactOnlineConnect.data.StoreOrder(user.DivisionId, exactOrder);

            if (message.StatusCode == System.Net.HttpStatusCode.InternalServerError)
            {
                return(RedirectToAction(nameof(Index), new { sm = StateMessage.FailedSendingOrder }));
            }

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index), new { sm = StateMessage.SuccessSendingOrder }));
        }
        /// <summary>
        /// Order page
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task <IActionResult> Order(int Id)
        {
            // Get and check user
            var user = _dbHelper.GetUser(_context, User.Identity.Name);

            if (user == null)
            {
                return(RedirectToAction("LogOut", "Account"));
            }

            // Get and check order
            var order = _dbHelper.GetExactOrderByIdToVM(_context, Id);

            if (order == null || order.Lines == null)
            {
                return(RedirectToAction(nameof(Index), new { sm = StateMessage.FailedMissingInfo }));
            }

            var vm = new ExactOrderVM();

            // Check if the order have to be sent or is already sent
            if (!order.IsStoredInExact)
            {
                // Set Exact data
                ExactOnlineConnect.data.Context     = _context;
                ExactOnlineConnect.data.AccessToken = user.Token;
                // TODO: Check if active??

                // Check if items are int exact and if not it neeeds to be created first
                if (!order.ItemsAreInExact)
                {
                    return(RedirectToAction(nameof(ItemsToBeCreatedInExact), new { Id = Id }));
                }

                // Get DB values
                var config = _dbHelper.GetExactConfig(_context, user.ConfigID);

                // Checks if config is correctly saved
                if (config.PaymentConditionId == null)
                {
                    return(RedirectToAction(nameof(Areas.ExactOnline.Controllers.ConfigurationController.Index), new { sm = StateMessage.FailedPaymentConditionMissing }));
                }
                if (config.UserId == null)
                {
                    return(RedirectToAction(nameof(Areas.ExactOnline.Controllers.ConfigurationController.Index), new { sm = StateMessage.FailedOrderCreatorMissing }));
                }

                var items = await ExactOnlineConnect.data.GetItems(user.DivisionId);

                if (items == null)
                {
                    return(RedirectToAction(nameof(Index), new { sm = StateMessage.FailedNoExactConnection }));
                }

                var toBeCreated = new List <Item>();

                vm.Id                 = order.Id;
                vm.Currency           = order.Currency;
                vm.Description        = order.Description;
                vm.Created            = order.Created;
                vm.CreatorId          = user.CreatorId;
                vm.PaymentConditionId = config.PaymentConditionId.ToString();
                vm.Lines              = new List <OrderLineVM>();
                vm.CreatedItems       = toBeCreated.Any() ? toBeCreated
                                        .Select(x => x.Description)
                                        .ToList()
                    :
                                        new List <string>();
                vm.HasBeenSend  = order.IsStoredInExact;
                vm.SupplierName = order.SupplierName;

                // Get alertData
                var messageItems = order.Lines
                                   .Select(y => y.Item)
                                   .Where(y => !y.MessageSeen)
                                   .ToList();

                if (messageItems.Any())
                {
                    vm.CreatedItems = messageItems.Select(y => y.Name).ToList();
                    foreach (var x in messageItems)
                    {
                        var item = _context.ExactItems
                                   .FirstOrDefault(y => y.Id == x.Id);
                        item.MessageSeen = true;
                    }
                    await _context.SaveChangesAsync();
                }
                else
                {
                    vm.CreatedItems = new List <string>();
                }

                foreach (var x in order.Lines)
                {
                    //var item = listItems.FirstOrDefault(y => y.Description == x.Item.Name);

                    var line = new OrderLineVM
                    {
                        Id          = x.Id,
                        AmountDC    = x.AmountDC,
                        Description = x.Description,
                        Item        = new ItemVM
                        {
                            ExactId     = x.Item.ExactId,
                            Name        = x.Item.Name,
                            Code        = x.Item.Code,
                            Description = x.Item.Description,
                        },
                        NetPrice      = x.NetPrice,
                        Project       = x.Project,
                        Quantity      = x.Quantity,
                        ReceiptDate   = x.ReceiptDate,
                        Unit          = x.Unit,
                        VATAmount     = x.VATAmount,
                        VATCode       = x.VATCode,
                        VATPercentage = x.VATPercentage,
                    };
                    vm.Lines.Add(line);
                    vm.StateMessage = StateMessage.SuccessAddedItems;
                }
                ;
            }
            else
            {
                vm.Id                   = order.Id;
                vm.CreatedItems         = new List <string>();
                vm.Currency             = order.Currency;
                vm.Description          = order.Description;
                vm.Created              = order.Created;
                vm.Project              = order.Project;
                vm.Document             = order.Document;
                vm.OrderNumber          = order.OrderNumber;
                vm.CreatorId            = user.CreatorId;
                vm.OrderDate            = order.OrderDate;
                vm.PaymentConditionName = "";
                vm.Lines                = new List <OrderLineVM>();
                vm.IsStoredInExact      = order.IsStoredInExact;
                foreach (var x in order.Lines)
                {
                    var line = new OrderLineVM
                    {
                        Id          = x.Id,
                        AmountDC    = x.AmountDC,
                        Description = x.Description,
                        Item        = new ItemVM
                        {
                            ExactId     = x.Item.ExactId,
                            Name        = x.Item.Name,
                            Code        = x.Item.Code,
                            Description = x.Item.Description,
                        },
                        NetPrice      = x.NetPrice,
                        Project       = x.Project,
                        Quantity      = x.Quantity,
                        ReceiptDate   = x.ReceiptDate,
                        Unit          = x.Unit,
                        VATAmount     = x.VATAmount,
                        VATCode       = x.VATCode,
                        VATPercentage = x.VATPercentage,
                    };
                    vm.Lines.Add(line);
                    vm.StateMessage = StateMessage.WarningNotCreatedYet;
                }
                ;
            }

            return(View(vm));
        }