private async Task <Tuple <bool, bool> > EligibleforAduDupDelete(TransactionQueueModel transactionQueueModel,
                                                                         ExternalDependencies.BusinessLayer.Models.Facility facility, TransactionRequest request)
        {
            var lastAduXrefs = await _lastAduXrefRepository.GetAllLastAduXrefTransactions();

            LastAduXref lastAduXref = lastAduXrefs?.FirstOrDefault(la => la.FacilityId == facility.Id &&
                                                                   la.FormularyId == transactionQueueModel.FormularyId &&
                                                                   la.Destination == request?.Patient?.DeliverToLocation);

            var transactions = await _transactionQueueRepository.GetAllTransactions();

            var AduItemAlreadyinQueue = transactions?.FirstOrDefault(tqa => tqa.FacilityId == facility.Id &&
                                                                     tqa.FormularyId == transactionQueueModel.FormularyId &&
                                                                     tqa.Destination == request?.Patient?.DeliverToLocation &&
                                                                     (tqa.Status == TransactionStatus.Pending.ToString() || tqa.Status == TransactionStatus.Hold.ToString()));

            bool eligibleforAduDupCheck = ((lastAduXref != null && lastAduXref.LastAduTransUtcDateTime != null) ||
                                           (AduItemAlreadyinQueue != null && AduItemAlreadyinQueue.ReceivedUtcDateTime != null)) &&
                                          facility.AduDupeTimeDelay != null &&
                                          facility.AduDupeTimeDelay > 0;

            if (eligibleforAduDupCheck)
            {
                bool eligibleforAduDupDelete =
                    (lastAduXref != null &&
                     ((DateTime)lastAduXref.LastAduTransUtcDateTime).AddMinutes((int)facility.AduDupeTimeDelay) > DateTime.UtcNow) ||
                    (AduItemAlreadyinQueue != null &&
                     ((DateTime)AduItemAlreadyinQueue.ReceivedUtcDateTime).AddMinutes((int)facility.AduDupeTimeDelay) > DateTime.UtcNow);

                if (eligibleforAduDupDelete)
                {
                    _logger.LogInformation(string.Format(CommonConstants.LoggingMessage.ProcessedAdmTransaction, request.RequestId));

                    //Not updated any existing txn and current txn should be ignored
                    return(new Tuple <bool, bool>(true, false));
                }
            }

            //Normal processing
            //Not updated any existing txn and current txn should also not be ignored
            return(new Tuple <bool, bool>(false, false));
        }
        /// <summary>
        /// This method is used for processing Adu transaction
        /// </summary>
        /// <param name="request"></param>
        /// <param name="transactionQueueModel"></param>
        /// <param name="item"></param>
        /// <param name="priority"></param>
        /// <param name="facility"></param>
        /// <returns></returns>
        public async Task <Tuple <bool, bool> > ProcessAduTransaction(TransactionRequest request, TransactionQueueModel transactionQueueModel, Item item,
                                                                      TransactionPriority priority, ExternalDependencies.BusinessLayer.Models.Facility facility)
        {
            var result = new Tuple <bool, bool>(false, false);

            //PyxisLoad duplicate check
            if (await IsPyxisLoadTransactionExists(request.Patient.DeliverToLocation, facility.Id,
                                                   transactionQueueModel.FormularyId))
            {
                _logger.LogInformation(string.Format(CommonConstants.LoggingMessage.PyxisLoadTransactionExists, request.RequestId));

                //Not updated any existing txn and current txn should be ignored
                result = new Tuple <bool, bool>(true, false);
                return(result);
            }

            //If the current transaction is not of type PYXISLOAD
            if (priority.IsAdu.GetValueOrDefault(false) && !StringHelper.IsEqual(priority.TransactionPriorityCode, Priority.PYXISLOAD.ToString()))
            {
                //Refill duplicate check
                if (StringHelper.IsEqual(priority.TransactionPriorityCode, Priority.PYXISREFILL.ToString()) &&
                    await IsPyxisRefillTransactionExists(request.Patient.DeliverToLocation, facility.Id,
                                                         transactionQueueModel.FormularyId))
                {
                    _logger.LogInformation(string.Format(CommonConstants.LoggingMessage.PyxisRefillTransactionExists, request.RequestId));

                    //Not updated any existing txn and current txn should be ignored
                    result = new Tuple <bool, bool>(true, false);
                    return(result);
                }

                //Get the critlow, stockout or stkout transaction from Database
                var tranQClSo = await GetCriticalLowOrStockOutTransaction(request, item);

                //If the current transaction is refill
                if (StringHelper.IsEqual(priority.TransactionPriorityCode, Priority.PYXISREFILL.ToString()) && tranQClSo != null)
                {
                    result = await UpdateQuantityFromReplenishmentOrder(transactionQueueModel.Quantity.Value, tranQClSo);

                    return(result);
                }

                //If the current transaction is critlow, stockout or stkout
                if (priority != null && IsCriticalLowOrStockoutPriority(priority.TransactionPriorityCode))
                {
                    //Get the refill transaction from Database
                    var tranQRefillDetail = await GetRefillTransaction(request, item);

                    //If there is not transation of type Refill present in Database
                    if (tranQRefillDetail != null)
                    {
                        await ConvertReplenishmentToCriticalLowOrStockOut(tranQRefillDetail, request, priority, transactionQueueModel.Quantity.Value);

                        _logger.LogInformation(string.Format(CommonConstants.LoggingMessage.UpdatedQuantityPriorityAndRequestIdForAduTransaction, transactionQueueModel.Quantity.Value, priority.TransactionPriorityCode, request.RequestId));

                        //Updated one existing txn
                        result = new Tuple <bool, bool>(true, true);
                        return(result);
                    }

                    if (tranQClSo != null)
                    {
                        var priorityResult = await _transactionPriorityManager.GetTransactionPriority(tranQClSo.TranPriorityId);

                        //Case when Critical Low exists in DB
                        if (IsCriticalLowOrStockoutPriority(priority.TransactionPriorityCode) &&
                            StringHelper.IsEqual(priorityResult.TransactionPriorityCode, Priority.PYXISCRITLOW.ToString()))
                        {
                            tranQClSo.Quantity          = transactionQueueModel.Quantity.Value;
                            tranQClSo.QuantityProcessed = tranQClSo.Quantity;

                            tranQClSo.TranPriorityId          = priority.TransactionPriorityId;
                            tranQClSo.StatusChangeDT          = DateTime.Now;
                            tranQClSo.StatusChangeUtcDateTime = DateTime.UtcNow;
                            tranQClSo.IncomingRequestId       = request.RequestId;
                            await _transactionQueueRepository.UpdateTransaction(tranQClSo);

                            //Updated one existing txn
                            result = new Tuple <bool, bool>(true, true);
                        }
                        //Case when Stockout Low exists in DB
                        else if (IsCriticalLowOrStockoutPriority(priority.TransactionPriorityCode) &&
                                 StringHelper.IsEqual(priorityResult.TransactionPriorityCode,
                                                      Priority.PYXISSTOCKOUT.ToString()))
                        {
                            tranQClSo.Quantity          = transactionQueueModel.Quantity.Value;
                            tranQClSo.QuantityProcessed = tranQClSo.Quantity;

                            tranQClSo.StatusChangeDT          = DateTime.Now;
                            tranQClSo.StatusChangeUtcDateTime = DateTime.UtcNow;
                            tranQClSo.IncomingRequestId       = request.RequestId;
                            await _transactionQueueRepository.UpdateTransaction(tranQClSo);

                            //Updated one existing txn
                            result = new Tuple <bool, bool>(true, true);
                        }
                        else
                        {
                            //Not updated any existing txn and current txn should be ignored
                            result = new Tuple <bool, bool>(true, false);
                        }

                        return(result);
                    }

                    return(await EligibleforAduDupDelete(transactionQueueModel, facility, request));
                }
            }

            return(result);
        }