/
sebero
/
lk
Обзор
Документация
Войти
/
sebero
/
lk
Код
Пакеты
0
Релизы
0
Аналитика
master
app/Models/PendingOrder.php
133 строки
3 KB
Молодцов Андрей Сергеевич
first_commit
20 фев 2026, 13:08
20 фев 2026, 13:08
f6c0eb9
Код
Авторство
О чём код?
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class PendingOrder extends Model { protected $table = 'pending_orders'; protected $fillable = [ 'bitrix_item_id', 'owner_type', 'product_rows', 'total_products', 'added_products', 'failed_products', 'last_successful_batch', 'status', 'attempts', 'last_error', 'failed_items', ]; protected $casts = [ 'product_rows' => 'array', 'failed_items' => 'array', 'total_products' => 'integer', 'added_products' => 'integer', 'failed_products' => 'integer', 'last_successful_batch' => 'integer', 'attempts' => 'integer', 'bitrix_item_id' => 'integer', ]; const STATUS_PENDING = 'pending'; const STATUS_PROCESSING = 'processing'; const STATUS_COMPLETED = 'completed'; const STATUS_FAILED = 'failed'; const STATUS_PARTIALLY_COMPLETED = 'partially_completed'; public function scopeRetryable($query) { return $query->whereIn('status', [self::STATUS_FAILED, self::STATUS_PARTIALLY_COMPLETED]); } public function scopeInProgress($query) { return $query->where('status', self::STATUS_PROCESSING); } public function scopeIncomplete($query) { return $query->whereIn('status', [ self::STATUS_PENDING, self::STATUS_PROCESSING, self::STATUS_FAILED, self::STATUS_PARTIALLY_COMPLETED, ]); } public function markProcessing(): void { $this->update([ 'status' => self::STATUS_PROCESSING, 'attempts' => $this->attempts + 1, ]); } public function markCompleted(int $addedProducts): void { $this->update([ 'status' => self::STATUS_COMPLETED, 'added_products' => $addedProducts, 'failed_products' => 0, 'last_error' => null, ]); } public function markPartiallyCompleted(int $addedProducts, int $failedProducts, int $lastBatch, array $failedItems = []): void { $this->update([ 'status' => self::STATUS_PARTIALLY_COMPLETED, 'added_products' => $addedProducts, 'failed_products' => $failedProducts, 'last_successful_batch' => $lastBatch, 'failed_items' => $failedItems, ]); } public function markFailed(string $error, int $addedProducts = 0, int $failedProducts = 0, array $failedItems = []): void { $this->update([ 'status' => self::STATUS_FAILED, 'last_error' => mb_substr($error, 0, 5000), 'added_products' => $addedProducts, 'failed_products' => $failedProducts, 'failed_items' => $failedItems, ]); } public function updateBatchProgress(int $batchIndex, int $addedProducts): void { $this->update([ 'last_successful_batch' => $batchIndex, 'added_products' => $addedProducts, ]); } }