public PurchaseOrdersListViewModel()
        {
            if (DesignerProperties.GetIsInDesignMode(
                new System.Windows.DependencyObject())) return;

            try
            {
                var purchaseOrders = db.PurchaseOrders.Include(p => p.Supplier);
                PurchaseOrders = new ObservableCollection<PurchaseOrder>(purchaseOrders.ToList());
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message.ToString());
            }
            try
            {
                var suppliers = db.Suppliers.ToList();
                Suppliers = new ObservableCollection<Supplier>(suppliers);
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message.ToString());
            }
            try
            {
                var rawMaterials = db.RawMaterials.ToList();
                RawMaterials = new ObservableCollection<RawMaterial>(rawMaterials);
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message.ToString());
            }

            _newPurchaseOrder = new PurchaseOrder();
            _newPurchaseOrderLine = new PurchaseOrderLine();
            _newPurchaseOrder.PurchaseOrderLines = new ObservableCollection<PurchaseOrderLine>();
            _purchaseOrderLines = new ObservableCollection<PurchaseOrderLine>();
            _searchResults = new ObservableCollection<PurchaseOrder>();

            DeletePurchaseOrderCommand = new RelayCommand<PurchaseOrder>(onDeletePurchaseOrder);
            UpdatePurchaseOrderCommand = new RelayCommand<PurchaseOrder>(onUpdatePurchaseOrder);
            AddPurchaseOrderCommand = new RelayCommand<PurchaseOrder>(onAddPurchaseOrder);
            AddPurchaseOrderLineCommand = new RelayCommand<PurchaseOrderLine>(onAddPurchaseOrderLine);
            UpdatePurchaseOrderLineCommand = new RelayCommand<PurchaseOrderLine>(onUpdatePurchaseOrderLine);
            SelectPurchaseOrderCommand = new RelayCommand<PurchaseOrder>(onSelectPurchaseOrder);
            FindPurchaseOrdersCommand = new RelayCommand<string>(onFindPurchaseOrders);

            DeletePurchaseOrderLineCommand = new RelayCommand<PurchaseOrderLine>(onDeletePurchaseOrderLine);
            DeleteNewPurchaseOrderLineCommand = new RelayCommand<PurchaseOrderLine>(onDeleteNewPurchaseOrderLine);
        }
        public void onUpdatePurchaseOrder(PurchaseOrder ord)
        {
            var purchaseOrder = ord;

            if (purchaseOrder != null)
            {
                db.Entry(purchaseOrder).State = EntityState.Modified;
                db.SaveChanges();
                PurchaseOrders = UpdatePurchaseOrdersCollection(PurchaseOrders);

                MessageBoxResult msgBox = MessageBox.Show("Changes Saved", "Success");

            }
        }
        public void onDeletePurchaseOrder(PurchaseOrder ord)
        {
            if (ord != null)
            {
                MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("Are you sure you wish to delete this purchaseOrder?", "Confirm Delete", System.Windows.MessageBoxButton.YesNo);

                if (messageBoxResult == MessageBoxResult.Yes)
                {
                    int purchaseOrderid = ord.purchaseOrderID;
                    PurchaseOrder ordToRemove = (PurchaseOrder)PurchaseOrders.Single(p => p.purchaseOrderID == purchaseOrderid);

                    db.PurchaseOrders.Remove(ordToRemove);
                    db.SaveChanges();
                    PurchaseOrders = UpdatePurchaseOrdersCollection(PurchaseOrders);
                }
            }
        }
 public void onSelectPurchaseOrder(PurchaseOrder ord)
 {
     SelectedPurchaseOrder = ord;
     PurchaseOrderLines = new ObservableCollection<PurchaseOrderLine>(SelectedPurchaseOrder.PurchaseOrderLines.ToList());
 }
        public void onAddPurchaseOrder(PurchaseOrder ord)
        {
            var newPurchaseOrder = ord;
            var ordSup = from s in db.Suppliers where s.supplierID == ord.supplierID select s;
            newPurchaseOrder.Supplier = (Supplier)ordSup.Single();

            if (newPurchaseOrder != null)
            {
                db.PurchaseOrders.Add(newPurchaseOrder);
                db.SaveChanges();
                PurchaseOrders = UpdatePurchaseOrdersCollection(PurchaseOrders);
                MessageBoxResult msgBox = MessageBox.Show("PurchaseOrder Added", "Success");
            }
        }