Updates to 03-2025
This commit is contained in:
parent
bfa3bb1df4
commit
9ae662f63e
243 changed files with 12580 additions and 12018 deletions
|
|
@ -12,13 +12,16 @@ use App\Mail\MailVerifyAccount;
|
|||
use App\Services\PaymentHelper;
|
||||
use App\Repositories\UserRepository;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
|
||||
class CronController extends Controller
|
||||
{
|
||||
|
||||
|
||||
protected $userRepo;
|
||||
|
||||
// Konstanten für bessere Lesbarkeit
|
||||
private const CRON_KEY = 'CqZHL79FwUCcy9pjvi';
|
||||
private const RUN_CRON_KEY = 'G8ZvEbnP8fEPfnWX4L';
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
|
|
@ -28,58 +31,94 @@ class CronController extends Controller
|
|||
public function __construct(UserRepository $userRepo)
|
||||
{
|
||||
$this->userRepo = $userRepo;
|
||||
|
||||
// $this->middleware('auth');
|
||||
Log::channel('cron')->info('CronController initialisiert');
|
||||
}
|
||||
|
||||
/**
|
||||
* Hauptindex-Methode für Cron-Jobs
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
Log::channel('cron')->info('Cron-Index aufgerufen');
|
||||
//$this->checkConfirmation();
|
||||
//TODO
|
||||
//SEPA Booking
|
||||
//Mail reminder
|
||||
return "Cron-Index ausgeführt";
|
||||
}
|
||||
|
||||
public function action($action = false, $key = false){
|
||||
|
||||
if($key !== 'CqZHL79FwUCcy9pjvi'){
|
||||
/**
|
||||
* Führt eine bestimmte Cron-Aktion aus
|
||||
*
|
||||
* @param string|bool $action Die auszuführende Aktion
|
||||
* @param string|bool $key Sicherheitsschlüssel
|
||||
* @return mixed
|
||||
*/
|
||||
public function action($action = false, $key = false)
|
||||
{
|
||||
Log::channel('cron')->info('Cron-Aktion aufgerufen: ' . $action);
|
||||
|
||||
if($key !== self::CRON_KEY){
|
||||
Log::channel('cron')->warning('Ungültiger Cron-Key verwendet: ' . $key);
|
||||
abort(404);
|
||||
}
|
||||
|
||||
if($action === 'check_payments_account'){
|
||||
$this->checkPaymentsAccounts();
|
||||
Log::channel('cron')->info('Starte Überprüfung der Zahlungskonten');
|
||||
return $this->checkPaymentsAccounts();
|
||||
}
|
||||
|
||||
Log::channel('cron')->warning('Unbekannte Aktion angefordert: ' . $action);
|
||||
return response('Keine gültige Aktion angegeben', 400);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the application dashboard.
|
||||
* Überprüft Benutzerbestätigungen und sendet Erinnerungen
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
* @return string
|
||||
*/
|
||||
public function checkConfirmation()
|
||||
{
|
||||
Log::channel('cron')->info('Starte Überprüfung der Benutzerbestätigungen');
|
||||
|
||||
$now = date('Y-m-d H:i:s');
|
||||
$next = date('Y-m-d H:i:s', strtotime('+3 week'));
|
||||
|
||||
$users = User::where('confirmed', '=', 0)->where('confirmation_code_to', '<', $now)->get();
|
||||
Log::channel('cron')->info('Gefundene unbestätigte Benutzer: ' . $users->count());
|
||||
|
||||
foreach ($users as $user) {
|
||||
|
||||
//delete user
|
||||
if ($user->confirmation_code_remider == 1) {
|
||||
Log::channel('cron')->warning('Lösche unbestätigten Benutzer: ' . $user->email);
|
||||
$this->userRepo->deleteUser($user);
|
||||
|
||||
}
|
||||
//send new remider
|
||||
if ($user->confirmation_code_remider == 0) {
|
||||
Mail::to($user->email)->locale($user->getLocale())->send(new MailVerifyAccount($user->confirmation_code, $user));
|
||||
$user->confirmation_code_to = $next;
|
||||
$user->confirmation_code_remider = 1;
|
||||
$user->save();
|
||||
if(!Util::isTestSystem()){
|
||||
Log::channel('cron')->info('Sende Bestätigungserinnerung an: ' . $user->email);
|
||||
Mail::to($user->email)->locale($user->getLocale())->send(new MailVerifyAccount($user->confirmation_code, $user));
|
||||
$user->confirmation_code_to = $next;
|
||||
$user->confirmation_code_remider = 1;
|
||||
$user->save();
|
||||
} else {
|
||||
Log::channel('cron')->info('Testsystem: Bestätigungserinnerung an: ' . $user->email);
|
||||
}
|
||||
}
|
||||
}
|
||||
return "TOSK";
|
||||
}
|
||||
|
||||
public function checkPaymentsAccounts(){
|
||||
/**
|
||||
* Überprüft Zahlungskonten und sendet Erinnerungen
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function checkPaymentsAccounts()
|
||||
{
|
||||
Log::channel('cron')->info('Starte Überprüfung der Zahlungskonten');
|
||||
|
||||
/*RULES
|
||||
reminders
|
||||
|
|
@ -93,207 +132,289 @@ class CronController extends Controller
|
|||
*/
|
||||
//max Date for reminder
|
||||
$renewalDate = Carbon::now()->modify('+'.(config('mivita.remind_first_days')+1).' days');
|
||||
//dump($renewalDate);
|
||||
Log::channel('cron')->info('Erneuerungsdatum für Zahlungen: ' . $renewalDate->format('Y-m-d H:i:s'));
|
||||
|
||||
$users = User::where('payment_account', '!=', NULL)
|
||||
->where('active', '=', 1)
|
||||
->where('blocked', '!=', 1)
|
||||
->where('payment_account', '<', $renewalDate)
|
||||
->get();
|
||||
|
||||
/* $user = User::find(2);
|
||||
$this->checkReminderPayments($user);
|
||||
dump($user->daysActiveAccount());
|
||||
dump($user->email." | ".$user->getPaymentAccountDateFormat());
|
||||
die();*/
|
||||
Log::channel('cron')->info('Gefundene Benutzer für Zahlungserinnerungen: ' . $users->count());
|
||||
|
||||
foreach ($users as $user){
|
||||
Log::channel('cron')->info('Prüfe Zahlungserinnerungen für Benutzer: ' . $user->email);
|
||||
$this->checkReminderPayments($user);
|
||||
/* Abo Option deaktiviert
|
||||
$this->userInitAboPayment($user);
|
||||
*/
|
||||
}
|
||||
|
||||
return "TOSK";
|
||||
}
|
||||
|
||||
private function userInitAboPayment(User $user){
|
||||
/**
|
||||
* Initiiert Abo-Zahlungen für einen Benutzer
|
||||
* hier geht es um die Mitglieschaft Abos - die sind derzeit deaktiviert
|
||||
*
|
||||
* @param User $user Benutzer
|
||||
* @return bool
|
||||
*/
|
||||
private function userInitAboPayment(User $user)
|
||||
{
|
||||
if(!$user->isAcountAboPayDate()){
|
||||
Log::channel('cron')->info('Kein Abo-Zahlungsdatum für Benutzer: ' . $user->email);
|
||||
return false;
|
||||
}
|
||||
|
||||
//user has a open Abo Payment
|
||||
if($this->checkIsAboPaymentOpen($user)){
|
||||
Log::channel('cron')->info('Offene Abo-Zahlung für Benutzer: ' . $user->email);
|
||||
return false;
|
||||
}
|
||||
|
||||
if($user->payment_order_product){
|
||||
Log::channel('cron')->info('Starte Abo-Zahlung für Benutzer: ' . $user->email);
|
||||
$this->buyProductAboPayment($user, $user->payment_order_product);
|
||||
}
|
||||
/*dump($user->daysActiveAccount());
|
||||
dump($user->email." | ".$user->getPaymentAccountDateFormat());
|
||||
dump('-------------------');*/
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function checkIsAboPaymentOpen(User $user){
|
||||
/**
|
||||
* Prüft, ob eine offene Abo-Zahlung existiert
|
||||
*
|
||||
* @param User $user Benutzer
|
||||
* @return bool
|
||||
*/
|
||||
private function checkIsAboPaymentOpen(User $user)
|
||||
{
|
||||
$isOpen = UserHistory::whereUserId($user->id)
|
||||
->whereAction('abo_open_payment')
|
||||
->whereIdentifier($user->payment_account)
|
||||
->where('status', '>=', 1) //open //error // payment
|
||||
->get()->last();
|
||||
|
||||
if($isOpen){
|
||||
Log::channel('cron')->info('Offene Abo-Zahlung gefunden für: ' . $user->email);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function checkReminderPayments(User $user){
|
||||
|
||||
/* $isSend = $this->checkIsReminderSend($user, 31);
|
||||
$isSend = $this->checkIsReminderSend($user, 32);
|
||||
$isSend = $this->checkIsReminderSend($user, 33);
|
||||
$isSend = $this->checkIsReminderSend($user, 34);
|
||||
$isSend = $this->checkIsReminderSend($user, 35);
|
||||
$isSend = $this->checkIsReminderSend($user, 36);
|
||||
return ;*/
|
||||
|
||||
/**
|
||||
* Prüft und sendet Zahlungserinnerungen basierend auf Benutzerkontostand
|
||||
*
|
||||
* @param User $user Benutzer
|
||||
* @return bool
|
||||
*/
|
||||
private function checkReminderPayments(User $user)
|
||||
{
|
||||
//35 reminder_deaktiv, 36 reminder_deaktiv_sepa
|
||||
if(!$user->isActiveAccount()){
|
||||
/* Abo Option deaktiviert
|
||||
if($user->isAboOption()){
|
||||
$isSend = $this->checkIsReminderSend($user, 36);
|
||||
return $isSend;
|
||||
}
|
||||
*/
|
||||
Log::channel('cron')->info('Inaktives Konto für Benutzer: ' . $user->email);
|
||||
$isSend = $this->checkIsReminderSend($user, 35);
|
||||
return $isSend;
|
||||
}
|
||||
|
||||
//34 reminder_last
|
||||
if($user->daysActiveAccount() <= config('mivita.remind_last_days')){
|
||||
Log::channel('cron')->info('Letzte Erinnerung für Benutzer: ' . $user->email . ' (Tage aktiv: ' . $user->daysActiveAccount() . ')');
|
||||
$isSend = $this->checkIsReminderSend($user, 34);
|
||||
return $isSend;
|
||||
}
|
||||
|
||||
//33 reminder_sec
|
||||
if($user->daysActiveAccount() <= config('mivita.remind_sec_days')){
|
||||
/* Abo Option deaktiviert
|
||||
if(!$user->isAboOption()){
|
||||
$isSend = $this->checkIsReminderSend($user, 33);
|
||||
return $isSend;
|
||||
}
|
||||
*/
|
||||
|
||||
Log::channel('cron')->info('Zweite Erinnerung für Benutzer: ' . $user->email . ' (Tage aktiv: ' . $user->daysActiveAccount() . ')');
|
||||
$isSend = $this->checkIsReminderSend($user, 33);
|
||||
return $isSend;
|
||||
}
|
||||
|
||||
//31 reminder_first, 32 reminder_first_sepa
|
||||
//31 reminder_first
|
||||
if($user->daysActiveAccount() > config('mivita.remind_sec_days')){
|
||||
/* Abo Option deaktiviert
|
||||
if($user->isAboOption()){
|
||||
$isSend = $this->checkIsReminderSend($user, 32);
|
||||
return $isSend;
|
||||
} */
|
||||
Log::channel('cron')->info('Erste Erinnerung für Benutzer: ' . $user->email . ' (Tage aktiv: ' . $user->daysActiveAccount() . ')');
|
||||
$isSend = $this->checkIsReminderSend($user, 31);
|
||||
return $isSend;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function checkIsReminderSend(User $user, $status){
|
||||
|
||||
/**
|
||||
* Überprüft, ob eine Erinnerung bereits gesendet wurde
|
||||
*
|
||||
* @param User $user Benutzer
|
||||
* @param int $status Status-Code der Erinnerung
|
||||
* @return bool
|
||||
*/
|
||||
private function checkIsReminderSend(User $user, $status)
|
||||
{
|
||||
$isSend = UserHistory::whereUserId($user->id)
|
||||
->whereAction('reminder_payments')
|
||||
->whereIdentifier($user->payment_account)
|
||||
->whereStatus($status)
|
||||
->get()->last();
|
||||
->latest()
|
||||
->first();
|
||||
|
||||
if($isSend){
|
||||
Log::channel('cron')->info('Erinnerung bereits gesendet für Benutzer: ' . $user->email . ' (Status: ' . $status . ')');
|
||||
return true;
|
||||
}
|
||||
|
||||
Log::channel('cron')->info('Sende neue Erinnerung für Benutzer: ' . $user->email . ' (Status: ' . $status . ')');
|
||||
$referenz = $this->sendReminderMail($user, $status);
|
||||
|
||||
//is not sent create
|
||||
UserHistory::create(['user_id' => $user->id, 'action'=>'reminder_payments', 'referenz'=>$referenz, 'identifier'=>$user->payment_account, 'status'=>$status]);
|
||||
UserHistory::create([
|
||||
'user_id' => $user->id,
|
||||
'action' => 'reminder_payments',
|
||||
'referenz' => $referenz,
|
||||
'identifier' => $user->payment_account,
|
||||
'status' => $status
|
||||
]);
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
private function sendReminderMail(User $user, $status){
|
||||
|
||||
$days = $user->daysActiveAccount();
|
||||
if($days < 0){
|
||||
$days = $days*-1;
|
||||
}
|
||||
|
||||
//dump($days);
|
||||
//dump($status);
|
||||
$pay_date = Carbon::parse($user->payment_account)->modify('- '.config('mivita.abo_booking_days').' days')->format('d.m.Y');
|
||||
/**
|
||||
* Sendet eine Erinnerungs-E-Mail an den Benutzer
|
||||
*
|
||||
* @param User $user Benutzer
|
||||
* @param int $status Status-Code der Erinnerung
|
||||
* @return int
|
||||
*/
|
||||
private function sendReminderMail(User $user, $status)
|
||||
{
|
||||
$days = abs($user->daysActiveAccount());
|
||||
|
||||
$pay_date = Carbon::parse($user->payment_account)
|
||||
->modify('- ' . config('mivita.abo_booking_days') . ' days')
|
||||
->format('d.m.Y');
|
||||
|
||||
$datetime = $user->getPaymentAccountDateFormat();
|
||||
$price = "";
|
||||
|
||||
if($user->payment_order_id && isset($user->payment_order_product->price)){
|
||||
$price = 'von '.$user->payment_order_product->getFormattedPrice().' EUR';
|
||||
$price = 'von ' . $user->payment_order_product->getFormattedPrice() . ' EUR';
|
||||
}
|
||||
|
||||
$message = __('reminder.copy_first_'.$status, ['days'=>$days, 'datetime'=>$datetime, 'price' =>$price, 'pay_date'=>$pay_date]);
|
||||
$message_last = __('reminder.copy_last_'.$status, ['days'=>$days, 'datetime'=>$datetime, 'price' =>$price, 'pay_date'=>$pay_date]);
|
||||
$button = __('reminder.button_'.$status);
|
||||
$message = __('reminder.copy_first_' . $status, [
|
||||
'days' => $days,
|
||||
'datetime' => $datetime,
|
||||
'price' => $price,
|
||||
'pay_date' => $pay_date
|
||||
]);
|
||||
|
||||
$message_last = __('reminder.copy_last_' . $status, [
|
||||
'days' => $days,
|
||||
'datetime' => $datetime,
|
||||
'price' => $price,
|
||||
'pay_date' => $pay_date
|
||||
]);
|
||||
|
||||
$button = __('reminder.button_' . $status);
|
||||
|
||||
$message = preg_replace("/[\n\r]/","",$message);
|
||||
$message_last = preg_replace("/[\n\r]/","",$message_last);
|
||||
$message = preg_replace("/[\n\r]/", "", $message);
|
||||
$message_last = preg_replace("/[\n\r]/", "", $message_last);
|
||||
|
||||
$data = [
|
||||
'subject' => __('reminder.subject')." | ID: ".$status,
|
||||
'subject' => __('reminder.subject') . " | ID: " . $status,
|
||||
'message' => $message,
|
||||
'message_last' => $message_last,
|
||||
'url' => route('user_membership'),
|
||||
'button' => $button,
|
||||
];
|
||||
//dump($data);
|
||||
|
||||
$sender = User::find(1);
|
||||
$customer_mail = UserMessage::create([
|
||||
'user_id' => $user->id,
|
||||
'user_id' => $user->id,
|
||||
'send_user_id' => $sender->id,
|
||||
'email' => $user->email,
|
||||
'subject' => $data['subject'],
|
||||
'message' => $data['message']." ".$data['message_last'],
|
||||
'message' => $data['message'] . " " . $data['message_last'],
|
||||
]);
|
||||
try{
|
||||
if($status >= 34){
|
||||
Mail::to($user->email)->locale($user->getLocale())->bcc(config('app.default_mail'))->send(new MailCustomMessage($user, $data, $sender, false));
|
||||
}else{
|
||||
Mail::to($user->email)->locale($user->getLocale())->send(new MailCustomMessage($user, $data, $sender, false));
|
||||
|
||||
try {
|
||||
if(!Util::isTestSystem()){
|
||||
if($status >= 34){
|
||||
Log::channel('cron')->info('Sende kritische Erinnerung mit BCC an: ' . $user->email);
|
||||
Mail::to($user->email)
|
||||
->locale($user->getLocale())
|
||||
->bcc(config('app.default_mail'))
|
||||
->send(new MailCustomMessage($user, $data, $sender, false));
|
||||
} else {
|
||||
Log::channel('cron')->info('Sende normale Erinnerung an: ' . $user->email);
|
||||
Mail::to($user->email)
|
||||
->locale($user->getLocale())
|
||||
->send(new MailCustomMessage($user, $data, $sender, false));
|
||||
}
|
||||
} else {
|
||||
Log::channel('cron')->info('Testsystem: E-Mail-Versand simuliert für: ' . $user->email);
|
||||
}
|
||||
}
|
||||
catch(\Exception $e){
|
||||
\Log::channel('cron')->error('Mail Error: '.$e->getMessage());
|
||||
// Never reached
|
||||
} catch(\Exception $e) {
|
||||
Log::channel('cron')->error('Mail-Fehler für Benutzer ' . $user->email . ': ' . $e->getMessage());
|
||||
|
||||
$customer_mail->fail = true;
|
||||
$customer_mail->error = $e->getMessage();
|
||||
$customer_mail->save();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
$customer_mail->send = true;
|
||||
$customer_mail->sent_at = now();
|
||||
$customer_mail->save();
|
||||
|
||||
Log::channel('cron')->info('Erinnerungsmail erfolgreich gesendet an: ' . $user->email);
|
||||
return 1;
|
||||
}
|
||||
|
||||
private function buyProductAboPayment($user, $product){
|
||||
/**
|
||||
* Kauft ein Produkt mit Abo-Zahlung
|
||||
*
|
||||
* @param User $user Benutzer
|
||||
* @param object $product Produkt
|
||||
* @return void
|
||||
*/
|
||||
private function buyProductAboPayment($user, $product)
|
||||
{
|
||||
Log::channel('cron')->info('Starte Abo-Produktkauf für Benutzer: ' . $user->email);
|
||||
$paymentHelper = new PaymentHelper();
|
||||
$paymentHelper->setProduct($product);
|
||||
$paymentHelper->initELVPayment($user);
|
||||
Log::channel('cron')->info('Abo-Produktkauf abgeschlossen für: ' . $user->email);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Führt das Cron-Script aus
|
||||
*
|
||||
* @param string $key Sicherheitsschlüssel
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function runCron($key)
|
||||
{
|
||||
if($key !== 'G8ZvEbnP8fEPfnWX4L'){
|
||||
Log::channel('cron')->info('Cron-Script-Ausführung angefordert');
|
||||
|
||||
if($key !== self::RUN_CRON_KEY){
|
||||
Log::channel('cron')->warning('Ungültiger Cron-Script-Key verwendet: ' . $key);
|
||||
abort(404);
|
||||
}
|
||||
if(Util::isTestSystem()){
|
||||
exec("/bin/bash ../cron_script_local.sh 2>&1", $out, $result);
|
||||
}else{
|
||||
exec("/bin/bash ../cron_script_server.sh 2>&1", $out, $result);
|
||||
}
|
||||
echo "Returncode: " .$result ."<br>";
|
||||
echo "Ausgabe des Scripts: " ."<br>";
|
||||
|
||||
$scriptPath = Util::isTestSystem() ? '../cron_script_local.sh' : '../cron_script_server.sh';
|
||||
Log::channel('cron')->info('Führe Script aus: ' . $scriptPath);
|
||||
|
||||
exec("/bin/bash {$scriptPath} 2>&1", $out, $result);
|
||||
|
||||
Log::channel('cron')->info('Cron-Script-Ausführung abgeschlossen mit Code: ' . $result);
|
||||
|
||||
echo "Returncode: " . $result . "<br>";
|
||||
echo "Ausgabe des Scripts: " . "<br>";
|
||||
echo "<pre>"; print_r($out);
|
||||
|
||||
exit;
|
||||
|
||||
/*return response()->view('cron.result', [
|
||||
'result' => $result,
|
||||
'output' => $out
|
||||
]);*/
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue