WorkOrder.php 12 KB

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