StockMovement.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Models;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. class StockMovement extends Model
  7. {
  8. public const TYPE_RECEIPT = 'receipt';
  9. public const TYPE_RESERVE = 'reserve';
  10. public const TYPE_ISSUE = 'issue';
  11. public const TYPE_RESERVE_CANCEL = 'reserve_cancel';
  12. public const TYPE_NAMES = [
  13. self::TYPE_RECEIPT => 'Поступление',
  14. self::TYPE_RESERVE => 'Бронирование',
  15. self::TYPE_ISSUE => 'Списание',
  16. self::TYPE_RESERVE_CANCEL => 'Отмена брони',
  17. ];
  18. protected $fillable = [
  19. 'common_catalog_item_id',
  20. 'stock_order_id',
  21. 'stock_reservation_id',
  22. 'movement_type',
  23. 'quantity',
  24. 'user_id',
  25. 'note',
  26. ];
  27. protected function casts(): array
  28. {
  29. return [
  30. 'common_catalog_item_id' => 'integer',
  31. 'stock_order_id' => 'integer',
  32. 'stock_reservation_id' => 'integer',
  33. 'quantity' => 'integer',
  34. 'user_id' => 'integer',
  35. ];
  36. }
  37. public function item(): BelongsTo
  38. {
  39. return $this->belongsTo(CommonCatalogItem::class, 'common_catalog_item_id');
  40. }
  41. public function order(): BelongsTo
  42. {
  43. return $this->belongsTo(StockOrder::class, 'stock_order_id');
  44. }
  45. public function reservation(): BelongsTo
  46. {
  47. return $this->belongsTo(StockReservation::class, 'stock_reservation_id');
  48. }
  49. public function user(): BelongsTo
  50. {
  51. return $this->belongsTo(User::class);
  52. }
  53. public function getTypeNameAttribute(): string
  54. {
  55. return self::TYPE_NAMES[$this->movement_type] ?? $this->movement_type;
  56. }
  57. }