mivita/app/Services/UserService.php
Kevin ac3bd6e8ea Statistiken/Abo: Kundenfeedback 2026-07-24 komplett umgesetzt + Steuern: DATEV-Fixes 08+09/2025 (Sammel-Commit Branch-WIP)
Feedback-Umsetzung (dev/2026-07-22/feedback-analyse-2026-07-24.md, alle 10 Punkte):
- VIP-Statistik: eigener Berater ('ich', Linie 0) in Neue-Kundenabos- und beiden Top-10-Listen; Top-10-Umsatz sortiert nach abgerechneten Ist-Punkten + Abo-Anzahl-Spalte; Fußnote nicht-payline-wirksame Punkte (SalesVolumePoints::nonPaylinePoints)
- Führungskräfte-Liste: beide Sichten 'direkt' (an nächster FK abgeschnitten, additiv) und 'gesamt' (komplette Downline), Gesamt-Spalte nach Kundenzählweise
- Team-Abos: je Ebene 'abgerechnet (Monat)' und 'bis zum 25. erwartet'; Payline-Karte mit Teamabo-/Kundenabo-Zählern
- Incentive-Live-Ranking: Qualifizierte zuerst nummeriert (fixt auch isWinner rank<=max_winners)
- Porto: Netto-/Brutto-Kennzeichnung (Berater netto, Endkunden inkl. MwSt., portal_view-Flag; auch CRM-/Shop-Warenkorb + Checkout), Yard::shipping_tax-Fix
- Struktur-Ansicht: Abo-Badges 'A' (eigenes Abo) und '(n)' (Kundenabos) je Teampartner, gebatcht
- Abo-Löschung: Softdelete setzt active=0 (UserAbo deleting-Hook) + Bereinigungs-Migration für gelöschte-aber-aktive Abos
- Doku: technische Analyse + Kundinnen-Feedback-Dokument unter dev/2026-07-22/
- Tests: IncentiveRankingTest, TeamStructureAboBadgesTest, YardShippingTaxTest (neu); BackofficeStatisticsListsTest, TeamAboOverviewTest, PaylineForecastServiceTest, AboAdminDeleteTest, AboOneTimeViewTest erweitert

Außerdem enthalten (Branch-WIP vor dem Feedback): DATEV-Abgleich-Fixes 08+09/2025, USt-ID-Live-Validierung sowie restliche unkommittete Stände (u. a. Abo-Pause-Sprachkeys).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-24 13:56:02 +00:00

240 lines
7.9 KiB
PHP

<?php
namespace App\Services;
use App\Models\ShippingCountry;
use App\User;
use Illuminate\Support\Str;
use RuntimeException;
use Yard;
class UserService
{
public static $user_country;
public static $shipping_country;
public static $shipping_free = false;
public static $user_tax_free = false;
public static $user_reverse_charge = false;
public static $instance = 'shopping';
public static function getTransChange()
{
$langs = config('localization.supportedLocales');
$ret = [];
foreach ($langs as $code => $lang) {
$ret[strtolower($code)] = strtolower($lang['native']);
}
return $ret;
}
public static function setInstance($instance)
{
self::$instance = $instance;
}
// init Yard for user order Customer
public static function initCustomerYard($shopping_user, $for)
{
self::$user_tax_free = false;
if ($shopping_user->same_as_billing) {
self::$user_country = $shopping_user->billing_country;
self::$shipping_country = $shopping_user->billing_country;
} else {
self::$user_country = $shopping_user->billing_country;
self::$shipping_country = $shopping_user->shipping_country;
}
if (self::$user_country->supply_country && self::$shipping_country->supply_country) {
self::$user_tax_free = true;
}
$shippingCountry = ShippingCountry::whereCountryId(self::$shipping_country->id)->first();
if (! $shippingCountry) {
$shippingCountry = ShippingCountry::query()
->whereHas('shipping', fn ($q) => $q->where('active', true))
->orderBy('id')
->first();
}
if (! $shippingCountry) {
$shippingCountry = ShippingCountry::query()->orderBy('id')->first();
}
if (! $shippingCountry) {
throw new RuntimeException('Kein Eintrag in shipping_countries (Tabelle leer oder nicht migriert).');
}
self::$shipping_free = $shippingCountry->shipping?->free ?? false;
self::$shipping_free = self::$shipping_free !== null ? self::$shipping_free : false;
Yard::instance(self::$instance)->setShippingCountryWithPrice($shippingCountry->id, $for);
Yard::instance(self::$instance)->setUserPriceInfos(self::getYardInfo());
}
// init Yard for user order Berater
public static function initUserYard(User $user, $shipping_country_id, $for)
{
self::$shipping_free = false;
self::checkUserTaxShippingCountry($user, $shipping_country_id);
Yard::instance(self::$instance)->setShippingCountryWithPrice($shipping_country_id, $for);
Yard::instance(self::$instance)->setUserPriceInfos(self::getYardInfo());
}
public static function checkUserTaxShippingCountry(User $user, $shipping_country_id)
{
if (! $user->account || ! $user->account->country_id) {
abort(403, 'Error: User hat kein Land!');
}
$ShippingCountry = ShippingCountry::findOrFail($shipping_country_id);
self::$user_tax_free = self::performUserTaxShippingCountry($user, $ShippingCountry);
return $ShippingCountry;
/*
dump( self::$user_price_code );
dump( self::$user_tax_free );
dump( self::$user_reverse_charge );
*/
}
public static function performUserTaxShippingCountry($user, $ShippingCountry)
{
// preise für das Land
self::$user_country = $user->account->country;
self::$shipping_country = $ShippingCountry->country;
// ausgehend vom Land des Rechnungsempfänger $user->account->country
// ist der Rechnungsempfänger im Drittland?
if ($user->account->country->supply_country) {
if ($ShippingCountry->country->supply_country) {
// Lieferadresse im Drittland?
return true;
}
}
// Rechnungsempfänger in der EU
// Lieferland mit RSV
if ($ShippingCountry->country->eu_country) {
// Rechnungsempfänger mit valid aktiv RSV
if ($user->account->reverse_charge && $user->account->reverse_charge_valid) {
// Rechnungsland ist auch Lieferland, dann RSV
if (strtolower($user->account->reverse_charge_code) == strtolower($ShippingCountry->country->code)) {
// USt-ID bei jeder Verwendung erneut prüfen (VIES, gecacht)
if (! self::verifyReverseChargeVatId($user)) {
return false;
}
self::$user_reverse_charge = true;
return true;
}
}
}
// Lieferland ohne RSV
return false;
}
/**
* Prüft die hinterlegte USt-ID bei jeder Verwendung erneut.
* Deutsche, formal ungültige oder laut VIES ungültige USt-IDs verlieren den
* Reverse-Charge-Status; bei VIES-Ausfall gilt der gespeicherte Status weiter.
*/
public static function verifyReverseChargeVatId(User $user): bool
{
$vatService = app(VatIdService::class);
$vatId = $user->account->tax_identification_number;
if (! $vatService->qualifiesForReverseCharge($vatId)) {
self::revokeReverseCharge($user);
return false;
}
$result = $vatService->checkViesCached($vatId, $user->account->country->code ?? null);
if ($result === VatIdService::CHECK_INVALID) {
self::revokeReverseCharge($user);
return false;
}
// valid oder VIES nicht erreichbar (Fallback auf gespeicherten Status)
return true;
}
/**
* Entzieht den Reverse-Charge-Status und informiert den User im Checkout,
* dass die Bestellung mit Umsatzsteuer berechnet wird.
*/
private static function revokeReverseCharge(User $user): void
{
$user->account->reverse_charge = 0;
$user->account->reverse_charge_valid = null;
$user->account->save();
if (app()->bound('request') && request()->hasSession()) {
request()->session()->flash('alert-error', __('msg.VATID_no_longer_valid_taxes_applied'));
}
}
public static function getYardInfo()
{
return [
'shipping_free' => self::$shipping_free,
'user_tax_free' => self::$user_tax_free,
'user_reverse_charge' => self::$user_reverse_charge,
'user_country_id' => self::$user_country->id,
'shipping_country_id' => self::$shipping_country->id,
];
}
public static function getTaxFree()
{
return self::$user_tax_free ? true : false;
}
public static function getUserPriceInfos()
{
return [
'user_tax_free' => self::$user_tax_free,
'user_reverse_charge' => self::$user_reverse_charge,
'user_country_id' => self::$user_country->id,
];
}
public static function getOrderInfo($key = false)
{
if (! self::$user_country) {
return '';
}
switch ($key) {
case 'billing_state':
return self::$user_country->getLocated();
break;
case 'shipping_state':
return self::$shipping_country->getLocated();
break;
case 'tax_free':
return self::$user_tax_free ? __('no') : __('yes');
break;
case 'user_reverse_charge':
return self::$user_reverse_charge ? __('yes') : __('no');
break;
}
}
public static function createConfirmationCode()
{
$unique = false;
do {
$confirmation_code = Str::random(30);
if (User::where('confirmation_code', '=', $confirmation_code)->count() == 0) {
$unique = true;
}
} while (! $unique);
return $confirmation_code;
}
}