public ActionResult Create([Bind(Include = "Id, Employee, PaymentGroup, Month, Opening, NormalLoans, IncomingLoans, Restitution, OutgoingLoans, SalaryPayment, CashPayment")] MonthlyBalance monthlyBalance)
        {
            Db db = new Db(DbServices.ConnectionString);

            if (ModelState.IsValid)
            {
                try
                {
                    MonthlyBalanceServices.Insert(CurrentUser.Id, monthlyBalance, db);
                    TempData["Success"] = ResourceServices.GetString(Cf.Data.Resources.ResourceBase.Culture, "UI", "InsertConfirmed");
                    return(RedirectToAction("Index"));
                }
                catch (CfException cfex)
                {
                    TempData["Failure"] = cfex.ErrorDefinition.LocalizedMessage;
                }
                catch (Exception ex)
                {
                    TempData["Failure"] = ex.Message;
                }
            }

            ViewBag.EmployeeList     = new SelectList(EmployeeServices.List(db), "Id", "FirstName");
            ViewBag.PaymentGroupList = new SelectList(PaymentGroupServices.List(db), "Id", "Name");
            return(View(monthlyBalance));
        }
        // GET: MonthlyBalance/Delete/5
        public ActionResult Delete(Nullable <int> id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            Db             db             = new Db(DbServices.ConnectionString);
            MonthlyBalance monthlyBalance = MonthlyBalanceServices.Get(id.Value, db);

            if (monthlyBalance == null)
            {
                return(HttpNotFound());
            }
            return(View(monthlyBalance));
        }
        // GET: MonthlyBalance/Edit/5
        public ActionResult Edit(Nullable <int> id)
        {
            Db db = new Db(DbServices.ConnectionString);

            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            MonthlyBalance monthlyBalance = MonthlyBalanceServices.Get(id.Value, db);

            if (monthlyBalance == null)
            {
                return(HttpNotFound());
            }

            ViewBag.EmployeeList     = new SelectList(EmployeeServices.List(db), "Id", "FirstName", monthlyBalance.Employee);
            ViewBag.PaymentGroupList = new SelectList(PaymentGroupServices.List(db), "Id", "Name", monthlyBalance.PaymentGroup);
            return(View(monthlyBalance));
        }
        public async Task <BaseResponse> Create(MonthlyBalanceDto monthlyBalance)
        {
            var response = new BaseResponse();

            User currentUser = await _requestDataService.GetCurrentUser();

            var currentMonthlyBalance = await _monthlyBalanceRepository.GetNewestMonthlyBalance(currentUser.UserId);

            if (currentMonthlyBalance != null && currentMonthlyBalance.ValidUntil > DateTime.Now)
            {
                response.Infos.Errors.Add($"You already have a monthly balance until {currentMonthlyBalance.ValidUntil.ToString("dd.MM.yyyy")}. Please delete it first to set a new one.");
                response.StatusCode = HttpStatusCode.Conflict;
                return(response);
            }

            MonthlyBalanceDtoValidator validator = new MonthlyBalanceDtoValidator();
            var result = validator.Validate(monthlyBalance);

            if (!result.IsValid)
            {
                response.Infos.Errors.AddRange(result.Errors.ToList().Select(error => error.ErrorMessage));
                response.StatusCode = HttpStatusCode.UnprocessableEntity;
                return(response);
            }

            var newMonthlyBalance = new MonthlyBalance
            {
                ValidUntil = monthlyBalance.ValidUntil,
                AvailableMonthlyBalance = monthlyBalance.AvailableMonthlyBalance,
                BalanceUser             = currentUser
            };

            var dbBalance = await _monthlyBalanceRepository.Insert(newMonthlyBalance);

            dbBalance.BalanceUser = null;

            response.Data.Add("balance", dbBalance);

            return(response);
        }
        public void Upload()
        {
            var file = Request.Form.Files[0];

            // Get content of your Excel file
            var ms = new MemoryStream();

            file.CopyTo(ms);


            //Excel File Read
            try
            {
                string heading = ExcelReader.GetCellValue(ms, "Sheet1", "A1");

                string[] headingChunks = heading.Split(' ');
                string   year          = headingChunks.Last();
                int      lastTwoIndex  = headingChunks.Count() - 2;
                string   month         = headingChunks[lastTwoIndex];
                string   iMonthNo      = Convert.ToDateTime("01-" + month + "-" + year).Month.ToString("D2");
                string   idString      = year + iMonthNo;
                int      orderNo       = Int32.Parse(idString);

                List <MonthlyBalance> monthlyAccBalancesToInsert = new List <MonthlyBalance>();

                //Account R&D

                MonthlyBalance monthlyBalanceRDToInsert = new MonthlyBalance();
                monthlyBalanceRDToInsert.Id        = Guid.NewGuid();
                monthlyBalanceRDToInsert.Account   = accountDataService.GetAccountsByName(ExcelReader.GetCellValue(ms, "Sheet1", "A3"));
                monthlyBalanceRDToInsert.AddedTime = DateTime.Now;
                monthlyBalanceRDToInsert.Balance   = ExcelReader.GetCellValue(ms, "Sheet1", "B3");
                monthlyBalanceRDToInsert.IsLatest  = true;
                monthlyBalanceRDToInsert.Month     = month + " " + year;
                monthlyBalanceRDToInsert.Order     = orderNo;
                monthlyBalanceRDToInsert.AddedBy   = User.Identity.Name;

                monthlyAccBalancesToInsert.Add(monthlyBalanceRDToInsert);

                //Canteen

                MonthlyBalance monthlyBalanceCanteenToInsert = new MonthlyBalance();
                monthlyBalanceCanteenToInsert.Id        = Guid.NewGuid();
                monthlyBalanceCanteenToInsert.Account   = accountDataService.GetAccountsByName(ExcelReader.GetCellValue(ms, "Sheet1", "A4"));
                monthlyBalanceCanteenToInsert.AddedTime = DateTime.Now;
                monthlyBalanceCanteenToInsert.Balance   = ExcelReader.GetCellValue(ms, "Sheet1", "B4");
                monthlyBalanceCanteenToInsert.IsLatest  = true;
                monthlyBalanceCanteenToInsert.Month     = month + " " + year;
                monthlyBalanceCanteenToInsert.Order     = orderNo;
                monthlyBalanceCanteenToInsert.AddedBy   = User.Identity.Name;

                monthlyAccBalancesToInsert.Add(monthlyBalanceCanteenToInsert);

                //CEO’s car

                MonthlyBalance monthlyBalanceCarToInsert = new MonthlyBalance();
                monthlyBalanceCarToInsert.Id        = Guid.NewGuid();
                monthlyBalanceCarToInsert.Account   = accountDataService.GetAccountsByName(ExcelReader.GetCellValue(ms, "Sheet1", "A5"));
                monthlyBalanceCarToInsert.AddedTime = DateTime.Now;
                monthlyBalanceCarToInsert.Balance   = ExcelReader.GetCellValue(ms, "Sheet1", "B5");
                monthlyBalanceCarToInsert.IsLatest  = true;
                monthlyBalanceCarToInsert.Month     = month + " " + year;
                monthlyBalanceCarToInsert.Order     = orderNo;
                monthlyBalanceCarToInsert.AddedBy   = User.Identity.Name;

                monthlyAccBalancesToInsert.Add(monthlyBalanceCarToInsert);

                //Marketing

                MonthlyBalance monthlyBalanceMarketingToInsert = new MonthlyBalance();
                monthlyBalanceMarketingToInsert.Id        = Guid.NewGuid();
                monthlyBalanceMarketingToInsert.Account   = accountDataService.GetAccountsByName(ExcelReader.GetCellValue(ms, "Sheet1", "A6"));
                monthlyBalanceMarketingToInsert.AddedTime = DateTime.Now;
                monthlyBalanceMarketingToInsert.Balance   = ExcelReader.GetCellValue(ms, "Sheet1", "B6");
                monthlyBalanceMarketingToInsert.IsLatest  = true;
                monthlyBalanceMarketingToInsert.Month     = month + " " + year;
                monthlyBalanceMarketingToInsert.Order     = orderNo;
                monthlyBalanceMarketingToInsert.AddedBy   = User.Identity.Name;

                monthlyAccBalancesToInsert.Add(monthlyBalanceMarketingToInsert);

                //Parking fines

                MonthlyBalance monthlyBalanceParkingToInsert = new MonthlyBalance();
                monthlyBalanceParkingToInsert.Id        = Guid.NewGuid();
                monthlyBalanceParkingToInsert.Account   = accountDataService.GetAccountsByName(ExcelReader.GetCellValue(ms, "Sheet1", "A7"));
                monthlyBalanceParkingToInsert.AddedTime = DateTime.Now;
                monthlyBalanceParkingToInsert.Balance   = ExcelReader.GetCellValue(ms, "Sheet1", "B7");
                monthlyBalanceParkingToInsert.IsLatest  = true;
                monthlyBalanceParkingToInsert.Month     = month + " " + year;
                monthlyBalanceParkingToInsert.Order     = orderNo;
                monthlyBalanceParkingToInsert.AddedBy   = User.Identity.Name;

                monthlyAccBalancesToInsert.Add(monthlyBalanceParkingToInsert);


                accountDataService.AddAccounts(monthlyAccBalancesToInsert);
            }

            //CSV File Read
            catch (Exception e)
            {
                try
                {
                    TextReader textReader = new StreamReader(ms);
                    string     heading    = textReader.ReadLine();

                    string[] headingChunks = heading.Split(' ');
                    string   year          = headingChunks.Last();
                    int      lastTwoIndex  = headingChunks.Count() - 2;
                    string   month         = headingChunks[lastTwoIndex];
                    string   iMonthNo      = Convert.ToDateTime("01-" + month + "-" + year).Month.ToString("D2");
                    string   idString      = year + iMonthNo;
                    int      orderNo       = Int32.Parse(idString);

                    var csv = new CsvReader(textReader);

                    csv.Configuration.HasHeaderRecord = false;

                    var balances = csv.GetRecords <MonthlyAccountBalanceViewModel>().ToList();

                    List <MonthlyBalance> monthlyAccBalancesToInsert = new List <MonthlyBalance>();

                    foreach (MonthlyAccountBalanceViewModel monthlyBalancesAcc in balances)
                    {
                        MonthlyBalance monthlyBalanceRecToInsert = new MonthlyBalance();
                        monthlyBalanceRecToInsert.Id        = Guid.NewGuid();
                        monthlyBalanceRecToInsert.Account   = accountDataService.GetAccountsByName(monthlyBalancesAcc.DisplayAccountName);
                        monthlyBalanceRecToInsert.AddedTime = DateTime.Now;
                        monthlyBalanceRecToInsert.Balance   = monthlyBalancesAcc.DisplayAccountBalance;
                        monthlyBalanceRecToInsert.IsLatest  = true;
                        monthlyBalanceRecToInsert.Month     = month + " " + year;
                        monthlyBalanceRecToInsert.Order     = orderNo;
                        monthlyBalanceRecToInsert.AddedBy   = User.Identity.Name;

                        monthlyAccBalancesToInsert.Add(monthlyBalanceRecToInsert);
                    }

                    accountDataService.AddAccounts(monthlyAccBalancesToInsert);
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }