public string AddNewCustomer(string companName, string contactTitle, string contactName, string address, string country, string region, string city, string postalCode, string phone, string homePage, string email, string fax, string rC, string nF, string nIs, string aI, int status, byte[] photo)
 {
     try
     {
         _gestionDb = new GcdbEntities();
         Customer newCustomer = new Customer();
         newCustomer.CompanyName = companName;
         newCustomer.ContactTitle = contactTitle;
         newCustomer.ContactName = contactName;
         newCustomer.Address = address;
         newCustomer.Country = country;
         newCustomer.Region = region;
         newCustomer.City = city;
         newCustomer.PostalCode = postalCode;
         newCustomer.Phone = phone;
         newCustomer.HomePage = homePage;
         newCustomer.Email = email;
         newCustomer.Fax = fax;
         newCustomer.RC = rC;
         newCustomer.NF = nF;
         newCustomer.NIS = nIs;
         newCustomer.AI = aI;
         newCustomer.Photo = photo;
         newCustomer.Status = status;
         _gestionDb.Customers.Add(newCustomer);
         _gestionDb.SaveChanges();
         return "Ajouté avec succés";
     }
     catch (Exception)
     {
         return "Erreur";
     }
 }
        public String AddCustomer(Customer c)
        {
            _gestionDb = new GcdbEntities();
            if (IsCustomerExist(c)) return "Client existe déjà";

            _gestionDb.Customers.Add(c);
            _gestionDb.SaveChanges();

            return "Ajouté avec succes";
        }
 public String DesactivateCustomer(Customer cust)
 {
     _gestionDb = new GcdbEntities();
     var query = from t in _gestionDb.Customers
         where t.CustomerID == cust.CustomerID
         select t;
     if (!query.Any()) return "Erreur";
     query.First().Status = 1;
     _gestionDb.SaveChanges();
     return "Client désactivé avec succes";
 }
        public String DelCustomer(Customer c)
        {
            _gestionDb = new GcdbEntities();
            if (!IsCustomerExist(c)) return "Client n'existante pas";
            var customer = _gestionDb.Customers.Single(cus => cus.CustomerID.Equals(c.CustomerID));
            _gestionDb.Customers.Remove(customer);

            _gestionDb.SaveChanges();

            return "Client supprimer avec succes";
        }
        private void LoadCustomerFields(Customer cust)
        {
            // ImageSource image = ByteToImage(cust.Photo);
            ImageEdit1.Source = null;

            //**********************
            CompanyTxtBox.Text = cust.CompanyName;
            TitleContactTxtBox.Text = cust.ContactTitle;
            NameContactTxtBox.Text = cust.ContactName;
            AdresseTxtBox.Text = cust.Address;
            CountryTxtBox.Text = cust.Country;
            RegionTxtBox.Text = cust.Region;
            CityTxtBox.Text = cust.City;
            ZipCodeTxtBox.Text = cust.PostalCode;
            TelephonTxtBox.Text = cust.Phone;
            FaxTxtBox.Text = cust.Fax;
            WebSiteTxtBox.Text = cust.HomePage;
            EmailTxtBox.Text = cust.Email;
            RcTxtBox.Text = cust.RC;
            NsTxtBox.Text = cust.NF;
            NisTxtBox.Text = cust.NIS;
            AiTxtBox.Text = cust.AI;
        }
 public string UpdateCustomer(Customer cust, string companName, string contactTitle, string contactName,
     string address, string country, string region, string city, string postalCode, string phone, 
     string homePage,
     string email, string fax, string rC, string nF, string nIs, string aI, int status, byte[] photo)
 {
     try
     {
         _gestionDb = new GcdbEntities();
         var query = from t in _gestionDb.Customers
             where t.CustomerID == cust.CustomerID
             select t;
         if (!query.Any()) return "Erreur";
         query.First().CompanyName = companName;
         query.First().ContactTitle = contactTitle;
         query.First().ContactName = contactName;
         query.First().Address = address;
         query.First().Country = country;
         query.First().Region = region;
         query.First().City = city;
         query.First().PostalCode = postalCode;
         query.First().Phone = phone;
         query.First().HomePage = homePage;
         query.First().Email = email;
         query.First().Fax = fax;
         query.First().RC = rC;
         query.First().NF = nF;
         query.First().NIS = nIs;
         query.First().AI = aI;
         query.First().Photo = photo;
         query.First().Status = status;
         _gestionDb.SaveChanges();
         return "Mise à jour avec succés";
     }
     catch (Exception)
     {
         return "Erreur";
     }
 }
        private bool IsCustomerExist(Customer c)
        {
            try
            {

                _gestionDb = new GcdbEntities();

            var query = from t in _gestionDb.Customers
                          where (t.CompanyName.Equals(c.CompanyName) )
                          select t;

             return query.Any();

            }
            catch (Exception)
            {

                return false;
            }

                return false;
        }
        public Double GetTotalPerCustomer(Customer cust)
        {
            try
            {
                _gestionDb = new GcdbEntities();

                var list = from od in _gestionDb.OrderDetails
                           where (od.Order.CustomerID == cust.CustomerID)
                           select od;
                Double somme = 0;

                foreach (OrderDetail od in list) somme += (Double)od.UnitPrice * od.Quantity;

                return somme;
            }
            catch (Exception)
            {
                return 0;
            }
        }
        public Double GetTotalPerCustomer(Customer sup)
        {
            try
            {
               _gestionDb = new GcdbEntities();

                var list = from pd in _gestionDb.OrderDetails
                           where (pd.Order.Customer.CustomerID == sup.CustomerID)
                           select pd;
                Double somme = 0;

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

                return somme;
            }
            catch (Exception)
            {
                return 0;
            }
        }
        private Customer GetCustomer()
        {
            if (CustomerDataGrid.VisibleRowCount == 0) return null;
            int rowHandle = CustomerDataGrid.View.FocusedRowHandle;
            int custorId = (int) CustomerDataGrid.GetCellValue(rowHandle, "CustomerID");
            _customer = _customersManager.GetCustomerById(custorId);

            return _customer;
        }
        private String Enregistrer()
        {
            Employee newEmployee = WhoEmployee.Employee;
            _customer = GetCustomer();
            if (_customer == null)
            {
                DXMessageBox.Show(this,"Vous devez choisir un Client");
                return "Commande non Enregistrer";
            }
            if (_productSelSelected.Count == 0)
            {
                DXMessageBox.Show(this,"Vous devez choisir au moin un produit");
                return "Commande non Enregistrer";
            }
            String remarque = RemarqueRichTxtBox.Text;
            decimal tvaValue = ConvertToDecimal(TvaComboBox.Text);
            CreateVente( _productSelSelected, remarque, _customer, OrderDateDateEdit.DateTime, tvaValue, newEmployee);

            return "Votre commande a été enregistrer avec succes";
        }
        private void CreateVente(List<ProduitsSel> productSelSelected, string remarque, Customer customer, DateTime dateTime, decimal tvaValue, Employee newEmployee)
        {
            try
            {

                Customer getCustomer = gestionDb.Customers.FirstOrDefault(c => c.CustomerID==customer.CustomerID);
                if(getCustomer!=null)
                {
                    Order newOrder = new Order
                    {
                        OrderDate = dateTime,
                        CommandeDate = dateTime,
                        CustomerID = getCustomer.CustomerID,
                        Customer = getCustomer,
                        TvaValue = tvaValue,
                        Description = remarque,
                        Status = 0,
                    };
                    foreach (var entity in productSelSelected)
                    {
                        OrderDetail newOrderDetail = new OrderDetail
                        {

                            ProductID = entity.TheProduct.ProductID,
                            Quantity = (int) entity.UnitsOnOrder,
                            UnitPrice = entity.UnitePrice,
                            TotalPrice = entity.TotalPrice,
                        };
                        newOrder.OrderDetails.Add(newOrderDetail);
                    }
                    gestionDb.Orders.Add(newOrder);
                    gestionDb.SaveChanges();
                    theOrder= newOrder;
                }
                else theOrder = null;
            }
            catch (Exception exe)
            {
                MessageBox.Show(exe.ToString());
                theOrder= null;
            }
        }