216 lines
7.9 KiB
PHP
216 lines
7.9 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Models\UserAbo;
|
|
use App\Services\AboHelper;
|
|
use Carbon\Carbon;
|
|
|
|
class AboRepository extends BaseRepository
|
|
{
|
|
private const LOCK_DAYS_CHANGE = 10;
|
|
|
|
private const LOCK_DAYS_PAUSE_CANCEL = 3;
|
|
|
|
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 = (int) $data['abo_interval'];
|
|
$nextDate = $this->isAdminUpdate($data)
|
|
? $this->calculateAdminNextDate($data)
|
|
: $this->calculateNewNextDate((int) $data['abo_interval']);
|
|
$this->model->next_date = $nextDate;
|
|
$this->model->save();
|
|
|
|
$daysUntilNext = AboHelper::calendarDaysUntil(now(), $nextDate);
|
|
\Session()->flash('alert-warning', __('abo.warning_next_date_info', ['days' => $daysUntilNext, 'date' => $nextDate->format('d.m.Y')]));
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function create($data) {}
|
|
|
|
private function updateStatus($data)
|
|
{
|
|
$isAdminUpdate = $this->isAdminUpdate($data);
|
|
|
|
// Handle cancellation
|
|
if (isset($data['abo_cancel']) && $data['abo_cancel'] == 'true') {
|
|
// Sperre: 3 Tage vor Ausführung kann nicht mehr pausiert/gekündigt werden
|
|
if (! $isAdminUpdate && $this->model->next_date) {
|
|
$daysUntil = (int) now()->diffInDays(\Carbon\Carbon::parse($this->model->next_date), false);
|
|
if ($daysUntil >= 0 && $daysUntil < self::LOCK_DAYS_PAUSE_CANCEL) {
|
|
\Session()->flash('alert-error', __('abo.error_cancel_locked', ['days' => $daysUntil]));
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
// 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;
|
|
// Sperre: 3 Tage vor Ausführung kann nicht mehr pausiert werden
|
|
if (! $isAdminUpdate && $this->model->active && ! $active && $this->model->next_date) {
|
|
$daysUntil = (int) now()->diffInDays(\Carbon\Carbon::parse($this->model->next_date), false);
|
|
if ($daysUntil >= 0 && $daysUntil < self::LOCK_DAYS_PAUSE_CANCEL) {
|
|
\Session()->flash('alert-error', __('abo.error_pause_locked', ['days' => $daysUntil]));
|
|
|
|
return;
|
|
}
|
|
}
|
|
// 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 (! isset($data['abo_interval']) || ! in_array((int) $data['abo_interval'], \App\Models\UserAbo::$aboDeliveryDays, true)) {
|
|
\Session()->flash('alert-error', __('abo.error_abo_interval'));
|
|
|
|
return false;
|
|
}
|
|
|
|
if ($this->isAdminUpdate($data)) {
|
|
if (! isset($data['abo_next_month']) || ! in_array($data['abo_next_month'], $this->getAdminExecutionMonths(), true)) {
|
|
\Session()->flash('alert-error', __('abo.error_next_date'));
|
|
|
|
return false;
|
|
}
|
|
|
|
if ($this->calculateAdminNextDate($data)->startOfDay()->lt(now()->startOfDay())) {
|
|
\Session()->flash('alert-error', __('abo.error_next_date'));
|
|
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// Sperre: 10 Tage vor nächster Ausführung keine Änderungen mehr (Pakete werden vorgepackt)
|
|
if ($this->model->next_date) {
|
|
$daysUntilExecution = (int) now()->diffInDays(\Carbon\Carbon::parse($this->model->next_date), false);
|
|
if ($daysUntilExecution >= 0 && $daysUntilExecution < self::LOCK_DAYS_CHANGE) {
|
|
\Session()->flash('alert-error', __('abo.error_change_locked', ['days' => $daysUntilExecution]));
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Prüfung: Das neue berechnete Ausführungsdatum muss mindestens LOCK_DAYS_CHANGE Tage entfernt sein.
|
|
// Falls next_date bereits in einem zukünftigen Monat liegt, wird das neue Datum in diesem Monat berechnet.
|
|
$newNextDate = $this->calculateNewNextDate($data['abo_interval']);
|
|
$daysUntilNewDate = (int) now()->diffInDays($newNextDate, false);
|
|
if ($daysUntilNewDate < self::LOCK_DAYS_CHANGE) {
|
|
\Session()->flash('alert-error', __('abo.error_abo_interval_too_soon', ['days' => $daysUntilNewDate]));
|
|
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Berechnet das neue Ausführungsdatum unter Berücksichtigung des aktuellen next_date.
|
|
* Falls next_date bereits in einem zukünftigen Monat liegt, wird der Monatsanfang
|
|
* dieses Monats als Referenz verwendet, sodass der neue Tag im selben Monat landet.
|
|
*/
|
|
private function calculateNewNextDate(int $aboInterval): \Carbon\Carbon
|
|
{
|
|
$referenceDate = now();
|
|
|
|
if ($this->model->next_date) {
|
|
$currentNextDate = \Carbon\Carbon::parse($this->model->next_date);
|
|
|
|
if ($currentNextDate->format('Y-m') > now()->format('Y-m')) {
|
|
$referenceDate = $currentNextDate->startOfMonth();
|
|
}
|
|
}
|
|
|
|
return AboHelper::setNextDate($referenceDate, $aboInterval);
|
|
}
|
|
|
|
private function calculateAdminNextDate(array $data): Carbon
|
|
{
|
|
return Carbon::createFromFormat('Y-m-d', $data['abo_next_month'].'-01')
|
|
->startOfMonth()
|
|
->addDays(((int) $data['abo_interval']) - 1);
|
|
}
|
|
|
|
/**
|
|
* @return array<int, string>
|
|
*/
|
|
private function getAdminExecutionMonths(): array
|
|
{
|
|
return collect(range(0, 3))
|
|
->map(fn (int $offset): string => now()->copy()->startOfMonth()->addMonths($offset)->format('Y-m'))
|
|
->all();
|
|
}
|
|
|
|
private function isAdminUpdate(array $data): bool
|
|
{
|
|
return ($data['view'] ?? null) === 'admin';
|
|
}
|
|
}
|