WorkOrder.php 11 KB

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