SELECT IFNULL(ob.debit, 0) as oDebit, sum(b.amount_dr) as currentyeardebit, IFNULL(ob.credit, 0) as oCredit, sum(b.amount_cr) as currentyearcredit, sum(b.amount_dr - b.amount_cr) as total, `a`.*, IFNULL(sum(c.amountcr), 0) as budgetAmount, `d`.`acctTypeID`
FROM (`comp_chart_of_acct` a)
LEFT OUTER JOIN `comp_journal_detail` b ON `a`.`ID` = `b`.`acctCode` and b.year=2015 and b.period <= 7 and b.companyID = 3
JOIN `_ref_acct_group` d ON `d`.`ID` = `a`.`acctGroupID` and (d.acctTypeID = "3" OR d.acctTypeID = "4")
LEFT OUTER JOIN `comp_opening_balance` ob ON `ob`.`companyID` = `a`.`companyID` and ob.accountID = a.ID and ob.year = b.year
LEFT OUTER JOIN `comp_budget` c ON `c`.`acctID` = `a`.`ID` and c.year = b.year and c.companyID = a.companyID
WHERE `a`.`companyID` =  '3'
GROUP BY `a`.`ID`
ORDER BY `a`.`acctCode`