WorkOrder.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. class WorkOrder extends Model
  16. {
  17. use ModelTimeFormat;
  18. use SoftDeletes;
  19. // 工单 信息
  20. protected $fillable = [
  21. 'order_id', // 订单id
  22. 'owner_id', // 货主
  23. 'logistic_id', // 承运商
  24. 'creator_id', // 创建人
  25. 'reviewer_id', // 审核人
  26. 'last_handler_id', // 上一个处理人
  27. 'order_issue_type_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. ];
  43. static public $process_progress = [
  44. '已处理','已签收', // 拦截 承运商
  45. '拦截成功','拦截失败', // 宝时处理
  46. '已处理','无法更改', // 信息更改 承运商
  47. '更改成功','更改失败', // 宝时处理
  48. '已处理','已拦截', // 快递异常 承运商
  49. '丢件赔偿','签收成功', // 宝时处理
  50. '已核实', // 错漏发 宝时处理
  51. '补发','不补发', // 商家处理
  52. ''
  53. ];
  54. static public $enums = [
  55. 'status' => [
  56. '' => 0,
  57. '宝时处理' => 1,
  58. '货主处理' => 2,
  59. '承运商处理' => 3,
  60. '宝时终审' => 4,
  61. '完成' => 5,
  62. '待货主完结' => 6,
  63. ],
  64. 'last_status' => [
  65. '' => 0,
  66. '宝时处理' => 1,
  67. '货主处理' => 2,
  68. '承运商处理' => 3,
  69. '宝时终审' => 4,
  70. '完成' => 5,
  71. '待货主完结' => 6,
  72. ],
  73. ];
  74. function __construct(array $attributes = [])
  75. {
  76. foreach (self::$enums as &$enum) {
  77. $enum = $enum + array_flip($enum);
  78. }
  79. parent::__construct($attributes);
  80. }
  81. public function getStatusAttribute($value)
  82. {
  83. if (!$value) return '';
  84. return self::$enums['status'][$value];
  85. }
  86. public function setStatusAttribute($value)
  87. {
  88. if (!$value) return;
  89. if (is_numeric($value)) {
  90. $this->attributes['status'] = $value;
  91. } else {
  92. $this->attributes['status'] = self::$enums['status'][$value];
  93. }
  94. }
  95. public function getLastStatusAttribute($value)
  96. {
  97. if (!$value) return '';
  98. return self::$enums['last_status'][$value];
  99. }
  100. public function setLastStatusAttribute($value)
  101. {
  102. if (!$value) return;
  103. if (is_numeric($value)) {
  104. $this->attributes['last_status'] = $value;
  105. } else {
  106. $this->attributes['last_status'] = self::$enums['last_status'][$value];
  107. }
  108. }
  109. // 关联订单
  110. public function order(): BelongsTo
  111. {
  112. return $this->belongsTo(Order::class);
  113. }
  114. // 关联货主
  115. public function owner(): BelongsTo
  116. {
  117. return $this->belongsTo(Owner::class, 'owner_id');
  118. }
  119. // 关联承运商
  120. public function logistic(): BelongsTo
  121. {
  122. return $this->belongsTo(Logistic::class);
  123. }
  124. // 创建人
  125. public function creator(): BelongsTo
  126. {
  127. return $this->belongsTo(User::class, 'creator_id');
  128. }
  129. public function lastHandler(): BelongsTo
  130. {
  131. return $this->belongsTo(User::class,'last_handler_id');
  132. }
  133. // 审核人
  134. public function reviewer(): BelongsTo
  135. {
  136. return $this->belongsTo(User::class, 'reviewer_id');
  137. }
  138. // 生成问题件类型
  139. public function issueType(): BelongsTo
  140. {
  141. return $this->belongsTo(OrderIssueType::class, 'order_issue_type_id');
  142. }
  143. // 对应问题件
  144. public function orderIssue(): BelongsTo
  145. {
  146. return $this->belongsTo(OrderIssue::class, 'order_id', 'order_id');
  147. }
  148. public function details(): HasMany
  149. {
  150. return $this->hasMany(WorkOrderDetail::class);
  151. }
  152. public function scopeFilter($query, $filters)
  153. {
  154. return $filters->apply($query);
  155. }
  156. /** 默认 with 参数 */
  157. public function scopeDefaultWith($query)
  158. {
  159. $query->with($this->defaultWith());
  160. }
  161. public function defaultWith(): array
  162. {
  163. return ['owner', 'logistic', 'issueType', 'creator', 'lastHandler','details' => function ($query) {
  164. return $query->with('commodities.commodity', 'logs.creator', 'images.uploadFile','issueType');
  165. },
  166. 'reviewer',
  167. 'order.packages',
  168. 'orderIssue' => function ($query) {
  169. /** @var Builder $query */
  170. $query->with(['issueType', 'logs' => function ($query) {
  171. if (Gate::denies('订单管理-问题件-客户不可见')) {
  172. $query->with('user')->orderByDesc('created_at');
  173. } else {
  174. $query->with('user')->where('tag', '=', 0)->orderByDesc('created_at');
  175. }
  176. }]);
  177. }];
  178. }
  179. public function loadDefaultWith()
  180. {
  181. $this->loadMissing($this->defaultWith());
  182. }
  183. public function notification()
  184. {
  185. $user = Auth::user();
  186. $this->loadMissing('owner', 'order');
  187. $remark = $this->remark;
  188. $ownerName = $this->owner->name ?? '';
  189. $clientCode = $this->order->client_code ?? '';
  190. $msg = $user["name"] . "建立了新工单<br/>" . $ownerName . ":" . $clientCode . "<br/>" . $remark;
  191. NotificationService::SingleRegister($msg, $clientCode, "订单管理-问题件");
  192. }
  193. public function saveWorkOrderDetail($params)
  194. {
  195. $param = (new WorkOrderDetail($params))->getAttributes();
  196. $this->details()->create($param);
  197. $this->loadMissing('details');
  198. }
  199. // 工单完结
  200. public function end()
  201. {
  202. $this->update(['status' => 5,'work_order_status' => 0]);
  203. }
  204. /**
  205. * 工单状态
  206. */
  207. public function changeStatus($status)
  208. {
  209. $this->status = $status;
  210. $this->update();
  211. }
  212. /**
  213. * 处理进度
  214. */
  215. public function changeProcessProgress($process_progress){
  216. $this->update(['process_progress' => $process_progress]);
  217. }
  218. /**
  219. * 工单状态 and 处理状态 and 处理人
  220. * @param $status
  221. * @param $process_progress
  222. * @param $last_status
  223. */
  224. public function change($status,$process_progress,$last_status){
  225. $user = Auth::user();
  226. $this->last_status = $last_status;
  227. $this->status = $status;
  228. $this->process_progress = $process_progress;
  229. $this->last_handler_id = $user['id'] ?? '';
  230. $this->update();
  231. }
  232. /**
  233. * 清除创建标记
  234. */
  235. public function clearWorkOrderStatus(){
  236. $this->update(['work_order_status' => 0]);
  237. }
  238. }