コード例 #1
0
        public AddTripViewModel()
        {
            TripSelected = new Trip(0);
            ResetView();
            // Danh sách các loại chi phí trong combo box
            AllCostTypes = DataAccess.GetCostsType();
            // Tất cả thành viên trong nhóm
            AllMembers = DataAccess.GetAllMembers();
            // Tất cả địa điểm có thể đi
            AllLocations = DataAccess.GetAllLocations();

            // Delete commands
            DeleteTripImageCommand = new RelayCommand <object>((p) => { return(p != null); }, (p) =>
            {
                TripImages selectedItem = (TripImages)p;
                foreach (TripImages element in AllTripImages)
                {
                    if (element.ImagePath == selectedItem.ImagePath)
                    {
                        AllTripImages.Remove(element);
                        break;
                    }
                }
            });
            DeleteTripCostCommand = new RelayCommand <object>((p) => { return(p != null); }, (p) =>
            {
                TripCost selectedItem = (TripCost)p;
                TripCosts.Remove(selectedItem);
            });
            DeleteTripLocationCommand = new RelayCommand <object>((p) => { return(p != null); }, (p) =>
            {
                Location selectedItem = (Location)p;
                TripLocations.Remove(selectedItem);
            });
            DeleteTripMemberCommand = new RelayCommand <object>((p) => { return(p != null); }, (p) =>
            {
                Member selectedItem = (Member)p;
                TripMembers.Remove(selectedItem);
            });

            // Add commands
            AddLocationCommand = new RelayCommand <object>((p) => { return(true); }, (p) =>
            {
                // Những địa điểm đã có trong DB
                var existingLocations = new HashSet <int>(from location in TripLocations select location.ID);
                // Kiểm tra thành viên được thêm vào có trong DB chưa
                bool isExisted = existingLocations.Any(locationID => locationID == LocationCBBSelected.ID);
                if (!isExisted)
                {
                    // Thêm vào thành viên mới lên UI
                    TripLocations.Add(new Location(LocationCBBSelected));
                    return;
                }
                CustomDialog.ShowDialog("Địa điểm đã có trong danh sách", CustomDialog.Buttons.OK);
            });
            AddMemberCommand = new RelayCommand <object>((p) => { return(true); }, (p) =>
            {
                if (int.TryParse(MemberCostAmountInput, out int amount))
                {
                    double doubleAmount = (double)amount;
                    // Những thành viên đã có trong DB
                    var existingMembers = new HashSet <int>(from member in TripMembers select member.MemberID);
                    // Kiểm tra thành viên được thêm vào có trong DB chưa
                    bool isExisted = existingMembers.Any(memberID => memberID == MemberCBBSelected.MemberID);
                    if (!isExisted)
                    {
                        // Thêm vào thành viên mới lên UI
                        TripMembers.Add(new Member(MemberCBBSelected)
                        {
                            AmountPaid = doubleAmount
                        });
                        return;
                    }
                    CustomDialog.ShowDialog("Thành viên đã có trong danh sách, thay đổi tiền trong bảng", CustomDialog.Buttons.OK);
                    return;
                }
                CustomDialog.ShowDialog("Nhập tiền không đúng định dạng", CustomDialog.Buttons.OK);
            });
            AddCostCommand = new RelayCommand <object>((p) => { return(true); }, (p) =>
            {
                if (int.TryParse(CostAmountInput, out int amount))
                {
                    double doubleAmount = (double)amount;
                    // Những chi phí đã có trong DB
                    var existingCosts = new HashSet <int>(from cost in TripCosts select cost.ID);
                    // Kiểm tra chi phí mới có trong DB chưa
                    bool isExisted = existingCosts.Any(costID => costID == CostCBBSelected.COST_ID);
                    if (!isExisted)
                    {
                        // Thêm vào chi phí mới lên UI
                        TripCosts.Add(new TripCost()
                        {
                            Name    = CostCBBSelected.NAME,
                            ID      = CostCBBSelected.COST_ID,
                            Trip_ID = TripSelected.ID,
                            Amount  = doubleAmount
                        });
                        return;
                    }
                    CustomDialog.ShowDialog("Chi phí đã có trong danh sách, thay đổi tiền trong bảng", CustomDialog.Buttons.OK);
                    return;
                }
                CustomDialog.ShowDialog("Nhập tiền không đúng định dạng", CustomDialog.Buttons.OK);
            });
            AddTripImageCommand = new RelayCommand <object>((p) => { return(true); }, (p) =>
            {
                OpenFileDialog open = new OpenFileDialog
                {
                    Multiselect = true,
                    Filter      = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg; *.png; *.jpeg; *.gif; *.bmp"
                };
                bool?result = open.ShowDialog();
                if (result == true)
                {
                    // Những hình đã có trong DB
                    var existingImgs = new HashSet <string>(from img in AllTripImages select img.ImagePath);
                    // Những hình đã chọn, lọc ra hình chưa có
                    var newItems = open.FileNames.Where(item => !existingImgs.Contains(item));
                    // Thêm vào nhũng hình chưa có
                    foreach (var item in newItems)
                    {
                        // Thay đổi UI
                        AllTripImages.Add(new TripImages()
                        {
                            Trip_ID   = TripSelected.ID,
                            ImagePath = item
                        });
                    }
                }
            });
            AddTripThumbnailCommand = new RelayCommand <object>((p) => { return(true); }, (p) =>
            {
                OpenFileDialog open = new OpenFileDialog
                {
                    Multiselect = false,
                    Filter      = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg; *.png; *.jpeg; *.gif; *.bmp"
                };
                bool?result = open.ShowDialog();
                if (result == true)
                {
                    // Thay đổi UI
                    var newThumbnailOpened   = open.FileName;
                    TripBinding.ThumnailPath = newThumbnailOpened;
                }
            });
            // Other commands
            AddNewTripCommand = new RelayCommand <object>((p) => { return(true); }, (p) =>
            {
                TripSelected = TripBinding.Clone();
                // Kiểm tra bỏ trống
                if (TripSelected.IsAnyFieldNull())
                {
                    CustomDialog.ShowDialog("Không thể thêm mới: có thông tin bỏ trống !", CustomDialog.Buttons.OK);
                    return;
                }
                // Kiểm tra ngày về có sớm hơn ngày đi hay không
                if (DateTime.Compare(TripSelected.EndDate, TripSelected.StartDate) < 0)
                {
                    CustomDialog.ShowDialog("Nhập sai ngày đi hoặc ngày về !", CustomDialog.Buttons.OK);
                    return;
                }
                // Kiểm tra tiền
                double totalMemberPaid = 0;
                double totalCosts      = 0;
                foreach (Member member in TripMembers)
                {
                    totalMemberPaid += member.AmountPaid;
                }
                foreach (TripCost cost in TripCosts)
                {
                    totalCosts += cost.Amount;
                }
                if (totalMemberPaid > totalCosts)
                {
                    CustomDialog.ShowDialog("Tiền các thành viên nhiều hơn chi phí chuyến đi, xin hãy nhập lại !", CustomDialog.Buttons.OK);
                    return;
                }

                // Copy hình thumbnail mới vào folder của chương trình và lưu record vào DB
                string newThumbnail       = Utils.StringHelper.CopyFile(TripBinding.ThumnailPath, TripSelected.ID, true);
                TripSelected.ThumnailPath = newThumbnail;

                int newID = DataAccess.AddNewTrip(TripSelected);
                if (newID < 0)
                {
                    CustomDialog.ShowDialog("Không thể thêm mới: lỗi nghiêm trọng !", CustomDialog.Buttons.OK);
                    return;
                }
                TripSelected.ID = newID;
                // Địa điểm
                DataAccess.UpdateAddRemoveTripLocations(TripSelected.ID, TripLocations.ToList());
                // Hình ảnh
                foreach (TripImages image in AllTripImages)
                {
                    image.Trip_ID = newID;
                    if (image.IsNew)
                    {
                        string newImage = Utils.StringHelper.CopyFile(image.ImagePath, TripSelected.ID, false);
                        image.ImagePath = newImage;
                        image.IsNew     = false;
                    }
                }
                DataAccess.UpdateAddRemoveTripImages(TripSelected.ID, AllTripImages.ToList());
                // Thành viên
                DataAccess.UpdateAddRemoveTripMembers(TripSelected.ID, TripMembers.ToList());
                // Chi phí
                foreach (TripCost tripCost in TripCosts)
                {
                    tripCost.Trip_ID = newID;
                }
                DataAccess.UpdateAddRemoveTripCosts(TripSelected.ID, TripCosts.ToList());

                CustomDialog.ShowDialog("Thêm mới thành công", CustomDialog.Buttons.OK);
            });
            DiscardChangesAndReload = new RelayCommand <object>((p) => { return(true); }, (p) =>
            {
                ResetView();
            });
        }
コード例 #2
0
        public UpdateTripViewModel(Trip trip)
        {
            AddLocationCommand = new RelayCommand <object>((p) => { return(true); }, (p) =>
            {
                // Những địa điểm đã có trong DB
                var existingLocations = new HashSet <int>(from location in TripLocations select location.ID);
                // Kiểm tra thành viên được thêm vào có trong DB chưa
                bool isExisted = existingLocations.Any(locationID => locationID == LocationCBBSelected.ID);
                if (!isExisted)
                {
                    // Thêm vào thành viên mới lên UI
                    TripLocations.Add(new Location(LocationCBBSelected));
                    return;
                }
                CustomDialog.ShowDialog("Địa điểm đã có !", CustomDialog.Buttons.OK);
            });
            AddMemberCommand = new RelayCommand <object>((p) => { return(true); }, (p) =>
            {
                if (int.TryParse(MemberCostAmountInput, out int amount))
                {
                    double doubleAmount = (double)amount;
                    // Những thành viên đã có trong DB
                    var existingMembers = new HashSet <int>(from member in TripMembers select member.MemberID);
                    // Kiểm tra thành viên được thêm vào có trong DB chưa
                    bool isExisted = existingMembers.Any(memberID => memberID == MemberCBBSelected.MemberID);
                    if (!isExisted)
                    {
                        // Thêm vào thành viên mới lên UI
                        TripMembers.Add(new Member(MemberCBBSelected)
                        {
                            AmountPaid = doubleAmount
                        });
                        return;
                    }
                    CustomDialog.ShowDialog("Thành viên đã có, thay đổi số tiền trên bảng", CustomDialog.Buttons.OK);
                }
            });
            AddCostCommand = new RelayCommand <object>((p) => { return(true); }, (p) =>
            {
                if (int.TryParse(CostAmountInput, out int amount))
                {
                    double doubleAmount = (double)amount;
                    // Những chi phí đã có trong DB
                    var existingCosts = new HashSet <int>(from cost in TripCosts select cost.ID);
                    // Kiểm tra chi phí mới có trong DB chưa
                    bool isExisted = existingCosts.Any(costID => costID == CostSelected.COST_ID);
                    if (!isExisted)
                    {
                        // Thêm vào chi phí mới lên UI
                        TripCosts.Add(new TripCost()
                        {
                            Name    = CostSelected.NAME,
                            ID      = CostSelected.COST_ID,
                            Trip_ID = TripSelected.ID,
                            Amount  = doubleAmount
                        });
                        return;
                    }
                    CustomDialog.ShowDialog("Chi phí đã có, thay đổi số tiền trên bảng", CustomDialog.Buttons.OK);
                }
            });
            AddTripImageCommand = new RelayCommand <object>((p) => { return(true); }, (p) =>
            {
                OpenFileDialog open = new OpenFileDialog
                {
                    Multiselect = true,
                    Filter      = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg; *.png; *.jpeg; *.gif; *.bmp"
                };
                bool?result = open.ShowDialog();
                if (result == true)
                {
                    // Những hình đã có trong DB
                    var existingImgs = new HashSet <string>(from img in AllTripImages select img.ImagePath);
                    // Những hình đã chọn, lọc ra hình chưa có
                    var newItems = open.FileNames.Where(item => !existingImgs.Contains(item));
                    // Thêm vào nhũng hình chưa có
                    foreach (var item in newItems)
                    {
                        // Thay đổi UI
                        AllTripImages.Add(new TripImages()
                        {
                            Trip_ID   = TripSelected.ID,
                            ImagePath = item
                        });
                    }
                }
            });
            ChooseTripThumbnailCommand = new RelayCommand <object>((p) => { return(true); }, (p) =>
            {
                OpenFileDialog open = new OpenFileDialog
                {
                    Multiselect = false,
                    Filter      = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg; *.png; *.jpeg; *.gif; *.bmp"
                };
                bool?result = open.ShowDialog();
                if (result == true)
                {
                    // Thay đổi UI
                    var newThumbnailOpened   = open.FileName;
                    TripBinding.ThumnailPath = newThumbnailOpened;
                }
            });
            SaveDetailsCommand = new RelayCommand <object>((p) => { return(true); }, (p) =>
            {
                bool isChanged = false;
                // Kiểm tra có bỏ trống trường nào không
                if (TripBinding.IsAnyFieldNull())
                {
                    CustomDialog.ShowDialog("Có thông tin bị bỏ trống, xin mời nhập lại !", CustomDialog.Buttons.OK);
                    return;
                }
                // Kiểm tra ngày về có sớm hơn ngày đi hay không
                if (DateTime.Compare(TripBinding.EndDate, TripBinding.StartDate) < 0)
                {
                    CustomDialog.ShowDialog("Nhập sai ngày đi hoặc ngày về !", CustomDialog.Buttons.OK);
                    return;
                }
                // Kiểm tra tiền
                double totalMemberPaid = 0;
                double totalCosts      = 0;
                foreach (Member member in TripMembers)
                {
                    totalMemberPaid += member.AmountPaid;
                }
                foreach (TripCost cost in TripCosts)
                {
                    totalCosts += cost.Amount;
                }
                if (totalMemberPaid > totalCosts)
                {
                    CustomDialog.ShowDialog("Tiền các thành viên nhiều hơn chi phí chuyến đi, xin hãy nhập lại !", CustomDialog.Buttons.OK);
                    return;
                }
                // Title
                if (TripBinding.Title != TripSelected.Title)
                {
                    TripSelected.Title = TripBinding.Title;
                    isChanged          = true;
                }
                // Description
                if (TripBinding.Description != TripSelected.Description)
                {
                    TripSelected.Description = TripBinding.Description;
                    isChanged = true;
                }
                // Copy hình thumbnail mới vào folder của chương trình và lưu record vào DB
                if (TripBinding.ThumnailPath != TripSelected.ThumnailPath)
                {
                    string newThumbnail       = Utils.StringHelper.CopyFile(TripBinding.ThumnailPath, TripSelected.ID, true);
                    TripSelected.ThumnailPath = newThumbnail;
                    isChanged = true;
                }
                // StartDate
                if (TripBinding.StartDate != TripSelected.StartDate)
                {
                    TripSelected.StartDate = TripBinding.StartDate;
                    isChanged = true;
                }
                // EndDate
                if (TripBinding.EndDate != TripSelected.EndDate)
                {
                    TripSelected.EndDate = TripBinding.EndDate;
                    isChanged            = true;
                }
                // Kiểm tra ngày về với ngày hiện tại, nếu sớm hơn thì đã đi xong rồi, ngược lại thì đang đi
                if (DateTime.Compare(TripSelected.EndDate, DateTime.Today) < 0)
                {
                    TripSelected.IsDone = true;
                    isChanged           = true;
                }
                else
                {
                    TripSelected.IsDone = false;
                }
                // Lưu dữ liệu vào DB
                if (isChanged)
                {
                    DataAccess.UpdateTripInfo(TripSelected);
                }
                // Địa điểm
                DataAccess.UpdateAddRemoveTripLocations(TripSelected.ID, TripLocations.ToList());
                // Hình ảnh
                foreach (TripImages image in AllTripImages)
                {
                    if (image.IsNew)
                    {
                        string newThumbnail = Utils.StringHelper.CopyFile(image.ImagePath, TripSelected.ID, false);
                        image.ImagePath     = newThumbnail;
                        image.IsNew         = false;
                    }
                }
                DataAccess.UpdateAddRemoveTripImages(TripSelected.ID, AllTripImages.ToList());
                // Thành viên
                DataAccess.UpdateAddRemoveTripMembers(TripSelected.ID, TripMembers.ToList());
                // Chi phí
                DataAccess.UpdateAddRemoveTripCosts(TripSelected.ID, TripCosts.ToList());

                CustomDialog.ShowDialog("Chỉnh sửa chuyến đi thành công", CustomDialog.Buttons.OK);
            });
            DiscardChangesAndReload = new RelayCommand <object>((p) => { return(true); }, (p) =>
            {
                Reset();
            });
            DeleteTripImageCommand = new RelayCommand <object>((p) => { return(p != null); }, (p) =>
            {
                TripImages selectedItem = (TripImages)p;
                foreach (TripImages element in AllTripImages)
                {
                    if (element.ImagePath == selectedItem.ImagePath)
                    {
                        AllTripImages.Remove(element);
                        break;
                    }
                }
            });
            DeleteTripCostCommand = new RelayCommand <object>((p) => { return(p != null); }, (p) =>
            {
                TripCost selectedItem = (TripCost)p;
                TripCosts.Remove(selectedItem);
            });
            DeleteTripLocationCommand = new RelayCommand <object>((p) => { return(p != null); }, (p) =>
            {
                Location selectedItem = (Location)p;
                TripLocations.Remove(selectedItem);
            });
            DeleteTripMemberCommand = new RelayCommand <object>((p) => { return(p != null); }, (p) =>
            {
                Member selectedItem = (Member)p;
                TripMembers.Remove(selectedItem);
            });
            // Get selected Trip
            TripSelected = trip;
            // Danh sách các loại chi phí trong combo box
            AllCostTypes = DataAccess.GetCostsType();
            // Tất cả thành viên trong nhóm
            AllMembers = DataAccess.GetAllMembers();
            // Tất cả địa điểm có thể đi
            AllLocations = DataAccess.GetAllLocations();
            // Populate dynamic data
            Reset();
        }