public MainResponse GetGlobalCode(string categoryName)
        {
            var globalCodeResponse = _globalCodeRepository.GetCodes(categoryName);

            if (globalCodeResponse.totalRecords != 0)
            {
                _mainResponse.Success = true;
                _mainResponse.GlobalCodeMainResponse = globalCodeResponse;
            }
            else
            {
                _mainResponse.Message = Constants.NO_RECORD_FOUND;
                _mainResponse.Success = false;
            }
            return(_mainResponse);
        }
        public MainResponse AddFinancialTransaction(AddFinancialTransactionRequest addFinancialTransactionRequest, string actionBy)
        {
            var feeType = _globalCodeRepository.GetCodes("FeeType");

            int additionalFeeId = feeType.globalCodeResponse.Where(x => x.CodeName == "Additional Program").Select(x => x.GlobalCodeId).FirstOrDefault();
            int sponsorRefund   = feeType.globalCodeResponse.Where(x => x.CodeName == "Ad Sponsor Refund").Select(x => x.GlobalCodeId).FirstOrDefault();

            if (addFinancialTransactionRequest.FeeTypeId == additionalFeeId)
            {
                var additionalfinancialTransaction = new ExhibitorPaymentDetail()
                {
                    ExhibitorId   = addFinancialTransactionRequest.ExhibitorId,
                    PayDate       = Convert.ToDateTime(addFinancialTransactionRequest.PayDate),
                    TimeFrameType = "",
                    FeeTypeId     = addFinancialTransactionRequest.FeeTypeId,
                    Amount        = addFinancialTransactionRequest.Amount,
                    AmountPaid    = addFinancialTransactionRequest.AmountPaid,
                    RefundAmount  = addFinancialTransactionRequest.RefundAmount,
                    CreatedBy     = actionBy,
                    CreatedDate   = DateTime.Now
                };
                _exhibitorPaymentDetailRepository.Add(additionalfinancialTransaction);
                _mainResponse.Message = Constants.FINANCIAL_TRANSACTION_ADDED;
                _mainResponse.Success = true;
                return(_mainResponse);
            }
            if (addFinancialTransactionRequest.FeeTypeId == sponsorRefund)
            {
                var sponsorTransaction = new ExhibitorPaymentDetail()
                {
                    ExhibitorId   = addFinancialTransactionRequest.ExhibitorId,
                    PayDate       = Convert.ToDateTime(addFinancialTransactionRequest.PayDate),
                    FeeTypeId     = addFinancialTransactionRequest.FeeTypeId,
                    TimeFrameType = "",
                    Amount        = 0,
                    AmountPaid    = 0,
                    RefundAmount  = addFinancialTransactionRequest.RefundAmount,
                    CreatedBy     = actionBy,
                    CreatedDate   = DateTime.Now
                };
                _exhibitorPaymentDetailRepository.Add(sponsorTransaction);
                _mainResponse.Message = Constants.FINANCIAL_TRANSACTION_ADDED;
                _mainResponse.Success = true;
                return(_mainResponse);
            }
            var financialTransaction = new ExhibitorPaymentDetail()
            {
                ExhibitorId   = addFinancialTransactionRequest.ExhibitorId,
                PayDate       = Convert.ToDateTime(addFinancialTransactionRequest.PayDate),
                FeeTypeId     = addFinancialTransactionRequest.FeeTypeId,
                TimeFrameType = addFinancialTransactionRequest.TimeFrameType,
                Amount        = addFinancialTransactionRequest.Amount,
                AmountPaid    = addFinancialTransactionRequest.AmountPaid,
                RefundAmount  = addFinancialTransactionRequest.RefundAmount,
                CreatedBy     = actionBy,
                CreatedDate   = DateTime.Now
            };

            _exhibitorPaymentDetailRepository.Add(financialTransaction);
            _mainResponse.Message = Constants.FINANCIAL_TRANSACTION_ADDED;
            _mainResponse.Success = true;
            return(_mainResponse);
        }
        public MainResponse AddUpdateSponsorDistribution(SponsorDistributionRequest request)
        {
            int idNumber = 0;

            Int32.TryParse(request.TypeId, out idNumber);
            if (idNumber <= 0)
            {
                _mainResponse.Message = Constants.INVALID_ID_NUMBER;
                _mainResponse.Success = false;
                return(_mainResponse);
            }
            var sponsortypes = _GlobalCodeRepository.GetCodes("SponsorTypes");

            var classsponsortype = sponsortypes.globalCodeResponse.Where(x => x.CodeName == "Class").FirstOrDefault();

            if (request.SponsorDistributionId <= 0)
            {
                decimal sponsorAmount = Convert.ToDecimal(_SponsorRepository.GetSingle(x => x.SponsorId == request.SponsorId).AmountReceived);

                decimal sponsorexhibitorPaid    = _SponsorExhibitorRepository.GetAll(x => x.SponsorId == request.SponsorId && x.IsDeleted == false).Select(x => x.SponsorAmount).Sum();
                decimal sponsordistributionPaid = _SponsorDistributionRepository.GetAll(x => x.SponsorId == request.SponsorId && x.IsDeleted == false).Select(x => x.TotalDistribute).Sum();

                decimal checkSponsorAmount = sponsorAmount - (sponsorexhibitorPaid + sponsordistributionPaid);
                if (checkSponsorAmount <= 0)
                {
                    _mainResponse.Message = Constants.SPONSOR_NO_FUND;
                    _mainResponse.Success = false;
                    return(_mainResponse);
                }
                decimal checkAddedAmount = checkSponsorAmount - request.TotalDistribute;
                if (checkAddedAmount < 0)
                {
                    _mainResponse.Message = Constants.SPONSOR_NO_FUND;
                    _mainResponse.Success = false;
                    return(_mainResponse);
                }
                var sponsorType = _GlobalCodeRepository.GetSingle(x => x.GlobalCodeId == request.SponsorTypeId);

                if (sponsorType.CodeName == "Ad" || sponsorType.CodeName == "Cooler" || sponsorType.CodeName == "Patron")
                {
                    var sponsorAdExist = _SponsorDistributionRepository.GetSingle(x => x.TypeId == Convert.ToString(idNumber) &&
                                                                                  x.SponsorTypeId != classsponsortype.GlobalCodeId && x.IsDeleted == false);



                    if (sponsorAdExist != null)
                    {
                        _mainResponse.Message = Constants.AD_NUMBER_EXIST;
                        _mainResponse.Success = false;
                        return(_mainResponse);
                    }
                }
                request.TypeId = Convert.ToString(idNumber);
                var sponsorDistribution = _Mapper.Map <SponsorDistributions>(request);
                sponsorDistribution.IsActive    = true;
                sponsorDistribution.IsDeleted   = false;
                sponsorDistribution.CreatedDate = DateTime.Now;
                _SponsorDistributionRepository.Add(sponsorDistribution);
                _mainResponse.Message = Constants.RECORD_ADDED_SUCCESS;
                _mainResponse.Success = true;
            }
            else
            {
                var sponsorDistribution = _SponsorDistributionRepository.GetSingle(x => x.SponsorDistributionId == request.SponsorDistributionId);
                if (sponsorDistribution != null && sponsorDistribution.SponsorDistributionId > 0)
                {
                    sponsorDistribution.SponsorId       = request.SponsorId;
                    sponsorDistribution.SponsorTypeId   = request.SponsorTypeId;
                    sponsorDistribution.TotalDistribute = request.TotalDistribute;
                    sponsorDistribution.AdTypeId        = Convert.ToInt32(request.AdTypeId);
                    sponsorDistribution.TypeId          = request.TypeId;
                    sponsorDistribution.ModifiedDate    = DateTime.Now;

                    _SponsorDistributionRepository.Update(sponsorDistribution);
                    _mainResponse.Message = Constants.RECORD_UPDATE_SUCCESS;
                    _mainResponse.Success = true;
                }
                else
                {
                    _mainResponse.Message = Constants.NO_RECORD_EXIST_WITH_ID;
                    _mainResponse.Success = false;
                }
            }
            return(_mainResponse);
        }