public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            Invoice invoice = await _context.Invoice
                              .Include(i => i.Repair)
                              .ThenInclude(r => r.ReplacedParts)
                              .Include(i => i.IssuedBy)
                              .Include(i => i.IssuedTo)
                              .AsNoTracking()
                              .FirstOrDefaultAsync(i => i.RepairID == id.Value);


            if (invoice == null)
            {
                return(NotFound());
            }

            var user = await _userManager.GetUserAsync(User);



            if (user.Id != invoice.IssuedTo.Id && User.IsInRole("Client"))
            {
                return(NotFound());
            }

            //if ( !User.IsInRole("Mechanic"))
            //{
            //    return NotFound();
            //}

            //var seqNum = _context.GetNextDocVal();

            //string newNumber = "FA" + seqNum.ToString("D5") + "/" + DateTime.Now.ToString("yyyy-MM-dd");

            InvoiceModel = new InvoiceViewModel
            {
                DocNum         = invoice.InvoiceNumber,
                CompanyName    = _configuration["Company:Name"],
                CompanyAddress = _configuration["Company:Address"],
                CompanyPhone   = _configuration["Company:Phone"],
                BillUser       = invoice.IssuedTo,
                issueDate      = invoice.createdDate,
                WorkCost       = invoice.Repair.WorkPrice,
                Sum            = invoice.Sum,
                IssuedBy       = invoice.IssuedBy.FullName,
                Parts          = invoice.Repair.ReplacedParts,
                RepairID       = invoice.RepairID
            };



            //Invoice = await _context.Invoice
            //    .Include(i => i.Repair).FirstOrDefaultAsync(m => m.InvoiceID == id);


            return(Page());
        }
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            bool exists = await _context.Invoice.AnyAsync(i => i.RepairID == id.Value);

            if (exists)
            {
                return(RedirectToPage("/Repairs/Index"));
            }



            Repair repair = await _context.Repair
                            .Include(r => r.Client)
                            .Include(r => r.ReplacedParts)
                            .AsNoTracking()
                            .FirstOrDefaultAsync(r => r.RepairID == id.Value && r.RepairState == RepairState.Finished);

            if (repair == null)
            {
                return(NotFound());
            }

            var user = await _userManager.GetUserAsync(User);

            decimal sum    = 0;
            var     result = _context.ReplacedPart.Where(p => p.RepairID == repair.RepairID).GroupBy(p => "1")
                             .Select(p => p.Sum(i => i.Quantity * i.Price));

            foreach (var grp in result)
            {
                sum += grp;
            }
            //var seqNum = _context.GetNextDocVal();

            //string newNumber = "FA" + seqNum.ToString("D5") + "/" + DateTime.Now.ToString("yyyy-MM-dd");

            InvoiceModel = new InvoiceViewModel {
                CompanyName    = _configuration["Company:Name"],
                CompanyAddress = _configuration["Company:Address"],
                CompanyPhone   = _configuration["Company:Phone"],
                BillUser       = repair.Client,
                issueDate      = DateTime.Now,
                WorkCost       = repair.WorkPrice,
                Sum            = sum + repair.WorkPrice,
                IssuedBy       = user.FullName,
                Parts          = repair.ReplacedParts,
                RepairID       = repair.RepairID
            };



            //Invoice = await _context.Invoice
            //    .Include(i => i.Repair).FirstOrDefaultAsync(m => m.InvoiceID == id);


            return(Page());
        }