public async Task InsertAchatAsync(Achat achat)
        {
            _context.Achats.Add(achat);
            _context.SaveChanges();
            triturationUpdate = null;
            triturationUpdate = await _context.Triturations.SingleOrDefaultAsync(c => c.Id == achat.TriturationId);

            triturationUpdate.HuileRestante         = triturationUpdate.HuileRestante - achat.QteAchete;
            _context.Entry(triturationUpdate).State = EntityState.Modified;

            AllHuile = 0;  AllPrixUnitaire = 0;  sommeAllAchat = 0;
            achats.Clear();
            achats = await _context.Achats.AsNoTracking().Where(c => c.Trituration.VarieteId == achat.Trituration.VarieteId).ToListAsync();

            foreach (var achato in achats)
            {
                AllHuile      = AllHuile + achato.QteAchete;
                sommeAllAchat = sommeAllAchat + achato.MontantAchat;
            }
            AllPrixUnitaire = sommeAllAchat / AllHuile;
            produitHuile    = null;
            produitHuile    = await _context.ProduitHuilles.AsNoTracking().SingleOrDefaultAsync(c => c.VarieteId == achat.Trituration.VarieteId);


            produitHuile.Qte_En_Stock  = produitHuile.Qte_En_Stock + achat.QteAchete;
            produitHuile.Prix_unitaire = AllPrixUnitaire;
            _context.ProduitHuilles.Attach(produitHuile);
            _context.Entry(produitHuile).State = EntityState.Modified;
            await SaveAsync();
        }
        public async void UpdateStockageOliveBeforeDeleteAchat(Achat achat)
        {
            AllHuileUpdateTwo = 0; AllPrixUnitaireUpdateTwo = 0; sommeAllAchatUpdateTwo = 0;
            achatsUpdateTwo.Clear();
            achatsUpdateTwo = await _context.Achats.AsNoTracking().Where(c => c.Trituration.VarieteId == achat.Trituration.VarieteId).ToListAsync();

            produitHuilleTwo = null;
            produitHuilleTwo = await _context.ProduitHuilles.SingleOrDefaultAsync(c => c.VarieteId == achat.Trituration.VarieteId);

            if (achatsUpdateTwo != null || achatsUpdateTwo.Count != 0)
            {
                foreach (var achato in achatsUpdateTwo)
                {
                    AllHuileUpdateTwo      = AllHuileUpdateTwo + achato.QteAchete;
                    sommeAllAchatUpdateTwo = sommeAllAchatUpdateTwo + achato.MontantAchat;
                }
                if (sommeAllAchatUpdateTwo == 0)
                {
                    produitHuilleTwo.Qte_En_Stock          = 0;
                    produitHuilleTwo.Prix_unitaire         = 0;
                    _context.Entry(produitHuilleTwo).State = EntityState.Modified;
                    _context.SaveChanges();
                }
                AllPrixUnitaireUpdateTwo               = sommeAllAchatUpdateTwo / AllHuileUpdateTwo;
                produitHuilleTwo.Qte_En_Stock          = produitHuilleTwo.Qte_En_Stock - achat.QteAchete;
                produitHuilleTwo.Prix_unitaire         = AllPrixUnitaireUpdateTwo;
                _context.Entry(produitHuilleTwo).State = EntityState.Modified;
                _context.SaveChanges();
            }
        }
        public async Task UpdateAchatAsync(Achat achat, Trituration triturationUpdate)
        {
            AllHuileUpdateOne = 0;  AllPrixUnitaireUpdateOne = 0; sommeAllAchatUpdateOne = 0;

            achatUpdateOne = await _context.Achats.AsNoTracking().SingleOrDefaultAsync(i => i.Id == achat.Id);

            triturationUpdate.HuileRestante = triturationUpdate.HuileRestante + achatUpdateOne.QteAchete;

            if (triturationUpdate.HuileRestante >= achat.QteAchete)
            {
                triturationUpdate.HuileRestante         = triturationUpdate.HuileRestante - achat.QteAchete;
                _context.Entry(triturationUpdate).State = EntityState.Modified;
                _context.Entry(achat).State             = EntityState.Modified;
                _context.SaveChanges();
                achatsUpdateOne.Clear();
                achatsUpdateOne = await _context.Achats.Include(c => c.Trituration).Where(c => c.Trituration.VarieteId == achat.Trituration.VarieteId).ToListAsync();

                produitHuilleUpdate = null;
                produitHuilleUpdate = await _context.ProduitHuilles.SingleOrDefaultAsync(c => c.VarieteId == achat.Trituration.VarieteId);

                produitHuilleUpdate.Qte_En_Stock = produitHuilleUpdate.Qte_En_Stock - achatUpdateOne.QteAchete;
                produitHuilleUpdate.Qte_En_Stock = produitHuilleUpdate.Qte_En_Stock + achat.QteAchete;
                foreach (var achato in achatsUpdateOne)
                {
                    AllHuileUpdateOne      = AllHuileUpdateOne + achato.QteAchete;
                    sommeAllAchatUpdateOne = sommeAllAchatUpdateOne + achato.MontantAchat;
                }
                AllPrixUnitaireUpdateOne                  = sommeAllAchatUpdateOne / AllHuileUpdateOne;
                produitHuilleUpdate.Prix_unitaire         = AllPrixUnitaireUpdateOne;
                _context.Entry(produitHuilleUpdate).State = EntityState.Modified;
                _context.SaveChanges();
            }
            await SaveAsync();
        }
Example #4
0
        public async Task InsertVenteHuileAsync(VenteHuile venteHuile)
        {
            produitHuilleInsert = null;
            produitHuilleInsert = await _context.ProduitHuilles.Where(h => h.VarieteId == venteHuile.VarieteId).SingleOrDefaultAsync();

            produitHuilleInsert.Qte_En_Stock          = produitHuilleInsert.Qte_En_Stock - venteHuile.Qte_Vente;
            _context.Entry(produitHuilleInsert).State = EntityState.Modified;
            _context.VenteHuiles.Add(venteHuile);
            await SaveAsync();
        }
Example #5
0
 public VenteHuileService(DataContext context, IMapper mapper)
 {
     _context            = context;
     _mapper             = mapper;
     produitHuilleInsert = new ProduitHuille();
     produitHuilleUpdate = new ProduitHuille();
     venteHuileUpdate    = new VenteHuile();
     produitHuilleDelete = new ProduitHuille();
     venteHuileDelete    = new VenteHuile();
     produitHuilleCheck  = new ProduitHuille();
     venteHuileCheck     = new VenteHuile();
 }
Example #6
0
        public async Task DeletVenteHuileAsync(int id)
        {
            produitHuilleDelete = null;
            venteHuileDelete    = null;
            venteHuileDelete    = await _context.VenteHuiles.FindAsync(id);

            produitHuilleDelete = await _context.ProduitHuilles.Where(h => h.VarieteId == venteHuileDelete.VarieteId).SingleOrDefaultAsync();

            produitHuilleDelete.Qte_En_Stock          = produitHuilleDelete.Qte_En_Stock + venteHuileDelete.Qte_Vente;
            _context.Entry(produitHuilleDelete).State = EntityState.Modified;
            _context.VenteHuiles.Remove(venteHuileDelete);
            await SaveAsync();
        }
Example #7
0
        public async Task UpdateVenteHuileAsync(VenteHuile venteHuile)
        {
            produitHuilleUpdate = null;
            venteHuileUpdate    = null;
            produitHuilleUpdate = await _context.ProduitHuilles.Where(h => h.VarieteId == venteHuile.VarieteId).SingleOrDefaultAsync();

            venteHuileUpdate = await _context.VenteHuiles.AsNoTracking().SingleOrDefaultAsync(c => c.Id == venteHuile.Id);

            produitHuilleUpdate.Qte_En_Stock          = produitHuilleUpdate.Qte_En_Stock + venteHuileUpdate.Qte_Vente;
            produitHuilleUpdate.Qte_En_Stock          = produitHuilleUpdate.Qte_En_Stock - venteHuile.Qte_Vente;
            _context.Entry(produitHuilleUpdate).State = EntityState.Modified;
            _context.Entry(venteHuile).State          = EntityState.Modified;
            await SaveAsync();
        }
Example #8
0
 public bool CheckQantityHuileUpdateInf(VenteHuile venteHuile)
 {
     produitHuilleCheck = null; venteHuileCheck = null;
     produitHuilleCheck = _context.ProduitHuilles.AsNoTracking().Where(h => h.VarieteId == venteHuile.VarieteId).SingleOrDefault();
     venteHuileCheck    = _context.VenteHuiles.AsNoTracking().SingleOrDefault(c => c.Id == venteHuile.Id);
     produitHuilleCheck.Qte_En_Stock = produitHuilleCheck.Qte_En_Stock + venteHuileCheck.Qte_Vente;
     if (produitHuilleCheck.Qte_En_Stock < venteHuile.Qte_Vente)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
        public async Task DeleteAchatAsync(Achat achat)
        {
            // var achata = await _context.Achats.AsNoTracking().SingleOrDefaultAsync(i => i.Id == id);
            // UpdateTriturationBeforeDeleteAchat(achata);
            // UpdateStockageOliveBeforeDeleteAchat(achata);
            triturationDelete = null;
            triturationDelete = await _context.Triturations.SingleOrDefaultAsync(c => c.Id == achat.TriturationId);

            triturationDelete.HuileRestante         = triturationDelete.HuileRestante + achat.QteAchete;
            _context.Entry(triturationDelete).State = EntityState.Modified;
            _context.Achats.Remove(achat);
            _context.SaveChanges();

            AllHuileUpdate = 0;   AllPrixUnitaireUpdate = 0;  sommeAllAchatUpdate = 0;
            achatsDelete.Clear(); produitHuileDelete = null;
            achatsDelete = await _context.Achats.Where(c => c.Trituration.VarieteId == achat.Trituration.VarieteId).ToListAsync();

            produitHuileDelete = await _context.ProduitHuilles.SingleOrDefaultAsync(c => c.VarieteId == achat.Trituration.VarieteId);

            if (achatsDelete != null || achatsDelete.Count != 0)
            {
                foreach (var achato in achatsDelete)
                {
                    AllHuileUpdate      = AllHuileUpdate + achato.QteAchete;
                    sommeAllAchatUpdate = sommeAllAchatUpdate + achato.MontantAchat;
                }
                AllPrixUnitaireUpdate = sommeAllAchatUpdate / AllHuileUpdate;
                if (sommeAllAchatUpdate == 0)
                {
                    produitHuileDelete.Qte_En_Stock          = 0;
                    produitHuileDelete.Prix_unitaire         = 0;
                    _context.Entry(produitHuileDelete).State = EntityState.Modified;
                    _context.SaveChanges();
                }
                else
                {
                    produitHuileDelete.Qte_En_Stock          = produitHuileDelete.Qte_En_Stock - achat.QteAchete;
                    produitHuileDelete.Prix_unitaire         = AllPrixUnitaireUpdate;
                    _context.Entry(produitHuileDelete).State = EntityState.Modified;
                    _context.SaveChanges();
                }
            }


            await SaveAsync();
        }
 public AchatsService(DataContext context, IMapper mapper)
 {
     _context = context;
     //   _templateGenerator = templateGenerator;
     _mapper             = mapper;
     achat               = new Achat();
     achatUpdateOne      = new Achat();
     achatsUpdateOne     = new Collection <Achat>();
     achatsUpdateTwo     = new Collection <Achat>();
     triturationUpdate   = new Trituration();
     triturationDelete   = new Trituration();
     produitHuile        = new ProduitHuille();
     produitHuileDelete  = new ProduitHuille();
     achats              = new Collection <Achat>();
     achatsDelete        = new Collection <Achat>();
     produitHuilleUpdate = new ProduitHuille();
     produitHuilleTwo    = new ProduitHuille();
 }