Пример #1
0
        public ActionResult EditSave(OrderRecord data)
        {
            recsys_order order;
            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 (!String.IsNullOrEmpty(data.StartTimeMinute))
            {
                if (!Int32.TryParse(data.StartTimeMinute, out timeInt) || Convert.ToInt32(data.StartTimeMinute) < 0 || Convert.ToInt32(data.StartTimeMinute) > 59)
                {
                    data.SaveResult.WarningMessage = "請修改開始時間";
                    data.SaveResult.SavedSuccessfully = false;
                    return Json(data);
                }
            }
            if (!String.IsNullOrEmpty(data.EndTimeHour))
            {
                if (!Int32.TryParse(data.EndTimeHour, out timeInt) || Convert.ToInt32(data.EndTimeHour) < 0 || Convert.ToInt32(data.EndTimeHour) > 23)
                {
                    data.SaveResult.WarningMessage = "請修改結束時間";
                    data.SaveResult.SavedSuccessfully = false;
                    return Json(data);
                }
            }
            if (!String.IsNullOrEmpty(data.EndTimeMinute))
            {
                if (!Int32.TryParse(data.EndTimeMinute, out timeInt) || 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
                          {
                              Order = e,
                              CustomerData = c
                          };
            var record = records.FirstOrDefault();

            Member member = new Member("users");

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

            order = record.Order;
            customerData = record.CustomerData;

            //# validation
            if (order == null || customerData == null)
                throw new RecordNotFoundException();
            if (!order.last_update.HasValue)
                throw new ImproperDataException();
            if (order.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;
            }

            //# saving
            //# mapping
            order.by_system = 1;
            order.completion_date = DateChecking(data.CompleteDate);
            if ((String.IsNullOrEmpty(data.EndTimeHour)) && (String.IsNullOrEmpty(data.EndTimeMinute)))
                order.end_time = null;
            else
                order.end_time = new TimeSpan(Convert.ToInt32(data.EndTimeHour), Convert.ToInt32(data.EndTimeMinute), 0);
            order.engineer_id_1 = data.engineer_id_1;
            order.engineer_id_2 = data.engineer_id_2;
            order.engineer_id_3 = data.engineer_id_3;
            order.engineer_id_4 = data.engineer_id_4;
            order.fault = data.fault;
            order.last_update = DateTime.Now;
            order.model = data.model;
            order.remark = data.remark2;
            order.repair = data.repair;
            order.repair_date = DateChecking(data.repair_date);
            order.report = data.report;
            order.reviewer = data.reviewer;
            if ((String.IsNullOrEmpty(data.StartTimeHour)) && (String.IsNullOrEmpty(data.StartTimeMinute)))
                order.start_time = null;
            else
                order.start_time = new TimeSpan(Convert.ToInt32(data.StartTimeHour), Convert.ToInt32(data.StartTimeMinute), 0);
            order.status = (byte)data.status;
            order.tc = (byte)(data.tc ? 1 : 0);
            order.update_user_id = (int)member.infoBySession("id");
            order.job_number = data.JobNumber;
            order.order_status = data.order_status;
            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;
            customerData.reference_number = data.customer_reference_number;

            //alter 故障內容
            if (!string.IsNullOrEmpty(order.fault))
            {
                int index1 = order.fault.IndexOf("[");
                int index2 = order.fault.IndexOf("]");
                if (index1 > -1 && index2 > -1)
                    order.fault = order.fault.Substring(index2 + 1);
            }

            if (order.completion_date.HasValue)
            {
                order.order_status = OrderStatus.WarrantyCompleted.ID;
                order.fault = "[" + OrderStatus.WarrantyCompleted.Name + "] " + order.fault;
            }
            else if (order.order_status == OrderStatus.DoingWarranty.ID)
            {
                order.fault = "[" + OrderStatus.DoingWarranty.Name + "] " + order.fault;
                order.order_status = OrderStatus.DoingWarranty.ID;
            }
            else if (order.order_status == OrderStatus.AppointedWarranty.ID)
            {
                order.fault = "[" + OrderStatus.AppointedWarranty.Name + "] " + order.fault;
                order.order_status = OrderStatus.AppointedWarranty.ID;
            }

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

            return Json(data);
        }
Пример #2
0
        public ActionResult AddSave(OrderRecord data)
        {
            //# validation
            if (data.MasterRecordID < 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 (!String.IsNullOrEmpty(data.StartTimeMinute))
            {
                if (!Int32.TryParse(data.StartTimeMinute, out timeInt) || Convert.ToInt32(data.StartTimeMinute) < 0 || Convert.ToInt32(data.StartTimeMinute) > 59)
                {
                    data.SaveResult.WarningMessage = "請修改開始時間";
                    data.SaveResult.SavedSuccessfully = false;
                    return Json(data);
                }
            }
            if (!String.IsNullOrEmpty(data.EndTimeHour))
            {
                if (!Int32.TryParse(data.EndTimeHour, out timeInt) || Convert.ToInt32(data.EndTimeHour) < 0 || Convert.ToInt32(data.EndTimeHour) > 23)
                {
                    data.SaveResult.WarningMessage = "請修改結束時間";
                    data.SaveResult.SavedSuccessfully = false;
                    return Json(data);
                }
            }
            if (!String.IsNullOrEmpty(data.EndTimeMinute))
            {
                if (!Int32.TryParse(data.EndTimeMinute, out timeInt) || 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 == OrderMasterRecordType.quotation)
                return this.AddSave_Quotation(data);
            else
                return this.AddSave_Maintenance(data);
        }
Пример #3
0
        public ActionResult Add_FromQuotation(int quotationID)
        {
            OrderRecord model;

            //# 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();

            model = new OrderRecord()
            {
                //# customer data fields mapping
                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,
                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,
                customer_reference_number = record.masterCustomerData.reference_number,

                //# default
                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);
        }
Пример #4
0
        public ActionResult Edit(int id, OrderMasterRecordType masterRecordType)
        {
            OrderRecord 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()
                          where e.id == id
                          select
                          new
                          {
                              Order = 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
                          };

            var record = records.FirstOrDefault();

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

            model = new OrderRecord()
            {
                //# field mapping
                //customer_reference_number = record.CustomerData.reference_number,
                //get the newset number
                customer_reference_number = record.masterCustomer.reference_number,
                CompleteDate = record.Order.completion_date.HasValue ? record.Order.completion_date.Value.ToString("dd-MM-yyyy") : string.Empty,
                EndTimeHour = record.Order.end_time.HasValue ? Convert.ToString(record.Order.end_time.Value.Hours) : string.Empty,
                EndTimeMinute = record.Order.end_time.HasValue ? Convert.ToString(record.Order.end_time.Value.Minutes) : string.Empty,
                ActualStartTimeHour = record.Order.actual_start_time.HasValue ? Convert.ToString(record.Order.actual_start_time.Value.Hours) : string.Empty,///Peter
                ActualStartTimeMinute = record.Order.actual_start_time.HasValue ? Convert.ToString(record.Order.actual_start_time.Value.Minutes) : string.Empty,
                ActualEndTimeHour = record.Order.actual_end_time.HasValue ? Convert.ToString(record.Order.actual_end_time.Value.Hours) : string.Empty,///Peter
                ActualEndTimeMinute = record.Order.actual_end_time.HasValue ? Convert.ToString(record.Order.actual_end_time.Value.Minutes) : string.Empty,
                engineer_id_1 = record.Order.engineer_id_1,
                engineer_id_2 = record.Order.engineer_id_2,
                engineer_id_3 = record.Order.engineer_id_3,
                engineer_id_4 = record.Order.engineer_id_4,
                fault = record.Order.fault,
                model = record.Order.model,
                repair = record.Order.repair,
                repair_date = record.Order.repair_date.HasValue ? record.Order.repair_date.Value.ToString("dd-MM-yyyy") : string.Empty,
                report = record.Order.report,
                reviewer = record.Order.reviewer,
                StartTimeHour = record.Order.start_time.HasValue ? Convert.ToString(record.Order.start_time.Value.Hours) : string.Empty,
                StartTimeMinute = record.Order.start_time.HasValue ? Convert.ToString(record.Order.start_time.Value.Minutes) : string.Empty,
                tc = record.Order.tc.ToString() == "1",
                address1 = record.CustomerData.address1,
                address2 = record.CustomerData.address2,
                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.Order.id,
                last_update = record.Order.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.Order.remark,
                status = record.Order.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.Order.create_date,
                Technician1Name = record.Technician1Name,
                Technician2Name = record.Technician2Name,
                Technician3Name = record.Technician3Name,
                Technician4Name = record.Technician4Name,
                JobNumber = record.Order.job_number,
                order_status = (int)record.Order.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);
        }
Пример #5
0
        public ActionResult AddSave_Quotation(OrderRecord data)
        {
            Member member;
            recsys_relate_customers masterCustomerData;
            recsys_relate_customers customerData;
            recsys_order order;
            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,
                tel1 = data.tel1,
                tel2 = data.tel2,
                title = (byte)data.title,
                type = (byte)masterCustomerData.type
            };

            this._db.recsys_relate_customers.AddObject(customerData);

            TimeSpan? endTime = null;
            if (!(String.IsNullOrEmpty(data.EndTimeHour)) && !(String.IsNullOrEmpty(data.EndTimeMinute)))
                endTime = new TimeSpan(Convert.ToInt32(data.EndTimeHour), Convert.ToInt32(data.EndTimeMinute), 0);

            TimeSpan? startTime = null;
            if (!(String.IsNullOrEmpty(data.StartTimeHour)) && !(String.IsNullOrEmpty(data.StartTimeMinute)))
                startTime = new TimeSpan(Convert.ToInt32(data.StartTimeHour), Convert.ToInt32(data.StartTimeMinute), 0);

            order = new recsys_order()
            {
                by_system = 1,
                completion_date = DateChecking(data.CompleteDate),
                end_time = endTime,
                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 = startTime,
                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 = data.order_status
            };

            //alter 故障內容
            if (!string.IsNullOrEmpty(order.fault))
            {
                int index1 = order.fault.IndexOf("[");
                int index2 = order.fault.IndexOf("]");
                if (index1 > -1 && index2 > -1)
                    order.fault = order.fault.Substring(index2 + 1);
            }

            if (order.completion_date.HasValue)
            {
                order.order_status = OrderStatus.WarrantyCompleted.ID;
                order.fault = "[" + OrderStatus.WarrantyCompleted.Name + "] " + order.fault;
            }
            else if (order.order_status == OrderStatus.DoingWarranty.ID)
            {
                order.fault = "[" + OrderStatus.DoingWarranty.Name + "] " + order.fault;
                order.order_status = OrderStatus.DoingWarranty.ID;
            }
            else if (order.order_status == OrderStatus.AppointedWarranty.ID)
            {
                order.fault = "[" + OrderStatus.AppointedWarranty.Name + "] " + order.fault;
                order.order_status = OrderStatus.AppointedWarranty.ID;
            }

            this._db.recsys_order.AddObject(order);

            try
            {
                this._db.SaveChanges();

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

                order.customer_id = customerData.id;

                this._db.SaveChanges();

                data.id = order.id;

                relation = new recsys_relate()
                {
                    id1 = data.MasterRecordID,
                    id2 = order.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);
        }