Skip to content

Commit

Permalink
fix: payments and transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
jesusantguerrero committed Jun 28, 2024
1 parent 3d5cac1 commit 8360f93
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/Http/Controllers/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controllers/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
15 changes: 9 additions & 6 deletions src/Models/Core/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -24,6 +24,7 @@ class Payment extends Model
'concept',
'notes',
'account_id',
// 'transaction_id',
'account_name',
'number',
'amount',
Expand All @@ -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) {
Expand Down Expand Up @@ -83,18 +86,18 @@ public function transaction() {
public static function setNumber($payment)
{
$isInvalidNumber = true;

if ($payment->number) {
$isInvalidNumber = Payment::where([
"team_id" => $payment->team_id,
"number" => $payment->number,
])->whereNot([
"id" => $payment->id
])->get();

$isInvalidNumber = count($isInvalidNumber);
}

if ($isInvalidNumber) {
$result = Payment::where([
"team_id" => $payment->team_id,
Expand Down
35 changes: 19 additions & 16 deletions src/Models/Core/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/Traits/Transactionable.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
namespace Insane\Journal\Traits;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;
use Insane\Journal\Models\Core\Account;
use Insane\Journal\Models\Core\Category;
use Insane\Journal\Models\Core\Transaction;
Expand Down Expand Up @@ -57,7 +57,7 @@ public static function createContactAccount($payable)
"name" => "{$payable->client?->names} Account",
"currency_code" => "DOP"
]);

return $account->id;
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 8360f93

Please sign in to comment.