161 lines
5.4 KiB
PHP
161 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\User;
|
|
use Carbon\Carbon;
|
|
use App\Services\UserUtil;
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
class UserCleanUp extends Command
|
|
{
|
|
/**
|
|
* ln -sfv /usr/bin/php73 /usr/bin/php
|
|
* php74 artisan user:cleanup
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'user:cleanup';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'User Clean Up inactive for Business Structur and UserDetails';
|
|
private $timeStart;
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
|
|
$this->info('RUN Command user:cleanup');
|
|
\Log::channel('cleanup')->info('COMMAND [user:cleanup] started.');
|
|
|
|
$this->timeStart = microtime(true);
|
|
|
|
$this->deleteInavtiveUsers();
|
|
//alle inaktive User werden deaktivert, die childs werden dem nächsten aktiven Berater (parent) zugewiesen.
|
|
$this->cleanUpInActiveUser();
|
|
|
|
return 0;
|
|
|
|
\Log::channel('cleanup')->info('COMMAND [user:cleanup] finished.');
|
|
//return 0;
|
|
}
|
|
|
|
//gibt es gelöschte Berater mit Kunden und childs???
|
|
|
|
private function deleteInavtiveUsers()
|
|
{
|
|
|
|
$this->info('START Command deleteInavtiveUsers');
|
|
$count = 0;
|
|
|
|
$date = Carbon::now()->modify('-2 month');
|
|
$delete_users = User::where('admin', 0)->where('payment_account', '<', $date)->get();
|
|
|
|
foreach ($delete_users as $delete_user) {
|
|
/*
|
|
dump('delete_users ---------- ');
|
|
dump($delete_user->id);
|
|
dump($delete_user->email);
|
|
*/
|
|
//finde nächsten aktiven Sponsor $delete_user->id kann sponsor oder pre sponsor sein
|
|
$active_sponsor = UserUtil::findNextActiveSponsor($delete_user->id);
|
|
if ($active_sponsor) {
|
|
//setze alle Berater vom Sponsor für alle childs
|
|
UserUtil::setNewSponsorToChilds($delete_user->id, $active_sponsor->id);
|
|
UserUtil::setShoppingUserToNewMember($delete_user->id, $active_sponsor->id);
|
|
} else {
|
|
\Log::channel('cleanup')->error('deleteInavtiveUsers find no active_sponsor by delete_user_id:' . $delete_user->id);
|
|
continue;
|
|
}
|
|
/*
|
|
dump('findNextActiveSponsor');
|
|
dump($active_sponsor->email);
|
|
*/
|
|
//make User to an Client from sponsor and delete User
|
|
UserUtil::setUserToClient($delete_user->id, $active_sponsor->id);
|
|
|
|
$data = [
|
|
'user_id' => $delete_user->id,
|
|
'email' => $delete_user->email,
|
|
'm_account' => $delete_user->account ? $delete_user->account->m_account : '',
|
|
'm_first_name' => $delete_user->account ? $delete_user->account->m_first_name : '',
|
|
'm_last_name' => $delete_user->account ? $delete_user->account->m_last_name : '',
|
|
];
|
|
$count++;
|
|
\Log::channel('cleanup')->info('deleteUser: ' . json_encode($data));
|
|
UserUtil::deleteUser($delete_user);
|
|
}
|
|
|
|
$diff = microtime(true) - $this->timeStart;
|
|
$sec = intval($diff);
|
|
$micro = $diff - $sec;
|
|
|
|
$this->info('END Command deleteInavtiveUsers: ' . $count . ' | Time: ' . $sec . 'sec :' . round($micro * 1000, 4) . " ms");
|
|
}
|
|
|
|
private function cleanUpInActiveUser()
|
|
{
|
|
|
|
$this->info('START Command cleanUpInActiveUser');
|
|
$count = 0;
|
|
|
|
//clean up user where inactive since 2 weeks
|
|
$date = Carbon::now()->modify('-2 weeks');
|
|
|
|
$inactive_users = User::where('active', true)->where('m_sponsor', '!=', null)->where('payment_account', '<', $date)->get();
|
|
foreach ($inactive_users as $inactive_user) {
|
|
/*
|
|
dump('inactive_user ---------- ');
|
|
dump($inactive_user->id);
|
|
dump($inactive_user->email);
|
|
*/
|
|
$active_sponsor = UserUtil::findNextActiveSponsor($inactive_user->m_sponsor);
|
|
if ($active_sponsor) {
|
|
UserUtil::setNewSponsorToChilds($inactive_user->id, $active_sponsor->id);
|
|
} else {
|
|
\Log::channel('cleanup')->error('cleanUpInActiveUser find no active_sponsor by inactive_user:' . $inactive_user->id);
|
|
}
|
|
/*
|
|
dump('findNextActiveSponsor');
|
|
dump($active_sponsor->email);
|
|
*/
|
|
$data = [
|
|
'user_id' => $inactive_user->id,
|
|
'email' => $inactive_user->email,
|
|
'm_account' => $inactive_user->account ? $inactive_user->account->m_account : '',
|
|
'm_first_name' => $inactive_user->account ? $inactive_user->account->m_first_name : '',
|
|
'm_last_name' => $inactive_user->account ? $inactive_user->account->m_last_name : '',
|
|
];
|
|
$count++;
|
|
|
|
\Log::channel('cleanup')->info('inactive_user: ' . json_encode($data));
|
|
UserUtil::deactiveUser($inactive_user);
|
|
}
|
|
|
|
$diff = microtime(true) - $this->timeStart;
|
|
$sec = intval($diff);
|
|
$micro = $diff - $sec;
|
|
|
|
$this->info('END Command cleanUpInActiveUser: ' . $count . ' | Time: ' . $sec . 'sec :' . round($micro * 1000, 4) . " ms");
|
|
}
|
|
}
|