public async Task <DC31EcritureCollectiveJournal> SaisieEcritureSimple(int noClient, int noUtilisateur, DtoTGC003InpDC31EcritureCollectiveJournalForWriteEcritureSimple dto) { var recordDC31 = new DC31EcritureCollectiveJournal() { NoClient = noClient, NoUtilisateur = noUtilisateur }; var lastDC31 = _context.DC31EcritureCollectiveJournal .Where(x => x.NoClient == noClient && x.NoUtilisateur == noUtilisateur) .OrderByDescending(X => X.NoEcritureCollectiveJournal) .ToList(); var nextDC31 = 0; foreach (var i in lastDC31) { nextDC31 = i.NoEcritureCollectiveJournal; break; } nextDC31++; recordDC31 = new DC31EcritureCollectiveJournal() { NoClient = noClient, NoUtilisateur = noUtilisateur, NoEcritureCollectiveJournal = nextDC31, DateEcriture = dto.DateEcriture }; Add <DC31EcritureCollectiveJournal>(recordDC31); //if (!await SaveAll()) // pas besoin de rollback, c'est pas une suite d'écritures // return null; var recordDC30 = new DC30EcritureJournal() { NoClient = noClient, NoUtilisateur = noUtilisateur, NoEcritureCollectiveJournal = nextDC31, NoEcritureJournal = 1, NoCompteDebit = dto.NoCompteDebit, NoCompteCredit = dto.NoCompteCredit, NoPiece = dto.NoPiece, // DatePiece A FAIRE ISSUE 31 Libelle1 = dto.Libelle1Debit, Libelle2 = dto.Libelle2Debit, // Libellé crédit A FAIRE ISSUE 31 Montant = dto.Montant, SwAutomatique = false, }; Add <DC30EcritureJournal>(recordDC30); if (!await SaveAll()) { return(null); } return(await _context.DC31EcritureCollectiveJournal .FirstOrDefaultAsync(x => (x.NoClient == noClient) && (x.NoUtilisateur == noUtilisateur) && (x.NoEcritureCollectiveJournal == nextDC31))); }
private async Task <DC20Journal> RollBackJournalisation(int noClient, int noUtilisateur, int noJournal, List <DC31EcritureCollectiveJournal> itemsDC31) { var itemDC20 = await _context.DC20Journal .Include(x => x.EcritureCollective) .ThenInclude(x => x.Ecriture) .FirstOrDefaultAsync(x => (x.NoClient == noClient) && (x.NoJournal == noJournal)); // On efface la comptabilisation foreach (var item in itemDC20.EcritureCollective) { foreach (var i in item.Ecriture) { Delete <DC21Ecriture>(i); } Delete <DC22EcritureCollective>(item); } // Puis le journal Delete <DC20Journal>(itemDC20); // On re-replit les écritures à journaliser + collective foreach (var item in itemsDC31) { var itemsDC30 = item.EcritureJournal; item.EcritureJournal = new List <DC30EcritureJournal>(); item.NoClient = noClient; item.NoUtilisateur = noUtilisateur; // Increment - manuel pour clé composite var lastDC31 = _context.DC31EcritureCollectiveJournal .Where(x => (x.NoClient == noClient) && (x.NoUtilisateur == noUtilisateur)) .OrderByDescending(x => x.NoEcritureCollectiveJournal) .ToList(); var nextDC31 = 0; foreach (var i in lastDC31) { nextDC31 = i.NoEcritureCollectiveJournal; break; } nextDC31++; item.NoEcritureCollectiveJournal = nextDC31; Add <DC31EcritureCollectiveJournal>(item); await SaveAll(); foreach (var itemDC30 in itemsDC30) { // Increment - manuel pour clé composite var lastDC30 = _context.DC30EcritureJournal .Where(x => (x.NoClient == noClient) && (x.NoUtilisateur == noUtilisateur) && (x.NoEcritureCollectiveJournal == item.NoEcritureCollectiveJournal)) .OrderByDescending(x => x.NoEcritureJournal) .ToList(); var nextDC30 = 0; foreach (var i in lastDC30) { nextDC30 = i.NoEcritureJournal; break; } nextDC30++; var recordDC30 = new DC30EcritureJournal() { NoClient = noClient, NoUtilisateur = noUtilisateur, NoEcritureCollectiveJournal = item.NoEcritureCollectiveJournal, NoEcritureJournal = nextDC30, NoCompteDebit = itemDC30.NoCompteDebit, NoCompteCredit = itemDC30.NoCompteCredit, NoPiece = itemDC30.NoPiece, Libelle1 = itemDC30.Libelle1, Libelle2 = itemDC30.Libelle2, Montant = itemDC30.Montant, SwAutomatique = itemDC30.SwAutomatique, }; Add <DC30EcritureJournal>(recordDC30); await SaveAll(); } } return(itemDC20); }