create procedure [dbo].[ReportStandardARCollections_XLedger]
@inBranch nvarchar(max),
@inCompanyWideBranchFlag bit,
@inAuthority nvarchar(max),
@inCompanyWideAuthorityFlag bit,
@inCustomerNumber nvarchar(max),
@inGroupBy varchar(30)
as
declare @theInvoiceStatusID int
declare @theCollectionsNoteTypeID int
declare @theSQLQuery nvarchar(max)
set @theInvoiceStatusID = ( select InvoiceStatusID from InvoiceStatus where Status = 'Invoice' )
set @theCollectionsNoteTypeID = ( select NoteTypeID from NoteType where NoteType = 'Collections' )
create table #theCustomerData
(
CustomerNumber char(15)
)
create unique clustered index #IX_theCustomerData on #theCustomerData
(
CustomerNumber
) with fillfactor = 100
insert into #theCustomerData
(
CustomerNumber
)
select Item from dbo.ParseMultiValuedParameter( @inCustomerNumber, ',' )
create table #theBranchData
(
BranchCode char(11)
)
create unique clustered index #IX_theBranchData on #theBranchData
(
BranchCode
) with fillfactor = 100
insert into #theBranchData ( BranchCode )
select isnull( Item, '' ) from dbo.ParseMultiValuedParameter( @inBranch, ',' )
create table #theData
(
DocumentNumber varchar( 21 ),
DocumentDescription varchar(31),
Authority varchar( 31 ),
OrderID int,
OrderNumber varchar( 24 ),
Salesperson varchar( 43 ),
Shipper varchar( 50 ),
BranchID varchar( 11 ),
CustomerNumber varchar( 15 ),
CustomerName varchar( 65 ),
CustomerClass varchar( 15 ),
CustomerContact varchar( 61 ),
CustomerAddress1 varchar( 128 ),
CustomerAddress2 varchar( 128 ),
CustomerAddress3 varchar( 128 ),
CustomerCity varchar( 35 ),
CustomerState varchar( 29 ),
CustomerPostalCode varchar( 11 ),
CustomerPhone1 varchar( 21 ),
CustomerPhone2 varchar( 21 ),
CustomerPhone3 varchar( 21 ),
CurrentTransactionAmount money,
InvoiceNumber varchar( 64 ),
InvoiceDate datetime,
InvoiceTotal money,
DocumentDate datetime,
DueDate datetime,
PostDate datetime,
JournalDate datetime,
DocumentType int,
OrderInvoiceTotal money,
CustomerInvoiceTotal money,
CollectionsSubject varchar( 256 ),
CollectionsNote text,
MultiBranchInvoiceFlag bit default(0)
)
declare @theAccountingCustomerAddressTypeFid int =
(
select AccountingCustomerAddressTypeId
from AccountingCustomerAddressType
where AccountingCustomerAddressType.TypeName = dbo.GetDefaultAccountingCustomerAddressTypeName()
)
set @theSQLQuery = N'declare @theAuthority table( Authority char(31) ) ' +
'insert into @theAuthority ( Authority ) ' +
'select isnull( Item, '''' ) from dbo.ParseMultiValuedParameter( @inAuthority, '','' ) '
set @theSQLQuery = @theSQLQuery +
N'insert into #theData ' +
'( ' +
'DocumentNumber, ' +
'DocumentDescription, ' +
'Authority, ' +
'OrderID, ' +
'OrderNumber, ' +
'Salesperson, ' +
'Shipper, ' +
'BranchID, ' +
'CustomerNumber, ' +
'CustomerName, ' +
'CustomerClass, ' +
'CustomerContact, ' +
'CustomerAddress1, ' +
'CustomerAddress2, ' +
'CustomerAddress3, ' +
'CustomerCity, ' +
'CustomerState, ' +
'CustomerPostalCode, ' +
'CustomerPhone1, ' +
'CustomerPhone2, ' +
'CustomerPhone3, ' +
'CurrentTransactionAmount, ' +
'InvoiceNumber, ' +
'InvoiceDate, ' +
'InvoiceTotal, ' +
'DocumentDate, ' +
'DueDate, ' +
'PostDate, ' +
'JournalDate, ' +
'DocumentType ' +
') ' +
'select distinct ' +
'DocumentNumber = rtrim( GPWarehouse.DocumentNumber ), ' +
'DocumentDescription = rtrim( GPWarehouse.DocumentDescription ), ' +
'Authority = rtrim( GPWarehouse.Authority ), ' +
'OrderID = GPWarehouse.OrderID, ' +
'OrderNumber = case Orders.Archived ' +
'when 1 then rtrim( GPWarehouse.OrderNumber) + '' [A]'' ' +
'else rtrim( GPWarehouse.OrderNumber ) ' +
'end, ' +
'Salesperson = rtrim( GPwarehouse.SalesPerson ), ' +
'Shipper = rtrim( GPWarehouse.Shipper ), ' +
'BranchID = rtrim( GPWarehouse.BranchID ), ' +
'CustomerNumber = rtrim( GPWarehouse.CustomerNumber ), ' +
'CustomerName = rtrim( GPCustomer.CustomerName ), ' +
'CustomerClass = rtrim( GPCustomer.CustomerClass ), ' +
'CustomerContact = rtrim( AccountingCustomer.Contact ), ' +
'CustomerAddress1 = rtrim( AccountingCustomerAddress.Address1 ), ' +
'CustomerAddress2 = rtrim( AccountingCustomerAddress.Address2 ), ' +
'CustomerAddress3 = rtrim( AccountingCustomerAddress.Address3 ), ' +
'CustomerCity = rtrim( AccountingCustomerAddress.City ), ' +
'CustomerState = rtrim( AccountingCustomerAddress.[State] ), ' +
'CustomerPostalCode = rtrim( AccountingCustomerAddress.PostalCode ), ' +
'CustomerPhone1 = AccountingCustomer.Phone1, ' +
'CustomerPhone2 = AccountingCustomer.Phone2, ' +
'CustomerPhone3 = AccountingCustomer.Phone3, ' +
'CurrentTransactionAmount = GPWarehouse.CurrentTransactionAmount, ' +
'InvoiceNumber = rtrim( GPWarehouse.InvoiceNumber ), ' +
'InvoiceDate = GPWarehouse.InvoiceDate, ' +
'InvoiceTotal = GPWarehouse.InvoiceAmount, ' +
'DocumentDate = GPWarehouse.DocumentDate, ' +
'DueDate = GPWarehouse.DueDate, ' +
'PostDate = GPWarehouse.PostDate, ' +
'JournalDate = GPWarehouse.JournalDate, ' +
'DocumentType = GPWarehouse.DocumentType '
set @theSQLQuery = @theSQLQuery +
N'from GPWarehouse ' +
'inner join GPCustomer on GPCustomer.CustomerNumber = GPWarehouse.CustomerNumber ' +
'inner join AccountingCustomer on AccountingCustomer.AccountingCustomerId = GPWarehouse.AccountingCustomerFid ' +
'left outer join AccountingCustomerAddress on AccountingCustomerAddress.AccountingCustomerFid = AccountingCustomer.AccountingCustomerId and ' +
' AccountingCustomerAddress.AccountingCustomerAddressTypeFid = ' + ltrim( rtrim( str( @theAccountingCustomerAddressTypeFid ) ) ) + ' '
set @theSQLQuery = @theSQLQuery +
N'inner join #theCustomerData as theCustomerData on theCustomerData.CustomerNumber = GPWarehouse.CustomerNumber '
set @theSQLQuery = @theSQLQuery +
N'inner join #theBranchData as theBranchData on isnull( GPWarehouse.BranchID, '''' ) = theBranchData.BranchCode '
set @theSQLQuery = @theSQLQuery +
N'inner join @theAuthority as theAuthorityData on GPWarehouse.Authority = theAuthorityData.Authority '
set @theSQLQuery = @theSQLQuery +
N'left outer join Orders on GPWarehouse.OrderID = Orders.PriKey '
set @theSQLQuery = @theSQLQuery +
N'where GPWarehouse.CurrentTransactionAmount != 0 '
set @theSQLQuery = @theSQLQuery +
N'order by CustomerNumber, OrderNumber, DocumentNumber'
exec sp_executesql @theSQLQuery,
N'@inInvoiceStatusID int, @inAuthority varchar(max)', @theInvoiceStatusID, @inAuthority
drop table #theCustomerData
drop table #theBranchData
create table #theInvoiceTotals
(
Customer varchar(15),
OrderID int,
Invoice varchar(64),
Amount money
)
insert into #theInvoiceTotals
(
Customer,
OrderID,
Invoice,
Amount
)
select distinct
CustomerNumber,
OrderID,
InvoiceNumber,
InvoiceTotal
from #theData
where InvoiceTotal is not null
if( @inGroupBy = 'Customer' )
begin
update #theData
set CustomerInvoiceTotal =
isnull( (
select sum( Amount )
from #theInvoiceTotals
where #theData.CustomerNumber = #theInvoiceTotals.Customer
group by Customer
), 0 )
update #theData
set OrderInvoiceTotal =
isnull( (
select sum( Amount )
from #theInvoiceTotals
where
#theInvoiceTotals.Customer = #theData.CustomerNumber and
#theInvoiceTotals.OrderID = #theData.OrderID
group by
#theInvoiceTotals.Customer,
#theInvoiceTotals.OrderID
), 0 )
end
else
begin
update #theData
set CustomerInvoiceTotal =
isnull( (
select sum( Amount )
from #theInvoiceTotals
where
#theInvoiceTotals.OrderID = #theData.OrderID and
#theInvoiceTotals.Customer = #theData.CustomerNumber
group by
OrderID,
Customer
), 0 )
update #theData
set OrderInvoiceTotal =
isnull( (
select sum( Amount )
from #theInvoiceTotals
where #theInvoiceTotals.OrderID = #theData.OrderID
group by OrderID
), 0 )
from #theData
end
drop table #theInvoiceTotals
update #theData
set CollectionsSubject =
(
select top 1 Subject
from OrderNoteHeader
where
OrderFID = theData.OrderID and
NoteTypeFID = @theCollectionsNoteTypeID
order by OrderNoteHeaderID desc
),
CollectionsNote =
(
select top 1 OrderNoteDetail.NoteBody
from OrderNoteHeader
inner join OrderNoteDetail on OrderNoteDetail.OrderNoteHeaderFID = OrderNoteHeader.OrderNoteHeaderID
where
OrderNoteHeader.OrderFID = theData.OrderID and
OrderNoteHeader.NoteTypeFID = @theCollectionsNoteTypeID
order by
OrderNoteHeaderID desc,
OrderNoteDetailID desc
)
from #theData as theData
update #theData
set MultiBranchInvoiceFlag =
isnull( (
select 1
from #theData as theInnerData
where theInnerData.InvoiceNumber = theOuterData.InvoiceNumber
group by theInnerData.InvoiceNumber
having count( distinct theInnerData.BranchID ) > 1
), 0 )
from #theData as theOuterData
where rtrim( theOuterData.InvoiceNumber ) != ''
select * from #theData
drop table #theData
GO
GRANT EXECUTE ON [dbo].[ReportStandardARCollections_XLedger] TO [MssExec]
GO