WorkOrder.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. namespace App;
  3. use App\Services\NotificationService;
  4. use App\Services\WorkOrderLogService;
  5. use App\Traits\ModelTimeFormat;
  6. use Illuminate\Database\Eloquent\Builder;
  7. use Illuminate\Database\Eloquent\Model;
  8. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  9. use Illuminate\Database\Eloquent\Relations\HasMany;
  10. use Illuminate\Database\Eloquent\SoftDeletes;
  11. use Illuminate\Support\Facades\Auth;
  12. use Illuminate\Support\Facades\Gate;
  13. class WorkOrder extends Model
  14. {
  15. use ModelTimeFormat;
  16. use SoftDeletes;
  17. // 工单 信息
  18. protected $fillable = [
  19. 'order_id', // 订单id
  20. 'owner_id', // 货主
  21. 'logistic_id', // 承运商
  22. 'creator_id', // 创建人
  23. 'reviewer_id', // 审核人
  24. 'last_handler_id', // 上一个处理人
  25. 'order_issue_type_id', // 问题件类型
  26. 'process_progress', // 处理进度 -----------------------------------
  27. // 拦截: 承运商->[已处理,已签收] 宝时审核->[`无法拦截`,`成功已退回,不赔偿`,`拦截在途丢件,赔偿`]
  28. // 信息更改: 承运商->[已处理,无法更改] 宝时审核->[更改成功,更改失败]
  29. // 快递异常: 承运商->[已处理,已拦截] 宝时审核->[丢件赔偿,签收成功]
  30. // 错漏发: 宝时处理->[已核实] 商家处理->[补发,不补发]
  31. // 破损: 承运商->[核实全部破损,核实部分破损,核实未破损] 宝时终审->[核实全部破损,核实部分破损,核实未破损]
  32. // 快递丢件: 待商家处理 待终审 完结
  33. // -----------------------------------
  34. 'type', // 快递异常填写:在途异常,签收未收到
  35. 'status', // 审核状态
  36. 'review_at', // 审核时间
  37. 'uniquely_tag', // 唯一标识
  38. 'remark', // 工单信息描述
  39. 'work_order_status', // 1 为创建状态 0 为处理状态
  40. 'logistic_tag', // 待承运商处理标记 0 为默认状态 1无 2 滞 3新
  41. 'bao_shi_tag', // 待宝时处理标记 0 为默认状态 1无 2 滞 3新
  42. 'owner_tag', // 待货主处理标记 0 为默认状态 1无 2 滞 3新
  43. "is_new_rejecting", // 回库标记
  44. 'created_at',
  45. ];
  46. static public $enums = [
  47. 'status' => [
  48. '' => 0,
  49. '宝时处理' => 1,
  50. '货主处理' => 2,
  51. '承运商处理' => 3,
  52. '宝时终审' => 4,
  53. '完成' => 5,
  54. '待货主完结' => 6,
  55. ],
  56. 'last_status' => [
  57. '' => 0,
  58. '宝时处理' => 1,
  59. '货主处理' => 2,
  60. '承运商处理' => 3,
  61. '宝时终审' => 4,
  62. '完成' => 5,
  63. '待货主完结' => 6,
  64. ],
  65. 'is_new_rejecting' => [
  66. '' => 0,
  67. '回库' => 1,
  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 getIsNewRejectingAttribute($value)
  78. {
  79. if (!$value) return '';
  80. return self::$enums['is_new_rejecting'][$value];
  81. }
  82. public function setIsNewRejectingAttribute($value)
  83. {
  84. if (!$value) return;
  85. if (is_numeric($value)) {
  86. $this->attributes['is_new_rejecting'] = $value;
  87. } else {
  88. $this->attributes['is_new_rejecting'] = self::$enums['is_new_rejecting'][$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 getLastStatusAttribute($value)
  106. {
  107. if (!$value) return '';
  108. return self::$enums['last_status'][$value];
  109. }
  110. public function setLastStatusAttribute($value)
  111. {
  112. if (!$value) return;
  113. if (is_numeric($value)) {
  114. $this->attributes['last_status'] = $value;
  115. } else {
  116. $this->attributes['last_status'] = self::$enums['last_status'][$value];
  117. }
  118. }
  119. // 关联订单
  120. public function order(): BelongsTo
  121. {
  122. return $this->belongsTo(Order::class);
  123. }
  124. // 关联货主
  125. public function owner(): BelongsTo
  126. {
  127. return $this->belongsTo(Owner::class, 'owner_id');
  128. }
  129. // 关联承运商
  130. public function logistic(): BelongsTo
  131. {
  132. return $this->belongsTo(Logistic::class);
  133. }
  134. // 创建人
  135. public function creator(): BelongsTo
  136. {
  137. return $this->belongsTo(User::class, 'creator_id');
  138. }
  139. public function lastHandler(): BelongsTo
  140. {
  141. return $this->belongsTo(User::class, 'last_handler_id');
  142. }
  143. // 审核人
  144. public function reviewer(): BelongsTo
  145. {
  146. return $this->belongsTo(User::class, 'reviewer_id');
  147. }
  148. // 生成问题件类型
  149. public function issueType(): BelongsTo
  150. {
  151. return $this->belongsTo(OrderIssueType::class, 'order_issue_type_id');
  152. }
  153. // 对应问题件
  154. public function orderIssue(): BelongsTo
  155. {
  156. return $this->belongsTo(OrderIssue::class, 'order_id', 'order_id');
  157. }
  158. public function details(): HasMany
  159. {
  160. return $this->hasMany(WorkOrderDetail::class);
  161. }
  162. public function scopeFilter($query, $filters)
  163. {
  164. return $filters->apply($query);
  165. }
  166. /** 默认 with 参数 */
  167. public function scopeDefaultWith($query)
  168. {
  169. $query->with($this->defaultWith());
  170. }
  171. public function defaultWith(): array
  172. {
  173. return ['owner', 'logistic', 'issueType', 'creator', 'lastHandler', 'details' => function ($query) {
  174. return $query->with(['commodities.commodity', 'logs.creator', 'images.uploadFile', 'issueType', 'processLogs' => function ($query) {
  175. return $query->with('user')->orderByDesc('created_at');
  176. }]);
  177. },
  178. 'reviewer',
  179. 'order.packages',
  180. 'orderIssue' => function ($query) {
  181. /** @var Builder $query */
  182. $query->with(['issueType', 'logs' => function ($query) {
  183. if (Gate::denies('订单管理-问题件-客户不可见')) {
  184. $query->with('user')->orderByDesc('created_at');
  185. } else {
  186. $query->with('user')->where('tag', '=', 0)->orderByDesc('created_at');
  187. }
  188. }]);
  189. }];
  190. }
  191. public function loadDefaultWith()
  192. {
  193. $this->loadMissing($this->defaultWith());
  194. }
  195. public function notification()
  196. {
  197. $user = Auth::user();
  198. $this->loadMissing('owner', 'order');
  199. $remark = $this->remark;
  200. $ownerName = $this->owner->name ?? '';
  201. $clientCode = $this->order->client_code ?? '';
  202. $msg = $user["name"] . "建立了新工单<br/>" . $ownerName . ":" . $clientCode . "<br/>" . $remark;
  203. NotificationService::SingleRegister($msg, $clientCode, "订单管理-问题件");
  204. }
  205. public function saveWorkOrderDetail($params)
  206. {
  207. $param = (new WorkOrderDetail($params))->getAttributes();
  208. $this->details()->create($param);
  209. $this->loadMissing('details');
  210. }
  211. // 工单完结
  212. public function end()
  213. {
  214. $this->update(['status' => 5, 'work_order_status' => 0]);
  215. }
  216. /**
  217. * 工单状态
  218. */
  219. public function changeStatus($status)
  220. {
  221. $this->status = $status;
  222. $this->update();
  223. }
  224. /**
  225. * 处理进度
  226. */
  227. public function changeProcessProgress($process_progress)
  228. {
  229. $this->update(['process_progress' => $process_progress]);
  230. }
  231. /**
  232. * 工单状态 and 处理状态 and 处理人
  233. * @param $status
  234. * @param $process_progress
  235. * @param $last_status
  236. */
  237. public function change($status, $process_progress, $last_status)
  238. {
  239. $this->last_status = $last_status;
  240. $this->status = $status;
  241. $this->process_progress = $process_progress;
  242. $this->last_handler_id = Auth::id();
  243. $this->update();
  244. }
  245. public function syncIntercept()
  246. {
  247. if ($this->status === '完成') return;
  248. /** @var WorkOrderDetail $detail */
  249. $detail = $this->details()->where('order_issue_type_id', $this->order_issue_type_id)
  250. ->whereIn('status', [1, 2, 3, 4, 5])
  251. ->orderByDesc('created_at')->first();
  252. if (!$detail) return;
  253. app(WorkOrderLogService::class)->createLog($detail, '终审', '自动终审');
  254. $detail->change('待货主完结', '成功已退回,不赔偿', '宝时终审');
  255. $this->change('待货主完结', '成功已退回,不赔偿', '宝时终审');
  256. $this->update([
  257. 'owner_tag' => 2,
  258. 'logistic_tag' => 0,
  259. 'bao_shi_tag' => 0,
  260. ]);
  261. }
  262. }