119 lines
4 KiB
PHP
119 lines
4 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Models\UserAbo;
|
|
use App\Services\AboHelper;
|
|
|
|
class AboRepository extends BaseRepository
|
|
{
|
|
public function __construct()
|
|
{
|
|
// $this->model = $model;
|
|
}
|
|
|
|
public function setModel(UserAbo $model)
|
|
{
|
|
$this->model = $model;
|
|
}
|
|
|
|
public function update($data)
|
|
{
|
|
if (isset($data['action'])) {
|
|
if ($data['action'] === 'abo_update_settings') {
|
|
if ($this->validate($data)) {
|
|
$this->updateStatus($data);
|
|
$this->model->abo_interval = $data['abo_interval'];
|
|
$this->model->next_date = AboHelper::setNextDate(now(), $data['abo_interval']);
|
|
$this->model->save();
|
|
\Session()->flash('alert-success', 'Einstellungen gespeichert');
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function create($data) {}
|
|
|
|
private function updateStatus($data)
|
|
{
|
|
// Handle cancellation
|
|
if (isset($data['abo_cancel']) && $data['abo_cancel'] == 'true') {
|
|
// Status 4 = abo_cancel (storniert/gekündigt)
|
|
$this->model->status = 4;
|
|
$this->model->active = false;
|
|
$this->model->cancel_date = now();
|
|
$this->model->save();
|
|
|
|
return;
|
|
}
|
|
|
|
$active = (isset($data['abo_is_active']) && $data['abo_is_active']) ? true : false;
|
|
// if status is active and active is false, set status to inactive
|
|
if ($this->model->active && ! $active) {
|
|
if ($this->model->status == 2) { // okay
|
|
$this->model->status = 6; // inactive
|
|
$this->model->active = false;
|
|
$this->model->save();
|
|
}
|
|
}
|
|
if (! $this->model->active && $active) {
|
|
if ($this->model->status = 6) { // inactive
|
|
$this->model->status = 2; // okay
|
|
$this->model->active = true;
|
|
$this->model->save();
|
|
}
|
|
}
|
|
$this->model->active = $active;
|
|
}
|
|
|
|
private function validate($data)
|
|
{
|
|
if ($data['view'] !== 'admin' && $data['view'] !== 'portal') {
|
|
if ($this->model->is_for === 'me' && $this->model->user_id !== \Auth::user()->id) {
|
|
\Session()->flash('alert-error', 'Unauthorized action. User ID does not match.');
|
|
|
|
return false;
|
|
}
|
|
if ($this->model->is_for === 'ot' && $this->model->member_id !== \Auth::user()->id) {
|
|
\Session()->flash('alert-error', 'Unauthorized action. User ID does not match.');
|
|
|
|
return false;
|
|
}
|
|
if ($data['view'] === 'me' && $this->model->is_for !== 'me') {
|
|
\Session()->flash('alert-error', 'Unauthorized action. Is not for me');
|
|
|
|
return false;
|
|
}
|
|
if ($data['view'] === 'ot' && $this->model->is_for !== 'ot') {
|
|
\Session()->flash('alert-error', 'Unauthorized action. Is not your customer');
|
|
|
|
return false;
|
|
}
|
|
}
|
|
if (! in_array($data['abo_interval'], \App\Models\UserAbo::$aboDeliveryDays)) {
|
|
// to check if user is not admin
|
|
\Session()->flash('alert-error', __('abo.error_abo_interval'));
|
|
|
|
return false;
|
|
}
|
|
|
|
// Prüfung: Wenn das Abo diesen Monat noch nicht ausgeführt wurde (oder noch nie),
|
|
// darf das Abo-Intervall nicht auf einen Tag gesetzt werden, der bereits vergangen ist (oder heute ist),
|
|
// da setNextDate das nächste Ausführungsdatum sonst auf den nächsten Monat setzt und dieser Monat übersprungen wird.
|
|
$executedThisMonth = $this->model->last_date && \Carbon\Carbon::parse($this->model->last_date)->isCurrentMonth();
|
|
|
|
if (! $executedThisMonth && $data['abo_interval'] <= now()->day) {
|
|
\Session()->flash('alert-error', __('abo.error_abo_interval_in_the_past'));
|
|
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|