Example #1
0
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string username = Request.Cookies["usernameLoginSystem"].Value;

            int id = ViewState["ID"].ToString().ToInt(0);

            if (id > 0)
            {
                var d = CustomerController.GetByID(id);
                if (d != null)
                {
                    //Phần thêm ảnh đại diện khách hàng
                    string path   = "/uploads/avatars/";
                    string Avatar = ListAvatarImage.Value;
                    if (UploadAvatarImage.UploadedFiles.Count > 0)
                    {
                        foreach (UploadedFile f in UploadAvatarImage.UploadedFiles)
                        {
                            var o = path + Guid.NewGuid() + f.GetExtension();
                            try
                            {
                                f.SaveAs(Server.MapPath(o));
                                Avatar = o;
                            }
                            catch { }
                        }
                    }

                    if (Avatar != ListAvatarImage.Value)
                    {
                        if (File.Exists(Server.MapPath(ListAvatarImage.Value)))
                        {
                            File.Delete(Server.MapPath(ListAvatarImage.Value));
                        }
                    }

                    int    PaymentType           = ddlPaymentType.SelectedValue.ToInt(0);
                    int    ShippingType          = ddlShippingType.SelectedValue.ToInt(0);
                    int    TransportCompanyID    = ddlTransportCompanyID.SelectedValue.ToInt(0);
                    int    TransportCompanySubID = ddlTransportCompanySubID.SelectedValue.ToInt(0);
                    string note       = txtNote.Text;
                    int    ProvinceID = hdfProvinceID.Value.ToInt(0);
                    int    DistrictID = hdfDistrictID.Value.ToInt(0);
                    int    WardID     = hdfWardID.Value.ToInt(0);

                    string warning = "Cập nhật khách hàng thành công";

                    string CustomerPhone    = d.CustomerPhone;
                    string NewCustomerPhone = Regex.Replace(txtCustomerPhone.Text.Trim(), @"[^\d]", "");
                    // kiểm tra số điện thoại mới
                    if (NewCustomerPhone != d.CustomerPhone)
                    {
                        // kiểm tra số điện thoại mới có khả dụng ko?
                        var c = CustomerController.GetByPhone(NewCustomerPhone);
                        if (c != null && c.ID != d.ID)
                        {
                            warning = "Số điện thoại này đã tồn tại của khách khác!";
                        }
                        else
                        {
                            warning = "Cập nhật khách hàng thành công! Số điện thoại khách hàng đã được đổi.<br>Lưu ý: Các đơn hàng cũ của khách này cũng đã được đổi số điện thoại.";
                            note    = "Số điện thoại cũ: " + d.CustomerPhone + ". " + note;

                            CustomerPhone = NewCustomerPhone;

                            // đổi số mới cho đơn hàng cũ
                            var orders = OrderController.GetByCustomerID(d.ID);
                            foreach (var order in orders)
                            {
                                string update = OrderController.UpdateCustomerPhone(order.ID, CustomerPhone);
                            }

                            // đổi số mới cho đơn hàng đổi trả cũ
                            var refundorders = RefundGoodController.GetByCustomerID(d.ID);
                            foreach (var refundorder in refundorders)
                            {
                                string update = RefundGoodController.UpdateCustomerPhone(refundorder.ID, CustomerPhone);
                            }
                        }
                    }

                    string CustomerPhone2    = "";
                    string NewCustomerPhone2 = Regex.Replace(txtCustomerPhone2.Text.Trim(), @"[^\d]", "");
                    if (NewCustomerPhone2 != "")
                    {
                        // kiểm tra số điện thoại 2 mới
                        var b = CustomerController.GetByPhone(NewCustomerPhone2);
                        if (b == null)
                        {
                            CustomerPhone2 = NewCustomerPhone2;
                        }
                        else
                        {
                            warning = "Số điện thoại 2 đã tồn tại của khách khác!";
                        }
                    }

                    CustomerController.Update(id, txtCustomerName.Text, CustomerPhone, txtAddress.Text, "", 0, 1, ddlUser.SelectedItem.ToString(), DateTime.Now, username, chkIsHidden.Checked, Regex.Replace(txtZalo.Text.Trim(), @"[^\d]", ""), txtFacebook.Text, note, txtNick.Text, Avatar, ShippingType, PaymentType, TransportCompanyID, TransportCompanySubID, CustomerPhone2, ProvinceID, DistrictID, WardID);
                    PJUtils.ShowMessageBoxSwAlert(warning, "s", true, Page);
                }
            }
        }