Initial commit

This commit is contained in:
Developer
2025-04-21 16:03:20 +02:00
commit 2832896157
22874 changed files with 3092801 additions and 0 deletions

View File

@@ -0,0 +1,92 @@
<?php
namespace App\Console\Commands;
use DB;
use Carbon\Carbon;
use Illuminate\Console\Command;
use App\Domain\Points\Models\Point;
use App\Domain\Points\Enums\DirectionEnum;
use App\Domain\Subscriptions\Models\Package;
use App\Domain\Subscriptions\Models\Subscription;
use App\Domain\Subscriptions\Service\SubscriptionService;
class AutoSubscribeApp extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'teaser:auto-subs-app';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Autosubscription to the site';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$plan = Package::findOrFail(1);
$price = $plan->price;
$lists = DB::table('subscriptions')
->join('users', 'subscriptions.user_id', '=', 'users.id')
->selectRaw('subscriptions.user_id, MAX(subscriptions.ends_at) as last_time, users.autosubscription_site')
->groupBy('subscriptions.user_id', 'users.autosubscription_site')
->havingRaw('MAX(subscriptions.ends_at) <= ?', [now()])
->having('users.autosubscription_site', true)
->get();
foreach($lists as $list){
$balance = SubscriptionService::calculate($list->user_id);
$ends_at = Carbon::now()->addMonths();
if ($price > $balance) {
echo 'Недостаточно средств ' . $list->user_id . PHP_EOL;
DB::table('users')
->where('id', $list->user_id)
->update(['autosubscription_site' => false]);
continue;
}
$sub = new Subscription;
$sub->user_id = $list->user_id;
$sub->package_id = $plan->id;
$sub->price = $price;
$sub->ends_at = $ends_at;
$sub->status = 'complete'; //YSV ENUM!
$sub->save();
$point = new Point;
$point->user_id =$list->user_id;
$point->point = $price;
$point->type = 'Оплата за подписку'; //YSV ENUM!
$point->direction = DirectionEnum::EXPENSE();
$point->save();
}
return 0;
}
}

View File

@@ -0,0 +1,182 @@
<?php
namespace App\Console\Commands;
use DB;
use App\Models\User;
use Illuminate\Console\Command;
use App\Domain\Points\Models\Point;
use App\Domain\Users\Models\UserPackage;
use App\Domain\Points\Enums\DirectionEnum;
use App\Notifications\UserCustomPaidSubscription;
use App\Domain\Subscriptions\Service\SubscriptionService;
class AutoSubscribePaidUsers extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'teaser:user-auto-subs';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Auto-subscription for paid users';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
\DB::beginTransaction();
$users_package_customers = DB::table('users_package_customers')
// ->join('users_subscribers', 'users_package_customers.customer_id', '=', 'users_subscribers.subscriber_id')
->join('users_subscribers', function ($join) {
$join
->on('users_package_customers.user_id', '=', 'users_subscribers.subscriber_id')
->where('users_subscribers.autosubscription', true);
})
//->select('users_package_customers.*', 'users_subscribers.user_id as userId', 'users_subscribers.subscriber_id as subscriberId', 'users_subscribers.autosubscription')
->select(DB::raw('MAX(users_package_customers.time_end) as last_time,users_package_customers.customer_id,users_package_customers.package_id','users_subscribers.autosubscription'))
->groupBy('users_package_customers.customer_id', 'users_package_customers.package_id', 'users_subscribers.autosubscription')
->havingRaw('MAX(users_package_customers.time_end) <= ?', [now()])
->get();
foreach ($users_package_customers as $user_package_customers) {
$userCustomer = User::find($user_package_customers->customer_id);
$userPackage = UserPackage::find($user_package_customers->package_id);
$userHeadPackage = User::find($userPackage->user_id);
$balance = SubscriptionService::calculate($userCustomer->id);
if ($userPackage->price > $balance) {
DB::table('users_subscribers')
->where('user_id', $userCustomer->id)
->where('subscriber_id', $userHeadPackage->id)
->update(['autosubscription' => DB::raw('NOT autosubscription')]);
continue;
}
DB::table('users_package_customers')->insert([
'user_id' => $userHeadPackage->id,
'customer_id' => $userCustomer->id,
'package_id' => $userPackage->id,
'price' => $userPackage->price,
'time_end' => now()->addMonths(),
//'time_end' => now()->addMinutes(10),
'created_at' => now(),
]);
$point = new Point;
$point->user_id = $userCustomer->id;
$point->point = $userPackage->price;
$point->type = 'Оформлена подписка на пользователя: ' . $userHeadPackage->name . ' (' . $userHeadPackage->username . ')'; //YSV ENUM!
$point->direction = DirectionEnum::EXPENSE();
$point->save();
$point = new Point;
$point->user_id = $userHeadPackage->id;
$point->point = $userPackage->price;
$point->type = 'Пользователь оформил платную подписку: ' . $userCustomer->name . ' (' . $userCustomer->username . ')'; //YSV ENUM!
$point->direction = DirectionEnum::COMING();
$point->save();
$message = [
'user_id' => $userCustomer->id,
'node_id' => null,
];
$userHeadPackage->notify(new UserCustomPaidSubscription($message));
}
\DB::commit();
// dd($users_package_customers);
return 0;
// max
// $users_package_customers = DB::table('users_package_customers')
// ->select(DB::raw('max(time_end) as last_time, avg(attempt_auto_paid) as attempt_auto_paid_avg,customer_id,package_id'))
// ->groupBy('customer_id', 'package_id')
// ->havingRaw('MAX(time_end) <= ?', [now()])
// ->havingRaw('AVG(attempt_auto_paid) < ?', [3])
// ->get();
// foreach ($users_package_customers as $users_package_customer) {
// $userCustomer = User::find($users_package_customer->customer_id);
// $userPackage = UserPackage::find($users_package_customer->package_id);
// $userHeadPackage = User::find($userPackage->user_id);
// $balance = SubscriptionService::calculate($userCustomer->id);
// if ($userPackage->price > $balance) {
// DB::table('users_package_customers')
// ->where('customer_id', $users_package_customer->customer_id)
// ->where('package_id', $users_package_customer->package_id)
// ->increment('attempt_auto_paid');
// continue;
// }
// DB::table('users_package_customers')->insert([
// 'user_id' => $userHeadPackage->id,
// 'customer_id' => $userCustomer->id,
// 'package_id' => $userPackage->id,
// 'price' => $userPackage->price,
// // 'time_end' => now()->addMonths(),
// 'time_end' => now()->addMinutes(10),
// 'created_at' => now(),
// ]);
// $point = new Point;
// $point->user_id = $userCustomer->id;
// $point->point = $userPackage->price;
// $point->type = 'Оформлена подписка на пользователя: ' . $userHeadPackage->username; //YSV ENUM!
// $point->direction = DirectionEnum::EXPENSE();
// $point->save();
// $point = new Point;
// $point->user_id = $userHeadPackage->id;
// $point->point = $userPackage->price;
// $point->type = 'Пользователь оформил платную подписку: ' . $userCustomer->username; //YSV ENUM!
// $point->direction = DirectionEnum::COMING();
// $point->save();
// $message = [
// 'user_id' => $userCustomer->id,
// 'node_id' => null,
// ];
// $userHeadPackage->notify(new UserCustomPaidSubscription($message));
// }
// return 0;
}
}

43
app/Console/Kernel.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('teaser:user-auto-subs')->weekly();
$schedule->command('teaser:auto-subs-app')->weekly();
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}