WorkOrder.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. 'rejecting_status', // 退回状态
  46. ];
  47. /**
  48. * @var int 默认
  49. */
  50. public static $DEFAULT_STATUS = 0; //
  51. /**
  52. * @var int 宝时处理
  53. */
  54. public static $BAO_SHI_HANDLER_STATUS = 1; // 宝时处理
  55. /**
  56. * @var int 货主处理
  57. */
  58. public static $OWNER_HANDLER_STATUS = 2; // 货主处理
  59. /**
  60. * @var int 承运商处理
  61. */
  62. public static $LOGISTIC_HANDLER_STATUS = 3; // 承运商处理
  63. /**
  64. * @var int 宝时终审
  65. */
  66. public static $BAO_SHI_REVIEW_STATUS = 4; // 宝时终审
  67. /**
  68. * @var int 完成
  69. */
  70. public static $END_STATUS = 5; // 完成
  71. /**
  72. * @var int 待货主完结
  73. */
  74. public static $TO_BO_OWNER_END_STATUS = 6; // 待货主完结
  75. /**
  76. * @var int `新`标记
  77. */
  78. public static $NEW_TAG = 3;
  79. /**
  80. * @var int `滞`标记
  81. */
  82. public static $STRAND_TAG = 2;
  83. /**
  84. * @var int `无`标记
  85. */
  86. public static $NO_STATE_TAG = 1;
  87. /**
  88. * @var int 默认标记
  89. */
  90. public static $DEFAULT_TAG = 0;
  91. static public $enums = [
  92. 'status' => [
  93. '' => 0,
  94. '宝时处理' => 1,
  95. '货主处理' => 2,
  96. '承运商处理' => 3,
  97. '宝时终审' => 4,
  98. '完成' => 5,
  99. '待货主完结' => 6,
  100. ],
  101. 'last_status' => [
  102. '' => 0,
  103. '宝时处理' => 1,
  104. '货主处理' => 2,
  105. '承运商处理' => 3,
  106. '宝时终审' => 4,
  107. '完成' => 5,
  108. '待货主完结' => 6,
  109. ],
  110. 'is_new_rejecting' => [
  111. '' => 0,
  112. '回库' => 1,
  113. ],
  114. ];
  115. function __construct(array $attributes = [])
  116. {
  117. foreach (self::$enums as &$enum) {
  118. $enum = $enum + array_flip($enum);
  119. }
  120. parent::__construct($attributes);
  121. }
  122. public function getIsNewRejectingAttribute($value)
  123. {
  124. if (!$value) return '';
  125. return self::$enums['is_new_rejecting'][$value];
  126. }
  127. public function setIsNewRejectingAttribute($value)
  128. {
  129. if (!$value) return;
  130. if (is_numeric($value)) {
  131. $this->attributes['is_new_rejecting'] = $value;
  132. } else {
  133. $this->attributes['is_new_rejecting'] = self::$enums['is_new_rejecting'][$value];
  134. }
  135. }
  136. public function getStatusAttribute($value)
  137. {
  138. if (!$value) return '';
  139. return self::$enums['status'][$value];
  140. }
  141. public function setStatusAttribute($value)
  142. {
  143. if (!$value) return;
  144. if (is_numeric($value)) {
  145. $this->attributes['status'] = $value;
  146. } else {
  147. $this->attributes['status'] = self::$enums['status'][$value];
  148. }
  149. }
  150. public function getLastStatusAttribute($value)
  151. {
  152. if (!$value) return '';
  153. return self::$enums['last_status'][$value];
  154. }
  155. public function setLastStatusAttribute($value)
  156. {
  157. if (!$value) return;
  158. if (is_numeric($value)) {
  159. $this->attributes['last_status'] = $value;
  160. } else {
  161. $this->attributes['last_status'] = self::$enums['last_status'][$value];
  162. }
  163. }
  164. // 关联订单
  165. public function order(): BelongsTo
  166. {
  167. return $this->belongsTo(Order::class);
  168. }
  169. // 关联货主
  170. public function owner(): BelongsTo
  171. {
  172. return $this->belongsTo(Owner::class, 'owner_id');
  173. }
  174. // 关联承运商
  175. public function logistic(): BelongsTo
  176. {
  177. return $this->belongsTo(Logistic::class);
  178. }
  179. // 创建人
  180. public function creator(): BelongsTo
  181. {
  182. return $this->belongsTo(User::class, 'creator_id');
  183. }
  184. public function lastHandler(): BelongsTo
  185. {
  186. return $this->belongsTo(User::class, 'last_handler_id');
  187. }
  188. // 审核人
  189. public function reviewer(): BelongsTo
  190. {
  191. return $this->belongsTo(User::class, 'reviewer_id');
  192. }
  193. // 生成问题件类型
  194. public function issueType(): BelongsTo
  195. {
  196. return $this->belongsTo(OrderIssueType::class, 'order_issue_type_id');
  197. }
  198. // 对应问题件
  199. public function orderIssue(): BelongsTo
  200. {
  201. return $this->belongsTo(OrderIssue::class, 'order_id', 'order_id');
  202. }
  203. public function details(): HasMany
  204. {
  205. return $this->hasMany(WorkOrderDetail::class);
  206. }
  207. public function orderDetail(): BelongsTo
  208. {
  209. return $this->belongsTo(OrderDetail::class,'order_id','order_id');
  210. }
  211. public function scopeFilter($query, $filters)
  212. {
  213. return $filters->apply($query);
  214. }
  215. /** 默认 with 参数 */
  216. public function scopeDefaultWith($query)
  217. {
  218. $query->with($this->defaultWith());
  219. }
  220. public function defaultWith(): array
  221. {
  222. return ['owner', 'logistic', 'issueType', 'creator', 'lastHandler','orderDetail', 'details' => function ($query) {
  223. return $query->with(['commodities.commodity', 'logs' => function ($query) {
  224. return $query->with('creator')->orderByDesc('created_at');
  225. }, 'images.uploadFile', 'issueType', 'processLogs' => function ($query) {
  226. return $query->with('user')->orderByDesc('created_at');
  227. }]);
  228. },
  229. 'reviewer',
  230. 'order'=> function($query){
  231. return $query->with('packages.commodities.commodity','shop');
  232. },
  233. 'orderIssue' => function ($query) {
  234. /** @var Builder $query */
  235. $query->with(['issueType', 'logs' => function ($query) {
  236. if (Gate::denies('订单管理-问题件-客户不可见')) {
  237. $query->with('user')->orderByDesc('created_at');
  238. } else {
  239. $query->with('user')->where('tag', '=', 0)->orderByDesc('created_at');
  240. }
  241. }]);
  242. }];
  243. }
  244. public function loadDefaultWith()
  245. {
  246. $this->loadMissing($this->defaultWith());
  247. }
  248. public function notification()
  249. {
  250. $user = Auth::user();
  251. $this->loadMissing('owner', 'order');
  252. $remark = $this->remark;
  253. $ownerName = $this->owner->name ?? '';
  254. $clientCode = $this->order->client_code ?? '';
  255. $msg = $user["name"] . "建立了新工单<br/>" . $ownerName . ":" . $clientCode . "<br/>" . $remark;
  256. NotificationService::SingleRegister($msg, $clientCode, "订单管理-问题件");
  257. }
  258. public function saveWorkOrderDetail($params)
  259. {
  260. $param = (new WorkOrderDetail($params))->getAttributes();
  261. $this->details()->create($param);
  262. $this->loadMissing('details');
  263. }
  264. // 工单完结
  265. public function end()
  266. {
  267. $this->update(['status' => 5, 'work_order_status' => 0]);
  268. }
  269. /**
  270. * 工单状态
  271. */
  272. public function changeStatus($status)
  273. {
  274. $this->status = $status;
  275. $this->update();
  276. }
  277. /**
  278. * 处理进度
  279. */
  280. public function changeProcessProgress($process_progress)
  281. {
  282. $this->update(['process_progress' => $process_progress]);
  283. }
  284. /**
  285. * 工单状态 and 处理状态 and 处理人
  286. * @param $status
  287. * @param $process_progress
  288. * @param $last_status
  289. */
  290. public function change($status, $process_progress, $last_status)
  291. {
  292. $this->last_status = $last_status;
  293. $this->status = $status;
  294. $this->process_progress = $process_progress;
  295. $this->last_handler_id = Auth::id();
  296. $this->update();
  297. }
  298. public function syncIntercept()
  299. {
  300. if ($this->status === '完成') return;
  301. /** @var WorkOrderDetail $detail */
  302. $detail = $this->details()->where('order_issue_type_id', $this->order_issue_type_id)
  303. ->whereIn('status', [1, 2, 3, 4, 5])
  304. ->orderByDesc('created_at')->first();
  305. if (!$detail) return;
  306. $process_progress = '成功已退回,不赔偿';
  307. $detail->update([
  308. 'status' => WorkOrder::$TO_BO_OWNER_END_STATUS,
  309. 'process_progress' => $process_progress,
  310. 'last_status' => WorkOrder::$BAO_SHI_REVIEW_STATUS,
  311. ]);
  312. $this->update([
  313. 'status' => WorkOrder::$TO_BO_OWNER_END_STATUS,
  314. 'process_progress' => $process_progress,
  315. 'last_status' => WorkOrder::$BAO_SHI_REVIEW_STATUS,
  316. 'owner_tag' => WorkOrder::$STRAND_TAG,
  317. 'logistic_tag' => WorkOrder::$DEFAULT_TAG,
  318. 'bao_shi_tag' => WorkOrder::$DEFAULT_TAG,
  319. ]);
  320. app(WorkOrderLogService::class)->createLog($detail, '终审', '自动终审');
  321. }
  322. }