mivita/tests/Feature/Incentive/IncentiveParticipantDetailDataAboTest.php
2026-04-10 17:15:27 +02:00

188 lines
5.5 KiB
PHP

<?php
/**
* Detailansicht: Akkumulierte Abo-Punkte muessen dem richtigen Neuabo zugeordnet werden,
* auch wenn die Verlaengerungsbestellung einen anderen shopping_user_id hat (Replikat).
*/
use App\Http\Controllers\Admin\IncentiveController;
use App\Models\Country;
use App\Models\Incentive;
use App\Models\IncentiveNewAbo;
use App\Models\IncentiveParticipant;
use App\Models\IncentivePointsLog;
use App\Models\Shipping;
use App\Models\ShippingCountry;
use App\Models\ShoppingOrder;
use App\Models\ShoppingUser;
use App\Models\UserAbo;
use App\Models\UserAboOrder;
use App\Models\UserSalesVolume;
use App\Models\UserShop;
use App\User;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
Carbon::setTestNow(Carbon::parse('2026-05-15 12:00:00'));
});
afterEach(function () {
Carbon::setTestNow();
});
it('ordnet akkumulierte Abo-Punkte dem Neuabo ueber user_abo_id zu (Replikat-ShoppingUser)', function () {
$country = Country::create([
'code' => 'DE',
'phone' => '49',
'en' => 'Germany',
'de' => 'Deutschland',
'es' => 'Alemania',
'fr' => 'Allemagne',
'it' => 'Germania',
'ru' => 'Германия',
]);
$shipping = Shipping::create([
'name' => 'Standard',
'active' => true,
]);
$shippingCountry = ShippingCountry::create([
'shipping_id' => $shipping->id,
'country_id' => $country->id,
]);
$shopOwner = User::forceCreate([
'email' => 'shop-owner-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$userShop = UserShop::create([
'user_id' => $shopOwner->id,
'name' => 'TS'.substr(uniqid('', true), 0, 8),
'slug' => 'ts-'.uniqid(),
'active' => true,
]);
$consultant = User::forceCreate([
'email' => 'consultant-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$customer = User::forceCreate([
'email' => 'customer-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$shoppingUserStamm = ShoppingUser::create([
'auth_user_id' => $customer->id,
'member_id' => $consultant->id,
'billing_country_id' => $country->id,
'shipping_country_id' => $country->id,
'billing_email' => 'cust-detail-'.uniqid('', true).'@example.com',
]);
$shoppingUserReplica = ShoppingUser::create([
'auth_user_id' => $customer->id,
'member_id' => $consultant->id,
'billing_country_id' => $country->id,
'shipping_country_id' => $country->id,
'billing_email' => $shoppingUserStamm->billing_email,
]);
$userAbo = UserAbo::create([
'user_id' => null,
'member_id' => $consultant->id,
'shopping_user_id' => $shoppingUserStamm->id,
'is_for' => 'ot',
'email' => $shoppingUserStamm->billing_email,
'payone_userid' => 999201,
'clearingtype' => 'cc',
'active' => true,
'status' => 2,
'abo_interval' => 1,
]);
$incentive = Incentive::factory()->create([
'qualification_start' => '2026-04-01',
'qualification_end' => '2026-07-31',
'calculation_end' => '2026-08-31',
'status' => 1,
'points_abo_onetime' => 100,
]);
$participant = IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $consultant->id,
]);
$newAbo = IncentiveNewAbo::create([
'participant_id' => $participant->id,
'user_abo_id' => $userAbo->id,
'activated_at' => Carbon::parse('2026-04-10'),
]);
$shoppingOrder = ShoppingOrder::create([
'shopping_user_id' => $shoppingUserReplica->id,
'auth_user_id' => $customer->id,
'member_id' => $consultant->id,
'country_id' => $shippingCountry->id,
'user_shop_id' => $userShop->id,
'payment_for' => 6,
'points' => 42,
'is_abo' => true,
'paid' => true,
'txaction' => 'paid',
'mode' => 'test',
'total' => 100,
'subtotal' => 90,
]);
UserAboOrder::create([
'user_abo_id' => $userAbo->id,
'shopping_order_id' => $shoppingOrder->id,
'status' => 2,
'paid' => true,
]);
$userSalesVolume = UserSalesVolume::create([
'user_id' => $customer->id,
'shopping_order_id' => $shoppingOrder->id,
'month' => 5,
'year' => 2026,
'date' => '15.05.2026',
'points' => 42,
'total_net' => 90,
'status_points' => 1,
'status' => 2,
'message' => 'Verlängerung',
]);
IncentivePointsLog::create([
'participant_id' => $participant->id,
'type' => 'abo',
'source_type' => UserSalesVolume::class,
'source_id' => $userSalesVolume->id,
'source_label' => 'SV Test',
'month' => 5,
'year' => 2026,
'points_onetime' => 0,
'points_accumulated' => 42,
'user_sales_volume_id' => $userSalesVolume->id,
'incentive_new_abo_id' => $newAbo->id,
'is_storno' => false,
]);
$data = IncentiveController::buildParticipantDetailData($participant->fresh(['incentive', 'user']));
$aboRow = $data['abo_sources']->first();
expect($aboRow)->not->toBeNull()
->and($aboRow['id'])->toBe($newAbo->id)
->and(array_sum($aboRow['monthly']))->toBe(42);
});