WorkOrderDetail.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. ],
  34. 'tag' => [
  35. '' => 0,
  36. '完成' => 1,
  37. '标记' => 2,
  38. ],
  39. ];
  40. function __construct(array $attributes = [])
  41. {
  42. foreach (self::$enums as &$enum) {
  43. $enum = $enum + array_flip($enum);
  44. }
  45. parent::__construct($attributes);
  46. }
  47. public function getStatusAttribute($value)
  48. {
  49. if (!$value) return '';
  50. return self::$enums['status'][$value];
  51. }
  52. public function setStatusAttribute($value)
  53. {
  54. if (!$value) return;
  55. if (is_numeric($value)) {
  56. $this->attributes['status'] = $value;
  57. } else {
  58. $this->attributes['status'] = self::$enums['status'][$value];
  59. }
  60. }
  61. public function getTagAttribute($value)
  62. {
  63. if (!$value) return '';
  64. return self::$enums['tag'][$value];
  65. }
  66. public function setTagAttribute($value)
  67. {
  68. if (!$value) return;
  69. if (is_numeric($value)) {
  70. $this->attributes['tag'] = $value;
  71. } else {
  72. $this->attributes['tag'] = self::$enums['tag'][$value];
  73. }
  74. }
  75. public function workOrder(): BelongsTo
  76. {
  77. return $this->belongsTo(WorkOrder::class);
  78. }
  79. public function issueType(): BelongsTo
  80. {
  81. return $this->belongsTo(OrderIssueType::class,'order_issue_type_id');
  82. }
  83. // 图片
  84. public function images(): HasMany
  85. {
  86. return $this->hasMany(WorkOrderImage::class);
  87. }
  88. public function getPackageImagesAttribute()
  89. {
  90. return $this->images->where('type', 1);
  91. }
  92. public function getCommodityImagesAttribute()
  93. {
  94. return $this->images->where('type', 2);
  95. }
  96. public function getDealImagesAttribute()
  97. {
  98. return $this->images->where('type', 3);
  99. }
  100. public function getRefundImagesAttribute()
  101. {
  102. return $this->images->where('type', 4);
  103. }
  104. public function logs(): HasMany
  105. {
  106. return $this->hasMany(WorkOrderLog::class);
  107. }
  108. public function commodities(): HasMany
  109. {
  110. return $this->hasMany(WorkOrderCommodities::class);
  111. }
  112. // 未完成历史标记
  113. public function undoneTag(){
  114. if ($this->tag != '完成'){
  115. $this->update(['tag' => 2]);
  116. }
  117. }
  118. // 完成标记
  119. public function end(){
  120. $this->status = 5;
  121. $this->tag = 1;
  122. $this->update();
  123. }
  124. public function changeStatus($status)
  125. {
  126. $this->status = $status;
  127. $this->update();
  128. }
  129. }