akaunting/app/Console/Commands/RecurringCheck.php

305 lines
8.5 KiB
PHP
Raw Normal View History

2018-04-27 16:02:42 +00:00
<?php
namespace App\Console\Commands;
2020-09-26 21:33:06 +00:00
use App\Events\Banking\TransactionCreated;
use App\Events\Banking\TransactionRecurring;
2020-12-23 22:28:38 +00:00
use App\Events\Document\DocumentCreated;
use App\Events\Document\DocumentRecurring;
2020-09-26 21:33:06 +00:00
use App\Models\Banking\Transaction;
2021-04-15 21:59:43 +00:00
use App\Models\Common\Company;
2020-10-20 15:27:26 +00:00
use App\Models\Common\Recurring;
2020-12-23 22:28:38 +00:00
use App\Models\Document\Document;
2020-09-30 14:01:35 +00:00
use App\Utilities\Date;
2018-04-27 16:02:42 +00:00
use Illuminate\Console\Command;
2022-06-01 07:15:55 +00:00
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
2018-04-27 16:02:42 +00: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 07:21:14 +00:00
2018-04-27 16:02:42 +00:00
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
2022-06-01 07:15:55 +00:00
// Bind to container
app()->instance(static::class, $this);
// Disable model cache
config(['laravel-model-caching.enabled' => false]);
2020-10-20 15:27:26 +00:00
// Get all recurring
2022-06-01 07:15:55 +00:00
$recurring = Recurring::with('company')
/*->whereHas('recurable', function (Builder $query) {
$query->allCompanies();
})*/
->active()
->allCompanies()
->cursor();
//$this->info('Total recurring: ' . $recurring->count());
2020-10-20 15:27:26 +00:00
2022-06-01 07:15:55 +00:00
$today = Date::today();
2020-10-20 15:27:26 +00:00
foreach ($recurring as $recur) {
if (empty($recur->company)) {
2022-06-01 07:15:55 +00:00
$this->info('Missing company.');
$recur->delete();
2020-10-20 15:27:26 +00:00
continue;
}
2022-06-01 07:15:55 +00:00
$this->info('Creating records for ' . $recur->id . ' recurring...');
2020-10-20 15:27:26 +00:00
2022-06-01 07:15:55 +00:00
$company_name = !empty($recur->company->name) ? $recur->company->name : 'Missing Company Name : ' . $recur->company->id;
2020-10-20 15:27:26 +00:00
// Check if company is disabled
2022-06-01 07:15:55 +00:00
if (! $recur->company->enabled) {
2020-10-20 15:27:26 +00: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();
}
2018-04-27 16:02:42 +00:00
continue;
}
2020-10-20 15:27:26 +00: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 07:15:55 +00:00
if (! $has_active_users) {
2020-10-20 15:27:26 +00:00
$this->info('No active users for ' . $company_name . ' company. Skipping...');
$recur->delete();
continue;
}
2019-11-16 07:21:14 +00:00
2021-04-15 21:59:43 +00:00
company($recur->company_id)->makeCurrent();
2018-04-27 16:02:42 +00:00
2022-06-01 07:15:55 +00:00
if (! $model = $recur->recurable) {
$this->info('Missing model.');
$recur->delete();
2018-04-27 16:02:42 +00:00
2020-10-20 15:27:26 +00:00
continue;
}
2020-09-26 21:33:06 +00:00
2020-10-20 15:27:26 +00:00
$children_count = $this->getChildrenCount($model);
2022-06-01 07:15:55 +00:00
$schedules = $recur->getRecurringSchedule();
2020-10-20 15:27:26 +00:00
$schedule_count = $schedules->count();
2020-09-30 14:01:35 +00:00
2020-10-20 15:27:26 +00:00
// All recurring created, including today
if ($children_count > ($schedule_count - 1)) {
2022-06-01 07:15:55 +00:00
$this->info('All recurring created.');
$recur->update(['status' => Recurring::COMPLETE_STATUS]);
2020-10-20 15:27:26 +00:00
continue;
}
2020-09-30 14:01:35 +00:00
2020-10-20 15:27:26 +00:00
// Recur only today
if ($children_count == ($schedule_count - 1)) {
2022-06-01 07:15:55 +00:00
$this->info('Recur only today.');
2020-10-20 15:27:26 +00:00
$this->recur($model, $recur->recurable_type, $today);
2020-09-30 14:01:35 +00:00
2022-06-01 07:15:55 +00:00
$recur->update(['status' => Recurring::COMPLETE_STATUS]);
2020-10-20 15:27:26 +00:00
continue;
}
2020-09-30 14:01:35 +00:00
2022-06-01 07:15:55 +00:00
// Don't create records for the future
$schedules = $schedules->endsBefore($recur->getRecurringRuleTomorrowDate());
// Recur all schedules, including the previously failed ones
2020-10-20 15:27:26 +00:00
foreach ($schedules as $schedule) {
2022-06-01 07:15:55 +00:00
$this->info('Recur all schedules.');
2020-10-20 15:27:26 +00:00
$schedule_date = Date::parse($schedule->getStart()->format('Y-m-d'));
2020-09-26 21:33:06 +00:00
2020-10-20 15:27:26 +00:00
$this->recur($model, $recur->recurable_type, $schedule_date);
2018-04-27 16:02:42 +00:00
}
}
2021-04-15 21:59:43 +00:00
Company::forgetCurrent();
2022-06-01 07:15:55 +00:00
// Remove from container
app()->forgetInstance(static::class);
2018-04-27 16:02:42 +00:00
}
2020-09-26 21:33:06 +00:00
protected function recur($model, $type, $schedule_date)
2018-04-27 16:02:42 +00:00
{
2022-06-01 07:15:55 +00:00
DB::transaction(function () use ($model, $type, $schedule_date) {
/** @var Document|Transaction $clone */
if (! $clone = $this->getClone($model, $schedule_date)) {
2020-09-30 14:01:35 +00:00
return;
}
2018-05-01 16:00:33 +00:00
2020-09-30 14:01:35 +00:00
switch ($type) {
2020-12-23 22:28:38 +00:00
case 'App\Models\Document\Document':
event(new DocumentCreated($clone, request()));
2018-04-27 19:16:35 +00:00
event(new DocumentRecurring($clone));
2019-11-16 07:21:14 +00:00
2020-09-30 14:01:35 +00:00
break;
case 'App\Models\Banking\Transaction':
event(new TransactionCreated($clone));
2019-11-16 07:21:14 +00:00
2020-09-30 14:01:35 +00:00
event(new TransactionRecurring($clone));
2019-11-16 07:21:14 +00:00
2020-09-30 14:01:35 +00:00
break;
}
});
2018-04-27 16:02:42 +00:00
}
2019-11-16 07:21:14 +00:00
/**
2020-09-26 21:33:06 +00:00
* Clone the model and return it.
2019-11-16 07:21:14 +00:00
*
* @param $model
2020-09-26 21:33:06 +00:00
* @param $schedule_date
2019-11-16 07:21:14 +00:00
*
* @return boolean|object
*/
2020-09-26 21:33:06 +00:00
protected function getClone($model, $schedule_date)
2018-04-27 16:02:42 +00:00
{
2020-09-26 21:33:06 +00:00
if ($this->skipThisClone($model, $schedule_date)) {
2019-11-16 07:21:14 +00:00
return false;
}
2020-09-26 21:33:06 +00:00
$function = ($model instanceof Transaction) ? 'getTransactionClone' : 'getDocumentClone';
2018-05-01 16:00:33 +00:00
2020-07-10 10:37:48 +00:00
try {
2020-09-26 21:33:06 +00:00
return $this->$function($model, $schedule_date);
2021-06-08 20:33:17 +00:00
} catch (\Throwable $e) {
2020-07-10 10:37:48 +00:00
$this->error($e->getMessage());
2021-06-08 20:33:17 +00:00
report($e);
2020-07-10 10:37:48 +00:00
return false;
}
2020-09-26 21:33:06 +00:00
}
2018-04-27 16:02:42 +00:00
2020-09-26 21:33:06 +00:00
/**
* Clone the document and return it.
*
* @param $model
* @param $schedule_date
*
* @return boolean|object
*/
protected function getDocumentClone($model, $schedule_date)
{
$model->cloneable_relations = ['items', 'totals'];
$clone = $model->duplicate();
2019-11-16 07:21:14 +00:00
// Days between issued and due date
2022-06-01 07:15:55 +00:00
$diff_days = Date::parse($model->due_at)->diffInDays(Date::parse($model->issued_at));
2018-04-27 19:16:35 +00:00
2022-06-01 07:15:55 +00:00
$clone->type = $this->getRealType($clone->type);
2020-09-26 21:33:06 +00:00
$clone->parent_id = $model->id;
2022-06-01 07:15:55 +00:00
$clone->issued_at = $schedule_date->format('Y-m-d');
2020-09-26 21:33:06 +00:00
$clone->due_at = $schedule_date->copy()->addDays($diff_days)->format('Y-m-d');
2021-09-10 08:21:11 +00:00
$clone->created_from = 'core::recurring';
2018-04-27 19:16:35 +00:00
$clone->save();
2020-02-21 08:12:52 +00:00
2019-11-16 07:21:14 +00:00
return $clone;
2018-04-27 16:02:42 +00:00
}
2020-09-26 21:33:06 +00:00
/**
* Clone the transaction and return it.
*
* @param $model
* @param $schedule_date
*
* @return boolean|object
*/
protected function getTransactionClone($model, $schedule_date)
{
$model->cloneable_relations = [];
$clone = $model->duplicate();
2022-06-01 07:15:55 +00:00
$clone->type = $this->getRealType($clone->type);
2020-09-26 21:33:06 +00:00
$clone->parent_id = $model->id;
$clone->paid_at = $schedule_date->format('Y-m-d');
2021-09-10 08:21:11 +00:00
$clone->created_from = 'core::recurring';
2020-09-26 21:33:06 +00:00
$clone->save();
return $clone;
}
protected function skipThisClone($model, $schedule_date)
{
$date_field = $this->getDateField($model);
// Skip model created on the same day, but scheduler hasn't run yet
if ($schedule_date->equalTo(Date::parse($model->$date_field->format('Y-m-d')))) {
return true;
}
2022-06-01 07:15:55 +00:00
$already_cloned = DB::table($model->getTable())
2020-09-26 21:33:06 +00:00
->where('parent_id', $model->id)
->whereDate($date_field, $schedule_date)
->value('id');
// Skip if already cloned
if ($already_cloned) {
return true;
}
return false;
}
2020-09-30 14:01:35 +00:00
protected function getChildrenCount($model)
{
2022-06-01 07:15:55 +00:00
return DB::table($model->getTable())
2020-09-30 14:01:35 +00:00
->where('parent_id', $model->id)
->count();
}
2020-09-26 21:33:06 +00:00
protected function getDateField($model)
{
if ($model instanceof Transaction) {
return 'paid_at';
}
2020-12-23 22:28:38 +00:00
if ($model instanceof Document) {
return 'issued_at';
2020-09-26 21:33:06 +00:00
}
}
2022-06-01 07:15:55 +00:00
public function getRealType(string $recurring_type): string
2020-09-26 21:33:06 +00:00
{
2022-06-01 07:15:55 +00:00
return Str::replace('-recurring', '', $recurring_type);
2020-09-26 21:33:06 +00:00
}
2018-04-27 16:02:42 +00:00
}