SELECT
worksheet.accountid AS id,
worksheet.`type` AS `type`,
worksheet.`number` AS `number`,
worksheet.`name` AS `name`,
totals.credit AS totalcredit,
totals.debit AS totaldebit,
worksheet.itemid AS items_id,
worksheet.debit AS items_debit,
worksheet.credit AS items_credit,
(@itemBalance := @itemBalance + worksheet.amount) AS items_balance,
worksheet.fiscaldate AS items_fiscaldate,
worksheet.description AS items_description,
worksheet.receiptid AS items_receiptid,
worksheet.transactionid AS items_transactionid
FROM (
SELECT
a.`id` AS accountid,
`type` AS `type`,
`full_account_number` AS `number`,
`name` AS `name`,
i.id AS itemid,
i.debit AS debit,
i.credit AS credit,
DATE_FORMAT(t.fiscal_date, '%c/%e/%Y') AS fiscaldate,
t.description AS description,
r.receipt_id AS receiptid,
t.id AS transactionid,
IF ( a.`type` IN ('REVENUE', 'ASSET', 'EQUITY'),
i.credit - i.debit,
i.debit - i.credit
) AS 'amount'
FROM gl_transaction_items i
LEFT JOIN gl_accounts a ON i.glaccount_id = a.id
LEFT JOIN gl_transactions t ON t.id = i.gltransaction_id
LEFT OUTER JOIN gl_auto_post_history r ON r.transaction_id = t.id
WHERE a.id = :account
AND t.fiscal_date >= :startDate
AND t.fiscal_date <= :endDate
AND (t.voided = 0 OR t.voided IS NULL )
AND t.posted = 1
ORDER BY t.fiscal_date DESC
) AS worksheet
JOIN (
SELECT
glaccount_id AS accountid,
SUM(i.credit) AS credit,
SUM(i.debit) AS debit
FROM gl_transaction_items i
LEFT JOIN gl_transactions t ON t.id = i.gltransaction_id
WHERE i.glaccount_id = :account
AND t.fiscal_date >= :startDate
AND t.fiscal_date <= :endDate
AND (t.voided = 0 OR t.voided IS NULL )
AND t.posted = 1
GROUP BY i.glaccount_id
) AS totals ON totals.accountid = worksheet.accountid
Bookmarks