WorkOrder.php 8.3 KB

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