-- Breif Invoice InfoSELECTi.InvoiceIdAS InvoiceId,
SUM(Amount) as InvoiceTotal,
i.InvoiceDateAS InvoiceDate,
(
SELECTp.AmountAS PaymentAmount
FROM Payments p
WHEREp.InvoiceId=i.InvoiceId
) AS PaymentAmount
FROM InvoiceItem it INNER JOIN
Invoices i ONit.InvoiceId=i.InvoiceIdWHEREi.InvoiceIdIN (
SELECT
InvoiceId
FROM Invoices
WHERE AllocationId =23
)
GROUP BYi.InvoiceId, i.InvoiceDate;
-- ARP By AllocationSELECT
AutomatedRaisedPaymentId,
arp.AllocationIdAS AllocationId,
arp.AmountAS Amount,
Code AS Category,
arp.DescriptionAS [Description]
FROM AutomatedRaisedPayments arp
INNER JOIN
InvoiceItemCategory itc ONarp.InvoiceItemCategoryId=itc.InvoiceItemCategoryIdINNER JOIN Allocation a
ONarp.AllocationId=a.AllocationIdINNER JOIN
Rentals r ONr.RentalId=a.RentalIdWHEREa.IsActive=1ANDr.UserId=2008ANDarp.AllocationId=23;
-- ARP By All AllocationSELECTarp.AllocationIdAS AllocationId,
SUM(arp.Amount) AS Amount,
r.TitleAs Title
FROM AutomatedRaisedPayments arp
INNER JOIN
InvoiceItemCategory itc ONarp.InvoiceItemCategoryId=itc.InvoiceItemCategoryIdINNER JOIN Allocation a
ONarp.AllocationId=a.AllocationIdINNER JOIN
Rentals r ONr.RentalId=a.RentalIdWHEREa.IsActive=1ANDr.UserId=2008GROUP BYarp.AllocationId, r.Title;
-- ARP Total CollectionSELECTSUM(arp.Amount) AS TotalArpCollection
FROM AutomatedRaisedPayments arp
WHERE AllocationId IN (
SELECT
AllocationId
FROM Allocation a INNER JOIN
Rentals r ONa.RentalId=r.RentalIdWHEREa.IsActive=1ANDr.UserId=2008
);
-- Current Rent CollectionSELECTSUM(Amount) AS CurrentRentCollection
FROM Rentals
WHERE UserId =2008AND IsDeleted =0;
-- ARP By CategorySELECTitc.CodeAS Category,
SUM(arp.Amount) AS Amount
FROM AutomatedRaisedPayments arp INNER JOIN
InvoiceItemCategory itc ONarp.InvoiceItemCategoryId=itc.InvoiceItemCategoryIdWHERE AllocationId IN (
SELECT
AllocationId
FROM Allocation a INNER JOIN
Rentals r Ona.RentalId=r.RentalIdWHEREa.IsActive=1ANDr.UserId=2008
)
GROUP BYitc.Code;
-- Monthly collection by categorySELECTitc_main.CodeAS Category,
'May-2022'AS MonthYear,
(
CASE
WHEN CollectionByMonth.Amount IS NULL THEN 0
ELSE CollectionByMonth.Amount
END
)
AS Amount
FROM InvoiceItemCategory itc_main
LEFT JOIN
(
SELECTitc.CodeAS Category,
SUM(itm.Amount) AS Amount,
CONCAT(DATENAME(M, itm.Date), '-' ,DATEPART(YEAR, itm.[Date])) AS MonthYear
FROM InvoiceItem itm INNER JOIN
InvoiceItemCategory itc ONitm.InvoiceItemCategoryId=itc.InvoiceItemCategoryIdWHERE InvoiceId IN (
SELECT
InvoiceId
FROM Invoices i
WHERE AllocationId IN (
SELECT
AllocationId
FROM Allocation a INNER JOIN
Rentals r ONr.RentalId=a.RentalIdWHEREa.IsActive=1ANDr.UserId=2008
)
)
ANDitc.CodeIN (
SELECT
Code
FROM InvoiceItemCategory
WHERE UserId =2008
)
AND CONCAT(DATENAME(M, itm.Date), '-' ,DATEPART(YEAR, itm.[Date])) ='May-2022'GROUP BYitc.Code, CONCAT(DATENAME(M, itm.Date), '-' ,DATEPART(YEAR, itm.[Date])), CONCAT(DATEPART(MONTH, itm.Date), '-', DATEPART(YEAR, itm.Date))
-- ORDER BY CONCAT(DATEPART(MONTH, itm.Date), '-', DATEPART(YEAR, itm.Date))
) AS CollectionByMonth ONCollectionByMonth.Category=itc_main.Code;
-- Collection by payment modeSELECTmof_main.Code,
(
CASE
WHEN PaymentByMode.Amount IS NULL THEN 0
ELSE PaymentByMode.Amount
END
) AS PaymentMode
FROM ModeOfPayment mof_main LEFT JOIN
(
SELECTmof.CodeAS PaymentMode,
-SUM(p.Amount) AS Amount
FROM Payments p INNER JOIN
ModeOfPayment mof ONp.ModeOfPaymentId=mof.ModeOfPaymentIdWHEREp.InvoiceIdIN (
SELECT
InvoiceId
FROM Invoices i
WHERE AllocationId IN (
SELECT
AllocationId
FROM Allocation a INNER JOIN
Rentals r ONr.RentalId=a.RentalIdWHEREa.IsActive=1ANDr.UserId=2008
)
)
GROUP BYmof.Code
) AS PaymentByMode ONPaymentByMode.PaymentMode=mof_main.Code;
-- Collection by payment modeSELECTmof_main.Code,
(
CASE
WHEN PaymentByMode.Amount IS NULL THEN 0
ELSE PaymentByMode.Amount
END
) AS PaymentMode,
'June-2022'AS MonthYear
FROM ModeOfPayment mof_main LEFT JOIN
(
SELECTmof.CodeAS PaymentMode,
-SUM(p.Amount) AS Amount,
CONCAT(DATENAME(M, p.Date), '-' ,DATEPART(YEAR, p.Date)) AS MonthYear
FROM Payments p INNER JOIN
ModeOfPayment mof ONp.ModeOfPaymentId=mof.ModeOfPaymentIdWHEREp.InvoiceIdIN (
SELECT
InvoiceId
FROM Invoices i
WHERE AllocationId IN (
SELECT
AllocationId
FROM Allocation a INNER JOIN
Rentals r ONr.RentalId=a.RentalIdWHEREa.IsActive=1ANDr.UserId=2008
)
)
AND CONCAT(DATENAME(M, p.Date), '-' ,DATEPART(YEAR, p.Date)) ='June-2022'GROUP BYmof.Code, CONCAT(DATENAME(M, p.Date), '-' ,DATEPART(YEAR, p.Date))
) AS PaymentByMode ONPaymentByMode.PaymentMode=mof_main.Code;
Generate Month Year List Procedure
-- Generate Month Year List from UserDefined date to current date-- Date format: yyyy-mm-01
CREATE PROCEDURE GenerateMonthYearList @dateDATEASBEGIN
TRUNCATE TABLE MonthYearList;
DECLARE @i INT=0;
DECLARE @tempDate DATE= @date;
DECLARE @months INT= DATEDIFF(MONTH, @tempDate, GETDATE());
Print @months;
WHILE @i <= @months
BEGININSERT INTO MonthYearList VALUES (CONCAT(DATENAME(M, @tempDate), '-', DATEPART(YEAR, @tempDate)));
SET @tempDate = DATEADD(MONTH, 1, @tempDate);
SET @i = @i +1;
END;
END;
-- EXEC GenerateMonthYearList '2022-04-01';
Raised Payment By Month
SELECT
(
CASE
WHEN RaisedPaymentByMonth.Amount IS NULL THEN 0
ELSE RaisedPaymentByMonth.Amount
END
) AS Amount,
myl.MonthYearAS MonthYear
FROM MonthYearList myl LEFT JOIN
(
SELECTSUM(it.Amount) AS Amount,
CONCAT(DATENAME(M, it.Date), '-' ,DATEPART(YEAR, it.Date)) ASDateFROM InvoiceItem it
WHEREit.InvoiceIdIN
(
SELECT
InvoiceId
FROM Invoices i
WHEREi.AllocationIdIN
(
SELECT
AllocationId
FROM Allocation a INNER JOIN
Rentals r ONa.RentalId=r.RentalIdWHEREr.UserId= @UserId
)
ANDi.IsDeleted=0
)
GROUP BY CONCAT(DATENAME(M, it.Date), '-' ,DATEPART(YEAR, it.Date))
) AS RaisedPaymentByMonth ONmyl.MonthYear=RaisedPaymentByMonth.DateORDER BYmyl.SerialNoASC;