コード例 #1
0
ファイル: License.cs プロジェクト: Natsui31/keyhub
        /// <summary>
        /// Constructor that creates a license based on a provided SKU 
        /// </summary>
        /// <param name="licensedSku">SKU this license is based on</param>
        /// <param name="owner">Owning customer</param>
        /// <param name="purchaser">Purchasing customer</param>
        public License(SKU licensedSku, Customer owner, Customer purchaser)
        {
            this.SkuId = licensedSku.SkuId;
            this.LicenseIssued = DateTime.Now;
            this.PurchasingCustomerId = purchaser.ObjectId;
            this.OwningCustomerId = owner.ObjectId;
            this.OwnerName = owner.Name;

            if (licensedSku.LicenseDuration.HasValue)
            {
                this.LicenseExpires = DateTime.Now.AddMonths(licensedSku.LicenseDuration.Value);
            }
        }
コード例 #2
0
ファイル: BasketWrapper.cs プロジェクト: Natsui31/keyhub
        public void ExecuteCheckout(Customer purchasingCustomer, Customer owningCustomer)
        {
            var currentUser = context.GetUser(HttpContext.Current.User.Identity);

            //Add PurchasingCustomer if none existing
            if (purchasingCustomer.ObjectId == new Guid())
            {
                context.Customers.Add(purchasingCustomer);
                context.SaveChanges();
                if (!currentUser.IsVendorAdmin)
                {
                    context.UserCustomerRights.Add(new UserCustomerRight
                    {
                        RightObject = purchasingCustomer,
                        RightId = EditEntityMembers.Id,
                        UserId = currentUser.UserId
                    });
                    context.SaveChanges();
                }
            }

            //Add OwningCustomer if none existing
            if (owningCustomer != purchasingCustomer)
            {
                if (owningCustomer.ObjectId == new Guid())
                {
                    context.Customers.Add(owningCustomer);
                    context.SaveChanges();
                    if (!currentUser.IsVendorAdmin)
                    {
                        context.UserCustomerRights.Add(new UserCustomerRight
                        {
                            RightObject = owningCustomer,
                            RightId = EditEntityInfo.Id,
                            UserId = currentUser.UserId
                        });
                        context.SaveChanges();
                    }
                }
            }

            //Create licenses for every transactionitem
            foreach (var item in Transaction.TransactionItems)
            {
                if (item.License == null)
                {
                    var newLicense = new Model.License(item.Sku, owningCustomer, purchasingCustomer);
                    context.Licenses.Add(newLicense);

                    item.License = newLicense;
                }
            }
            context.SaveChanges();

            var existingCustomerApps = (from x in context.Licenses
                join y in context.LicenseCustomerApps on x.ObjectId equals y.LicenseId
                where x.OwningCustomerId == owningCustomer.ObjectId
                select y.CustomerApp).ToList();

            //Add to any existing apps
            if (existingCustomerApps.Any())
            {
                foreach (var customerApp in existingCustomerApps)
                {
                    customerApp.AddLicenses((from x in Transaction.TransactionItems select x.License.ObjectId));
                }
                context.SaveChanges();
            }
            else
            {
                //Create default application containing all licenses
                var newCustomerApp = new CustomerApp()
                {
                    ApplicationName = owningCustomer.Name + "_Application"
                };
                context.CustomerApps.Add(newCustomerApp);
                newCustomerApp.AddLicenses((from x in Transaction.TransactionItems select x.License.ObjectId));
                newCustomerApp.CustomerAppKeys.Add(new CustomerAppKey());
            }
            Transaction.Status = TransactionStatus.Complete;
            context.SaveChanges();
        }