CREATE PROCEDURE Sp_GetCustomerReceiptDetailEdit @VoucherNo NVARCHAR(50) AS BEGIN SET NOCOUNT ON; -- First SELECT statement SELECT CustomerReceiptDetail.InvoiceNo, CustomerReceiptDetail.Discount, CustomerReceiptDetail.ReceivedAmount, ISNULL(CustomerReceiptDetail.Narration, '') AS Narration, Invoice.NetAmount AS InvoiceAmount, ISNULL((SELECT SUM(ReceivedAmount) FROM CustomerReceiptDetail WHERE CustomerReceiptDetail.InvoiceNo = Invoice.InvNo), 0) AS ReceivedAmt, FORMAT(InvDate, 'dd-MM-yyyy') AS EntryDatefrmtd, ISNULL((SELECT SUM(Discount) FROM CustomerReceiptDetail WHERE CustomerReceiptDetail.InvoiceNo = Invoice.InvNo), 0) AS DiscountAmt, CASE WHEN (SELECT COUNT(*) FROM CustomerReceiptDetail AS CRD2 WHERE CRD2.InvoiceNo = CustomerReceiptDetail.InvoiceNo GROUP BY InvoiceNo) = 1 THEN Invoice.NetAmount ELSE (SELECT (Invoice.NetAmount - COALESCE(SUM(ReceivedAmount), 0) - COALESCE(SUM(Discount), 0)) FROM CustomerReceiptDetail AS CRD2 WHERE CRD2.InvoiceNo = CustomerReceiptDetail.InvoiceNo AND CRD2.VoucherNo != @VoucherNo GROUP BY InvoiceNo) END AS TotalReceivedAmount, CASE WHEN EXISTS (SELECT 1 FROM CustomerReceiptDetail AS CRD2 WHERE CRD2.InvoiceNo = CustomerReceiptDetail.InvoiceNo AND CRD2.PaidDate > CustomerReceiptDetail.PaidDate) THEN 'True' ELSE 'False' END AS EntryAfterPaidDate, CASE WHEN (SELECT COUNT(*) FROM CustomerReceiptDetail AS CRD2 WHERE CRD2.InvoiceNo = CustomerReceiptDetail.InvoiceNo GROUP BY InvoiceNo) = 1 THEN 0 ELSE (SELECT COALESCE(SUM(ReceivedAmount), 0) FROM CustomerReceiptDetail AS CRD2 WHERE CRD2.InvoiceNo = CustomerReceiptDetail.InvoiceNo AND CRD2.VoucherNo != @VoucherNo GROUP BY InvoiceNo) END AS ReceivedWithoutInv, CASE WHEN (SELECT COUNT(*) FROM CustomerReceiptDetail AS CRD2 WHERE CRD2.InvoiceNo = CustomerReceiptDetail.InvoiceNo GROUP BY InvoiceNo) = 1 THEN 0 ELSE (SELECT COALESCE(SUM(Discount), 0) FROM CustomerReceiptDetail AS CRD2 WHERE CRD2.InvoiceNo = CustomerReceiptDetail.InvoiceNo AND CRD2.VoucherNo != @VoucherNo GROUP BY InvoiceNo) END AS DiscountWithoutInv FROM CustomerReceiptDetail LEFT JOIN Invoice ON CustomerReceiptDetail.InvoiceNo = Invoice.InvNo WHERE CustomerReceiptDetail.VoucherNo = @VoucherNo AND Invoice.NetAmount IS NOT NULL UNION ALL -- Second SELECT statement SELECT CustomerReceiptDetail.InvoiceNo, CustomerReceiptDetail.Discount, CustomerReceiptDetail.ReceivedAmount, ISNULL(CustomerReceiptDetail.Narration, '') AS Narration, CustomerOPEntryUpdated.InvoiceAmt AS InvoiceAmount, ISNULL((SELECT SUM(ReceivedAmount) FROM CustomerReceiptDetail WHERE CustomerReceiptDetail.InvoiceNo = CustomerOPEntryUpdated.InvoiceNo), 0) AS ReceivedAmt, FORMAT(InvDate, 'dd-MM-yyyy') AS EntryDatefrmtd, ISNULL((SELECT SUM(Discount) FROM CustomerReceiptDetail WHERE CustomerReceiptDetail.InvoiceNo = CustomerOPEntryUpdated.InvoiceNo), 0) AS DiscountAmt, CASE WHEN (SELECT COUNT(*) FROM CustomerReceiptDetail AS CRD2 WHERE CRD2.InvoiceNo = CustomerReceiptDetail.InvoiceNo GROUP BY InvoiceNo) = 1 THEN CustomerOPEntryUpdated.InvoiceAmt ELSE (SELECT (CustomerOPEntryUpdated.InvoiceAmt - COALESCE(SUM(ReceivedAmount), 0) - COALESCE(SUM(Discount), 0)) FROM CustomerReceiptDetail AS CRD2 WHERE CRD2.InvoiceNo = CustomerReceiptDetail.InvoiceNo AND CRD2.VoucherNo != @VoucherNo GROUP BY InvoiceNo) END AS TotalReceivedAmount, CASE WHEN EXISTS (SELECT 1 FROM CustomerReceiptDetail AS CRD2 WHERE CRD2.InvoiceNo = CustomerReceiptDetail.InvoiceNo AND CRD2.PaidDate > CustomerReceiptDetail.PaidDate) THEN 'True' ELSE 'False' END AS EntryAfterPaidDate, CASE WHEN (SELECT COUNT(*) FROM CustomerReceiptDetail AS CRD2 WHERE CRD2.InvoiceNo = CustomerReceiptDetail.InvoiceNo GROUP BY InvoiceNo) = 1 THEN 0 ELSE (SELECT COALESCE(SUM(ReceivedAmount), 0) FROM CustomerReceiptDetail AS CRD2 WHERE CRD2.InvoiceNo = CustomerReceiptDetail.InvoiceNo AND CRD2.VoucherNo != @VoucherNo GROUP BY InvoiceNo) END AS ReceivedWithoutInv, CASE WHEN (SELECT COUNT(*) FROM CustomerReceiptDetail AS CRD2 WHERE CRD2.InvoiceNo = CustomerReceiptDetail.InvoiceNo GROUP BY InvoiceNo) = 1 THEN 0 ELSE (SELECT COALESCE(SUM(Discount), 0) FROM CustomerReceiptDetail AS CRD2 WHERE CRD2.InvoiceNo = CustomerReceiptDetail.InvoiceNo AND CRD2.VoucherNo != @VoucherNo GROUP BY InvoiceNo) END AS DiscountWithoutInv FROM CustomerReceiptDetail LEFT JOIN CustomerOPEntryUpdated ON CustomerReceiptDetail.InvoiceNo = CustomerOPEntryUpdated.InvoiceNo WHERE CustomerReceiptDetail.VoucherNo = @VoucherNo END GO