| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 |
- <?php
- namespace App\Models;
- use App\Helpers\Price;
- use Illuminate\Database\Eloquent\Casts\Attribute;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\BelongsToMany;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use Illuminate\Database\Eloquent\SoftDeletes;
- /**
- * Каталог запчастей — СПРАВОЧНИК.
- *
- * ВАЖНО: Эта модель НЕ хранит остатки!
- * Остатки рассчитываются на основе:
- * - SparePartOrder.available_qty (физический остаток)
- * - Reservation (активные резервы)
- *
- * Свободный остаток = Физический - Зарезервировано
- */
- class SparePart extends Model
- {
- use SoftDeletes;
- const DEFAULT_SORT_BY = 'article';
- protected $fillable = [
- // БЕЗ year! Каталог общий для всех лет
- 'article',
- 'used_in_maf',
- 'product_id',
- 'note',
- 'purchase_price',
- 'customer_price',
- 'expertise_price',
- 'tsn_number',
- 'min_stock',
- ];
- /**
- * Атрибуты для автоматической сериализации.
- *
- * ВАЖНО: Подробные остатки (physical_stock_*, reserved_*, free_stock_*) НЕ включены сюда,
- * т.к. согласно спецификации каталог не хранит остатки.
- * Для получения детальных остатков используйте SparePartInventoryService::getStockInfo()
- * или явно вызывайте соответствующие аксессоры.
- *
- * Атрибуты quantity_* оставлены для обратной совместимости с UI.
- */
- protected $appends = [
- 'purchase_price',
- 'customer_price',
- 'expertise_price',
- 'purchase_price_txt',
- 'customer_price_txt',
- 'expertise_price_txt',
- 'image',
- 'tsn_number_description',
- 'pricing_code_description',
- 'pricing_codes_list',
- // Обратная совместимость для UI (отображаются как свободный остаток)
- 'quantity_without_docs',
- 'quantity_with_docs',
- 'total_quantity',
- ];
- // Аксессоры для цен (копейки -> рубли)
- protected function purchasePrice(): Attribute
- {
- return Attribute::make(
- get: fn($value) => $value ? $value / 100 : null,
- set: fn($value) => $value ? round($value * 100) : null,
- );
- }
- protected function customerPrice(): Attribute
- {
- return Attribute::make(
- get: fn($value) => $value ? $value / 100 : null,
- set: fn($value) => $value ? round($value * 100) : null,
- );
- }
- protected function expertisePrice(): Attribute
- {
- return Attribute::make(
- get: fn($value) => $value ? $value / 100 : null,
- set: fn($value) => $value ? round($value * 100) : null,
- );
- }
- // Текстовое представление цен
- protected function purchasePriceTxt(): Attribute
- {
- return Attribute::make(
- get: fn() => isset($this->attributes['purchase_price']) && $this->attributes['purchase_price'] !== null
- ? Price::format($this->attributes['purchase_price'] / 100)
- : '-',
- );
- }
- protected function customerPriceTxt(): Attribute
- {
- return Attribute::make(
- get: fn() => isset($this->attributes['customer_price']) && $this->attributes['customer_price'] !== null
- ? Price::format($this->attributes['customer_price'] / 100)
- : '-',
- );
- }
- protected function expertisePriceTxt(): Attribute
- {
- return Attribute::make(
- get: fn() => isset($this->attributes['expertise_price']) && $this->attributes['expertise_price'] !== null
- ? Price::format($this->attributes['expertise_price'] / 100)
- : '-',
- );
- }
- // Атрибут для картинки
- public function image(): Attribute
- {
- $path = '';
- if (file_exists(public_path() . '/images/spare_parts/' . $this->article . '.jpg')) {
- $path = url('/images/spare_parts/' . $this->article . '.jpg');
- }
- return Attribute::make(get: fn() => $path);
- }
- // ========== ОТНОШЕНИЯ ==========
- public function product(): BelongsTo
- {
- return $this->belongsTo(Product::class);
- }
- public function orders(): HasMany
- {
- return $this->hasMany(SparePartOrder::class);
- }
- public function reclamations(): BelongsToMany
- {
- return $this->belongsToMany(Reclamation::class, 'reclamation_spare_part')
- ->withPivot('quantity', 'with_documents', 'status', 'reserved_qty', 'issued_qty')
- ->withTimestamps();
- }
- public function pricingCodes(): BelongsToMany
- {
- return $this->belongsToMany(PricingCode::class, 'spare_part_pricing_code')
- ->withTimestamps();
- }
- public function reservations(): HasMany
- {
- return $this->hasMany(Reservation::class);
- }
- public function shortages(): HasMany
- {
- return $this->hasMany(Shortage::class);
- }
- public function movements(): HasMany
- {
- return $this->hasMany(InventoryMovement::class);
- }
- // ========== ВЫЧИСЛЯЕМЫЕ ПОЛЯ ОСТАТКОВ ==========
- // Примечание: YearScope автоматически применяется к SparePartOrder
- /**
- * Физический остаток БЕЗ документов
- */
- public function getPhysicalStockWithoutDocsAttribute(): int
- {
- return (int) ($this->orders()
- ->where('status', SparePartOrder::STATUS_IN_STOCK)
- ->where('with_documents', false)
- ->sum('available_qty') ?? 0);
- }
- /**
- * Физический остаток С документами
- */
- public function getPhysicalStockWithDocsAttribute(): int
- {
- return (int) ($this->orders()
- ->where('status', SparePartOrder::STATUS_IN_STOCK)
- ->where('with_documents', true)
- ->sum('available_qty') ?? 0);
- }
- /**
- * Зарезервировано БЕЗ документов
- */
- public function getReservedWithoutDocsAttribute(): int
- {
- return (int) (Reservation::query()
- ->where('spare_part_id', $this->id)
- ->where('with_documents', false)
- ->where('status', Reservation::STATUS_ACTIVE)
- ->sum('reserved_qty') ?? 0);
- }
- /**
- * Зарезервировано С документами
- */
- public function getReservedWithDocsAttribute(): int
- {
- return (int) (Reservation::query()
- ->where('spare_part_id', $this->id)
- ->where('with_documents', true)
- ->where('status', Reservation::STATUS_ACTIVE)
- ->sum('reserved_qty') ?? 0);
- }
- /**
- * Свободный остаток БЕЗ документов
- */
- public function getFreeStockWithoutDocsAttribute(): int
- {
- return $this->physical_stock_without_docs - $this->reserved_without_docs;
- }
- /**
- * Свободный остаток С документами
- */
- public function getFreeStockWithDocsAttribute(): int
- {
- return $this->physical_stock_with_docs - $this->reserved_with_docs;
- }
- /**
- * Общий физический остаток
- */
- public function getTotalPhysicalStockAttribute(): int
- {
- return $this->physical_stock_without_docs + $this->physical_stock_with_docs;
- }
- /**
- * Общее зарезервировано
- */
- public function getTotalReservedAttribute(): int
- {
- return $this->reserved_without_docs + $this->reserved_with_docs;
- }
- /**
- * Общий свободный остаток
- */
- public function getTotalFreeStockAttribute(): int
- {
- return $this->total_physical_stock - $this->total_reserved;
- }
- // ========== ОБРАТНАЯ СОВМЕСТИМОСТЬ ==========
- // Старые атрибуты для совместимости с существующим кодом
- public function getQuantityWithoutDocsAttribute(): int
- {
- return $this->free_stock_without_docs;
- }
- public function getQuantityWithDocsAttribute(): int
- {
- return $this->free_stock_with_docs;
- }
- public function getTotalQuantityAttribute(): int
- {
- return $this->total_free_stock;
- }
- // ========== МЕТОДЫ ПРОВЕРКИ ==========
- /**
- * Есть ли открытые дефициты?
- */
- public function hasOpenShortages(): bool
- {
- return $this->shortages()->open()->exists();
- }
- /**
- * Количество в открытых дефицитах
- */
- public function getOpenShortagesQtyAttribute(): int
- {
- return (int) ($this->shortages()->open()->sum('missing_qty') ?? 0);
- }
- /**
- * Ниже минимального остатка?
- */
- public function isBelowMinStock(): bool
- {
- return $this->total_free_stock < $this->min_stock;
- }
- /**
- * @deprecated Используйте hasOpenShortages()
- */
- public function hasCriticalShortage(): bool
- {
- return $this->hasOpenShortages();
- }
- // ========== РАСШИФРОВКИ ==========
- protected function tsnNumberDescription(): Attribute
- {
- return Attribute::make(
- get: fn() => PricingCode::getTsnDescription($this->tsn_number)
- );
- }
- protected function pricingCodeDescription(): Attribute
- {
- return Attribute::make(
- get: function () {
- $codes = $this->pricingCodes;
- if ($codes->isEmpty()) {
- return null;
- }
- return $codes->map(fn($code) => $code->code . ($code->description ? ': ' . $code->description : ''))->implode("\n");
- }
- );
- }
- public function getPricingCodesListAttribute(): string
- {
- return $this->pricingCodes->pluck('code')->implode(', ');
- }
- // ========== ВСПОМОГАТЕЛЬНЫЕ МЕТОДЫ ==========
- /**
- * Получить свободный остаток для конкретного типа документов
- */
- public function getFreeStock(bool $withDocuments): int
- {
- return $withDocuments
- ? $this->free_stock_with_docs
- : $this->free_stock_without_docs;
- }
- /**
- * Получить физический остаток для конкретного типа документов
- */
- public function getPhysicalStock(bool $withDocuments): int
- {
- return $withDocuments
- ? $this->physical_stock_with_docs
- : $this->physical_stock_without_docs;
- }
- /**
- * Получить зарезервировано для конкретного типа документов
- */
- public function getReserved(bool $withDocuments): int
- {
- return $withDocuments
- ? $this->reserved_with_docs
- : $this->reserved_without_docs;
- }
- }
|