public List <string> GetdetailList(string id)
        {
            AdjustmentRecord ad = _context.AdjustmentRecord.Where(s => s.VoucherNo == id).ToList().First();

            if (id.Contains("VTemp") && ad == null)
            {
                var adjustmentRecord = new AdjustmentRecord()
                {
                    VoucherNo = id,
                    IssueDate = DateTime.Today,
                    ClerkId   = int.Parse(id.Replace("VTemp", "")),
                    Status    = "draft"
                };
                _context.AdjustmentRecord.Add(adjustmentRecord);
                _context.SaveChanges();
            }
            List <string> result = new List <string>();
            //int idID = _context.CollectionPoint.Where(s => s.CollectionPointName.Contains(id)).ToList().First().CollectionPointId;
            List <RecordDetails> dis = _context.RecordDetails.Where(s => s.Rrid == id).ToList();

            if (dis == null)
            {
                return(null);
            }
            foreach (RecordDetails i in dis)
            {
                result.Add(i.Rdid.ToString());
            }
            return(result);
        }
        public async Task <IActionResult> AdjustmentRecordDelete(string id)
        {
            ADTeam5User user = await _userManager.GetUserAsync(HttpContext.User);

            List <string> identity = userCheck.checkUserIdentityAsync(user);
            int           userID   = user.WorkID;

            AdjustmentRecord adjustmentRecordToBeDeleted = _context.AdjustmentRecord.FirstOrDefault(x => x.VoucherNo == id);

            _context.AdjustmentRecord.Remove(adjustmentRecordToBeDeleted);
            _context.SaveChanges();
            b.RemoveRecordDetails(id);

            AdjustmentRecord                 ar = _context.AdjustmentRecord.FirstOrDefault(x => x.ClerkId == userID && !x.VoucherNo.Contains("Vtemp"));
            List <AdjustmentRecord>          tempAdjustmentRecords = new List <AdjustmentRecord>();
            List <AdjustmentRecordViewModel> arViewModelList       = new List <AdjustmentRecordViewModel>();

            if (ar != null)
            {
                tempAdjustmentRecords = _context.AdjustmentRecord.Where(x => x.ClerkId == userID && !x.VoucherNo.Contains("Vtemp") && x.Status == "Draft").OrderByDescending(x => x.VoucherNo).ToList();
                arViewModelList       = b.CreateAdjustmentRecordViewModel(tempAdjustmentRecords);
            }
            else
            {
                NotFound();
            }
            return(PartialView("_TempAdjustmentRecords", arViewModelList));
        }
        public List <string> GetVoucher(string id)
        {
            List <string>    result = new List <string>();
            AdjustmentRecord ad     = _context.AdjustmentRecord.Where(s => s.VoucherNo == id).ToList().First();

            if (ad == null)
            {
                return(null);
            }
            else
            {
                BizLogic k = new BizLogic();
                result.Add(ad.VoucherNo);
                result.Add(ad.IssueDate.Date.ToString());
                result.Add(k.getPriceForAdjust(ad.VoucherNo));
                //this part in fact is price
                result.Add(ad.Status);
                return(result);
            }
        }
        public void ApplyVoucher(int id)
        {
            List <RecordDetails> source = _context.RecordDetails.Where(s => s.Rrid == ("VTemp" + id)).ToList();
            string vono = new BizLogic().IDGenerator("V");
            //shengcheng vocher
            AdjustmentRecord temp = new AdjustmentRecord();

            temp.VoucherNo = vono;
            temp.ClerkId   = id;
            temp.IssueDate = DateTime.Now;
            temp.Status    = "Pending Approval";
            _context.AdjustmentRecord.Add(temp);
            foreach (RecordDetails i in source)
            {
                i.Rrid = vono;
                _context.RecordDetails.Update(i);
            }
            _context.SaveChanges();
            //save changes
        }
        public List <string> GetVoucher1(string id)
        {
            List <string>    result = new List <string>();
            AdjustmentRecord ad     = _context.AdjustmentRecord.Where(s => s.VoucherNo == id).ToList().First();

            if (ad == null)
            {
                return(null);
            }
            else
            {
                BizLogic k = new BizLogic();
                result.Add(ad.VoucherNo);
                var l = _context.User.Where(s => s.UserId == ad.ClerkId).ToList().First();
                result.Add(l.Name);
                result.Add(k.getPriceForAdjust(ad.VoucherNo));
                //this part in fact is price
                result.Add(ad.Status);
                return(result);
            }
        }
        //// GET: AdjustmentRecords
        //public async Task<IActionResult> Index()
        //{
        //    ADTeam5User user = await _userManager.GetUserAsync(HttpContext.User);
        //    List<string> identity = userCheck.checkUserIdentityAsync(user);
        //    int userID = user.WorkID;
        //    string userRole = identity[1];

        //    List<AdjustmentRecord> arList = new List<AdjustmentRecord>();
        //    //records shown to clerk
        //    switch (userRole)
        //    {
        //        case "Clerk":
        //            AdjustmentRecord arC = _context.AdjustmentRecord.FirstOrDefault(x => x.ClerkId == userID && !x.VoucherNo.Contains("Vtemp") && x.Status == "Draft");
        //            if (arC != null)
        //            {
        //                arList = _context.AdjustmentRecord.Where(x => x.ClerkId == userID && !x.VoucherNo.Contains("Vtemp") && x.Status == "Draft").OrderByDescending(x => x.VoucherNo).ToList();
        //            }
        //            else
        //            {
        //                NotFound();
        //            }
        //            break;
        //        case "Supervisor":
        //            AdjustmentRecord arS = _context.AdjustmentRecord.FirstOrDefault(x => x.Status == "Pending Approval" && !x.VoucherNo.Contains("Vtemp"));
        //            if (arS != null)
        //            {
        //                arList = _context.AdjustmentRecord.Where(x => x.Status == "Pending Approval" && !x.VoucherNo.Contains("Vtemp")).OrderByDescending(x => x.VoucherNo).ToList();
        //            }
        //            else
        //            {
        //                NotFound();
        //            }
        //            break;
        //        case "Manager":
        //            AdjustmentRecord arM = _context.AdjustmentRecord.FirstOrDefault(x => x.Status == "Pending Manager Approval" && !x.VoucherNo.Contains("Vtemp"));
        //            if (arM != null)
        //            {
        //                arList = _context.AdjustmentRecord.Where(x => x.Status == "Pending Manager Approval" && !x.VoucherNo.Contains("Vtemp")).OrderByDescending(x => x.VoucherNo).ToList();
        //            }
        //            else
        //            {
        //                NotFound();
        //            }
        //            break;
        //    }
        //    return View(arList);
        //}

        // POST: AdjustmentRecords
        //[HttpPost]
        public async Task <IActionResult> Index(string state)
        {
            ADTeam5User user = await _userManager.GetUserAsync(HttpContext.User);

            List <string> identity = userCheck.checkUserIdentityAsync(user);
            int           userID   = user.WorkID;
            string        userRole = identity[1];

            List <AdjustmentRecord>          arList          = new List <AdjustmentRecord>();
            List <AdjustmentRecordViewModel> arViewModelList = new List <AdjustmentRecordViewModel>();

            switch (state)
            {
            case "OutstandingRecords":
                //records shown to clerk
                switch (userRole)
                {
                case "Clerk":
                    AdjustmentRecord arC = _context.AdjustmentRecord.FirstOrDefault(x => x.ClerkId == userID && !x.VoucherNo.Contains("Vtemp") && x.Status == "Draft");
                    if (arC != null)
                    {
                        arList          = _context.AdjustmentRecord.Where(x => x.ClerkId == userID && !x.VoucherNo.Contains("Vtemp") && (x.Status == "Draft" || x.Status == "Rejected")).OrderByDescending(x => x.VoucherNo).ToList();
                        arViewModelList = b.CreateAdjustmentRecordViewModel(arList);
                    }
                    else
                    {
                        NotFound();
                    }
                    break;

                case "Supervisor":
                    AdjustmentRecord arS = _context.AdjustmentRecord.FirstOrDefault(x => x.Status == "Pending Approval");
                    if (arS != null)
                    {
                        arList          = _context.AdjustmentRecord.Where(x => x.Status == "Pending Approval").OrderByDescending(x => x.VoucherNo).ToList();
                        arViewModelList = b.CreateAdjustmentRecordViewModel(arList);
                    }
                    else
                    {
                        NotFound();
                    }
                    break;

                case "Manager":
                    AdjustmentRecord arM = _context.AdjustmentRecord.FirstOrDefault(x => x.Status == "Pending Manager Approval");
                    if (arM != null)
                    {
                        arList          = _context.AdjustmentRecord.Where(x => x.Status == "Pending Manager Approval").OrderByDescending(x => x.VoucherNo).ToList();
                        arViewModelList = b.CreateAdjustmentRecordViewModel(arList);
                    }
                    else
                    {
                        NotFound();
                    }
                    break;
                }
                break;

            case "PastRecords":
                //records shown to clerk
                switch (userRole)
                {
                case "Clerk":
                    AdjustmentRecord arC = _context.AdjustmentRecord.FirstOrDefault(x => x.ClerkId == userID && !x.VoucherNo.Contains("Vtemp") && x.Status != "Draft");
                    if (arC != null)
                    {
                        arList          = _context.AdjustmentRecord.Where(x => x.ClerkId == userID && !x.VoucherNo.Contains("Vtemp") && x.Status != "Draft" && x.Status != "Rejected").OrderByDescending(x => x.VoucherNo).ToList();
                        arViewModelList = b.CreateAdjustmentRecordViewModel(arList);
                    }
                    else
                    {
                        NotFound();
                    }
                    break;

                case "Supervisor":
                    AdjustmentRecord arS = _context.AdjustmentRecord.FirstOrDefault(x => x.Status == "Rejected" || x.Status == "Approved" || x.Status == "Pending Manager Approval");
                    if (arS != null)
                    {
                        arList          = _context.AdjustmentRecord.Where(x => x.Status == "Rejected" || x.Status == "Approved").OrderByDescending(x => x.VoucherNo).ToList();
                        arViewModelList = b.CreateAdjustmentRecordViewModel(arList);
                    }
                    else
                    {
                        NotFound();
                    }
                    break;

                case "Manager":
                    AdjustmentRecord arM = _context.AdjustmentRecord.FirstOrDefault(x => x.Status == "Rejected" || x.Status == "Approved");
                    if (arM != null)
                    {
                        arList          = _context.AdjustmentRecord.Where(x => x.Status == "Rejected" || x.Status == "Approved").OrderByDescending(x => x.VoucherNo).ToList();
                        arViewModelList = b.CreateAdjustmentRecordViewModel(arList);
                    }
                    else
                    {
                        NotFound();
                    }
                    break;
                }
                break;

            default:
                switch (userRole)
                {
                case "Clerk":
                    AdjustmentRecord arC = _context.AdjustmentRecord.FirstOrDefault(x => x.ClerkId == userID && !x.VoucherNo.Contains("Vtemp") && x.Status == "Draft");
                    if (arC != null)
                    {
                        arList          = _context.AdjustmentRecord.Where(x => x.ClerkId == userID && !x.VoucherNo.Contains("Vtemp") && (x.Status == "Draft" || x.Status == "Rejected")).OrderByDescending(x => x.VoucherNo).ToList();
                        arViewModelList = b.CreateAdjustmentRecordViewModel(arList);
                    }
                    else
                    {
                        NotFound();
                    }
                    break;

                case "Supervisor":
                    AdjustmentRecord arS = _context.AdjustmentRecord.FirstOrDefault(x => x.Status == "Pending Approval");
                    if (arS != null)
                    {
                        arList          = _context.AdjustmentRecord.Where(x => x.Status == "Pending Approval").OrderByDescending(x => x.VoucherNo).ToList();
                        arViewModelList = b.CreateAdjustmentRecordViewModel(arList);
                    }
                    else
                    {
                        NotFound();
                    }
                    break;

                case "Manager":
                    AdjustmentRecord arM = _context.AdjustmentRecord.FirstOrDefault(x => x.Status == "Pending Manager Approval");
                    if (arM != null)
                    {
                        arList          = _context.AdjustmentRecord.Where(x => x.Status == "Pending Manager Approval").OrderByDescending(x => x.VoucherNo).ToList();
                        arViewModelList = b.CreateAdjustmentRecordViewModel(arList);
                    }
                    else
                    {
                        NotFound();
                    }
                    break;
                }
                break;
            }

            return(View(arViewModelList));
        }