|
|
@@ -95,7 +95,7 @@ class OrderService
|
|
|
} else if ($user->plan_id !== NULL && $order->plan_id !== $user->plan_id && ($user->expired_at > time() || $user->expired_at === NULL)) {
|
|
|
if (!(int)config('v2board.plan_change_enable', 1)) abort(500, '目前不允许更改订阅,请联系客服或提交工单操作');
|
|
|
$order->type = 3;
|
|
|
- if ((int)config('v2board.surplus_enable', 1)) $this->getSurplusValue($user, $order);
|
|
|
+ $this->getSurplusValue($user, $order);
|
|
|
if ($order->surplus_amount >= $order->total_amount) {
|
|
|
$order->refund_amount = $order->surplus_amount - $order->total_amount;
|
|
|
$order->total_amount = 0;
|
|
|
@@ -156,63 +156,26 @@ class OrderService
|
|
|
|
|
|
private function getSurplusValue(User $user, Order $order)
|
|
|
{
|
|
|
- if ($user->expired_at === NULL) {
|
|
|
- $this->getSurplusValueByOneTime($user, $order);
|
|
|
- } else {
|
|
|
- $this->getSurplusValueByPeriod($user, $order);
|
|
|
- }
|
|
|
+ $plan = Plan::find($user->plan_id);
|
|
|
+ if (!$plan) return;
|
|
|
+ if ($user->expired_at) $this->getSurplusValueByTime($user, $order, $plan);
|
|
|
+ $this->getSurplusValueByTransfer($user, $order, $plan);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- private function getSurplusValueByOneTime(User $user, Order $order)
|
|
|
+ private function getSurplusValueByTime(User $user, Order $order, Plan $plan)
|
|
|
{
|
|
|
- $lastOneTimeOrder = Order::where('user_id', $user->id)
|
|
|
- ->where('period', 'onetime_price')
|
|
|
- ->where('status', 3)
|
|
|
- ->orderBy('id', 'DESC')
|
|
|
- ->first();
|
|
|
- if (!$lastOneTimeOrder) return;
|
|
|
- $nowUserTraffic = $user->transfer_enable / 1073741824;
|
|
|
- if (!$nowUserTraffic) return;
|
|
|
- $paidTotalAmount = ($lastOneTimeOrder->total_amount + $lastOneTimeOrder->balance_amount);
|
|
|
- if (!$paidTotalAmount) return;
|
|
|
- $trafficUnitPrice = $paidTotalAmount / $nowUserTraffic;
|
|
|
- $notUsedTraffic = $nowUserTraffic - (($user->u + $user->d) / 1073741824);
|
|
|
- $result = $trafficUnitPrice * $notUsedTraffic;
|
|
|
- $orderModel = Order::where('user_id', $user->id)->where('period', '!=', 'reset_price')->where('status', 3);
|
|
|
- $order->surplus_amount = $result > 0 ? $result : 0;
|
|
|
- $order->surplus_order_ids = array_column($orderModel->get()->toArray(), 'id');
|
|
|
+ if (!$plan['daily_unit_price']) return;
|
|
|
+ $timeLeftDays = $user['expired_at'] - time() / 86400;
|
|
|
+ if (!$timeLeftDays) return;
|
|
|
+ $order->surplus_amount = $order->surplus_amount + ($timeLeftDays * $plan['daily_unit_price']);
|
|
|
}
|
|
|
|
|
|
- private function getSurplusValueByPeriod(User $user, Order $order)
|
|
|
+ private function getSurplusValueByTransfer(User $user, Order $order, Plan $plan)
|
|
|
{
|
|
|
- $orders = Order::where('user_id', $user->id)
|
|
|
- ->where('period', '!=', 'reset_price')
|
|
|
- ->where('period', '!=', 'onetime_price')
|
|
|
- ->where('status', 3)
|
|
|
- ->get()
|
|
|
- ->toArray();
|
|
|
- if (!$orders) return;
|
|
|
- $orderAmountSum = 0;
|
|
|
- $orderMonthSum = 0;
|
|
|
- $lastValidateAt = 0;
|
|
|
- foreach ($orders as $item) {
|
|
|
- $period = self::STR_TO_TIME[$item['period']];
|
|
|
- if (strtotime("+{$period} month", $item['created_at']) < time()) continue;
|
|
|
- $lastValidateAt = $item['created_at'];
|
|
|
- $orderMonthSum = $period + $orderMonthSum;
|
|
|
- $orderAmountSum = $orderAmountSum + ($item['total_amount'] + $item['balance_amount'] + $item['surplus_amount'] - $item['refund_amount']);
|
|
|
- }
|
|
|
- if (!$lastValidateAt) return;
|
|
|
- $expiredAtByOrder = strtotime("+{$orderMonthSum} month", $lastValidateAt);
|
|
|
- if ($expiredAtByOrder < time()) return;
|
|
|
- $orderSurplusSecond = $expiredAtByOrder - time();
|
|
|
- $orderRangeSecond = $expiredAtByOrder - $lastValidateAt;
|
|
|
- $avgPrice = $orderAmountSum / $orderRangeSecond;
|
|
|
- $orderSurplusAmount = $avgPrice * $orderSurplusSecond;
|
|
|
- if (!$orderSurplusSecond || !$orderSurplusAmount) return;
|
|
|
- $order->surplus_amount = $orderSurplusAmount > 0 ? $orderSurplusAmount : 0;
|
|
|
- $order->surplus_order_ids = array_column($orders, 'id');
|
|
|
+ if (!$plan['transfer_unit_price']) return;
|
|
|
+ $transferLeft = ($user['transfer_enable'] - $user['u'] + $user['d']) / 1073741824;
|
|
|
+ if (!$transferLeft) return;
|
|
|
+ $order->surplus_amount = $order->surplus_amount + ($transferLeft * $plan['transfer_unit_price']);
|
|
|
}
|
|
|
|
|
|
public function paid(string $callbackNo)
|