60 lines
1.7 KiB
PHP
60 lines
1.7 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\Income;
|
|
|
|
final readonly class IncomesSyncService 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 [
|
|
'income_id' => $row['income_id'],
|
|
'number' => $row['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'],
|
|
'quantity' => $row['quantity'],
|
|
'total_price' => $row['total_price'],
|
|
'date_close' => $row['date_close'],
|
|
'warehouse_name' => $row['warehouse_name'],
|
|
'nm_id' => $row['nm_id'],
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
];
|
|
})->toArray();
|
|
|
|
Income::insert($rows);
|
|
});
|
|
}
|
|
}
|