ProductionOrder.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Models;
  4. use App\Enums\ProductionOrderExecutionType;
  5. use App\Enums\ProductionOrderSource;
  6. use App\Enums\ProductionOrderStatus;
  7. use Database\Factories\ProductionOrderFactory;
  8. use Illuminate\Database\Eloquent\Factories\HasFactory;
  9. use Illuminate\Database\Eloquent\Model;
  10. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  11. use Illuminate\Database\Eloquent\Relations\HasMany;
  12. use Illuminate\Database\Eloquent\SoftDeletes;
  13. class ProductionOrder extends Model
  14. {
  15. /** @use HasFactory<ProductionOrderFactory> */
  16. use HasFactory;
  17. use SoftDeletes;
  18. public const DEFAULT_SORT_BY = 'created_at';
  19. public const DEFAULT_ORDER_BY = 'desc';
  20. protected $fillable = [
  21. 'external_id',
  22. 'source',
  23. 'order_number',
  24. 'order_year',
  25. 'customer_name',
  26. 'object_address',
  27. 'invoice_number',
  28. 'invoice_date',
  29. 'contract_number',
  30. 'contract_date',
  31. 'payment_date',
  32. 'supply_working_days',
  33. 'contract_shipment_date',
  34. 'application_shipment_date',
  35. 'status',
  36. 'execution_type',
  37. 'manager_id',
  38. 'note',
  39. 'created_by',
  40. 'updated_by',
  41. ];
  42. protected function casts(): array
  43. {
  44. return [
  45. 'source' => ProductionOrderSource::class,
  46. 'order_year' => 'integer',
  47. 'invoice_date' => 'immutable_date',
  48. 'contract_date' => 'immutable_date',
  49. 'payment_date' => 'immutable_date',
  50. 'supply_working_days' => 'integer',
  51. 'contract_shipment_date' => 'immutable_date',
  52. 'application_shipment_date' => 'immutable_date',
  53. 'status' => ProductionOrderStatus::class,
  54. 'execution_type' => ProductionOrderExecutionType::class,
  55. 'manager_id' => 'integer',
  56. 'created_by' => 'integer',
  57. 'updated_by' => 'integer',
  58. ];
  59. }
  60. public function manager(): BelongsTo
  61. {
  62. return $this->belongsTo(User::class, 'manager_id');
  63. }
  64. public function creator(): BelongsTo
  65. {
  66. return $this->belongsTo(User::class, 'created_by');
  67. }
  68. public function updater(): BelongsTo
  69. {
  70. return $this->belongsTo(User::class, 'updated_by');
  71. }
  72. public function items(): HasMany
  73. {
  74. return $this->hasMany(ProductionOrderItem::class)->orderBy('sort')->orderBy('id');
  75. }
  76. public function deliveries(): HasMany
  77. {
  78. return $this->hasMany(ProductionOrderDelivery::class)->orderBy('delivery_date')->orderBy('id');
  79. }
  80. public function installations(): HasMany
  81. {
  82. return $this->hasMany(ProductionOrderInstallation::class)->orderBy('installation_date')->orderBy('id');
  83. }
  84. public function activities(): HasMany
  85. {
  86. return $this->hasMany(ProductionOrderActivity::class)->latest('created_at')->latest('id');
  87. }
  88. public function statusLabel(): string
  89. {
  90. return $this->status->label();
  91. }
  92. public function executionTypeLabel(): string
  93. {
  94. return $this->execution_type->label();
  95. }
  96. public function getStatusNameAttribute(): string
  97. {
  98. return $this->statusLabel();
  99. }
  100. public function getExecutionTypeNameAttribute(): string
  101. {
  102. return $this->executionTypeLabel();
  103. }
  104. }