From bd6819cac5ec1653a939ee4d9407fc6392aa0940 Mon Sep 17 00:00:00 2001 From: Jesus Guerrero Date: Thu, 2 Feb 2023 15:05:33 -0400 Subject: [PATCH] feat: allow open account balance --- src/Models/Core/Account.php | 26 ++++++++++++++++++++++++++ src/Models/Core/Payee.php | 3 ++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Models/Core/Account.php b/src/Models/Core/Account.php index d164386..71e3589 100644 --- a/src/Models/Core/Account.php +++ b/src/Models/Core/Account.php @@ -5,6 +5,7 @@ use App\Models\Team; use App\Models\User; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Carbon; use Illuminate\Support\Str; use Illuminate\Support\Facades\DB; @@ -213,4 +214,29 @@ public static function setNumber($account) $account->number = $number + 1; } } + + public function openBalance($amount, $type = 1, $counterAccountId = null) { + $formData = [ + 'account_id' => $this->id, + 'team_id' => $this->team_id, + 'user_id' => $this->user_id, + 'date' => Carbon::now()->format('Y-m-d H:i:s'), + 'payee_id' => 'new', + 'payee_label' => $this->user->name, + 'currency_code' => $this->currency_code ?? "DOP", + 'counter_account_id' => $counterAccountId ?? Account::guessAccount($this, ['opening_balance_capital', 'business_owner_contribution']), + 'description' => 'Starting Balance', + 'direction' => $type == 1 ? Transaction::DIRECTION_DEBIT : Transaction::DIRECTION_CREDIT, + 'total' => $amount, + 'items' => [], + 'status' => Transaction::STATUS_VERIFIED, + 'metaData' => json_encode([ + "resource_id" => "SYSTEM:$this->id", + "resource_origin" => 'SYSTEM', + "resource_type" => 'transaction', + ]) + ]; + + Transaction::createTransaction($formData); + } } diff --git a/src/Models/Core/Payee.php b/src/Models/Core/Payee.php index ac35f85..fd90cab 100644 --- a/src/Models/Core/Payee.php +++ b/src/Models/Core/Payee.php @@ -55,12 +55,13 @@ public static function createContactAccount($payee) $clientTrustType = AccountDetailType::where([ 'name' => 'client_trust_account', ])->first(); + $account = Account::create([ "team_id" => $payee->team_id, "user_id" => $payee->user_id, "display_id" => Str::slug($accountName), "name" => $accountName, - "account_detail_type_id" => $clientTrustType->id, + "account_detail_type_id" => $clientTrustType?->id ?? 1, "currency_code" => "DOP" ]); return $account->id;