Order.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace App\Models\Service;
  3. use App\Models\Coach\User;
  4. use DateTimeInterface;
  5. use Illuminate\Database\Eloquent\Factories\HasFactory;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Support\Facades\DB;
  8. class Order extends Model
  9. {
  10. use HasFactory;
  11. protected $table = 'service_order';
  12. protected $guarded = [];
  13. protected $appends = [];
  14. protected $casts = [
  15. 'created_at' => 'datetime',
  16. 'pay_time' => 'datetime',
  17. 'start_time' => 'datetime',
  18. 'end_time' => 'datetime'
  19. ];
  20. protected function serializeDate(DateTimeInterface $date): string
  21. {
  22. return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
  23. }
  24. public function buildPayInfo($user_id, $project_id, $use_balance = 0, $coach_id = null, $car_type = 0, $coupon_id = 0, $order_id = 0, $distance = 0): array
  25. {
  26. if ($order_id) {
  27. $order = Order::query()->find($order_id);
  28. $project_id = $order['project_id'];
  29. $use_balance = +$order['balance_price'] > 0 ? 1 : 0;
  30. }
  31. $data['project_id'] = $project_id;
  32. // 通过定位获取代理
  33. // 获取代理项目
  34. $project = Project::query()->find($data['project_id']);
  35. // 项目价格
  36. $data['service_price'] = number_format($project['price'], 2, '.', '');
  37. // $coupon_model= new Coupon();
  38. $coach_model = new User();
  39. // $car_config_model = new CarPrice();
  40. $coach = $coach_model->info($coach_id);
  41. // 优惠卷
  42. $data['coupon_id'] = $coupon_id ?: 0;
  43. // 优惠金额
  44. $data['coupon_price'] = number_format(0, 2, '.', '');
  45. // 折扣金额
  46. $data['discount_price'] = number_format(0, 2, '.', '');
  47. // 物料费
  48. $data['material_price'] = number_format(0, 2, '.', '');
  49. // $project['material_price']
  50. // 车费默认值
  51. $data['car_price'] = number_format(0, 2, '.', '');
  52. // 选择技师
  53. if ($coach) {
  54. $data['coach_id'] = $coach_id;
  55. // if ($lat && $lng) {
  56. // 订单距离
  57. // $data['distance'] = getDriveDistance($coach['lng'], $coach['lat'], $lng, $lat, $coach['uniacid']);
  58. // $data['distance'] += $data['car_config']['invented_distance'] * $data['distance'] / 100;
  59. //车费
  60. // $data['car_price'] = 0;
  61. // $data['car_price'] = $this->getCarPrice($data['distance'], $data['car_config'], $car_type);
  62. // }
  63. }
  64. // 距离
  65. if ($distance) {
  66. // 获取技师信息
  67. // 获取车费配置
  68. $baseCarPrice = 900;// 起步
  69. $baseDistance = 3; // 公里
  70. $carPrice = 300; // 每公里
  71. $freeDistance = 3; // 免费公里
  72. $distanceLen = $distance / 1000;
  73. $distancePrice = $distanceLen <= $freeDistance ? 0 : ($distanceLen > $baseDistance ? ($distanceLen - $baseDistance) * $carPrice + $baseCarPrice : $baseCarPrice);
  74. $data['car_price'] = number_format(+$distancePrice / 100, 2, '.', '');
  75. }
  76. // 订单支付价
  77. // 订单总价格
  78. $pay_total_price = round(+$project['price'], 2) + round(+$data['car_price'], 2) + round(+$data['material_price'], 2);
  79. // 订单总优惠
  80. $coupon_total_price = intval($data['coupon_price']) + intval($data['discount_price']);
  81. // 支付金额
  82. $pay_price = $pay_total_price - $coupon_total_price;
  83. $pay_price <= 0 && ($pay_price = 0);
  84. $data['pay_price'] = number_format($pay_price, 2, '.', '');
  85. // 余额支付
  86. $data['balance_price'] = number_format(0, 2, '.', '');
  87. if ($use_balance) {
  88. // 用户余额
  89. $memberQuery = \App\Models\Member\User::query();
  90. $user = $memberQuery->find($user_id);
  91. // 余额抵扣金额
  92. $balance_price = round(+$user['balance'], 2) - round($pay_price, 2);
  93. if ($balance_price >= 0) {
  94. $data['balance_price'] = number_format($pay_price, 2, '.', '');
  95. $data['pay_price'] = number_format(0, 2, '.', '');
  96. } else {
  97. $data['balance_price'] = $user['balance'];
  98. $data['pay_price'] = number_format(abs($balance_price), 2, '.', '');
  99. }
  100. }
  101. // 店铺
  102. // $data['store_id'] = $coach['store_id'];
  103. // 服务时长
  104. $data['time_long'] = $project['time_long'];
  105. return $data;
  106. }
  107. public function coach(): \Illuminate\Database\Eloquent\Relations\HasOne
  108. {
  109. return $this->hasOne(User::class, 'id', 'coach_id');
  110. }
  111. public function grab()
  112. {
  113. return $this->hasOne(OrderGrab::class, 'order_id')->where('status', 1);
  114. }
  115. public function getServicePriceAttribute($value): string
  116. {
  117. return number_format($value / 100, 2, '.', '');
  118. }
  119. public function getPayPriceAttribute($value): string
  120. {
  121. return number_format($value / 100, 2, '.', '');
  122. }
  123. public function getMaterialPriceAttribute($value): string
  124. {
  125. return number_format($value / 100, 2, '.', '');
  126. }
  127. public function getCouponPriceAttribute($value): string
  128. {
  129. return number_format($value / 100, 2, '.', '');
  130. }
  131. public function getDiscountPriceAttribute($value): string
  132. {
  133. return number_format($value / 100, 2, '.', '');
  134. }
  135. public function getBalancePriceAttribute($value): string
  136. {
  137. return number_format($value / 100, 2, '.', '');
  138. }
  139. public function getCarPriceAttribute($value): string
  140. {
  141. return number_format($value / 100, 2, '.', '');
  142. }
  143. }