WorkOrder.php 11 KB

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