WorkOrder.php 8.3 KB

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