I have an issue while calculating the column-like for bank statements. actually, I am trying to calculate Opening Balance and Closing Bal in MySQL PHP.
I have two tables. material_in for IN +
entry and material_approvefor OUT -
.
Everything is perfect in my code but I am not able to calculate the opening and the closing balance.
I am trying to display the previous closing balance will show the next column as the opening balance. and for first time the opening balance will by default 0
.
I am new in PHP and I am here using Union All function. Please help me to create the function for getting the Opening / and Closing calculation in my code.
I used the CASE WHEN
function but I could not able to implement the previous column data to the next row column.
Please help to achieve the desired output in mu code.
table- material_in
id | components_key | insert_date | credit | transaction_type |
---|---|---|---|---|
1 | 12345 | 2021-04-16 | 100 | IN |
2 | 12345 | 2021-04-16 | 50 | IN |
table- material_approve
id | components_key | insert_date | debit | transaction_type |
---|---|---|---|---|
1 | 12345 | 2021-04-16 | 30 | OUT |
2 | 12345 | 2021-04-16 | 2 | OUT |
I am trying the code -
SELECT
`u`.`component_key`,
`u`.`insert_date`,
`u`.`transaction_qty`,
`u`.`transaction_type`,
CASE WHEN `u`.`query_type` = 'IN' THEN `u`.`transaction_qty` ELSE '0' END AS `today_credit`,
CASE WHEN `u`.`query_type` = 'OUT' THEN `u`.`transaction_qty` ELSE '0' END AS `today_debit`,
CASE WHEN `u`.`query_type` = 'IN' OR `u`.`query_type` = 'OUT' THEN (`close_bal`) ELSE '0' END AS `open_bal`,
CASE WHEN `u`.`query_type` = 'IN' OR `u`.`query_type` = 'OUT' THEN (`open_bal` + `today_credit`-`today_debit`) ELSE '0' END AS `close_bal`,
FROM
(
SELECT
`components_key` AS `component_key`,
`insert_date`,
`credit` AS `transaction_qty`,
CASE WHEN `transaction_type` = 'OA' THEN 'IN' WHEN `transaction_type` = 'O' THEN 'IN' WHEN `transaction_type` = 'I' THEN 'IN' ELSE '--'
END AS `query_type`, null AS today_debit, null AS today_credit, null AS open_bal, null AS close_bal
FROM
`material_in`
UNION ALL
SELECT
`components_key`,
`insert_date`,
`debit` AS `transaction_qty`,
CASE WHEN `transaction_type` = 'OA' THEN 'OUT' WHEN `transaction_type` = 'I' THEN 'OUT' WHEN `transaction_type` = 'O' THEN 'OUT' ELSE '--'
END AS `query_type`, null AS today_debit, null AS today_credit, null AS open_bal, null AS close_bal
FROM
`material_approve`
) u
WHERE
`u`.`component_key` = '12345'
ORDER BY
`u`.`insert_date`
DESC
the output comes -
components_key | insert_date | transaction_qty | transaction_type | today_credit | today_debit | open_bal | close_bal |
---|---|---|---|---|---|---|---|
12345 | 2021-04-16 | 100 | IN | 100 | 0 | NULL | NULL |
12345 | 2021-04-16 | 50 | IN | 50 | 0 | NULL | NULL |
12345 | 2021-04-16 | 30 | OUT | 0 | 30 | NULL | NULL |
12345 | 2021-04-16 | 2 | OUT | 0 | 2 | NULL | NULL |
but I want it comes like this below
the output comes -
output will be-
components_key | insert_date | transaction_qty | transaction_type | today_credit | today_debit | open_bal | close_bal |
---|---|---|---|---|---|---|---|
12345 | 2021-04-16 | 100 | IN | 100 | 0 | 0 | 100 |
12345 | 2021-04-16 | 50 | IN | 50 | 0 | 150 | 200 |
12345 | 2021-04-16 | 30 | OUT | 0 | 30 | 200 | 170 |
12345 | 2021-04-16 | 2 | OUT | 0 | 2 | 170 | 168 |
FORMULA FOR CLOSE BAL = open_bal + today_credit - today_debit
= close_bal
FORMULA FOR OPEN BAL = close_bal + today_credit - today_debit
= open_bal