Exemplo n.º 1
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string uid = (sender as Button).Uid;
            positionError.Visibility = System.Windows.Visibility.Collapsed;
            SuccessMessgage.Visibility = System.Windows.Visibility.Collapsed;
            saveButton.Visibility = System.Windows.Visibility.Visible;

            if (dispathTimer != null) {
                dispathTimer.Stop();
                dispathTimer = null;
            }

            if (uid == "addNew") {
                dgPosition.SelectedItem = null;
                toEditPosition = null;
            }

            if (uid == "rowEdit") {
                pawnguardDb = new PawnGuardDBDataContext();
                toEditPosition = pawnguardDb.Positions
                               .SingleOrDefault<Position>(pos => pos.id.ToString() == (sender as Button).Content.ToString());

                dgPosition.SelectedItem = toEditPosition;
                VisualStateManager.GoToElementState(this.LayoutRoot, "ShowState", true);
            }

            if (uid == "closePanel") {
                VisualStateManager.GoToElementState(this.LayoutRoot, "HideState", true);
                Page_Loaded(sender, e);
            }

            if (uid == "deleteAll") {
                if (toDeletePosition.Count > 0) {
                    pawnguardDb = new PawnGuardDBDataContext();
                    List<Position> deletePosition = new List<Position>();
                    for (int i = 0; i < toDeletePosition.Count; i++) {
                        var _pos = pawnguardDb.Positions
                                   .FirstOrDefault(pos => pos.id == toDeletePosition[i].id);

                        deletePosition.Add(_pos);
                    }

                    if (deletePosition.Count > 0) {
                        pawnguardDb.Positions.DeleteAllOnSubmit(deletePosition);
                        pawnguardDb.SubmitChanges();
                        Page_Loaded(sender, e);
                        isCheckAll.IsChecked = false;
                        toDeletePosition.Clear();
                    }
                }
            }

            if (uid == "save") {
                if (toEditPosition == null) {
                    if (string.IsNullOrWhiteSpace(txtPositionName.Text)) {
                        positionError.Visibility = System.Windows.Visibility.Visible;
                        errorMsg.Text = MSG_REQUIRED;
                        return;
                    }

                    pawnguardDb = new PawnGuardDBDataContext();
                    bool exist = pawnguardDb.Positions.AsEnumerable()
                                 .Where(pos => pos.name.ToLower() == txtPositionName.Text.ToLower())
                                 .Any();

                    if (exist) {
                        positionError.Visibility = System.Windows.Visibility.Visible;
                        errorMsg.Text = MSG_EXIST;
                        return;
                    }

                    pawnguardDb.Positions.InsertOnSubmit(new Position() {
                        name = txtPositionName.Text,
                        createdAt = DateTime.Now,
                        updatedAt = DateTime.Now
                    });
                } else {
                    string editName = toEditPosition.name;
                    if (!editName.Equals(txtPositionName.Text, StringComparison.InvariantCultureIgnoreCase)) {
                        bool exist = pawnguardDb.Positions.AsEnumerable()
                                 .Where(pos => pos.name.ToLower() == txtPositionName.Text.ToLower())
                                 .Any();

                        if (exist) {
                            positionError.Visibility = System.Windows.Visibility.Visible;
                            errorMsg.Text = MSG_EXIST;
                            return;
                        }
                    } else {
                        VisualStateManager.GoToElementState(this.LayoutRoot, "HideState", true);
                        return;
                    }

                    toEditPosition.name = txtPositionName.Text;
                    toEditPosition.updatedAt = DateTime.Now;
                }

                try {
                    pawnguardDb.SubmitChanges();
                    saveButton.Visibility = System.Windows.Visibility.Collapsed;

                    dispathTimer = new DispatcherTimer();
                    dispathTimer.Interval = new TimeSpan(0, 0, 1);
                    dispathTimer.Start();

                    int time = 3;
                    SuccessMessgage.Visibility = System.Windows.Visibility.Visible;
                    dispathTimer.Tick += delegate(object s, EventArgs ea) {
                        if (time >= 0)
                            runTime.Text = time + " sec.";
                        else {
                            dispathTimer.Stop();
                            SuccessMessgage.Visibility = System.Windows.Visibility.Collapsed;
                            VisualStateManager.GoToElementState(this.LayoutRoot, "HideState", true);
                            runTime.Text = "3 sec.";
                            txtPositionName.Clear();
                            Page_Loaded(sender, e);
                        }
                        time--;
                    };
                } catch (Exception) {
                }
            }
        }
Exemplo n.º 2
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string uid = (sender as Button).Uid;
            SuccessMessgage.Visibility = System.Windows.Visibility.Collapsed;
            saveButton.Visibility = System.Windows.Visibility.Visible;

            if (dispathTimer != null) {
                dispathTimer.Stop();
                dispathTimer = null;
            }

            if (uid == "addNew") {
                dgItems.SelectedItem = null;
                toEditItem = null;
            }

            if (uid == "rowEdit") {
                pawnguardDb = new PawnGuardDBDataContext();
                toEditItem = pawnguardDb.ItemCategories
                             .SingleOrDefault<ItemCategory>(pos => pos.id.ToString() == (sender as Button).Content.ToString());

                dgItems.SelectedItem = toEditItem;
                VisualStateManager.GoToElementState(this.LayoutRoot, "ShowState", true);
            }

            if (uid == "closePanel") {
                VisualStateManager.GoToElementState(this.LayoutRoot, "HideState", true);
                FillGrid();
                page_Loaded(sender, e);
            }

            if (uid == "deleteAll") {
                if (toDeleteItem.Count > 0) {
                    pawnguardDb = new PawnGuardDBDataContext();
                    List<ItemCategory> deleteUser = new List<ItemCategory>();
                    for (int i = 0; i < toDeleteItem.Count; i++) {
                        var itemCategory = pawnguardDb.ItemCategories
                                   .FirstOrDefault(usr => usr.id == toDeleteItem[i].id);

                        deleteUser.Add(itemCategory);
                    }

                    if (deleteUser.Count > 0) {
                        pawnguardDb.ItemCategories.DeleteAllOnSubmit(deleteUser);
                        pawnguardDb.SubmitChanges();
                        isCheckAll.IsChecked = false;
                        toDeleteItem.Clear();

                        FillGrid();
                        page_Loaded(sender, e);
                    }
                }
            }

            if (uid == "save") {
                if (toEditItem == null) {

                    if (!IsValid())
                        return;

                    pawnguardDb = new PawnGuardDBDataContext();
                    bool exist = pawnguardDb.ItemCategories.AsEnumerable()
                                 .Where(itm => itm.main.ToLower() == txtCategory.Text.ToLower() ||
                                               itm.option1.ToLower() == txtOption1.Text.ToLower() ||
                                               itm.option2.ToLower() == txtOption2.Text.ToLower())
                                 .Any();

                    if (exist) {
                        categoryError.Visibility = System.Windows.Visibility.Visible;
                        errorMsgCategory.Text = MSG_EXIST;
                        return;
                    }

                    decimal price = 0;
                    decimal.TryParse(txtPrice.Text, out price);
                    pawnguardDb.ItemCategories.InsertOnSubmit(new ItemCategory() {
                        parentId = AppData.parentId,
                        code = txtCode.Text,
                        main = txtCategory.Text,
                        option1 = txtOption1.Text,
                        option2 = txtOption2.Text,
                        price = price,
                        createdAt = DateTime.Now,
                        updatedAt = DateTime.Now
                    });
                } else {
                    if (!IsValid())
                        return;

                    string editEmail = toEditItem.main;
                    string editoption1 = toEditItem.option1;
                    string editoption2 = toEditItem.option2;
                    if (!editEmail.Equals(txtCategory.Text, StringComparison.InvariantCultureIgnoreCase) &&
                        !editoption1.Equals(txtOption1.Text, StringComparison.InvariantCultureIgnoreCase) &&
                        !editoption2.Equals(txtOption2.Text, StringComparison.InvariantCultureIgnoreCase)) {

                        bool exist = pawnguardDb.ItemCategories.AsEnumerable()
                                .Where(itm => itm.main.ToLower() == txtCategory.Text.ToLower() ||
                                                itm.option1.ToLower() == txtOption1.Text.ToLower() ||
                                                itm.option2.ToLower() == txtOption2.Text.ToLower())
                                .Any();

                        if (exist) {
                            categoryError.Visibility = System.Windows.Visibility.Visible;
                            errorMsgCategory.Text = MSG_EXIST;
                            return;
                        }
                    }

                    decimal price = 0;
                    decimal.TryParse(txtPrice.Text, out price);
                    toEditItem.parentId = AppData.parentId;
                    toEditItem.code = txtCode.Text;
                    toEditItem.main = txtCategory.Text;
                    toEditItem.option1 = txtOption1.Text;
                    toEditItem.option2 = txtOption2.Text;
                    toEditItem.price = price;
                    toEditItem.updatedAt = DateTime.Now;
                }

                try {
                    pawnguardDb.SubmitChanges();
                    saveButton.Visibility = System.Windows.Visibility.Collapsed;

                    dispathTimer = new DispatcherTimer();
                    dispathTimer.Interval = new TimeSpan(0, 0, 1);
                    dispathTimer.Start();

                    int time = 3;
                    SuccessMessgage.Visibility = System.Windows.Visibility.Visible;
                    dispathTimer.Tick += delegate(object s, EventArgs ea) {
                        if (time >= 0)
                            runTime.Text = time + " sec.";
                        else {
                            dispathTimer.Stop();
                            SuccessMessgage.Visibility = System.Windows.Visibility.Collapsed;
                            VisualStateManager.GoToElementState(this.LayoutRoot, "HideState", true);
                            runTime.Text = "3 sec.";

                            FillGrid();
                            page_Loaded(sender, e);
                        }
                        time--;
                    };
                } catch (Exception) {
                }
            }
        }
Exemplo n.º 3
0
        private void ImportExcel(string path)
        {
            object rarity = null;
            object name = null;
            object carat = null;
            object price = null;
            object misValue = System.Reflection.Missing.Value;

            excelApp = new Excel.Application();
            Excel.Range range;
            worker = new BackgroundWorker();
            worker.WorkerSupportsCancellation = true;
            worker.WorkerReportsProgress = true;

            ProgStackPanel.Visibility = System.Windows.Visibility.Visible;
            ProgTextCancel.Visibility = System.Windows.Visibility.Collapsed;
            ProgressGrid.Visibility = System.Windows.Visibility.Visible;
            StoneListGrid.Visibility = System.Windows.Visibility.Hidden;
            Import.IsEnabled = false;
            Export.IsEnabled = false;
            this.MinHeight = 500;
            ProgText.Text = "Importing Data...";
            ProgressStone.Value = 0;

            worker.DoWork += delegate(object sdr, DoWorkEventArgs dw) {
                Excel.Application _excelApp = (Excel.Application)dw.Argument;
                xlWorkBook = _excelApp.Workbooks.Open(path, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

                range = xlWorkSheet.UsedRange;
                List<Stone> stoneItem = new List<Stone>();
                for (int row = 0; row <= range.Rows.Count; row++) {
                    for (int col = 0; col <= range.Columns.Count; col++) {
                        if ((range.Cells[row + 2, col + 1] as Excel.Range).Value2 != null) {
                            if (col == 0) rarity = (range.Cells[row + 2, col + 1] as Excel.Range).Value2;
                            if (col == 1) name = (range.Cells[row + 2, col + 1] as Excel.Range).Value2;
                            if (col == 2) carat = (range.Cells[row + 2, col + 1] as Excel.Range).Value2;
                            if (col == 3) price = (range.Cells[row + 2, col + 1] as Excel.Range).Value2;

                        } else {
                            if (col == 0) rarity = string.Empty;
                            if (col == 1) name = string.Empty;
                            if (col == 2) carat = string.Empty;
                            if (col == 3) price = string.Empty;
                        }
                    }

                    if (worker.CancellationPending) {
                        ProgText.Dispatcher.Invoke(new Action(() => { ProgText.Text = "Canceling..."; }));
                        System.Threading.Thread.Sleep(3000);
                        dw.Cancel = true;
                        return;
                    }

                    try {
                        if (!string.IsNullOrWhiteSpace(name.ToString())) {
                            stoneItem.Add(new Stone {
                                Id = row + 1,
                                name = name.ToString(),
                                rarity = rarity.ToString(),
                                carat = carat.ToString(),
                                price = price.ToString(),
                            });
                        }
                    } catch (Exception) {
                        ProgText.Dispatcher.Invoke(new Action(() => { ProgText.Text = "Invalid Template!"; }));
                        System.Threading.Thread.Sleep(1300);
                        worker.CancelAsync();
                    }

                    worker.ReportProgress(Convert.ToInt32(((decimal)row / (decimal)range.Rows.Count) * 100));
                    System.Threading.Thread.Sleep(5);
                    dw.Result = stoneItem;
                }
            };

            worker.ProgressChanged += delegate(object s, ProgressChangedEventArgs args) {
                ProgressStone.Value = args.ProgressPercentage;
            };

            worker.RunWorkerCompleted += delegate(object sdr, RunWorkerCompletedEventArgs rwc) {
                xlWorkBook.Close(true, misValue, misValue);
                excelApp.Quit();

                releaseObject(xlWorkSheet);
                releaseObject(xlWorkBook);
                releaseObject(excelApp);
                ProgressGrid.Visibility = System.Windows.Visibility.Hidden;
                StoneListGrid.Visibility = System.Windows.Visibility.Visible;
                Import.IsEnabled = true;
                Export.IsEnabled = true;
                this.MinHeight = 300;

                if (rwc.Error != null) {
                    MessageBox.Show(rwc.Error.Message);
                    return;
                }

                if (!rwc.Cancelled) {
                    List<Stone> stone = (List<Stone>)rwc.Result;
                    for (int i = 0; i < stone.Count; i++) {
                        stone[i].parentId = AppData.parentId;
                        stone[i].updatedAt = DateTime.Now;
                        stone[i].createdAt = DateTime.Now;
                    }

                    pawnguardDb = new PawnGuardDBDataContext();
                    LinqExt.Truncate(pawnguardDb.Stones);
                    pawnguardDb.Stones.InsertAllOnSubmit(stone);
                    pawnguardDb.SubmitChanges();

                    FillGrid();
                    page_Loaded(null, new RoutedEventArgs());
                }
            };

            worker.RunWorkerAsync(excelApp);
        }
Exemplo n.º 4
0
        private void SaveSetUp()
        {
            using (PawnGuardDBDataContext db = new PawnGuardDBDataContext()) {
                LinqExt.Truncate(db.Branches);
                LinqExt.Truncate(db.AppSettings);
                LinqExt.Truncate(db.Moneys);
                LinqExt.Truncate(db.Currencies);
                LinqExt.Truncate(db.ItemCategories);
                LinqExt.Truncate(db.Golds);
                LinqExt.Truncate(db.Stones);
                LinqExt.Truncate(db.InitialSetups);

                db.Branches.InsertOnSubmit(Branch);
                db.AppSettings.InsertOnSubmit(BranchPrimaryCurreny);
                db.Moneys.InsertAllOnSubmit(BranchMoney);
                db.Currencies.InsertAllOnSubmit(BranchCurrency);
                db.ItemCategories.InsertAllOnSubmit(ItemCategories);
                db.Golds.InsertAllOnSubmit(GoldList);
                db.Stones.InsertAllOnSubmit(StoneList);

                InitialSetup inSetup = new InitialSetup() {
                    parentId = "000001",
                    step = "SetState",
                    createdAt = DateTime.Now,
                    updateAt = DateTime.Now
                };

                db.InitialSetups.InsertOnSubmit(inSetup);
                db.SubmitChanges();

                ProgressBarFinal.Dispatcher.Invoke(new Action(() => {
                    ProgressBarFinal.Visibility = Visibility.Hidden;
                }));
                ButtonStartApp.Dispatcher.Invoke(new Action(() => {
                    ButtonStartApp.Visibility = Visibility.Visible;
                }));
            }
        }