コード例 #1
0
        public ActionResult Edit(int?Id)
        {
            var Ward = vwWardService.Get(Id.Value);

            if (Ward != null && Ward.IsDeleted != true)
            {
                var model = new WardViewModel();
                AutoMapper.Mapper.Map(Ward, model);

                if (model.CreatedUserId != Helpers.Common.CurrentUser.Id && Helpers.Common.CurrentUser.UserTypeId != 1)
                {
                    TempData["FailedMessage"] = "NotOwner";
                    return(RedirectToAction("Index"));
                }

                return(View(model));
            }
            if (Request.UrlReferrer != null)
            {
                return(Redirect(Request.UrlReferrer.AbsoluteUri));
            }
            return(RedirectToAction("Index"));
        }
コード例 #2
0
        public ActionResult Import(FormCollection fc)
        {
            try
            {
                var data        = new DataTable();
                var tinh_tps    = ProvinceService.Get();
                var quan_huyens = DistrictService.Get();
                var phuong_xas  = WardService.Get();
                var carLine     = CarLineRepository.GetAll();
                if (Request.Files[0] != null && Request.Files[0].ContentLength > 0)
                {
                    byte[] fileData = null;
                    using (var binaryReader = new BinaryReader(Request.Files[0].InputStream))
                    {
                        fileData = binaryReader.ReadBytes(Request.Files[0].ContentLength);
                    }

                    MemoryStream ms = new MemoryStream(fileData);
                    data = ReadFromExcelfile(ms, null, "Danhsach");

                    data.Columns.Add("Ghi chú");
                    var col_HO_TEN_KH          = data.Columns.IndexOf("HO_TEN_KH");
                    var col_DIEN_THOAI         = data.Columns.IndexOf("DIEN_THOAI");
                    var col_EMAIL              = data.Columns.IndexOf("EMAIL");
                    var col_DIA_CHI            = data.Columns.IndexOf("SO_NHA_TEN_DUONG");
                    var col_TINH_THANH_PHO     = data.Columns.IndexOf("TINH_THANH_PHO");
                    var col_QUAN_HUYEN         = data.Columns.IndexOf("QUAN_HUYEN");
                    var col_PHUONG_XA_THI_TRAN = data.Columns.IndexOf("PHUONG_XA_THI_TRAN");
                    var col_NGAY_SINH          = data.Columns.IndexOf("NGAY_SINH");
                    var col_GIO_TINH           = data.Columns.IndexOf("GIO_TINH");
                    var col_NGHE_NGHIEP        = data.Columns.IndexOf("NGHE_NGHIEP");
                    var col_GHI_CHU            = data.Columns.IndexOf("GHI_CHU");
                    //var col_HANG_SAN_XUAT = data.Columns.IndexOf("HANG_SAN_XUAT");
                    //var col_DONG_XE = data.Columns.IndexOf("DONG_XE");
                    //var col_MAU_XE = data.Columns.IndexOf("MAU_XE");
                    //var col_SO_KHUNG = data.Columns.IndexOf("SO_KHUNG");
                    //var col_SO_MAY = data.Columns.IndexOf("SO_MAY");
                    //var col_BKS = data.Columns.IndexOf("BKS");

                    var noteColumn = data.Columns.IndexOf("Ghi chú");

                    //tao cờ kiểm tra dữ kiệu
                    bool flag = false;

                    //tao moi 1 danh sách
                    List <ImportViewModel> customerList = new List <ImportViewModel>();
                    foreach (System.Data.DataRow row in data.Rows)
                    {
                        string HO_TEN_KH  = col_HO_TEN_KH == -1 ? null : row.Field <string>(col_HO_TEN_KH);
                        string DIEN_THOAI = col_DIEN_THOAI == -1 ? null : row.Field <string>(col_DIEN_THOAI);
                        string EMAIL      = col_EMAIL == -1 ? null : row.Field <string>(col_EMAIL);
                        string DIA_CHI    = col_DIA_CHI == -1 ? null : row.Field <string>(col_DIA_CHI);
                        string TINH_TP    = col_TINH_THANH_PHO == -1 ? null : row.Field <string>(col_TINH_THANH_PHO);
                        string QUAN_HUYEN = col_QUAN_HUYEN == -1 ? null : row.Field <string>(col_QUAN_HUYEN);
                        string PHUONG_XA  = col_PHUONG_XA_THI_TRAN == -1 ? null : row.Field <string>(col_PHUONG_XA_THI_TRAN);
                        string NGAY_SINH  = col_NGAY_SINH == -1 ? null : row.Field <string>(col_NGAY_SINH);
                        string GHI_CHU    = col_GHI_CHU == -1 ? null : row.Field <string>(col_GHI_CHU);

                        string GIO_TINH    = col_GIO_TINH == -1 ? null : row.Field <string>(col_GIO_TINH);
                        string NGHE_NGHIEP = col_NGHE_NGHIEP == -1 ? null : row.Field <string>(col_NGHE_NGHIEP);
                        //string HANG_SAN_XUAT = col_HANG_SAN_XUAT == -1 ? null : row.Field<string>(col_HANG_SAN_XUAT);
                        //string DONG_XE = col_DONG_XE == -1 ? null : row.Field<string>(col_DONG_XE);
                        //string MAU_XE = col_MAU_XE == -1 ? null : row.Field<string>(col_MAU_XE);
                        //string SO_KHUNG = col_SO_KHUNG == -1 ? null : row.Field<string>(col_SO_KHUNG);
                        //string SO_MAY = col_SO_MAY == -1 ? null : row.Field<string>(col_SO_MAY);
                        //string BKS = col_SO_MAY == -1 ? null : row.Field<string>(col_BKS);

                        string note     = string.Empty;
                        var    customer = new Customer
                        {
                            IsDeleted      = false,
                            CreatedUserId  = WebSecurity.CurrentUserId,
                            ModifiedUserId = WebSecurity.CurrentUserId,
                            CreatedDate    = DateTime.Now,
                            ModifiedDate   = DateTime.Now,
                            Name           = HO_TEN_KH,
                            Address        = DIA_CHI,
                            Phone          = DIEN_THOAI,
                            Email          = EMAIL,
                            BranchId       = Helpers.Common.CurrentUser.BranchId,
                            Note           = GHI_CHU,
                            Occupations    = NGHE_NGHIEP,
                        };
                        var Cars = new qts.webapp.backend.domain.Models.Sale.Cars
                        {
                            IsDeleted      = false,
                            CreatedUserId  = WebSecurity.CurrentUserId,
                            ModifiedUserId = WebSecurity.CurrentUserId,
                            CreatedDate    = DateTime.Now,
                            ModifiedDate   = DateTime.Now,
                            //Color = MAU_XE,
                        };
                        #region Địa chỉ
                        if (!string.IsNullOrEmpty(TINH_TP))
                        {
                            var TINH_TP_UP = TINH_TP.ToUpper();
                            var tp         = tinh_tps.Where(n => n.FullName.Contains(TINH_TP_UP) || n.Name.Contains(TINH_TP_UP));
                            if (tp != null && tp.Count() > 0)
                            {
                                customer.CityId = tp.FirstOrDefault().ProvinceId;
                            }
                        }
                        if (!string.IsNullOrEmpty(QUAN_HUYEN))
                        {
                            var QUAN_HUYEN_UP = QUAN_HUYEN.ToUpper();
                            var db            = quan_huyens.Where(n => n.FullName.Contains(QUAN_HUYEN_UP) || n.Name.Contains(QUAN_HUYEN_UP));
                            if (db != null && db.Count() > 0)
                            {
                                customer.DistrictId = db.FirstOrDefault().DistrictId;
                            }
                        }
                        if (!string.IsNullOrEmpty(PHUONG_XA))
                        {
                            var PHUONG_XA_UP = PHUONG_XA.ToUpper();
                            var db           = phuong_xas.Where(n => n.FullName.Contains(PHUONG_XA_UP) || n.Name.Contains(PHUONG_XA_UP));
                            if (db != null && db.Count() > 0)
                            {
                                customer.WardId = db.FirstOrDefault().WardId;
                            }
                        }
                        #endregion

                        #region thông tin kh
                        if (!string.IsNullOrEmpty(GIO_TINH))
                        {
                            if (GIO_TINH == "Nam")
                            {
                                customer.Gender = false;
                            }
                            else if (GIO_TINH == "Nữ")
                            {
                                customer.Gender = true;
                            }
                            else
                            {
                            }
                        }
                        if (string.IsNullOrEmpty(HO_TEN_KH))
                        {
                            note += "Cột HO_TEN_KH trống";
                            flag  = true;
                        }
                        if (string.IsNullOrEmpty(DIEN_THOAI))
                        {
                            note += "Cột DIEN_THOAI trống";
                            flag  = true;
                        }

                        if (string.IsNullOrEmpty(NGAY_SINH))
                        {
                            note += "Cột NGAY_SINH trống";
                            flag  = true;
                        }
                        else
                        {
                            NGAY_SINH = string.IsNullOrEmpty(NGAY_SINH) ? "" : Regex.Replace(NGAY_SINH, "[^0-9]", "");
                            DateTime date;
                            if (DateTime.TryParseExact(NGAY_SINH, "ddMMyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
                            {
                                customer.Birthday = date;
                                row.SetField <string>(col_NGAY_SINH, NGAY_SINH);
                            }
                            else
                            {
                                note += NGAY_SINH + ", NGAY_SINH không đúng định dạng (ví dụ: 30/12/1999 => 30121999)";
                                flag  = true;
                            }
                        }
                        #endregion

                        #region Thông tin xe
                        //if (string.IsNullOrEmpty(HANG_SAN_XUAT))
                        //{
                        //    note += "Cột HANG_SAN_XUAT trống";
                        //    flag = true;
                        //}
                        //else
                        //{
                        //    Cars.Manufacturer = HANG_SAN_XUAT;
                        //}
                        //if (string.IsNullOrEmpty(DONG_XE))
                        //{
                        //    note += "Cột DONG_XE trống";
                        //    flag = true;
                        //}
                        //else
                        //{
                        //    var DONG_XE_UP = DONG_XE.ToUpper();
                        //    var db = carLine.Where(n => n.ManufacturerCar == HANG_SAN_XUAT && n.Name.ToUpper() == DONG_XE_UP).FirstOrDefault();
                        //    if (db != null)
                        //    {
                        //        Cars.CarLineId = db.Id;
                        //    }
                        //    else
                        //    {
                        //        note += "Dữ liệu DONG_XE không tồn tại";
                        //        flag = true;
                        //    }
                        //}
                        //if (!string.IsNullOrEmpty(SO_KHUNG))
                        //{
                        //    int n;
                        //    bool isNumeric = int.TryParse(SO_KHUNG, out n);
                        //    if (isNumeric)
                        //    {
                        //        Cars.Frames = n;
                        //    }
                        //    else
                        //    {
                        //        note += "Dữ liệu SO_KHUNG phải là số";
                        //        flag = true;
                        //    }
                        //}
                        //if (!string.IsNullOrEmpty(SO_MAY))
                        //{
                        //    int n;
                        //    bool isNumeric = int.TryParse(SO_MAY, out n);
                        //    if (isNumeric)
                        //    {
                        //        Cars.Number = n;
                        //    }
                        //    else
                        //    {
                        //        note += "Dữ liệu SO_MAY phải là số";
                        //        flag = true;
                        //    }
                        //}
                        //if (string.IsNullOrEmpty(BKS))
                        //{
                        //    note += "Cột BKS trống";
                        //    flag = true;
                        //}
                        //else
                        //{
                        //    Cars.Plate = BKS;
                        //}
                        #endregion

                        // thông tin xe
                        row.SetField <string>(noteColumn, note);
                        // or if above does not work
                        //myData = tableRow.Field<string>(data.Columns.IndexOf(tinhColumn));
                        ImportViewModel ImportViewModel = new ImportViewModel();
                        ImportViewModel.CustomerList = customer;
                        ImportViewModel.Cars         = Cars;
                        customerList.Add(ImportViewModel);
                    }
                    //flag = false;
                    ViewBag.save            = flag;
                    Session["ExcelData"]    = data;
                    Session["customerList"] = customerList;
                }
                else
                {
                    var    check        = "";
                    string prefix       = Helpers.Common.GetSetting("prefixOrderNo_Customer");
                    int    stt          = Convert.ToInt32(Helpers.Common.GetSetting("orderNo_Customer"));
                    var    BranchCode   = Erp.BackOffice.Helpers.Common.CurrentUser.BranchCode;
                    var    customerList = (List <ImportViewModel>)Session["customerList"];
                    foreach (var customer in customerList)
                    {
                        customer.CustomerList.Code           = string.Format("{0}{1}{2}", prefix, BranchCode, stt.ToString("D4"));
                        customer.CustomerList.Point          = 0;
                        customer.CustomerList.PaidPoint      = 0;
                        customer.CustomerList.RemainingPoint = 0;
                        CustomerRepository.InsertCustomer(customer.CustomerList);
                        stt++;
                        var cars = customer.Cars;
                        cars.CustomerId = customer.CustomerList.Id;
                        CarsService.Create(cars);
                    }
                    Helpers.Common.SetSetting("orderNo_Customer", stt.ToString());

                    Session["ExcelData"]                = null;
                    Session["customerList"]             = null;
                    TempData[Globals.SuccessMessageKey] = App_GlobalResources.Wording.InsertSuccess;
                    if (!string.IsNullOrEmpty(check))
                    {
                        TempData[Globals.FailedMessageKey] = "Lỗi ghi sổ: " + check;
                    }
                    return(RedirectToAction("Index"));
                }


                return(View(data));
            }
            catch (Exception ex)
            {
                TempData[Globals.FailedMessageKey] = App_GlobalResources.Wording.InsertFailed;
                return(RedirectToAction("Import"));
            }
        }