public String DesactivatePurchase(Purchase purchase)
        {
            _gestionDb = new GcdbEntities();

            purchase.Status = 1;
            MajPurchase(purchase);

            return "Achat désactiver avec succes";
        }
        public String AddPurchase(Purchase pur)
        {
            _gestionDb = new GcdbEntities();
            if (IsPurchaseExist(pur)) return "Achat existe déjà";

            _gestionDb.Purchases.Add(pur);
            _gestionDb.SaveChanges();

            return "Achat ajouter avec succes";
        }
        public String DelPurchase(Purchase pur)
        {
            _gestionDb = new GcdbEntities();
            if (!IsPurchaseExist(pur)) return "Achat n'existante pas";

            Purchase purchase = _gestionDb.Purchases.Single(p => p.PurchaseID == pur.PurchaseID);
            _gestionDb.Purchases.Remove(pur);
            _gestionDb.SaveChanges();

            return "Achat supprimer avec succes";
        }
        private void CustomersDataTable_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (PurchaseGridControl.VisibleRowCount == 0) return;
            int rowHandle = PurchaseGridControl.View.FocusedRowHandle;
            if (rowHandle < 0) return;
            _purchaseClient = new PurchaseClient();
            int purchaseId = Convert.ToInt32(PurchaseGridControl.GetCellValue(rowHandle, "PurchaseID").ToString());
            _getPurchase = _purchaseClient.GetPurchaseById(purchaseId);

            if (_getPurchase == null) return;
            LoadPurchaseStoreGridControl(_getPurchase);
               // OrderDateDateEdit.Text = order.OrderDate.ToString();
        }
        private void LoadGridProducts(Purchase purchase)
        {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                _purchaseStoresList = _purchaseDetailsManager.GetPurchaseDetails();
                if (_purchaseStoresList != null)

                    SelectedProductsDataGrid.ItemsSource = (from t in _purchaseStoresList
                        where t.PurchaseID == purchase.PurchaseID
                        select new
                        {
                            ProductName = t.Product.ProductName,
                            CategoryName = t.Product.SubCategory.Category.CategoryName,
                            UnitsInStock = t.Product.UnitsInStock,
                            PurchasePrice = t.Product.PurchasePrice,
                            SalePrice = t.Product.SalePrice,
                         //   Mesure = t.Products.Measures.MesureName,
                        }
                        ).ToList();
            }));
        }
 private void LoadPurchaseStoreGridControl(Purchase thePurchase)
 {
     var getPurchaseStores = new BackgroundWorker();
     getPurchaseStores.DoWork += GetPurchaseStoresFromPurchaseOnDoWork;
     getPurchaseStores.RunWorkerCompleted += GetPurchaseStoresFromPurchaseOnRunWorkerCompleted;
     getPurchaseStores.RunWorkerAsync(thePurchase);
 }
        private String MajPurchase(Purchase pur)
        {
            _gestionDb = new GcdbEntities();

            Purchase purchase = _gestionDb.Purchases.Single(p => p.PurchaseID == pur.PurchaseID);
            try
            {
                purchase = pur; // si sa marche pas alors attache detache sinon affactation champ par champ
            }
            catch (Exception )
            {
                return "erreur";
            }

            return "Achat modifier avec succes";
        }
        private static bool IsPurchaseExist(Purchase pur)
        {
            try
            {

                GcdbEntities gestionDb = new GcdbEntities();

                var query = from t in gestionDb.Purchases
                          where t.PurchaseID.Equals(pur.PurchaseID)
                          select t;

             return query.Any();
            }
            catch (Exception)
            {
                return false;

            }

            return false;
        }
        public Double GetTotalPurchase(Purchase p)
        {
            try
            {
               _gestionDb = new GcdbEntities();

                var list = from purchase in _gestionDb.PurchaseStores
                           where (purchase.PurchaseID == p.PurchaseID)
                           select purchase;
                Double somme = 0;

              //  foreach (PurchaseStore pd in list) somme += (Double)pd.UnitPrice * pd.Quantity;

                return somme;
            }
            catch (Exception)
            {
                return 0;
            }
        }
 public List<PurchaseStore> GetPurchaseStoresList(Purchase getPurchase)
 {
     try
     {
         _gestionDb = new GcdbEntities();
         var query = from t in _gestionDb.PurchaseStores
                     where t.PurchaseID == getPurchase.PurchaseID
                     select t;
         return !query.Any() ? new List<PurchaseStore>() : query.ToList();
     }
     catch (Exception)
     {
         return new List<PurchaseStore>();
     }
 }
        private void CreatePurchase(float getDicount, string etatPayement, string modePayement,
            string modeAchat, string commandNum, string factureNum, float facturePriceHt,
            string remarque, Provider provider, DateTime commandeDtae, float tvaValue)
        {
            try
            {
                var gestionDb = new GcdbEntities();
                Provider getProvider = gestionDb.Providers.FirstOrDefault(c => c.SupplierID == provider.SupplierID);
                if(getProvider==null)return;
                var newPurchase = new Purchase
                {
                    TvaValue = tvaValue,
                    FacturePrice = facturePriceHt,
                    PurchaseMontant = _total,
                    EtatPaiement = etatPayement,
                    ModePaiement = modePayement,
                    ModeAchat = modeAchat,
                    CommandeNum = commandNum,
                    FactureNum = factureNum,
                    PurchaseDate = AchatDateDte.DateTime.Date,
                    CommandeDate = commandeDtae,
                    Status = 0,
                    Provider = getProvider,
                    SupplierID = provider.SupplierID,
                    Description = remarque,
                    Discount = (float?) getDicount,
                    Timbre = (double?) _timbre
                };
                foreach (var entitys in _productsSelSelected)
                {
                    var getId = Getid(entitys, tvaValue);
                    Product getProduct = gestionDb.Products.FirstOrDefault(c => c.ProductID == entitys.ProductId);
                    if(getProduct==null)continue;
                    var nwStore = new PurchaseStore
                    {
                        Product = getProduct,
                        ProductID = entitys.ProductId,
                        PurchasePrice = entitys.PurchasePrice,
                        TotalPriceAchat = entitys.TotalPriceAchat,
                        UnitsOnOrder = (double?) entitys.UnitsOnOrder,
                        VentePriceGros = entitys.VentePriceGros,
                        VentePriceDetail = entitys.VentePriceDetail,
                        VentePriceComptoire = entitys.VentePriceComptoire,
                        Discount = entitys.Discount,
                        TvaValue = tvaValue,
                        StockStoreID = getId
                    };
                    newPurchase.PurchaseStores.Add(nwStore);
                }
                gestionDb.Purchases.Add(newPurchase);
                gestionDb.SaveChanges();
            }
            catch (Exception e)
            {

                MessageBox.Show(e.ToString());
            }
        }