WorkOrderDetail.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. 'return_address', // 退回单 寄件人地址
  25. 'return_phone', // 退回单 寄件人联系号码
  26. 'return_name', // 退回单 寄件人姓名
  27. ];
  28. static public $enums = [
  29. 'status' => [
  30. '' => 0,
  31. '宝时处理' => 1,
  32. '货主处理' => 2,
  33. '承运商处理' => 3,
  34. '宝时终审' => 4,
  35. '完成' => 5,
  36. '待货主完结' => 6,
  37. ],
  38. 'tag' => [
  39. '创建' => 0,
  40. '完成' => 1,
  41. '标记' => 2,
  42. ],
  43. ];
  44. function __construct(array $attributes = [])
  45. {
  46. foreach (self::$enums as &$enum) {
  47. $enum = $enum + array_flip($enum);
  48. }
  49. parent::__construct($attributes);
  50. }
  51. public function getStatusAttribute($value)
  52. {
  53. if (!$value) return '';
  54. return self::$enums['status'][$value];
  55. }
  56. public function setStatusAttribute($value)
  57. {
  58. if (!$value) return;
  59. if (is_numeric($value)) {
  60. $this->attributes['status'] = $value;
  61. } else {
  62. $this->attributes['status'] = self::$enums['status'][$value];
  63. }
  64. }
  65. public function getTagAttribute($value)
  66. {
  67. if (is_numeric($value)) return self::$enums['tag'][$value];
  68. if (!$value) return '';
  69. return self::$enums['tag'][$value];
  70. }
  71. public function setTagAttribute($value)
  72. {
  73. if (!$value) return;
  74. if (is_numeric($value)) {
  75. $this->attributes['tag'] = $value;
  76. } else {
  77. $this->attributes['tag'] = self::$enums['tag'][$value];
  78. }
  79. }
  80. public function workOrder(): BelongsTo
  81. {
  82. return $this->belongsTo(WorkOrder::class);
  83. }
  84. public function issueType(): BelongsTo
  85. {
  86. return $this->belongsTo(OrderIssueType::class,'order_issue_type_id');
  87. }
  88. // 图片
  89. public function images(): HasMany
  90. {
  91. return $this->hasMany(WorkOrderImage::class);
  92. }
  93. public function getPackageImagesAttribute()
  94. {
  95. return $this->images->where('type', 1);
  96. }
  97. public function getCommodityImagesAttribute()
  98. {
  99. return $this->images->where('type', 2);
  100. }
  101. public function getDealImagesAttribute()
  102. {
  103. return $this->images->where('type', 3);
  104. }
  105. public function getRefundImagesAttribute()
  106. {
  107. return $this->images->where('type', 4);
  108. }
  109. public function logs(): HasMany
  110. {
  111. return $this->hasMany(WorkOrderLog::class);
  112. }
  113. public function commodities(): HasMany
  114. {
  115. return $this->hasMany(WorkOrderCommodities::class);
  116. }
  117. // 未完成历史标记
  118. public function undoneTag(){
  119. if ($this->tag != '完成'){
  120. $this->update(['tag' => 2]);
  121. }
  122. }
  123. // 完成标记
  124. public function end(){
  125. $this->status = 5;
  126. $this->tag = 1;
  127. $this->update();
  128. }
  129. public function changeStatus($status)
  130. {
  131. $this->status = $status;
  132. $this->update();
  133. }
  134. }