public async Task <IActionResult> Edit(string id, [Bind("CashflowOutId,MoneyTransferOrderId,CashflowOutNumber,CashflowOutDate,Description,CashRepositoryIdFrom,CashRepositoryIdTo,HasChild,createdAt")] CashflowOut cashflowOut)
        {
            if (id != cashflowOut.CashflowOutId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(cashflowOut);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CashflowOutExists(cashflowOut.CashflowOutId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["MoneyTransferOrderId"] = new SelectList(_context.MoneyTransferOrder, "MoneyTransferOrderId", "MoneyTransferOrderNumber", cashflowOut.MoneyTransferOrderId);
            return(View(cashflowOut));
        }
        // GET: CashflowOut/Create
        public IActionResult Create(string id)
        {
            ViewData["StatusMessage"]        = TempData["StatusMessage"];
            ViewData["moneyTransferOrderId"] = new SelectList(_context.MoneyTransferOrder.Where(x => x.MoneyTransferOrderStatus == MoneyTransferOrderStatus.Open && x.isIssued == false).ToList(), "MoneyTransferOrderId", "MoneyTransferOrderNumber");
            ViewData["cashRepositoryIdFrom"] = new SelectList(_context.CashRepository, "CashRepositoryId", "CashRepositoryName");
            ViewData["cashRepositoryIdTo"]   = new SelectList(_context.CashRepository, "CashRepositoryId", "CashRepositoryName");
            CashflowOut obj = new CashflowOut();

            obj.MoneyTransferOrderId = id;
            return(View(obj));
        }
        public async Task <IActionResult> Create([Bind("CashflowOutId,MoneyTransferOrderId,CashflowOutNumber,CashflowOutDate,Description,CashRepositoryIdFrom,CashRepositoryIdTo,HasChild,createdAt")] CashflowOut cashflowOut)
        {
            if (ModelState.IsValid)
            {
                //check MoneyTransferOrder
                CashflowOut check = await _context.CashflowOut
                                    .Include(x => x.MoneyTransferOrder)
                                    .SingleOrDefaultAsync(x => x.MoneyTransferOrderId.Equals(cashflowOut.MoneyTransferOrderId));

                if (check != null)
                {
                    ViewData["StatusMessage"]        = "Σφάλμα. Η εντολή εκροής έχει ήδη εκδοθεί. " + check.CashflowOutNumber;
                    ViewData["MoneyTransferOrderId"] = new SelectList(_context.MoneyTransferOrder.ToList(), "MoneyTransferOrderId", "MoneyTransferOrderNumber");
                    ViewData["CashRepositoryIdFrom"] = new SelectList(_context.CashRepository, "CashRepositoryId", "CashRepositoryName");
                    ViewData["CashRepositoryIdTo"]   = new SelectList(_context.CashRepository, "CashRepositoryId", "CashRepositoryName");
                    return(View(cashflowOut));
                }
                //check Balance
                bool isBalanceOK = true;

                MoneyTransferOrder mto = _context.MoneyTransferOrder.Where(x => x.MoneyTransferOrderId == cashflowOut.MoneyTransferOrderId).FirstOrDefault();
                MoneyTransferOrder to  = await _context.MoneyTransferOrder.Where(x => x.MoneyTransferOrderId.Equals(cashflowOut.MoneyTransferOrderId)).FirstOrDefaultAsync();

                cashflowOut.CashRepositoryIdFrom = to.CashRepositoryIdFrom;
                cashflowOut.CashRepositoryIdTo   = to.CashRepositoryIdTo;

                cashflowOut.CashRepositoryFrom = await _context.CashRepository.Include(x => x.Employee).SingleOrDefaultAsync(x => x.CashRepositoryId.Equals(cashflowOut.CashRepositoryIdFrom));

                cashflowOut.CashRepositoryTo = await _context.CashRepository.Include(x => x.Employee).SingleOrDefaultAsync(x => x.CashRepositoryId.Equals(cashflowOut.CashRepositoryIdTo));

                to.isIssued = true;

                CashRepository cashrepository = await _context.CashRepository.Where(x => x.CashRepositoryId == cashflowOut.CashRepositoryIdFrom).FirstAsync();

                if (mto != null && cashrepository != null)
                {
                    if (cashrepository.Balance < mto.PaymentAmount)
                    {
                        isBalanceOK = false;
                    }
                }
                else
                {
                    isBalanceOK = false;
                }


                if (!isBalanceOK)
                {
                    TempData["StatusMessage"] = "Σφάλμα. Υπάρχει πρόβλημα στα ταμειακά διαθέσιμα, το ταμείο σας έχει " + cashrepository.Balance + " €";
                    return(RedirectToAction(nameof(Create)));
                }
                _context.Add(cashflowOut);
                await _context.SaveChangesAsync();

                TempData["TransMessage"] = "Η ταμειακή εκροή " + cashflowOut.CashflowOutNumber + " έγινε με Επιτυχία!";
                return(RedirectToAction("Details", "CashRepository", new { id = cashflowOut.CashRepositoryIdTo }));
            }
            ViewData["MoneyTransferOrderId"] = new SelectList(_context.MoneyTransferOrder, "MoneyTransferOrderId", "MoneyTransferOrderNumber", cashflowOut.MoneyTransferOrderId);
            return(View(cashflowOut));
        }