Exemplo n.º 1
0
        /// <summary>
        /// Create a new Credit card
        /// </summary>
        /// <returns></returns>
        public override Guid Create(CreditCard record)
        {
            DataModel            dataModel   = new DataModel();
            DataModelTransaction transaction = DataModelTransaction.Current;
            Guid blotterId = this.FindContainingBlotter(transaction, record.ConsumerId.GetValueOrDefault());
            Guid tenantId  = PersistenceHelper.GetTenantForEntity(
                transaction,
                blotterId);

            if (!DataModelFilters.HasAccess(transaction, TradingSupport.UserId, blotterId, AccessRight.Write))
            {
                throw new SecurityAccessDeniedException("The current user does not write permission to the selected blotter");
            }

            Guid ccId = Guid.NewGuid();

            ////Create a entry in credit card
            dataModel.CreateCreditCard(
                record.AccountBalance,
                record.AccountNumber,
                record.ConsumerId.GetValueOrDefault(),
                ccId,
                record.DebtHolder,
                record.DebtRuleId,
                Guid.NewGuid().ToString(),
                record.AccountNumber,
                record.OriginalAccountNumber,
                tenantId);

            return(ccId);
        }
        /// <summary>
        /// Create a new Debt Holder Record
        /// </summary>
        /// <returns></returns>
        internal Guid Create(EntityRow existingTrust, CreditCardRow existingCard)
        {
            DataModelTransaction dataModelTransaction = DataModelTransaction.Current;
            DataModel            dataModel            = new DataModel();
            Guid    tenantId = PersistenceHelper.GetTenantForEntity(dataModelTransaction, this.Record.Blotter);
            Guid    consumerId;
            Guid    entityId            = Guid.Empty;
            Guid    existingCardId      = Guid.Empty;
            Int64   existingCardVersion = 0;
            Boolean updateExistingCard  = false;

            if (existingTrust != null)
            {
                entityId = existingTrust.EntityId;
                existingTrust.ReleaseReaderLock(dataModelTransaction.TransactionId);
            }

            if (existingCard != null)
            {
                existingCard.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout);
                existingCardId      = existingCard.CreditCardId;
                existingCardVersion = existingCard.RowVersion;
                if (TradingSupport.IsColumnOld(existingCard, "AccountBalance", this.Record.AccountBalance) ||
                    TradingSupport.IsColumnOld(existingCard, "AccountNumber", this.Record.AccountCode) ||
                    TradingSupport.IsColumnOld(existingCard, "DebtHolder", this.Record.DebtHolder) ||
                    TradingSupport.IsColumnOld(existingCard, "OriginalAccountNumber", this.Record.OriginalAccountNumber))
                {
                    updateExistingCard = true;
                }
            }

            // We need write access to the containing blotter in order to add a record to it.
            if (!TradingSupport.HasAccess(dataModelTransaction, this.Record.Blotter, AccessRight.Write))
            {
                throw new SecurityException("Current user does not have write access to the selected blotter");
            }
#if false
            if (existingTrust != null &&
                !TradingSupport.HasAccess(dataModelTransaction, entityId, AccessRight.Write))
            {
                throw new SecurityException("Current user does not have write access to the selected consumer");
            }
#endif
            if (existingTrust == null)
            {
                consumerId = this.CreateConsumer();
            }
            else
            {
                consumerId = this.UpdateConsumer(existingTrust);
            }

            if (existingCard == null)
            {
                dataModel.CreateCreditCard(
                    this.Record.AccountBalance,
                    this.Record.AccountCode,
                    consumerId,
                    Guid.NewGuid(),
                    this.Record.DebtHolder,
                    null,
                    null,
                    this.Record.AccountCode,
                    StringUtilities.CleanUpAlphaNumericString(this.Record.OriginalAccountNumber),
                    tenantId);
            }
            else if (updateExistingCard)
            {
                dataModel.UpdateCreditCard(
                    this.Record.AccountBalance,
                    this.Record.AccountCode,
                    null,
                    existingCardId,
                    new object[] { existingCardId },
                    this.Record.DebtHolder,
                    null,
                    null,
                    this.Record.AccountCode,
                    StringUtilities.CleanUpAlphaNumericString(this.Record.OriginalAccountNumber),
                    existingCardVersion,
                    null);
            }

            return(consumerId);
        }
        /// <summary>
        /// Create a new consumer.
        /// </summary>
        /// <returns>The ConsumerId of the consumer.</returns>
        private Guid CreateConsumer()
        {
            DataModel            dataModel            = new DataModel();
            DataModelTransaction dataModelTransaction = DataModelTransaction.Current;
            Guid       userId         = TradingSupport.UserId;
            Guid       tenantId       = PersistenceHelper.GetTenantForEntity(DataModelTransaction.Current, this.Record.Blotter);
            Guid       entityId       = Guid.NewGuid();
            Guid       consumerId     = Guid.NewGuid();
            Guid       creditCardId   = Guid.NewGuid();
            Guid       workingOrderId = Guid.NewGuid();
            CountryRow country;
            Guid       countryId;
            Guid?      provinceId = null;
            EntityRow  dollars;
            Guid       dollarsId;
            TypeRow    type;
            Guid       typeId;
            ImageRow   image;
            Guid       imageId;
            DateTime   currentUTCTime = DateTime.UtcNow;

            country = TradingSupport.FindCountryByKey(
                this.Record.ConfigurationId,
                "FK_Country_Security",
                new object[] { this.Record.CountryCode });
            countryId = country.CountryId;
            country.ReleaseReaderLock(dataModelTransaction.TransactionId);

            if (this.Record.ProvinceCode != null)
            {
                ProvinceRow province = TradingSupport.FindProvinceByKey(
                    this.Record.ConfigurationId,
                    "FK_Province_Consumer",
                    new object[] { this.Record.ProvinceCode });
                provinceId = province.ProvinceId;
                province.ReleaseReaderLock(dataModelTransaction.TransactionId);
            }

            dollars = TradingSupport.FindEntityByKey(
                this.Record.ConfigurationId,
                "FK_Security_WorkingOrder_SettlementId",
                new object[] { this.Record.Currency });
            dollarsId = dollars.EntityId;
            dollars.ReleaseReaderLock(dataModelTransaction.TransactionId);

            image = TradingSupport.FindImageByKey(
                this.Record.ConfigurationId,
                "FK_Image_Entity",
                new object[] { "OBJECT" });
            imageId = image.ImageId;
            image.ReleaseReaderLock(dataModelTransaction.TransactionId);

            type = TradingSupport.FindTypeByKey(
                this.Record.ConfigurationId,
                "FK_Type_Entity",
                new object[] { "CONSUMER TRUST" });
            typeId = type.TypeId;
            type.ReleaseReaderLock(dataModelTransaction.TransactionId);

            dataModel.CreateEntity(
                currentUTCTime,
                null,
                entityId,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                this.Record.CustomerCode,
                imageId,
                false,
                false,
                currentUTCTime,
                this.Record.SavingsEntityCode,
                tenantId,
                typeId);

            dataModel.CreateSecurity(
                null,
                countryId,
                null,
                null,
                null,
                1,
                1,
                entityId,
                this.Record.AccountCode,
                tenantId,
                VolumeCategoryMap.FromCode(VolumeCategory.Unknown));

            dataModel.CreateConsumer(
                this.Record.Address1,
                this.Record.Address2,
                this.Record.BankAccountNumber,
                this.Record.BankRoutingNumber,
                this.Record.City,
                consumerId,
                this.Record.DateOfBirth != null ? (object)this.Record.DateOfBirth.Value : null,
                null,
                null,
                this.Record.FirstName,
                this.Record.IsEmployed != null ? (object)this.Record.IsEmployed.Value : null,
                this.Record.LastName,
                this.Record.MiddleName,
                StringUtilities.CleanUpAlphaNumericString(this.Record.PhoneNumber),
                this.Record.PostalCode,
                provinceId,
                this.Record.Salutation,
                StringUtilities.CleanUpAlphaNumericString(this.Record.SocialSecurityNumber),
                this.Record.Suffix);
            dataModel.CreateConsumerTrust(
                consumerId,
                entityId,
                null,
                null,
                this.Record.SavingsAccount,
                this.Record.SavingsBalance,
                this.Record.Tag,
                tenantId,
                this.Record.VendorCode);

            //If this not found, there will be an exception. Let the exception propagate up.
            dataModel.CreateWorkingOrder(
                null,
                this.Record.Blotter,
                currentUTCTime,
                userId,
                CrossingMap.FromCode(Crossing.AlwaysMatch),
                null,
                null,
                null,
                null,
                true,
                true,
                true,
                null,
                DateTime.UtcNow,
                TradingSupport.UserId,
                OrderTypeMap.FromCode(OrderType.Market),
                entityId,
                DateTime.UtcNow,
                dollarsId,
                SideMap.FromCode(Side.Sell),
                DateTime.UtcNow,
                StatusMap.FromCode(Status.New),
                null,
                null,
                null,
                null,
                TimeInForceMap.FromCode(TimeInForce.GoodTillCancel),
                DateTime.UtcNow,
                DateTime.UtcNow,
                workingOrderId);

            // Create the access control record for this new entity.
            dataModel.CreateAccessControl(
                Guid.NewGuid(),
                AccessRightMap.FromCode(AccessRight.FullControl),
                entityId,
                userId,
                tenantId);

            return(consumerId);
        }
        /// <summary>
        /// Create a new Debt Holder Record
        /// </summary>
        /// <returns></returns>
        internal Guid Create()
        {
            DataModel            dataModel            = new DataModel();
            DataModelTransaction dataModelTransaction = DataModelTransaction.Current;
            Guid       userId   = TradingSupport.UserId;
            Guid       tenantId = PersistenceHelper.GetTenantForEntity(dataModelTransaction, this.Record.Blotter);
            Guid       entityId = Guid.Empty;
            Guid       consumerId;
            Guid       creditCardId;
            Guid       workingOrderId;
            CountryRow country;
            Guid       countryId;
            Guid?      provinceId = null;
            TypeRow    type;
            Guid       typeId;
            ImageRow   image;
            Guid       imageId;

            // These variables are used for auditing the changes to this record.
            DateTime  createdTime    = DateTime.UtcNow;
            Guid      createdUserId  = TradingSupport.UserId;
            DateTime  modifiedTime   = createdTime;
            Guid      modifiedUserId = createdUserId;
            EntityRow dollars;
            Guid      dollarsId;

            // We need write access to the containing blotter in order to add a record to it.
            if (!DataModelFilters.HasAccess(dataModelTransaction, userId, this.Record.Blotter, AccessRight.Write))
            {
                throw new SecurityException("Current user does not have write access to the selected blotter");
            }

            country = TradingSupport.FindCountryByKey(
                this.Record.ConfigurationId,
                "FK_Country_Security",
                new object[] { this.Record.CountryCode });
            countryId = country.CountryId;
            country.ReleaseReaderLock(dataModelTransaction.TransactionId);

            if (this.Record.ProvinceCode != null)
            {
                ProvinceRow province = TradingSupport.FindProvinceByKey(
                    this.Record.ConfigurationId,
                    "FK_Province_Consumer",
                    new object[] { this.Record.ProvinceCode });
                provinceId = province.ProvinceId;
                province.ReleaseReaderLock(dataModelTransaction.TransactionId);
            }

            dollars = TradingSupport.FindEntityByKey(
                this.Record.ConfigurationId,
                "FK_Security_WorkingOrder_SettlementId",
                new object[] { this.Record.Currency });
            dollarsId = dollars.EntityId;
            dollars.ReleaseReaderLock(dataModelTransaction.TransactionId);

            image = TradingSupport.FindImageByKey(
                this.Record.ConfigurationId,
                "FK_Image_Entity",
                new object[] { "OBJECT" });
            imageId = image.ImageId;
            image.ReleaseReaderLock(dataModelTransaction.TransactionId);

            type = TradingSupport.FindTypeByKey(
                this.Record.ConfigurationId,
                "FK_Type_Entity",
                new object[] { "CONSUMER DEBT" });
            typeId = type.TypeId;
            type.ReleaseReaderLock(dataModelTransaction.TransactionId);

            entityId       = Guid.NewGuid();
            consumerId     = Guid.NewGuid();
            creditCardId   = Guid.NewGuid();
            workingOrderId = Guid.NewGuid();

            dataModel.CreateEntity(
                createdTime,
                null,
                entityId,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                this.Record.AccountCode,
                imageId,
                false,
                false,
                modifiedTime,
                this.Record.OriginalAccountNumber,
                tenantId,
                typeId);

            dataModel.CreateSecurity(
                null,
                countryId,
                null,
                null,
                null,
                1,
                1,
                entityId,
                this.Record.AccountCode,
                tenantId,
                VolumeCategoryMap.FromCode(VolumeCategory.Unknown));

            dataModel.CreateConsumer(
                this.Record.Address1,
                this.Record.Address2,
                null,
                null,
                this.Record.City,
                consumerId,
                this.Record.DateOfBirth != null ? (object)this.Record.DateOfBirth.Value : null,
                null,
                null,
                this.Record.FirstName,
                null,
                this.Record.LastName,
                this.Record.MiddleName,
                StringUtilities.CleanUpAlphaNumericString(this.Record.PhoneNumber),
                this.Record.PostalCode,
                provinceId,
                null,
                StringUtilities.CleanUpAlphaNumericString(this.Record.SocialSecurityNumber),
                this.Record.Suffix);
            dataModel.CreateCreditCard(
                this.Record.AccountBalance,
                this.Record.AccountCode,
                consumerId,
                creditCardId,
                this.Record.DebtHolder,
                null,
                null,
                this.Record.AccountCode,
                StringUtilities.CleanUpAlphaNumericString(this.Record.OriginalAccountNumber),
                tenantId);
            dataModel.CreateConsumerDebt(
                this.Record.CollectionDate,
                entityId,
                consumerId,
                creditCardId,
                this.Record.DateOfDelinquency != null ? (object)this.Record.DateOfDelinquency.Value : null,
                null,
                null,
                this.Record.Representative,
                this.Record.Tag,
                tenantId,
                this.Record.VendorCode);

            dataModel.CreateWorkingOrder(
                null,
                this.Record.Blotter,
                createdTime,
                createdUserId,
                CrossingMap.FromCode(Crossing.AlwaysMatch),
                null,
                null,
                null,
                null,
                true,
                true,
                true,
                null,
                modifiedTime,
                modifiedUserId,
                OrderTypeMap.FromCode(OrderType.Market),
                entityId,
                createdTime,
                dollarsId,
                SideMap.FromCode(Side.Sell),
                createdTime,
                StatusMap.FromCode(Status.New),
                null,
                null,
                null,
                null,
                TimeInForceMap.FromCode(TimeInForce.GoodTillCancel),
                createdTime,
                createdTime,
                workingOrderId);

            // Create the access control record for this new entity.
            dataModel.CreateAccessControl(
                Guid.NewGuid(),
                AccessRightMap.FromCode(AccessRight.FullControl),
                entityId,
                userId,
                tenantId);

            return(entityId);
        }