From 8360f9353a512112d434779ada74e6ef962d024a Mon Sep 17 00:00:00 2001 From: Jesus Guerrero Date: Thu, 27 Jun 2024 23:02:37 -0400 Subject: [PATCH] fix: payments and transactions --- src/Http/Controllers/AccountController.php | 4 +-- src/Http/Controllers/CategoryController.php | 4 +-- src/Models/Core/Payment.php | 15 +++++---- src/Models/Core/Transaction.php | 35 +++++++++++---------- src/Traits/Transactionable.php | 6 ++-- 5 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/Http/Controllers/AccountController.php b/src/Http/Controllers/AccountController.php index 1b299a6..8f1dd67 100644 --- a/src/Http/Controllers/AccountController.php +++ b/src/Http/Controllers/AccountController.php @@ -8,8 +8,8 @@ use Insane\Journal\Models\Core\Account; use Insane\Journal\Models\Core\Category; use Insane\Journal\Contracts\AccountCreates; +use Insane\Journal\Contracts\AccountDeletes; use Insane\Journal\Contracts\AccountUpdates; -use Insane\Journal\Contracts\DeleteAccounts; use Insane\Journal\Models\Core\AccountDetailType; final class AccountController @@ -69,7 +69,7 @@ public function update(Account $account) { } public function destroy(Request $request, Account $account) { - $accountDelete = app(DeleteAccounts::class); + $accountDelete = app(AccountDeletes::class); $accountDelete->delete(request()->user(), $account); return Redirect()->back(); } diff --git a/src/Http/Controllers/CategoryController.php b/src/Http/Controllers/CategoryController.php index ae1a650..2b3e896 100644 --- a/src/Http/Controllers/CategoryController.php +++ b/src/Http/Controllers/CategoryController.php @@ -2,12 +2,12 @@ namespace Insane\Journal\Http\Controllers; -use App\Domains\Journal\Actions\CategoryList; +use Insane\Journal\Contracts\CategoryListClientBalances; final class CategoryController { public function getWithClientBalance($uniqueField, $clientId) { - $categoryClientBalances = app(CategoryList::class); + $categoryClientBalances = app(CategoryListClientBalances::class); return $categoryClientBalances->list(request()->user(), $uniqueField, $clientId); } } diff --git a/src/Models/Core/Payment.php b/src/Models/Core/Payment.php index dda71bd..9b35d09 100644 --- a/src/Models/Core/Payment.php +++ b/src/Models/Core/Payment.php @@ -2,9 +2,9 @@ namespace Insane\Journal\Models\Core; -use Illuminate\Database\Eloquent\Factories\HasFactory; -use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\DB; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Factories\HasFactory; class Payment extends Model { @@ -24,6 +24,7 @@ class Payment extends Model 'concept', 'notes', 'account_id', + // 'transaction_id', 'account_name', 'number', 'amount', @@ -37,7 +38,9 @@ class Payment extends Model protected static function booted() { static::created(function ($payment) { - $payment->createTransaction(); + if ($payment->transaction_id) { + $payment->createTransaction(); + }; }); static::creating(function ($payment) { @@ -83,7 +86,7 @@ public function transaction() { public static function setNumber($payment) { $isInvalidNumber = true; - + if ($payment->number) { $isInvalidNumber = Payment::where([ "team_id" => $payment->team_id, @@ -91,10 +94,10 @@ public static function setNumber($payment) ])->whereNot([ "id" => $payment->id ])->get(); - + $isInvalidNumber = count($isInvalidNumber); } - + if ($isInvalidNumber) { $result = Payment::where([ "team_id" => $payment->team_id, diff --git a/src/Models/Core/Transaction.php b/src/Models/Core/Transaction.php index b8e7e55..45b1a43 100644 --- a/src/Models/Core/Transaction.php +++ b/src/Models/Core/Transaction.php @@ -266,20 +266,18 @@ public function createLines($items = [], $data = []) { $anchorLine->labels()->save($label); } - - - $this->lines()->create([ - "amount" => $this->total, - "date" => $this->date, - "concept" => $this->description, - "payee_id" => $this->payee_id, - "index" => 1, - "type"=> $this->direction == Transaction::DIRECTION_DEBIT ? -1 : 1, - "account_id" => $this->counter_account_id ?? $this->payee?->account_id, - "category_id" => 0, - "team_id" => $this->team_id, - "user_id" => $this->user_id - ]); + $this->lines()->create([ + "amount" => $this->total, + "date" => $this->date, + "concept" => $this->description, + "payee_id" => $this->payee_id, + "index" => 1, + "type"=> $this->direction == Transaction::DIRECTION_DEBIT ? -1 : 1, + "account_id" => $this->counter_account_id ?? $this->payee?->account_id, + "category_id" => 0, + "team_id" => $this->team_id, + "user_id" => $this->user_id + ]); } else if ($this->has_splits) { foreach ($items as $item) { @@ -300,9 +298,14 @@ public function createLines($items = [], $data = []) { "is_split" => true, ]); - $anchorLine->labels()->create([ - "label_id" => $data["label_id"] + $label = self::createLabel($data, [ + "user_id" => $anchorLine->user_id, + "team_id" => $anchorLine->team_id, ]); + + if ($label) { + $anchorLine->labels()->save($label); + } $this->lines()->create([ "type"=> $this->direction == Transaction::DIRECTION_DEBIT ? -1 : 1, diff --git a/src/Traits/Transactionable.php b/src/Traits/Transactionable.php index 350e489..57fd114 100644 --- a/src/Traits/Transactionable.php +++ b/src/Traits/Transactionable.php @@ -1,8 +1,8 @@ "{$payable->client?->names} Account", "currency_code" => "DOP" ]); - + return $account->id; } @@ -91,7 +91,7 @@ public function createTransaction(mixed $formData = []) $formData["account_id"] = isset($formData['account_id']) ? $formData['account_id'] : $this->getAccountId(); $formData["counter_account_id"] = $this->client_account_id; $formData["status"] = "verified"; - + if ($this->transaction) { $transaction = $this->transaction()->update($formData); } else {