// this section is use to update stock in hand item amount using shipment ID and total number of left
        public bool updateStockInHandAmountOfItems(String shipmentID,int noOfPiecesLeft)
        {
            try
            {
                using (adoraDBContext a = new adoraDBContext())
                {

                    var stockInHandObj = a.StockInHands.FirstOrDefault((sh) => sh.StockID == shipmentID);

                    stockInHandObj.NoOfItems = noOfPiecesLeft;

                    
                    int done = a.SaveChanges();

                    
                    if (done != -1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (System.Data.Entity.Infrastructure.DbUpdateException e)
            {
                MessageBox.Show(e.InnerException.ToString());
                return false;
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException)
            {
                return false;
            }
            catch (System.NotSupportedException)
            {
                return false;
            }
            catch (System.ObjectDisposedException)
            {
                return false;
            }
            catch (System.InvalidOperationException)
            {
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }
        // This section is use to update update frequency
        public bool updateFrequency(String shipmentID, int frequencyID, int noOfPieces, decimal pricePerPiece, DateTime date)
        {
            try
            {
                using (adoraDBContext a = new adoraDBContext())
                {
                    var frequency = a.FOBSalesFrequencies.FirstOrDefault((sf) => sf.ShipmentID == shipmentID && sf.FrequencyID == frequencyID);

                    frequency.NoOfPieces = noOfPieces;
                    frequency.PricePerPiece = pricePerPiece;
                    frequency.Date = date;

                    // _context.BnSFrequencies.Load();
                    int done = a.SaveChanges();

                    // If the operation is succeded
                    if (done != -1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (System.Data.Entity.Infrastructure.DbUpdateException e)
            {
                MessageBox.Show(e.InnerException.ToString());
                return false;
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException)
            {
                return false;
            }
            catch (System.NotSupportedException)
            {
                return false;
            }
            catch (System.ObjectDisposedException)
            {
                return false;
            }
            catch (System.InvalidOperationException)
            {
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }
        //This section is use to delete the current frequency
        public bool deleteCurrentFrequency(String shipmentId, int freqID)
        {
            try
            {
                using (adoraDBContext a = new adoraDBContext())
                {
                    // getting the deleting frequency object according to the shipment ID and frequncy ID
                    var deletingFrequencyObj = a.FOBSalesFrequencies.FirstOrDefault((sf) => sf.ShipmentID == shipmentId && sf.FrequencyID == freqID);

                    // deleting the selected object
                    a.FOBSalesFrequencies.Remove(deletingFrequencyObj);

                    // saving the changes to the database
                    int done = a.SaveChanges();

                    // If the operation is succeded retuen true, Otherwise return false
                    if (done != -1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (ArgumentNullException e)
            {
                MessageBox.Show(e.ToString());
                return false;
            }
            catch(Exception)
            {
                return false;
            }
        }
        // this section is use to add New Frequency 
        public bool addNewFrequency(String shipmentID, int frequencyID, int noOfPieces, decimal pricePerPiece, DateTime date)
        {
            try
            {
                using (adoraDBContext a = new adoraDBContext())
                {
                    var frequency = new FOBSalesFrequency();

                    frequency.FrequencyID = frequencyID;
                    frequency.ShipmentID = shipmentID;
                    frequency.NoOfPieces = noOfPieces;
                    frequency.PricePerPiece = pricePerPiece;
                    frequency.Date = date;

                    //Add to memory
                    a.FOBSalesFrequencies.Add(frequency);

                    //Save to database
                    int done = a.SaveChanges();

                    // If the operation is sucessfull
                    if (done != -1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (System.Data.Entity.Infrastructure.DbUpdateException e)
            {
                MessageBox.Show(e.InnerException.ToString());
                return false;
            }
            catch(System.Data.Entity.Validation.DbEntityValidationException)
            {
                return false;
            }
            catch(System.NotSupportedException)
            {
                return false;
            }
            catch(System.ObjectDisposedException)
            {
                return false;
            }
            catch(System.InvalidOperationException)
            {
                return false;
            }
            catch(Exception)
            {
                return false;
            }
        }
        // this section is use to add and update records
           public void addUpdateRecord(RadioButton updateRButtonX, RadioButton addRButtonX, TextBox factoryNameX, TextBox itemNameX, DatePicker datePick, TextBox descriptionX, TextBox noOfPiecesX, Label errX)
           {

                    // This section is use to update records     
                   if (updateRButtonX.IsChecked == true)
                   {
                       if (validationX.allFieldsFilled(factoryNameX, itemNameX, datePick, descriptionX, noOfPiecesX))
                       {
                           // retriving the corresponding stock object that matches current record stock ID
                           var Bns = con.StockInHands.FirstOrDefault((bns) => bns.StockID == currentRecordStockID);

                           // updating the retrived object data using the new data
                           Bns.FactoryName = factoryNameX.Text;
                           Bns.ItemName = itemNameX.Text;
                           Bns.Date = datePick.SelectedDate;
                           Bns.descript = descriptionX.Text;
                           Bns.NoOfItems = Convert.ToInt32(noOfPiecesX.Text);
                           con.StockInHands.Load();

                           // saving changes to the database
                           int done = con.SaveChanges();

                           if (done > 0)
                           {
                               MessageBox.Show("Update sucessfully!","Notification");
                               validationX.resetForm(factoryNameX, itemNameX, datePick, descriptionX, noOfPiecesX, errX);
                           }
                           else
                           {
                               MessageBox.Show("You have not modified any value!","Notification");
                           }
                       }
                       else
                       {
                           MessageBox.Show("Please Fill All Fields", "Error");
                       }
                   }
              
               // This method is used to add New record to the database
               if (addRButtonX.IsChecked == true)
               {
                   // validating whether al fields are filled correctly
                   if (validationX.allFieldsFilled(factoryNameX, itemNameX, datePick, descriptionX, noOfPiecesX))
                   {
                       adoraDBContext _context = new adoraDBContext();
                       

                    StockInHand sh = new StockInHand();

                    // getting data from the textboxes and saving them in newly created object
                    sh.FactoryName = factoryNameX.Text;
                    sh.ItemName = itemNameX.Text;
                    sh.Date = datePick.SelectedDate;
                    sh.descript = descriptionX.Text;
                    sh.NoOfItems = Convert.ToInt32((noOfPiecesX.Text));
                   
                   // sh.ACostID = id;
                    
                    // adding the object to the database
                    _context.StockInHands.Add(sh);

                    // saving the object in the database
                    _context.SaveChanges();

                    System.Windows.MessageBox.Show("Succesfully Added", "Done", MessageBoxButton.OK, MessageBoxImage.Information);

                    // this section is used to set CurrentRecordStockID global variable to newly added record's stock id
                    using (adoraDBContext b = new adoraDBContext())
                    {
                        // retriving newly added record's stock id from the database
                        var newRecordStockId = (from x in b.StockInHands
                                                where (x.FactoryName == factoryNameX.Text && x.ItemName == itemNameX.Text && x.descript == descriptionX.Text)
                                                select x.StockID).ToList();

                        // setting the CurrentRecordStockID global variable
                        setCurrentRecordStockID(newRecordStockId.First().ToString());
                    }
                   }
                   else
                   {
                       MessageBox.Show("Please Fill All Fields", "Error");
                   }
               }

               if (updateRButtonX.IsChecked == false && addRButtonX.IsChecked == false)
               {
                   MessageBox.Show("Please select Add or Update", "Error");
               }
           }