public List <orderHisto> getcommandehisto(ClientReturn client) { List <orderHisto> listOrderHisto = new List <orderHisto>(); List <Commande> listCommand = dbContext.Commandes.Where(w => w.id_client == client.id_client).ToList(); foreach (Commande com in listCommand) { orderHisto orderhisto = new orderHisto(); Achat achat = dbContext.Achats.FirstOrDefault(f => f.id_commande == com.id_commande); Stock st = dbContext.Stocks.FirstOrDefault(f => f.id_stock == achat.id_stock); Produit pro = dbContext.Produits.FirstOrDefault(f => f.id_stock == st.id_stock); orderhisto.heureCommand = com.heure_commande; orderhisto.statutLivraison = com.statut_livraison; orderhisto.quantite = (int)achat.quantité; orderhisto.prix_total = (double)achat.prix_total; orderhisto.nom_Produit = st.nom_produit_stock; orderhisto.prix_Produit_Unite = (double)pro.prix_unite; listOrderHisto.Add(orderhisto); } return(listOrderHisto); }
public string passerCommande(string nomclient, string nomproduit, int quantité) { var cli = dbContext.Clients.FirstOrDefault(f => f.nom == nomclient); var prod = dbContext.Stocks.FirstOrDefault(f => f.nom_produit_stock == nomproduit); var prixProd = dbContext.Produits.FirstOrDefault(f => f.nom_produit == nomproduit); if (prod.quantite_produit >= quantité) // si la quantité est inférieur ou égale a la quantité en stock { Commande nouvelCommande = new Commande(); // création nouvelle commande nouvelCommande.heure_commande = DateTime.Now.ToString(); nouvelCommande.statut_commande = "Validé"; nouvelCommande.statut_livraison = "Livré"; nouvelCommande.id_client = cli.id_client; // id_client dans commande sera egale au meme id_client dans client car le meme nom de client dbContext.Commandes.Add(nouvelCommande); //creation de la commande Achat nouvelAchat = new Achat(); //reation nouvel achat nouvelAchat.quantité = quantité; nouvelAchat.id_stock = prod.id_stock; // id_stoc dans achat sera egale au meme id_stock dans produit car le meme nom de produit prod.quantite_produit -= quantité; nouvelAchat.prix_total = quantité * prixProd.prix_unite; dbContext.Achats.Add(nouvelAchat); dbContext.SaveChanges(); var com = dbContext.Commandes.FirstOrDefault(f => f.id_commande == nouvelCommande.id_commande); var achat = dbContext.Achats.FirstOrDefault(f => f.id_achat == nouvelAchat.id_achat); achat.id_commande = com.id_commande; // recupération de id de la nouvelle commande dbContext.SaveChanges(); return("commande ok"); } else { return("la quantité de produit est insuffissant !!! \n verifier stock!"); } }