[dbo].[RevenueRatingImportInvoiceDetails]
CREATE PROCEDURE [dbo].[RevenueRatingImportInvoiceDetails]
@inOrdersID int,
@inImported int,
@inRevenueImportType varchar(50),
@inRevenueRatingImportHeaderID int
as
set nocount on
declare @theTempID int
declare @theContinueUpdatingDescriptions bit = 1
declare @theDistributionCode varchar(10)
declare @theVanlineGroupExternalIdentifier varchar(10);
declare @theErrorCode int;
declare @theICPriKey int;
declare @theItemCode int;
declare @theDescription varchar(128);
declare @theRGPriKey int;
declare @theInvoice varchar( 10 );
declare @theAgentPercentage decimal( 12, 4 );
declare @theDetailsTable table
(
TempID int identity( 1, 1 ) not null,
AgentFID int,
RevenueRatingImportDetailID int,
[Description] varchar(128),
DistributionCode varchar(10),
Revenue decimal(12,4),
IsMerged bit,
AgentID varchar(10),
DescriptionUpdated bit,
ImportDateTime datetime
)
if( @inRevenueImportType in ( 'Atlas Carrier' ) )
begin
if( 0 = @inImported )
begin
insert into @theDetailsTable
(
AgentFID,
RevenueRatingImportDetailID,
[Description],
DistributionCode,
Revenue,
IsMerged,
AgentID,
DescriptionUpdated
)
select
AgentFID = RevenueRatingImportInvoiceDetail.AgentFID,
RevenueRatingImportDetailID = RevenueRatingImportInvoiceDetail.RevenueRatingImportInvoiceDetailID,
[Description] = RevenueRatingImportInvoiceDetail.[Description],
DistributionCode = isnull( RevenueRatingImportInvoiceMerge.MergeToCode, RevenueRatingImportInvoiceDetail.DistributionCode ),
Revenue = RevenueRatingImportInvoiceDetail.GrossAmount - round( ( RevenueRatingImportInvoiceDetail.GrossAmount * RevenueRatingImportInvoiceDetail.DiscountPct ) / 100.0, 2 ),
IsMerged = case
when RevenueRatingImportInvoiceMerge.RevenueRatingImportInvoiceMergeID is not null then 1
else 0
end,
AgentID = Agent.AgentID,
DescriptionUpdated = case
when RevenueRatingImportInvoiceMerge.RevenueRatingImportInvoiceMergeID is not null then 0
else 1
end
from RevenueRatingImportInvoiceDetail
left outer join Agent on Agent.AgentPriKey = RevenueRatingImportInvoiceDetail.AgentFID
left outer join RevenueRatingImportInvoiceMerge on RevenueRatingImportInvoiceMerge.DistributionCode = RevenueRatingImportInvoiceDetail.DistributionCode
left outer join RevenueRatingImportInvoiceOmit on RevenueRatingImportInvoiceOmit.DistributionCode = RevenueRatingImportInvoiceDetail.DistributionCode
where RevenueRatingImportInvoiceDetail.RevenueRatingImportHeaderFID = @inRevenueRatingImportHeaderID and
RevenueRatingImportInvoiceOmit.RevenueRatingImportInvoiceOmitID is null
end
else if( 1 = @inImported )
begin
insert into @theDetailsTable
(
AgentFID,
RevenueRatingImportDetailID,
[Description],
DistributionCode,
Revenue,
IsMerged,
AgentID,
DescriptionUpdated,
ImportDateTime
)
select
AgentFID = RevenueRatingImportedInvoiceDetail.AgentFID,
RevenueRatingImportDetailID = RevenueRatingImportedInvoiceDetail.RevenueRatingImportedInvoiceDetailID,
[Description] = RevenueRatingImportedInvoiceDetail.[Description],
DistributionCode = isnull( RevenueRatingImportInvoiceMerge.MergeToCode, RevenueRatingImportedInvoiceDetail.DistributionCode ),
Revenue = RevenueRatingImportedInvoiceDetail.GrossAmount - round( ( RevenueRatingImportedInvoiceDetail.GrossAmount * RevenueRatingImportedInvoiceDetail.DiscountPct ) / 100.0, 2 ),
IsMerged = case
when RevenueRatingImportInvoiceMerge.RevenueRatingImportInvoiceMergeID is not null then 1
else 0
end,
AgentID = Agent.AgentID,
DescriptionUpdated = case
when RevenueRatingImportInvoiceMerge.RevenueRatingImportInvoiceMergeID is not null then 0
else 1
end,
ImportDateTime = RevenueRatingImportedInvoiceDetail.ImportDateTime
from RevenueRatingImportedInvoiceDetail
left outer join Agent on Agent.AgentPriKey = RevenueRatingImportedInvoiceDetail.AgentFID
left outer join RevenueRatingImportInvoiceMerge on RevenueRatingImportInvoiceMerge.DistributionCode = RevenueRatingImportedInvoiceDetail.DistributionCode
left outer join RevenueRatingImportInvoiceOmit on RevenueRatingImportInvoiceOmit.DistributionCode = RevenueRatingImportedInvoiceDetail.DistributionCode
where RevenueRatingImportedInvoiceDetail.RevenueRatingImportedHeaderFID = @inRevenueRatingImportHeaderID and
RevenueRatingImportInvoiceOmit.RevenueRatingImportInvoiceOmitID is null
end
set @theVanlineGroupExternalIdentifier = dbo.GetVanlineGroupExternalCodeFromRatingType( @inRevenueRatingImportHeaderID, @inImported );
while( @theContinueUpdatingDescriptions = 1 )
begin
set @theTempID = null
select top 1
@theTempID = TempID,
@theDistributionCode = DistributionCode
from @theDetailsTable
where DescriptionUpdated = 0
order by TempID
if( @theTempID is null )
begin
set @theContinueUpdatingDescriptions = 0
end
else
begin
exec spRevenueImportGetItemCode
@theDistributionCode,
null,
null,
null,
@theErrorCode output,
@theICPriKey output,
@theItemCode output,
@theDescription output,
@theRGPriKey output,
@theInvoice output,
@theAgentPercentage output,
@theVanlineGroupExternalIdentifier;
update @theDetailsTable set
[Description] = isnull( @theDescription, [Description] ),
DescriptionUpdated = 1
where TempID = @theTempID
end
end
update theSourceTable set
[Description] = theDescTable.[Description]
from @theDetailsTable as theSourceTable
inner join @theDetailsTable as theDescTable on theDescTable.[DistributionCode] = theSourceTable.[DistributionCode] and
theDescTable.IsMerged = 0
where theSourceTable.IsMerged = 1
end
select
RevenueRatingImportDetailID = theDetailsTable.RevenueRatingImportDetailID,
RevenueRatingImportHeaderFID = @inRevenueRatingImportHeaderID,
[Description] = theDetailsTable.[Description],
AgentID = theDetailsTable.AgentID,
DistributionCode = theDetailsTable.DistributionCode,
Revenue = theDetailsTable.Revenue,
AgentPercentage = convert( decimal(12,4), 100.0 ),
CartonCode = convert( varchar(50), null ),
ImportDateTime = theDetailsTable.ImportDateTime,
Import = 0,
AgentFID = theDetailsTable.AgentFID,
IsImported = @inImported
from @theDetailsTable as theDetailsTable
order by 3
GO
GRANT EXECUTE ON [dbo].[RevenueRatingImportInvoiceDetails] TO [MssExec]
GO