/// <summary> /// Update the debt class portion of a record. /// </summary> /// <param name="record"></param> public override void Update(DebtClass record) { DataModel dataModel = new DataModel(); DataModelTransaction transaction = DataModelTransaction.Current; DateTime modifiedTime = DateTime.UtcNow; CommissionScheduleRow oldCommissionScheduleRow; DebtClassRow debtClassRow = DataModel.DebtClass.DebtClassKey.Find(record.RowId); EntityRow entityRow; CommissionTrancheRow[] tranches = null; Guid oldCommissionScheduleId; Int64 oldCommissionScheduleRowVersion; Int64 debtClassRowVersion; Int64 entityRowVersion; bool newSchedule = false; debtClassRow.AcquireReaderLock(transaction); debtClassRowVersion = debtClassRow.RowVersion; entityRow = DataModel.Entity.EntityKey.Find(record.RowId); oldCommissionScheduleRow = debtClassRow.CommissionScheduleRow; if (record.CommissionSchedule != null && (oldCommissionScheduleRow == null || debtClassRow.CommissionScheduleId != record.CommissionSchedule.RowId)) { newSchedule = true; } if (record.DebtRuleId == null && !this.HasParent(transaction, entityRow)) { throw new FaultException <FieldRequiredFault>( new FieldRequiredFault("Top-level debt classes must have a debt rule."), "Top-level debt classes must have a debt rule."); } debtClassRow.ReleaseLock(transaction.TransactionId); entityRow.AcquireReaderLock(transaction); entityRowVersion = entityRow.RowVersion; entityRow.ReleaseLock(transaction.TransactionId); if (record.CommissionSchedule != null) { oldCommissionScheduleRow.AcquireReaderLock(transaction); oldCommissionScheduleId = oldCommissionScheduleRow.CommissionScheduleId; oldCommissionScheduleRowVersion = oldCommissionScheduleRow.RowVersion; tranches = oldCommissionScheduleRow.GetCommissionTrancheRows(); oldCommissionScheduleRow.ReleaseLock(transaction.TransactionId); } if (newSchedule) { if (oldCommissionScheduleRow != null) { dataModel.DestroyCommissionSchedule( new object[] { oldCommissionScheduleRow.CommissionScheduleId }, oldCommissionScheduleRow.RowVersion); } record.CommissionSchedule.RowId = Guid.NewGuid(); dataModel.CreateCommissionSchedule(record.CommissionSchedule.RowId, null, record.CommissionSchedule.Name); foreach (CommissionTranche tranche in record.CommissionSchedule.CommissionTranches) { dataModel.CreateCommissionTranche( record.CommissionSchedule.RowId, Guid.NewGuid(), tranche.CommissionType, tranche.CommissionUnit, tranche.EndRange, null, tranche.StartRange, tranche.Value); } } else if (record.CommissionSchedule != null) { foreach (CommissionTrancheRow tranche in tranches) { Guid trancheId; Int64 rowVersion; tranche.AcquireReaderLock(transaction); trancheId = tranche.CommissionTrancheId; rowVersion = tranche.RowVersion; tranche.ReleaseLock(transaction.TransactionId); if (record.CommissionSchedule.CommissionTranches.FirstOrDefault(t => t.RowId == trancheId) == null) { dataModel.DestroyCommissionTranche(new object[] { trancheId }, rowVersion); } } foreach (CommissionTranche tranche in record.CommissionSchedule.CommissionTranches) { if (DataModel.CommissionTranche.CommissionTrancheKey.Find(tranche.RowId) == null) { dataModel.CreateCommissionTranche( record.CommissionSchedule.RowId, Guid.NewGuid(), tranche.CommissionType, tranche.CommissionUnit, tranche.EndRange, null, tranche.StartRange, tranche.Value); } else { dataModel.UpdateCommissionTranche( record.CommissionSchedule.RowId, tranche.RowId, new object[] { tranche.RowId }, tranche.CommissionType, tranche.CommissionUnit, tranche.EndRange != null ? (object)tranche.EndRange.Value : DBNull.Value, null, tranche.RowVersion, tranche.StartRange, tranche.Value); } } dataModel.UpdateCommissionSchedule( record.CommissionSchedule.RowId, new object[] { record.CommissionSchedule.RowId }, null, record.CommissionSchedule.Name, record.CommissionSchedule.RowVersion); } dataModel.UpdateDebtClass( record.Address1, record.Address2 != null ? (object)record.Address2 : DBNull.Value, record.BankAccountNumber != null ? (object)record.BankAccountNumber : DBNull.Value, record.BankRoutingNumber != null ? (object)record.BankRoutingNumber : DBNull.Value, record.City, record.CommissionSchedule != null ? (object)record.CommissionSchedule.RowId : DBNull.Value, record.CompanyName != null ? (object)record.CompanyName : DBNull.Value, record.ContactName != null ? (object)record.ContactName : DBNull.Value, record.RowId, new object[] { record.RowId }, record.DebtRuleId != null ? record.DebtRuleId : DBNull.Value, record.Department != null ? record.Department : DBNull.Value, record.Email != null ? record.Email : DBNull.Value, record.Fax != null ? record.Fax : DBNull.Value, record.ForBenefitOf != null ? record.ForBenefitOf : DBNull.Value, record.Phone != null ? record.Phone : DBNull.Value, record.PostalCode, record.Province, debtClassRowVersion, record.SettlementTemplate != null ? record.SettlementTemplate : DBNull.Value); dataModel.UpdateEntity( null, null, record.RowId, new object[] { record.RowId }, null, null, null, null, null, null, null, null, null, null, null, modifiedTime, null, entityRowVersion, null, null); }