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

316 lines
9.1 KiB
PHP

<?php
/**
* Validiert IncentiveTracker::trackSalesVolume für Umsatz aus einer weiteren Abo-Bestellung
* (gleicher Kunde/shopping_user_id wie beim Neuabo). Rechnung und UserSalesVolume entstehen
* über Payment::paymentStatusPaidAction / InvoiceRepository::createAndSalesVolume (Payone-API),
* nicht über user:make_abo_order.
*/
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\IncentiveTracker;
use App\User;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
Carbon::setTestNow(Carbon::parse('2026-05-15 10:00:00'));
});
afterEach(function () {
Carbon::setTestNow();
});
it('addiert Umsatzpunkte beim Berater wenn UserSalesVolume zur Abo-Verlängerung gehört (trackSalesVolume Pfad Neuabo)', 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',
]);
$shoppingUser = ShoppingUser::create([
'auth_user_id' => $customer->id,
'member_id' => $consultant->id,
'billing_country_id' => $country->id,
'shipping_country_id' => $country->id,
'billing_email' => 'cust@example.com',
]);
$userAbo = UserAbo::create([
'user_id' => null,
'member_id' => $consultant->id,
'shopping_user_id' => $shoppingUser->id,
'is_for' => 'ot',
'email' => 'abo@example.com',
'payone_userid' => 999001,
'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,
]);
IncentiveNewAbo::create([
'participant_id' => $participant->id,
'user_abo_id' => $userAbo->id,
'activated_at' => Carbon::now()->subMonth(),
]);
$shoppingOrder = ShoppingOrder::create([
'shopping_user_id' => $shoppingUser->id,
'auth_user_id' => $customer->id,
'country_id' => $shippingCountry->id,
'user_shop_id' => $userShop->id,
'payment_for' => 6,
'points' => 75,
'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' => 75,
'total_net' => 90,
'status_points' => 1,
'status' => 2,
'message' => 'Abo Verlängerung Test',
]);
IncentiveTracker::trackSalesVolume($userSalesVolume);
$participant->refresh();
expect(
IncentivePointsLog::where('participant_id', $participant->id)
->where('type', 'abo')
->where('user_sales_volume_id', $userSalesVolume->id)
->where('points_accumulated', 75)
->exists()
)->toBeTrue()
->and($participant->total_points)->toBeGreaterThan(0);
});
it('addiert Umsatzpunkte bei Verlaengerung auch wenn shopping_order einen replizierten ShoppingUser hat', function () {
$country = Country::create([
'code' => 'CH',
'phone' => '41',
'en' => 'Switzerland',
'de' => 'Schweiz',
'es' => 'Suiza',
'fr' => 'Suisse',
'it' => 'Svizzera',
'ru' => 'Швейцария',
]);
$shipping = Shipping::create([
'name' => 'Std-CH',
'active' => true,
]);
$shippingCountry = ShippingCountry::create([
'shipping_id' => $shipping->id,
'country_id' => $country->id,
]);
$shopOwner = User::forceCreate([
'email' => 'so-ch-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$userShop = UserShop::create([
'user_id' => $shopOwner->id,
'name' => 'TSCH'.substr(uniqid('', true), 0, 6),
'slug' => 'tsch-'.uniqid(),
'active' => true,
]);
$consultant = User::forceCreate([
'email' => 'consultant-ch-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$customer = User::forceCreate([
'email' => 'customer-ch-'.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-ch-'.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' => 999101,
'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,
]);
IncentiveNewAbo::create([
'participant_id' => $participant->id,
'user_abo_id' => $userAbo->id,
'activated_at' => Carbon::now()->subMonth(),
]);
$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' => 40,
'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,
]);
expect($shoppingOrder->shopping_user_id)->not->toBe($userAbo->shopping_user_id);
$userSalesVolume = UserSalesVolume::create([
'user_id' => $customer->id,
'shopping_order_id' => $shoppingOrder->id,
'month' => 5,
'year' => 2026,
'date' => '15.05.2026',
'points' => 40,
'total_net' => 90,
'status_points' => 1,
'status' => 2,
'message' => 'Abo Verlängerung Replikat',
]);
IncentiveTracker::trackSalesVolume($userSalesVolume);
$participant->refresh();
expect(
IncentivePointsLog::where('participant_id', $participant->id)
->where('type', 'abo')
->where('user_sales_volume_id', $userSalesVolume->id)
->where('points_accumulated', 40)
->exists()
)->toBeTrue();
});