protected override Task <ICommandResult> Handle(UpdateTransactionCommand command)
        {
            List <TransactionDetail> transactionDetailList = new List <TransactionDetail>();
            var transaction = _transactionRepository.Get(command.TransactionId);

            if (transaction != null && transaction.CreatedBy != null && transaction.ChannelId != Channels.Feel)
            {
                var user = _userRepository.GetByAltId(transaction.CreatedBy);
                user.PhoneCode   = command.DeliveryDetail[0].PhoneCode;
                user.PhoneNumber = command.DeliveryDetail[0].PhoneNumber;
                User upadateUserResult = _userRepository.Save(user);
            }
            var     transactionDetails = AutoMapper.Mapper.Map <IEnumerable <FIL.Contracts.DataModels.TransactionDetail> >(_transactionDetailRepository.GetByTransactionId(command.TransactionId));
            var     eventTicketAttributeIds = transactionDetails.Select(s => s.EventTicketAttributeId).Distinct();
            var     eventTicketAttributes = AutoMapper.Mapper.Map <IEnumerable <Contracts.Models.EventTicketAttribute> >(_eventTicketAttributeRepository.GetByIds(eventTicketAttributeIds));
            var     ticketFeeDetails = AutoMapper.Mapper.Map <IEnumerable <Contracts.Models.TicketFeeDetail> >(_ticketFeeDetailRepository.GetByEventTicketAttributeIds(eventTicketAttributeIds));
            var     isItinerary = TransactionType.Regular;
            decimal totalConvenienceCharge = 0, smsCharges = 0, totalServiceCharge = 0, totalDeliveryCharge = 0, grossTicketAmount = 0, transactionFee = 0, transactionFeeValue = 0, transactionFeeValueTypeId = 0, creditCardSurcharge = 0, creditCardSurchargeValue = 0, creditCardSurchargeValueTypeId = 0;

            Channels ChannelID = transaction.ChannelId;

            foreach (var transactionDetail in transactionDetails)
            {
                TransactionDetail transactionDetailMapper = transactionDetail;
                var eventTicketAttribute  = eventTicketAttributes.Where(w => w.Id == transactionDetail.EventTicketAttributeId).FirstOrDefault();
                var ticketFeeDetailList   = ticketFeeDetails.Where(w => w.EventTicketAttributeId == eventTicketAttribute.Id);
                var commandDeliveryDetail = command.DeliveryDetail.Where(w => w.EventTicketAttributeId == eventTicketAttribute.Id).FirstOrDefault();
                grossTicketAmount += transactionDetail.PricePerTicket * transactionDetail.TotalTickets;
            }

            foreach (var transactionDetail in transactionDetails)
            {
                TransactionDetail transactionDetailMapper = transactionDetail;
                if (transactionDetail.TransactionType == TransactionType.Itinerary)
                {
                    isItinerary = TransactionType.Itinerary;
                }
                var eventTicketAttribute = eventTicketAttributes.Where(w => w.Id == transactionDetail.EventTicketAttributeId).FirstOrDefault();
                var ticketFeeDetailList  = ticketFeeDetails.Where(w => w.EventTicketAttributeId == eventTicketAttribute.Id).OrderBy(o => o.FeeId);

                // Convert TicketFeeDetails into geo currency...
                if (ChannelID == Channels.Feel)
                {
                    _geoCurrency.UpdateTicketFeeDetails(ticketFeeDetailList.ToList(), command.TargetCurrencyCode, eventTicketAttribute.CurrencyId);
                }
                var     commandDeliveryDetail = command.DeliveryDetail.Where(w => w.EventTicketAttributeId == eventTicketAttribute.Id).FirstOrDefault();
                decimal prevConvenienceCharges = 0;
                decimal DeliveryCharge = 0, ServiceCharge = 0, ConvenienceCharge = 0;
                decimal convenienceCharges = 0, serviceCharge = 0, pahCharge = 0, unitLevelSMSCharge = 0, unitLevelTransactionFee = 0;
                foreach (var ticketFeeDetail in ticketFeeDetailList)
                {
                    if (ticketFeeDetail.FeeId == (int)FeeType.ConvenienceCharge)
                    {
                        if (ticketFeeDetail.ValueTypeId == (int)ValueTypes.Percentage)
                        {
                            convenienceCharges     = ((ticketFeeDetail.Value * (transactionDetail.PricePerTicket * transactionDetail.TotalTickets)) / 100);
                            prevConvenienceCharges = convenienceCharges;
                        }
                        else if (ticketFeeDetail.ValueTypeId == (int)ValueTypes.Flat)
                        {
                            convenienceCharges     = ticketFeeDetail.Value * transactionDetail.TotalTickets;
                            prevConvenienceCharges = convenienceCharges;
                        }
                    }

                    if (ticketFeeDetail.FeeId == (int)FeeType.SMSCharge)
                    {
                        smsCharges = ticketFeeDetail.Value;
                    }

                    if (ticketFeeDetail.FeeId == (int)FeeType.TransactionFee)
                    {
                        transactionFeeValue       = ticketFeeDetail.Value;
                        transactionFeeValueTypeId = ticketFeeDetail.ValueTypeId;
                    }

                    if (ticketFeeDetail.FeeId == (int)FeeType.CreditCardSurcharge)
                    {
                        creditCardSurchargeValue       = ticketFeeDetail.Value;
                        creditCardSurchargeValueTypeId = ticketFeeDetail.ValueTypeId;
                    }

                    if (transactionFeeValueTypeId == (int)ValueTypes.Percentage)
                    {
                        transactionFee = ((transactionFeeValue * (totalConvenienceCharge + smsCharges)) / 100);
                    }
                    else if (transactionFeeValueTypeId == (int)ValueTypes.Flat)
                    {
                        transactionFee = transactionFeeValue;
                    }

                    if (creditCardSurchargeValueTypeId == (int)ValueTypes.Percentage)
                    {
                        creditCardSurcharge = ((creditCardSurchargeValue * (grossTicketAmount + transactionFee)) / 100);
                    }
                    else if (creditCardSurchargeValue == (int)ValueTypes.Flat)
                    {
                        creditCardSurcharge = creditCardSurchargeValue;
                    }
                    unitLevelSMSCharge      = (smsCharges / transactionDetails.Count());
                    unitLevelTransactionFee = ((transactionFee + creditCardSurcharge) / transactionDetails.Count());
                    ConvenienceCharge       = convenienceCharges + unitLevelSMSCharge + unitLevelTransactionFee;

                    if (ticketFeeDetail.FeeId == (int)FeeType.ServiceCharge)
                    {
                        if (ticketFeeDetail.ValueTypeId == (int)ValueTypes.Percentage)
                        {
                            serviceCharge          = ((ticketFeeDetail.Value * prevConvenienceCharges) / 100);
                            prevConvenienceCharges = 0;
                        }
                        else if (ticketFeeDetail.ValueTypeId == (int)ValueTypes.Flat)
                        {
                            serviceCharge = ticketFeeDetail.Value * transactionDetail.TotalTickets;
                        }
                    }
                    ServiceCharge = serviceCharge;

                    if (commandDeliveryDetail.DeliveryTypeId == DeliveryTypes.PrintAtHome && ticketFeeDetail.FeeId == (int)FeeType.PrintAtHomeCharge)
                    {
                        pahCharge = ticketFeeDetail.Value;
                    }
                    DeliveryCharge = pahCharge;
                }
                transactionDetailMapper.ConvenienceCharges = ConvenienceCharge;
                transactionDetailMapper.ServiceCharge      = ServiceCharge;
                transactionDetailMapper.DeliveryCharges    = DeliveryCharge;
                totalDeliveryCharge    += DeliveryCharge;
                totalServiceCharge     += ServiceCharge;
                totalConvenienceCharge += ConvenienceCharge;
                transactionDetailList.Add(transactionDetailMapper);
            }

            if (transactionFeeValueTypeId == (int)ValueTypes.Percentage)
            {
                transactionFee = ((transactionFeeValue * (totalConvenienceCharge + smsCharges)) / 100);
            }
            else if (transactionFeeValueTypeId == (int)ValueTypes.Flat)
            {
                transactionFee = transactionFeeValue;
            }

            if (creditCardSurchargeValueTypeId == (int)ValueTypes.Percentage)
            {
                creditCardSurcharge = ((creditCardSurchargeValue * (grossTicketAmount + transactionFee)) / 100);
            }
            else if (creditCardSurchargeValue == (int)ValueTypes.Flat)
            {
                creditCardSurcharge = creditCardSurchargeValue;
            }

            if (transactionFee > 0)
            {
                transaction.ConvenienceCharges = transactionFee + creditCardSurcharge;
                transaction.ServiceCharge      = totalServiceCharge;
                transaction.DeliveryCharges    = totalDeliveryCharge;
                transaction.GrossTicketAmount  = grossTicketAmount;
                transaction.NetTicketAmount    = ((grossTicketAmount + transactionFee + creditCardSurcharge) - transaction.DiscountAmount);
            }
            else
            {
                transaction.ConvenienceCharges = totalConvenienceCharge + transactionFee + creditCardSurcharge;
                transaction.ServiceCharge      = totalServiceCharge;
                transaction.DeliveryCharges    = totalDeliveryCharge;
                transaction.GrossTicketAmount  = grossTicketAmount;
                transaction.NetTicketAmount    = ((grossTicketAmount + totalConvenienceCharge + totalServiceCharge + totalDeliveryCharge + transactionFee + creditCardSurcharge) - transaction.DiscountAmount);
            }
            if (ChannelID == Channels.Feel)
            {
                _geoCurrency.UpdateTransactionUpdates(transaction, command.TargetCurrencyCode);
            }
            FIL.Contracts.DataModels.Transaction transactionResult = _transactionRepository.Save(transaction);
            foreach (var transactionDetail in transactionDetailList)
            {
                TransactionDetail transactionDetailResult = _transactionDetailRepository.Save(transactionDetail);
                try
                {
                    if (ChannelID == Channels.Feel)
                    {
                        var currentETA = command.EventTicketAttributeList.Where(s => s.Id == transactionDetail.EventTicketAttributeId && (transactionDetail.TicketTypeId != null ? (TicketType)transactionDetail.TicketTypeId : TicketType.Regular) == s.TicketType).FirstOrDefault();
                        if (currentETA != null && currentETA.GuestDetails != null)
                        {
                            foreach (FIL.Contracts.Commands.Transaction.GuestUserDetail currentGuestDetail in currentETA.GuestDetails)
                            {
                                _saveGuestUserProvider.SaveGuestUsers(currentGuestDetail, transactionDetail);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    _logger.Log(Logging.Enums.LogCategory.Error, e);
                }
            }

            var transactionDeliveryDetails = _transactionDeliveryDetailRepository.GetByTransactionDetailIds(transactionDetails.Select(s => s.Id).Distinct());

            foreach (var deliveryDetail in command.DeliveryDetail)
            {
                var transactionDeliveryDetail = transactionDeliveryDetails.Where(w => w.TransactionDetailId == transactionDetails.Where(c => c.EventTicketAttributeId == deliveryDetail.EventTicketAttributeId).Select(s => s.Id).FirstOrDefault()).FirstOrDefault();
                if (transactionDeliveryDetail == null)
                {
                    _transactionDeliveryDetailRepository.Save(new TransactionDeliveryDetail
                    {
                        TransactionDetailId = transactionDetails.Where(w => w.EventTicketAttributeId == deliveryDetail.EventTicketAttributeId).Select(s => s.Id).FirstOrDefault(),
                        DeliveryTypeId      = deliveryDetail.DeliveryTypeId,
                        PickupBy            = Convert.ToInt16(string.IsNullOrWhiteSpace(deliveryDetail.RepresentativeFirstName) ? 0 : 1),
                        SecondaryName       = $"{deliveryDetail.FirstName} {deliveryDetail.LastName}",
                        SecondaryContact    = $"{deliveryDetail.PhoneCode}-{deliveryDetail.PhoneNumber}",
                        SecondaryEmail      = deliveryDetail.DeliveryTypeId == DeliveryTypes.Courier ? deliveryDetail.Email : deliveryDetail.Email,
                        PickUpAddress       = command.PickUpAddress
                    });
                }
                else
                {
                    transactionDeliveryDetail.SecondaryName    = $"{deliveryDetail.FirstName} {deliveryDetail.LastName}";
                    transactionDeliveryDetail.SecondaryContact = $"{deliveryDetail.PhoneCode}-{deliveryDetail.PhoneNumber}";
                    transactionDeliveryDetail.SecondaryEmail   = deliveryDetail.DeliveryTypeId == DeliveryTypes.Courier ? deliveryDetail.Email : deliveryDetail.Email;
                    transactionDeliveryDetail.PickUpAddress    = command.PickUpAddress;
                    _transactionDeliveryDetailRepository.Save(transactionDeliveryDetail);
                }
            }
            var transactionresult = _transactionRepository.Get(command.TransactionId);

            UpdateTransactionCommandResult updateTransactionCommandResult = new UpdateTransactionCommandResult();

            updateTransactionCommandResult.Id                 = transactionresult.Id;
            updateTransactionCommandResult.CurrencyId         = transactionresult.CurrencyId;
            updateTransactionCommandResult.GrossTicketAmount  = transactionresult.GrossTicketAmount;
            updateTransactionCommandResult.DeliveryCharges    = transactionresult.DeliveryCharges;
            updateTransactionCommandResult.ConvenienceCharges = transactionresult.ConvenienceCharges;
            updateTransactionCommandResult.ServiceCharge      = transactionresult.ServiceCharge;
            updateTransactionCommandResult.DiscountAmount     = transactionresult.DiscountAmount;
            updateTransactionCommandResult.NetTicketAmount    = transactionresult.NetTicketAmount;

            return(Task.FromResult <ICommandResult>(updateTransactionCommandResult));
        }
Пример #2
0
        public async Task <ASIBookingResponse> saveASIData(ASIBooking aSIBooking)
        {
            var            transactionData    = _transactionRepository.Get(aSIBooking.TransactionId);
            var            transactionDetails = _transactionDetailRepository.GetByTransactionId(aSIBooking.TransactionId);
            List <Visitor> visitors           = new List <Visitor>();
            List <ASIHash> aSIHashes          = new List <ASIHash>();
            RootObject     rootObject         = new RootObject();

            foreach (FIL.Contracts.DataModels.TransactionDetail currentTransactionDetail in transactionDetails)
            {
                var clientETAData = aSIBooking.EventTicketAttributeList.Where(s => s.Id == currentTransactionDetail.EventTicketAttributeId && (TicketType)currentTransactionDetail.TicketTypeId == s.TicketType).FirstOrDefault();
                if (clientETAData != null)
                {
                    foreach (FIL.Contracts.Commands.Transaction.GuestUserDetail currentGuestDetail in clientETAData.GuestDetails)
                    {
                        _saveGuestUserProvider.SaveGuestUsers(currentGuestDetail, currentTransactionDetail);
                    }

                    _aSITransactionDetailTimeSlotIdMappingRepository.Save(new FIL.Contracts.DataModels.ASI.ASITransactionDetailTimeSlotIdMapping
                    {
                        EventTimeSlotMappingId = (long)clientETAData.VisitTimeId,
                        TransactionDetailId    = currentTransactionDetail.Id,
                        IsEnabled  = true,
                        CreatedUtc = DateTime.UtcNow,
                        UpdatedUtc = DateTime.UtcNow
                    });

                    _transactionDeliveryDetailRepository.Save(new TransactionDeliveryDetail
                    {
                        TransactionDetailId = currentTransactionDetail.Id,
                        DeliveryTypeId      = DeliveryTypes.PrintAtHome,
                        PickupBy            = 1,
                        SecondaryName       = "Zoonga",
                        SecondaryContact    = "Zoonga",
                        SecondaryEmail      = transactionData.EmailId,
                        PickUpAddress       = "Zoonga"
                    });
                }
            }
            // This is separate loop for API request payload..... I don't think we need to manage data from above loop... Better to keep it separate
            foreach (FIL.Contracts.DataModels.TransactionDetail currentTransactionDetail in transactionDetails)
            {
                var guestData          = _guestDetailRepository.GetByTransactionDetailId(currentTransactionDetail.Id);
                var asiTicketAttribute = aSIBooking.EventTicketAttributeList.Where(s => s.Id == currentTransactionDetail.EventTicketAttributeId).FirstOrDefault();
                if (guestData.Any())
                {
                    var currentETA        = _eventTicketAttributeRepository.Get(currentTransactionDetail.EventTicketAttributeId);
                    var currentETD        = _eventTicketDetailRepository.Get(currentETA.EventTicketDetailId);
                    var currentTc         = _ticketCategoryRepository.Get((int)currentETD.TicketCategoryId);
                    var currentED         = _eventDetailRepository.Get(currentETD.EventDetailId);
                    var currentE          = _eventRepository.Get(currentED.EventId);
                    var asiETableMapping  = _aSIMonumentEventTableMappingRepository.GetByEventId(currentE.Id);
                    var asiMonument       = _aSIMonumentRepository.Get(asiETableMapping.ASIMonumentId);
                    var asiMonumentDetail = _aSIMonumentDetailRepository.GetByNameAndMonumentId(currentED.Name, asiMonument.Id);
                    var asiTimeSlot       = _aSITransactionDetailTimeSlotIdMappingRepository.GetByTransactionDetailId(currentTransactionDetail.Id);
                    var eventTimeSlot     = _eventTimeSlotMappingRepository.Get(asiTimeSlot.EventTimeSlotMappingId);
                    //api.key |[monument.code | optional | date | age | country | amount] |[monument.code | optional | date | age | country | amount] | api.salt
                    foreach (FIL.Contracts.DataModels.GuestDetail currentGuestDetail in guestData)
                    {
                        Visitor     visitor     = new Visitor();
                        ASIHash     aSIHash     = new ASIHash();
                        Identity    indentity   = new Identity();
                        Monument    monument    = new Monument();
                        Nationality nationality = new Nationality();
                        Timeslot    timeslot    = new Timeslot();
                        visitor.Identity          = indentity;
                        visitor.Monument          = monument;
                        visitor.Monument.Timeslot = timeslot;
                        visitor.Nationality       = nationality;
                        var custerDocType = _customerDocumentTypeRepository.Get(currentGuestDetail.CustomerDocumentTypeId);
                        visitor.Age                  = Convert.ToInt32(currentGuestDetail.Age);
                        visitor.VisitorId            = currentGuestDetail.Id.ToString();
                        visitor.Amount               = currentTransactionDetail.PricePerTicket;
                        visitor.Gender               = currentGuestDetail.GenderId.ToString();
                        visitor.Name                 = currentGuestDetail.FirstName + " " + currentGuestDetail.LastName;
                        visitor.Date                 = ((System.DateTime)currentTransactionDetail.VisitDate).ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'");
                        visitor.Identity.Type        = custerDocType.DocumentType;
                        visitor.Identity.No          = currentGuestDetail.DocumentNumber;
                        visitor.Monument.Code        = "TAJ";
                        visitor.Monument.Optional    = asiMonumentDetail.IsOptional;
                        visitor.Monument.Main        = true;
                        visitor.Monument.Timeslot.Id = eventTimeSlot.TimeSlotId;
                        visitor.Nationality.Group    = currentTc.Name;
                        visitor.Nationality.Country  = asiTicketAttribute == null ? "India" : asiTicketAttribute.ASIUserSelectedCountry;
                        visitors.Add(visitor);

                        aSIHash.Code           = asiMonument.Code;
                        aSIHash.IsOptional     = asiMonumentDetail.IsOptional;
                        aSIHash.Date           = (System.DateTime)currentTransactionDetail.VisitDate;
                        aSIHash.Nationality    = currentTc.Name;
                        aSIHash.Age            = Convert.ToInt32(currentGuestDetail.Age);
                        aSIHash.Amount         = currentTransactionDetail.PricePerTicket;
                        aSIHash.IdentityType   = "Passport";
                        aSIHash.IdentityNumber = "SADFG";
                        aSIHash.VisitorId      = currentGuestDetail.Id.ToString();
                        aSIHashes.Add(aSIHash);
                    }
                }
            }

            var ticketHash = "";

            foreach (FIL.Contracts.Models.ASI.ASIHash currentHash in aSIHashes)
            {
                TimeZoneInfo INDIAN_ZONE;
                try
                {
                    INDIAN_ZONE = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
                }
                catch (Exception e)
                {
                    INDIAN_ZONE = TimeZoneInfo.FindSystemTimeZoneById("Asia/Kolkata");
                }
                DateTime indianTime = TimeZoneInfo.ConvertTimeFromUtc(currentHash.Date, INDIAN_ZONE);
                var      time       = indianTime.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'");
                ticketHash = ticketHash + "[" + currentHash.Code + "|" + (!currentHash.IsOptional).ToString().ToLower() + "|" + currentHash.IsOptional.ToString().ToLower() + "|" + indianTime.ToString("yyyy-MM-dd") + "|" + currentHash.Age + "|" + currentHash.Nationality + "|" + currentHash.Amount.ToString("0.00") + "]" + "|";
                //ticketHash = ticketHash + "[" + "TAJ" + "|" + time + "|" + currentHash.IdentityType + "|" + currentHash.IdentityNumber + "|" + currentHash.Age + "|" + currentHash.Nationality + "|" + currentHash.Amount.ToString("0.00") + "|" + currentHash.VisitorId + "]" + "|";
            }
            var bookingHashRequestData = _settings.GetConfigSetting <string>(SettingKeys.Integration.ASI.APIKey) + "|" + ticketHash + _settings.GetConfigSetting <string>(SettingKeys.Integration.ASI.APISalt);
            var bookingHash            = GenerateSHA512Hash(bookingHashRequestData);

            rootObject.TransactionId = aSIBooking.TransactionId.ToString();
            rootObject.Hash          = bookingHash.ToLower();
            rootObject.Visitors      = visitors;
            rootObject.Email         = transactionData.EmailId == "*****@*****.**" ? transactionData.PhoneNumber : transactionData.EmailId;
            UserCardDetail userCardDetail = _userCardDetailRepository.GetByUserCardNumber(string.Empty, aSIBooking.UserId);

            if (userCardDetail == null)
            {
                UserCardDetail obj = new UserCardDetail
                {
                    UserId      = aSIBooking.UserId,
                    AltId       = new Guid(),
                    NameOnCard  = string.Empty,
                    CardNumber  = string.Empty,
                    ExpiryMonth = 12,
                    ExpiryYear  = 2020,
                    CardTypeId  = CardType.None
                };
                userCardDetail = _userCardDetailRepository.Save(obj);
            }

            _transactionPaymentDetailRepository.Save(new TransactionPaymentDetail // Create transaction payment request before calling booking POST API request...
            {
                TransactionId    = aSIBooking.TransactionId,
                PaymentOptionId  = PaymentOptions.None,
                PaymentGatewayId = PaymentGateway.Payu,
                UserCardDetailId = userCardDetail.Id,
                RequestType      = "Charge Posting",
                Amount           = transactionData.NetTicketAmount.ToString(),
                PayConfNumber    = "",
                PaymentDetail    = "{\"Request\":" + Newtonsoft.Json.JsonConvert.SerializeObject(rootObject) + "{\"hashparameter\":" + bookingHashRequestData + "}}",
            });
            var baseAddress = _settings.GetConfigSetting <string>(SettingKeys.Integration.ASI.IntegrationRoot).ToString() + "" + _settings.GetConfigSetting <string>(SettingKeys.Integration.ASI.APIKey).ToString();

            return(new ASIBookingResponse
            {
                RootObject = rootObject,
                ReturnUrl = baseAddress
            });
        }