Skip to content

Commit

Permalink
Bugfix - wrong sql for fetching summed amount for payments and bills
Browse files Browse the repository at this point in the history
  • Loading branch information
Volmarg committed Feb 16, 2020
1 parent 0f0581d commit bb87938
Showing 1 changed file with 95 additions and 31 deletions.
126 changes: 95 additions & 31 deletions src/Repository/Modules/Reports/ReportsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,46 +27,110 @@ public function buildPaymentsSummariesForMonthsAndYears() {
$connection = $this->em->getConnection();

$sql = "
SELECT
DATE_FORMAT(mpm.date,'%Y-%m') AS yearAndMonth,
SELECT
dates.yearAndMonth,
IF(withoutBills.moneyWithoutBills IS NULL, 0, withoutBills.moneyWithoutBills) AS moneyWithoutBills,
IF(withBills.money IS NULL, 0, withBills.money) + IF(withoutBills.moneyWithoutBills IS NULL, 0, withoutBills.moneyWithoutBills) AS money
-- first get all the possible dates
FROM (
SELECT
DISTINCT DATE_FORMAT(mpm.date,'%Y-%m') AS yearAndMonth
FROM my_payment_monthly mpm
WHERE 1
AND mpm.deleted = 0
UNION
SELECT
DISTINCT DATE_FORMAT(mpbi.date,'%Y-%m') AS yearAndMonth
FROM my_payment_bill_item mpbi
WHERE 1
AND mpbi.deleted = 0
) AS dates
-- get money amount per month from payments
LEFT JOIN (
SELECT
DATE_FORMAT(mpmWithoutBill.date,'%Y-%m') AS yearAndMonth,
ROUND(
SUM(
mpm.money
) +
mpmWithoutBill.money
)
) AS moneyWithoutBills
FROM my_payment_monthly mpmWithoutBill
WHERE 1
AND mpmWithoutBill.deleted = 0
GROUP BY DATE_FORMAT(mpmWithoutBill.date,'%Y-%m')
) AS withoutBills
ON withoutBills.yearAndMonth = dates.yearAndMonth
-- get money amount from bills per month
LEFT JOIN (
SELECT
payment_bills.yearAndMonth AS yearAndMonth,
IF(
DATE_FORMAT(mpm.date,'%Y-%m') = yearAndMonth,
datesAndAmount.yearAndMonth = payment_bills.yearAndMonth,
CASE
WHEN payment_bills.money IS NULL THEN 0
ELSE
payment_bills.money
MAX(payment_bills.money)
END,
0
),
2) AS money,
ROUND(
SUM(
mpm.money
),
2) AS moneyWithoutBills
FROM my_payment_monthly mpm
LEFT JOIN (
SELECT
DATE_FORMAT(mpbi.date,'%Y-%m') AS yearAndMonth,
SUM(mpbi.amount) AS money
FROM my_payment_bill_item mpbi
GROUP BY (
DATE_FORMAT(mpbi.date,'%Y-%m')
)
) AS payment_bills
ON DATE_FORMAT(mpm.date,'%Y-%m') = payment_bills.yearAndMonth
) AS money
FROM (
SELECT
DISTINCT DATE_FORMAT(mpm.date,'%Y-%m') AS yearAndMonth,
ROUND(SUM(mpm.money)) AS amount
FROM my_payment_monthly mpm
WHERE 1
AND mpm.deleted = 0
GROUP BY DATE_FORMAT(mpm.date,'%Y-%m')
UNION
SELECT
DISTINCT DATE_FORMAT(mpbi.date,'%Y-%m') AS yearAndMonth,
0 AS amount
FROM my_payment_bill_item mpbi
WHERE 1
AND mpbi.deleted = 0
) AS datesAndAmount
RIGHT JOIN (
SELECT
DATE_FORMAT(mpbi.date,'%Y-%m') AS yearAndMonth,
SUM(mpbi.amount) AS money
FROM my_payment_bill_item mpbi
WHERE 1
AND mpbi.deleted = 0
GROUP BY DATE_FORMAT(mpbi.date,'%Y-%m')
) AS payment_bills
ON datesAndAmount.yearAndMonth = payment_bills.yearAndMonth
GROUP BY payment_bills.yearAndMonth
) AS withBills
ON withBills.yearAndMonth = dates.yearAndMonth
GROUP BY (
DATE_FORMAT(mpm.date,'%Y-%m')
)
ORDER BY dates.yearAndMonth ASC
";

$stmt = $connection->executeQuery($sql);
Expand Down

0 comments on commit bb87938

Please sign in to comment.