- E-Mail-Aenderung durch Berater wird erst nach Bestaetigung durch den Kunden aktiv (Link an die neue Adresse, Portal kunde/email-bestaetigen); danach ziehen alle Eintraege des Kunden und seine Abos mit. Admins aendern direkt. - Kundenhoheit: E-Mail eines Beraterkontos oder eines Kunden eines anderen Beraters wird abgelehnt (beim Speichern und bei der Bestaetigung). - Fix CustomerPriority::entryExists: aelteste gepruefte Zuordnung gilt, Eintraege in Pruefung (is_like) dienen nicht mehr als Vorlage. - "Berater zuweisen" voreingestellt auf alle Bestellungen des Kunden. - Neue Admin-Seite Kundenhoheit: offene Pruefungen, Kunden bei mehreren Beratern, abgelehnte/offene E-Mail-Aenderungen; Zaehler im Menue. - Neue Tabelle shopping_user_email_changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
308 lines
13 KiB
PHP
308 lines
13 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Exceptions\GdprDeletionException;
|
|
use App\Models\ShoppingUser;
|
|
use App\Repositories\CustomerRepository;
|
|
use App\Services\CustomerEmailChangeService;
|
|
use App\Services\CustomerPriority;
|
|
use App\Services\GdprDeletionService;
|
|
use App\Services\HTMLHelper;
|
|
use App\Services\Shop;
|
|
use Auth;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Request;
|
|
use Validator;
|
|
|
|
class CustomerController extends Controller
|
|
{
|
|
protected $customerRepository;
|
|
|
|
public function __construct(CustomerRepository $customerRepository)
|
|
{
|
|
$this->middleware('admin');
|
|
$this->middleware('superadmin')->only('gdprDelete');
|
|
$this->customerRepository = $customerRepository;
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
if (Request::get('reset') === 'filter') {
|
|
set_user_attr('filter_member_id', null);
|
|
set_user_attr('filter_customer_member', null);
|
|
set_user_attr('filter_gdpr_deleted', null);
|
|
|
|
return redirect(route('admin_customers'));
|
|
}
|
|
$filter_members = ShoppingUser::join('users', 'member_id', '=', 'users.id')->groupBy('member_id')->join('user_accounts', 'account_id', '=', 'user_accounts.id')->select('users.id', 'users.email', 'user_accounts.first_name', 'user_accounts.last_name')->get(); // ->pluck('email', 'id')->unique()->toArray();
|
|
$data = [
|
|
'filter_members' => $filter_members,
|
|
];
|
|
|
|
return view('admin.customer.index', $data);
|
|
}
|
|
|
|
public function detail($id)
|
|
{
|
|
$shopping_user = ShoppingUser::findOrFail($id);
|
|
$data = [
|
|
'shopping_user' => $shopping_user,
|
|
'deletion' => app(GdprDeletionService::class)->analyzeShoppingUser($shopping_user),
|
|
'isAdmin' => true,
|
|
'isView' => 'customer',
|
|
];
|
|
|
|
return view('admin.customer.detail', $data);
|
|
}
|
|
|
|
/**
|
|
* Komplett-Löschung (nur ohne Bestellungen), dev/_tickets/2026-09-08.md #2.
|
|
*/
|
|
public function delete($id)
|
|
{
|
|
$shopping_user = ShoppingUser::withTrashed()->findOrFail($id);
|
|
|
|
if (! Request::get('delete_customer')) {
|
|
\Session()->flash('alert-error', __('msg.delete_confirm_checkbox_missing'));
|
|
|
|
return redirect(route('admin_customer_detail', [$shopping_user->id]));
|
|
}
|
|
|
|
try {
|
|
$summary = app(GdprDeletionService::class)->purgeShoppingUser($shopping_user, Auth::id());
|
|
\Session()->flash('alert-success', __('msg.gdpr_purge_customer_success', ['count' => $summary['shopping_users_purged']]));
|
|
} catch (GdprDeletionException $e) {
|
|
\Session()->flash('alert-error', $e->getMessage());
|
|
|
|
return redirect(route('admin_customer_detail', [$shopping_user->id]));
|
|
}
|
|
|
|
return redirect(route('admin_customers'));
|
|
}
|
|
|
|
public function gdprDelete($id)
|
|
{
|
|
$shopping_user = ShoppingUser::withTrashed()->findOrFail($id);
|
|
|
|
if (! Auth::user()->isSuperAdmin()) {
|
|
abort(403);
|
|
}
|
|
|
|
if (! Request::get('gdpr_delete_customer')) {
|
|
\Session()->flash('alert-error', __('msg.gdpr_confirm_checkbox_missing'));
|
|
|
|
return redirect(route('admin_customer_detail', [$shopping_user->id]));
|
|
}
|
|
|
|
try {
|
|
$summary = app(GdprDeletionService::class)->deleteShoppingUser($shopping_user, Auth::id());
|
|
\Session()->flash('alert-success', __('msg.gdpr_delete_customer_success', ['count' => $summary['shopping_users_anonymized']]));
|
|
} catch (GdprDeletionException $e) {
|
|
\Session()->flash('alert-error', $e->getMessage());
|
|
|
|
return redirect(route('admin_customer_detail', [$shopping_user->id]));
|
|
}
|
|
|
|
return redirect(route('admin_customers'));
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
if ($id === 'new') {
|
|
$shopping_user = new ShoppingUser;
|
|
$shopping_user->id = 'new';
|
|
} else {
|
|
$shopping_user = ShoppingUser::findOrFail($id);
|
|
if ($shopping_user->isGdprDeleted()) {
|
|
return $this->redirectGdprLocked($shopping_user);
|
|
}
|
|
}
|
|
$data = [
|
|
'shopping_user' => $shopping_user,
|
|
'isAdmin' => true,
|
|
'isView' => 'customer',
|
|
];
|
|
|
|
return view('admin.customer.edit', $data);
|
|
}
|
|
|
|
public function store($id)
|
|
{
|
|
$data = Request::all();
|
|
|
|
if ($data['action'] === 'shopping-user-change-member') {
|
|
if (! isset($data['change_member_key']) || $data['change_member_key'] !== config('mivita.edit_data_pass')) {
|
|
$data = [
|
|
'change_member_error' => 'Das Passwort ist falsch.',
|
|
'shopping_user' => ShoppingUser::find($id),
|
|
'isAdmin' => true,
|
|
'isView' => 'customer',
|
|
];
|
|
|
|
return view('admin.customer.detail', $data);
|
|
}
|
|
// change
|
|
$shopping_user = ShoppingUser::findOrFail($data['id']);
|
|
CustomerPriority::newMemberForCustomer($shopping_user, $data['change_member_id'], $data['customer_set_member_for']);
|
|
\Session()->flash('alert-save', true);
|
|
|
|
// Aus der Übersicht Kundenhoheit aufgerufen: dorthin zurück.
|
|
$back = $data['back'] ?? null;
|
|
if ($back && str_starts_with($back, route('admin_customer_priority'))) {
|
|
return redirect($back);
|
|
}
|
|
|
|
return redirect(route('admin_customer_detail', [$shopping_user->id]));
|
|
}
|
|
if ($data['action'] === 'shopping-user-store') {
|
|
$rules = [
|
|
'billing_salutation' => 'required',
|
|
'billing_firstname' => 'required',
|
|
'billing_lastname' => 'required',
|
|
'billing_email' => 'required|email:filter',
|
|
'billing_address' => 'required',
|
|
'billing_zipcode' => 'required',
|
|
'billing_city' => 'required',
|
|
'billing_country_id' => 'required',
|
|
];
|
|
|
|
if (! Request::get('same_as_billing')) {
|
|
$rules = array_merge($rules, [
|
|
'shipping_firstname' => 'required',
|
|
'shipping_lastname' => 'required',
|
|
'shipping_address' => 'required',
|
|
'shipping_zipcode' => 'required',
|
|
'shipping_city' => 'required',
|
|
'shipping_salutation' => 'required',
|
|
'shipping_country_id' => 'required',
|
|
]);
|
|
}
|
|
$validator = Validator::make(Request::all(), $rules);
|
|
if ($validator->fails()) {
|
|
return back()->withErrors($validator)->withInput(Request::all());
|
|
}
|
|
|
|
$shopping_user = ShoppingUser::findOrFail($id);
|
|
if ($shopping_user->isGdprDeleted()) {
|
|
return $this->redirectGdprLocked($shopping_user);
|
|
}
|
|
|
|
$data['language'] = isset($data['language']) ? $data['language'] : \App::getLocale();
|
|
$data['has_buyed'] = isset($data['has_buyed']) ? true : false;
|
|
$data['subscribed'] = isset($data['subscribed']) ? true : false;
|
|
// subscribed can only true when has_buyed ist active
|
|
$data['subscribed'] = $data['has_buyed'] ? $data['subscribed'] : false;
|
|
|
|
/* if($shopping_user->auth_user_id > 0){
|
|
$data['has_buyed'] = true;
|
|
$data['subscribed'] = false;
|
|
}*/
|
|
$data['same_as_billing'] = isset($data['same_as_billing']) ? true : false;
|
|
$data['shipping_country_id'] = isset($data['shipping_country_id']) ? $data['shipping_country_id'] : $data['billing_country_id'];
|
|
CustomerPriority::checkChangeOne($shopping_user, $data, true);
|
|
|
|
// Admin ändert direkt (Support); alle Einträge des Kunden und seine Abos ziehen mit.
|
|
$emailChangeService = app(CustomerEmailChangeService::class);
|
|
if ($emailChangeService->changesAddress($shopping_user, $data['billing_email'] ?? null)) {
|
|
$emailChangeService->changeDirectly($shopping_user, $data['billing_email'], Auth::id());
|
|
unset($data['billing_email']);
|
|
}
|
|
|
|
$shopping_user->fill($data);
|
|
$shopping_user->save();
|
|
Shop::newUserOrder($shopping_user->number);
|
|
|
|
\Session()->flash('alert-save', true);
|
|
}
|
|
|
|
return redirect(route('admin_customer_detail', [$shopping_user->id]));
|
|
}
|
|
|
|
public function getCustomers()
|
|
{
|
|
$query = ShoppingUser::select('shopping_users.*')->where('auth_user_id', '=', null);
|
|
|
|
set_user_attr('filter_gdpr_deleted', Request::get('filter_gdpr_deleted'));
|
|
if (Request::get('filter_gdpr_deleted') === 'only') {
|
|
$query->whereNotNull('gdpr_deleted_at');
|
|
} elseif (Request::get('filter_gdpr_deleted') !== 'all') {
|
|
$query->withoutGdprDeleted();
|
|
}
|
|
|
|
set_user_attr('filter_member_id', Request::get('filter_member_id'));
|
|
if (Request::get('filter_member_id') != '') {
|
|
$query->where('member_id', '=', Request::get('filter_member_id'));
|
|
}
|
|
|
|
/* set_user_attr('filter_customer_member', Request::get('filter_customer_member'));
|
|
if(Request::get('filter_customer_member') != ""){
|
|
if(Request::get('filter_customer_member') === 'customers'){
|
|
$query->where('auth_user_id', '=', NULL);
|
|
}
|
|
if(Request::get('filter_customer_member') === 'members'){
|
|
$query->where('auth_user_id', '!=', NULL);
|
|
}
|
|
}*/
|
|
return \DataTables::eloquent($query)
|
|
->addColumn('id', function (ShoppingUser $ShoppingUser) {
|
|
return '<a href="'.route('admin_customer_detail', [$ShoppingUser->id]).'" class="btn icon-btn btn-sm btn-primary"><span class="fa fa-edit"></span></a>';
|
|
})
|
|
->addColumn('billing_email', function (ShoppingUser $ShoppingUser) {
|
|
return $ShoppingUser->faker_mail ? '-' : $ShoppingUser->billing_email;
|
|
})
|
|
->addColumn('billing_salutation', function (ShoppingUser $ShoppingUser) {
|
|
return HTMLHelper::getSalutationLang($ShoppingUser->billing_salutation);
|
|
})
|
|
->addColumn('billing_country_id', function (ShoppingUser $ShoppingUser) {
|
|
return $ShoppingUser->billing_country ? $ShoppingUser->billing_country->getLocated() : '';
|
|
})
|
|
->addColumn('isMember', function (ShoppingUser $ShoppingUser) {
|
|
return get_active_badge($ShoppingUser->auth_user_id).($ShoppingUser->mode === 'dev' ? ' <span class="badge badge-warning">dev</span>' : '')
|
|
.($ShoppingUser->isGdprDeleted() ? ' <span class="badge badge-secondary">DSGVO-gelöscht</span>' : '');
|
|
})
|
|
->addColumn('member_id', function (ShoppingUser $ShoppingUser) {
|
|
if ($ShoppingUser->is_like) {
|
|
return '<button type="button" class="btn btn-xs btn-outline-info" data-toggle="modal" data-target="#modals-load-content"
|
|
data-id="'.$ShoppingUser->id.'"
|
|
data-action="shopping-user-is-like-member"
|
|
data-back="'.route('admin_customers').'"
|
|
data-modal="modal-xl"
|
|
data-route="'.route('modal_load').'"><span class="fa fa-edit"></span> Berater zuordnen</button>';
|
|
}
|
|
if ($ShoppingUser->member) {
|
|
return '<a href="'.route('admin_lead_edit', [$ShoppingUser->member_id]).'">'.$ShoppingUser->member->getFullName().'</a>';
|
|
}
|
|
|
|
return '';
|
|
})
|
|
->addColumn('created_at', function (ShoppingUser $ShoppingUser) {
|
|
return $ShoppingUser->created_at->format('d.m.Y');
|
|
})
|
|
->addColumn('subscribed', function (ShoppingUser $ShoppingUser) {
|
|
return get_active_badge($ShoppingUser->subscribed);
|
|
})
|
|
->filterColumn('billing_email', function ($query, $keyword) {
|
|
if ($keyword != '') {
|
|
$query->where('billing_email', 'LIKE', '%'.$keyword.'%');
|
|
}
|
|
})
|
|
->orderColumn('id', 'id $1')
|
|
->orderColumn('billing_country_id', 'billing_country_id $1')
|
|
->orderColumn('billing_salutation', 'billing_salutation $1')
|
|
->orderColumn('billing_email', 'billing_email $1')
|
|
->orderColumn('created_at', 'created_at $1')
|
|
->orderColumn('isMember', 'auth_user_id $1')
|
|
->orderColumn('member_id', 'member_id $1')
|
|
->orderColumn('subscribed', 'subscribed $1')
|
|
->rawColumns(['id', 'subscribed', 'isMember', 'member_id'])
|
|
->make(true);
|
|
}
|
|
|
|
protected function redirectGdprLocked(ShoppingUser $shopping_user): RedirectResponse
|
|
{
|
|
\Session()->flash('alert-error', __('msg.gdpr_customer_locked'));
|
|
|
|
return redirect(route('admin_customer_detail', [$shopping_user->id]));
|
|
}
|
|
}
|