akaunting/app/Console/Commands/RecurringCheck.php

270 lines
8.3 KiB
PHP
Raw Normal View History

2018-04-27 19:02:42 +03:00
<?php
namespace App\Console\Commands;
2020-09-27 00:33:06 +03:00
use App\Events\Banking\TransactionCreated;
use App\Events\Banking\TransactionRecurring;
2020-12-24 01:28:38 +03:00
use App\Events\Document\DocumentCreated;
use App\Events\Document\DocumentRecurring;
2020-09-27 00:33:06 +03:00
use App\Models\Banking\Transaction;
2021-04-16 00:59:43 +03:00
use App\Models\Common\Company;
2020-10-20 18:27:26 +03:00
use App\Models\Common\Recurring;
2020-12-24 01:28:38 +03:00
use App\Models\Document\Document;
2020-09-30 17:01:35 +03:00
use App\Utilities\Date;
2018-04-27 19:02:42 +03:00
use Illuminate\Console\Command;
2022-06-01 10:15:55 +03:00
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
2022-06-08 01:48:13 +03:00
use Recurr\RecurrenceCollection;
2018-04-27 19:02:42 +03:00
class RecurringCheck extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'recurring:check';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Check for recurring';
2019-11-16 10:21:14 +03:00
2018-04-27 19:02:42 +03:00
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
2022-06-07 17:52:54 +03:00
$this->info('Checking for recurring...');
2022-06-01 10:15:55 +03:00
// Bind to container
app()->instance(static::class, $this);
// Disable model cache
config(['laravel-model-caching.enabled' => false]);
2020-10-20 18:27:26 +03:00
// Get all recurring
2022-06-01 10:15:55 +03:00
$recurring = Recurring::with('company')
/*->whereHas('recurable', function (Builder $query) {
$query->allCompanies();
})*/
->active()
->allCompanies()
->cursor();
//$this->info('Total recurring: ' . $recurring->count());
2020-10-20 18:27:26 +03:00
foreach ($recurring as $recur) {
if (empty($recur->company)) {
2022-06-01 10:15:55 +03:00
$this->info('Missing company.');
$recur->delete();
2020-10-20 18:27:26 +03:00
continue;
}
2022-06-08 01:48:13 +03:00
$this->info('Recurring ID: ' . $recur->id);
2020-10-20 18:27:26 +03:00
2022-06-01 10:15:55 +03:00
$company_name = !empty($recur->company->name) ? $recur->company->name : 'Missing Company Name : ' . $recur->company->id;
2020-10-20 18:27:26 +03:00
$template = $recur->recurable()->where('company_id', $recur->company_id)->first();
2020-10-20 18:27:26 +03:00
// Check if company is disabled
2022-06-01 10:15:55 +03:00
if (! $recur->company->enabled) {
2020-10-20 18:27:26 +03:00
$this->info($company_name . ' company is disabled. Skipping...');
if (Date::parse($recur->company->updated_at)->format('Y-m-d') > Date::now()->subMonth(3)->format('Y-m-d')) {
$recur->delete();
if ($template) {
$template->delete();
}
2020-10-20 18:27:26 +03:00
}
2018-04-27 19:02:42 +03:00
continue;
}
2020-10-20 18:27:26 +03:00
// Check if company has any active user
$has_active_users = false;
foreach ($recur->company->users as $company_user) {
if (Date::parse($company_user->last_logged_in_at)->format('Y-m-d') > Date::now()->subMonth(3)->format('Y-m-d')) {
$has_active_users = true;
break;
}
}
2022-06-01 10:15:55 +03:00
if (! $has_active_users) {
2020-10-20 18:27:26 +03:00
$this->info('No active users for ' . $company_name . ' company. Skipping...');
$recur->delete();
if ($template) {
$template->delete();
}
2020-10-20 18:27:26 +03:00
continue;
}
2019-11-16 10:21:14 +03:00
2021-04-16 00:59:43 +03:00
company($recur->company_id)->makeCurrent();
2018-04-27 19:02:42 +03:00
if (! $template) {
2022-06-01 10:15:55 +03:00
$this->info('Missing model.');
$recur->delete();
2018-04-27 19:02:42 +03:00
2020-10-20 18:27:26 +03:00
continue;
}
2020-09-27 00:33:06 +03:00
2022-06-07 17:52:54 +03:00
$this->info('Template ID: ' . $template->id);
2022-06-01 10:15:55 +03:00
2022-06-08 01:48:13 +03:00
// Get the remaining schedules, including the previously failed ones
$schedules = $this->getRemainingSchedules($template, $recur);
2022-06-01 10:15:55 +03:00
2022-06-07 17:52:54 +03:00
// Check if all schedules created
2022-06-08 01:48:13 +03:00
if ($schedules->count() == 0) {
2022-06-07 17:52:54 +03:00
$this->info('All schedules created.');
2020-09-30 17:01:35 +03:00
2022-06-01 10:15:55 +03:00
$recur->update(['status' => Recurring::COMPLETE_STATUS]);
2020-10-20 18:27:26 +03:00
continue;
}
2020-09-30 17:01:35 +03:00
2022-06-08 01:48:13 +03:00
// Don't create schedules for the future
$schedules = $schedules->endsBefore($recur->getRecurringRuleTomorrowDate());
if ($schedules->count() == 0) {
$this->info('No schedules for today.');
continue;
}
2022-06-01 10:15:55 +03:00
2020-10-20 18:27:26 +03:00
foreach ($schedules as $schedule) {
$schedule_date = Date::parse($schedule->getStart()->format('Y-m-d'));
2020-09-27 00:33:06 +03:00
2022-06-08 01:48:13 +03:00
$this->info('Schedule date: ' . $schedule_date->format('Y-m-d'));
2022-06-07 17:52:54 +03:00
$this->recur($template, $schedule_date);
2018-04-27 19:02:42 +03:00
}
}
2021-04-16 00:59:43 +03:00
Company::forgetCurrent();
2022-06-01 10:15:55 +03:00
// Remove from container
app()->forgetInstance(static::class);
2022-06-07 17:52:54 +03:00
$this->info('Recurring check done!');
2018-04-27 19:02:42 +03:00
}
2022-06-08 01:48:13 +03:00
protected function recur(Document|Transaction $template, Date $schedule_date): void
2018-04-27 19:02:42 +03:00
{
2022-06-07 17:52:54 +03:00
DB::transaction(function () use ($template, $schedule_date) {
if (! $model = $this->getModel($template, $schedule_date)) {
2020-09-30 17:01:35 +03:00
return;
}
2018-05-01 19:00:33 +03:00
2022-06-07 17:52:54 +03:00
$this->info('Model created: ' . $model->id);
2018-04-27 22:16:35 +03:00
2022-06-07 17:52:54 +03:00
switch ($template::class) {
case Document::class:
event(new DocumentCreated($model, request()));
event(new DocumentRecurring($model));
2019-11-16 10:21:14 +03:00
2020-09-30 17:01:35 +03:00
break;
2022-06-07 17:52:54 +03:00
case Transaction::class:
event(new TransactionCreated($model));
2019-11-16 10:21:14 +03:00
2022-06-07 17:52:54 +03:00
event(new TransactionRecurring($model));
2019-11-16 10:21:14 +03:00
2020-09-30 17:01:35 +03:00
break;
}
});
2018-04-27 19:02:42 +03:00
}
2022-06-08 01:48:13 +03:00
protected function getModel(Document|Transaction $template, Date $schedule_date): Document|Transaction
2018-04-27 19:02:42 +03:00
{
2022-06-07 17:52:54 +03:00
$function = ($template instanceof Transaction) ? 'getTransactionModel' : 'getDocumentModel';
2018-05-01 19:00:33 +03:00
2020-07-10 13:37:48 +03:00
try {
2022-06-07 17:52:54 +03:00
return $this->$function($template, $schedule_date);
2021-06-08 23:33:17 +03:00
} catch (\Throwable $e) {
2020-07-10 13:37:48 +03:00
$this->error($e->getMessage());
2021-06-08 23:33:17 +03:00
report($e);
2020-07-10 13:37:48 +03:00
return false;
}
2020-09-27 00:33:06 +03:00
}
2018-04-27 19:02:42 +03:00
2022-06-08 01:48:13 +03:00
protected function getDocumentModel(Document $template, Date $schedule_date): Document
2020-09-27 00:33:06 +03:00
{
2022-06-07 17:52:54 +03:00
$template->cloneable_relations = ['items', 'totals'];
2020-09-27 00:33:06 +03:00
2022-06-07 17:52:54 +03:00
$model = $template->duplicate();
2020-09-27 00:33:06 +03:00
2019-11-16 10:21:14 +03:00
// Days between issued and due date
2022-06-07 17:52:54 +03:00
$diff_days = Date::parse($template->due_at)->diffInDays(Date::parse($template->issued_at));
2018-04-27 22:16:35 +03:00
2022-06-07 17:52:54 +03:00
$model->type = $this->getRealType($template->type);
$model->parent_id = $template->id;
$model->issued_at = $schedule_date->format('Y-m-d');
$model->due_at = $schedule_date->copy()->addDays($diff_days)->format('Y-m-d');
$model->created_from = 'core::recurring';
$model->save();
2020-02-21 11:12:52 +03:00
2022-06-07 17:52:54 +03:00
return $model;
2018-04-27 19:02:42 +03:00
}
2020-09-27 00:33:06 +03:00
2022-06-08 01:48:13 +03:00
protected function getTransactionModel(Transaction $template, Date $schedule_date): Transaction
2020-09-27 00:33:06 +03:00
{
2022-06-07 17:52:54 +03:00
$template->cloneable_relations = [];
2020-09-27 00:33:06 +03:00
2022-06-07 17:52:54 +03:00
$model = $template->duplicate();
2020-09-27 00:33:06 +03:00
2022-06-07 17:52:54 +03:00
$model->type = $this->getRealType($template->type);
$model->parent_id = $template->id;
$model->paid_at = $schedule_date->format('Y-m-d');
$model->created_from = 'core::recurring';
$model->save();
2020-09-27 00:33:06 +03:00
2022-06-07 17:52:54 +03:00
return $model;
2020-09-27 00:33:06 +03:00
}
2022-06-08 01:48:13 +03:00
protected function getRemainingSchedules(Document|Transaction $template, Recurring $recur): RecurrenceCollection
2020-09-27 00:33:06 +03:00
{
2022-06-07 17:52:54 +03:00
$date_field = $this->getDateField($template);
$created_schedules = DB::table($template->getTable())
->where('type', $this->getRealType($template->type))
->where('parent_id', $template->id)
->get($date_field)
->transform(function ($item, $key) use ($date_field) {
return Date::parse($item->$date_field)->format('Y-m-d');
})
->toArray();
// Skip already created schedules
2022-06-08 01:48:13 +03:00
$schedules = $recur->getRecurringSchedule()->filter(function ($recurrence) use ($created_schedules) {
2022-06-07 17:52:54 +03:00
return ! in_array($recurrence->getStart()->format('Y-m-d'), $created_schedules);
});
2020-09-27 00:33:06 +03:00
2022-06-07 17:52:54 +03:00
return $schedules;
2020-09-27 00:33:06 +03:00
}
2022-06-08 01:48:13 +03:00
protected function getDateField(Document|Transaction $template): string
2020-09-27 00:33:06 +03:00
{
2022-06-08 01:48:13 +03:00
return ($template instanceof Transaction) ? 'paid_at' : 'issued_at';
2020-09-27 00:33:06 +03:00
}
2022-06-01 10:15:55 +03:00
public function getRealType(string $recurring_type): string
2020-09-27 00:33:06 +03:00
{
2022-06-01 10:15:55 +03:00
return Str::replace('-recurring', '', $recurring_type);
2020-09-27 00:33:06 +03:00
}
2018-04-27 19:02:42 +03:00
}