627 lines
28 KiB
PHP
627 lines
28 KiB
PHP
<?php
|
|
/* NOTES TO ME */
|
|
/*
|
|
|
|
We are developing this library to be used as global for accounting systems for any software we are going to build
|
|
|
|
|
|
Currenyly, in this function we are drawing balances based on selected branch and selected fiscal year.
|
|
We are supposing branch and fiscalyear is set to session. if no session values are found, we are assuming, fiscalyear is 1 and branch is 1
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
defined('BASEPATH') or exit('No direct script access allowed');
|
|
class newacc
|
|
{
|
|
private $FY;
|
|
private $Branch;
|
|
public function __construct()
|
|
{
|
|
$CI = &get_instance(); // Get the CodeIgniter instance
|
|
|
|
$CI->load->library('session'); // Assuming CodeIgniter's session library is already loaded
|
|
|
|
// Check if Branch is set in session
|
|
if ($CI->session->has_userdata('BranchID')) {
|
|
$this->Branch = $CI->session->userdata('BranchID');
|
|
} else {
|
|
$this->Branch = 1; // Default value if not found in session
|
|
}
|
|
|
|
// Check if FY is set in session
|
|
if ($CI->session->has_userdata('FiscalYearID')) {
|
|
$this->FY = $CI->session->userdata('FiscalYearID');
|
|
} else {
|
|
$this->FY = 1; // Default value if not found in session
|
|
}
|
|
// echo $this->FY;die;
|
|
}
|
|
function getAccountGroups(&$ACBalances = array())
|
|
{
|
|
$CI = &get_instance();
|
|
$t = "select * from tbl_acgroups where status=1";
|
|
$AccountGroups = $CI->db->query($t)->result();
|
|
foreach ($AccountGroups as $AccountGroup) :
|
|
$AccountGroup->dr = 0;
|
|
$AccountGroup->cr = 0;
|
|
$AccountGroup->openingdr = 0;
|
|
$AccountGroup->openingcr = 0;
|
|
$ACBalances = array();
|
|
$TotalBalance = $this->getGroupBalance($AccountGroup->acgroup_id, $ACBalances, 'getThisYearWithOpening');
|
|
$OpeningBalance = $this->getGroupBalance($AccountGroup->acgroup_id, $ACBalances, 'getOpeningOnly');
|
|
if ($AccountGroup->posting_side == "DR") {
|
|
$AccountGroup->dr = ($TotalBalance);
|
|
$AccountGroup->openingdr = ($OpeningBalance);
|
|
} else {
|
|
$AccountGroup->cr = ($TotalBalance);
|
|
$AccountGroup->openingcr = ($OpeningBalance);
|
|
}
|
|
endforeach;
|
|
//pre($AccountGroups);
|
|
return $AccountGroups;
|
|
}
|
|
function getChildCategories($accategory_id = 0, $condition = "showAll")
|
|
{
|
|
$ACBalances = array();
|
|
$CI = &get_instance();
|
|
$t = "select * from tbl_accategories where parent_category_id='" . $accategory_id . "' and status=1";
|
|
// echo $t;
|
|
$AccountCategories = $CI->db->query($t)->result();
|
|
foreach ($AccountCategories as $AccountCategory) :
|
|
$AccountCategory->posting_side = getFieldfromValue("tbl_acgroups", "posting_side", "acgroup_id", $AccountCategory->acgroup_id);
|
|
$AccountCategory->dr = 0;
|
|
$AccountCategory->cr = 0;
|
|
$AccountCategory->openingdr = 0;
|
|
$AccountCategory->openingcr = 0;
|
|
$ACBalances = array();
|
|
$TotalBalance = $this->getCategoryBalance($AccountCategory->accategory_id, $ACBalances);
|
|
$OpeningBalance = $this->getCategoryBalance($AccountCategory->accategory_id, $ACBalances, "getOpeningOnly");
|
|
if ($AccountCategory->posting_side == "DR") {
|
|
$AccountCategory->dr = $TotalBalance;
|
|
$AccountCategory->openingdr = $OpeningBalance;
|
|
} else {
|
|
$AccountCategory->cr = $TotalBalance;
|
|
$AccountCategory->openingcr = $OpeningBalance;
|
|
}
|
|
endforeach;
|
|
return $AccountCategories;
|
|
}
|
|
function getAccountCategories($group_id = 0, $condition = "showAll")
|
|
{
|
|
$ACBalances = array();
|
|
$CI = &get_instance();
|
|
if ($group_id == 0)
|
|
$t = "select * from tbl_accategories where status=1";
|
|
else
|
|
$t = "select * from tbl_accategories where status=1 and acgroup_id='$group_id'";
|
|
if ($condition == "onlyParents") {
|
|
$t .= " AND parent_category_id=0 ";
|
|
}
|
|
$AccountCategories = $CI->db->query($t)->result();
|
|
foreach ($AccountCategories as $AccountCategory) :
|
|
$AccountCategory->posting_side = getFieldfromValue("tbl_acgroups", "posting_side", "acgroup_id", $AccountCategory->acgroup_id);
|
|
$AccountCategory->dr = 0;
|
|
$AccountCategory->cr = 0;
|
|
$AccountCategory->openingdr = 0;
|
|
$AccountCategory->openingcr = 0;
|
|
$ACBalances = array();
|
|
$TotalBalance = $this->getCategoryBalance($AccountCategory->accategory_id, $ACBalances);
|
|
$OpeningBalance = $this->getCategoryBalance($AccountCategory->accategory_id, $ACBalances, "getOpeningOnly");
|
|
if ($AccountCategory->posting_side == "DR") {
|
|
$AccountCategory->dr = ($TotalBalance);
|
|
$AccountCategory->openingdr = ($OpeningBalance);
|
|
} else {
|
|
$AccountCategory->cr = ($TotalBalance);
|
|
$AccountCategory->openingcr = ($OpeningBalance);
|
|
}
|
|
endforeach;
|
|
return $AccountCategories;
|
|
}
|
|
|
|
function getAccountsByCategory($accategory_id)
|
|
{
|
|
$accounts = array();
|
|
$CI = &get_instance();
|
|
$t = "SELECT * FROM tbl_accategories WHERE accategory_id='$accategory_id'";
|
|
$AccountCategory = $CI->db->query($t)->row();
|
|
if ($AccountCategory) {
|
|
$accounts = $this->getAccountsRecursive($AccountCategory);
|
|
}
|
|
|
|
return $accounts;
|
|
}
|
|
|
|
|
|
function getAccountsRecursive($category)
|
|
{
|
|
$CI = &get_instance();
|
|
$t = "SELECT * FROM tbl_accounts WHERE accategory_id='$category->accategory_id'";
|
|
$accountQuery = $CI->db->query($t);
|
|
$Accounts = $accountQuery->result();
|
|
$t = "SELECT * FROM tbl_accategories WHERE parent_category_id='$category->accategory_id'";
|
|
$subcategories = $CI->db->query($t)->result();
|
|
foreach ($subcategories as $subcategory) {
|
|
$subcategoryAccounts = $this->getAccountsRecursive($subcategory);
|
|
$Accounts = array_merge($Accounts, $subcategoryAccounts);
|
|
}
|
|
foreach ($Accounts as $Account) {
|
|
$Account->dr = 0;
|
|
$Account->cr = 0;
|
|
$Account->openingdr = 0;
|
|
$Account->openingcr = 0;
|
|
|
|
$posting_side = getfieldFromValue("tbl_acgroups", "posting_side", "acgroup_id", (getfieldFromValue("tbl_accategories", "acgroup_id", "accategory_id", $Account->accategory_id)));
|
|
if ($posting_side == "DR") {
|
|
$Account->openingdr = $this->getAccountBalance($Account, $ACBalances, "getOpeningOnly");
|
|
$Account->dr = $this->getAccountBalance($Account, $ACBalances, "getAll");
|
|
} else {
|
|
$Account->openingcr = $this->getAccountBalance($Account, $ACBalances, "getOpeningOnly");
|
|
$Account->cr = $this->getAccountBalance($Account, $ACBalances, "getAll");
|
|
}
|
|
}
|
|
return $Accounts;
|
|
}
|
|
|
|
function getAccountBalanceByGroup($acgroup_id, $abs = true)
|
|
{
|
|
$total = 0;
|
|
$accounts = $this->getAccountsByGroup($acgroup_id);
|
|
foreach ($accounts as $account) {
|
|
if ($abs)
|
|
$total += (abs($account->balance));
|
|
else
|
|
$total += ($account->balance);
|
|
}
|
|
return $total;
|
|
}
|
|
function getAccountsByGroup($acgroup_id)
|
|
{
|
|
$CI = &get_instance();
|
|
$query = $CI->db->select('a.*')
|
|
->from('tbl_accounts a')
|
|
->join('tbl_accategories c', 'c.accategory_id = a.accategory_id')
|
|
->where('a.status', 1)
|
|
->where('c.acgroup_id', $acgroup_id)
|
|
->get();
|
|
$accounts = $query->result();
|
|
$ACBalances = array();
|
|
foreach ($accounts as $account) {
|
|
|
|
$account->balance = $this->getAccountBalance($account->account_id, $ACBalances);
|
|
}
|
|
|
|
return $accounts;
|
|
}
|
|
|
|
/**
|
|
* Retrieves the group balance based on the provided category ID and condition.
|
|
*
|
|
* @param stdClass $Account The group ID to retrieve the balance for.
|
|
* @param array &$ACBalances Reference to the array to store the balance results.
|
|
* @param string $condition The condition to determine which balances to retrieve. Accepted values: 'getOpeningOnly', 'getThisYear', 'getAll'.
|
|
* @return float The balance value.
|
|
*/
|
|
function getAccountBalance($Account, &$ACBalances, $condition = 'getAll')
|
|
{
|
|
/*
|
|
When $condition is set to 'getOpeningOnly', the function retrieves only the opening balances (where voucher_id = 0).
|
|
When $condition is set to 'getThisYear', the function retrieves balances for the current fiscal year (where voucher_id <> 0 and fiscalyear_id = $this->FY).
|
|
When $condition is set to 'getThisYearWithOpening', the function retrieves balances for the current fiscal year, including opening balances (where fiscalyear_id = $this->FY).
|
|
When $condition is set to 'getAll' and $this->FY is not set, the function retrieves overall balances (where voucher_id <> 0).
|
|
*/
|
|
// pre($Account);echo "NEXT";
|
|
$ci = &get_instance();
|
|
$DrBalance = 0;
|
|
$CrBalance = 0;
|
|
|
|
$t = "SELECT * FROM tbl_voucherdetails WHERE account_id = '$Account->account_id' AND status <> -1";
|
|
|
|
// Condition: Opening Only
|
|
if ($condition == 'getOpeningOnly') {
|
|
$t .= " AND voucher_id = 0";
|
|
}
|
|
// Condition: This Year
|
|
if ($condition == 'getThisYear') {
|
|
$t .= " AND voucher_id <> 0 AND fiscalyear_id = " . $this->FY;
|
|
}
|
|
// Condition: This Year with Opening
|
|
if ($condition == 'getThisYearWithOpening') {
|
|
$t .= " AND fiscalyear_id = " . $this->FY;
|
|
}
|
|
// Condition: Overall
|
|
if ($condition == 'getAll' && !isset($this->FY)) {
|
|
$t .= " AND voucher_id <> 0";
|
|
}
|
|
$t .= " AND branch_id = " . $this->Branch;
|
|
|
|
$Vouchers = $ci->db->query($t)->result();
|
|
foreach ($Vouchers as $Voucher) {
|
|
$DrBalance += $Voucher->dr;
|
|
$CrBalance += $Voucher->cr;
|
|
}
|
|
$ACBalances['DrBalance'] = $DrBalance;
|
|
$ACBalances['CrBalance'] = $CrBalance;
|
|
// $ACBalances['Balance'] = ($DrBalance > $CrBalance) ? ($DrBalance - $CrBalance) : ($CrBalance - $DrBalance);
|
|
// $ACBalances['Balance'] = $DrBalance - $CrBalance;
|
|
$posting_side = getfieldFromValue("tbl_acgroups", "posting_side", "acgroup_id", (getfieldFromValue("tbl_accategories", "acgroup_id", "accategory_id", $Account->accategory_id)));
|
|
if ($posting_side == "DR") {
|
|
$ACBalances['Balance'] = $DrBalance - $CrBalance;
|
|
} else {
|
|
$ACBalances['Balance'] = $CrBalance - $DrBalance;
|
|
}
|
|
return $ACBalances['Balance'];
|
|
}
|
|
/**
|
|
* Retrieves the group balance based on the provided category ID and condition.
|
|
*
|
|
* @param int $group_id The group ID to retrieve the balance for.
|
|
* @param array &$ACBalances Reference to the array to store the balance results.
|
|
* @param string $condition The condition to determine which balances to retrieve. Accepted values: 'getOpeningOnly', 'getThisYear', 'getAll'.
|
|
* @return float The balance value.
|
|
*/
|
|
function getGroupBalance($group_id, &$ACBalances, $condition = 'getAll')
|
|
{
|
|
/*
|
|
When $condition is set to 'getOpeningOnly', the function retrieves only the opening balances (where voucher_id = 0).
|
|
When $condition is set to 'getThisYear', the function retrieves balances for the current fiscal year (where voucher_id <> 0 and fiscalyear_id = $this->FY).
|
|
When $condition is set to 'getThisYearWithOpening', the function retrieves balances for the current fiscal year, including opening balances (where fiscalyear_id = $this->FY).
|
|
When $condition is set to 'getAll' and $this->FY is not set, the function retrieves overall balances (where voucher_id <> 0).
|
|
*/
|
|
$ci = &get_instance();
|
|
$t = "SELECT * FROM tbl_accounts WHERE accategory_id IN (SELECT accategory_id FROM tbl_accategories AS a WHERE a.acgroup_id = '$group_id' AND a.status = 1) AND status <> -1";
|
|
$Accounts = $ci->db->query($t)->result();
|
|
$Balance = 0;
|
|
foreach ($Accounts as $Account) {
|
|
$Balance += $this->getAccountBalance($Account, $ACBalances, $condition);
|
|
}
|
|
return $Balance;
|
|
}
|
|
/**
|
|
* Retrieves the group balance based on the provided category ID and condition.
|
|
*
|
|
* @param int $group_id The group ID to retrieve the balance for.
|
|
* @param array &$ACBalances Reference to the array to store the balance results.
|
|
* @param string $condition The condition to determine which balances to retrieve. Accepted values: 'getOpeningOnly', 'getThisYear', 'getAll'.
|
|
* @return float The balance value.
|
|
*/
|
|
function getCategoryBalance($accategory_id, &$ACBalances, $condition = "getAll")
|
|
{
|
|
$ci = &get_instance();
|
|
$Balance = 0;
|
|
$Accounts = $this->getAccountsByCategory($accategory_id);
|
|
//pre($Accounts);die;
|
|
foreach ($Accounts as $Account) {
|
|
// print_r($Account);die;
|
|
$Balance += $this->getAccountBalance($Account, $ACBalances, $condition);
|
|
}
|
|
return $Balance;
|
|
}
|
|
|
|
|
|
function showTable($Accounts, $abs = true, $showHead = false)
|
|
{
|
|
$total = 0;
|
|
$accColWidth = "col-9";
|
|
$balanceColWidth = "col-3";
|
|
?>
|
|
<table class="table table-bordered table-hover">
|
|
<?php if ($showHead) : ?>
|
|
<thead>
|
|
<tr>
|
|
<th class="<?php echo $accColWidth; ?>">Account</th>
|
|
<th class="<?php echo $balanceColWidth; ?>">Balance</th>
|
|
|
|
</tr>
|
|
</thead>
|
|
<?php endif; ?>
|
|
<tbody>
|
|
<?php foreach ($Accounts as $Account) :
|
|
if ($abs)
|
|
$total += abs($Account->balance);
|
|
else
|
|
$total += $Account->balance;
|
|
?>
|
|
<tr>
|
|
<td class="<?php echo $accColWidth; ?>"><?php echo $Account->account_name; ?></td>
|
|
<td class="<?php echo $balanceColWidth; ?>"><?php echo ($abs) ? myCurrency(abs($Account->balance)) : myCurrency($Account->balance); ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
|
|
</tbody>
|
|
<!-- <tfoot>
|
|
<tr>
|
|
<th>Total</th>
|
|
<th><?php echo myCurrency($total); ?></th>
|
|
|
|
</tr>
|
|
</tfoot> -->
|
|
</table>
|
|
|
|
<?php
|
|
}
|
|
|
|
/////////////////////////////////////////////////
|
|
|
|
/**
|
|
* Retrieves the account groups with balances, optionally filtered by group ID.
|
|
*
|
|
* This function queries the database to fetch the account groups from the "tbl_acgroups" table and calculates the totals of balances for each group by summing the account balances within each group. If the optional "acgroup_id" parameter is provided, the function returns only the group with the specified ID. Otherwise, it returns an array of stdClass objects representing each account group with the corresponding balance totals.
|
|
*
|
|
* @param int|null $acgroup_id (Optional) The ID of the account group to retrieve balances for.
|
|
* @return array|stdClass|null An array of stdClass objects representing the account groups with balance totals, or a single stdClass object for the specified group ID, or null if no group is found with the provided ID.
|
|
*/
|
|
function getAccountGroupsWithBalances($acgroup_id = null)
|
|
{
|
|
$CI = &get_instance();
|
|
$t = "SELECT * FROM tbl_acgroups";
|
|
|
|
if ($acgroup_id !== null) {
|
|
$t .= " WHERE acgroup_id='$acgroup_id'";
|
|
$result = $CI->db->query($t)->row();
|
|
|
|
if ($result === null) {
|
|
return null; // Return null if no group is found with the provided ID
|
|
}
|
|
|
|
$result->accounts = $this->generateAccountsByGroup($result->acgroup_id); // Call the generateAccountsByGroup function to retrieve the accounts with balances
|
|
|
|
// Calculate group balances based on the account balances
|
|
$result->dr_total = 0;
|
|
$result->cr_total = 0;
|
|
$result->dr_balance = 0;
|
|
$result->cr_balance = 0;
|
|
|
|
foreach ($result->accounts as $category) {
|
|
$result->dr_total += $category->dr_total;
|
|
$result->cr_total += $category->cr_total;
|
|
$result->dr_balance += $category->dr_balance;
|
|
$result->cr_balance += $category->cr_balance;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
$accountGroups = $CI->db->query($t)->result();
|
|
$result = [];
|
|
|
|
foreach ($accountGroups as $group) {
|
|
$group->accounts = $this->generateAccountsByGroup($group->acgroup_id); // Call the generateAccountsByGroup function to retrieve the accounts with balances
|
|
|
|
// Calculate group balances based on the account balances
|
|
$group->dr_total = 0;
|
|
$group->cr_total = 0;
|
|
$group->dr_balance = 0;
|
|
$group->cr_balance = 0;
|
|
|
|
foreach ($group->accounts as $category) {
|
|
$group->dr_total += $category->dr_total;
|
|
$group->cr_total += $category->cr_total;
|
|
$group->dr_balance += $category->dr_balance;
|
|
$group->cr_balance += $category->cr_balance;
|
|
}
|
|
|
|
$result[] = $group;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Retrieves the categories with balances that fall under the provided account group ID and optionally filters by parent categories.
|
|
*
|
|
* This function queries the database to fetch the categories from the "tbl_accategories" table associated with the provided account group ID (`acgroup_id`) and calculates the totals of balances for each category by summing the account balances within each category. If the `$acgroup_id` is not specified, it fetches all categories. It returns an array of stdClass objects representing the categories with their respective balance totals.
|
|
*
|
|
* @param int|null $acgroup_id Optional. The ID of the account group to retrieve categories and balances for. If not specified, it fetches categories from all groups.
|
|
* @param bool $onlyParents Optional. Specifies whether to include only parent categories (categories with parent_category_id = 0). Default is false.
|
|
* @return array An array of stdClass objects representing the categories with balance totals under the specified account group ID or all groups if not specified.
|
|
*/
|
|
function getCategoriesWithBalances($acgroup_id = null, $onlyParents = false)
|
|
{
|
|
$CI = &get_instance();
|
|
$t = "SELECT * FROM tbl_accategories";
|
|
|
|
if ($acgroup_id !== null) {
|
|
$t .= " WHERE acgroup_id='$acgroup_id'";
|
|
}
|
|
|
|
if ($onlyParents) {
|
|
$t .= " AND parent_category_id = 0";
|
|
}
|
|
|
|
$categories = $CI->db->query($t)->result();
|
|
$result = [];
|
|
|
|
foreach ($categories as $category) {
|
|
$category->accounts = $this->generateAccountsByCategory($category->accategory_id); // Call the generateAccountsByCategory function to retrieve the accounts with balances
|
|
|
|
// Calculate category balances based on the account balances
|
|
$category->dr_total = 0;
|
|
$category->cr_total = 0;
|
|
$category->dr_balance = 0;
|
|
$category->cr_balance = 0;
|
|
$category->opening_balance_dr = 0;
|
|
$category->opening_balance_cr = 0;
|
|
$category->regular_balance_dr = 0;
|
|
$category->regular_balance_cr = 0;
|
|
$category->total_balance_dr = 0;
|
|
$category->total_balance_cr = 0;
|
|
|
|
foreach ($category->accounts as $account) {
|
|
$balances = $this->getAccountBalances($account->account_id);
|
|
|
|
$category->dr_total += $balances['dr_total'];
|
|
$category->cr_total += $balances['cr_total'];
|
|
$category->dr_balance += $balances['dr_balance'];
|
|
$category->cr_balance += $balances['cr_balance'];
|
|
|
|
$category->opening_balance_dr += $balances['opening_balance_dr'];
|
|
$category->opening_balance_cr += $balances['opening_balance_cr'];
|
|
$category->regular_balance_dr += $balances['regular_balance_dr'];
|
|
$category->regular_balance_cr += $balances['regular_balance_cr'];
|
|
$category->total_balance_dr += $balances['total_balance_dr'];
|
|
$category->total_balance_cr += $balances['total_balance_cr'];
|
|
}
|
|
|
|
$result[] = $category;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function generateAccountsByGroup($group_id, $fiscal_year_id = "", $branch_id = "")
|
|
{
|
|
$CI = &get_instance();
|
|
$t = "SELECT * FROM tbl_accategories WHERE acgroup_id='$group_id'";
|
|
$categories = $CI->db->query($t)->result();
|
|
|
|
$result = [];
|
|
foreach ($categories as $category) {
|
|
$category->accounts = $this->generateAccountsByCategory($category->accategory_id, $fiscal_year_id, $branch_id); // Call the generateAccountsByCategory function to retrieve the accounts with balances
|
|
|
|
// Calculate category balances based on the account balances
|
|
$category->dr_total = 0;
|
|
$category->cr_total = 0;
|
|
$category->dr_balance = 0;
|
|
$category->cr_balance = 0;
|
|
$category->opening_balance_dr = 0;
|
|
$category->opening_balance_cr = 0;
|
|
$category->regular_balance_dr = 0;
|
|
$category->regular_balance_cr = 0;
|
|
|
|
foreach ($category->accounts as $account) {
|
|
$category->dr_total += $account->dr_total;
|
|
$category->cr_total += $account->cr_total;
|
|
$category->dr_balance += $account->dr_balance;
|
|
$category->cr_balance += $account->cr_balance;
|
|
$category->opening_balance_dr += $account->opening_balance_dr;
|
|
$category->opening_balance_cr += $account->opening_balance_cr;
|
|
$category->regular_balance_dr += $account->regular_balance_dr;
|
|
$category->regular_balance_cr += $account->regular_balance_cr;
|
|
}
|
|
|
|
$result[] = $category;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
|
|
|
|
function generateAccountsByCategory($accategory_id, $fiscal_year_id = "", $branch_id = "")
|
|
{
|
|
$CI = &get_instance();
|
|
$t = "SELECT * FROM tbl_accounts WHERE accategory_id='$accategory_id'";
|
|
$accounts = $CI->db->query($t)->result();
|
|
|
|
$result = [];
|
|
foreach ($accounts as $account) {
|
|
$accountBalances = $this->getAccountBalances($account->account_id, $fiscal_year_id ?: $this->FY, $branch_id ?: $this->Branch);
|
|
$account->dr_total = $accountBalances['dr_total'];
|
|
$account->cr_total = $accountBalances['cr_total'];
|
|
$account->dr_balance = $accountBalances['dr_balance'];
|
|
$account->cr_balance = $accountBalances['cr_balance'];
|
|
$account->opening_balance_dr = $accountBalances['opening_balance_dr'];
|
|
$account->opening_balance_cr = $accountBalances['opening_balance_cr'];
|
|
$account->regular_balance_dr = $accountBalances['regular_balance_dr'];
|
|
$account->regular_balance_cr = $accountBalances['regular_balance_cr'];
|
|
$result[] = $account;
|
|
}
|
|
|
|
$t = "SELECT * FROM tbl_accategories WHERE parent_category_id='$accategory_id'";
|
|
$categories = $CI->db->query($t)->result();
|
|
|
|
foreach ($categories as $category) {
|
|
$category->accounts = $this->generateAccountsByCategory($category->accategory_id, $fiscal_year_id, $branch_id);
|
|
|
|
$category->dr_total = 0;
|
|
$category->cr_total = 0;
|
|
$category->dr_balance = 0;
|
|
$category->cr_balance = 0;
|
|
$category->opening_balance_dr = 0;
|
|
$category->opening_balance_cr = 0;
|
|
$category->regular_balance_dr = 0;
|
|
$category->regular_balance_cr = 0;
|
|
|
|
foreach ($category->accounts as $account) {
|
|
$category->dr_total += $account->dr_total;
|
|
$category->cr_total += $account->cr_total;
|
|
$category->dr_balance += $account->dr_balance;
|
|
$category->cr_balance += $account->cr_balance;
|
|
$category->opening_balance_dr += $account->opening_balance_dr;
|
|
$category->opening_balance_cr += $account->opening_balance_cr;
|
|
$category->regular_balance_dr += $account->regular_balance_dr;
|
|
$category->regular_balance_cr += $account->regular_balance_cr;
|
|
}
|
|
|
|
$result[] = $category;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* Retrieves the balance of an account based on the provided account ID.
|
|
*
|
|
* This function calculates the balance of an account by summing the "dr" and "cr" columns from the "tbl_voucherdetails" table associated with the provided account ID. It also calculates the `dr_balance` and `cr_balance` based on the `posting_side` from the related tables. Additionally, it retrieves the opening balance by considering the records with `voucher_id` equal to 0 as opening balances. It returns an associative array with the balance values.
|
|
*
|
|
* @param int $account_id The account ID to retrieve the balance for.
|
|
* @param int $fiscal_year_id The fiscal year ID to filter the voucher details.
|
|
* @param int $branch_id The branch ID to filter the voucher details.
|
|
* @return array An associative array with the balance of the account, including the total of the "dr" column as 'dr_total', the total of the "cr" column as 'cr_total', the calculated "dr_balance", the calculated "cr_balance", the opening balance as 'opening_balance_dr', the opening balance as 'opening_balance_cr', the regular balance as 'regular_balance_dr', and the regular balance as 'regular_balance_cr'.
|
|
*/
|
|
function getAccountBalances($account_id, $fiscal_year_id = "", $branch_id = "")
|
|
{
|
|
$fiscal_year_id = ($fiscal_year_id == "") ? $this->FY : $fiscal_year_id;
|
|
$branch_id = ($branch_id == "") ? $this->Branch : $branch_id;
|
|
$CI = &get_instance();
|
|
$query = $CI->db->query("SELECT SUM(dr) AS dr_total, SUM(cr) AS cr_total FROM tbl_voucherdetails WHERE account_id='$account_id' AND status=1 AND fiscalyear_id='$fiscal_year_id' AND branch_id='$branch_id'");
|
|
$result = $query->row();
|
|
|
|
$drTotal = $result->dr_total;
|
|
$crTotal = $result->cr_total;
|
|
|
|
$posting_side = $CI->db->query("SELECT posting_side FROM tbl_acgroups WHERE acgroup_id=(SELECT acgroup_id FROM tbl_accategories WHERE accategory_id=(SELECT accategory_id FROM tbl_accounts WHERE account_id='$account_id'))")->row()->posting_side;
|
|
|
|
$drBalance = 0;
|
|
$crBalance = 0;
|
|
|
|
if ($posting_side == "DR") {
|
|
$drBalance = $drTotal - $crTotal;
|
|
} else {
|
|
$crBalance = $crTotal - $drTotal;
|
|
}
|
|
|
|
$openingBalanceQuery = $CI->db->query("SELECT SUM(dr) AS opening_balance_dr, SUM(cr) AS opening_balance_cr FROM tbl_voucherdetails WHERE account_id='$account_id' AND voucher_id=0 AND status=1 AND fiscalyear_id='$fiscal_year_id' AND branch_id='$branch_id'");
|
|
$openingBalanceResult = $openingBalanceQuery->row();
|
|
|
|
$openingBalanceDr = $openingBalanceResult->opening_balance_dr;
|
|
$openingBalanceCr = $openingBalanceResult->opening_balance_cr;
|
|
|
|
$regularBalanceQuery = $CI->db->query("SELECT SUM(dr) AS regular_balance_dr, SUM(cr) AS regular_balance_cr FROM tbl_voucherdetails WHERE account_id='$account_id' AND voucher_id<>0 AND status=1 AND fiscalyear_id='$fiscal_year_id' AND branch_id='$branch_id'");
|
|
$regularBalanceResult = $regularBalanceQuery->row();
|
|
|
|
$regularBalanceDr = $regularBalanceResult->regular_balance_dr;
|
|
$regularBalanceCr = $regularBalanceResult->regular_balance_cr;
|
|
|
|
return array(
|
|
'dr_total' => $drTotal,
|
|
'cr_total' => $crTotal,
|
|
'dr_balance' => $drBalance,
|
|
'cr_balance' => $crBalance,
|
|
'opening_balance_dr' => $openingBalanceDr,
|
|
'opening_balance_cr' => $openingBalanceCr,
|
|
'regular_balance_dr' => $regularBalanceDr,
|
|
'regular_balance_cr' => $regularBalanceCr,
|
|
);
|
|
}
|
|
}
|