Files
test_ea/laravel/app/Services/WbService/SyncServices/SalesSyncService.php
2026-02-15 01:34:17 +07:00

75 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\WbService\SyncServices;
use App\Contracts\WbClientInterface;
use App\Contracts\WbSyncInterface;
use App\DTO\WbRequestDTO;
use App\Exceptions\WbServiceException;
use App\Models\Sale;
final readonly class SalesSyncService implements WbSyncInterface
{
public function __construct(
private WbClientInterface $client,
) {
}
/**
* @throws WbServiceException
*/
public function sync(WbRequestDTO $dto): array
{
return $this->client->fetch($dto);
}
public function save(array $data): void
{
if (empty($data)) {
return;
}
collect($data)
->chunk(500)
->each(function ($chunk) {
$rows = $chunk->map(function (array $row) {
return [
'g_number' => $row['g_number'],
'date' => $row['date'],
'last_change_date' => $row['last_change_date'],
'supplier_article' => $row['supplier_article'],
'tech_size' => $row['tech_size'],
'barcode' => (string)$row['barcode'],
'total_price' => $row['total_price'],
'discount_percent' => $row['discount_percent'],
'is_supply' => $row['is_supply'],
'is_realization' => $row['is_realization'],
'promo_code_discount' => $row['promo_code_discount'],
'warehouse_name' => $row['warehouse_name'],
'country_name' => $row['country_name'],
'oblast_okrug_name' => $row['oblast_okrug_name'],
'region_name' => $row['region_name'],
'income_id' => $row['income_id'],
'sale_id' => $row['sale_id'],
'odid' => $row['odid'],
'spp' => $row['spp'],
'for_pay' => $row['for_pay'],
'finished_price' => $row['finished_price'],
'price_with_disc' => $row['price_with_disc'],
'nm_id' => $row['nm_id'],
'subject' => $row['subject'],
'category' => $row['category'],
'brand' => $row['brand'],
'is_storno' => $row['is_storno'],
'created_at' => now(),
'updated_at' => now(),
];
})->toArray();
Sale::insert($rows);
});
}
}