Console recurring re-factory
This commit is contained in:
parent
66e4c4064e
commit
fded803b72
@ -9,7 +9,7 @@ use App\Events\Purchase\BillRecurring;
|
|||||||
use App\Events\Sale\InvoiceCreated;
|
use App\Events\Sale\InvoiceCreated;
|
||||||
use App\Events\Sale\InvoiceRecurring;
|
use App\Events\Sale\InvoiceRecurring;
|
||||||
use App\Models\Banking\Transaction;
|
use App\Models\Banking\Transaction;
|
||||||
use App\Models\Common\Company;
|
use App\Models\Common\Recurring;
|
||||||
use App\Models\Sale\Invoice;
|
use App\Models\Sale\Invoice;
|
||||||
use App\Utilities\Date;
|
use App\Utilities\Date;
|
||||||
use App\Utilities\Overrider;
|
use App\Utilities\Overrider;
|
||||||
@ -41,19 +41,52 @@ class RecurringCheck extends Command
|
|||||||
// Disable model cache
|
// Disable model cache
|
||||||
config(['laravel-model-caching.enabled' => false]);
|
config(['laravel-model-caching.enabled' => false]);
|
||||||
|
|
||||||
// Get all companies
|
// Get all recurring
|
||||||
$companies = Company::enabled()->withCount('recurring')->cursor();
|
$recurring = Recurring::allCompanies()->with('company')->cursor();
|
||||||
|
|
||||||
foreach ($companies as $company) {
|
$this->info('Creating recurring records ' . $recurring->count());
|
||||||
// Check company recurring
|
|
||||||
if (!$company->recurring_count) {
|
foreach ($recurring as $recur) {
|
||||||
|
if (empty($recur->company)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->info('Creating recurring records for ' . $company->name . ' company.');
|
$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();
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
// Set company id
|
// Set company id
|
||||||
session(['company_id' => $company->id]);
|
session(['company_id' => $recur->company_id]);
|
||||||
|
|
||||||
// Override settings and currencies
|
// Override settings and currencies
|
||||||
Overrider::load('settings');
|
Overrider::load('settings');
|
||||||
@ -61,34 +94,32 @@ class RecurringCheck extends Command
|
|||||||
|
|
||||||
$today = Date::today();
|
$today = Date::today();
|
||||||
|
|
||||||
foreach ($company->recurring as $recurring) {
|
if (!$model = $recur->recurable) {
|
||||||
if (!$model = $recurring->recurable) {
|
continue;
|
||||||
continue;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$schedules = $recurring->getRecurringSchedule();
|
$schedules = $recur->getRecurringSchedule();
|
||||||
|
|
||||||
$children_count = $this->getChildrenCount($model);
|
$children_count = $this->getChildrenCount($model);
|
||||||
$schedule_count = $schedules->count();
|
$schedule_count = $schedules->count();
|
||||||
|
|
||||||
// All recurring created, including today
|
// All recurring created, including today
|
||||||
if ($children_count > ($schedule_count - 1)) {
|
if ($children_count > ($schedule_count - 1)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recur only today
|
// Recur only today
|
||||||
if ($children_count == ($schedule_count - 1)) {
|
if ($children_count == ($schedule_count - 1)) {
|
||||||
$this->recur($model, $recurring->recurable_type, $today);
|
$this->recur($model, $recur->recurable_type, $today);
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recur all schedules, previously failed
|
// Recur all schedules, previously failed
|
||||||
foreach ($schedules as $schedule) {
|
foreach ($schedules as $schedule) {
|
||||||
$schedule_date = Date::parse($schedule->getStart()->format('Y-m-d'));
|
$schedule_date = Date::parse($schedule->getStart()->format('Y-m-d'));
|
||||||
|
|
||||||
$this->recur($model, $recurring->recurable_type, $schedule_date);
|
$this->recur($model, $recur->recurable_type, $schedule_date);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,7 +127,7 @@ class User extends Authenticatable
|
|||||||
/**
|
/**
|
||||||
* Always return a valid picture when we retrieve it
|
* Always return a valid picture when we retrieve it
|
||||||
*/
|
*/
|
||||||
public function getLastLoggedInAtAttribute($value)
|
public function getLastLoggedAttribute($value)
|
||||||
{
|
{
|
||||||
// Date::setLocale('tr');
|
// Date::setLocale('tr');
|
||||||
|
|
||||||
|
@ -26,4 +26,16 @@ class Recurring extends Model
|
|||||||
{
|
{
|
||||||
return $this->morphTo();
|
return $this->morphTo();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scope to get all rows filtered, sorted and paginated.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Database\Eloquent\Builder
|
||||||
|
*/
|
||||||
|
public function scopeAllCompanies($query)
|
||||||
|
{
|
||||||
|
return $query->where('company_id', '<>', '0');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user