Skip to content

Commit

Permalink
feat: add credit card widgets top categories and usage
Browse files Browse the repository at this point in the history
  • Loading branch information
jesusantguerrero committed Jun 24, 2024
1 parent 6f4f3b5 commit 53e5353
Show file tree
Hide file tree
Showing 18 changed files with 785 additions and 46 deletions.
51 changes: 51 additions & 0 deletions app/Console/Commands/GenerateBillingCycles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use App\Domains\Loans\Actions\UpdateLatePayments;
use Tests\Feature\CreditCard\Helpers\CreditCardBase;
use App\Domains\Transaction\Services\CreditCardReportService;

class GenerateBillingCycles extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'bg:generate-billing-cycles {teamId} {date?}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Execute the console command.
*
* @return int
*/
public function handle(CreditCardReportService $service)
{
$teamId = $this->argument('teamId');
$date = $this->argument('date');

$monthsWithTransactions = $this->getFirstTransaction($teamId);
return $service->generateBillingCycles($teamId, $date ?? $monthsWithTransactions->date);
}

private function getFirstTransaction(int $teamId) {
return DB::table('transaction_lines')
->where([
"team_id" => $teamId
])
->selectRaw("date_format(transaction_lines.date, '%Y-%m') AS date")
->groupBy(DB::raw("date_format(transaction_lines.date, '%Y-%m')"))
->orderBy('date')
->first();
}
}
2 changes: 1 addition & 1 deletion app/Domains/AppCore/Policies/FinanceAccountPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public function create(User $user)
public function delete(User $user, Account $account)
{

return $user->id == $account->user_id;
return $user->current_team_id == $account->team_id;
}
}
1 change: 0 additions & 1 deletion app/Domains/Budget/Services/BudgetRolloverService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Insane\Journal\Models\Core\Account;
use Insane\Journal\Models\Core\Category;
use App\Domains\Budget\Models\BudgetMonth;
use App\Domains\Budget\Data\BudgetAssignData;
use App\Domains\Budget\Data\BudgetReservedNames;
use Insane\Journal\Models\Core\AccountDetailType;

Expand Down
6 changes: 3 additions & 3 deletions app/Domains/Journal/Actions/AccountDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use Illuminate\Foundation\Auth\User;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\ValidationException;
use Insane\Journal\Contracts\AccountDeletes;
use Insane\Journal\Models\Core\Account;
use Insane\Journal\Contracts\AccountDeletes;
use Illuminate\Validation\ValidationException;

class AccountDelete implements AccountDeletes
{
Expand All @@ -18,7 +18,7 @@ public function delete(User $user, Account $account)

public function validate(User $user, mixed $account)
{
Gate::forUser($user)->authorize('delete-account', $account);
Gate::forUser($user)->authorize('delete', $account);
if (count($account->transactions)) {
throw ValidationException::withMessages([
'account' => __('You may not delete account with transactions.'),
Expand Down
3 changes: 2 additions & 1 deletion app/Domains/Journal/Policies/AccountPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace App\Domains\Journal\Policies;

use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use Insane\Journal\Models\Core\Account;
use Illuminate\Auth\Access\HandlesAuthorization;

class AccountPolicy
{
Expand Down Expand Up @@ -32,6 +32,7 @@ public function update(User $user, Account $account)

public function delete(User $user, Account $account)
{
dd($account);
return $user->team_id == $account->team_id;
}
}
103 changes: 103 additions & 0 deletions app/Domains/Transaction/Models/BillingCycle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace App\Domains\Transaction\Models;

use Illuminate\Database\Eloquent\Model;
use Insane\Journal\Models\Core\Transaction;
use Insane\Journal\Traits\HasPaymentDocuments;

class BillingCycle extends Model
{
// use HasPaymentDocuments;

const STATUS_PENDING = 'PENDING';
const STATUS_PARTIALLY_PAID = 'PARTIALLY_PAID';
const STATUS_LATE = 'LATE';
const STATUS_PAID = 'PAID';
const STATUS_CANCELLED = 'CANCELLED';

protected $fillable = [
'team_id',
'user_id',
'account_id',
'end_at',
'due_at',
'start_at',
'due',
'subtotal',
'discounts',
'total'
];

protected $creditCategory = 'credit_cards';
protected $creditAccount = 'Credit Card Billings';

public function transactions() {
return $this->hasMany(TransactionLine::class, 'account_id')
->whereBetween("date", [$this->from, $this->until]);
}

// Transactionable config
public function getTransactionItems() {
return [];
}

public static function getCategoryName($payable): string {
return "credit_cards";
}

public function getTransactionDescription() {
return "Balance de credito";
}

public function getTransactionDirection(): string {
return Transaction::DIRECTION_CREDIT;
}

public function getAccountId() {
return $this->account_id;
}

public function getCounterAccountId(): int {
return $this->client_account_id;
}

// payable config
public function getStatusField(): string {
return 'payment_status';
}

public static function calculateTotal($payable) {
$payable->total = $payable->transactions()->sum('amount');
$payable->due = $payable->total - $payable->paid;
}

public static function checkStatus($payable) {
$debt = $payable->total - $payable->paid;
if ($debt <= 0) {
$status = self::STATUS_PAID;
} elseif ($debt > 0 && $debt < $payable->amount) {
$status = self::STATUS_PARTIALLY_PAID;
} elseif ($debt && $payable->hasLateInstallments()) {
$status = self::STATUS_LATE;
} elseif ($debt && !$payable->cancelled_at) {
$status = self::STATUS_PENDING;
} elseif ($payable->cancelled_at) {
$status = self::STATUS_CANCELLED;
} else {
$status = $payable->status;
}
$payable->status = $status;
}

public function updateStatus() {
self::checkPayments($this);
$this->save();
self::calculateTotal($this);
self::checkStatus($this);
}

public function getConceptLine(): string {
return "";
}
}
Loading

0 comments on commit 53e5353

Please sign in to comment.