WorkOrder.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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\BelongsToMany;
  10. use Illuminate\Database\Eloquent\Relations\HasMany;
  11. use Illuminate\Database\Eloquent\SoftDeletes;
  12. use Illuminate\Support\Facades\Auth;
  13. use Illuminate\Support\Facades\Gate;
  14. class WorkOrder extends Model
  15. {
  16. use ModelTimeFormat;
  17. use SoftDeletes;
  18. // 工单 信息
  19. protected $fillable = [
  20. 'order_id', // 订单id
  21. 'owner_id', // 货主
  22. 'logistic_id', // 承运商
  23. 'creator_id', // 创建人
  24. 'reviewer_id', // 审核人
  25. 'last_handler_id', // 上一个处理人
  26. 'order_issue_type_id', // 问题件类型
  27. 'work_order_detail_id', // 详情
  28. 'process_progress', // 处理进度 -----------------------------------
  29. // 拦截: 承运商->[已处理,已签收] 宝时审核->[`无法拦截`,`成功已退回,不赔偿`,`拦截在途丢件,赔偿`]
  30. // 信息更改: 承运商->[已处理,无法更改] 宝时审核->[更改成功,更改失败]
  31. // 快递异常: 承运商->[已处理,已拦截] 宝时审核->[丢件赔偿,签收成功]
  32. // 错漏发: 宝时处理->[已核实] 商家处理->[补发,不补发]
  33. // 破损: 承运商->[核实全部破损,核实部分破损,核实未破损] 宝时终审->[核实全部破损,核实部分破损,核实未破损]
  34. // 快递丢件: 待商家处理 待终审 完结
  35. // -----------------------------------
  36. 'type', // 快递异常填写:在途异常,签收未收到
  37. 'status', // 审核状态
  38. 'review_at', // 审核时间
  39. 'uniquely_tag', // 唯一标识
  40. 'remark', // 工单信息描述
  41. 'work_order_status', // 1 为创建状态 0 为处理状态
  42. 'logistic_tag', // 待承运商处理标记 0 为默认状态 1无 2 滞 3新
  43. 'bao_shi_tag', // 待宝时处理标记 0 为默认状态 1无 2 滞 3新
  44. 'owner_tag', // 待货主处理标记 0 为默认状态 1无 2 滞 3新
  45. "is_new_rejecting", // 回库标记
  46. 'created_at',
  47. 'rejecting_status', // 退回状态
  48. 'logistic_indemnity_money', // 承运商赔偿金额
  49. 'logistic_express_remission', // 承运商减免
  50. 'bao_shi_indemnity_money', // 宝时赔偿金额
  51. 'bao_shi_express_remission', // 宝时减免
  52. 'user_owner_group_id',
  53. ];
  54. /**
  55. * @var int 默认
  56. */
  57. public static $DEFAULT_STATUS = 0;
  58. /**
  59. * @var int 宝时处理
  60. */
  61. public static $BAO_SHI_HANDLER_STATUS = 1;
  62. /**
  63. * @var int 货主处理
  64. */
  65. public static $OWNER_HANDLER_STATUS = 2;
  66. /**
  67. * @var int 承运商处理
  68. */
  69. public static $LOGISTIC_HANDLER_STATUS = 3;
  70. /**
  71. * @var int 宝时终审
  72. */
  73. public static $BAO_SHI_REVIEW_STATUS = 4;
  74. /**
  75. * @var int 完成
  76. */
  77. public static $END_STATUS = 5;
  78. /**
  79. * @var int 待货主完结
  80. */
  81. public static $TO_BO_OWNER_END_STATUS = 6;
  82. /**
  83. * @var int `新`标记
  84. */
  85. public static $NEW_TAG = 3;
  86. /**
  87. * @var int `滞`标记
  88. */
  89. public static $STRAND_TAG = 2;
  90. /**
  91. * @var int `无`标记
  92. */
  93. public static $NO_STATE_TAG = 1;
  94. /**
  95. * @var int 默认标记
  96. */
  97. public static $DEFAULT_TAG = 0;
  98. static public $enums = [
  99. 'status' => [
  100. '' => 0,
  101. '宝时处理' => 1,
  102. '货主处理' => 2,
  103. '承运商处理' => 3,
  104. '宝时终审' => 4,
  105. '完成' => 5,
  106. '待货主完结' => 6,
  107. ],
  108. 'last_status' => [
  109. '' => 0,
  110. '宝时处理' => 1,
  111. '货主处理' => 2,
  112. '承运商处理' => 3,
  113. '宝时终审' => 4,
  114. '完成' => 5,
  115. '待货主完结' => 6,
  116. ],
  117. 'is_new_rejecting' => [
  118. '' => 0,
  119. '回库' => 1,
  120. ],
  121. ];
  122. function __construct(array $attributes = [])
  123. {
  124. foreach (self::$enums as &$enum) {
  125. $enum = $enum + array_flip($enum);
  126. }
  127. parent::__construct($attributes);
  128. }
  129. public function getIsNewRejectingAttribute($value)
  130. {
  131. if (!$value) return '';
  132. return self::$enums['is_new_rejecting'][$value];
  133. }
  134. public function setIsNewRejectingAttribute($value)
  135. {
  136. if (!$value) return;
  137. if (is_numeric($value)) {
  138. $this->attributes['is_new_rejecting'] = $value;
  139. } else {
  140. $this->attributes['is_new_rejecting'] = self::$enums['is_new_rejecting'][$value];
  141. }
  142. }
  143. public function getStatusAttribute($value)
  144. {
  145. if (!$value) return '';
  146. return self::$enums['status'][$value];
  147. }
  148. public function setStatusAttribute($value)
  149. {
  150. if (!$value) return;
  151. if (is_numeric($value)) {
  152. $this->attributes['status'] = $value;
  153. } else {
  154. $this->attributes['status'] = self::$enums['status'][$value];
  155. }
  156. }
  157. public function getLastStatusAttribute($value)
  158. {
  159. if (!$value) return '';
  160. return self::$enums['last_status'][$value];
  161. }
  162. public function setLastStatusAttribute($value)
  163. {
  164. if (!$value) return;
  165. if (is_numeric($value)) {
  166. $this->attributes['last_status'] = $value;
  167. } else {
  168. $this->attributes['last_status'] = self::$enums['last_status'][$value];
  169. }
  170. }
  171. // 关联订单
  172. public function order(): BelongsTo
  173. {
  174. return $this->belongsTo(Order::class);
  175. }
  176. // 关联货主
  177. public function owner(): BelongsTo
  178. {
  179. return $this->belongsTo(Owner::class, 'owner_id');
  180. }
  181. // 关联承运商
  182. public function logistic(): BelongsTo
  183. {
  184. return $this->belongsTo(Logistic::class);
  185. }
  186. // 创建人
  187. public function creator(): BelongsTo
  188. {
  189. return $this->belongsTo(User::class, 'creator_id');
  190. }
  191. public function lastHandler(): BelongsTo
  192. {
  193. return $this->belongsTo(User::class, 'last_handler_id');
  194. }
  195. // 审核人
  196. public function reviewer(): BelongsTo
  197. {
  198. return $this->belongsTo(User::class, 'reviewer_id');
  199. }
  200. // 生成问题件类型
  201. public function issueType(): BelongsTo
  202. {
  203. return $this->belongsTo(OrderIssueType::class, 'order_issue_type_id');
  204. }
  205. // 对应问题件
  206. public function orderIssue(): BelongsTo
  207. {
  208. return $this->belongsTo(OrderIssue::class, 'order_id', 'order_id');
  209. }
  210. public function details(): HasMany
  211. {
  212. return $this->hasMany(WorkOrderDetail::class);
  213. }
  214. public function orderDetail(): BelongsTo
  215. {
  216. return $this->belongsTo(OrderDetail::class,'order_id','order_id');
  217. }
  218. public function userWorkGroups(): BelongsToMany
  219. {
  220. return $this->belongsToMany(UserWorkgroup::class,'work_order_user_workgroup');
  221. }
  222. public function userOwnerGroup(): BelongsTo
  223. {
  224. return $this->BelongsTo(UserOwnerGroup::class);
  225. }
  226. public function detail(): BelongsTo
  227. {
  228. return $this->belongsTo(WorkOrderDetail::class,'work_order_detail_id','id');
  229. }
  230. public function scopeFilter($query, $filters)
  231. {
  232. return $filters->apply($query);
  233. }
  234. public function orderIssueRejectedBills(): HasMany
  235. {
  236. return $this->hasMany(OrderIssueRejectedBill::class,'order_id','order_id');
  237. }
  238. /** 默认 with 参数 */
  239. public function scopeDefaultWith($query)
  240. {
  241. $query->with($this->defaultWith());
  242. }
  243. public function defaultWith(): array
  244. {
  245. return ['owner', 'logistic', 'issueType', 'creator', 'lastHandler','orderDetail','orderIssueRejectedBills.rejectedBill.items',
  246. 'userWorkGroups','details' => function ($query) {
  247. return $query->with(['commodities.commodity', 'logs' => function ($query) {
  248. return $query->with('creator')->orderByDesc('created_at');
  249. }, 'images.uploadFile', 'issueType', 'processLogs' => function ($query) {
  250. return $query->with('user')->orderByDesc('created_at');
  251. }]);
  252. },
  253. 'reviewer',
  254. 'order'=> function($query){
  255. return $query->with('packages.commodities.commodity','shop');
  256. },
  257. 'orderIssue' => function ($query) {
  258. /** @var Builder $query */
  259. $query->with(['issueType', 'logs' => function ($query) {
  260. if (Gate::denies('订单管理-问题件-客户不可见')) {
  261. $query->with('user')->orderByDesc('created_at');
  262. } else {
  263. $query->with('user')->where('tag', '=', 0)->orderByDesc('created_at');
  264. }
  265. }]);
  266. }];
  267. }
  268. public function loadDefaultWith()
  269. {
  270. $this->loadMissing($this->defaultWith());
  271. }
  272. public function notification()
  273. {
  274. $user = Auth::user();
  275. $this->loadMissing('owner', 'order');
  276. $remark = $this->remark;
  277. $ownerName = $this->owner->name ?? '';
  278. $clientCode = $this->order->client_code ?? '';
  279. $msg = $user["name"] . "建立了新工单<br/>" . $ownerName . ":" . $clientCode . "<br/>" . $remark;
  280. NotificationService::SingleRegister($msg, $clientCode, "订单管理-问题件");
  281. }
  282. public function saveWorkOrderDetail($params)
  283. {
  284. $param = (new WorkOrderDetail($params))->getAttributes();
  285. $this->details()->create($param);
  286. $this->loadMissing('details');
  287. }
  288. // 工单完结
  289. public function end()
  290. {
  291. $this->update(['status' => 5, 'work_order_status' => 0]);
  292. }
  293. /**
  294. * 工单状态
  295. */
  296. public function changeStatus($status)
  297. {
  298. $this->status = $status;
  299. $this->update();
  300. }
  301. /**
  302. * 处理进度
  303. */
  304. public function changeProcessProgress($process_progress)
  305. {
  306. $this->update(['process_progress' => $process_progress]);
  307. }
  308. /**
  309. * 工单状态 and 处理状态 and 处理人
  310. * @param $status
  311. * @param $process_progress
  312. * @param $last_status
  313. */
  314. public function change($status, $process_progress, $last_status)
  315. {
  316. $this->last_status = $last_status;
  317. $this->status = $status;
  318. $this->process_progress = $process_progress;
  319. $this->last_handler_id = Auth::id();
  320. $this->update();
  321. }
  322. public function syncIntercept()
  323. {
  324. if ($this->status === '完成') return;
  325. /** @var WorkOrderDetail $detail */
  326. $detail = $this->details()->where('order_issue_type_id', $this->order_issue_type_id)
  327. ->whereIn('status', [1, 2, 3, 4, 5])
  328. ->orderByDesc('created_at')->first();
  329. if (!$detail) return;
  330. $process_progress = '成功已退回,不赔偿';
  331. $detail->update([
  332. 'status' => WorkOrder::$TO_BO_OWNER_END_STATUS,
  333. 'process_progress' => $process_progress,
  334. 'last_status' => WorkOrder::$BAO_SHI_REVIEW_STATUS,
  335. ]);
  336. $this->update([
  337. 'status' => WorkOrder::$TO_BO_OWNER_END_STATUS,
  338. 'process_progress' => $process_progress,
  339. 'last_status' => WorkOrder::$BAO_SHI_REVIEW_STATUS,
  340. 'owner_tag' => WorkOrder::$STRAND_TAG,
  341. 'logistic_tag' => WorkOrder::$DEFAULT_TAG,
  342. 'bao_shi_tag' => WorkOrder::$DEFAULT_TAG,
  343. ]);
  344. app(WorkOrderLogService::class)->createLog($detail, '终审', '自动终审');
  345. }
  346. }