Exemple #1
0
 /// <summary>
 /// Deprecated Method for adding a new object to the recsys_customers EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddTorecsys_customers(recsys_customers recsys_customers)
 {
     base.AddObject("recsys_customers", recsys_customers);
 }
        /// <summary>
        /// Business Logic Processing to customer before insert/ update to db
        /// </summary>
        /// <param name="customer"></param>
        private void CustomerBusinessLogicProcessing(ref recsys_customers customer, PageMode actionMode)
        {
            //logic: when type = "ACC" / "ACM", code is generated by system; therefore the code and customer_code field has to be update again
            //         : when type = "其他", code = prefix + manual input code
            if (customer.type == (int) CustomerType.ACC || customer.type == (int) CustomerType.ACM)       //when type = "ACC" / "ACM", code is generated by system; therefore the code and customer_code field has to be update again
            {
                if (actionMode == PageMode.Add)
                {
                    string customerType = customer.type == (int)CustomerType.ACC ? "ACC" : "ACM";
                    var customerSequenceItem = this._db.GetCustomerSequence(customerType).ToList().SingleOrDefault();
                    if (customerSequenceItem != null)
                    {
                        customer.code = customerSequenceItem.sequence_value;
                        customer.customer_code = this._type[customer.type.ToString()] + customer.code.Value.ToStringWithZeroLeftIndent(4);
                    }

                    //string customerType = customer.type == (int)CustomerType.ACC ? "ACC" : "ACM";

                    //int? maxCode = this._db.recsys_customers.Where(theCustomer =>
                    //(theCustomer.customer_code.StartsWith(customerType)) &&
                    //theCustomer.code.HasValue)
                    //.OrderByDescending(theCustomer => theCustomer.code.Value)
                    //.Select(theCustomer => theCustomer.code)
                    //.FirstOrDefault();      //# get current max system generated code

                    //if (!maxCode.HasValue)
                    //    maxCode = 0;
                    //customer.code = maxCode.Value + 1;
                    //customer.customer_code = this._type[customer.type.ToString()] + customer.code.Value.ToStringWithZeroLeftIndent(4);
                }

                customer.prefix = string.Empty;     //# prefix should be blank for type ACC and ACM
                customer.manual_input_code = (string)null;     //# manual input code should be blank for type ACC and ACM

            }
            else if (customer.type == (int) CustomerType.Others)        //when type = "其他", code = prefix + manual input code
                customer.customer_code = customer.prefix + customer.manual_input_code;
        }
Exemple #3
0
 /// <summary>
 /// Create a new recsys_customers object.
 /// </summary>
 /// <param name="id">Initial value of the id property.</param>
 /// <param name="type">Initial value of the type property.</param>
 /// <param name="name">Initial value of the name property.</param>
 /// <param name="chi_name">Initial value of the chi_name property.</param>
 /// <param name="title">Initial value of the title property.</param>
 /// <param name="status">Initial value of the status property.</param>
 /// <param name="create_date">Initial value of the create_date property.</param>
 public static recsys_customers Createrecsys_customers(global::System.Int32 id, global::System.Byte type, global::System.String name, global::System.String chi_name, global::System.Byte title, global::System.Byte status, global::System.DateTime create_date)
 {
     recsys_customers recsys_customers = new recsys_customers();
     recsys_customers.id = id;
     recsys_customers.type = type;
     recsys_customers.name = name;
     recsys_customers.chi_name = chi_name;
     recsys_customers.title = title;
     recsys_customers.status = status;
     recsys_customers.create_date = create_date;
     return recsys_customers;
 }
        public ActionResult AddSave(CustomerRecord model)
        {
            Member member = new Member("users");

            //check customer code duplicate
            if (model.type.HasValue)
            {
                if (model.type == (int)CustomerType.Others)
                {
                    string customer_code = model.prefix + model.manual_input_code;
                    var result = this._db.recsys_customers.Where(x => x.customer_code.ToUpper().Equals(customer_code.ToUpper())).SingleOrDefault();

                    if (result != null)
                    {
                        model.SaveResult.WarningMessage = "客戶編號重覆";
                        model.SaveResult.SavedSuccessfully = false;
                        return Json(model);
                    }
                }
            }

            if (!String.IsNullOrEmpty(model.name))
            {
                if (model.name.Length > 250)
                {
                    model.SaveResult.WarningMessage = "請修改客戶英文名稱至250字以內";
                    model.SaveResult.SavedSuccessfully = false;
                    return Json(model);
                }
            }
            else
            {
                model.SaveResult.WarningMessage = "請輸入客戶英文名稱";
                model.SaveResult.SavedSuccessfully = false;
                return Json(model);
            }

            if (!String.IsNullOrEmpty(model.chi_name))
            {
                if (model.chi_name.Length > 250)
                {
                    model.SaveResult.WarningMessage = "請修改客戶中文名稱至250字以內";
                    model.SaveResult.SavedSuccessfully = false;
                    return Json(model);
                }
            }
            else
            {
                model.SaveResult.WarningMessage = "請輸入客戶中文名稱";
                model.SaveResult.SavedSuccessfully = false;
                return Json(model);
            }

            if (String.IsNullOrEmpty(model.address1))
            {
                model.SaveResult.WarningMessage = "請輸入郵寄地址";
                model.SaveResult.SavedSuccessfully = false;
                return Json(model);
            }

            if (String.IsNullOrEmpty(model.MailingDistrictName))
            {
                model.SaveResult.WarningMessage = "請輸入郵寄地址地區";
                model.SaveResult.SavedSuccessfully = false;
                return Json(model);
            }

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

            if (createNewDistrict)
            {
                recsys_district addNewDistrict = new recsys_district()
                {
                    code = model.WorkingDistrictName.Trim(),
                    name = model.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();

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

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

            if (createNewDistrict)
            {
                recsys_district addNewDistrict = new recsys_district()
                {
                    code = model.MailingDistrictName.Trim(),
                    name = model.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();

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

            DateTime nowRef = DateTime.Now;
            //# mapping
            recsys_customers newCustomer = new recsys_customers()
            {
                address1 = model.address1 ?? "",
                address2 = model.address2 ?? "",
                code = model.code,
                contact = model.contact ?? "",
                create_date = nowRef,
                last_update = nowRef.AddMilliseconds(-nowRef.Millisecond),
                center = model.center,
                manual_input_code = model.manual_input_code ?? "",
                district1 = model.district1,
                district2 = model.district2,
                email = model.email ?? "",
                fax1 = model.fax1 ?? "",
                fax2 = model.fax2 ?? "",
                group_id = model.group_id,
                name = model.name ?? "",
                chi_name = model.chi_name ?? "",
                prefix = model.prefix ?? "",
                remark = model.remark ?? "",
                reference_number = model.reference_number ?? "",
                status = (byte)(model.status.HasValue ? model.status.Value : 1),
                tel1 = model.tel1 ?? "",
                tel2 = model.tel2 ?? "",
                title = (byte)(model.title.HasValue ? model.title.Value : 0),
                type = (byte)(model.type.HasValue ? model.type.Value : 0),
                update_user_id = (int) member.infoBySession("id")
            };

            #region Business Logic
            this.CustomerBusinessLogicProcessing(ref newCustomer, model.Mode);
            #endregion

            try
            {
                this._db.recsys_customers.AddObject(newCustomer);
                this._db.SaveChanges();
                this._db.Refresh(System.Data.Objects.RefreshMode.StoreWins, newCustomer);

                model.SaveResult.SavedSuccessfully = true;
                model.id = newCustomer.id;
                model.Mode = PageMode.Edit;         //# after inserted successfully, switch to edit mode
            }
            catch (OptimisticConcurrencyException)
            {
                //# log down
            }

            return Json(model);
        }