コード例 #1
0
        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            var newProduct = new AddProductWindow();

            if (newProduct.ShowDialog() == true)
            {
                var product = new Product()
                {
                    ID         = IDGenerator.createID("P"),
                    Name       = newProduct.P_Name,
                    CategoryID = newProduct.P_CategoryID,
                    Price      = newProduct.P_Price,
                    Weight     = newProduct.P_Weight,
                    isDeleted  = false,
                    Picture    = newProduct.P_Picture,
                };

                MainWindow.db.Products.Add(product);
                MainWindow.db.SaveChanges();

                productDataGrid.ItemsSource = MainWindow.db.Products.ToList();
            }
        }
コード例 #2
0
        private void ImportButton_Click(object sender, RoutedEventArgs e)
        {
            var screen = new OpenFileDialog();

            screen.Filter = "Excel files|*.xls;*.xlsx";

            if (screen.ShowDialog() == true)
            {
                var workbook = new Workbook(screen.FileName);

                //Invoice sheet
                var sheet = workbook.Worksheets[0];

                var col = "A";
                var row = 2;

                var cell = sheet.Cells[$"{col}{row}"];

                while (cell.Value != null)
                {
                    var id    = sheet.Cells[$"A{row}"].StringValue;
                    var newid = IDGenerator.createID("I");

                    var customerid = sheet.Cells[$"B{row}"].StringValue;

                    bool exists = MainWindow.db.Customers.ToList().Any(cus => cus.ID == customerid);
                    if (exists != true)
                    {
                        string message = "The customer with ID \"" + customerid + "\" in invoice #" + id + " does not exist.\nDo you want to add a new customer? You can skip importing this invoice by clicking \"No\".";

                        if (MessageBox.Show(message, "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
                        {
                            var newCustomer = new AddCustomerWindow();

                            if (newCustomer.ShowDialog() == true)
                            {
                                if (!string.IsNullOrWhiteSpace(newCustomer.C_Name) && !string.IsNullOrWhiteSpace(newCustomer.C_Telephone) && !string.IsNullOrWhiteSpace(newCustomer.C_Address))
                                {
                                    var customer = new Customer()
                                    {
                                        ID        = customerid,
                                        Name      = newCustomer.C_Name,
                                        Telephone = newCustomer.C_Telephone,
                                        Address   = newCustomer.C_Address,
                                        isDeleted = false,
                                    };

                                    MainWindow.db.Customers.Add(customer);
                                    MainWindow.db.SaveChanges();
                                    CustomerControl.customer_notification.CustomerChange = true;

                                    var date   = sheet.Cells[$"C{row}"].DateTimeValue;
                                    var total  = sheet.Cells[$"D{row}"].DoubleValue;
                                    var status = sheet.Cells[$"E{row}"].StringValue;

                                    var newInvoice = new Invoice()
                                    {
                                        ID         = newid,
                                        CustomerID = customerid,
                                        Date       = date,
                                        Total      = total,
                                        Status     = status,
                                        isDeleted  = false,
                                    };

                                    MainWindow.db.Invoices.Add(newInvoice);
                                    MainWindow.db.SaveChanges();

                                    //Invoice detail sheet
                                    var sheetd = workbook.Worksheets[1];

                                    var cold = "A";
                                    var rowd = 2;

                                    var celld = sheetd.Cells[$"{cold}{rowd}"];

                                    while (celld.Value != null)
                                    {
                                        var invoiceid = sheetd.Cells[$"A{rowd}"].StringValue;

                                        if (invoiceid == id)
                                        {
                                            var  productid     = sheetd.Cells[$"B{rowd}"].StringValue;
                                            bool existsproduct = MainWindow.db.Products.ToList().Any(cus => cus.ID == productid);

                                            if (existsproduct != true)
                                            {
                                                string messaged = "The product with ID \"" + productid + "\" in invoice #" + id + " does not exist.\nDo you want to add a new product? You can skip importing this product by clicking \"No\".";

                                                if (MessageBox.Show(messaged, "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
                                                {
                                                    var newProduct = new AddProductWindow();

                                                    if (newProduct.ShowDialog() == true)
                                                    {
                                                        if (!string.IsNullOrWhiteSpace(newProduct.P_Name) && !string.IsNullOrWhiteSpace(newProduct.P_CategoryID) && newProduct.P_Price > 0 && newProduct.P_Weight > 0)
                                                        {
                                                            var product = new Product()
                                                            {
                                                                ID         = productid,
                                                                Name       = newProduct.P_Name,
                                                                CategoryID = newProduct.P_CategoryID,
                                                                Price      = newProduct.P_Price,
                                                                Weight     = newProduct.P_Weight,
                                                                isDeleted  = false,
                                                                Picture    = newProduct.P_Picture,
                                                            };

                                                            MainWindow.db.Products.Add(product);
                                                            MainWindow.db.SaveChanges();

                                                            //maybe notify changed here?

                                                            var weight    = sheetd.Cells[$"C{rowd}"].DoubleValue;
                                                            var unitprice = sheetd.Cells[$"D{rowd}"].DoubleValue;
                                                            var amount    = sheetd.Cells[$"E{rowd}"].DoubleValue;

                                                            var newInvoiceDetail = new InvoiceDetail()
                                                            {
                                                                InvoiceID = newid,
                                                                ProductID = productid,
                                                                Weight    = weight,
                                                                UnitPrice = unitprice,
                                                                Amount    = amount,
                                                                isDeleted = false,
                                                            };

                                                            MainWindow.db.InvoiceDetails.Add(newInvoiceDetail);
                                                            MainWindow.db.SaveChanges();
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                var weight    = sheetd.Cells[$"C{rowd}"].DoubleValue;
                                                var unitprice = sheetd.Cells[$"D{rowd}"].DoubleValue;
                                                var amount    = sheetd.Cells[$"E{rowd}"].DoubleValue;

                                                var newInvoiceDetail = new InvoiceDetail()
                                                {
                                                    InvoiceID = newid,
                                                    ProductID = productid,
                                                    Weight    = weight,
                                                    UnitPrice = unitprice,
                                                    Amount    = amount,
                                                    isDeleted = false,
                                                };

                                                MainWindow.db.InvoiceDetails.Add(newInvoiceDetail);
                                                MainWindow.db.SaveChanges();
                                            }
                                        }

                                        rowd++;
                                        celld = sheetd.Cells[$"{cold}{rowd}"];
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        var date   = sheet.Cells[$"C{row}"].DateTimeValue;
                        var total  = sheet.Cells[$"D{row}"].DoubleValue;
                        var status = sheet.Cells[$"E{row}"].StringValue;

                        var newInvoice = new Invoice()
                        {
                            ID         = newid,
                            CustomerID = customerid,
                            Date       = date,
                            Total      = total,
                            Status     = status,
                            isDeleted  = false,
                        };

                        MainWindow.db.Invoices.Add(newInvoice);
                        MainWindow.db.SaveChanges();

                        //Invoice detail sheet
                        var sheetd = workbook.Worksheets[1];

                        var cold = "A";
                        var rowd = 2;

                        var celld = sheetd.Cells[$"{cold}{rowd}"];

                        while (celld.Value != null)
                        {
                            var invoiceid = sheetd.Cells[$"A{rowd}"].StringValue;

                            if (invoiceid == id)
                            {
                                var  productid     = sheetd.Cells[$"B{rowd}"].StringValue;
                                bool existsproduct = MainWindow.db.Products.ToList().Any(cus => cus.ID == productid);

                                if (existsproduct != true)
                                {
                                    string messaged = "The product with ID \"" + productid + "\" in invoice #" + id + " does not exist.\nDo you want to add a new product? You can skip importing this product by clicking \"No\".";

                                    if (MessageBox.Show(messaged, "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
                                    {
                                        var newProduct = new AddProductWindow();

                                        if (newProduct.ShowDialog() == true)
                                        {
                                            if (!string.IsNullOrWhiteSpace(newProduct.P_Name) && !string.IsNullOrWhiteSpace(newProduct.P_CategoryID) && newProduct.P_Price > 0 && newProduct.P_Weight > 0)
                                            {
                                                var product = new Product()
                                                {
                                                    ID         = productid,
                                                    Name       = newProduct.P_Name,
                                                    CategoryID = newProduct.P_CategoryID,
                                                    Price      = newProduct.P_Price,
                                                    Weight     = newProduct.P_Weight,
                                                    isDeleted  = false,
                                                    Picture    = newProduct.P_Picture,
                                                };

                                                MainWindow.db.Products.Add(product);
                                                MainWindow.db.SaveChanges();

                                                //maybe notify changed here?

                                                var weight    = sheetd.Cells[$"C{rowd}"].DoubleValue;
                                                var unitprice = sheetd.Cells[$"D{rowd}"].DoubleValue;
                                                var amount    = sheetd.Cells[$"E{rowd}"].DoubleValue;

                                                var newInvoiceDetail = new InvoiceDetail()
                                                {
                                                    InvoiceID = newid,
                                                    ProductID = productid,
                                                    Weight    = weight,
                                                    UnitPrice = unitprice,
                                                    Amount    = amount,
                                                    isDeleted = false,
                                                };

                                                MainWindow.db.InvoiceDetails.Add(newInvoiceDetail);
                                                MainWindow.db.SaveChanges();
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    var weight    = sheetd.Cells[$"C{rowd}"].DoubleValue;
                                    var unitprice = sheetd.Cells[$"D{rowd}"].DoubleValue;
                                    var amount    = sheetd.Cells[$"E{rowd}"].DoubleValue;

                                    var newInvoiceDetail = new InvoiceDetail()
                                    {
                                        InvoiceID = newid,
                                        ProductID = productid,
                                        Weight    = weight,
                                        UnitPrice = unitprice,
                                        Amount    = amount,
                                        isDeleted = false,
                                    };

                                    MainWindow.db.InvoiceDetails.Add(newInvoiceDetail);
                                    MainWindow.db.SaveChanges();
                                }
                            }

                            rowd++;
                            celld = sheetd.Cells[$"{cold}{rowd}"];
                        }
                    }

                    row++;
                    cell = sheet.Cells[$"{col}{row}"];
                }

                MessageBox.Show("Import completed!", "Message");

                invoiceDataGrid.ItemsSource = MainWindow.db.Invoices.ToList();
            }
        }