Exemple #1
0
        /// <summary>
        /// Deletes the selected row in Purchasing datagridView. Deleting the entity Purchase and BoxPurchase.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonDelete_Click(object sender, EventArgs e)
        {
            if ((dataGridViewPurchase.SelectedRows.Count <= 0))
            {
                MessageBox.Show("Please select the purchase to delete");
                return;
            }
            //selecting the row
            var selectedPurchase = dataGridViewPurchase.SelectedRows
                                   .OfType <DataGridViewRow>()
                                   .ToList();

            using (RosePurchaseManagementEntities context = new RosePurchaseManagementEntities())
            {
                var         pur           = (PurchaseBoxQuantity)selectedPurchase.Select(x => x).FirstOrDefault().DataBoundItem;
                var         selectedBoxId = context.Boxes.Where(b => b.BoxName == pur.BoxName).Select(i => i.BoxID).FirstOrDefault();
                Purchase    purchase      = context.Purchases.Where(p => p.PurchaseID == pur.PurchaseID).FirstOrDefault();
                BoxPurchase boxPurchase   = context.BoxPurchases.Where(b => (b.BoxID == selectedBoxId) && (b.PurchaseID == pur.PurchaseID)).FirstOrDefault();

                context.BoxPurchases.Remove(boxPurchase);
                context.SaveChanges();

                context.Purchases.Remove(purchase);
                context.SaveChanges();
            }
            //updates the datagridview after the deletions
            UpdatePurchase();
        }
Exemple #2
0
        /// <summary>
        /// Creates a new PUrchase entity and a new BoxPurchase entity. Updates the datagridView Purchase
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonPurchase_Click(object sender, EventArgs e)
        {
            Purchase    purchase    = new Purchase();
            BoxPurchase boxPurchase = new BoxPurchase();

            if ((listBoxWarehouse.SelectedItems.Count < 0))
            {
                MessageBox.Show("Warehouse to be selected");
                return;
            }
            if ((listBoxBox.SelectedItems.Count < 0))
            {
                MessageBox.Show("Box to be selected");
                return;
            }
            using (RosePurchaseManagementEntities context = new RosePurchaseManagementEntities())
            {
                //select the item from the listbox
                string selectedFarm    = textBoxFarmName.Text.Trim();
                int    selectedInvoice = int.Parse(textBoxInvoiceID.Text);

                //get the farmId for the selected farm
                var farmID = context.Farms.Where(f => f.FarmName == selectedFarm).FirstOrDefault();

                purchase.FarmID         = farmID.FarmID;
                purchase.RoseSizeID     = int.Parse(textBoxRoseSizeID.Text);
                purchase.Price_per_stem = float.Parse(textBoxPricePerStem.Text);
                purchase.InvoiceID      = int.Parse(textBoxInvoiceID.Text);
                purchase.WarehouseID    = listBoxWarehouse.SelectedIndex + 1;

                var purch = Controller <RosePurchaseManagementEntities, Purchase> .AddEntity(purchase);

                boxPurchase.PurchaseID = purch.PurchaseID;
                boxPurchase.BoxID      = listBoxBox.SelectedIndex + 1;
                boxPurchase.Quantity   = int.Parse(textBoxQuantity.Text);
                Controller <RosePurchaseManagementEntities, BoxPurchase> .AddEntity(boxPurchase);
            }
            UpdatePurchase();
            Clear();
        }
Exemple #3
0
        /// <summary>
        /// Updates the purchaseBoxQuantity object, by updating the Purchase entity and the BoxPurchase entity
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonUpdatePurchase_Click(object sender, EventArgs e)
        {
            //if no row is selected show a message
            if ((listBoxWarehouse.SelectedItems.Count < 0))
            {
                MessageBox.Show("Warehouse to be selected");
                return;
            }
            if ((listBoxBox.SelectedItems.Count < 0))
            {
                MessageBox.Show("Box to be selected");
                return;
            }

            //select the row in datagridview
            var selectedPurchase = dataGridViewPurchase.SelectedRows
                                   .OfType <DataGridViewRow>()
                                   .ToList();

            if (dataGridViewPurchase.SelectedRows.Count != 0)
            {
                var         pur         = (PurchaseBoxQuantity)selectedPurchase.Select(x => x).FirstOrDefault().DataBoundItem;
                Purchase    purchase    = new Purchase();
                BoxPurchase boxPurchase = new BoxPurchase();

                using (RosePurchaseManagementEntities context = new RosePurchaseManagementEntities())
                {
                    purchase    = context.Purchases.Where(x => x.PurchaseID == pur.PurchaseID).FirstOrDefault();
                    boxPurchase = context.BoxPurchases.Where(x => x.PurchaseID == pur.PurchaseID).FirstOrDefault();

                    //select the item from the listbox
                    string selectedFarm    = textBoxFarmName.Text.Trim();
                    int    selectedInvoice = int.Parse(textBoxInvoiceID.Text);

                    //get the farmId for the selected farm
                    purchase.RoseSizeID     = int.Parse(textBoxRoseSizeID.Text);
                    purchase.Price_per_stem = float.Parse(textBoxPricePerStem.Text);
                    purchase.InvoiceID      = int.Parse(textBoxInvoiceID.Text);
                    purchase.WarehouseID    = listBoxWarehouse.SelectedIndex + 1;
                    context.SaveChanges();

                    if (context.BoxPurchases.Where(x => (x.PurchaseID == purchase.PurchaseID) && (x.BoxID == listBoxBox.SelectedIndex + 1)).Count() > 0)
                    {
                        boxPurchase.PurchaseID = purchase.PurchaseID;
                        boxPurchase.BoxID      = listBoxBox.SelectedIndex + 1;
                        boxPurchase.Quantity   = int.Parse(textBoxQuantity.Text);
                        context.SaveChanges();
                    }
                    else
                    {
                        var boxSel = context.BoxPurchases.Where(x => (x.PurchaseID == purchase.PurchaseID) && (x.BoxID == boxId)).FirstOrDefault();
                        context.BoxPurchases.Remove(boxSel);
                        boxPurchase            = new BoxPurchase();
                        boxPurchase.PurchaseID = purchase.PurchaseID;
                        boxPurchase.BoxID      = listBoxBox.SelectedIndex + 1;
                        boxPurchase.Quantity   = int.Parse(textBoxQuantity.Text);
                        context.BoxPurchases.Add(boxPurchase);
                        context.SaveChanges();
                    }
                }
                UpdatePurchase();
                Clear();
            }
            else
            {
                MessageBox.Show("Please select the purchase");
            }
        }