Beispiel #1
0
        public async Task <IActionResult> OnGetAsync(string id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            MtdStore = await _context.MtdStore.FirstOrDefaultAsync(m => m.Id == id);

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

            var user = await _userHandler.GetUserAsync(HttpContext.User);

            bool isViewer = await _userHandler.IsViewer(user, MtdStore.MtdForm, MtdStore.Id);

            IsEditor = await _userHandler.IsEditor(user, MtdStore.MtdForm, MtdStore.Id);

            IsInstallerOwner = await _userHandler.IsInstallerOwner(user, MtdStore.MtdForm, MtdStore.Id);

            if (!isViewer)
            {
                return(Forbid());
            }


            MtdForm = await _context.MtdForm.Include(m => m.InverseParentNavigation).FirstOrDefaultAsync(x => x.Id == MtdStore.MtdForm);

            MtdLogDocument edited = await _context.MtdLogDocument.Where(x => x.MtdStore == MtdStore.Id).OrderByDescending(x => x.TimeCh).FirstOrDefaultAsync();

            MtdLogDocument created = await _context.MtdLogDocument.Where(x => x.MtdStore == MtdStore.Id).OrderBy(x => x.TimeCh).FirstOrDefaultAsync();

            StoreOwner = await _context.MtdStoreOwner.Where(x => x.Id == MtdStore.Id).FirstOrDefaultAsync();

            ChangesHistory = new ChangesHistory
            {
                CreateByTime = MtdStore.Timecr.ToString()
            };

            if (edited != null)
            {
                ChangesHistory.LastEditedUser = edited.UserName;
                ChangesHistory.LastEditedTime = edited.TimeCh.ToString();
            }

            if (created != null)
            {
                if (MtdStore.Timecr.Date == created.TimeCh.Date)
                {
                    ChangesHistory.CreateByUser = created.UserName;
                }
            }

            List <WebAppUser> webAppUsers = await _userHandler.GetUsersInGroupsAsync(user);

            ViewData["UsersList"] = new SelectList(webAppUsers, "Id", "Title");

            ApprovalHandler approvalHandler = new ApprovalHandler(_context, MtdStore.Id);

            IsApprover = await approvalHandler.IsApproverAsync(user);

            IsFirstStage = await approvalHandler.IsFirstStageAsync();

            IList <MtdApprovalStage> stages = await approvalHandler.GetStagesDownAsync();

            ViewData["Stages"] = new SelectList(stages.OrderByDescending(x => x.Stage), "Id", "Name");

            List <string> partIds = await approvalHandler.GetWilBeBlockedPartsIds();

            BlockParts = new List <MtdFormPart>();
            if (partIds.Count > 0)
            {
                BlockParts = await _context.MtdFormPart.Where(x => partIds.Contains(x.Id)).OrderBy(x => x.Sequence).ToListAsync();
            }
            IsFormApproval = await approvalHandler.IsApprovalFormAsync();

            if (IsFormApproval)
            {
                ApprovalStatus = await approvalHandler.GetStatusAsync(user);
            }


            return(Page());
        }
Beispiel #2
0
        public async Task <IActionResult> OnPostSaveAsync()
        {
            IFormCollection requestForm = await Request.ReadFormAsync();

            string Id = requestForm["idStore"];

            MtdStore mtdStore = await _context.MtdStore.FirstOrDefaultAsync(x => x.Id == Id);

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

            WebAppUser webAppUser = await _userHandler.GetUserAsync(HttpContext.User);

            bool isEditor = await _userHandler.IsEditor(webAppUser, mtdStore.MtdForm, mtdStore.Id);

            ApprovalHandler approvalHandler = new ApprovalHandler(_context, mtdStore.Id);
            ApprovalStatus  approvalStatus  = await approvalHandler.GetStatusAsync(webAppUser);

            if (!isEditor || approvalStatus == ApprovalStatus.Rejected || approvalStatus == ApprovalStatus.Waiting)
            {
                return(Ok(403));
            }

            MtdLogDocument mtdLog = new MtdLogDocument
            {
                MtdStore = mtdStore.Id,
                TimeCh   = DateTime.Now,
                UserId   = webAppUser.Id,
                UserName = webAppUser.Title
            };


            OutData outData = await CreateDataAsync(Id, webAppUser, TypeAction.Edit, requestForm);

            List <MtdStoreStack> stackNew = outData.MtdStoreStacks;


            IList <MtdStoreStack> stackOld = await _context.MtdStoreStack
                                             .Include(m => m.MtdStoreStackText)
                                             .Include(m => m.MtdStoreStackDecimal)
                                             .Include(m => m.MtdStoreStackFile)
                                             .Include(m => m.MtdStoreStackDate)
                                             .Include(m => m.MtdStoreStackInt)
                                             .Include(m => m.MtdStoreLink)
                                             .Where(x => x.MtdStore == Id).ToListAsync();

            foreach (MtdStoreStack stack in stackOld)
            {
                MtdStoreStack stackForField = stackNew.SingleOrDefault(x => x.MtdFormPartField == stack.MtdFormPartField);
                if (stackForField != null)
                {
                    stack.MtdStoreStackText    = stackForField.MtdStoreStackText;
                    stack.MtdStoreLink         = stackForField.MtdStoreLink;
                    stack.MtdStoreStackDate    = stackForField.MtdStoreStackDate;
                    stack.MtdStoreStackDecimal = stackForField.MtdStoreStackDecimal;
                    stack.MtdStoreStackFile    = stackForField.MtdStoreStackFile;
                    stack.MtdStoreStackInt     = stackForField.MtdStoreStackInt;
                }
            }


            try
            {
                if (stackNew.Count > 0)
                {
                    int count = await _context.MtdStoreLink.Where(x => x.MtdStore == Id).CountAsync();

                    if (count > 0)
                    {
                        string titleText             = outData.MtdStoreStacks.FirstOrDefault(x => x.MtdFormPartField == outData.MtdFormPartField.Id).MtdStoreStackText.Register;
                        IList <MtdStoreLink> linkIds = await _context.MtdStoreLink.Where(x => x.MtdStore == Id).Select(x => new MtdStoreLink {
                            Id = x.Id, MtdStore = x.MtdStore, Register = titleText
                        }).ToListAsync();

                        _context.MtdStoreLink.UpdateRange(linkIds);
                    }

                    _context.MtdStoreStack.UpdateRange(stackOld);


                    List <MtdStoreStack> stackNewOnly = stackNew.Where(x => !stackOld.Select(f => f.MtdFormPartField).Contains(x.MtdFormPartField)).ToList();

                    if (stackNewOnly.Any())
                    {
                        await _context.MtdStoreStack.AddRangeAsync(stackNewOnly);
                    }

                    _context.MtdLogDocument.Add(mtdLog);
                    await _context.SaveChangesAsync();
                }
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MtdStoreExists(Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok());
        }