mivita/tests/Unit/Services/Incentive/IncentivePointsLogRepairServiceTest.php
2026-04-10 17:15:27 +02:00

233 lines
7 KiB
PHP

<?php
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\Services\Incentive\IncentivePointsLogRepairService;
use App\User;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
uses(TestCase::class, RefreshDatabase::class);
it('setzt fehlende incentive_new_abo_id an einem Log per Bestellung', function () {
Carbon::setTestNow(Carbon::parse('2026-05-15 10:00:00'));
$country = Country::create([
'code' => 'DE',
'phone' => '49',
'en' => 'Germany',
'de' => 'Deutschland',
'es' => 'Alemania',
'fr' => 'Allemagne',
'it' => 'Germania',
'ru' => 'Германия',
]);
$shipping = Shipping::create(['name' => 'S', 'active' => true]);
$shippingCountry = ShippingCountry::create(['shipping_id' => $shipping->id, 'country_id' => $country->id]);
$shopOwner = User::forceCreate([
'email' => 'so-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$userShop = UserShop::create([
'user_id' => $shopOwner->id,
'name' => 'T',
'slug' => 't-'.uniqid(),
'active' => true,
]);
$consultant = User::forceCreate([
'email' => 'co-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$customer = User::forceCreate([
'email' => 'cu-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$shoppingUser = ShoppingUser::create([
'auth_user_id' => $customer->id,
'member_id' => $consultant->id,
'billing_country_id' => $country->id,
'shipping_country_id' => $country->id,
'billing_email' => 'e-'.uniqid('', true).'@example.com',
]);
$userAbo = UserAbo::create([
'member_id' => $consultant->id,
'shopping_user_id' => $shoppingUser->id,
'is_for' => 'ot',
'email' => $shoppingUser->billing_email,
'payone_userid' => 1,
'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,
]);
$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::now()->subMonth(),
]);
$order = ShoppingOrder::create([
'shopping_user_id' => $shoppingUser->id,
'auth_user_id' => $customer->id,
'member_id' => $consultant->id,
'country_id' => $shippingCountry->id,
'user_shop_id' => $userShop->id,
'payment_for' => 6,
'points' => 10,
'is_abo' => true,
'paid' => true,
'txaction' => 'paid',
'mode' => 'test',
'total' => 100,
'subtotal' => 90,
]);
UserAboOrder::create([
'user_abo_id' => $userAbo->id,
'shopping_order_id' => $order->id,
'status' => 2,
'paid' => true,
]);
$sv = UserSalesVolume::create([
'user_id' => $customer->id,
'shopping_order_id' => $order->id,
'month' => 5,
'year' => 2026,
'date' => '15.05.2026',
'points' => 10,
'total_net' => 90,
'status_points' => 1,
'status' => 2,
'message' => 'x',
]);
IncentivePointsLog::create([
'participant_id' => $participant->id,
'type' => 'abo',
'source_type' => UserSalesVolume::class,
'source_id' => $sv->id,
'source_label' => 'alt',
'month' => 5,
'year' => 2026,
'points_onetime' => 0,
'points_accumulated' => 10,
'user_sales_volume_id' => $sv->id,
'incentive_new_abo_id' => null,
'is_storno' => false,
]);
$service = new IncentivePointsLogRepairService;
$r = $service->repairForeignKeys($participant->fresh());
expect($r['abo_fk'])->toBe(1)
->and(IncentivePointsLog::first()->incentive_new_abo_id)->toBe($newAbo->id);
Carbon::setTestNow();
});
it('legt fehlendes Abo-Tracking ohne UserAboOrder manuell an', function () {
Carbon::setTestNow(Carbon::parse('2026-05-10 10:00:00'));
$country = Country::create([
'code' => 'AT',
'phone' => '43',
'en' => 'Austria',
'de' => 'Österreich',
'es' => 'Austria',
'fr' => 'Autriche',
'it' => 'Austria',
'ru' => 'Австрия',
]);
$shipping = Shipping::create(['name' => 'S2', 'active' => true]);
ShippingCountry::create(['shipping_id' => $shipping->id, 'country_id' => $country->id]);
$consultant = User::forceCreate([
'email' => 'co2-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$customer = User::forceCreate([
'email' => 'cu2-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$shoppingUser = ShoppingUser::create([
'auth_user_id' => $customer->id,
'member_id' => $consultant->id,
'billing_country_id' => $country->id,
'shipping_country_id' => $country->id,
'billing_email' => 'e2-'.uniqid('', true).'@example.com',
]);
$userAbo = UserAbo::create([
'member_id' => $consultant->id,
'shopping_user_id' => $shoppingUser->id,
'is_for' => 'ot',
'email' => $shoppingUser->billing_email,
'payone_userid' => 2,
'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' => 400,
]);
$participant = IncentiveParticipant::factory()->create([
'incentive_id' => $incentive->id,
'user_id' => $consultant->id,
]);
$service = new IncentivePointsLogRepairService;
$added = $service->syncMissingTrackingAbos($participant->fresh());
expect($added)->toBe(1)
->and(IncentiveNewAbo::where('participant_id', $participant->id)->where('user_abo_id', $userAbo->id)->exists())->toBeTrue()
->and(IncentivePointsLog::where('participant_id', $participant->id)->where('type', 'abo')->where('source_type', UserAbo::class)->exists())->toBeTrue();
Carbon::setTestNow();
});