예제 #1
0
        public async Task <IActionResult> CreateBill(BillViewModel model)
        {
            if (ModelState.IsValid)
            {
                model.FinalValue = _stepRepository.CalculateFinalPrice(model.Consumption);
                var result = await _billRepository.InsertBillAsync(model);


                if (result == 0)
                {
                    return(NotFound());
                }
                else if (result == 1)
                {
                    ModelState.AddModelError(string.Empty, "The meter is innactive, can't insert new bills");
                    return(View(model));
                }
                else if (result == 2)
                {
                    ModelState.AddModelError(string.Empty, "Can't insert bill with month & year lower than water meter's creation");
                    return(View(model));
                }
                else if (result == 3)
                {
                    ModelState.AddModelError(string.Empty, "Can't insert bill with month & year equal or higher than the current date");
                    return(View(model));
                }



                var bill = new Bill
                {
                    Id           = model.Id,
                    Consumption  = model.Consumption,
                    FinalValue   = model.FinalValue,
                    MonthYear    = model.MonthYear,
                    WaterMeterId = model.WaterMeterId
                };

                var waterMeter = await _context.WaterMeters.Include(w => w.User).Where(w => w.Id == model.WaterMeterId).FirstOrDefaultAsync();

                _mailHelper.SendInvoiceMail(waterMeter.User.NormalizedUserName, model, await _pdfHelper.GenerateBillPDFAsync(bill));

                return(RedirectToAction($"Details/{model.WaterMeterId}"));
            }

            var waterMeter1 = await _waterMeterRepository.GetByIdAsync(model.WaterMeterId);

            model.DatePickerStartDate = new DateTime(waterMeter1.CreationDate.Year, waterMeter1.CreationDate.Month, 1);
            model.DatePickerEndDate   = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month - 1, 1);
            return(View(model));
        }