first commit

This commit is contained in:
2026-02-15 01:34:17 +07:00
commit baac22e3ae
95 changed files with 13238 additions and 0 deletions

View File

@@ -0,0 +1,101 @@
<?php
declare(strict_types=1);
namespace App\Console\Commands;
use App\Enums\SyncStatus;
use Illuminate\Console\Command;
use App\Enums\WbEndpoint;
use App\Models\SyncState;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Artisan;
final class SyncAllCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sync:all';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle(): int
{
$this->syncEndpoint(WbEndpoint::ORDERS, 'sync:orders');
$this->syncEndpoint(WbEndpoint::SALES, 'sync:sales');
$this->syncEndpoint(WbEndpoint::INCOMES, 'sync:incomes');
$this->syncStocks();
return self::SUCCESS;
}
private function syncEndpoint(WbEndpoint $endpoint, string $command): void
{
$yesterday = Carbon::yesterday();
$lastSuccess = SyncState::query()
->where('entity', $endpoint->name)
->where('status', SyncStatus::SUCCESS)
->orderByDesc('date_to')
->first();
if (!$lastSuccess) {
$dateFrom = Carbon::now()->subYears(2);
$dateTo = $yesterday;
} else {
$dateFrom = Carbon::parse($lastSuccess->date_to);
$dateTo = $dateFrom->copy()->addDay();
if ($dateTo->gt($yesterday)) {
$dateTo = $yesterday->copy();
}
}
if ($dateFrom->gte($yesterday)) {
$this->info("$endpoint->name already up-to-date");
return;
}
$this->info("Syncing $endpoint->name from {$dateFrom->toDateString()} to {$dateTo->toDateString()}");
Artisan::call($command, [
'--dateFrom' => $dateFrom->toDateString(),
'--dateTo' => $dateTo->toDateString(),
]);
}
private function syncStocks(): void
{
$today = Carbon::today()->toDateString();
$lastSuccess = SyncState::query()
->where('entity', WbEndpoint::STOCKS->name)
->where('status', SyncStatus::SUCCESS)
->where('date_from', $today)
->first();
if ($lastSuccess) {
$this->info("STOCKS already synced for today");
return;
}
$this->info("Syncing STOCKS for today");
Artisan::call('sync:stocks', [
'--date' => $today,
]);
}
}

View File

@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace App\Console\Commands;
use App\Contracts\WbSyncManagerInterface;
use App\Enums\WbEndpoint;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
final class SyncIncomesCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sync:incomes {--dateFrom=} {--dateTo=}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Sync incomes from WB API';
/**
* Execute the console command.
*/
public function handle(WbSyncManagerInterface $manager): int
{
$dateFrom = $this->option('dateFrom');
$dateTo = $this->option('dateTo');
$dateFrom = $dateFrom ?: Carbon::now()->subYear()->toDateString();
$dateTo = $dateTo ?: Carbon::yesterday()->toDateString();
$manager->start(WbEndpoint::INCOMES, $dateFrom, $dateTo);
$this->info('Sync started');
return self::SUCCESS;
}
}

View File

@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace App\Console\Commands;
use App\Contracts\WbSyncManagerInterface;
use App\Enums\WbEndpoint;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
final class SyncOrdersCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sync:orders {--dateFrom=} {--dateTo=}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Sync orders from API';
public function handle(WbSyncManagerInterface $manager): int
{
$dateFrom = $this->option('dateFrom')
?: Carbon::now()->subYears(2)->toDateString();
$dateTo = $this->option('dateTo')
?: Carbon::yesterday()->toDateString();
if ($dateFrom >= $dateTo) {
$this->info('Nothing to sync');
return self::SUCCESS;
}
$this->info("Syncing ORDERS from $dateFrom to $dateTo");
$manager->start(WbEndpoint::ORDERS, $dateFrom, $dateTo);
return self::SUCCESS;
}
}

View File

@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace App\Console\Commands;
use App\Contracts\WbSyncManagerInterface;
use App\Enums\WbEndpoint;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
final class SyncSalesCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sync:sales {--dateFrom=} {--dateTo=}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Sync sales from WB API';
/**
* Execute the console command.
*/
public function handle(WbSyncManagerInterface $manager): int
{
$dateFrom = $this->option('dateFrom');
$dateTo = $this->option('dateTo');
$dateFrom = $dateFrom ?: Carbon::now()->subYear()->toDateString();
$dateTo = $dateTo ?: Carbon::yesterday()->toDateString();
$manager->start(WbEndpoint::SALES, $dateFrom, $dateTo);
$this->info('Sales sync started');
return self::SUCCESS;
}
}

View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace App\Console\Commands;
use App\Contracts\WbSyncManagerInterface;
use App\Enums\WbEndpoint;
use Illuminate\Console\Command;
final class SyncStocksCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sync:stocks {--date=}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Sync stocks from WB test API';
/**
* Execute the console command.
*/
public function handle(WbSyncManagerInterface $manager): int
{
$date = (string)($this->option('date') ?? now()->toDateString());
$manager->start(WbEndpoint::STOCKS, $date);
$this->info('Sync started');
return self::SUCCESS;
}
}