CREATE PROCEDURE [dbo].[ChangeAlternateBillToOnBillingMinorItems]
@inOrderID int,
@inOldCustomerNumber varchar(15),
@inOldCustomerAddressCode varchar(15),
@inNewCustomerNumber varchar(15),
@inNewCustomerAddressCode varchar(15),
@outErrorCode int output,
@outRecordsModified int output
as
set nocount on
select
@outErrorCode = 0,
@outRecordsModified = 0
declare @theRowsUpdated int
declare @theNewCustomerInfoIsValid bit
declare @theNewAlternateCustomerNumber varchar(15)
declare @theNewAlternateCustomerAddressCode varchar(15)
declare @theNewAlternateAccountingCustomerFid int
declare @theNewAlternateAccountingCustomerAddressFid int
declare @ERROR_CODE_INVALID_NEW_ALTERNATE_BILLING int = 1252
declare @theBillingMinorCount int
declare @theCounter int
declare @theBMinPriKey int
declare @SourceBMinPriKey table
(
TableID int identity( 1, 1 ) not null,
BMinPriKey int
)
declare @BillingMinorItemDetails table
(
TableID int identity( 1, 1 ) not null,
BMajPriKey int,
GroupDescription varchar(128),
GroupAmount decimal(12,2),
RevenueGroupID int,
SplitGroup bit,
AllowDistributionsFlag bit,
PostedDists int,
HasCommissions int,
HasPostedThirdParties int,
HasPostedCommissions int,
AddBackBillingSourceID int,
BranchFID int,
DivisionFID int,
ICPriKey int,
Description varchar(128),
InvoiceFlag varchar(1),
LSPriKey int,
ARBranchFID int,
ARDivisionFID int,
RateTypeFID int,
Quantity float,
Quantity2 float,
Rate float,
GrossAmount money,
Discount float,
InvoiceAmount money,
Percentage float,
AllocatedInvoiceAmount money,
ReductionAmount decimal(12,2),
ReducedInvoiceAmount decimal(12,2),
AgentPercentage float,
Amount money,
PointOfServiceFID int,
ExtraStopFID int,
CustomerInvoiceReportEDIItemFID int,
TaxCode varchar(25),
LaborRatingTypeFID int,
RateSource varchar(50),
RateSourceRecord int,
DocumentNumber varchar(21),
ApplyToDoc int,
IHPriKey int,
RevenueRatingImportedSplitHaulFID int,
OriginalAllocation decimal(12,4),
SplitHaulPercentage decimal(12,4),
BMinPriKey int
)
select
@theNewCustomerInfoIsValid = 1,
@theNewAlternateCustomerNumber = CustomerAddressInfo.CustomerNumber,
@theNewAlternateCustomerAddressCode = CustomerAddressInfo.AddressCode,
@theNewAlternateAccountingCustomerFid = CustomerAddressInfo.CustomerFid,
@theNewAlternateAccountingCustomerAddressFid = CustomerAddressInfo.CustomerAddressId
from dbo.GetCustomerAddressInformation_Synonym( @inNewCustomerNumber, @inNewCustomerAddressCode ) as CustomerAddressInfo
if( isnull( @theNewCustomerInfoIsValid, 0 ) != 1 )
begin
set @outErrorCode = @ERROR_CODE_INVALID_NEW_ALTERNATE_BILLING
end
else
begin
if( @inOldCustomerNumber != @inNewCustomerNumber )
begin
insert into @SourceBMinPriKey( BMinPriKey )
select
BillingMinorItem.BMinPriKey
from BillingMajorItem
inner join BillingMinorItem on BillingMinorItem.BMajPriKey = BillingMajorItem.BMajPriKey
where BillingMajorItem.OrdPriKey = @inOrderID and
BillingMinorItem.AlternateCustomerNumber = @inOldCustomerNumber and
BillingMinorItem.AlternateCustomerAddressCode = @inOldCustomerAddressCode
set @theBillingMinorCount = @@ROWCOUNT
set @theCounter = 1
while( @theCounter <= @theBillingMinorCount )
begin
select
@theBMinPriKey = BMinPriKey
from @SourceBMinPriKey
where TableID = @theCounter
insert into @BillingMinorItemDetails
(
BMajPriKey,
GroupDescription,
GroupAmount,
RevenueGroupID,
SplitGroup ,
AllowDistributionsFlag,
PostedDists,
HasCommissions,
HasPostedThirdParties,
HasPostedCommissions,
AddBackBillingSourceID,
BranchFID,
DivisionFID,
ICPriKey,
Description,
InvoiceFlag,
LSPriKey,
ARBranchFID,
ARDivisionFID,
RateTypeFID,
Quantity,
Quantity2,
Rate,
GrossAmount,
Discount,
InvoiceAmount,
Percentage,
AllocatedInvoiceAmount,
ReductionAmount,
ReducedInvoiceAmount,
AgentPercentage,
Amount,
PointOfServiceFID,
ExtraStopFID,
CustomerInvoiceReportEDIItemFID,
TaxCode,
LaborRatingTypeFID,
RateSource,
RateSourceRecord,
DocumentNumber,
ApplyToDoc,
IHPriKey,
RevenueRatingImportedSplitHaulFID,
OriginalAllocation,
SplitHaulPercentage,
BMinPriKey
)
exec RevenueGetBillingMinorItem
@inBMinPriKey = @theBMinPriKey
set @theCounter = @theCounter + 1
end
end
update BillingMinorItem set
AlternateCustomerNumber = @theNewAlternateCustomerNumber,
AlternateCustomerAddressCode = @theNewAlternateCustomerAddressCode,
AlternateAccountingCustomerFid = @theNewAlternateAccountingCustomerFid,
AlternateAccountingCustomerAddressFid = @theNewAlternateAccountingCustomerAddressFid
from BillingMajorItem
inner join BillingMinorItem on BillingMinorItem.BMajPriKey = BillingMajorItem.BMajPriKey
inner join BillingMinorItemAlternateCustomer_Synonym as BillingMinorItemAlternateCustomer on BillingMinorItemAlternateCustomer.BMinPriKey = BillingMinorItem.BMinPriKey
cross join dbo.GetCustomerAddressJoinFields_Synonym( @theNewAlternateAccountingCustomerFid, @theNewAlternateAccountingCustomerAddressFid, @theNewAlternateCustomerNumber, @theNewAlternateCustomerAddressCode ) as NewCustomerAddressJoinFields
left outer join @BillingMinorItemDetails as BillingMinorItemDetails on BillingMinorItemDetails.BMinPriKey = BillingMinorItem.BMinPriKey
where BillingMajorItem.OrdPriKey = @inOrderID and
BillingMinorItem.AlternateCustomerNumber = @inOldCustomerNumber and
BillingMinorItem.AlternateCustomerAddressCode = @inOldCustomerAddressCode and
(
(
isnull( BillingMinorItem.DocumentNumber, '' ) = '' and
isnull( BillingMinorItemDetails.PostedDists, 0 ) != 1 and
isnull( BillingMinorItem.ApplyToDoc, 0 ) = 0 and
isnull( BillingMinorItem.IHPriKey, 0 ) = 0 and
isnull( BillingMinorItemDetails.HasPostedCommissions, 0 ) = 0 and
isnull( BillingMinorItemDetails.HasPostedThirdParties, 0 ) = 0
) or
BillingMinorItemAlternateCustomer.CustomerJoinField = NewCustomerAddressJoinFields.CustomerJoinField
)
set @outRecordsModified = @@ROWCOUNT
end
GO
GRANT EXECUTE ON [dbo].[ChangeAlternateBillToOnBillingMinorItems] TO [MssExec]
GO