ekmungai / eloquent-ifrs

Eloquent Double Entry Accounting with a focus on IFRS Compliant Reporting
MIT License
334 stars 68 forks source link

Generating Chart of Accounts with balances #104

Closed kchizi closed 2 years ago

kchizi commented 2 years ago

Hi, been trying to implement the package for some time and having difficulty generating COA to display to the user, how do I generate or query for the chart of accounts with balances?

hicka commented 2 years ago

Hi, You can get all the accounts of a specific entity that you want like this,


// get the entity
$entity = Auth::user()->entity;

// get the accounts
$accounts = Account::where('entity_id','=',$entity->id)->get();

// you can get the closing balance like this
foreach($accounts as $account) {

   $balance = $account->closingBalance()[entity->currency_id];
}
p7Rck commented 2 years ago

Hi, can you do a query in accounts that will get all the asset side accounts only or the liability side?

ekmungai commented 2 years ago

@p7Rck a simple extension of the where clause should do the trick. Read the docs on the Account class to see what to filter by.

Cheers, Edward

kchizi commented 2 years ago

Thank you @hicka @p7Rck @ekmungai Managed to build it with this code

    $entity = \IFRS\Models\Entity::where('id',$business->entity_id)->first();
            Log::info('ENTITY '.$entity->id);
            //create reporting period if missing
            if(ReportingPeriod::where('entity_id', $entity->id)->first() == null){
                \IFRS\Models\ReportingPeriod::create(['period_count=>1','calendar_year'=>2021, $entity]);
            }
            //check if  user has entity if not assign the business entity
            $user = User::find($userid);
            if($user->entity_id == null){
                $user->entity_id == $entity->id;
                $user->save();
            }
    $categories = \IFRS\Models\Category::where('entity_id',$entity->id)->get();
            foreach($categories as $category){
            // get the accounts
            $category_type = $category->category_type;
            $category_name = $category->name;
            $categoryid = $category->id;
            $coadata[] = array(
                "id" => 'category_'.$categoryid,
                "text" => $category_name,
                "icon" => "fa fa-folder icon-lg kt-font-",
                "parent" => "node_".Str::lower($category_type),
                "type" => "root"
               );
            $accounts = Account::where('category_id',$categoryid)->get();

    // you can get the closing balance like this
    foreach($accounts as $account) {

        $coadata[] = array(
            "id" => 'account_'.$account->id,
            "text" => $account->code." - ".$account->name,
            "icon" => "fa fa-file icon-lg kt-font-",
            "parent" => 'category_'.$categoryid,
            "type" => "root"
           );
    //$balance = $account->closingBalance()[$entity->currency_id];
    }

    }