commitall

This commit is contained in:
Sampanna Rimal
2024-07-10 18:28:19 +05:45
parent 140abda4e6
commit 9cd05ef3cb
15723 changed files with 4818733 additions and 0 deletions

View File

@ -0,0 +1,287 @@
<?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 acc
{
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;
return $AccountGroups;
}
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;
$ACBalances = array();
$TotalBalance = $this->getCategoryBalance($AccountCategory->accategory_id, $ACBalances);
if ($AccountCategory->posting_side == "DR") {
$AccountCategory->dr = ($TotalBalance);
} else {
$AccountCategory->cr = ($TotalBalance);
}
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_array();
$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);
}
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;
}
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();
$DrBalance = 0;
$CrBalance = 0;
foreach ($Accounts as $Account) {
$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);
return $ACBalances['Balance'];
}
function getCategoryBalance($accategory_id, &$ACBalances)
{
$ci = &get_instance();
// $t = "SELECT * FROM tbl_accounts WHERE status=1 and accategory_id ='$accategory_id' or accategory_id in (select accategory_id from tbl_categories where parent_category_id='$accategory_id')";
// $Accounts = $ci->db->query($t)->result();
// $groupBalance = 0;
$DrBalance = 0;
$CrBalance = 0;
$Accounts = $this->getAccountsByCategory($accategory_id);
foreach ($Accounts as $Account) {
// $t = "SELECT * FROM tbl_voucherdetails WHERE account_id = '$Account->account_id' AND status <> -1";
$t = "SELECT * FROM tbl_voucherdetails WHERE account_id = '" . $Account['account_id'] . "' AND status <> -1";
$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);
return $ACBalances['Balance'];
}
function getAccountBalance($account_id, &$ACBalances)
{
$ci = &get_instance();
$t = "SELECT * FROM tbl_voucherdetails WHERE account_id = '$account_id' AND status <> -1";
$Vouchers = $ci->db->query($t)->result();
$DrBalance = 0;
$CrBalance = 0;
foreach ($Vouchers as $Voucher) {
$DrBalance += $Voucher->dr;
$CrBalance += $Voucher->cr;
}
$ACBalances['DrBalance'] = $DrBalance;
$ACBalances['CrBalance'] = $CrBalance;
$ACBalances['Balance'] = $DrBalance - $CrBalance;
return $ACBalances['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
}
}

View File

@ -0,0 +1,288 @@
<?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 acc
{
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;
return $AccountGroups;
}
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;
$ACBalances = array();
$TotalBalance = $this->getCategoryBalance($AccountCategory->accategory_id, $ACBalances);
if ($AccountCategory->posting_side == "DR") {
$AccountCategory->dr = ($TotalBalance);
} else {
$AccountCategory->cr = ($TotalBalance);
}
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_array();
$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);
}
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;
}
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();
$DrBalance = 0;
$CrBalance = 0;
foreach ($Accounts as $Account) {
$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);
return $ACBalances['Balance'];
}
function getCategoryBalance($accategory_id, &$ACBalances)
{
$ci = &get_instance();
// $t = "SELECT * FROM tbl_accounts WHERE status=1 and accategory_id ='$accategory_id' or accategory_id in (select accategory_id from tbl_categories where parent_category_id='$accategory_id')";
// $Accounts = $ci->db->query($t)->result();
// $groupBalance = 0;
$DrBalance = 0;
$CrBalance = 0;
$Accounts = $this->getAccountsByCategory($accategory_id);
foreach ($Accounts as $Account) {
// $t = "SELECT * FROM tbl_voucherdetails WHERE account_id = '$Account->account_id' AND status <> -1";
$t = "SELECT * FROM tbl_voucherdetails WHERE account_id = '" . $Account['account_id'] . "' AND status <> -1";
$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);
return $ACBalances['Balance'];
}
function getAccountBalance($account_id, &$ACBalances)
{
$ci = &get_instance();
$t = "SELECT * FROM tbl_voucherdetails WHERE account_id = '$account_id' AND status <> -1";
$Vouchers = $ci->db->query($t)->result();
$DrBalance = 0;
$CrBalance = 0;
foreach ($Vouchers as $Voucher) {
$DrBalance += $Voucher->dr;
$CrBalance += $Voucher->cr;
}
$ACBalances['DrBalance'] = $DrBalance;
$ACBalances['CrBalance'] = $CrBalance;
$ACBalances['Balance'] = $DrBalance - $CrBalance;
return $ACBalances['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
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,808 @@
<?php
/*
NOTES TO ME
*/
defined('BASEPATH') or exit('No direct script access allowed');
class bibaccounts
{
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;
}
/**
* 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();
$result = [];
$whereClause = "";
if ($acgroup_id !== null) {
$whereClause = "WHERE acgroup_id = '$acgroup_id' AND status=1";
}
$t = "SELECT * FROM tbl_acgroups $whereClause";
$accountGroups = $CI->db->query($t)->result();
foreach ($accountGroups as $group) {
$t = "SELECT * FROM tbl_accounts
WHERE (accategory_id IN (SELECT accategory_id FROM tbl_accategories WHERE acgroup_id = '{$group->acgroup_id}')
OR accategory_id IN (SELECT parent_category_id FROM tbl_accategories WHERE accategory_id IN (SELECT accategory_id FROM tbl_accategories WHERE acgroup_id = '{$group->acgroup_id}')))
AND status <> -1";
$accounts = $CI->db->query($t)->result();
$group->dr_total = 0;
$group->cr_total = 0;
$group->regular_balance_dr = 0;
$group->regular_balance_cr = 0;
$group->opening_balance_dr = 0;
$group->opening_balance_cr = 0;
foreach ($accounts as $account) {
$accountBalances = $this->getAccountBalances($account->account_id);
// pre($accountBalances);die;
$group->opening_balance_dr += $accountBalances['opening_balance_dr'];
$group->opening_balance_cr += $accountBalances['opening_balance_cr'];
$group->dr_total += $accountBalances['dr_total'];
$group->cr_total += $accountBalances['cr_total'];
$group->regular_balance_dr += $accountBalances['regular_balance_dr'];
$group->regular_balance_cr += $accountBalances['regular_balance_cr'];
}
if (
$group->dr_total > 0 &&
$group->cr_total > 0 &&
$group->regular_balance_dr > 0 &&
$group->regular_balance_cr > 0 &&
$group->opening_balance_dr > 0 &&
$group->opening_balance_cr > 0
) $group->isZero = 1;
else $group->isZero = 0;
$group->opening_balance = ($group->posting_side == "DR") ? $group->opening_balance_dr - $group->opening_balance_cr : $group->opening_balance_cr - $group->opening_balance_dr;
$group->closing_balance = ($group->posting_side == "DR") ? $group->dr_total - $group->cr_total : $group->cr_total - $group->dr_total;
$group->regular_balance = ($group->posting_side == "DR") ? $group->regular_balance_dr - $group->regular_balance_cr : $group->regular_balance_cr - $group->regular_balance_dr;
$group->accounts = $accounts;
$result[] = $group;
}
// pre($result);die;
return $result;
}
function getAccountCategoriesWithBalances($acgroup_id = null)
{
$CI = &get_instance();
$result = [];
$whereClause = "";
if ($acgroup_id !== null) {
$whereClause = "WHERE acgroup_id = '$acgroup_id' AND status=1";
}
$t = "SELECT * FROM tbl_accategories $whereClause";
$categories = $CI->db->query($t)->result();
foreach ($categories as $category) {
$t = "SELECT * FROM tbl_accounts
WHERE accategory_id = '{$category->accategory_id}'
AND status <> -1";
$accounts = $CI->db->query($t)->result();
$category->isParent = ($CI->db->query("SELECT * FROM tbl_accategories WHERE parent_category_id = '" . $category->accategory_id . "'")->num_rows() > 0) ? true : false;
$category->dr_total = 0;
$category->cr_total = 0;
$category->regular_balance_dr = 0;
$category->regular_balance_cr = 0;
$category->opening_balance_dr = 0;
$category->opening_balance_cr = 0;
foreach ($accounts as $account) {
$accountBalances = $this->getAccountBalances($account->account_id);
$category->opening_balance_dr += $accountBalances['opening_balance_dr'];
$category->opening_balance_cr += $accountBalances['opening_balance_cr'];
$category->dr_total += $accountBalances['dr_total'];
$category->cr_total += $accountBalances['cr_total'];
$category->regular_balance_dr += $accountBalances['regular_balance_dr'];
$category->regular_balance_cr += $accountBalances['regular_balance_cr'];
}
$category->posting_side = $CI->db->query("SELECT posting_side FROM tbl_acgroups WHERE acgroup_id=(SELECT acgroup_id FROM tbl_accategories WHERE accategory_id='" . $category->accategory_id . "')")->row()->posting_side;
$category->opening_balance = ($category->posting_side == "DR") ? $category->opening_balance_dr - $category->opening_balance_cr : $category->opening_balance_cr - $category->opening_balance_dr;
$category->closing_balance = ($category->posting_side == "DR") ? $category->dr_total - $category->cr_total : $category->cr_total - $category->dr_total;
$category->regular_balance = ($category->posting_side == "DR") ? $category->regular_balance_dr - $category->regular_balance_cr : $category->regular_balance_cr - $category->regular_balance_dr;
$category->accounts = $accounts;
$result[] = $category;
}
return $result;
}
function getAccountsByCategory($accategory_id)
{
$accounts = array();
$CI = &get_instance();
$t = "SELECT * FROM tbl_accategories WHERE accategory_id='$accategory_id' and status=1";
$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' and status=1";
$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);
}
// Remove duplicate accounts based on account_id
$uniqueAccounts = array_column($accounts, null, 'account_id');
return array_values($uniqueAccounts);
}
function getRootCategoriesWithBalances($acgroup_id = null)
{
$CI = &get_instance();
$result = [];
$whereClause = "WHERE parent_category_id = '0' AND status = '1'";
if ($acgroup_id !== null) {
$whereClause .= " AND acgroup_id = '$acgroup_id'";
}
$t = "SELECT * FROM tbl_accategories $whereClause";
$categories = $CI->db->query($t)->result();
foreach ($categories as $category) {
$accounts = $this->getAccountsByCategory($category->accategory_id);
$category->isParent = ($CI->db->query("SELECT * FROM tbl_accategories WHERE parent_category_id = '" . $category->accategory_id . "'")->num_rows() > 0) ? true : false;
$category->dr_total = 0;
$category->cr_total = 0;
$category->regular_balance_dr = 0;
$category->regular_balance_cr = 0;
$category->opening_balance_dr = 0;
$category->opening_balance_cr = 0;
foreach ($accounts as $account) {
// pre($account);
$accountBalances = $this->getAccountBalances($account->account_id);
$category->opening_balance_dr += $accountBalances['opening_balance_dr'];
$category->opening_balance_cr += $accountBalances['opening_balance_cr'];
$category->dr_total += $accountBalances['dr_total'];
$category->cr_total += $accountBalances['cr_total'];
$category->regular_balance_dr += $accountBalances['regular_balance_dr'];
$category->regular_balance_cr += $accountBalances['regular_balance_cr'];
}
$category->posting_side = $CI->db->query("SELECT posting_side FROM tbl_acgroups WHERE acgroup_id=(SELECT acgroup_id FROM tbl_accategories WHERE accategory_id='" . $category->accategory_id . "')")->row()->posting_side;
$category->opening_balance = ($category->posting_side == "DR") ? $category->opening_balance_dr - $category->opening_balance_cr : $category->opening_balance_cr - $category->opening_balance_dr;
$category->closing_balance = ($category->posting_side == "DR") ? $category->dr_total - $category->cr_total : $category->cr_total - $category->dr_total;
$category->regular_balance = ($category->posting_side == "DR") ? $category->regular_balance_dr - $category->regular_balance_cr : $category->regular_balance_cr - $category->regular_balance_dr;
$category->isZero = ($category->dr_total == 0 && $category->cr_total == 0 && $category->opening_balance_dr == 0 && $category->opening_balance_cr == 0 && $category->regular_balance_dr == 0 && $category->regular_balance_cr == 0) ? 1 : 0;
$category->accounts = $accounts;
$result[] = $category;
}
return $result;
}
function getChildCategoriesWithBalances($accategory_id)
{
$CI = &get_instance();
$result = [];
$t = "SELECT * FROM tbl_accategories WHERE parent_category_id = '$accategory_id' AND status=1";
$categories = $CI->db->query($t)->result();
foreach ($categories as $category) {
$t = "SELECT * FROM tbl_accounts
WHERE accategory_id = '{$category->accategory_id}'
AND status <> -1";
$accounts = $CI->db->query($t)->result();
$category->isParent = ($CI->db->query("SELECT * FROM tbl_accategories WHERE parent_category_id = '" . $category->accategory_id . "'")->num_rows() > 0) ? true : false;
$category->dr_total = 0;
$category->cr_total = 0;
$category->regular_balance_dr = 0;
$category->regular_balance_cr = 0;
$category->opening_balance_dr = 0;
$category->opening_balance_cr = 0;
foreach ($accounts as $account) {
$accountBalances = $this->getAccountBalances($account->account_id);
$category->opening_balance_dr += $accountBalances['opening_balance_dr'];
$category->opening_balance_cr += $accountBalances['opening_balance_cr'];
$category->dr_total += $accountBalances['dr_total'];
$category->cr_total += $accountBalances['cr_total'];
$category->regular_balance_dr += $accountBalances['regular_balance_dr'];
$category->regular_balance_cr += $accountBalances['regular_balance_cr'];
}
$category->posting_side = $CI->db->query("SELECT posting_side FROM tbl_acgroups WHERE acgroup_id=(SELECT acgroup_id FROM tbl_accategories WHERE accategory_id='" . $category->accategory_id . "')")->row()->posting_side;
$category->opening_balance = ($category->posting_side == "DR") ? $category->opening_balance_dr - $category->opening_balance_cr : $category->opening_balance_cr - $category->opening_balance_dr;
$category->closing_balance = ($category->posting_side == "DR") ? $category->dr_total - $category->cr_total : $category->cr_total - $category->dr_total;
$category->regular_balance = ($category->posting_side == "DR") ? $category->regular_balance_dr - $category->regular_balance_cr : $category->regular_balance_cr - $category->regular_balance_dr;
$category->isZero = ($category->dr_total == 0 && $category->cr_total == 0 && $category->opening_balance_dr == 0 && $category->opening_balance_cr == 0 && $category->regular_balance_dr == 0 && $category->regular_balance_cr == 0) ? 1 : 0;
$category->accounts = $accounts;
$result[] = $category;
}
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;
}
/**
* Retrieves the child categories with balances based on the provided parent category ID.
*
* This function queries the database to fetch the child categories from the "tbl_accategories" table associated with the provided parent category ID (`accategory_id`) and includes the balances for each category by calling the `getAccountBalances` function. It returns an array of stdClass objects representing the child categories with their respective balances.
*
* @param int $accategory_id The ID of the parent category to retrieve child categories for.
* @return array An array of stdClass objects representing the child categories with balances under the specified parent category ID.
*/
function getChildCategories($accategory_id)
{
$CI = &get_instance();
$t = "SELECT * FROM tbl_accategories WHERE parent_category_id='$accategory_id'";
$childCategories = $CI->db->query($t)->result();
foreach ($childCategories as $category) {
$category->accounts = $this->generateAccountsByCategory($category->accategory_id); // Call the generateAccountsByCategory function to retrieve the accounts with balances
// Initialize category 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) {
$accountBalances = $this->getAccountBalances($account->account_id);
$category->dr_total += $accountBalances['dr_total'];
$category->cr_total += $accountBalances['cr_total'];
$category->dr_balance += $accountBalances['dr_balance'];
$category->cr_balance += $accountBalances['cr_balance'];
$category->opening_balance_dr += $accountBalances['opening_balance_dr'];
$category->opening_balance_cr += $accountBalances['opening_balance_cr'];
$category->regular_balance_dr += $accountBalances['regular_balance_dr'];
$category->regular_balance_cr += $accountBalances['regular_balance_cr'];
$category->total_balance_dr += $accountBalances['total_balance_dr'];
$category->total_balance_cr += $accountBalances['total_balance_cr'];
}
}
return $childCategories;
}
/**
* Generates the account hierarchy based on the provided group ID, fiscal year ID, and branch ID.
*
* This function retrieves the accounts and categories associated with the provided group ID (`$group_id`) and generates a hierarchical structure using stdClass objects. It uses recursion to handle any depth of child categories. It includes the account balances in each account object.
*
* @param int $group_id The group ID to start the hierarchy generation from.
* @param string $fiscal_year_id Optional. The fiscal year ID to filter the accounts. Default is an empty string.
* @param string $branch_id Optional. The branch ID to filter the accounts. Default is an empty string.
* @return array An array of stdClass objects representing the account hierarchy with account balances.
*/
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;
}
//pre($category);die;
$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' and status=1";
$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;
}
function generateAccountsBySingleCategory($accategory_id, $fiscal_year_id = "", $branch_id = "")
{
$CI = &get_instance();
$t = "SELECT * FROM tbl_accounts WHERE accategory_id='$accategory_id' and status=1";
$accounts = $CI->db->query($t)->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'];
$account->posting_side = $CI->db->query("SELECT posting_side FROM tbl_acgroups WHERE acgroup_id=(SELECT acgroup_id FROM tbl_accategories WHERE accategory_id='" . $account->accategory_id . "')")->row()->posting_side;
$account->opening_balance = ($account->posting_side == "DR") ? $account->opening_balance_dr - $account->opening_balance_cr : $account->opening_balance_cr - $account->opening_balance_dr;
$account->closing_balance = ($account->posting_side == "DR") ? $account->dr_total - $account->cr_total : $account->cr_total - $account->dr_total;
$account->regular_balance = ($account->posting_side == "DR") ? $account->regular_balance_dr - $account->regular_balance_cr : $account->regular_balance_cr - $account->regular_balance_dr;
$account->isZero = ($account->dr_total == 0 && $account->cr_total == 0 && $account->opening_balance_dr == 0 && $account->opening_balance_cr == 0 && $account->regular_balance_dr == 0 && $account->regular_balance_cr == 0) ? 1 : 0;
}
return $accounts;
}
/**
* 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,
);
}
//////
function renderAccountCategoriesTable($accountCategories, $ReportOptions, $displayHeadings = true, $parentCategories = [])
{
if(!isset($ReportOptions['AmountColWidth']))$ReportOptions['AmountColWidth']=180;
?>
<table class="table table-sm">
<?php if ($displayHeadings) { ?>
<thead>
<tr>
<th>Particulars</th>
<?php if ($ReportOptions['showOB']) : ?>
<th class="dr opening-dr">Opening (Dr)</th>
<th class="cr opening-cr">Opening (Cr)</th>
<?php endif; ?>
<?php if ($ReportOptions['showPeriod']) : ?>
<th class="dr this-year-dr">This Year (Dr)</th>
<th class="cr this-year-cr">This Year (Cr)</th>
<?php endif; ?>
<?php if ($ReportOptions['showClosing']) : ?>
<th class="dr closing-dr">Closing (Dr)</th>
<th class="cr closing-cr">Closing (Cr)</th>
<?php endif; ?>
</tr>
</thead>
<?php } ?>
<tbody>
<?php foreach ($accountCategories as $accountCategory) : ?>
<?php if ($ReportOptions['showZeroBalances'] && $accountCategory->isZero == true) continue; ?>
<tr class="<?php echo ($accountCategory->isParent) ? 'bg-darker parent-row' : ''; ?>" data-toggle="collapse" data-target="#accategory_<?php echo $accountCategory->accategory_id; ?>">
<td class="group-name"><?php echo $accountCategory->accategory_name; ?></td>
<?php if ($ReportOptions['showOB']) : ?>
<td class="group-total" width="<?php echo $ReportOptions['AmountColWidth']; ?>"><?php echo myCurrency(($accountCategory->posting_side == 'DR') ? $accountCategory->opening_balance : 0); ?></td>
<td class="group-total" width="<?php echo $ReportOptions['AmountColWidth']; ?>"><?php echo myCurrency(($accountCategory->posting_side == 'CR') ? $accountCategory->opening_balance : 0); ?></td>
<?php endif; ?>
<?php if ($ReportOptions['showPeriod']) : ?>
<td class="group-total" width="<?php echo $ReportOptions['AmountColWidth']; ?>"><?php echo myCurrency(($accountCategory->posting_side == 'DR') ? $accountCategory->regular_balance : 0); ?></td>
<td class="group-total" width="<?php echo $ReportOptions['AmountColWidth']; ?>"><?php echo myCurrency(($accountCategory->posting_side == 'CR') ? $accountCategory->regular_balance : 0); ?></td>
<?php endif; ?>
<?php if ($ReportOptions['showClosing']) : ?>
<td class="group-total" width="<?php echo $ReportOptions['AmountColWidth']; ?>"><?php echo myCurrency(($accountCategory->posting_side == 'DR') ? $accountCategory->closing_balance : 0); ?></td>
<td class="group-total" width="<?php echo $ReportOptions['AmountColWidth']; ?>"><?php echo myCurrency(($accountCategory->posting_side == 'CR') ? $accountCategory->closing_balance : 0); ?></td>
<?php endif; ?>
</tr>
<?php if ($accountCategory->isParent == 0) : ?>
<?php $Accounts = $this->generateAccountsBySingleCategory($accountCategory->accategory_id); ?>
<tr>
<td colspan="7">
<div id="accategory_<?php echo $accountCategory->accategory_id; ?>" class="collapse">
<?php $this->renderAccountsTable($Accounts, $ReportOptions, false); ?>
</div>
</td>
</tr>
<?php endif; ?>
<?php if ($accountCategory->isParent == 1) : ?>
<?php $Accounts = $this->generateAccountsBySingleCategory($accountCategory->accategory_id); ?>
<?php if ($Accounts) : ?>
<tr>
<td colspan="7">
<div id="accategory_<?php echo $accountCategory->accategory_id; ?>" class="collapse">
<?php $this->renderAccountsTable($Accounts, $ReportOptions, false); ?>
</div>
</td>
</tr>
<?php endif; ?>
<tr class="">
<td colspan="7">
<div id="accategory_<?php echo $accountCategory->accategory_id; ?>" class="collapse">
<?php $childCategories = $this->getChildCategoriesWithBalances($accountCategory->accategory_id); ?>
<?php $this->renderAccountCategoriesTable($childCategories, $ReportOptions, false, $parentCategories); ?>
</div>
</td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>
<?php
}
function renderAccountCategoriesTableForBS($accountCategories, $ReportOptions, $displayHeadings = true, $parentCategories = [])
{
if(!isset($ReportOptions['AmountColWidth']))$ReportOptions['AmountColWidth']=180;
?>
<table class="table table-hover table-sm">
<tbody>
<?php foreach ($accountCategories as $accountCategory) : ?>
<?php if ($ReportOptions['showZeroBalances']==false && $accountCategory->isZero == true) continue; ?>
<tr class="<?php echo ($accountCategory->isParent) ? 'bg-darker parent-row' : ''; ?>" data-toggle="collapse" data-target="#accategory_<?php echo $accountCategory->accategory_id; ?>">
<td class="group-name"><?php echo $accountCategory->accategory_name; ?></td>
<?php if ($ReportOptions['showOB']) : ?>
<!-- <td class="group-total" width="<?php echo $ReportOptions['AmountColWidth']; ?>"><?php echo myCurrency(($accountCategory->posting_side == 'DR') ? $accountCategory->opening_balance : 0); ?></td> -->
<td class="group-total" width="<?php echo $ReportOptions['AmountColWidth']; ?>"><?php echo myCurrency(($accountCategory->posting_side == 'CR') ? $accountCategory->opening_balance : 0); ?> <?php echo myCurrency(($accountCategory->posting_side == 'DR') ? $accountCategory->opening_balance : 0); ?></td>
<?php endif; ?>
<?php if ($ReportOptions['showPeriod']) : ?>
<!-- <td class="group-total" width="<?php echo $ReportOptions['AmountColWidth']; ?>"><?php echo myCurrency(($accountCategory->posting_side == 'DR') ? $accountCategory->regular_balance : 0); ?></td> -->
<td class="group-total" width="<?php echo $ReportOptions['AmountColWidth']; ?>"><?php echo myCurrency(($accountCategory->posting_side == 'CR') ? $accountCategory->regular_balance : 0); ?> <?php echo myCurrency(($accountCategory->posting_side == 'DR') ? $accountCategory->regular_balance : 0); ?></td>
<?php endif; ?>
<?php if ($ReportOptions['showClosing']) : ?>
<!-- <td class="group-total" width="<?php echo $ReportOptions['AmountColWidth']; ?>"><?php echo myCurrency(($accountCategory->posting_side == 'DR') ? $accountCategory->closing_balance : 0); ?></td> -->
<td class="group-total" width="<?php echo $ReportOptions['AmountColWidth']; ?>"><?php echo myCurrency(($accountCategory->posting_side == 'CR') ? $accountCategory->closing_balance : 0); ?> <?php echo myCurrency(($accountCategory->posting_side == 'DR') ? $accountCategory->closing_balance : 0); ?></td>
<?php endif; ?>
</tr>
<?php if ($accountCategory->isParent == 0) : ?>
<?php $Accounts = $this->generateAccountsBySingleCategory($accountCategory->accategory_id); ?>
<tr>
<td colspan="7">
<div id="accategory_<?php echo $accountCategory->accategory_id; ?>" class="collapse">
<?php $this->renderAccountsTableForBS($Accounts, $ReportOptions, false); ?>
</div>
</td>
</tr>
<?php endif; ?>
<?php if ($accountCategory->isParent == 1) : ?>
<?php $Accounts = $this->generateAccountsBySingleCategory($accountCategory->accategory_id); ?>
<?php if ($Accounts) : ?>
<tr>
<td colspan="7">
<div id="accategory_<?php echo $accountCategory->accategory_id; ?>" class="collapse">
<?php $this->renderAccountsTableForBS($Accounts, $ReportOptions, false); ?>
</div>
</td>
</tr>
<?php endif; ?>
<tr class="">
<td colspan="7">
<div id="accategory_<?php echo $accountCategory->accategory_id; ?>" class="collapse">
<?php $childCategories = $this->getChildCategoriesWithBalances($accountCategory->accategory_id); ?>
<?php $this->renderAccountCategoriesTableForBS($childCategories, $ReportOptions, false, $parentCategories); ?>
</div>
</td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>
<?php
}
function renderAccountsTable($Accounts, $ReportOptions, $displayHeadings = false)
{
if(!isset($ReportOptions['AmountColWidth']))$ReportOptions['AmountColWidth']=180;
?>
<table class="table table-hover table-gray">
<?php if ($displayHeadings) { ?>
<thead>
<tr>
<th>Particulars</th>
<?php if ($ReportOptions['showOB']) : ?>
<th class="dr opening-dr">Opening (Dr)</th>
<th class="cr opening-cr">Opening (Cr)</th>
<?php endif; ?>
<?php if ($ReportOptions['showPeriod']) : ?>
<th class="dr this-year-dr">This Year (Dr)</th>
<th class="cr this-year-cr">This Year (Cr)</th>
<?php endif; ?>
<?php if ($ReportOptions['showClosing']) : ?>
<th class="dr closing-dr">Closing (Dr)</th>
<th class="cr closing-cr">Closing (Cr)</th>
<?php endif; ?>
</tr>
</thead>
<?php } ?>
<tbody>
<?php foreach ($Accounts as $Account) : ?>
<?php if ($ReportOptions['showZeroBalances'] == false && $Account->isZero == true) continue; ?>
<tr>
<td class="group-name"><?php echo $Account->account_name; ?></td>
<?php if ($ReportOptions['showOB']) : ?>
<td class="group-total"><?php echo myCurrency(($Account->posting_side == 'DR') ? $Account->opening_balance : 0); ?></td>
<td class="group-total"><?php echo myCurrency(($Account->posting_side == 'CR') ? $Account->opening_balance : 0); ?></td>
<?php endif; ?>
<?php if ($ReportOptions['showPeriod']) : ?>
<td class="group-total"><?php echo myCurrency(($Account->posting_side == 'DR') ? $Account->regular_balance : 0); ?></td>
<td class="group-total"><?php echo myCurrency(($Account->posting_side == 'CR') ? $Account->regular_balance : 0); ?></td>
<?php endif; ?>
<?php if ($ReportOptions['showClosing']) : ?>
<td class="group-total"><?php echo myCurrency(($Account->posting_side == 'DR') ? $Account->closing_balance : 0); ?></td>
<td class="group-total"><?php echo myCurrency(($Account->posting_side == 'CR') ? $Account->closing_balance : 0); ?></td>
<?php endif; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
}
function renderAccountsTableForBS($Accounts, $ReportOptions, $displayHeadings = false)
{
if(!isset($ReportOptions['AmountColWidth']))$ReportOptions['AmountColWidth']=180;
?>
<table class="table table-hover table-gray">
<?php foreach ($Accounts as $Account) : ?>
<?php if ($ReportOptions['showZeroBalances'] == false && $Account->isZero == 1) continue; ?>
<tr>
<td class="group-name"><?php echo $Account->account_name; ?></td>
<?php if ($ReportOptions['showOB']) : ?>
<!-- <td class="group-total"></td> -->
<td class="group-total" width="<?php echo $ReportOptions['AmountColWidth']; ?>"><?php echo myCurrency(($Account->posting_side == 'CR') ? $Account->opening_balance : 0); ?><?php echo myCurrency(($Account->posting_side == 'DR') ? $Account->opening_balance : 0); ?></td>
<?php endif; ?>
<?php if ($ReportOptions['showPeriod']) : ?>
<!-- <td class="group-total"></td> -->
<td class="group-total" width="<?php echo $ReportOptions['AmountColWidth']; ?>"><?php echo myCurrency(($Account->posting_side == 'CR') ? $Account->regular_balance : 0); ?><?php echo myCurrency(($Account->posting_side == 'DR') ? $Account->regular_balance : 0); ?></td>
<?php endif; ?>
<?php if ($ReportOptions['showClosing']) : ?>
<!-- <td class="group-total"></td> -->
<td class="group-total" width="<?php echo $ReportOptions['AmountColWidth']; ?>"><?php echo myCurrency(($Account->posting_side == 'CR') ? $Account->closing_balance : 0); ?><?php echo myCurrency(($Account->posting_side == 'DR') ? $Account->closing_balance : 0); ?></td>
<?php endif; ?>
</tr>
<?php endforeach; ?>
</table>
<?php
}
//////
}

View File

@ -0,0 +1,128 @@
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH . "/third_party/pdf/fpdm.php";
class fpdflib
{
public function __construct()
{
}
function createPDF()
{
echo "COW";
$pdf = new FPDM\FPDM(APPPATH."/../pdf/voucher1.pdf");
/*
$result = 'SELECT * FROM report WHERE time BETWEEN "' . $_POST["fromdate"] . '" AND "' . $_POST["todate"] . '"';
$link = $db->prepare($result);
$link->execute();
$resultset = $link->fetchAll();
$count = $link->rowCount();
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->SetTitle("Report");
$pdf->AddPage();
$row_height = 10;
$y_axis = 30;
$pdf->SetY($y_axis);
$pdf->SetX(25);
$pdf->Cell(30, 10, "", 0, 0, 1);
$y_axis = $y_axis + $row_height;
$pdf->SetDrawColor(128, 0, 0);
$pdf->SetTextColor(102, 68, 34);
$pdf->SetFont('Arial', 'B', 10);
$pdf->SetY($y_axis);
$pdf->SetX(11);
$pdf->Cell(34, 10, 'Order ID', 1, 0, 'C');
$pdf->Cell(35, 10, 'Name', 1, 0, 'C');
$pdf->Cell(30, 10, 'TID', 1, 0, 'C');
$pdf->Cell(20, 10, 'Quantity', 1, 0, 'C');
$pdf->Cell(20, 10, 'Date', 1, 0, 'C');
$pdf->Cell(20, 10, 'Time', 1, 0, 'C');
$pdf->Cell(30, 10, 'Bill Amount', 1, 0, 'C');
$y_axis = $y_axis + $row_height;
$total = 0;
foreach ($resultset as $row) {
$len = strlen($row['name']);
if ($len > 21) {
$name = substr($row['name'], 0, 19) . "..";
} else {
$name = $row['name'];
}
$oid = $row['order_id'];
$tid = $row['t_id'];
$qty = $row['quantity'];
$date = $row['date'];
$time = $row['time'];
$amt = $row['bill_amount'];
$total = $total + $amt;
$pdf->SetDrawColor(128, 0, 0);
$pdf->SetTextColor(0);
$pdf->SetFont('Arial', '', 9);
$pdf->SetY($y_axis);
$pdf->SetX(11);
$pdf->Cell(34, 10, $oid, 1, 0, 'L');
$pdf->Cell(35, 10, $name, 1, 0, 'L');
$pdf->Cell(30, 10, $tid, 1, 0, 'C');
$pdf->Cell(20, 10, $qty, 1, 0, 'C');
$pdf->Cell(20, 10, $date, 1, 0, 'C');
$pdf->Cell(20, 10, $time, 1, 0, 'C');
$pdf->Cell(30, 10, $amt, 1, 0, 'R');
$y_axis = $y_axis + $row_height;
$pdf->SetY(10);
$pdf->SetX(170);
}
$totalre = $total - $r_amt;
$pdf->SetDrawColor(128, 0, 0);
$pdf->SetTextColor(0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->SetY($y_axis);
$pdf->SetX(137);
$pdf->Cell(42, 10, 'Total', 0, 0, 'C');
$pdf->SetTextColor(255, 0, 0);
$pdf->Cell(25, 10, $totalre, 0, 0, 'C');
$y_axis = $y_axis + $row_height;
$pdf->SetAutoPageBreak(false, 20);
$pdf->Output();
*/
}
function Header()
{
$this->Image('picture.png', 10, 6, 30);
$this->SetFont('Arial', 'B', 15);
// Move to the right
$this->Cell(80);
// Title
$this->SetTextColor(255, 119, 0);
$this->Cell(30, 10, 'Report', 0, 0, 'C');
// Line break
$this->Line(10, 22, 210 - 10, 22);
$this->Ln(20);
}
// Page footer
function Footer()
{
$this->Line(10, 280, 210 - 10, 280);
$this->SetY(-15);
$this->SetFont('Arial', 'I', 8);
// Page number
$this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,626 @@
<?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,
);
}
}

View File

@ -0,0 +1,100 @@
<?php
class numbertoword
{
//Free number to Nepali word converter by http://pujann.com.np
//GPU License
private function check_valid_number($nums)
{// use $nums > PHP_INT_MAX if error occurred for large numbers
if($nums>9900000000000)
{
throw new Exception("गणकले गन्न सक्ने भन्दा बढी नम्बर हाल्नुभो");
}
}
private $number_list = array('एक', 'दुइ', 'तीन', 'चार', 'पाँच', 'छ', 'सात', 'आठ', 'नौ', 'दश', 'एघार', 'बाह्र', 'तेर', 'चौध', 'पन्ध्र', 'सोर्ह', 'सत्र', 'अठार', 'उन्नाइस', 'बीस','एक्काइस', 'बाईस', 'तेइस', 'चौबिस', 'पच्चीस', 'छब्बीस', 'सत्ताइस', 'अठ्ठाइस', 'उन्नतीस', 'तीस', 'एक्तीस', 'बत्तीस', 'तेत्तीस', 'चौतीस', 'पैतिस', 'छत्तिस', ' सड्तीस', 'अड्तीस', 'उन्नचालीस', 'चालीस','एकचालीस', 'ब्यालीस', 'त्रीचालीस', 'चौबालीस', 'पैतालीस', 'छ्यालीस', 'सड्चालीट्ठ', 'अड्चालीस', 'उन्नपचास', 'पचास', 'एकाउन्न', 'बाउन्न', 'त्रीपन्न', 'चौबन्न', 'पच्पन्न', 'छप्पन्न', 'सन्ताउन्न', 'अन्ठाउन्न', 'उनन्साठ्ठी', 'साठी','एकसाठी', 'ब्यासठ्ठी', 'त्रीसठ्ठी', 'चौसठ्ठी', 'पैसठ्ठी', 'छ्यासठ्ठी', 'सड्सठ्ठी', 'अठ्सठ्ठी', 'उन्नसत्तरी', 'सत्तरी', 'एकत्तर', 'बहत्तर', 'त्रीयत्तर', 'चौरत्तर', 'पचत्तर', 'छ्यातर', 'सत्तर', 'अठ्अार', 'उन्नअसी', 'असी','एकासी', 'बयासी', 'त्रीरासी', 'चौरासी', 'पचासी', 'छ्यासी', 'सतासी', 'अठासी', 'उनानब्बे', 'नब्बे', 'एकानब्बे', 'बयानब्बे', 'त्रीयानब्बे', 'चौरानब्बे', 'पञ्चानब्बे', 'छ्यानब्बे', 'सन्तानब्बे', 'अन्ठानब्बे', 'उनान्सय');
private $mainlist = array('100000000000'=>'खरव', '1000000000'=>'अरव', '10000000'=>'करोड', '100000'=>'लाख', '1000'=>'हजार', '100'=>'सय');
private $eng_number_list = array("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Twenty One", "Twenty Two", "Twenty Three", "Twenty Four", "Twenty Five", "Twenty Six", "Twenty Seven", "Twenty Eight", "Twenty Nine", "Thirty", "Thirty One", "Thirty Two", "Thirty Three", "Thirty Four", "Thirty Five", "Thirty Six", "Thirty Seven", "Thirty Eight", "Thirty Nine", "Fourty", "Fourty One", "Fourty Two", "Fourty Three", "Fourty Four", "Fourty Five", "Fourty Six", "Fourty Seven", "Fourty Eight", "Fourty Nine", "Fifty", "Fifty One", "Fifty Two", "Fifty Three", "Fifty Four", "Fifty Five", "Fifty Six", "Fifty Seven", "Fifty Eight", "Fifty Nine", "Sixty", "Sixty One", "Sixty Two", "Sixty Three", "Sixty Four", "Sixty Five", "Sixty Six", "Sixty Seven", "Sixty Eight", "Sixty Nine", "Seventy", "Seventy One", "Seventy Two", "Seventy Three", "Seventy Four", "Seventy Five", "Seventy Six", "Seventy Seven", "Seventy Eight", "Seventy Nine", "Eighty", "Eighty One", "Eighty Two", "Eighty Three", "Eighty Four", "Eighty Five", "Eighty Six", "Eighty Seven", "Eighty Eight", "Eighty Nine", "Ninety", "Ninety One", "Ninety Two", "Ninety Three", "Ninety Four", "Ninety Five", "Ninety Six", "Ninety Seven", "Ninety Eight","Ninety Nine");
private $eng_mainlist = array('100000000000'=>'Kharba', '1000000000'=>'Arba', '10000000'=>'Karod', '100000'=>'Lakh', '1000'=>'Thousands', '100'=>'Hundred');
private function ten($nums)
{
if ($nums>0) {
return $this->number_list[$nums-1];
}
}
private function eng_ten($nums)
{
if ($nums>0) {
return $this->eng_number_list[$nums-1];
}
}
private function nepali_name($nums, $divident)
{
$number = floor($nums/$divident);
if($number>0)
{
return $this->number_list[$number-1].' '.$this->mainlist[$divident];
}
}
public function nepali_word($nums)
{
$n=explode(".",$nums);
$nums = abs($nums);
$this->check_valid_number($nums);
$result = '';
$coll="";
foreach ($this->mainlist as $count => $name)
{
$result.= $this->nepali_name($nums, $count).' ';
$nums = bcmod($nums,$count);
if($nums<=99)
{
$result.= $this->ten($nums);
$coll=$result;
}
}
if(isset($n[1]))$coll.=" रुपैयाँ ".$this->ten($n[1])." पैसा";
return $coll." मात्र";
}
private function english_name($nums, $divident)
{
$number = floor($nums/$divident);
if($number>0)
{
return $this->eng_number_list[$number-1].' '.$this->eng_mainlist[$divident];
}
}
public function english_word($nums)
{
$n=explode(".",$nums);
$nums=$n[0];
$nums = abs($nums);
$this->check_valid_number($nums);
$result = '';
$coll='';
foreach ($this->eng_mainlist as $count => $name)
{
$result.= $this->english_name($nums, $count).' ';
$nums = bcmod($nums,$count);
if($nums<=99)
{
$result.= $this->eng_ten($nums);
$coll=$result;
}
}
if(isset($n[1]))$coll.=" And ".$this->eng_decimal_part($n[1])." Paisa";
return $coll." Only";
}
private function eng_decimal_part($decimal)
{
return $this->eng_ten($decimal);
/* //IF WANT TO RETURN 68 Paisa as SIX EIGHT Paisa
$digits=str_split($decimal);
$n="";
foreach($digits as $d)
{
$n.=$this->eng_number_list[$d-1]." ";
}
return $n;
*/
}
}

View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>403 Forbidden</title>
</head>
<body>
<p>Directory access is forbidden.</p>
</body>
</html>