123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- namespace App\Models\Service;
- use App\Models\Coach\User;
- use DateTimeInterface;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\DB;
- class Order extends Model
- {
- use HasFactory;
- protected $table = 'service_order';
- protected $guarded = [];
- protected $appends = [];
- protected $casts = [
- 'created_at' => 'datetime',
- 'pay_time' => 'datetime',
- 'start_time' => 'datetime',
- 'end_time' => 'datetime'
- ];
- protected function serializeDate(DateTimeInterface $date): string
- {
- return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
- }
- 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
- {
- if ($order_id) {
- $order = Order::query()->find($order_id);
- $project_id = $order['project_id'];
- // $use_balance = +$order['balance_price'] > 0 ? 1 : 0;
- }
- $data['project_id'] = $project_id;
- // 通过定位获取代理
- // 获取代理项目
- $project = Project::query()->find($data['project_id']);
- // 项目价格
- $data['service_price'] = number_format($project['price'], 2, '.', '');
- // $coupon_model= new Coupon();
- $coach_model = new User();
- // $car_config_model = new CarPrice();
- $coach = $coach_model->info($coach_id);
- // 优惠卷
- $data['coupon_id'] = $coupon_id ?: 0;
- // 优惠金额
- $data['coupon_price'] = number_format(0, 2, '.', '');
- // 折扣金额
- $data['discount_price'] = number_format(0, 2, '.', '');
- // 物料费
- $data['material_price'] = number_format(0, 2, '.', '');
- // $project['material_price']
- // 车费默认值
- $data['car_price'] = number_format(0, 2, '.', '');
- // 选择技师
- if ($coach) {
- $data['coach_id'] = $coach_id;
- // if ($lat && $lng) {
- // 订单距离
- // $data['distance'] = getDriveDistance($coach['lng'], $coach['lat'], $lng, $lat, $coach['uniacid']);
- // $data['distance'] += $data['car_config']['invented_distance'] * $data['distance'] / 100;
- //车费
- // $data['car_price'] = 0;
- // $data['car_price'] = $this->getCarPrice($data['distance'], $data['car_config'], $car_type);
- // }
- }
- // 距离
- if ($distance) {
- // 获取技师信息
- // 获取车费配置
- $baseCarPrice = 900;// 起步
- $baseDistance = 3; // 公里
- $carPrice = 300; // 每公里
- $freeDistance = 3; // 免费公里
- $distanceLen = $distance / 1000;
- $distancePrice = $distanceLen <= $freeDistance ? 0 : ($distanceLen > $baseDistance ? ($distanceLen - $baseDistance) * $carPrice + $baseCarPrice : $baseCarPrice);
- $data['car_price'] = number_format(+$distancePrice / 100, 2, '.', '');
- }
- // 订单支付价
- // 订单总价格
- $pay_total_price = round(+$project['price'], 2) + round(+$data['car_price'], 2) + round(+$data['material_price'], 2);
- // 订单总优惠
- $coupon_total_price = intval($data['coupon_price']) + intval($data['discount_price']);
- // 支付金额
- $pay_price = $pay_total_price - $coupon_total_price;
- $pay_price <= 0 && ($pay_price = 0);
- $data['pay_price'] = number_format($pay_price, 2, '.', '');
- // 余额支付
- $data['balance_price'] = number_format(0, 2, '.', '');
- if ($use_balance) {
- // 用户余额
- $memberQuery = \App\Models\Member\User::query();
- $user = $memberQuery->find($user_id);
- // 余额抵扣金额
- $balance_price = round(+$user['balance'], 2) - round($pay_price, 2);
- if ($balance_price >= 0) {
- $data['balance_price'] = number_format($pay_price, 2, '.', '');
- $data['pay_price'] = number_format(0, 2, '.', '');
- } else {
- $data['balance_price'] = $user['balance'];
- $data['pay_price'] = number_format(abs($balance_price), 2, '.', '');
- }
- }
- // 店铺
- // $data['store_id'] = $coach['store_id'];
- // 服务时长
- $data['time_long'] = $project['time_long'];
- return $data;
- }
- public function coach(): \Illuminate\Database\Eloquent\Relations\HasOne
- {
- return $this->hasOne(User::class, 'id', 'coach_id');
- }
- public function grab()
- {
- return $this->hasOne(OrderGrab::class, 'order_id')->where('status', 1);
- }
- public function getServicePriceAttribute($value): string
- {
- return number_format($value / 100, 2, '.', '');
- }
- public function getPayPriceAttribute($value): string
- {
- return number_format($value / 100, 2, '.', '');
- }
- public function getMaterialPriceAttribute($value): string
- {
- return number_format($value / 100, 2, '.', '');
- }
- public function getCouponPriceAttribute($value): string
- {
- return number_format($value / 100, 2, '.', '');
- }
- public function getDiscountPriceAttribute($value): string
- {
- return number_format($value / 100, 2, '.', '');
- }
- public function getBalancePriceAttribute($value): string
- {
- return number_format($value / 100, 2, '.', '');
- }
- public function getCarPriceAttribute($value): string
- {
- return number_format($value / 100, 2, '.', '');
- }
- }
|