akaunting/app/Console/Commands/RecurringCheck.php

279 lines
7.4 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;
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()
{
// Disable model cache
config(['laravel-model-caching.enabled' => false]);
2020-10-20 18:27:26 +03:00
// Get all recurring
$recurring = Recurring::allCompanies()->with('company')->get();
2020-10-20 18:27:26 +03:00
$this->info('Creating recurring records ' . $recurring->count());
foreach ($recurring as $recur) {
if (empty($recur->company)) {
continue;
}
$company_name = !empty($recur->company->name) ? $recur->company->name : 'Missing Company Name : ' . $recur->company->id;
$this->info('Creating recurring records for ' . $company_name . ' company...');
// Check if company is disabled
if (!$recur->company->enabled) {
$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 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;
}
}
if (!$has_active_users) {
$this->info('No active users for ' . $company_name . ' company. Skipping...');
$recur->delete();
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
2020-09-30 17:01:35 +03:00
$today = Date::today();
2018-04-27 19:02:42 +03:00
2020-10-20 18:27:26 +03:00
if (!$model = $recur->recurable) {
continue;
}
2020-09-27 00:33:06 +03:00
2020-10-20 18:27:26 +03:00
$schedules = $recur->getRecurringSchedule();
2020-09-30 17:01:35 +03:00
2020-10-20 18:27:26 +03:00
$children_count = $this->getChildrenCount($model);
$schedule_count = $schedules->count();
2020-09-30 17:01:35 +03:00
2020-10-20 18:27:26 +03:00
// All recurring created, including today
if ($children_count > ($schedule_count - 1)) {
continue;
}
2020-09-30 17:01:35 +03:00
2020-10-20 18:27:26 +03:00
// Recur only today
if ($children_count == ($schedule_count - 1)) {
$this->recur($model, $recur->recurable_type, $today);
2020-09-30 17:01:35 +03:00
2020-10-20 18:27:26 +03:00
continue;
}
2020-09-30 17:01:35 +03:00
2020-10-20 18:27:26 +03:00
// Recur all schedules, previously failed
foreach ($schedules as $schedule) {
$schedule_date = Date::parse($schedule->getStart()->format('Y-m-d'));
2020-09-27 00:33:06 +03:00
2020-10-20 18:27:26 +03:00
$this->recur($model, $recur->recurable_type, $schedule_date);
2018-04-27 19:02:42 +03:00
}
}
2021-04-16 00:59:43 +03:00
Company::forgetCurrent();
2018-04-27 19:02:42 +03:00
}
2020-09-27 00:33:06 +03:00
protected function recur($model, $type, $schedule_date)
2018-04-27 19:02:42 +03:00
{
2020-09-30 17:01:35 +03:00
\DB::transaction(function () use ($model, $type, $schedule_date) {
/** @var Document $clone */
2020-09-30 17:01:35 +03:00
if (!$clone = $this->getClone($model, $schedule_date)) {
return;
}
2018-05-01 19:00:33 +03:00
2020-09-30 17:01:35 +03:00
switch ($type) {
2020-12-24 01:28:38 +03:00
case 'App\Models\Document\Document':
event(new DocumentCreated($clone, request()));
2018-04-27 22:16:35 +03:00
event(new DocumentRecurring($clone));
2019-11-16 10:21:14 +03:00
2020-09-30 17:01:35 +03:00
break;
case 'App\Models\Banking\Transaction':
event(new TransactionCreated($clone));
2019-11-16 10:21:14 +03:00
2020-09-30 17:01:35 +03:00
event(new TransactionRecurring($clone));
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
}
2019-11-16 10:21:14 +03:00
/**
2020-09-27 00:33:06 +03:00
* Clone the model and return it.
2019-11-16 10:21:14 +03:00
*
* @param $model
2020-09-27 00:33:06 +03:00
* @param $schedule_date
2019-11-16 10:21:14 +03:00
*
* @return boolean|object
*/
2020-09-27 00:33:06 +03:00
protected function getClone($model, $schedule_date)
2018-04-27 19:02:42 +03:00
{
2020-09-27 00:33:06 +03:00
if ($this->skipThisClone($model, $schedule_date)) {
2019-11-16 10:21:14 +03:00
return false;
}
2020-09-27 00:33:06 +03:00
$function = ($model instanceof Transaction) ? 'getTransactionClone' : 'getDocumentClone';
2018-05-01 19:00:33 +03:00
2020-07-10 13:37:48 +03:00
try {
2020-09-27 00:33:06 +03:00
return $this->$function($model, $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
2020-09-27 00:33:06 +03: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();
$date_field = $this->getDateField($model);
2018-05-01 19:00:33 +03:00
2019-11-16 10:21:14 +03:00
// Days between issued and due date
$diff_days = Date::parse($clone->due_at)->diffInDays(Date::parse($clone->$date_field));
2018-04-27 22:16:35 +03:00
2020-09-27 00:33:06 +03:00
$clone->parent_id = $model->id;
$clone->$date_field = $schedule_date->format('Y-m-d');
$clone->due_at = $schedule_date->copy()->addDays($diff_days)->format('Y-m-d');
2021-09-10 11:21:11 +03:00
$clone->created_from = 'core::recurring';
2018-04-27 22:16:35 +03:00
$clone->save();
2020-02-21 11:12:52 +03:00
2019-11-16 10:21:14 +03:00
return $clone;
2018-04-27 19:02:42 +03:00
}
2020-09-27 00:33:06 +03: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();
$clone->parent_id = $model->id;
$clone->paid_at = $schedule_date->format('Y-m-d');
2021-09-10 11:21:11 +03:00
$clone->created_from = 'core::recurring';
2020-09-27 00:33:06 +03: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;
}
$table = $this->getTable($model);
$already_cloned = \DB::table($table)
->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 17:01:35 +03:00
protected function getChildrenCount($model)
{
$table = $this->getTable($model);
return \DB::table($table)
->where('parent_id', $model->id)
->count();
}
2020-09-27 00:33:06 +03:00
protected function getDateField($model)
{
if ($model instanceof Transaction) {
return 'paid_at';
}
2020-12-24 01:28:38 +03:00
if ($model instanceof Document) {
return 'issued_at';
2020-09-27 00:33:06 +03:00
}
}
protected function getTable($model)
{
if ($model instanceof Transaction) {
return 'transactions';
}
2020-12-24 01:28:38 +03:00
if ($model instanceof Document) {
return 'documents';
2020-09-27 00:33:06 +03:00
}
}
2018-04-27 19:02:42 +03:00
}