/// <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);
        }
        /// <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);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Handler for validating Source Order records.
        /// </summary>
        /// <param name="sender">The object that originated the event.</param>
        /// <param name="e">The event arguments.</param>
        internal static void OnWorkingOrderRowValidate(object sender, WorkingOrderRowChangeEventArgs e)
        {
            // The Business Rules will be enforced on this Working Order.  Note that it is locked at the point this handler is called.
            WorkingOrderRow workingOrderRow = e.Row;

            // The action on the row determines which rule to evaluate.
            switch (e.Action)
            {
            case DataRowAction.Add:
            case DataRowAction.Change:

                // This will evaluate whether the given Working Order is a candidate for matching.
                Boolean isSubmitted = false;

                // The 'Away' flag is used to inhibit matching without loosing the original preference for matching.  It is used when the trader wants to walk
                // away from his blotter.  When the trader returns, they want to flip a switch and have all the previous settings active for the orders on the
                // blotter.  This skips the matching logic for any trade marked 'Away'.
                if ((CrossingMap.FromId(workingOrderRow.CrossingId) & Crossing.Away) == Crossing.None)
                {
                    // This determines if the trade is eligible for crossing.
                    switch (CrossingMap.FromId(workingOrderRow.CrossingId) & ~Crossing.Away)
                    {
                    case Crossing.AlwaysMatch:

                        // The trade is eligible for crossing.
                        isSubmitted = true;
                        break;

                    case Crossing.UsePreferences:

                        // The trade is only eligible for crossing if it passes a set of preferences properties set by the user.
                        isSubmitted = workingOrderRow.IsAwake;

                        // The start and stop time must be extracted from the record using the base class.  It is safe here because the triggers are only
                        // invoked while the record is locked.
                        DateTime startTime = workingOrderRow.StartTime;
                        DateTime stopTime  = workingOrderRow.StopTime;

                        // The order is only eligible for crossing if the time of day is after the start time.
                        if (startTime != null && DateTime.UtcNow.TimeOfDay < startTime.TimeOfDay)
                        {
                            isSubmitted = false;
                        }

                        // The order is only eligible for crossing if the time if day is before the end time.
                        if (stopTime != null && DateTime.UtcNow.TimeOfDay > stopTime.TimeOfDay)
                        {
                            isSubmitted = false;
                        }

                        break;
                    }
                }

                // This will submit the order for crossing when it isn't already submitted.
                if (isSubmitted && workingOrderRow.StatusId != StatusMap.FromCode(Status.Submitted))
                {
                    UpdateWorkingOrderStatus(workingOrderRow, Status.Submitted);
                }

                // This removes the order for consideration in the crossing pool when it has been submitted.
                if (!isSubmitted && workingOrderRow.StatusId == StatusMap.FromCode(Status.Submitted))
                {
                    UpdateWorkingOrderStatus(workingOrderRow, Status.New);
                }

                break;
            }
        }