コード例 #1
0
        /// <summary>
        /// Passes a transaction to the cashier
        /// </summary>
        /// <param name="counterNo"></param>
        /// <returns></returns>
        public async Task <Tuple <string, int> > PassTransactionToCashier(int counterNo)
        {
            using (var transaction = await _context.Database.BeginTransactionAsync())
            {
                var counter = await _context.Counters
                              .AsNoTracking()
                              .FirstOrDefaultAsync(x => x.COunterNo == counterNo);

                if (counter == null)
                {
                    return(new Tuple <string, int>("", 0));
                }

                // find the transaction to be passed
                var trans = await _context.Transactions
                            .Include(x => x.TransPool)
                            .FirstOrDefaultAsync(x => x.TransactionId == counter.TransactionId);

                // Make sure that the transaction is good for the current day
                if (trans == null)
                {
                    return(new Tuple <string, int>("", 0));
                }

                var prevTrans = await _context.Transactions
                                .FirstOrDefaultAsync(x => x.TransactionId == trans.PrevId);

                var nextTrans = await _context.Transactions
                                .FirstOrDefaultAsync(x => x.TransactionId == trans.NextId);

                // Determine if the version is in sync with the database
                _context.Entry(trans).Property(x => x.RowVersion2).OriginalValue =
                    trans.RowVersion2;

                // Find if there's an existing Trans control for payments
                var cashierTransControl = await _context.TransControls
                                          .Include(x => x.CounterType)
                                          .FirstOrDefaultAsync(x => x.CounterType.IsEndpoint);

                // Create a transaction control if not found
                if (cashierTransControl == null)
                {
                    var cashierCounterType = await _context.CounterTypes
                                             .AsNoTracking()
                                             .FirstOrDefaultAsync(x => x.IsEndpoint);

                    var newCashierTransControl = new TransControl
                    {
                        CounterTypeId = cashierCounterType.CounterTypeId,
                        IsSpecial     = false
                    };

                    await _context.TransControls.AddAsync(newCashierTransControl);

                    await _context.SaveChangesAsync();

                    cashierTransControl = newCashierTransControl;
                }

                // find if there's an existing trans pool for payments
                var cashierTransPool = await _context.TransPools
                                       .FirstOrDefaultAsync(x => x.TransControlId == cashierTransControl.TransControlId &&
                                                            x.TransactionDate.Date == DateTime.Now.Date &&
                                                            x.IsSpecial == trans.TransPool.IsSpecial);

                // Create a trans pool if not found
                if (cashierTransPool == null)
                {
                    var newCashierTransPool = new TransPool
                    {
                        TransControlId  = cashierTransControl.TransControlId,
                        IsSpecial       = trans.TransPool.IsSpecial,
                        First           = 1,
                        Last            = 0,
                        TransactionDate = DateTime.Now.Date
                    };

                    await _context.TransPools.AddAsync(newCashierTransPool);

                    await _context.SaveChangesAsync();

                    cashierTransPool = newCashierTransPool;
                }

                // Find the tail of the main chain of payments
                var cashierTailTrans = await _context.Transactions
                                       .FirstOrDefaultAsync(x => !x.IsServed &&
                                                            x.TransPool.TransPoolId == cashierTransPool.TransPoolId &&
                                                            x.NextId == null);

                if (cashierTailTrans != null)
                {
                    // Determine if the version of object is in sync with what with the database
                    _context.Entry(cashierTailTrans).Property(x => x.RowVersion2).OriginalValue =
                        cashierTailTrans.RowVersion2;

                    // establish the link between the cashier's tail and the passed transaction
                    cashierTailTrans.NextId = trans.TransactionId;
                    trans.PrevId            = cashierTailTrans.TransactionId;
                    trans.NextId            = null; // Since this serves as the new tail of the cashier pool chain

                    _context.Transactions.Update(cashierTailTrans);
                }
                else
                {
                    // Happens when the transaction is the first one in the cashier
                    trans.NextId = null;
                    trans.PrevId = null;
                }

                // Sever ties between the neighboring transactions and the passed transaction
                var transName = "";
                var prioNo    = 0;

                if (prevTrans == null && nextTrans == null)
                {
                    counter.TransactionId = null;
                }

                if (prevTrans != null && nextTrans != null)
                {
                    _context.Entry(prevTrans).Property(x => x.RowVersion2).OriginalValue =
                        prevTrans.RowVersion2;
                    _context.Entry(nextTrans).Property(x => x.RowVersion2).OriginalValue =
                        nextTrans.RowVersion2;

                    nextTrans.PrevId = prevTrans.TransactionId;
                    prevTrans.NextId = nextTrans.TransactionId;

                    // Pass the next transaction to the counter
                    counter.TransactionId = nextTrans.TransactionId;
                    transName             = nextTrans.Name;
                    prioNo = nextTrans.PrioNo;

                    // Update the prev trans and next trans
                    _context.Transactions.Update(prevTrans);
                    _context.Transactions.Update(nextTrans);
                }

                // If there is a previous transaction and no next trans
                if (prevTrans != null && nextTrans == null)
                {
                    _context.Entry(prevTrans).Property(x => x.RowVersion2).OriginalValue =
                        prevTrans.RowVersion2;

                    prevTrans.NextId = null;

                    // Pass the previous transaction to the counter
                    counter.TransactionId = prevTrans.TransactionId;

                    _context.Transactions.Update(prevTrans);
                }

                // If there is no prev transaction but there is a next transaction
                if (prevTrans == null && nextTrans != null)
                {
                    _context.Entry(nextTrans).Property(x => x.RowVersion2).OriginalValue =
                        nextTrans.RowVersion2;

                    nextTrans.PrevId = null;

                    // Pass the next transaction to the counter
                    counter.TransactionId = nextTrans.TransactionId;
                    transName             = nextTrans.Name;
                    prioNo = nextTrans.PrioNo;

                    _context.Transactions.Update(nextTrans);
                }

                // Transfer the pool to the cashier's pool
                trans.TransPoolId     = cashierTransPool.TransPoolId;
                trans.IsServed        = false;
                trans.DateTimeOrdered = DateTime.Now; // The time date and time it is transfered

                _context.Counters.Update(counter);
                _context.Transactions.Update(trans);

                await _context.SaveChangesAsync();

                transaction.Commit();

                return(new Tuple <string, int>(transName, prioNo));
            }
        }
コード例 #2
0
        /// <summary>
        /// Returns the next number
        /// Updates the transaction
        /// Creates a transaction if there's none
        /// </summary>
        /// <returns></returns>
        public async Task <Tuple <string, string, int> > PrintPrioNo(int counterTypeId, bool isSpecial)
        {
            using (var transaction =
                       await _context.Database.BeginTransactionAsync())
            {
                var counterType = await _context.CounterTypes
                                  .AsNoTracking()
                                  .FirstOrDefaultAsync(x => x.CounterTypeId == counterTypeId);

                if (counterType == null)
                {
                    return(new Tuple <string, string, int>("", "", 0));
                }

                // find out if there's an existing transpool
                var transControl = await _context.TransControls
                                   .FirstOrDefaultAsync(x =>
                                                        x.CounterTypeId == counterType.CounterTypeId);

                // Create a transaction controller if there's not anything found
                if (transControl == null)
                {
                    var newTransControl = new TransControl
                    {
                        CounterTypeId = counterType.CounterTypeId,
                        IsSpecial     = false
                    };

                    await _context.TransControls.AddAsync(newTransControl);

                    await _context.SaveChangesAsync();

                    transControl = newTransControl;
                }

                // find out if there's an existing trans pool
                var transPool = await _context.TransPools
                                .FirstOrDefaultAsync(x =>
                                                     x.TransControlId == transControl.TransControlId &&
                                                     x.IsSpecial == isSpecial &&
                                                     x.TransactionDate.Date == DateTime.Now.Date);

                // Create new transaction pool if there's nothing found
                if (transPool == null)
                {
                    var newTransPool = new TransPool
                    {
                        TransControlId  = transControl.TransControlId,
                        IsSpecial       = isSpecial,
                        TransactionDate = DateTime.Now
                    };

                    if (transControl.IsSpecial == null)
                    {
                        transControl.IsSpecial = false;
                    }

                    _context.TransControls.Update(transControl);

                    await _context.TransPools.AddAsync(newTransPool);

                    await _context.SaveChangesAsync(); // needed this to generate Id

                    transPool = newTransPool;
                }

                // Find out if there's any previous transaction that was printed
                var trans = await _context.Transactions
                            .FirstOrDefaultAsync(x => x.NextId == null &&
                                                 x.TransPoolId == transPool.TransPoolId &&
                                                 !x.IsServed);

                // Create new transaction and link it to the previous one if there's any
                if (trans != null)
                {
                    // Determine if the objects' version are in sync with what's in the database
                    _context.Entry(trans).Property(x => x.RowVersion2).OriginalValue = trans.RowVersion2;

                    // Save the last number generated
                    transPool.Last = transPool.Last + 1;

                    // increment the prio number before creating the new transaction
                    var newTransRes = await CreateNewTransaction(transPool.TransPoolId,
                                                                 transPool.Last, isSpecial, counterType.CounterShortName);

                    var newTrans = newTransRes.Item2;

                    _context.Entry(newTrans).Property(x => x.RowVersion2).OriginalValue = newTrans.RowVersion2;

                    trans.NextId    = newTrans.TransactionId;
                    newTrans.PrevId = trans.TransactionId;

                    _context.TransPools.Update(transPool);
                    _context.Transactions.Update(trans);
                    _context.Transactions.Update(newTrans);

                    await _context.SaveChangesAsync();

                    transaction.Commit();

                    return(new Tuple <string, string, int>(counterType.CounterName,
                                                           newTransRes.Item1, newTransRes.Item2.PrioNo));
                }

                // Update the trans pool to save the first and last number
                transPool.First = transPool.First + 1;
                transPool.Last  = transPool.Last + 1;

                var newTransRes2 = await CreateNewTransaction(transPool.TransPoolId,
                                                              transPool.Last, isSpecial, counterType.CounterShortName);

                if (newTransRes2.Item2 == null || newTransRes2.Item2.TransactionId == 0)
                {
                    return(new Tuple <string, string, int>("", "", 0));
                }

                _context.TransPools.Update(transPool);

                await _context.SaveChangesAsync();

                transaction.Commit(); // Commit all to the database

                return(new Tuple <string, string, int>(counterType.CounterName,
                                                       newTransRes2.Item1,
                                                       newTransRes2.Item2.PrioNo));
            }
        }