WorkOrderDetail.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. use Illuminate\Support\Facades\Auth;
  9. class WorkOrderDetail extends Model
  10. {
  11. use ModelLogChanging;
  12. use ModelTimeFormat;
  13. protected $fillable = [
  14. 'work_order_id',
  15. 'order_issue_type_id', // 工单异常类型
  16. 'price', // 商品价值
  17. 'sku_amount', // 破损sku数
  18. 'type', // 快递异常填写:在途异常,签收未收到
  19. 'last_handler_id', // 上一个处理人
  20. 'reissue_logistic_number', // 补发单号
  21. 'return_logistic_number', // 退回单号 (错漏发:商家填写) (破损:创建时填写)
  22. 'process_progress', // 处理进度
  23. 'logistic_number', // 快递单号
  24. 'status', // 当前状态
  25. 'remark', // 创建工单时的问题标记
  26. 'tag', // 标记当前工单是否为历史标记
  27. 'receive_address', // 收方信息
  28. 'return_address', // 退回单 寄件人地址
  29. 'return_phone', // 退回单 寄件人联系号码
  30. 'return_name', // 退回单 寄件人姓名
  31. 'logistic_handle_tag', // 承运商在处理标记
  32. ];
  33. static public $enums = [
  34. 'status' => [
  35. '' => 0,
  36. '宝时处理' => 1,
  37. '货主处理' => 2,
  38. '承运商处理' => 3,
  39. '宝时终审' => 4,
  40. '完成' => 5,
  41. '待货主完结' => 6,
  42. ],
  43. 'last_status' => [
  44. '' => 0,
  45. '宝时处理' => 1,
  46. '货主处理' => 2,
  47. '承运商处理' => 3,
  48. '宝时终审' => 4,
  49. '完成' => 5,
  50. '待货主完结' => 6,
  51. ],
  52. 'tag' => [
  53. '创建' => 0,
  54. '完成' => 1,
  55. '标记' => 2,
  56. ],
  57. 'last_handler' => [
  58. '' => 0,
  59. '商家' => 1,
  60. '承运商' => 2,
  61. '宝时' => 3,
  62. ],
  63. 'logistic_handle_tag' => [
  64. '' => 0,
  65. '承运商处理中' => 1,
  66. '取消标记' => 2
  67. ],
  68. ];
  69. function __construct(array $attributes = [])
  70. {
  71. foreach (self::$enums as &$enum) {
  72. $enum = $enum + array_flip($enum);
  73. }
  74. parent::__construct($attributes);
  75. }
  76. public function getLogisticHandleTagAttribute($value)
  77. {
  78. if (!$value) return '';
  79. return self::$enums['logistic_handle_tag'][$value];
  80. }
  81. public function setLogisticHandleTagAttribute($value)
  82. {
  83. if (!$value) return;
  84. if (is_numeric($value)) {
  85. $this->attributes['logistic_handle_tag'] = $value;
  86. } else {
  87. $this->attributes['logistic_handle_tag'] = self::$enums['logistic_handle_tag'][$value];
  88. }
  89. }
  90. public function getStatusAttribute($value)
  91. {
  92. if (!$value) return '';
  93. return self::$enums['status'][$value];
  94. }
  95. public function setStatusAttribute($value)
  96. {
  97. if (!$value) return;
  98. if (is_numeric($value)) {
  99. $this->attributes['status'] = $value;
  100. } else {
  101. $this->attributes['status'] = self::$enums['status'][$value];
  102. }
  103. }
  104. public function getTagAttribute($value)
  105. {
  106. if (is_numeric($value)) return self::$enums['tag'][$value];
  107. if (!$value) return '';
  108. return self::$enums['tag'][$value];
  109. }
  110. public function setTagAttribute($value)
  111. {
  112. if (!$value) return;
  113. if (is_numeric($value)) {
  114. $this->attributes['tag'] = $value;
  115. } else {
  116. $this->attributes['tag'] = self::$enums['tag'][$value];
  117. }
  118. }
  119. public function getLastStatusAttribute($value)
  120. {
  121. if (!$value) return '';
  122. return self::$enums['last_status'][$value];
  123. }
  124. public function setLastStatusAttribute($value)
  125. {
  126. if (!$value) return;
  127. if (is_numeric($value)) {
  128. $this->attributes['last_status'] = $value;
  129. } else {
  130. $this->attributes['last_status'] = self::$enums['last_status'][$value];
  131. }
  132. }
  133. public function workOrder(): BelongsTo
  134. {
  135. return $this->belongsTo(WorkOrder::class);
  136. }
  137. public function issueType(): BelongsTo
  138. {
  139. return $this->belongsTo(OrderIssueType::class, 'order_issue_type_id');
  140. }
  141. // 图片
  142. public function images(): HasMany
  143. {
  144. return $this->hasMany(WorkOrderImage::class);
  145. }
  146. public function getPackageImagesAttribute()
  147. {
  148. return $this->images->where('type', 1);
  149. }
  150. public function getCommodityImagesAttribute()
  151. {
  152. return $this->images->where('type', 2);
  153. }
  154. public function getDealImagesAttribute()
  155. {
  156. return $this->images->where('type', 3);
  157. }
  158. public function getRefundImagesAttribute()
  159. {
  160. return $this->images->where('type', 4);
  161. }
  162. public function logs(): HasMany
  163. {
  164. return $this->hasMany(WorkOrderLog::class);
  165. }
  166. public function commodities(): HasMany
  167. {
  168. return $this->hasMany(WorkOrderCommodities::class);
  169. }
  170. public function processLogs(): HasMany
  171. {
  172. return $this->hasMany(WorkOrderProcessLog::class,'work_order_detail_id','id');
  173. }
  174. // 未完成历史标记
  175. public function undoneTag()
  176. {
  177. if ($this->tag != '完成') {
  178. $this->update(['tag' => 2]);
  179. }
  180. }
  181. // 完成标记
  182. public function end()
  183. {
  184. $this->status = 5;
  185. $this->tag = 1;
  186. $this->update();
  187. }
  188. public function changeStatus($status)
  189. {
  190. $this->status = $status;
  191. $this->update();
  192. }
  193. public function changeProcessProgress($process_progress)
  194. {
  195. $this->update(['process_progress' => $process_progress]);
  196. }
  197. /**
  198. * 工单状态 and 处理状态
  199. * @param $status
  200. * @param $process_progress
  201. * @param $last_status
  202. */
  203. public function change($status, $process_progress, $last_status)
  204. {
  205. $user = Auth::user();
  206. $this->status = $status;
  207. $this->last_status = $last_status;
  208. $this->process_progress = $process_progress;
  209. $this->last_handler_id = $user['id'] ?? '';
  210. $this->update();
  211. }
  212. public function logisticTagHandle()
  213. {
  214. $this->update(['logistic_handle_tag' => 1]);
  215. }
  216. public function cancelLogisticTagHandle()
  217. {
  218. $this->logistic_handle_tag = '取消标记';
  219. $this->update();
  220. }
  221. }