Exemplo n.º 1
0
 //
 // GET: /Freight/Create
 public ActionResult Create()
 {
     var model = new RequestFreight();
     model.RequestFreightCargoes.Add(new RequestFreightCargo());
     return View(model);
 }
Exemplo n.º 2
0
        public ActionResult Create(RequestFreight rf, IEnumerable<HttpPostedFileBase> files)
        {
            if (rf.Request.ApproverEmployeeId == 0)
            {
                    ViewBag.ErrorMessage = "Веберите руководителя, который подпишет заявку!";
                    return View(rf);
            }

            try
            {
                rf.Request.Status = 0;
                rf.Request.UserLogin = User.Identity.Name;
                rf.Request.UserFio = AccountManager.GetUserDisplayName(User.Identity.Name);
                rf.Request.PublishDate = DateTime.Now;
                rf.Request.IsDeleted = false;

                if (rf.Request.CustomerId == 0)
                    rf.Request.CustomerId = null;

                if (Utils.AccountManager.IsApprover(User.Identity.Name).Item2)
                {
                    rf.Request.Status = 1;
                    rf.Request.ApproveDate = DateTime.Now;
                    rf.Request.ApproverLogin = User.Identity.Name;
                    rf.Request.ApproverFio = Utils.AccountManager.GetUserDisplayName(User.Identity.Name);

                    RequestEvent re = new RequestEvent();
                    re.Status = 1;
                    re.EventDate = DateTime.Now;
                    rf.Request.RequestEvents.Add(re);
                }

                //Файлы
                if (files != null)
                {
                    foreach (var file in files)
                    {
                        if (file != null)
                        {
                            RequestAttachment attFile = new RequestAttachment();
                            attFile.Name = file.FileName.Substring(file.FileName.LastIndexOf("\\") + 1);

                            int fl = file.ContentLength;
                            byte[] fc = new byte[fl];

                            file.InputStream.Read(fc, 0, fl);

                            attFile.Cont = fc;

                            rf.Request.RequestAttachments.Add(attFile);
                        }
                    }
                }

                _db.RequestFreights.Add(rf);
                _db.SaveChanges();

                return View("Published", rf.Request.RequestId);
            }
            catch (DbEntityValidationException dbEx)
            {

                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                        //TODO: Попытка удалить запись, у которой ExpDate истек
                    }
                }

                ViewBag.ErrMessage = "Ошибка при создании записи";
                ViewBag.BackController = "Home";
                return View("Error");
            }
        }
Exemplo n.º 3
0
        public ActionResult Edit(RequestFreight rf, IEnumerable<HttpPostedFileBase> files, int[] oldFiles)
        {
            try
            {
                var model = _db.RequestFreights.Single(m => m.RequestId == rf.RequestId);
                var respCargoes = rf.RequestFreightCargoes;

                model.DestinationPoint = rf.DestinationPoint;
                model.OrderNumber = rf.OrderNumber;
                model.LoadingType = rf.LoadingType;
                model.LoadingAddress = rf.LoadingAddress;
                model.ContactName = rf.ContactName;
                model.Responsible = rf.Responsible;
                model.Request.RequestDate = rf.Request.RequestDate;
                model.Request.ApproverEmployeeId = rf.Request.ApproverEmployeeId;
                model.VehicleType = rf.VehicleType;

                if (rf.Request.CustomerId == 0)
                    rf.Request.CustomerId = null;
                model.Request.CustomerId = rf.Request.CustomerId;

                if (model.Request.Status == 3 && User.Identity.Name.ToLower() == model.Request.UserLogin.ToLower())
                {
                    model.Request.Status = 0;
                    RequestEvent re = new RequestEvent();
                    re.Status = 0;
                    re.Message = String.Empty;
                    re.EventDate = DateTime.Now;
                    model.Request.RequestEvents.Add(re);
                }

                var modelCargoes = model.RequestFreightCargoes;

                foreach (var modelCargo in modelCargoes)
                {
                    //Существует ли такой груз в респонсе
                    bool cargoExistInResponse = false;

                    foreach(var respCargo in respCargoes)
                    {
                        //Если обновление груза
                        if (respCargo.CargoId == modelCargo.CargoId)
                        {
                            modelCargo.CargoName = respCargo.CargoName;
                            modelCargo.Weight = respCargo.Weight;
                            modelCargo.Length = respCargo.Length;
                            modelCargo.Height = respCargo.Height;
                            modelCargo.Width = respCargo.Width;
                            modelCargo.Volume = respCargo.Volume;

                            cargoExistInResponse = true;
                        }
                    }

                    if (cargoExistInResponse == false)
                        modelCargo.IsDeleted = true;
                }

                //Добавляем все новые грузы, т.е те, у которых CargoId == 0
                foreach (var respCargo in respCargoes)
                    if (respCargo.CargoId == 0)
                        modelCargoes.Add(respCargo);

                //Удаляем файлы, которые пользователь отметил как удаленные
                var attForRemoving = new List<RequestAttachment>();
                if (oldFiles != null) //Если остался хоть один старый файл
                {
                    attForRemoving = model.Request.RequestAttachments.Where(a => !oldFiles.Contains(a.Id)).ToList();
                }
                else //Если пользователь удаляет все файлы
                {
                    attForRemoving = model.Request.RequestAttachments.Where(a => (a.IsDeleted == false || a.IsDeleted == null)).ToList();
                }
                foreach (var att in attForRemoving)
                {
                    att.IsDeleted = true;
                }

                //Добавляем новые файлы
                if (files != null)
                {
                    foreach (var file in files)
                    {
                        if (file != null)
                        {
                            RequestAttachment attFile = new RequestAttachment();
                            attFile.Name = file.FileName.Substring(file.FileName.LastIndexOf("\\") + 1);

                            int fl = file.ContentLength;
                            byte[] fc = new byte[fl];

                            file.InputStream.Read(fc, 0, fl);

                            attFile.Cont = fc;
                            attFile.IsDeleted = false;

                            model.Request.RequestAttachments.Add(attFile);
                        }
                    }
                }

                _db.SaveChanges();

                return RedirectToAction("Index", "Home");
            }
            catch
            {
                ViewBag.ErrMessage = "Ошибка при редактировании записи";
                ViewBag.BackController = "Freight";
                return View("Error");
            }
        }