Пример #1
0
        public ActionResult PPS()
        {
            var absnow_context = new ABSNowEntities();
            var context = new IPTV2Entities();
            DateTime registDt = DateTime.Now;

            var customers = absnow_context.TFCNowRetailMigrationTFCTVs.Where(t => t.LicenseEndDate > registDt && t.IsMigrated == 0);
            var customerIds = customers.Select(t => t.Email.ToLower()).ToArray();
            var users = context.Users.Where(u => customerIds.Contains(u.TfcNowUserName.ToLower()));

            var TFCnowUsernames = users.Select(t => t.TfcNowUserName.ToLower()).ToArray();

            var migratedUsers = customers.Where(c => TFCnowUsernames.Contains(c.Email.ToLower())).OrderBy(c => c.Email);
            var ctp = migratedUsers.Count();

            var MigratedUsersWithGoms = migratedUsers.Join(absnow_context.TFCNowRetailMigrationIDs, license => license.TFCnowPackageID, package => package.TFCnowPackageID, (license, package) => new { license, package })
                .Select(m => new { Id = m.license.ID, Email = m.license.Email, LicenseEndDate = m.license.LicenseEndDate, TFCnowPackageId = m.license.TFCnowPackageID, GOMSInternalId = m.package.GOMSInternalID })
                .OrderBy(m => m.Email);

            List<MigratedUser> list = new List<MigratedUser>();
            foreach (var item in MigratedUsersWithGoms)
            {
                var user = context.Users.FirstOrDefault(u => u.TfcNowUserName.ToLower() == item.Email.ToLower());
                if (user == null)
                    throw new TFCtvObjectIsNull("User");
                Product product = context.Products.FirstOrDefault(p => p.GomsProductId == item.GOMSInternalId);
                if (product == null)
                    throw new TFCtvObjectIsNull("Product");

                DateTime endDate = (DateTime)item.LicenseEndDate;
                if (product is ShowSubscriptionProduct)
                {
                    var subscription = (ShowSubscriptionProduct)product;
                    if (subscription.ALaCarteSubscriptionTypeId == 2) // Pay per serye. Extend to 1 year.
                    {
                        endDate = registDt.AddYears(1);
                    }

                    TimeSpan difference = endDate.Subtract(registDt);

                    var category = subscription.Categories.FirstOrDefault();
                    if (category == null)
                        throw new TFCtvObjectIsNull("Category");
                    ShowEntitlement entitlement = null;
                    //Check entitlement for Category/Show
                    var show_entitlement = user.ShowEntitlements.FirstOrDefault(s => s.CategoryId == category.CategoryId);

                    if (show_entitlement != null)
                    {
                        show_entitlement.EndDate = show_entitlement.EndDate.Add(difference);
                        endDate = show_entitlement.EndDate;
                    }
                    else
                    {
                        entitlement = new ShowEntitlement()
                        {
                            EndDate = endDate,
                            Show = category.Show,
                            OfferingId = Global.OfferingId,
                        };
                        user.ShowEntitlements.Add(entitlement);
                    }
                    string reference = String.Format("TNRMTFCTV-{0}", item.Id);

                    EntitlementRequest request = CreateEntitlementRequest(registDt, endDate, category.Product, "cPanel Migration", reference);

                    if (request != null)
                    {
                        if (entitlement != null)
                            entitlement.LatestEntitlementRequest = request;
                        else
                            user.EntitlementRequests.Add(request);
                    }
                    MigrationTransaction transaction = new MigrationTransaction()
                    {
                        Amount = 0,
                        Currency = Global.DefaultCurrency,
                        Date = registDt,
                        OfferingId = Global.OfferingId,
                        Reference = reference,
                        MigratedProductId = (int)product.ProductId
                    };
                    user.Transactions.Add(transaction);

                    list.Add(new MigratedUser()
                    {
                        Id = item.Id,
                        Email = item.Email,
                        LicenseEndDate = item.LicenseEndDate,
                        GOMSInternalId = item.GOMSInternalId,
                        TFCnowPackageId = item.TFCnowPackageId,
                        EntitlementId = show_entitlement != null ? show_entitlement.EntitlementId : entitlement.EntitlementId,
                        EntitlementRequestId = request.EntitlementRequestId,
                        TransactionId = transaction.TransactionId
                    });

                    using (var ctx = new ABSNowEntities())
                    {
                        var taggedLicense = ctx.TFCNowRetailMigrationTFCTVs.FirstOrDefault(t => t.ID == item.Id);
                        if (taggedLicense != null)
                        {
                            taggedLicense.IsMigrated = 1;
                            ctx.SaveChanges();
                        }
                    }
                }
            }

            if (context.SaveChanges() > 0)
            {
                return this.Json(list, JsonRequestBehavior.AllowGet);
            }

            //return this.Json(list, JsonRequestBehavior.AllowGet);
            return this.Json(null, JsonRequestBehavior.AllowGet);
        }
Пример #2
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();
            }
        }