WorkOrderDetail.php 7.3 KB

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