WorkOrder.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 ModelLogChanging;
  18. use ModelTimeFormat;
  19. use SoftDeletes;
  20. // 工单 信息
  21. protected $fillable = [
  22. 'owner_id', // 货主
  23. 'logistic_id', // 承运商
  24. 'order_issue_type_id', // 问题件类型
  25. 'status', // 审核状态
  26. 'creator_id', // 创建人
  27. 'reviewer_id', // 审核人
  28. 'work_order_type_id', // 工单类型
  29. 'review_at', // 审核时间
  30. 'order_id', // 订单id
  31. 'uniquely_tag', // 唯一标识
  32. // 'grad', // 紧急等级
  33. // 'remark', // 工单信息描述
  34. // 'outer_table_name', // 链接表名
  35. // 'work_order_status', // 工单状态
  36. ];
  37. static public $enums = [
  38. 'status' => [
  39. '' => 0,
  40. '宝时处理' => 1,
  41. '货主处理' => 2,
  42. '承运商处理' => 3,
  43. ],
  44. // 'work_order_status' => [
  45. // '' => 0,
  46. // '创建' => 1,
  47. // '信息已填写' => 2,
  48. // '快递已处理' => 3,
  49. // '工单完成' => 4,
  50. // ],
  51. // 'grad' => [
  52. // '' => 0,
  53. // '一般' => 1,
  54. // '重要' => 2,
  55. // '紧急' => 3,
  56. // '重要且紧急' => 4,
  57. // ],
  58. ];
  59. function __construct(array $attributes = [])
  60. {
  61. foreach (self::$enums as &$enum) {
  62. $enum = $enum + array_flip($enum);
  63. }
  64. parent::__construct($attributes);
  65. }
  66. public function getStatusAttribute($value)
  67. {
  68. if (!$value) return '';
  69. return self::$enums['status'][$value];
  70. }
  71. public function setStatusAttribute($value)
  72. {
  73. if (!$value) return;
  74. if (is_numeric($value)) {
  75. $this->attributes['status'] = $value;
  76. } else {
  77. $this->attributes['status'] = self::$enums['status'][$value];
  78. }
  79. }
  80. // public function getWorkOrderStatusAttribute($value)
  81. // {
  82. // if (!$value) return '';
  83. // return self::$enums['work_order_status'][$value];
  84. // }
  85. //
  86. // public function setWorkOrderStatusAttribute($value)
  87. // {
  88. // if (!$value) return ;
  89. // if (is_numeric($value)) {
  90. // $this->attributes['work_order_status'] = $value;
  91. // } else {
  92. // $this->attributes['work_order_status'] = self::$enums['work_order_status'][$value];
  93. // }
  94. // }
  95. //
  96. // public function getGradAttribute($value)
  97. // {
  98. // if (!$value) return '';
  99. // return self::$enums['grad'][$value];
  100. // }
  101. //
  102. // public function setGradAttribute($value)
  103. // {
  104. // if (!$value) return ;
  105. // if (is_numeric($value)) {
  106. // $this->attributes['grad'] = $value;
  107. // } else {
  108. // $this->attributes['grad'] = self::$enums['grad'][$value];
  109. // }
  110. // }
  111. // 关联订单
  112. public function order(): BelongsTo
  113. {
  114. return $this->belongsTo(Order::class);
  115. }
  116. // 关联货主
  117. public function owner(): BelongsTo
  118. {
  119. return $this->belongsTo(Owner::class, 'owner_id');
  120. }
  121. // 关联承运商
  122. public function logistic(): BelongsTo
  123. {
  124. return $this->belongsTo(Logistic::class);
  125. }
  126. // 创建人
  127. public function creator(): BelongsTo
  128. {
  129. return $this->belongsTo(User::class, 'creator_id');
  130. }
  131. // 审核人
  132. public function reviewer(): BelongsTo
  133. {
  134. return $this->belongsTo(User::class, 'reviewer_id');
  135. }
  136. // 工单类型
  137. public function type(): BelongsTo
  138. {
  139. return $this->BelongsTo(WorkOrderType::class, 'work_order_type_id');
  140. }
  141. // 生成问题件类型
  142. public function issueType(): BelongsTo
  143. {
  144. return $this->belongsTo(OrderIssueType::class, 'order_issue_type_id');
  145. }
  146. // 对应问题件
  147. public function orderIssue(): BelongsTo
  148. {
  149. return $this->belongsTo(OrderIssue::class, 'order_id', 'order_id');
  150. }
  151. // 图片
  152. public function images(): HasMany
  153. {
  154. return $this->hasMany(WorkOrderImage::class);
  155. }
  156. public function getPackageImagesAttribute()
  157. {
  158. return $this->images->where('type', 1);
  159. }
  160. public function getCommodityImagesAttribute()
  161. {
  162. return $this->images->where('type', 2);
  163. }
  164. public function getDealImagesAttribute()
  165. {
  166. return $this->images->where('type', 3);
  167. }
  168. public function getRefundImagesAttribute()
  169. {
  170. return $this->images->where('type', 4);
  171. }
  172. // 工单详情
  173. public function details(): HasMany
  174. {
  175. return $this->hasMany(WorkOrderDetail::class);
  176. }
  177. // 工单商品信息
  178. public function commodities(): HasMany
  179. {
  180. return $this->hasMany(WorkOrderCommodities::class);
  181. }
  182. // 处理日志
  183. public function processLogs(): HasMany
  184. {
  185. return $this->hasMany(WorkOrderProcessLog::class);
  186. }
  187. public function baoShiLog()
  188. {
  189. return $this->processLogs->where('type', 1);
  190. }
  191. public function logisticLog()
  192. {
  193. return $this->processLogs->where('type', 2);
  194. }
  195. public function scopeFilter($query, $filters)
  196. {
  197. return $filters->apply($query);
  198. }
  199. /** 默认 with 参数 */
  200. public function scopeDefaultWith($query)
  201. {
  202. $query->with(['type', 'owner', 'logistic', 'issueType', 'creator', 'details', 'commodities.commodity',
  203. 'processLogs.creator',
  204. 'images.uploadFile',
  205. 'reviewer',
  206. 'order.packages',
  207. 'orderIssue' => function ($query) {
  208. /** @var Builder $query */
  209. $query->with(['issueType', 'logs' => function ($query) {
  210. if (Gate::denies('订单管理-问题件-客户不可见')) {
  211. $query->with('user')->orderByDesc('created_at');
  212. } else {
  213. $query->with('user')->where('tag', '=', 0)->orderByDesc('created_at');
  214. }
  215. }]);
  216. }]);
  217. }
  218. public function loadDefaultWith()
  219. {
  220. $this->loadMissing(['type', 'owner', 'logistic', 'issueType', 'creator', 'details', 'commodities.commodity',
  221. 'baoShiLog.creator',
  222. 'images.uploadFile',
  223. 'reviewer',
  224. 'order.packages',
  225. 'orderIssue' => function ($query) {
  226. /** @var Builder $query */
  227. $query->with(['issueType', 'logs' => function ($query) {
  228. if (Gate::denies('订单管理-问题件-客户不可见')) {
  229. $query->with('user')->orderByDesc('created_at');
  230. } else {
  231. $query->with('user')->where('tag', '=', 0)->orderByDesc('created_at');
  232. }
  233. }]);
  234. }]);
  235. }
  236. public function notification()
  237. {
  238. $user = Auth::user();
  239. $this->loadMissing('owner', 'order');
  240. $remark = $this->remark;
  241. $ownerName = $this->owner->name ?? '';
  242. $clientCode = $this->order->client_code ?? '';
  243. $msg = $user["name"] . "建立了新工单<br/>" . $ownerName . ":" . $clientCode . "<br/>" . $remark;
  244. NotificationService::SingleRegister($msg, $clientCode, "订单管理-问题件");
  245. }
  246. public function saveWorkOrderDetail($params)
  247. {
  248. $param = (new WorkOrderDetail($params))->getAttributes();
  249. $this->details()->create($param);
  250. $this->loadMissing('details');
  251. }
  252. // 工单完结
  253. public function end()
  254. {
  255. $this->update(['ststus' => 4]);
  256. }
  257. public function changeStatus($status)
  258. {
  259. $this->status = $status;
  260. $this->update();
  261. }
  262. }