Exemple #1
0
        public LedgerServiceAccountTransactionBuilder Begin(string description, DateTime postDateTime)
        {
            if (m_transaction != null)
            {
                throw new InvalidOperationException("Begin has already been called.");
            }

            m_transaction = new MLedger_PostLedgerTransaction()
            {
                Description  = description,
                PostDateTime = postDateTime,
                Entries      = new List <MLedger_PostLedgerTransactionEntry>()
            };

            return(this);
        }
Exemple #2
0
        public async Task <long> PostLedgerAccountTransactionAsync(MLedger_PostLedgerTransaction transaction)
        {
            using var log = BeginFunction(nameof(LedgerMicroService), nameof(PostLedgerAccountTransactionAsync), transaction);
            try
            {
                var utcNow = GetUtcNow();

                using var ctx = CreateQuiltContext();

                var builder = ctx.CreateLedgerAccountTransactionBuilder()
                              .Begin(transaction.Description, transaction.PostDateTime, utcNow);

                if (transaction.UnitOfWork != null)
                {
                    var unitOfWork = new UnitOfWork(transaction.UnitOfWork);
                    _ = builder.UnitOfWork(unitOfWork);
                }

                foreach (var entry in transaction.Entries)
                {
                    _ = entry.DebitCreditCode switch
                    {
                        LedgerAccountCodes.Debit => builder.Debit(entry.LedgerAccountNumber, entry.EntryAmount, entry.LedgerReference, entry.SalesTaxJurisdiction),
                        LedgerAccountCodes.Credit => builder.Credit(entry.LedgerAccountNumber, entry.EntryAmount, entry.LedgerReference, entry.SalesTaxJurisdiction),
                        _ => throw new ArgumentException($"Invalid DebitCreditCode {entry.DebitCreditCode}."),
                    };
                }

                var dbLedgerTransaction = builder.Create();

                _ = await ctx.SaveChangesAsync().ConfigureAwait(false);

                var result = dbLedgerTransaction.LedgerTransactionId;

                log.Result(result);

                return(result);
            }
            catch (Exception ex)
            {
                log.Exception(ex);
                throw;
            }
        }