Exemplo n.º 1
0
        private static void AddTfcEverywhereEntitlement(IPTV2Entities context, int GomsProductId, string expiryDate, int serviceId, User user, int GomsTransactionId = 0)
        {
            DateTime registDt = DateTime.Now;
            var product = context.Products.FirstOrDefault(p => p.GomsProductId == GomsProductId && p.GomsProductQuantity == 1);
            if (product != null)
            {
                ProductPrice productPrice;
                try
                {
                    productPrice = product.ProductPrices.FirstOrDefault(i => i.CurrencyCode == user.Country.CurrencyCode);
                }
                catch (Exception)
                {
                    productPrice = product.ProductPrices.FirstOrDefault(i => i.CurrencyCode == GlobalConfig.DefaultCurrency);
                }

                //Create Purchase
                Purchase purchase = ContextHelper.CreatePurchase(registDt, "TFC Everywhere");
                //Create Purchase Item
                PurchaseItem purchaseItem = ContextHelper.CreatePurchaseItem(user.UserId, product, productPrice);

                var productPackage = context.ProductPackages.FirstOrDefault(p => p.ProductId == product.ProductId);
                //Create Entitlement & EntitlementRequest
                Entitlement entitlement = user.PackageEntitlements.FirstOrDefault(i => i.PackageId == productPackage.PackageId);

                user.Purchases.Add(purchase);
                user.PurchaseItems.Add(purchaseItem);


                if (entitlement != null)
                {
                    entitlement.EndDate = Convert.ToDateTime(expiryDate);
                    EntitlementRequest request = new EntitlementRequest()
                    {
                        DateRequested = registDt,
                        StartDate = registDt,
                        EndDate = entitlement.EndDate,
                        Product = productPackage.Product,
                        Source = "TFC Everywhere",
                        ReferenceId = GomsTransactionId.ToString()
                    };
                    user.EntitlementRequests.Add(request);
                    entitlement.LatestEntitlementRequest = request;
                    purchaseItem.EntitlementRequest = request;
                }
                else
                {
                    EntitlementRequest request = new EntitlementRequest()
                    {
                        DateRequested = registDt,
                        StartDate = registDt,
                        EndDate = Convert.ToDateTime(expiryDate),
                        Product = productPackage.Product,
                        Source = "TFC Everywhere",
                        ReferenceId = GomsTransactionId.ToString()
                    };

                    PackageEntitlement pkg_entitlement = new PackageEntitlement()
                    {
                        EndDate = Convert.ToDateTime(expiryDate),
                        Package = (IPTV2_Model.Package)productPackage.Package,
                        OfferingId = GlobalConfig.offeringId,
                        LatestEntitlementRequest = request
                    };

                    user.PackageEntitlements.Add(pkg_entitlement);
                    user.EntitlementRequests.Add(request);
                    purchaseItem.EntitlementRequest = request;
                }
            }
        }
Exemplo n.º 2
0
 private EpisodeEntitlement CreateEpisodeEntitlement(EntitlementRequest request, EpisodeSubscriptionProduct subscription, ProductEpisode episode, DateTime registDt)
 {
     EpisodeEntitlement entitlement = new EpisodeEntitlement()
     {
         EndDate = GetEntitlementEndDate(subscription.Duration, subscription.DurationType, registDt),
         Episode = (Episode)episode.Episode,
         OfferingId = offeringId,
         LatestEntitlementRequest = request
     };
     return entitlement;
 }
Exemplo n.º 3
0
 private EntitlementRequest CreateEntitlementRequest(DateTime registDt, DateTime endDate, Product product, string source, string reference, DateTime startDate)
 {
     EntitlementRequest request = new EntitlementRequest()
     {
         DateRequested = registDt,
         EndDate = endDate,
         StartDate = startDate,
         Product = product,
         Source = source,
         ReferenceId = reference
     };
     return request;
 }
Exemplo n.º 4
0
 private static PackageEntitlement CreatePackageEntitlement(EntitlementRequest request, PackageSubscriptionProduct subscription, ProductPackage package, DateTime registDt)
 {
     PackageEntitlement entitlement = new PackageEntitlement()
     {
         EndDate = GetEntitlementEndDate(subscription.Duration, subscription.DurationType, registDt),
         Package = (Package)package.Package,
         OfferingId = offeringId,
         LatestEntitlementRequest = request
     };
     return entitlement;
 }
Exemplo n.º 5
0
 private static ShowEntitlement CreateShowEntitlement(EntitlementRequest request, ShowSubscriptionProduct subscription, ProductShow show, DateTime registDt)
 {
     ShowEntitlement entitlement = new ShowEntitlement()
     {
         EndDate = GetEntitlementEndDate(subscription.Duration, subscription.DurationType, registDt),
         Show = (Show)show.Show,
         OfferingId = offeringId,
         LatestEntitlementRequest = request
     };
     return entitlement;
 }
Exemplo n.º 6
0
 private static ShowEntitlement CreateShowEntitlement(EntitlementRequest request, ShowSubscriptionProduct subscription, ProductShow show, DateTime registDt)
 {
     var currentDt = registDt;
     if (subscription.BreakingDate != null)
         registDt = (DateTime)subscription.BreakingDate > currentDt ? (DateTime)subscription.BreakingDate : currentDt;
     ShowEntitlement entitlement = new ShowEntitlement()
     {
         EndDate = MyUtility.getEntitlementEndDate(subscription.Duration, subscription.DurationType, registDt),
         Show = (Show)show.Show,
         OfferingId = GlobalConfig.offeringId,
         LatestEntitlementRequest = request
     };
     return entitlement;
 }
Exemplo n.º 7
0
 private static EpisodeEntitlement CreateEpisodeEntitlement(EntitlementRequest request, EpisodeSubscriptionProduct subscription, ProductEpisode episode, DateTime registDt)
 {
     var currentDt = registDt;
     if (subscription.BreakingDate != null)
         registDt = (DateTime)subscription.BreakingDate > currentDt ? (DateTime)subscription.BreakingDate : currentDt;
     EpisodeEntitlement entitlement = new EpisodeEntitlement()
     {
         EndDate = MyUtility.getEntitlementEndDate(subscription.Duration, subscription.DurationType, registDt),
         Episode = (Episode)episode.Episode,
         OfferingId = GlobalConfig.offeringId,
         LatestEntitlementRequest = request
     };
     return entitlement;
 }
Exemplo n.º 8
0
        private static bool Upgrade(IPTV2Entities context, System.Guid userId, Product product, System.Guid recipientUserId, int? cpId)
        {
            if (userId == recipientUserId)
            {
                if (product is PackageSubscriptionProduct)
                {
                    PackageSubscriptionProduct subscription = (PackageSubscriptionProduct)product;

                    DateTime registDt = DateTime.Now;
                    User user = context.Users.FirstOrDefault(u => u.UserId == new Guid(HttpContext.Current.User.Identity.Name));
                    string CurrencyCode = MyUtility.GetCurrencyOrDefault(user.Country != null ? user.Country.Code : GlobalConfig.DefaultCountry);
                    var offering = context.Offerings.FirstOrDefault(o => o.OfferingId == GlobalConfig.offeringId);
                    var currentSubscription = user.GetSubscribedProducts(offering).FirstOrDefault(s => s.ProductId == cpId);
                    if (currentSubscription == null)
                        return false;

                    //Check if its upgrade to the product being bought
                    if (currentSubscription.ProductGroup.UpgradeableToProductGroups().Contains(subscription.ProductGroup))
                    {
                        //if true, deactivate the package based on product

                        //Get all product Id inside the current subscription
                        var list = currentSubscription.ProductGroup.SubscriptionProducts.Select(e => e.ProductId);

                        //Get the Packagea
                        var productPackage = context.ProductPackages.FirstOrDefault(p => list.Contains(p.ProductId));

                        //Get Package Entitlement based on the Package Id
                        var packageEntitlement = user.PackageEntitlements.Where(e => e.PackageId == productPackage.PackageId);
                        foreach (var entitlement in packageEntitlement)
                        {
                            //Compute for remaining days.
                            var remainingDaysToBeAdded = GetEquivalentPremiumDuration(context, CurrencyCode, entitlement);
                            //Deactivate the package
                            DateTime originalExpirationDate = entitlement.EndDate;
                            entitlement.EndDate = registDt;

                            //DateTime newExpirationDate = MyUtility.getEntitlementEndDate((int)remainingDaysToBeAdded, "d", registDt);
                            DateTime newExpirationDate = MyUtility.getEntitlementEndDate((int)remainingDaysToBeAdded, "d", GetPackageEndDateIfAvailable(user, subscription, registDt));

                            //Create an UpgradeTransaction
                            UpgradeTransaction transaction = CreateUpgradeTransaction(originalExpirationDate, currentSubscription.ProductId, newExpirationDate, product.ProductId, CurrencyCode, registDt);
                            user.Transactions.Add(transaction);

                            //Add the new package

                            foreach (var package in subscription.Packages)
                            {
                                PackageEntitlement currentPackage = user.PackageEntitlements.FirstOrDefault(p => p.PackageId == package.PackageId);

                                EntitlementRequest request = new EntitlementRequest()
                                {
                                    DateRequested = registDt,
                                    EndDate = newExpirationDate,
                                    Product = product,
                                    Source = "SUBSCRIPTION UPGRADE",
                                    ReferenceId = "SUBSCRIPTION UPGRADE"
                                };

                                if (currentPackage != null)
                                {
                                    currentPackage.LatestEntitlementRequest = request;
                                    currentPackage.EndDate = newExpirationDate;
                                    request.EndDate = newExpirationDate;
                                }
                                else
                                {
                                    PackageEntitlement pkgEntitlement = new PackageEntitlement()
                                    {
                                        EndDate = newExpirationDate,
                                        Package = (Package)package.Package,
                                        OfferingId = GlobalConfig.offeringId,
                                        LatestEntitlementRequest = request
                                    };

                                    request.EndDate = pkgEntitlement.EndDate;
                                    user.PackageEntitlements.Add(pkgEntitlement);
                                }

                                user.EntitlementRequests.Add(request);
                            }
                        }
                    }

                    if (context.SaveChanges() > 0)
                        return true;
                }
                return false;
            }
            return false;
        }
Exemplo n.º 9
0
 private static PackageEntitlement CreatePackageEntitlement(EntitlementRequest request, PackageSubscriptionProduct subscription, ProductPackage package, DateTime registDt)
 {
     var currentDt = registDt;
     if (subscription.BreakingDate != null)
         registDt = (DateTime)subscription.BreakingDate > currentDt ? (DateTime)subscription.BreakingDate : currentDt;
     PackageEntitlement entitlement = new PackageEntitlement()
     {
         EndDate = MyUtility.getEntitlementEndDate(subscription.Duration, subscription.DurationType, registDt),
         Package = (Package)package.Package,
         OfferingId = GlobalConfig.offeringId,
         LatestEntitlementRequest = request
     };
     return entitlement;
 }
Exemplo n.º 10
0
 private static PackageEntitlement CreatePackageEntitlement(EntitlementRequest request, PackageSubscriptionProduct subscription, ProductPackage package, DateTime endDate, int offeringId)
 {
     PackageEntitlement entitlement = new PackageEntitlement()
     {
         EndDate = endDate,
         Package = (IPTV2_Model.Package)package.Package,
         OfferingId = offeringId,
         LatestEntitlementRequest = request
     };
     return entitlement;
 }
Exemplo n.º 11
0
        public TFCtvResponse CreateTFCtvEverywhereEntitlement(ReqCreateTFCtvEverywhereEntitlement req)
        {
            TFCtvResponse resp = null;
            DateTime registDt = DateTime.Now;
            string ip = ConfigurationManager.AppSettings["IpWhiteList"];
            string[] IpAddresses = ip.Split(';');
            bool isWhitelisted = IpAddresses.Contains(HttpContext.Current.Request.UserHostAddress);
            if (!isWhitelisted)
            {
                resp = new TFCtvResponse() { Code = -3001, Message = "Ip address is unauthorized." }; return resp;
            }

            if (!HttpContext.Current.Request.IsLocal)
            {
                //Check SoapHeader
                if (Credentials.Username.ToLower() != SoapHeaderUsername || Credentials.Password != SoapHeaderPassword)
                { resp = new TFCtvResponse() { Code = -3002, Message = "Call is unauthorized." }; return resp; }
            }

            if (req == null)
                resp = new TFCtvResponse() { Code = -3003, Message = "Request parameter is empty." };
            else
            {
                if (req.GomsCustomerId == null || req.GomsTransactionDate == null || req.GomsTransactionId == null || req.GomsProductId == null || req.GomsProductQuantity == null || req.GomsTFCEverywhereEndDate == null || req.GomsTFCEverywhereStartDate == null || req.EmailAddress == null)
                {
                    resp = new TFCtvResponse() { Code = -3004, Message = "Missing required fields." };
                    return resp;
                }

                if (req.GomsProductId != null)
                    if (req.GomsProductId <= 0)
                    {
                        resp = new TFCtvResponse() { Code = -1000, Message = "GomsProductId is not applicable." };
                        return resp;
                    }

                var context = new IPTV2Entities();
                if (isProduction)
                    context.Database.Connection.ConnectionString = IPTV2EntitiesAzureConnectionString;


                var product = context.Products.FirstOrDefault(p => p.GomsProductId == req.GomsProductId && p.GomsProductQuantity == req.GomsProductQuantity);
                if (product == null)
                {
                    resp = new TFCtvResponse() { Code = -1007, Message = "Product does not exist." };
                    return resp;
                }

                var user = context.Users.FirstOrDefault(u => u.GomsCustomerId == req.GomsCustomerId && String.Compare(u.EMail, req.EmailAddress, true) == 0);
                if (user == null)
                {
                    resp = new TFCtvResponse() { Code = -1001, Message = "User does not exist." };
                    return resp;
                }

                //if (String.Compare(user.EMail, req.EmailAddress, true) != 0)
                //{
                //    resp = new TFCtvResponse() { Code = -1001, Message = "User does not exist." };
                //    return resp;
                //}

                if (user.Country == null)
                {
                    resp = new TFCtvResponse() { Code = -1002, Message = "User country is not valid." };
                    return resp;
                }
                //else // Do we need to check for Japan/US only?
                //{
                //    var countries = TVECountryWhitelist.Split(',');
                //    if (!countries.Contains(user.Country.Code))
                //    {
                //        resp = new TFCtvResponse() { Code = -1002, Message = "User country is not valid." };
                //        return resp;
                //    }
                //}

                if (user.IsTVEverywhere == null)
                {
                    resp = new TFCtvResponse() { Code = -1003, Message = "User is not applicable for TVEverywhere." };
                    return resp;
                }
                else
                {
                    if (user.IsTVEverywhere == false)
                    {
                        resp = new TFCtvResponse() { Code = -1003, Message = "User is not applicable for TVEverywhere." };
                        return resp;
                    }
                }

                var match = System.Text.RegularExpressions.Regex.Match(req.Reference, "Update|Deactivate|Activate|Change Plan", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                if (!match.Success)
                {
                    if (user.Transactions.Count(t => t.GomsTransactionId == req.GomsTransactionId) > 0)
                    {
                        resp = new TFCtvResponse() { Code = -1006, Message = "Transaction already exists in TFC.tv." };
                        return resp;
                    }
                }


                if (user != null)
                {
                    //user.LastUpdated = registDt;
                    user.LastUpdated = (DateTime)req.GomsTransactionDate;

                    if (user.IsTVEverywhere != null) // Check if set to true
                        if (user.IsTVEverywhere == false)
                            user.IsTVEverywhere = true;

                    ProductPrice productPrice;
                    try
                    {
                        productPrice = product.ProductPrices.FirstOrDefault(i => i.CurrencyCode == user.Country.CurrencyCode);
                    }
                    catch (Exception)
                    {
                        productPrice = product.ProductPrices.FirstOrDefault(i => i.CurrencyCode == DefaultCurrencyCode);
                    }

                    //Create Purchase
                    Purchase purchase = CreatePurchase((DateTime)req.GomsTransactionDate, "TFC Everywhere");
                    //Create Purchase Item
                    PurchaseItem purchaseItem = CreatePurchaseItem(user.UserId, product, productPrice);

                    var productPackage = context.ProductPackages.FirstOrDefault(p => p.ProductId == product.ProductId);
                    //Create Entitlement & EntitlementRequest
                    Entitlement entitlement = user.PackageEntitlements.FirstOrDefault(i => i.PackageId == productPackage.PackageId);

                    //DateTime endDate = registDt;
                    DateTime endDate = (DateTime)req.GomsTFCEverywhereEndDate;


                    if (entitlement != null)
                    {
                        //if (entitlement.EndDate > registDt)
                        //    entitlement.EndDate = entitlement.EndDate.Add(difference);
                        //else
                        //    entitlement.EndDate = registDt.Add(difference);

                        entitlement.EndDate = (DateTime)req.GomsTFCEverywhereEndDate;
                        EntitlementRequest request = new EntitlementRequest()
                        {
                            //DateRequested = registDt,
                            DateRequested = (DateTime)req.GomsTransactionDate,
                            StartDate = (DateTime)req.GomsTFCEverywhereStartDate,
                            EndDate = entitlement.EndDate,
                            Product = productPackage.Product,
                            Source = "TFC Everywhere",
                            ReferenceId = req.Reference
                        };

                        endDate = entitlement.EndDate;
                        user.EntitlementRequests.Add(request);
                        entitlement.LatestEntitlementRequest = request;
                    }
                    else
                    {
                        EntitlementRequest request = new EntitlementRequest()
                        {
                            //DateRequested = registDt,
                            DateRequested = (DateTime)req.GomsTransactionDate,
                            StartDate = (DateTime)req.GomsTFCEverywhereStartDate,
                            EndDate = (DateTime)req.GomsTFCEverywhereEndDate,
                            Product = productPackage.Product,
                            Source = "TFC Everywhere",
                            ReferenceId = req.Reference
                        };

                        PackageEntitlement pkg_entitlement = new PackageEntitlement()
                        {
                            EndDate = (DateTime)req.GomsTFCEverywhereEndDate,
                            Package = (IPTV2_Model.Package)productPackage.Package,
                            OfferingId = offeringId,
                            LatestEntitlementRequest = request
                        };

                        //endDate = item.LicenseEndDate;

                        user.PackageEntitlements.Add(pkg_entitlement);
                    }

                    //Create TFCtvEverywhereTransaction
                    TfcEverywhereTransaction transaction = new TfcEverywhereTransaction()
                    {
                        GomsTFCEverywhereEndDate = (DateTime)req.GomsTFCEverywhereEndDate,
                        GomsTFCEverywhereStartDate = (DateTime)req.GomsTFCEverywhereStartDate,
                        GomsTFCEverywhereSubscriptionId = "N/A",
                        GomsTFCEverywhereServiceId = "N/A",
                        GomsTransactionDate = req.GomsTransactionDate,
                        GomsTransactionId = req.GomsTransactionId,
                        Amount = productPrice.Amount,
                        Currency = productPrice.CurrencyCode,
                        //Date = registDt,
                        Date = (DateTime)req.GomsTransactionDate,
                        Reference = req.Reference,
                        OfferingId = offeringId,
                        StatusId = 1
                    };

                    user.Transactions.Add(transaction);
                }

                try
                {
                    if (context.SaveChanges() > 0)
                        resp = new TFCtvResponse() { Code = 0, Message = String.Format("Successfully created entitlement ending {0} to GomsCustomerId {1}.", req.GomsTFCEverywhereEndDate, req.GomsCustomerId) };
                    else
                        resp = new TFCtvResponse() { Code = -1005, Message = "Unable to create TFC.tv Everywhere entitlement." };
                }
                catch (Exception e)
                {
                    resp = new TFCtvResponse() { Code = -3000, Message = e.InnerException.Message };
                }

            }
            return resp;
        }
Exemplo n.º 12
0
 private static ShowEntitlement CreateShowEntitlement(DateTime startDt, int CategoryId, int Duration, string DurationType, EntitlementRequest request)
 {
     ShowEntitlement entitlement = new ShowEntitlement()
      {
          EndDate = GetEntitlementEndDate(Duration, DurationType, startDt),
          CategoryId = CategoryId,
          OfferingId = offeringId,
          LatestEntitlementRequest = request
      };
     return entitlement;
 }
Exemplo n.º 13
0
        private static void RunThis()
        {
            DateTime registDt = DateTime.Now;
            var absnow_context = new ABSNowEntities();
            var context = new IPTV2Entities();

            var processed_licenses = absnow_context.LicensePurchasedProcesseds.ToList();
            Console.WriteLine(String.Format("Processed Licenses: {0}", processed_licenses.Count()));
            //var platinum_users = absnow_context.CustomerLicensePurchased1.Where(i => i.PackageID == TFCnowPlatinumProductId && i.LicenseEndDate > registDt) // && i.EmailAddress == "*****@*****.**")
            //    .OrderByDescending(i => i.LicenseEndDate)
            //    .AsEnumerable()
            //    .GroupBy(i => i.EmailAddress)
            //     .Select(i => new
            //     {
            //         EmailAddress = i.Key,
            //         PackageID = i.Max(ii => ii.PackageID),
            //         CustomerID = i.Max(ii => ii.CustomerID),
            //         LicenseEndDate = i.Max(ii => ii.LicenseEndDate)
            //     }).ToList();

            var processedLicenses = processed_licenses.Select(i => i.LicensePurchasedID).ToArray();
            absnow_context.CommandTimeout = 120;
            var platinum_users = absnow_context.vwCustomerLicensePurchaseds.Where(i => !processedLicenses.Contains(i.LicensePurchasedID)).ToList();
            Console.WriteLine(String.Format("Platinum Users: {0}", platinum_users.Count()));
            var productPlatinum = context.Products.FirstOrDefault(i => i.ProductId == PlatinumProductId);
            var PremiumPackage = context.ProductPackages.FirstOrDefault(i => i.PackageId == PremiumPackageId);

            ProductPrice productPrice;
            if (platinum_users != null)
            {
                int counter = 1;
                foreach (var item in platinum_users)
                {
                    Console.WriteLine(String.Format("{1}. PROCESSING {0}", item.EmailAddress, counter));
                    //var purchaseId = absnow_context.CustomerLicensePurchased1.Where(i => i.EmailAddress.ToLower() == item.EmailAddress.ToLower() && i.PackageID == TFCnowPlatinumProductId).OrderByDescending(i => i.LicenseEndDate).First();
                    Console.WriteLine(String.Format("LicensePurchase ID: {0}", item.LicensePurchasedID));
                    counter++;
                    //if (!processed_licenses.Select(i => i.LicensePurchasedID).Contains(purchaseId.LicensePurchasedID))
                    //{
                    var difference = item.LicenseEndDate.Subtract(registDt);

                    var user = context.Users.FirstOrDefault(i => i.TfcNowUserName.ToLower() == item.EmailAddress.ToLower());
                    if (user != null)
                    {
                        Console.WriteLine(String.Format("TAGGED TFCnow {0} TO TFCtv {1}", item.EmailAddress, user.EMail));
                        try
                        {
                            productPrice = productPlatinum.ProductPrices.FirstOrDefault(i => i.CurrencyCode == user.Country.CurrencyCode);
                        }
                        catch (Exception)
                        {
                            productPrice = productPlatinum.ProductPrices.FirstOrDefault(i => i.CurrencyCode == DefaultCurrencyCode);
                        }

                        user.LastUpdated = registDt;


                        //Create Purchase
                        Purchase purchase = CreatePurchase(registDt, "TFC.tv Everywhere");
                        //Create Purchase Item
                        PurchaseItem purchaseItem = CreatePurchaseItem(user.UserId, productPlatinum, productPrice);

                        //Create Entitlement & EntitlementRequest
                        Entitlement entitlement = user.PackageEntitlements.FirstOrDefault(i => i.PackageId == PremiumPackageId);

                        DateTime endDate = registDt;

                        if (entitlement != null)
                        {

                            if (entitlement.EndDate > registDt)
                                entitlement.EndDate = entitlement.EndDate.Add(difference);
                            else
                                entitlement.EndDate = registDt.Add(difference);

                            //entitlement.EndDate = item.LicenseEndDate;

                            EntitlementRequest request = new EntitlementRequest()
                            {
                                DateRequested = registDt,
                                EndDate = entitlement.EndDate,
                                Product = PremiumPackage.Product,
                                Source = "TFC.tv Everywhere",
                                ReferenceId = String.Format("{0}", item.LicensePurchasedID)
                                //ReferenceId = String.Format("{0}", String.Empty)
                            };

                            endDate = entitlement.EndDate;

                            user.EntitlementRequests.Add(request);
                        }
                        else
                        {

                            EntitlementRequest request = new EntitlementRequest()
                            {
                                DateRequested = registDt,
                                EndDate = item.LicenseEndDate,
                                Product = PremiumPackage.Product,
                                Source = "TFC.tv Everywhere",
                                ReferenceId = String.Format("{0}", item.LicensePurchasedID)
                                //ReferenceId = String.Format("{0}", String.Empty)
                            };

                            PackageEntitlement pkg_entitlement = new PackageEntitlement()
                            {
                                EndDate = item.LicenseEndDate,
                                Package = (IPTV2_Model.Package)PremiumPackage.Package,
                                OfferingId = OfferingId,
                                LatestEntitlementRequest = request
                            };

                            //endDate = item.LicenseEndDate;

                            user.PackageEntitlements.Add(pkg_entitlement);

                        }

                        //Create TFCtvEverywhereTransaction
                        TfcEverywhereTransaction transaction = new TfcEverywhereTransaction()
                        {
                            GomsTFCEverywhereEndDate = endDate,
                            GomsTFCEverywhereStartDate = registDt,
                            GomsTFCEverywhereSubscriptionId = "N/A",
                            GomsTFCEverywhereServiceId = "N/A",
                            Amount = productPrice.Amount,
                            Currency = productPrice.CurrencyCode,
                            Date = registDt,
                            Reference = String.Format("TVE-{0}", item.TransactionID),
                            OfferingId = OfferingId,
                            StatusId = 1
                        };

                        user.Transactions.Add(transaction);

                        //Console.WriteLine(String.Format("TFCtv {0} LICENSE END DATE UPDATED", user.EMail));

                        if (context.SaveChanges() > 0)
                        {
                            Console.WriteLine(String.Format("TFCtv {0} LICENSE END DATE UPDATED", user.EMail));
                            LicensePurchasedProcessed receipt = new LicensePurchasedProcessed();
                            receipt.LicensePurchasedID = item.LicensePurchasedID;
                            receipt.CreateDate = registDt;
                            absnow_context.LicensePurchasedProcesseds.AddObject(receipt);
                            absnow_context.SaveChanges();
                        }
                    }
                    else
                        Console.WriteLine(String.Format("{0} NOT FOUND. SKIPPING.", item.EmailAddress));

                    //}
                }

                //absnow_context.SaveChanges();
            }
        }
Exemplo n.º 14
0
        public ActionResult Migrate()
        {
            if (GlobalConfig.IsTVERegistrationEnabled)
                return RedirectToAction("Index", "Home");

            Dictionary<string, object> collection = new Dictionary<string, object>();
            ErrorCodes errorCode = ErrorCodes.UnknownError;
            string errorMessage = MyUtility.getErrorMessage(ErrorCodes.UnknownError);
            DateTime registDt = DateTime.Now;
            collection = MyUtility.setError(errorCode, errorMessage);

            if (TempData["TFCnowCustomer"] == null)
                return RedirectToAction("Index", "Home");

            if (!MyUtility.isUserLoggedIn())
                return RedirectToAction("Index", "Home");

            Customer customer = (Customer)TempData["TFCnowCustomer"];

            var absnow_context = new ABSNowEntities();

            string TFCnowPackageIds = GlobalConfig.TFCnowPackageIds;
            var packageids = MyUtility.StringToIntList(TFCnowPackageIds);
            var context = new IPTV2Entities();
            var userId = new Guid(User.Identity.Name);
            User user = context.Users.FirstOrDefault(u => u.UserId == userId);
            if (user == null)
                return RedirectToAction("Index", "Migration");



            //Tag account
            user.TfcNowUserName = customer.EmailAddress;
            user.LastUpdated = registDt;
            context.SaveChanges();

            bool isLicenseMigrated = false;
            bool isWalletMigrated = false;
            //Migrate Wallet
            var balance = absnow_context.NCashWalletBalanceTables.FirstOrDefault(u => u.userId.ToLower() == customer.EmailAddress.ToLower());

            if (balance != null)
            {
                if (balance.cashBalance > 0)
                {
                    var transfer_amount = Forex.Convert(context, GlobalConfig.DefaultCurrency, user.Country.CurrencyCode, balance.cashBalance);

                    var userWallet = user.UserWallets.FirstOrDefault(u => u.Currency == user.Country.CurrencyCode && u.IsActive == true);
                    if (userWallet == null)
                    {
                        userWallet = new UserWallet()
                        {
                            Balance = transfer_amount,
                            IsActive = true,
                            Currency = user.Country.CurrencyCode,
                        };
                        user.UserWallets.Add(userWallet);
                    }
                    else
                        userWallet.Balance += transfer_amount;

                    //Create Paypal transaction

                    var ppTransaction = new PaypalReloadTransaction()
                    {
                        Amount = transfer_amount,
                        Currency = user.Country.CurrencyCode,
                        Date = registDt,
                        Reference = String.Format("M-{0}", customer.CustomerID),
                        OfferingId = GlobalConfig.offeringId,
                        UserWallet = userWallet,
                        StatusId = GlobalConfig.Visible
                    };

                    user.Transactions.Add(ppTransaction);
                    if (context.SaveChanges() > 0)
                        isWalletMigrated = true;
                }
            }

            var licenses = absnow_context.LicensePurchaseds.Where(l => l.CustomerID == customer.CustomerID && packageids.Contains(l.PackageID) && l.LicenseEndDate > registDt).OrderByDescending(l => l.LicenseEndDate);

            //Migrate Licenses
            if (licenses.Count() == 0)
            {
                if (isWalletMigrated)
                {
                    user.TfcNowUserName = customer.EmailAddress;
                    context.SaveChanges();
                }

                //  return RedirectToAction("Complete", "Migration");
                //collection = MyUtility.setError(ErrorCodes.UnknownError, "No licenses to migrate.");
            }
            else
            {
                bool isPremiumProcessed = false;
                bool isLiteProcessed = false;
                bool isMovieChannelProcessed = false;
                bool isLiveStreamProcessed = false;

                var TFCnowPremium = MyUtility.StringToIntList(GlobalConfig.TFCnowPremium);
                var TFCnowLite = MyUtility.StringToIntList(GlobalConfig.TFCnowLite);
                var TFCnowMovieChannel = MyUtility.StringToIntList(GlobalConfig.TFCnowMovieChannel);
                var TFCnowLiveStream = MyUtility.StringToIntList(GlobalConfig.TFCnowLiveStream);

                List<LicenseDisplay> display = new List<LicenseDisplay>();

                var premiumLicense = GetLicense(absnow_context, TFCnowPremium, licenses, 1427);
                display.Add(premiumLicense);
                display.Add(GetLicense(absnow_context, TFCnowLite, licenses, 1425));
                display.Add(GetLicense(absnow_context, TFCnowMovieChannel, licenses, 45));
                if (premiumLicense == null)
                    display.Add(GetLicense(absnow_context, TFCnowLiveStream, licenses, 1427));

                display.RemoveAll(item => item == null);

                foreach (var item in display)
                {
                    int TFCtvPackageId = 0;
                    int TFCtvProductId = 0;

                    if (item.LicenseEndDate > registDt)
                    {
                        var difference = item.LicenseEndDate.Subtract(registDt);
                        PackageEntitlement entitlement = null;
                        if (TFCnowPremium.Contains(item.PackageId))
                        {
                            if (!isPremiumProcessed)
                            {
                                entitlement = user.PackageEntitlements.FirstOrDefault(p => p.PackageId == GlobalConfig.premiumId);
                                isPremiumProcessed = true;
                                TFCtvPackageId = GlobalConfig.premiumId;
                            }
                        }
                        else if (TFCnowLite.Contains(item.PackageId))
                        {
                            if (!isLiteProcessed)
                            {
                                if (user.CountryCode == GlobalConfig.DefaultCountry)
                                {
                                    entitlement = user.PackageEntitlements.FirstOrDefault(p => p.PackageId == GlobalConfig.premiumId);
                                    TFCtvPackageId = GlobalConfig.premiumId;
                                }

                                else
                                {
                                    entitlement = user.PackageEntitlements.FirstOrDefault(p => p.PackageId == GlobalConfig.liteId);
                                    TFCtvPackageId = GlobalConfig.liteId;
                                }

                                isLiteProcessed = true;
                            }
                        }
                        else if (TFCnowMovieChannel.Contains(item.PackageId))
                        {
                            if (!isMovieChannelProcessed)
                            {
                                entitlement = user.PackageEntitlements.FirstOrDefault(p => p.PackageId == GlobalConfig.movieId);
                                isMovieChannelProcessed = true;
                                TFCtvPackageId = GlobalConfig.movieId;
                            }
                        }

                        else if (TFCnowLiveStream.Contains(item.PackageId))
                        {
                            if (!isLiveStreamProcessed)
                            {
                                if (!isPremiumProcessed)
                                {
                                    entitlement = user.PackageEntitlements.FirstOrDefault(p => p.PackageId == GlobalConfig.premiumId);
                                    isLiveStreamProcessed = true;
                                    TFCtvPackageId = GlobalConfig.premiumId;
                                }
                            }
                        }

                        else
                        {
                            //Provide load
                        }
                        if (TFCtvPackageId > 0)
                        {
                            ProductPackage package = context.ProductPackages.FirstOrDefault(p => p.PackageId == TFCtvPackageId);
                            TFCtvProductId = package.Product.ProductId;

                            if (entitlement != null)
                            {
                                if (entitlement.EndDate > registDt)
                                    entitlement.EndDate = entitlement.EndDate.Add(difference);
                                else
                                    entitlement.EndDate = registDt.Add(difference);


                                EntitlementRequest request = new EntitlementRequest()
                                {
                                    DateRequested = registDt,
                                    StartDate = item.LicenseStartDate,
                                    EndDate = entitlement.EndDate,
                                    Product = package.Product,
                                    Source = "Entitlement Transfer",
                                    ReferenceId = item.LicensePurchaseId.ToString()
                                };
                                entitlement.LatestEntitlementRequest = request; // ADDED DECEMBER 06, 2012
                                user.EntitlementRequests.Add(request);
                            }

                            else
                            {
                                EntitlementRequest request = new EntitlementRequest()
                                {
                                    DateRequested = registDt,
                                    StartDate = item.LicenseStartDate,
                                    EndDate = item.LicenseEndDate,
                                    Product = package.Product,
                                    Source = "Entitlement Transfer",
                                    ReferenceId = item.LicensePurchaseId.ToString()
                                };

                                user.EntitlementRequests.Add(request);
                                PackageEntitlement pkg_entitlement = new PackageEntitlement()
                                {
                                    EndDate = item.LicenseEndDate,
                                    Package = (IPTV2_Model.Package)package.Package,
                                    OfferingId = GlobalConfig.offeringId,
                                    LatestEntitlementRequest = request
                                };
                                user.PackageEntitlements.Add(pkg_entitlement);
                            }
                        }
                    }

                    if (TFCtvProductId > 0)
                    {
                        MigrationTransaction transaction = new MigrationTransaction()
                        {
                            Amount = 0,
                            Currency = GlobalConfig.DefaultCurrency,
                            Date = registDt,
                            OfferingId = GlobalConfig.offeringId,
                            Reference = item.LicensePurchaseId.ToString(),
                            MigratedProductId = TFCtvProductId,
                            StatusId = GlobalConfig.Visible
                        };
                        user.Transactions.Add(transaction);
                    }
                }
                if (context.SaveChanges() > 0)
                    isLicenseMigrated = true;
            }

            bool isTingiMigrated = false;
            var tingi = absnow_context.TFCNowRetailMigrationIDs.Select(i => new { i.TFCnowPackageID, i.GOMSInternalID });
            var tingi_licenses = absnow_context.LicensePurchaseds.Where(l => l.CustomerID == customer.CustomerID && tingi.Select(i => i.TFCnowPackageID).Contains(l.PackageID) && l.LicenseEndDate > registDt).OrderByDescending(l => l.LicenseEndDate);
            if (tingi_licenses.Count() == 0)
            {
                if (isLicenseMigrated || isWalletMigrated)
                {
                    user.TfcNowUserName = customer.EmailAddress;
                    context.SaveChanges();
                }

                return RedirectToAction("Complete", "Migration");
            }
            else
            {
                foreach (var item in tingi_licenses)
                {
                    var diff = item.LicenseEndDate.Subtract(registDt);

                    var GomsProductId = tingi.FirstOrDefault(t => t.TFCnowPackageID == item.PackageID);
                    var TFCtvProduct = context.Products.FirstOrDefault(p => p.GomsProductId == GomsProductId.GOMSInternalID);

                    if (TFCtvProduct != null)
                    {
                        if (TFCtvProduct is ShowSubscriptionProduct)
                        {
                            var showSubscription = (ShowSubscriptionProduct)TFCtvProduct;
                            ShowEntitlement se = user.ShowEntitlements.FirstOrDefault(s => s.Show.CategoryId == showSubscription.Categories.First().CategoryId);
                            if (se != null)
                            {
                                if (se.EndDate > registDt)
                                    se.EndDate = showSubscription.ALaCarteSubscriptionTypeId == 2 ? se.EndDate.AddYears(1) : se.EndDate.Add(diff);
                                else
                                    se.EndDate = showSubscription.ALaCarteSubscriptionTypeId == 2 ? registDt.AddYears(1) : registDt.Add(diff);

                                EntitlementRequest request = new EntitlementRequest()
                                {
                                    DateRequested = registDt,
                                    EndDate = se.EndDate,
                                    Product = TFCtvProduct,
                                    Source = "Entitlement Transfer",
                                    ReferenceId = item.LicensePurchasedID.ToString()
                                };
                                user.EntitlementRequests.Add(request);
                            }
                            else
                            {
                                EntitlementRequest request = new EntitlementRequest()
                                {
                                    DateRequested = registDt,
                                    EndDate = showSubscription.ALaCarteSubscriptionTypeId == 2 ? registDt.AddYears(1) : registDt.Add(diff),
                                    Product = TFCtvProduct,
                                    Source = "Entitlement Transfer",
                                    ReferenceId = item.LicensePurchasedID.ToString()
                                };

                                user.EntitlementRequests.Add(request);
                                ShowEntitlement show_entitlement = new ShowEntitlement()
                                {
                                    EndDate = showSubscription.ALaCarteSubscriptionTypeId == 2 ? registDt.AddYears(1) : registDt.Add(diff),
                                    Show = showSubscription.Categories.First().Show,
                                    OfferingId = GlobalConfig.offeringId,
                                    LatestEntitlementRequest = request
                                };
                                user.ShowEntitlements.Add(show_entitlement);
                            }

                            MigrationTransaction transaction = new MigrationTransaction()
                            {
                                Amount = 0,
                                Currency = GlobalConfig.DefaultCurrency,
                                Date = registDt,
                                OfferingId = GlobalConfig.offeringId,
                                Reference = item.LicensePurchasedID.ToString(),
                                MigratedProductId = TFCtvProduct.ProductId,
                                StatusId = GlobalConfig.Visible
                            };
                            user.Transactions.Add(transaction);
                        }
                    }
                }
                if (context.SaveChanges() > 0)
                    isTingiMigrated = true;
            }

            if (isLicenseMigrated || isWalletMigrated || isTingiMigrated)
            {
                user.TfcNowUserName = customer.EmailAddress;
                user.LastUpdated = registDt;
            }
            else
            {
                user.TfcNowUserName = customer.EmailAddress;
                user.LastUpdated = registDt;
            }

            if (context.SaveChanges() > 0)
            {
                return RedirectToAction("Complete", "Migration");
            }
            return Content(MyUtility.buildJson(collection), "application/json");
        }