WorkOrderDetail.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace App;
  3. use App\Traits\ModelTimeFormat;
  4. use App\Traits\ModelLogChanging;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  8. use Illuminate\Database\Eloquent\Relations\HasMany;
  9. use Illuminate\Support\Facades\Auth;
  10. class WorkOrderDetail extends Model
  11. {
  12. use ModelLogChanging;
  13. use ModelTimeFormat;
  14. protected $fillable = [
  15. 'work_order_id',
  16. 'order_issue_type_id', // 工单异常类型
  17. 'price', // 商品价值
  18. 'sku_amount', // 破损sku数
  19. 'type', // 快递异常填写:在途异常,签收未收到
  20. 'last_handler_id', // 上一个处理人
  21. 'reissue_logistic_number', // 补发单号
  22. 'return_logistic_number', // 退回单号 (错漏发:商家填写) (破损:创建时填写)
  23. 'process_progress', // 处理进度
  24. 'logistic_number', // 快递单号
  25. 'status', // 当前状态
  26. 'remark', // 创建工单时的问题标记
  27. 'tag', // 标记当前工单是否为历史标记
  28. 'receive_address', // 收方信息
  29. 'return_address', // 退回单 寄件人地址
  30. 'return_phone', // 退回单 寄件人联系号码
  31. 'return_name', // 退回单 寄件人姓名
  32. 'logistic_handle_tag', // 承运商在处理标记
  33. 'logistic_indemnity_money', // 承运商赔偿金额
  34. 'logistic_express_remission', // 承运商减免
  35. 'bao_shi_indemnity_money', // 宝时赔偿金额
  36. 'bao_shi_express_remission', // 宝时减免
  37. 'user_owner_group_id',
  38. ];
  39. static public $enums = [
  40. 'status' => [
  41. '' => 0,
  42. '宝时处理' => 1,
  43. '货主处理' => 2,
  44. '承运商处理' => 3,
  45. '宝时终审' => 4,
  46. '完成' => 5,
  47. '待货主完结' => 6,
  48. ],
  49. 'last_status' => [
  50. '' => 0,
  51. '宝时处理' => 1,
  52. '货主处理' => 2,
  53. '承运商处理' => 3,
  54. '宝时终审' => 4,
  55. '完成' => 5,
  56. '待货主完结' => 6,
  57. ],
  58. 'tag' => [
  59. '创建' => 0,
  60. '完成' => 1,
  61. '标记' => 2,
  62. ],
  63. 'last_handler' => [
  64. '' => 0,
  65. '商家' => 1,
  66. '承运商' => 2,
  67. '宝时' => 3,
  68. ],
  69. 'logistic_handle_tag' => [
  70. '' => 0,
  71. '承运商处理中' => 1,
  72. '取消标记' => 2
  73. ],
  74. ];
  75. function __construct(array $attributes = [])
  76. {
  77. foreach (self::$enums as &$enum) {
  78. $enum = $enum + array_flip($enum);
  79. }
  80. parent::__construct($attributes);
  81. }
  82. public function getLogisticHandleTagAttribute($value)
  83. {
  84. if (!$value) return '';
  85. return self::$enums['logistic_handle_tag'][$value];
  86. }
  87. public function setLogisticHandleTagAttribute($value)
  88. {
  89. if (!$value) return;
  90. if (is_numeric($value)) {
  91. $this->attributes['logistic_handle_tag'] = $value;
  92. } else {
  93. $this->attributes['logistic_handle_tag'] = self::$enums['logistic_handle_tag'][$value];
  94. }
  95. }
  96. public function getStatusAttribute($value)
  97. {
  98. if (!$value) return '';
  99. return self::$enums['status'][$value];
  100. }
  101. public function setStatusAttribute($value)
  102. {
  103. if (!$value) return;
  104. if (is_numeric($value)) {
  105. $this->attributes['status'] = $value;
  106. } else {
  107. $this->attributes['status'] = self::$enums['status'][$value];
  108. }
  109. }
  110. public function getTagAttribute($value)
  111. {
  112. if (is_numeric($value)) return self::$enums['tag'][$value];
  113. if (!$value) return '';
  114. return self::$enums['tag'][$value];
  115. }
  116. public function setTagAttribute($value)
  117. {
  118. if (!$value) return;
  119. if (is_numeric($value)) {
  120. $this->attributes['tag'] = $value;
  121. } else {
  122. $this->attributes['tag'] = self::$enums['tag'][$value];
  123. }
  124. }
  125. public function getLastStatusAttribute($value)
  126. {
  127. if (!$value) return '';
  128. return self::$enums['last_status'][$value];
  129. }
  130. public function setLastStatusAttribute($value)
  131. {
  132. if (!$value) return;
  133. if (is_numeric($value)) {
  134. $this->attributes['last_status'] = $value;
  135. } else {
  136. $this->attributes['last_status'] = self::$enums['last_status'][$value];
  137. }
  138. }
  139. public function workOrder(): BelongsTo
  140. {
  141. return $this->belongsTo(WorkOrder::class);
  142. }
  143. public function issueType(): BelongsTo
  144. {
  145. return $this->belongsTo(OrderIssueType::class, 'order_issue_type_id');
  146. }
  147. // 图片
  148. public function images(): HasMany
  149. {
  150. return $this->hasMany(WorkOrderImage::class);
  151. }
  152. public function getPackageImagesAttribute()
  153. {
  154. return $this->images->where('type', 1);
  155. }
  156. public function getCommodityImagesAttribute()
  157. {
  158. return $this->images->where('type', 2);
  159. }
  160. public function getDealImagesAttribute()
  161. {
  162. return $this->images->where('type', 3);
  163. }
  164. public function getRefundImagesAttribute()
  165. {
  166. return $this->images->where('type', 4);
  167. }
  168. public function logs(): HasMany
  169. {
  170. return $this->hasMany(WorkOrderLog::class);
  171. }
  172. public function commodities(): HasMany
  173. {
  174. return $this->hasMany(WorkOrderCommodities::class);
  175. }
  176. public function processLogs(): HasMany
  177. {
  178. return $this->hasMany(WorkOrderProcessLog::class,'work_order_detail_id','id');
  179. }
  180. public function userOwnerGroup(): BelongsTo
  181. {
  182. return $this->belongsTo(UserOwnerGroup::class);
  183. }
  184. public function userWorkgroups(): BelongsToMany
  185. {
  186. return $this->belongsToMany(UserWorkgroup::class);
  187. }
  188. // 未完成历史标记
  189. public function undoneTag()
  190. {
  191. if ($this->tag != '完成') {
  192. $this->update(['tag' => 2]);
  193. }
  194. }
  195. // 完成标记
  196. public function end()
  197. {
  198. $this->status = 5;
  199. $this->tag = 1;
  200. $this->update();
  201. }
  202. public function changeStatus($status)
  203. {
  204. $this->status = $status;
  205. $this->update();
  206. }
  207. public function changeProcessProgress($process_progress)
  208. {
  209. $this->update(['process_progress' => $process_progress]);
  210. }
  211. /**
  212. * 工单状态 and 处理状态
  213. * @param $status
  214. * @param $process_progress
  215. * @param $last_status
  216. */
  217. public function change($status, $process_progress, $last_status)
  218. {
  219. $user = Auth::user();
  220. $this->status = $status;
  221. $this->last_status = $last_status;
  222. $this->process_progress = $process_progress;
  223. $this->last_handler_id = $user['id'] ?? '';
  224. $this->update();
  225. }
  226. public function logisticTagHandle()
  227. {
  228. $this->update(['logistic_handle_tag' => 1]);
  229. }
  230. public function cancelLogisticTagHandle()
  231. {
  232. $this->logistic_handle_tag = '取消标记';
  233. $this->update();
  234. }
  235. }