Пример #1
0
        public ActionResult Modify(JourneySupervision model, DateTime? oldTime)
        {
            if (ModelState.IsValid)
            {
                model.Updator = User.Identity.Name;
                model.UpdateTime = DateTime.Now;
                DbContext.Entry(model).State = EntityState.Modified;
                if (model.CreatedDate.Value.Date != oldTime.Value.Date)
                {
                    var list = new List<Result>();
                    var result =repository.UpdateResult(model.ShipId, model.TripId, model.StateId,model.PortId);
                    if (result !=null)
                    {
                        foreach (var item in result)
                        {

                            item.JourneyDate = model.CreatedDate;
                            if (item.PlanDate != null)
                            {
                                var days = (item.PlanDate.Value.Date - model.CreatedDate.Value.Date).Days;
                                if (days > 0)
                                {
                                    var mode = DbContext.Results.Find(item.ResultId);
                                    mode.Remark = "Sớm hơn " + days + " ngày so với dự kiến";
                                    mode.JourneyDate = model.CreatedDate;
                                    list.Add(mode);
                                    DbContext.SaveChanges();
                                }
                                if (days < 0)
                                {
                                    var mode = DbContext.Results.Find(item.ResultId);
                                    mode.Remark = "Trễ hơn " + -days + " ngày so với dự kiến";
                                    mode.JourneyDate = model.CreatedDate;
                                    list.Add(mode);
                                    DbContext.SaveChanges();
                                }
                                if (days == 0)
                                {
                                    var mode = DbContext.Results.Find(item.ResultId);
                                    mode.Remark = "Đúng theo dự kiến";
                                    mode.JourneyDate = model.CreatedDate;
                                    mode.ResonId = null;
                                    DbContext.SaveChanges();
                                }
                            }
                        }
                        if (list.Count>0)
                        {
                            ViewBag.availableResons = repository.GetResons();
                            return View("UpdateReson",list);
                        }
                    }
                }
                DbContext.SaveChanges();
            }
            return RedirectToAction("Detail", "Home", new { shipId = model.ShipId, tripId = model.TripId, all = true, currentTrip = true });
        }
        //public Single SetRestWeight(int shipId, string tripId, int taskId, int? stateId, Single arrangedWeight)
        //{
        //    var jns = db.Journeys.FirstOrDefault(p => p.TripId == tripId && p.ShipId == shipId);
        //    Single result = (jns.Weight - arrangedWeight);
        //    return result;
        //}
        private void UpdateModels(UpdateViewModel upv, string tripId, int shipId, dynamic model, int kind, string userName)
        {
            model.ShipId = shipId;
            model.TripId = tripId;
            switch (kind)
            {
                case 0:
                    int taskId = model.TaskId;
                    int? stateId = model.StateId;
                    int? portId = model.PortId;
                    //if ((stateId == ConstId.LoadedStateId || stateId == ConstId.UnloadedStateId || stateId == ConstId.UnloadingStateId || stateId == ConstId.LoadingStateId) && (taskId == ConstId.LoadTaskId || taskId == ConstId.UnloadTaskId))
                    //{
                    //    model.RestWeight = SetRestWeight(shipId, tripId, taskId, stateId, model.ArrangedWeight);
                    //}
                    //JourneySupervision a = db.JourneySuppervision.Where(p => p.TripId == tripId && p.ShipId == shipId && p.TaskId == taskId && p.PortId == portId && p.StateId == stateId).FirstOrDefault();
                    //if (a == null)
                    //{
                    if (model.PortId != null || model.CreatedDate != null || model.Position != null || model.ArrangedWeight != 0)
                    {
                        var js = new JourneySupervision
                        {
                            ShipId = shipId,
                            TripId = tripId,
                            StateId = stateId,
                            PortId = model.PortId,
                            ArrangedWeight = model.ArrangedWeight,
                            CreatedDate = model.CreatedDate,
                            RestWeight = model.RestWeight,
                            Position = model.Position,
                            TaskId = taskId,
                            CategoryName = model.CategoryName,
                            Updator = userName,
                            UpdateTime = DateTime.Now,
                            NationName = model.NationName
                        };
                        db.JourneySuppervision.Add(js);
                    }
                    //}
                    //else
                    //{
                    //    if (model.StateId == ConstId.LoadingStateId || model.StateId == ConstId.UnloadingStateId)
                    //    {
                    //        var js = new JourneySupervision
                    //        {
                    //            ShipId = shipId,
                    //            TripId = tripId,
                    //            StateId = stateId,
                    //            PortId = model.PortId,
                    //            ArrangedWeight = model.ArrangedWeight,
                    //            CreatedDate = model.CreatedDate,
                    //            RestWeight = model.RestWeight,
                    //            Position = model.Position,
                    //            TaskId = taskId,
                    //            CategoryName = model.CategoryName,
                    //            Updator = userName,
                    //            UpdateTime = DateTime.Now
                    //        };
                    //        db.JourneySuppervision.Add(js);
                    //    }
                    //    else
                    //    {
                    //        a.ArrangedWeight = model.ArrangedWeight ?? 0;
                    //        a.RestWeight = model.RestWeight ?? 0;
                    //        a.CreatedDate = model.CreatedDate;
                    //        a.Note = model.Note;
                    //        a.PortId = model.PortId;
                    //        a.Position = model.Position;
                    //        a.StateId = stateId;
                    //        a.CategoryName = model.CategoryName;
                    //        a.UpdateTime = DateTime.Now;
                    //        a.Updator = userName;
                    //        db.Entry(a).State = EntityState.Modified;
                    //    }
                    //}
                    if (model.StateId != null)
                    {
                        var plan = db.Compares.FirstOrDefault(p => p.StateId == stateId);
                        if (plan != null)
                        {
                            string plid = plan.PlanId;

                            var jourpl = db.journeyPlans.FirstOrDefault(p => p.ShipId == shipId && p.TripId == tripId && p.PlanId == plid && p.PortId == portId);
                            if (jourpl != null)
                            {
                                var plTime = jourpl.PlanTime;
                                DateTime? jnTime = model.CreatedDate;
                                if (plTime != null && jnTime != null)
                                {
                                    var rs1 = db.Results.Where(p => p.TripId == tripId && p.ShipId == shipId && p.PlanId == plid && p.PortId == portId);
                                    if (rs1 != null)
                                    {
                                        foreach (var item in rs1)
                                        {
                                            item.StateId = stateId;
                                            item.JourneyDate = jnTime;
                                            var days = (item.PlanDate.Value - jnTime.Value).Days;
                                            if (days > 0)
                                            {
                                                upv.IsLate = 1;
                                                item.Remark = "Sớm hơn " + days + " ngày so với dự kiến";
                                            }
                                            if (days < 0)
                                            {
                                                item.Remark = "Trễ hơn " + -days + " ngày so với dự kiến";
                                                upv.IsLate = 2;
                                            }
                                            if (days == 0)
                                            {
                                                upv.IsLate = 0;
                                                item.Remark = "Đúng theo dự kiến";
                                            }
                                        }
                                        upv.rStateId = stateId;
                                        upv.rPlanId = plid;
                                        upv.rShipId = shipId;
                                    }
                                }
                            }
                        }

                    }
                    string ctripId = GenerateTripId(tripId);
                    var jn = db.Journeys.FirstOrDefault(p => p.ShipId == shipId && p.TripId == ctripId);
                    if (model.TaskId == ConstId.UnloadTaskId && model.StateId == ConstId.UnloadedStateId && jn == null)
                    {
                        var now = model.CreatedDate??DateTime.Now;
                        var updateJourney = db.Journeys.FirstOrDefault(p => p.ShipId == shipId && p.TripId == tripId);
                        updateJourney.EndDate = now;
                       // db.Entry(updateJourney).State = System.Data.EntityState.Modified;
                       // db.SaveChanges();
                        var journey = new Journey { ShipId = model.ShipId, TripId = ctripId, StartPortId = model.PortId, StartDate = now };
                        db.Journeys.Add(journey);
                    }
                    db.SaveChanges();

                    break;
                case 1:
                    string planId = model.PlanId;
                    //var b = db.journeyPlans.FirstOrDefault(p => p.TripId == tripId && p.ShipId == shipId && p.PlanId == planId);
                    //if (b != null)
                    //{
                    //    b.PlanTime = model.PlanTime;
                    //    b.PortId = model.PortId;
                    //    b.UpdateTime = DateTime.Now;
                    //    b.Updator = userName;
                    //}
                    //else
                    //{
                    if (model.PlanId != null || model.PlanTime != null && model.PortId)
                    {
                        var jp = new JourneyPlan
                        {
                            ShipId = shipId,
                            TripId = tripId,
                            PlanId = planId,
                            PlanTime = model.PlanTime,
                            PortId = model.PortId,
                            Updator = userName,
                            UpdateTime = DateTime.Now
                        };
                        var rs = new Result { PlanDate = model.PlanTime, PlanId = planId, ShipId = shipId, TripId = tripId, PortId = model.PortId };
                        db.journeyPlans.Add(jp);
                        db.Results.Add(rs);
                    }
                    //}
                    db.SaveChanges();
                    break;
                case 2:
                    bool isnull = string.IsNullOrEmpty(model.NoteContent);
                    if (!isnull)
                    {
                        var note = new Note { CreatedDate = model.CreatedDate, ShipId = shipId, TripId = tripId, NoteContent = model.NoteContent, Updator = userName, UpdateTime = DateTime.Now };
                        db.Notes.Add(note);
                        db.SaveChanges();
                    }
                    break;
                default:
                    break;
            }
        }
        public LoadingReport GetLoadingReport(int shipId, string tripId, bool UnLoad)
        {
            var result = new LoadingReport();
            var temp = new JourneySupervision();
            var list = new List<JourneySupervision>();
            if (!UnLoad)
            {
                list = db.JourneySuppervision.Where(p => p.ShipId == shipId && p.TripId == tripId && p.TaskId == 1).ToList();
            }
            else
            {
                list = db.JourneySuppervision.Where(p => p.ShipId == shipId && p.TripId == tripId && p.TaskId == 2).ToList();

            }
            temp = list.FirstOrDefault(p => p.StateId == 1);
            if (temp != null && temp.CreatedDate.HasValue)
            {
                result.PortComeDate = temp.CreatedDate.Value;
                result.Goods = temp.CategoryName;
                result.PortName = temp.Port.PortName;
            }
            temp = list.FirstOrDefault(p => p.StateId == 9);
            if (temp != null && temp.CreatedDate.HasValue)
            {
                result.PortComeTime = temp.CreatedDate.Value;
            }
            temp = list.FirstOrDefault(p => p.StateId == 4);
            if (temp != null && temp.CreatedDate.HasValue)
            {
                result.LoadingTime = temp.CreatedDate.Value;
            }
            temp = list.FirstOrDefault(p => p.StateId == 11);
            if (temp != null && temp.CreatedDate.HasValue)
            {
                result.LoadingComplTime = temp.CreatedDate.Value;
            }
            temp = list.FirstOrDefault(p => p.StateId == 10);
            if (temp != null && temp.CreatedDate.HasValue)
            {
                result.LeavingTime = temp.CreatedDate.Value;
            }
            return result;
        }