Пример #1
0
        public ActionResult Edit(int id, ECallMasterRecordType masterRecordType)
        {
            ECallRecord model;

            //# validation
            if (id < 0)
                throw new SystemException("Invalid Entity ID");

            //# get old record
            var records = from e in this._db.recsys_order
                          join cd in this._db.recsys_relate_customers on e.customer_id equals cd.id into cds
                          from cd in cds.DefaultIfEmpty()
                          join c in this._db.recsys_customers on cd.customer_id equals c.id into cs
                          from c in cs.DefaultIfEmpty()
                          join u in this._db.recsys_users on e.update_user_id equals u.id into uss
                          from u in uss.DefaultIfEmpty()
                          join d1 in this._db.recsys_district on cd.district1 equals d1.id into d1s
                          from d1 in d1s.DefaultIfEmpty()
                          join d2 in this._db.recsys_district on cd.district2 equals d2.id into d2s
                          from d2 in d2s.DefaultIfEmpty()
                          join t1 in this._db.recsys_users on e.engineer_id_1 equals t1.id into t1s
                          from t1 in t1s.DefaultIfEmpty()
                          join t2 in this._db.recsys_users on e.engineer_id_2 equals t2.id into t2s
                          from t2 in t2s.DefaultIfEmpty()
                          join t3 in this._db.recsys_users on e.engineer_id_3 equals t3.id into t3s
                          from t3 in t3s.DefaultIfEmpty()
                          join t4 in this._db.recsys_users on e.engineer_id_4 equals t4.id into t4s
                          from t4 in t4s.DefaultIfEmpty()
                          join cu in this._db.recsys_users on e.call_pickup_user_id equals cu.id into cus
                          from cu in cus.DefaultIfEmpty()
                          where e.id == id
                          select
                          new
                          {
                              ECall = e,
                              CustomerData = cd,
                              masterCustomer = c,
                              UpdateUserName = u.name,
                              WorkingDistrictName = d2.name,
                              MailingDistrictName = d1.name,
                              Technician1Name = t1.name,
                              Technician2Name = t2.name,
                              Technician3Name = t3.name,
                              Technician4Name = t4.name,
                              CallPickerName = cu.name
                          };

            var record = records.FirstOrDefault();

            //# validation
            if (record == null)
                throw new RecordNotFoundException();

            model = new ECallRecord()
            {
                //# field mapping
                CompleteDate = record.ECall.completion_date.HasValue ? record.ECall.completion_date.Value.ToString("dd-MM-yyyy") : string.Empty,
                EndTimeHour = record.ECall.end_time.HasValue ? Convert.ToString(record.ECall.end_time.Value.Hours) : string.Empty,
                EndTimeMinute = record.ECall.end_time.HasValue ? Convert.ToString(record.ECall.end_time.Value.Minutes) : string.Empty,
                ActualStartTimeHour = record.ECall.actual_start_time.HasValue ? Convert.ToString(record.ECall.actual_start_time.Value.Hours) : string.Empty,///Peter
                ActualStartTimeMinute = record.ECall.actual_start_time.HasValue ? Convert.ToString(record.ECall.actual_start_time.Value.Minutes) : string.Empty,
                ActualEndTimeHour = record.ECall.actual_end_time.HasValue ? Convert.ToString(record.ECall.actual_end_time.Value.Hours) : string.Empty,///Peter
                ActualEndTimeMinute = record.ECall.actual_end_time.HasValue ? Convert.ToString(record.ECall.actual_end_time.Value.Minutes) : string.Empty,
                engineer_id_1 = record.ECall.engineer_id_1,
                engineer_id_2 = record.ECall.engineer_id_2,
                engineer_id_3 = record.ECall.engineer_id_3,
                engineer_id_4 = record.ECall.engineer_id_4,
                fault = record.ECall.fault,
                model = record.ECall.model,
                repair = record.ECall.repair,
                repair_date = record.ECall.repair_date.HasValue ? record.ECall.repair_date.Value.ToString("dd-MM-yyyy") : string.Empty,
                report = record.ECall.report,
                reviewer = record.ECall.reviewer,
                StartTimeHour = record.ECall.start_time.HasValue ? Convert.ToString(record.ECall.start_time.Value.Hours) : string.Empty,
                StartTimeMinute = record.ECall.start_time.HasValue ? Convert.ToString(record.ECall.start_time.Value.Minutes) : string.Empty,
                tc = record.ECall.tc.ToString() == "1",
                address1 = record.CustomerData.address1,
                address2 = record.CustomerData.address2,
                call_pickup_user_id = record.ECall.call_pickup_user_id,
                CallPickerName = record.CallPickerName,
                center = record.CustomerData.center,
                code = record.CustomerData.code,
                contact = record.CustomerData.contact,
                customer_code = record.CustomerData.customer_code,
                district1 = record.CustomerData.district1,
                district2 = record.CustomerData.district2,
                email = record.CustomerData.email,
                fax1 = record.CustomerData.fax1,
                fax2 = record.CustomerData.fax2,
                group_id = record.CustomerData.group_id,
                id = record.ECall.id,
                last_update = record.ECall.last_update,
                last_update_user_name = record.UpdateUserName,
                MailingDistrictName = record.MailingDistrictName,
                name = record.CustomerData.name,
                chi_name = record.CustomerData.chi_name,
                prefix = record.CustomerData.prefix,
                remark = record.CustomerData.remark,
                remark2 = record.ECall.remark,
                status = record.ECall.status,
                tel1 = record.CustomerData.tel1,
                tel2 = record.CustomerData.tel2,
                title = record.CustomerData.title.HasValue ? record.CustomerData.title.Value : 0,
                type = record.CustomerData.type.HasValue ? record.CustomerData.type.Value : 0,
                WorkingDistrictName = record.WorkingDistrictName,
                create_date = record.ECall.create_date,
                Technician1Name = record.Technician1Name,
                Technician2Name = record.Technician2Name,
                Technician3Name = record.Technician3Name,
                Technician4Name = record.Technician4Name,
                //customer_reference_number = record.CustomerData.reference_number,
                //customer_reference_number = record.CustomerData.reference_number,
                //get the newset number
                customer_reference_number = record.masterCustomer.reference_number,
                JobNumber = record.ECall.job_number,
                order_status = (int)record.ECall.order_status,

                //# settings
                SaveResult = null,
                Mode = PageMode.Edit,
                FetchRecordTime = DateTime.Now,
                MasterRecordType = masterRecordType,

                //# masters
                Districts = this._db.recsys_district
                                                        .Where(theDistrict => theDistrict.status == 1)
                                                        .OrderBy(theDistrict => theDistrict.name),
                Groups = this._db.recsys_group
                                                        .Where(theGroup => theGroup.status == 1)
                                                        .OrderBy(theGroup => theGroup.name),
                Titles = this._title,
                Statuses = this._status,
                Reviewers = this._db.recsys_users
                                                        .Where(theUser => theUser.status == 1)
                                                        .OrderBy(theUser => theUser.name)
                                                        .Select(theUser => new Reviewer()
                                                        {
                                                            ID = theUser.id,
                                                            Name = theUser.name
                                                        }),
                OrderStatuses = this._orderStatuses
            };

            return PartialView("AddEdit", model);
        }
Пример #2
0
        public ActionResult AddSave(ECallRecord data)
        {
            //# validation
            if (data.MasterRecordID < 0)
                throw new SystemException("Invalid Entity ID");

            int timeInt = 0;
            if (!Int32.TryParse(data.StartTimeHour, out timeInt) || !String.IsNullOrEmpty(data.StartTimeHour))
            {
                if (Convert.ToInt32(data.StartTimeHour) < 0 || Convert.ToInt32(data.StartTimeHour) > 23)
                {
                    data.SaveResult.WarningMessage = "請修改開始時間";
                    data.SaveResult.SavedSuccessfully = false;
                    return Json(data);
                }
            }
            if (!Int32.TryParse(data.StartTimeMinute, out timeInt) || !String.IsNullOrEmpty(data.StartTimeMinute))
            {
                if (Convert.ToInt32(data.StartTimeMinute) < 0 || Convert.ToInt32(data.StartTimeMinute) > 59)
                {
                    data.SaveResult.WarningMessage = "請修改開始時間";
                    data.SaveResult.SavedSuccessfully = false;
                    return Json(data);
                }
            }
            if (!Int32.TryParse(data.EndTimeHour, out timeInt) || !String.IsNullOrEmpty(data.EndTimeHour))
            {
                if (Convert.ToInt32(data.EndTimeHour) < 0 || Convert.ToInt32(data.EndTimeHour) > 23)
                {
                    data.SaveResult.WarningMessage = "請修改結束時間";
                    data.SaveResult.SavedSuccessfully = false;
                    return Json(data);
                }
            }
            if (!Int32.TryParse(data.EndTimeMinute, out timeInt) || !String.IsNullOrEmpty(data.EndTimeMinute))
            {
                if (Convert.ToInt32(data.EndTimeMinute) < 0 || Convert.ToInt32(data.EndTimeMinute) > 59)
                {
                    data.SaveResult.WarningMessage = "請修改結束時間";
                    data.SaveResult.SavedSuccessfully = false;
                    return Json(data);
                }
            }
            if (!String.IsNullOrEmpty(data.repair_date) && !String.IsNullOrEmpty(data.CompleteDate))
            {
                if (DateTime.ParseExact(data.CompleteDate, "dd-MM-yyyy", null) < DateTime.ParseExact(data.repair_date, "dd-MM-yyyy", null))
                {
                    data.SaveResult.WarningMessage = "完成日期應在維修日期之後";
                    data.SaveResult.SavedSuccessfully = false;
                    return Json(data);
                }
            }

            Member member = new Member("users");
            bool createNewDistrict = false;
            if (!string.IsNullOrEmpty(data.WorkingDistrictName))
            {
                recsys_district workingDistrict = this._db.recsys_district.Where(x => x.name.Trim().ToUpper().Equals(data.WorkingDistrictName.Trim().ToUpper())).SingleOrDefault();
                if (workingDistrict != null)
                {
                    data.district2 = workingDistrict.id;
                    data.group_id = workingDistrict.group_id;
                }
                else
                    createNewDistrict = true;
            }

            if (createNewDistrict)
            {
                recsys_district addNewDistrict = new recsys_district()
                {
                    code = data.WorkingDistrictName.Trim(),
                    name = data.WorkingDistrictName.Trim(),
                    group_id = this._db.recsys_group.Where(x => x.name.Equals("Unknown Group For System Migration")).SingleOrDefault().id,
                    region = 4,
                    status = 1,
                    last_update = DateTime.Now,
                    update_user_id = (int)member.infoBySession("id")
                };

                this._db.recsys_district.AddObject(addNewDistrict);
                this._db.SaveChanges();

                data.district2 = this._db.recsys_district.Where(x => x.name.Equals(data.WorkingDistrictName.Trim())).SingleOrDefault().id;
                data.group_id = this._db.recsys_district.Where(x => x.name.Equals(data.WorkingDistrictName.Trim())).SingleOrDefault().group_id;
            }

            createNewDistrict = false;
            if (!string.IsNullOrEmpty(data.MailingDistrictName))
            {
                recsys_district mailingDistrict = this._db.recsys_district.Where(x => x.name.Trim().ToUpper().Equals(data.MailingDistrictName.Trim().ToUpper())).SingleOrDefault();
                if (mailingDistrict != null)
                {
                    data.district1 = mailingDistrict.id;
                    data.group_id = mailingDistrict.group_id;
                }
                else
                    createNewDistrict = true;
            }

            if (createNewDistrict)
            {
                recsys_district addNewDistrict = new recsys_district()
                {
                    code = data.MailingDistrictName.Trim(),
                    name = data.MailingDistrictName.Trim(),
                    group_id = this._db.recsys_group.Where(x => x.name.Equals("Unknown Group For System Migration")).SingleOrDefault().id,
                    region = 4,
                    status = 1,
                    last_update = DateTime.Now,
                    update_user_id = (int)member.infoBySession("id")
                };

                this._db.recsys_district.AddObject(addNewDistrict);
                this._db.SaveChanges();

                data.district1 = this._db.recsys_district.Where(x => x.name.Equals(data.MailingDistrictName.Trim())).SingleOrDefault().id;
                data.group_id = this._db.recsys_district.Where(x => x.name.Equals(data.MailingDistrictName.Trim())).SingleOrDefault().group_id;
            }

            //# get old master customer record
            if (data.MasterRecordType == ECallMasterRecordType.quotation)
                return this.AddSave_Quotation(data);
            else if (data.MasterRecordType == ECallMasterRecordType.maintenance)
                return this.AddSave_Maintenance(data);
            else
                return this.AddSave_Customer(data);
        }
Пример #3
0
        public ActionResult AddSave_Quotation(ECallRecord data)
        {
            Member member;
            recsys_relate_customers masterCustomerData;
            recsys_relate_customers customerData;
            recsys_order ecall;
            recsys_relate relation;

            //# validation
            if (data.MasterRecordID < 0)
                throw new SystemException("Invalid Entity ID");

            //# get old master customer record
            var _masterCustomerData = from q in this._db.recsys_quotation
                                        join cd in this._db.recsys_relate_customers on q.customer_id equals cd.id into cds
                                        from cd in cds.DefaultIfEmpty()
                                        where q.id == data.MasterRecordID
                                        select new
                                        {
                                            cd
                                        };
            masterCustomerData = _masterCustomerData.Select(theRecord => theRecord.cd).FirstOrDefault();

            //# validation
            if (masterCustomerData == null)
                throw new RecordNotFoundException();

            member = new Member("users");

            //# mapping
            customerData = new recsys_relate_customers()
            {
                address1 = data.address1,
                address2 = data.address2,
                center = masterCustomerData.center,
                code = masterCustomerData.code,
                contact = data.contact,
                customer_code = masterCustomerData.customer_code,
                customer_id = masterCustomerData.customer_id,
                district1 = data.district1,
                district2 = data.district2,
                email = data.email,
                fax1 = data.fax1,
                fax2 = data.fax2,
                group_id = data.group_id,
                manual_input_code = masterCustomerData.manual_input_code,
                name = data.name,
                chi_name = data.chi_name,
                prefix = masterCustomerData.prefix,
                relate_type = (int)Rec_System.Models.Customers.CustomerDataRelateType.Quotation,
                remark = data.remark,
                reference_number = masterCustomerData.reference_number,
                tel1 = data.tel1,
                tel2 = data.tel2,
                title = (byte)data.title,
                type = (byte)masterCustomerData.type
            };

            this._db.recsys_relate_customers.AddObject(customerData);

            ecall = new recsys_order()
            {
                by_system = 0,
                completion_date = DateChecking(data.CompleteDate),
                call_pickup_user_id = data.call_pickup_user_id,
                end_time = new TimeSpan(Convert.ToInt32(data.EndTimeHour), Convert.ToInt32(data.EndTimeMinute), 0),
                engineer_id_1 = data.engineer_id_1,
                engineer_id_2 = data.engineer_id_2,
                engineer_id_3 = data.engineer_id_3,
                engineer_id_4 = data.engineer_id_4,
                fault = data.fault,
                model = data.model,
                repair = data.repair,
                repair_date = DateChecking(data.repair_date),
                report = data.report,
                reviewer = data.reviewer,
                start_time = new TimeSpan(Convert.ToInt32(data.StartTimeHour), Convert.ToInt32(data.StartTimeMinute), 0),
                tc = (byte) (data.tc ? 1 : 0),
                create_date = DateTime.Now,
                last_update = DateTime.Now,
                remark = data.remark2,
                status = (byte)data.status,
                update_user_id = (int)member.infoBySession("id"),
                job_number = data.JobNumber,
                order_status = DateChecking(data.CompleteDate).HasValue ? 3 : (DateChecking(data.repair_date).HasValue ? 2 : 1)
            };

            this._db.recsys_order.AddObject(ecall);

            try
            {
                this._db.SaveChanges();

                this._db.Refresh(System.Data.Objects.RefreshMode.StoreWins, customerData);
                this._db.Refresh(System.Data.Objects.RefreshMode.StoreWins, ecall);

                ecall.customer_id = customerData.id;

                this._db.SaveChanges();

                data.id = ecall.id;

                relation = new recsys_relate()
                {
                    id1 = data.MasterRecordID,
                    id2 = ecall.id,
                    table1 = "quotation",
                    table2 = "order"
                };

                this._db.recsys_relate.AddObject(relation);

                this._db.SaveChanges();

                data.SaveResult.SavedSuccessfully = true;
            }
            catch (OptimisticConcurrencyException)
            {
                //# log down
            }

            return Json(data);
        }
Пример #4
0
        public ActionResult Add_FromQuotation(int quotationID)
        {
            ECallRecord model;
            Member member;

            //# get old master customer record
            var records = from q in this._db.recsys_quotation
                          join cd in this._db.recsys_relate_customers on q.customer_id equals cd.id into cds
                          from cd in cds.DefaultIfEmpty()
                          join d1 in this._db.recsys_district on cd.district1 equals d1.id into d1s
                          from d1 in d1s.DefaultIfEmpty()
                          join d2 in this._db.recsys_district on cd.district2 equals d2.id into d2s
                          from d2 in d2s.DefaultIfEmpty()
                          where q.id == quotationID
                          select new
                          {
                              masterCustomerData = cd,
                              MailingDistrictName = d1.name,
                              WorkingDistrictName = d2.name
                          };
            var record = records.FirstOrDefault();

            //# validation
            if (record == null)
                throw new RecordNotFoundException();

            member = new Member("users");

            model = new ECallRecord()
            {
                //# customer data fields mapping
                MasterRecordType = ECallMasterRecordType.quotation,
                address1 = record.masterCustomerData.address1,
                address2 = record.masterCustomerData.address2,
                center = record.masterCustomerData.center,
                code = record.masterCustomerData.code,
                contact = record.masterCustomerData.contact,
                customer_code = record.masterCustomerData.customer_code,
                customer_reference_number = record.masterCustomerData.reference_number,
                district1 = record.masterCustomerData.district1,
                district2 = record.masterCustomerData.district2,
                email = record.masterCustomerData.email,
                fax1 = record.masterCustomerData.fax1,
                fax2 = record.masterCustomerData.fax2,
                MailingDistrictName = record.MailingDistrictName,
                MasterRecordID = quotationID,
                name = record.masterCustomerData.name,
                chi_name = record.masterCustomerData.chi_name,
                prefix = record.masterCustomerData.prefix,
                remark = record.masterCustomerData.remark,
                tel1 = record.masterCustomerData.tel1,
                tel2 = record.masterCustomerData.tel2,
                title = record.masterCustomerData.title ?? 0,
                type = record.masterCustomerData.type ?? 0,
                WorkingDistrictName = record.WorkingDistrictName,

                //# default
                call_pickup_user_id = (int) member.infoBySession("id"),
                CallPickerName = (string) member.infoBySession("name"),
                status = (int)RecordStatus.Active,

                //# settings
                Mode = PageMode.Add,

                //# masters
                Districts = this._db.recsys_district
                                                        .Where(theDistrict => theDistrict.status == 1)
                                                        .OrderBy(theDistrict => theDistrict.name),
                Groups = this._db.recsys_group
                                                        .Where(theGroup => theGroup.status == 1)
                                                        .OrderBy(theGroup => theGroup.name),
                Titles = this._title,
                Statuses = this._status,
                Reviewers = this._db.recsys_users
                                                        .Where(theUser => theUser.status == 1)
                                                        .OrderBy(theUser => theUser.name)
                                                        .Select(theUser => new Reviewer()
                                                        {
                                                            ID = theUser.id,
                                                            Name = theUser.name
                                                        }),
                OrderStatuses = this._orderStatuses

            };

            return PartialView("AddEdit", model);
        }
Пример #5
0
        public ActionResult EditSave(ECallRecord data)
        {
            recsys_order ecall;
            recsys_relate_customers customerData;

            //# validation
            if (data.id < 0)
                throw new SystemException("Invalid Entity ID");

            int timeInt = 0;
            if (!String.IsNullOrEmpty(data.StartTimeHour))
            {
                if (!Int32.TryParse(data.StartTimeHour, out timeInt) || Convert.ToInt32(data.StartTimeHour) < 0 || Convert.ToInt32(data.StartTimeHour) > 23)
                {
                    data.SaveResult.WarningMessage = "請修改開始時間";
                    data.SaveResult.SavedSuccessfully = false;
                    return Json(data);
                }
            }
            if (!Int32.TryParse(data.StartTimeMinute, out timeInt) || !String.IsNullOrEmpty(data.StartTimeMinute))
            {
                if (Convert.ToInt32(data.StartTimeMinute) < 0 || Convert.ToInt32(data.StartTimeMinute) > 59)
                {
                    data.SaveResult.WarningMessage = "請修改開始時間";
                    data.SaveResult.SavedSuccessfully = false;
                    return Json(data);
                }
            }
            if (!Int32.TryParse(data.EndTimeHour, out timeInt) || !String.IsNullOrEmpty(data.EndTimeHour))
            {
                if (Convert.ToInt32(data.EndTimeHour) < 0 || Convert.ToInt32(data.EndTimeHour) > 23)
                {
                    data.SaveResult.WarningMessage = "請修改結束時間";
                    data.SaveResult.SavedSuccessfully = false;
                    return Json(data);
                }
            }
            if (!Int32.TryParse(data.EndTimeMinute, out timeInt) || !String.IsNullOrEmpty(data.EndTimeMinute))
            {
                if (Convert.ToInt32(data.EndTimeMinute) < 0 || Convert.ToInt32(data.EndTimeMinute) > 59)
                {
                    data.SaveResult.WarningMessage = "請修改結束時間";
                    data.SaveResult.SavedSuccessfully = false;
                    return Json(data);
                }
            }
            if (!String.IsNullOrEmpty(data.repair_date) && !String.IsNullOrEmpty(data.CompleteDate))
            {
                if (DateTime.ParseExact(data.CompleteDate, "dd-MM-yyyy", null) < DateTime.ParseExact(data.repair_date, "dd-MM-yyyy", null))
                {
                    data.SaveResult.WarningMessage = "完成日期應在維修日期之後";
                    data.SaveResult.SavedSuccessfully = false;
                    return Json(data);
                }
            }

            //# get old record
            var records = from e in this._db.recsys_order
                          join c in this._db.recsys_relate_customers on e.customer_id equals c.id into cs
                          from c in cs.DefaultIfEmpty()
                          where e.id == data.id
                          select new
                          {
                              ECall = e,
                              CustomerData = c
                          };
            var record = records.FirstOrDefault();

            Member member = new Member("users");

            //# validation
            if (record == null)
                throw new RecordNotFoundException();

            ecall = record.ECall;
            customerData = record.CustomerData;

            //# validation
            if (ecall == null || customerData == null)
                throw new RecordNotFoundException();
            if (!ecall.last_update.HasValue)
                throw new ImproperDataException();
            if (ecall.last_update.Value > data.FetchRecordTime)
                throw new OptimisticConcurrencyException("資料已被其他使用者更新");

            bool createNewDistrict = false;
            if (!string.IsNullOrEmpty(data.WorkingDistrictName))
            {
                recsys_district workingDistrict = this._db.recsys_district.Where(x => x.name.Trim().ToUpper().Equals(data.WorkingDistrictName.Trim().ToUpper())).SingleOrDefault();
                if (workingDistrict != null)
                {
                    data.district2 = workingDistrict.id;
                    data.group_id = workingDistrict.group_id;
                }
                else
                    createNewDistrict = true;
            }

            if (createNewDistrict)
            {
                recsys_district addNewDistrict = new recsys_district()
                {
                    code = data.WorkingDistrictName.Trim(),
                    name = data.WorkingDistrictName.Trim(),
                    group_id = this._db.recsys_group.Where(x => x.name.Equals("Unknown Group For System Migration")).SingleOrDefault().id,
                    region = 4,
                    status = 1,
                    last_update = DateTime.Now,
                    update_user_id = (int)member.infoBySession("id")
                };

                this._db.recsys_district.AddObject(addNewDistrict);
                this._db.SaveChanges();

                data.district2 = this._db.recsys_district.Where(x => x.name.Equals(data.WorkingDistrictName.Trim())).SingleOrDefault().id;
                data.group_id = this._db.recsys_district.Where(x => x.name.Equals(data.WorkingDistrictName.Trim())).SingleOrDefault().group_id;
            }

            createNewDistrict = false;
            if (!string.IsNullOrEmpty(data.MailingDistrictName))
            {
                recsys_district mailingDistrict = this._db.recsys_district.Where(x => x.name.Trim().ToUpper().Equals(data.MailingDistrictName.Trim().ToUpper())).SingleOrDefault();
                if (mailingDistrict != null)
                {
                    data.district1 = mailingDistrict.id;
                    data.group_id = mailingDistrict.group_id;
                }
                else
                    createNewDistrict = true;
            }

            if (createNewDistrict)
            {
                recsys_district addNewDistrict = new recsys_district()
                {
                    code = data.MailingDistrictName.Trim(),
                    name = data.MailingDistrictName.Trim(),
                    group_id = this._db.recsys_group.Where(x => x.name.Equals("Unknown Group For System Migration")).SingleOrDefault().id,
                    region = 4,
                    status = 1,
                    last_update = DateTime.Now,
                    update_user_id = (int)member.infoBySession("id")
                };

                this._db.recsys_district.AddObject(addNewDistrict);
                this._db.SaveChanges();

                data.district1 = this._db.recsys_district.Where(x => x.name.Equals(data.MailingDistrictName.Trim())).SingleOrDefault().id;
                data.group_id = this._db.recsys_district.Where(x => x.name.Equals(data.MailingDistrictName.Trim())).SingleOrDefault().group_id;
            }
            //check range of time input
            if (data.StartTimeHour != null)
            {
                if (Convert.ToInt32(data.StartTimeHour) < 0 || Convert.ToInt32(data.StartTimeHour) > 23)
                {
                    data.SaveResult.WarningMessage = "請修改至0-23以內";
                    data.SaveResult.SavedSuccessfully = false;
                    return Json(data);
                }

            }
            if (data.StartTimeMinute != null)
            {
                if (Convert.ToInt32(data.StartTimeMinute) < 0 || Convert.ToInt32(data.StartTimeMinute) > 59)
                {
                    data.SaveResult.WarningMessage = "請修改至0-59以內";
                    data.SaveResult.SavedSuccessfully = false;
                    return Json(data);
                }

            }

            //# saving
            //# mapping
            ecall.by_system = 0;
            ecall.call_pickup_user_id = data.call_pickup_user_id;
            ecall.completion_date = DateChecking(data.CompleteDate);
            if ((string.IsNullOrEmpty(data.EndTimeHour)) && (string.IsNullOrEmpty(data.EndTimeMinute)))
            {
                ecall.end_time = null;
            }
            else
            {
                ecall.end_time = new TimeSpan(Convert.ToInt32(data.EndTimeHour), Convert.ToInt32(data.EndTimeMinute), 0);
            }
            ecall.engineer_id_1 = data.engineer_id_1;
            ecall.engineer_id_2 = data.engineer_id_2;
            ecall.engineer_id_3 = data.engineer_id_3;
            ecall.engineer_id_4 = data.engineer_id_4;
            ecall.fault = data.fault;
            ecall.last_update = DateTime.Now;
            ecall.model = data.model;
            ecall.remark = data.remark2;
            ecall.repair = data.repair;
            ecall.repair_date = DateChecking(data.repair_date);
            ecall.report = data.report;
            ecall.reviewer = data.reviewer;
            if ((string.IsNullOrEmpty(data.StartTimeHour)) && (string.IsNullOrEmpty(data.StartTimeMinute)))
            {
                ecall.start_time = null;
            }
            else
            {
                ecall.start_time = new TimeSpan(Convert.ToInt32(data.StartTimeHour), Convert.ToInt32(data.StartTimeMinute), 0);
            }
            ecall.status = (byte) data.status;
            ecall.tc = (byte) (data.tc ? 1 : 0);
            ecall.update_user_id = (int)member.infoBySession("id");
            ecall.job_number = data.JobNumber;
            ecall.order_status = ecall.completion_date.HasValue ? 3 : (ecall.repair_date.HasValue ? 2 : 1);
            customerData.address1 = data.address1;
            customerData.address2 = data.address2;
            customerData.contact = data.contact;
            customerData.district1 = data.district1;
            customerData.district2 = data.district2;
            customerData.email = data.email;
            customerData.fax1 = data.fax1;
            customerData.fax2 = data.fax2;
            customerData.group_id = data.group_id;
            customerData.name = data.name;
            customerData.chi_name = data.chi_name;
            customerData.remark = data.remark;
            customerData.tel1 = data.tel1;
            customerData.tel2 = data.tel2;
            customerData.title = (byte)data.title;

            try
            {
                this._db.SaveChanges();
                this._db.Refresh(System.Data.Objects.RefreshMode.StoreWins, ecall);
                data.id = ecall.id;
                data.SaveResult.SavedSuccessfully = true;
            }
            catch (OptimisticConcurrencyException)
            {
                //# log down
            }

            return Json(data);
        }