WorkOrder.php 10 KB

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