WorkOrderDetail.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App;
  3. use App\Traits\ModelTimeFormat;
  4. use Illuminate\Database\Eloquent\Model;
  5. use App\Traits\ModelLogChanging;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Database\Eloquent\Relations\HasMany;
  8. class WorkOrderDetail extends Model
  9. {
  10. use ModelLogChanging;
  11. use ModelTimeFormat;
  12. protected $fillable = [
  13. 'work_order_id',
  14. 'order_issue_type_id', // 工单异常类型
  15. 'price', // 商品价值
  16. 'sku_amount', // 破损sku数
  17. 'receive_address', // 收方信息
  18. 'reissue_logistic_number', // 补发单号
  19. 'return_logistic_number', // 退回单号
  20. 'logistic_number', // 快递单号
  21. 'status', // 当前状态
  22. 'remark', // 创建工单时的问题标记
  23. 'tag', // 标记当前工单是否为历史标记
  24. ];
  25. static public $enums = [
  26. 'status' => [
  27. '' => 0,
  28. '宝时处理' => 1,
  29. '货主处理' => 2,
  30. '承运商处理' => 3,
  31. '宝时终审' => 4,
  32. '完成' => 5,
  33. '待货主完结' => 6,
  34. ],
  35. 'tag' => [
  36. '创建' => 0,
  37. '完成' => 1,
  38. '标记' => 2,
  39. ],
  40. ];
  41. function __construct(array $attributes = [])
  42. {
  43. foreach (self::$enums as &$enum) {
  44. $enum = $enum + array_flip($enum);
  45. }
  46. parent::__construct($attributes);
  47. }
  48. public function getStatusAttribute($value)
  49. {
  50. if (!$value) return '';
  51. return self::$enums['status'][$value];
  52. }
  53. public function setStatusAttribute($value)
  54. {
  55. if (!$value) return;
  56. if (is_numeric($value)) {
  57. $this->attributes['status'] = $value;
  58. } else {
  59. $this->attributes['status'] = self::$enums['status'][$value];
  60. }
  61. }
  62. public function getTagAttribute($value)
  63. {
  64. if (is_numeric($value)) return self::$enums['tag'][$value];
  65. if (!$value) return '';
  66. return self::$enums['tag'][$value];
  67. }
  68. public function setTagAttribute($value)
  69. {
  70. if (!$value) return;
  71. if (is_numeric($value)) {
  72. $this->attributes['tag'] = $value;
  73. } else {
  74. $this->attributes['tag'] = self::$enums['tag'][$value];
  75. }
  76. }
  77. public function workOrder(): BelongsTo
  78. {
  79. return $this->belongsTo(WorkOrder::class);
  80. }
  81. public function issueType(): BelongsTo
  82. {
  83. return $this->belongsTo(OrderIssueType::class,'order_issue_type_id');
  84. }
  85. // 图片
  86. public function images(): HasMany
  87. {
  88. return $this->hasMany(WorkOrderImage::class);
  89. }
  90. public function getPackageImagesAttribute()
  91. {
  92. return $this->images->where('type', 1);
  93. }
  94. public function getCommodityImagesAttribute()
  95. {
  96. return $this->images->where('type', 2);
  97. }
  98. public function getDealImagesAttribute()
  99. {
  100. return $this->images->where('type', 3);
  101. }
  102. public function getRefundImagesAttribute()
  103. {
  104. return $this->images->where('type', 4);
  105. }
  106. public function logs(): HasMany
  107. {
  108. return $this->hasMany(WorkOrderLog::class);
  109. }
  110. public function commodities(): HasMany
  111. {
  112. return $this->hasMany(WorkOrderCommodities::class);
  113. }
  114. // 未完成历史标记
  115. public function undoneTag(){
  116. if ($this->tag != '完成'){
  117. $this->update(['tag' => 2]);
  118. }
  119. }
  120. // 完成标记
  121. public function end(){
  122. $this->status = 5;
  123. $this->tag = 1;
  124. $this->update();
  125. }
  126. public function changeStatus($status)
  127. {
  128. $this->status = $status;
  129. $this->update();
  130. }
  131. }