CREATE procedure [dbo].[CompleteMoversSuitePlasticPayment]
@inPlasticDealId int,
@inApprovedAmount money,
@inStatementDetailId int,
@inHolderName varchar(1000),
@inHolderLastName varchar(500),
@inBillingAddress varchar(1000),
@inBillingAddress2 varchar(1000),
@inBillingCity varchar(1000),
@inBillingState varchar(255),
@inBillingPostalCode varchar(255),
@inBillingCountry varchar(255),
@inGatewayCustomerId varchar(40),
@inToken varchar(1000),
@inAccessory varchar(1000),
@inExpirationDate varchar(10),
@inLastFour varchar(4),
@inTokenTypeCode varchar(2),
@inTokenTypeExtendedCode varchar(32),
@inApprovalCode varchar(100),
@inProcessorCode varchar(100),
@inTransactionId varchar(40),
@inResponseCode varchar(10),
@inResponseMessage varchar(255),
@inCscResponseCode varchar(1),
@inTransactionStatus varchar(15),
@inBillingCycle bigint,
@inCreatedAtUtcDateTime datetime = null,
@inTransactionDate datetime,
@inCurrentSysUserId int,
@inHppSessionId varchar(50),
@inMssTransactionId varchar(40),
@inCurrencyCode varchar(3),
@inRecurringScheduleTransId nvarchar(64),
@outErrorCode int output,
@outMessage varchar(256) output
as
set nocount on
set @outErrorCode = 0
set @outMessage = null
declare @UNKNOWN_STATEMENT_HEADER_ID int = 926
declare @UNKNOWN_CASH_RECEIPTS_BRANCH_NUMBERING int = 927
declare @UNSUCCESSFUL_PAYMENT_ON_A_POSTED_BATCH int = 928
declare @SUCCESSFUL_PAYMENT_ALREADY_RECORDED int = 929
declare @theRowCount int
declare @theOkToContinue bit = 0
declare @theHaveSuccessfulPayment bit
declare @theAccountingNoteId int
declare @thePlasticInstrumentId int
declare @theProviderId int
declare @theStatementHeaderId int
declare @theStatementDetailId int
declare @theStatementDetailDistId int
declare @theCustomerNumber varchar(15)
declare @theCRNumber varchar(20)
declare @theIsPostedOnAnother bit
declare @thePostedOnCRNumber varchar(20)
declare @theVoidedReason varchar(267)
declare @theCurrentDateTime datetime = getdate()
declare @theTokenTypeId int
declare @theOrderId int
declare @theNoteBody varchar(350)
declare @thePlasticDealDescription varchar(255)
declare @theBranchPriKey int
declare @theJournalDate datetime
declare @theDepositTransactionDate datetime
declare @thePaymentTypeId int
declare @thePaymentCodeId int
declare @theShipperName varchar(55)
declare @theOrderNumber varchar(23)
declare @theTransactionCode varchar(267)
declare @theTokenTypeTable table
(
TokenTypeFID int
)
set @theHaveSuccessfulPayment = case when isnull( @inApprovedAmount, 0 ) != 0 then 1 else 0 end
update StatementDetail set
@theAccountingNoteId = VoidedNote,
@thePlasticInstrumentId = PlasticDeal.InstrumentFID,
@theProviderId = PlasticInstrument.ProviderFID,
@theOrderId = PlasticInstrument.OrderFID,
@theStatementHeaderId = StatementDetail.SHPriKey,
@theStatementDetailId = StatementDetail.SDPriKey,
@theCustomerNumber = case
when isnull( PlasticInstrument.CustomerNumber, '' ) != '' then rtrim( PlasticInstrument.CustomerNumber )
when isnull( Orders.CustomerNumber, '' ) != '' then rtrim( Orders.CustomerNumber )
else null
end,
@theCRNumber = StatementDetail.CRNumber,
@thePlasticDealDescription = PlasticDeal.[Description],
Amount = case when @theHaveSuccessfulPayment = 1 then @inApprovedAmount else 0 end,
CheckConf = @inApprovalCode,
VoidedBy = case when @theHaveSuccessfulPayment = 1 then null else @inCurrentSysUserId end,
VoidedDate = case when @theHaveSuccessfulPayment = 1 then null else @theCurrentDateTime end,
VoidedNote = case when @theHaveSuccessfulPayment = 1 then null else VoidedNote end,
Status = case when @theHaveSuccessfulPayment = 1 then 'Open' else 'Voided' end
from PlasticDeal
inner join PlasticInstrument on PlasticInstrument.InstrumentID = PlasticDeal.InstrumentFID
inner join StatementDetail on StatementDetail.SDPriKey = PlasticDeal.StatementDetailFID
inner join StatementHeader on StatementHeader.SHPriKey = StatementDetail.SHPriKey
left outer join Orders on Orders.PriKey = PlasticInstrument.OrderFID
where PlasticDeal.DealID = @inPlasticDealId and
PlasticDeal.ApprovedAmount = 0 and
PlasticDeal.StatementDetailFID = @inStatementDetailId and
StatementDetail.VoidedDate is not null and
StatementDetail.Amount = 0 and
(
(
@theHaveSuccessfulPayment = 1 and
StatementHeader.[Status] = 'System' and
(
StatementDetail.CheckConf = 'Pending' or
StatementDetail.[Status] = 'Voided'
)
) or
(
@theHaveSuccessfulPayment = 0 and
StatementDetail.[Status] = 'Voided'
)
)
set @theRowCount = @@rowcount
if( @theRowCount = 1 )
begin
set @theOkToContinue = 1
end
else if( @theRowCount = 0 )
begin
select
@theAccountingNoteId = null,
@thePlasticInstrumentId = PlasticDeal.InstrumentFID,
@theProviderId = PlasticInstrument.ProviderFID,
@theOrderId = isnull( PlasticDeal.OrderFID, PlasticInstrument.OrderFID ),
@theStatementHeaderId = null,
@theStatementDetailId = null,
@theStatementDetailDistId = null,
@theCustomerNumber = case
when isnull( PlasticDeal.CustomerNumber, '' ) != '' then rtrim( PlasticDeal.CustomerNumber )
when isnull( PlasticInstrument.CustomerNumber, '' ) != '' then rtrim( PlasticInstrument.CustomerNumber )
when isnull( Orders.CustomerNumber, '' ) != '' then rtrim( Orders.CustomerNumber )
else null
end,
@theCRNumber = null,
@thePlasticDealDescription = PlasticDeal.[Description],
@theBranchPriKey = StatementDetail.BranchPriKey,
@theShipperName = dbo.FormatFirstNameLastName( Orders.FirstName, Orders.LastName ),
@theOrderNumber = Orders.OrderNo,
@thePaymentTypeId = StatementDetail.PTPriKey,
@thePaymentCodeId = StatementDetail.PaymentCodePriKey,
@theDepositTransactionDate = StatementHeader.JournalDate
from PlasticDeal
inner join PlasticInstrument on PlasticInstrument.InstrumentID = PlasticDeal.InstrumentFID
left outer join StatementDetail on StatementDetail.SDPriKey = PlasticDeal.StatementDetailFID
left outer join StatementHeader on StatementHeader.SHPriKey = StatementDetail.SHPriKey
left outer join Orders on Orders.PriKey = PlasticInstrument.OrderFID
where PlasticDeal.DealID = @inPlasticDealId
set @theRowCount = @@rowcount
if( @theRowCount = 1 )
begin
if( @theHaveSuccessfulPayment = 1 )
begin
select top 1
@theIsPostedOnAnother = 1,
@thePostedOnCRNumber = isnull( StatementDetail.CRNumber, '??' )
from PlasticDeal
inner join PlasticInstrument on PlasticInstrument.InstrumentID = PlasticDeal.InstrumentFID
left outer join StatementDetail on StatementDetail.SDPriKey = PlasticDeal.StatementDetailFID
where PlasticInstrument.ProviderFID= @theProviderId and
PlasticDeal.TransactionID = @inTransactionId and
PlasticDeal.ApprovedAmount = @inApprovedAmount
if( isnull( @theIsPostedOnAnother, 0 ) = 1 )
begin
set @outErrorCode = @SUCCESSFUL_PAYMENT_ALREADY_RECORDED
set @theOkToContinue = 0
end
else
begin
set @theOkToContinue = 1
end
end
else
begin
set @outErrorCode = @UNSUCCESSFUL_PAYMENT_ON_A_POSTED_BATCH
set @theOkToContinue = 0
end
end
if( @theOkToContinue = 1 )
begin
exec GetPlasticCRNumber
@inBranchPriKey = @theBranchPriKey,
@outCrNumber = @theCRNumber output
if( isnull( @theCRNumber, '' ) = '' )
begin
set @outErrorCode = @UNKNOWN_CASH_RECEIPTS_BRANCH_NUMBERING
set @theOkToContinue = 0
end
end
if( @theOkToContinue = 1 )
begin
if( @inCreatedAtUtcDateTime is null )
begin
set @theJournalDate = @inTransactionDate
end
exec CashReceiptGetPlasticBatch
@inBranchPriKey = @theBranchPriKey,
@inSysUserID = @inCurrentSysUserId,
@inSuppressStatementHeaderOutput = 1,
@inJournalDate = @theJournalDate,
@outStatementHeaderID = @theStatementHeaderId output,
@inProviderID = @theProviderId,
@inCreatedAtUtcDateTime = @inCreatedAtUtcDateTime
if( isnull( @theStatementHeaderId, 0 ) = 0 )
begin
set @outErrorCode = @UNKNOWN_STATEMENT_HEADER_ID
set @theOkToContinue = 0
end
end
if( @theOkToContinue = 1 )
begin
set @theDepositTransactionDate = isnull( ( select JournalDate from StatementHeader where SHPriKey = @theStatementHeaderId ), @inTransactionDate )
insert into StatementDetail
(
SHPriKey,
[Description],
Amount,
BranchPriKey,
CheckConf,
CreatedBy,
CRNumber,
PTPriKey,
[Status],
UnappliedAmount,
PaymentCodePriKey,
Balance,
ApplyAmount,
AmountRemaining,
ContestCharge,
VanLineRefund,
CustomerNumber,
CRPrinted,
IsApplyGridBuilt,
CRNote,
DateTimeCreated
)
select
SHPriKey = @theStatementHeaderId,
[Description] = case
when isnull( @theOrderId, 0 ) > 0 then ( select dbo.FormatFirstNameLastName( FirstName, LastName ) from Orders where PriKey = @theOrderId )
when isnull( @theCustomerNumber, '' ) != '' then ( select CustomerName from vCustomerInformation where CustomerNumber = @theCustomerNumber )
else left( @thePlasticDealDescription, 128 )
end,
Amount = @inApprovedAmount,
BranchPriKey = @theBranchPriKey,
CheckConf = @inApprovalCode,
CreatedBy = @inCurrentSysUserId,
CRNumber = @theCRNumber,
PTPriKey = @thePaymentTypeId,
[Status] = 'Open',
UnappliedAmount = 0,
PaymentCodePriKey = @thePaymentCodeId,
Balance = 0,
ApplyAmount = 0,
AmountRemaining = 0,
ContestCharge = 0,
VanLineRefund = 0,
CustomerNumber = case
when isnull( @theCustomerNumber, '' ) != '' then left( @theCustomerNumber + space( 15 ), 15 )
else null
end,
CRPrinted = 0,
IsApplyGridBuilt = 0,
CRNote = @theAccountingNoteId,
DateTimeCreated = getdate()
set @theStatementDetailId = scope_identity()
if( isnull( @theOrderId, 0 ) > 0 )
begin
insert into StatementDetailDist
(
SDPriKey,
OrderControl,
[Description],
Amount,
BranchPriKey,
OrdPriKey,
CustomerNumber,
UnappliedCash,
UnappliedAmount,
PaymentCodePriKey,
Balance,
ApplyAmount,
AmountRemaining,
Adjustment,
ApplyCheck,
ContestCharge
)
select
SDPriKey = @theStatementDetailId,
OrderControl = Orders.OrderNo,
[Description] = Orders.LastName,
Amount = 0,
BranchPriKey = @theBranchPriKey,
OrdPriKey = @theOrderId,
CustomerNumber = case
when isnull( @theCustomerNumber, '' ) != '' then @theCustomerNumber
else null
end,
UnappliedCash = 0,
UnappliedAmount = 0,
PaymentCodePriKey = @thePaymentCodeId,
Balance = 0,
ApplyAmount = 0,
AmountRemaining = 0,
Adjustment = 0,
ApplyCheck = 0,
ContestCharge = 0
from Orders
where PriKey = @theOrderId
set @theStatementDetailDistId = scope_identity()
end
if( isnull( @theCustomerNumber, '' ) != '' )
begin
exec CashReceiptAddContact
@inStatementDetailID = @theStatementDetailId,
@inCustomerNumber = @theCustomerNumber,
@inCustomerAddressCode = ''
end
insert into PlasticDeal
(
InstrumentFID,
DealType,
[Description],
TransactionCode,
ApprovalCode,
ProcessorCode,
TransactionID,
BillingCycle,
OriginalAmount,
ApprovedAmount,
OriginFID,
StatementDetailFID,
TransactionDate,
ResponseCode,
ResponseMessage,
CscResponseCode,
TransactionStatus,
SysUserFID,
ActivityDate,
HppSessionId,
MssTransactionId,
CurrencyCode,
OrderFID,
CustomerNumber,
BranchFID
)
select
InstrumentFID = @thePlasticInstrumentId,
DealType = PlasticDeal.DealType,
[Description] = PlasticDeal.[Description],
TransactionCode = PlasticDeal.TransactionCode,
ApprovalCode = @inApprovalCode,
ProcessorCode = @inProcessorCode,
TransactionID = @inTransactionId,
BillingCycle = @inBillingCycle,
OriginalAmount = PlasticDeal.OriginalAmount,
ApprovedAmount = @inApprovedAmount,
OriginFID = PlasticDeal.OriginFID,
StatementDetailFID = @theStatementDetailId,
TransactionDate = @theDepositTransactionDate,
ResponseCode = @inResponseCode,
ResponseMessage = @inResponseMessage,
CscResponseCode = @inCscResponseCode,
TransactionStatus = @inTransactionStatus,
SysUserFID = @inCurrentSysUserId,
ActivityDate = PlasticDeal.ActivityDate,
HppSessionId = @inHppSessionId,
MssTransactionId = @inMssTransactionId,
CurrencyCode = @inCurrencyCode,
OrderFID = @theOrderId,
CustomerNumber = @theCustomerNumber,
BranchFID = @theBranchPriKey
from PlasticDeal
where PlasticDeal.DealID = @inPlasticDealId
set @inPlasticDealId = scope_identity()
end
end
if( @theOkToContinue = 1 )
begin
if( @theHaveSuccessfulPayment = 1 )
begin
if( @theAccountingNoteId is not null )
begin
delete from AccountingNote
where ANPriKey = @theAccountingNoteId
end
end
else
begin
if( ( isnull( @inResponseCode, '' ) != '' ) or ( isnull( @inResponseMessage, '' ) != '' ) )
begin
set @theVoidedReason = concat( @inResponseCode, ': ', @inResponseMessage )
end
else
begin
set @theVoidedReason = 'Transaction did not complete successfully.'
end
if( @theAccountingNoteId is null )
begin
insert into AccountingNote
(
DateCreated,
CreatedBy,
NoteText
)
select
DateCreated = @theCurrentDateTime,
CreatedBy = @inCurrentSysUserId,
NoteText = @theVoidedReason
set @theAccountingNoteId = scope_identity()
update StatementDetail set
VoidedNote = @theAccountingNoteId
where StatementDetail.SDPriKey = @theStatementDetailId
end
else
begin
update AccountingNote set
DateCreated = @theCurrentDateTime,
CreatedBy = @inCurrentSysUserId,
NoteText = @theVoidedReason
where AccountingNote.ANPriKey = @theAccountingNoteId
end
end
if( @theHaveSuccessfulPayment = 1 )
begin
insert into @theTokenTypeTable( TokenTypeFID )
exec DeterminePlasticTokenTypeId
@inCode = @inTokenTypeCode,
@inProviderId = @theProviderId,
@inExternalCode = @inTokenTypeExtendedCode
select
@theTokenTypeId = TokenTypeFID
from @theTokenTypeTable
update PlasticInstrument set
Accessory = isnull( @inAccessory, Accessory ),
ExpirationDate = isnull( @inExpirationDate, ExpirationDate ),
TokenTypeFID = @theTokenTypeId,
LastFour = @inLastFour,
Token = isnull( @inToken, '' ),
Inactive = case
when isnull( @inToken, '' ) = '' then 1
else Inactive
end,
GatewayCustomerID = @inGatewayCustomerId,
HolderName = isnull( @inHolderName, HolderName ),
HolderLastName = isnull( @inHolderLastName, HolderLastName ),
BillingAddress = isnull( @inBillingAddress, BillingAddress ),
BillingAddress2 = isnull( @inBillingAddress2, BillingAddress2 ),
BillingCity = isnull( @inBillingCity, BillingCity ),
BillingState = isnull( @inBillingState, BillingState ),
BillingPostalCode = isnull( @inBillingPostalCode, BillingPostalCode ),
BillingCountry = isnull( @inBillingCountry, BillingCountry )
where PlasticInstrument.InstrumentID = @thePlasticInstrumentId
end
else
begin
update PlasticInstrument set
Inactive = 1
from PlasticInstrument
inner join PlasticTokenType on PlasticTokenType.TokenTypeID = PlasticInstrument.TokenTypeFID
where PlasticInstrument.InstrumentID = @thePlasticInstrumentId and
PlasticTokenType.Code = '**'
end
update PlasticDeal set
ApprovalCode = @inApprovalCode,
ProcessorCode = isnull( @inProcessorCode, ProcessorCode ),
TransactionID = isnull( @inTransactionId, TransactionID ),
ResponseCode = isnull( @inResponseCode, ResponseCode ),
ResponseMessage = isnull( @inResponseMessage, ResponseMessage ),
CscResponseCode = isnull( @inCscResponseCode, CscResponseCode ),
TransactionStatus = isnull( @inTransactionStatus, TransactionStatus ),
BillingCycle = @inBillingCycle,
ApprovedAmount = case when @theHaveSuccessfulPayment = 1 then @inApprovedAmount else 0 end,
CurrencyCode = isnull( @inCurrencyCode, CurrencyCode ),
HppSessionID = isnull( HppSessionID, @inHppSessionId ),
MssTransactionID = isnull( MssTransactionID, @inMssTransactionId ),
OrderFID = isnull( OrderFID, @theOrderId ),
CustomerNumber = isnull( CustomerNumber, @theCustomerNumber ),
BranchFID = isnull( BranchFID, @theBranchPriKey )
where PlasticDeal.DealID = @inPlasticDealId
if( @theHaveSuccessfulPayment = 1 )
begin
if( isnull( @theOrderId, 0 ) > 0 )
begin
set @theNoteBody = 'Cash receipt ' + @theCRNumber + ' processed for ' + convert( varchar(30), isnull( @inApprovedAmount, 0 ), 1 ) +
': ' + isnull( @thePlasticDealDescription, '' )
exec AddPlasticPaymentOrderNote
@inProviderId = @theProviderId,
@inOrderId = @theOrderId,
@inCreatedBySysUserId = @inCurrentSysUserId,
@inNoteBody = @theNoteBody
end
if( isnull( @theStatementHeaderId, 0 ) > 0 )
begin
exec CashReceiptUpdateBatch
@inOldStatementHeaderID = null,
@inNewStatementHeaderID = @theStatementHeaderId
end
end
end
if( @outErrorCode != 0 )
begin
select
@outMessage = replace( ErrorCode.[Description], '{PostedOnCRNumber}', @thePostedOnCRNumber )
from ErrorCode
where ErrorCode.ECPriKey = @outErrorCode
end
else
begin
declare @theResultType varchar(20) = case
when @theHaveSuccessfulPayment = 1 then 'recorded and updated'
else 'marked as voided'
end
set @outMessage = concat( 'Payment was ', @theResultType, ' on cash receipt ', isnull( @theCRNumber, '???' ), '.' )
end
GO
GRANT EXECUTE ON [dbo].[CompleteMoversSuitePlasticPayment] TO [MssExec]
GO