WorkOrderDetail.php 7.0 KB

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