public ActionResult EditJobSave(JobDetailEditDTO model)
        {
            bool isSuccess = true;
            bool isChanged = false;
            List<int>[] involvedTechnicianIDsArr = new List<int>[3];

            try
            {
                Dictionary<string, string> error = new Dictionary<string, string>();
                if (!model.dRepairDate.HasValue)
                {
                    error.Add("dRepairDate", "請填寫Repair Date");
                    return Json(
                    new
                    {
                        isSuccess = false,
                        errorType = "Validation",
                        validationError = error
                    });
                }
                if (model.nStartHour.HasValue && model.nStartMinute.HasValue && model.nEndHour.HasValue && model.nEndMinute.HasValue)
                {
                    if (model.nStartHour.Value != 0 || model.nEndHour != 0 || model.nStartMinute != 0 || model.nEndMinute != 0)
                    {
                        TimeSpan startTime = new TimeSpan(model.nStartHour.Value, model.nStartMinute.Value, 0);
                        TimeSpan endTime = new TimeSpan(model.nEndHour.Value, model.nEndMinute.Value, 0);
                        if (startTime > endTime)
                        {
                            return Json(
                            new
                            {
                                isSuccess = false,
                                errorType = "Time-Error",
                                exceptionMessage = "結束時間要比開始時間較後"
                            });
                        }
                    }
                }

                IJobService jobService = AutoSessionServiceFactory.GetJobService(ApplicationSetting.Current.DefaultConnectionString);
                JobDTO oldJob = jobService.GetJobByID(model.nID);

                //### Check for sending Notification
                #region Check for sending Notification
                if (!oldJob.dCompletionDate.HasValue && oldJob.nStatus != 3)
                {
                    List<int> technicianIDs = new List<int>();

                    List<int> technicianIDsInModel = new List<int>();
                    if (model.nTechnician1ID.HasValue) technicianIDsInModel.Add(model.nTechnician1ID.Value);
                    if (model.nTechnician2ID.HasValue) technicianIDsInModel.Add(model.nTechnician2ID.Value);
                    if (model.nTechnician3ID.HasValue) technicianIDsInModel.Add(model.nTechnician3ID.Value);
                    if (model.nTechnician4ID.HasValue) technicianIDsInModel.Add(model.nTechnician4ID.Value);

                    List<int> technicianIDsInOldJob = new List<int>();
                    if (oldJob.nTechnician1ID.HasValue) technicianIDsInOldJob.Add(oldJob.nTechnician1ID.Value);
                    if (oldJob.nTechnician2ID.HasValue) technicianIDsInOldJob.Add(oldJob.nTechnician2ID.Value);
                    if (oldJob.nTechnician3ID.HasValue) technicianIDsInOldJob.Add(oldJob.nTechnician3ID.Value);
                    if (oldJob.nTechnician4ID.HasValue) technicianIDsInOldJob.Add(oldJob.nTechnician4ID.Value);

                    //#Check Create (Add Technician)
                    foreach (int modelTechnicianID in technicianIDsInModel)
                    {
                        //new Technician
                        if (!technicianIDsInOldJob.Contains(modelTechnicianID))
                            technicianIDs.Add(modelTechnicianID);
                    }

                    if (technicianIDs.Count > 0)
                        involvedTechnicianIDsArr[0] = technicianIDs;

                    //#Check Update (Edit fields)
                    TimeSpan? startTime;
                    if (model.nStartHour.HasValue && model.nStartMinute.HasValue && (model.nStartHour.Value != 0 || model.nStartMinute != 0))
                    {
                        startTime = new TimeSpan(model.nStartHour.Value, model.nStartMinute.Value, 0);
                    }
                    else
                    {
                        startTime = new TimeSpan(0, 0, 0);
                    }
                    TimeSpan? endTime;
                    if (model.nEndHour.HasValue && model.nEndMinute.HasValue && (model.nEndHour.Value != 0 || model.nEndMinute != 0))
                    {
                        endTime = new TimeSpan(model.nEndHour.Value, model.nEndMinute.Value, 0);
                    }
                    else
                    {
                        endTime = new TimeSpan(0, 0, 0);
                    }

                    isChanged = (model.dRepairDate != oldJob.dRepairDate) || (startTime != oldJob.tStartTime) || (endTime != oldJob.tEndTime);

                    if (isChanged)
                    {
                        //if there is any "new" technician, other technicians receive Update Notification
                        if (technicianIDs.Count > 0)
                        {
                            technicianIDs = technicianIDsInModel.Except(technicianIDs).ToList();
                        }
                        else
                        {
                            technicianIDs = technicianIDsInModel;
                        }

                        if (technicianIDs.Count > 0)
                            involvedTechnicianIDsArr[1] = technicianIDs;
                    }

                    //#Check Delete (Remove Technician)
                    technicianIDs = new List<int>();

                    foreach (int recordTechnicianID in technicianIDsInOldJob)
                    {
                        //Removed Technician
                        if (!technicianIDsInModel.Contains(recordTechnicianID))
                            technicianIDs.Add(recordTechnicianID);
                    }

                    if (technicianIDs.Count > 0)
                    {
                        involvedTechnicianIDsArr[2] = technicianIDs;
                    }
                }
                #endregion Check for sending Notification

                jobService.UpdateJob(model);

                //Send Create, Update,
                for (int i = 0; i < 2; i++)
                {
                    if (involvedTechnicianIDsArr[i] != null)
                        this.SendNotification(i, model.nID, involvedTechnicianIDsArr[i]);
                }

                //Send Delete Notifications
                if (involvedTechnicianIDsArr[2] != null)
                    this.SendRemoveNotification(model.nID, involvedTechnicianIDsArr[2], oldJob.sCustomerCode, oldJob.sCustomerName, model.dRepairDate);
            }
            catch
            {
                isSuccess = false;
            }

            return Json(
                    new
                    {
                        isSuccess = isSuccess
                    });
        }
Exemple #2
0
        public void UpdateJob(JobDetailEditDTO model)
        {
            if (model != null)
            {
                using (var ds = new DataService(this.connectionString, this.consumerInfo))
                {
                    CrmtOrder order = ds.CrmtOrder.GetAll().Where(j => j.nID == model.nID).FirstOrDefault();
                    order.dRepairDate = model.dRepairDate;
                    if (model.nStartHour.HasValue && model.nStartMinute.HasValue && (model.nStartHour.Value != 0 || model.nStartMinute != 0))
                    {
                        order.tStartTime = new TimeSpan(model.nStartHour.Value, model.nStartMinute.Value, 0);
                    }
                    else
                    {
                        order.tStartTime = null;
                    }
                    if (model.nEndHour.HasValue && model.nEndMinute.HasValue && (model.nEndHour.Value != 0 || model.nEndMinute != 0))
                    {
                        order.tEndTime = new TimeSpan(model.nEndHour.Value, model.nEndMinute.Value, 0);
                    }
                    else
                    {
                        order.tEndTime = null;
                    }
                    order.nTechnician1ID = model.nTechnician1ID;
                    order.nTechnician2ID = model.nTechnician2ID;
                    order.nTechnician3ID = model.nTechnician3ID;
                    order.nTechnician4ID = model.nTechnician4ID;
                    order.nAdHocTechnicianID = model.nAdHocTechnicianID;

                    if (!model.bIsEcall)
                    {
                        if (model.sMaintenanceRoutine == "AY")
                        {
                            CrmtMaintenance maintenanceRecord = (from m in ds.CrmtMaintenance.GetAll()
                                                                 join r in ds.CrmtTableRelation.GetAll() on m.nID equals r.nTable1ID
                                                                 where r.sTable1 == "maintenance" && r.sTable2 == "order" && r.nTable2ID == order.nID
                                                                 select m).FirstOrDefault();

                            DateTime? contractYearStart = maintenanceRecord.dStartDate;
                            DateTime? contractYearNext = contractYearStart.Value.AddYears(1);

                            while (contractYearStart.Value <= maintenanceRecord.dEndDate.Value)
                            {
                                if (order.dRepairDate.Value >= contractYearStart.Value && order.dRepairDate.Value < contractYearNext.Value)
                                    break;

                                contractYearStart = contractYearStart.Value.AddYears(1);
                                contractYearNext = contractYearNext.Value.AddYears(1);
                            }

                            CrmtOrder originalAYorder = (from o in ds.CrmtOrder.GetAll()
                                                         join r in ds.CrmtTableRelation.GetAll() on o.nID equals r.nTable2ID
                                                         where r.sTable1 == "maintenance" && r.sTable2 == "order"
                                                         && r.nTable1ID == maintenanceRecord.nID
                                                         && o.sRoutineTermCode == "AY" && o.nID != order.nID
                                                         && o.dRepairDate.Value >= contractYearStart && o.dRepairDate.Value < contractYearNext
                                                         select o).FirstOrDefault();

                            if (originalAYorder != null)
                            {
                                string sRoutineID = maintenanceRecord.nRoutineID.ToString();
                                originalAYorder.sRoutineTermCode = (from op in ds.CrmmWomOption.GetAll() where op.sTypeCode == "MROC" && op.sKey == sRoutineID select op.sValue).FirstOrDefault();
                                originalAYorder.dLastUpdate = DateTime.Now;
                                originalAYorder.nUpdateUserID = UserInfo.Instance.UserID;
                            }

                        }
                        order.sRoutineTermCode = model.sMaintenanceRoutine;
                    }

                    order.dLastUpdate = DateTime.Now;
                    order.nUpdateUserID = UserInfo.Instance.UserID;

                    ds.SaveChanges();
                }
            }
        }