调整砍价
parent
7c278507eb
commit
82807fdfae
|
|
@ -20,6 +20,7 @@ class OrderVerifyController extends Controller
|
||||||
$rules = [
|
$rules = [
|
||||||
'coupon_id' => ['bail', 'nullable', 'int'],
|
'coupon_id' => ['bail', 'nullable', 'int'],
|
||||||
'shipping_address_id' => ['bail', 'nullable', 'int'],
|
'shipping_address_id' => ['bail', 'nullable', 'int'],
|
||||||
|
'baragain_order_id' => ['bail', 'nullable', 'int'],
|
||||||
];
|
];
|
||||||
|
|
||||||
// 快速下单
|
// 快速下单
|
||||||
|
|
@ -40,6 +41,7 @@ class OrderVerifyController extends Controller
|
||||||
'product.quantity' => '数量',
|
'product.quantity' => '数量',
|
||||||
'coupon_id' => '优惠券',
|
'coupon_id' => '优惠券',
|
||||||
'shipping_address_id' => '收货地址',
|
'shipping_address_id' => '收货地址',
|
||||||
|
'bargain_order_id'=> '砍价',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$user = $request->user();
|
$user = $request->user();
|
||||||
|
|
@ -52,6 +54,7 @@ class OrderVerifyController extends Controller
|
||||||
$request->input('product.quantity'),
|
$request->input('product.quantity'),
|
||||||
$request->input('shipping_address_id'),
|
$request->input('shipping_address_id'),
|
||||||
$request->input('coupon_id'),
|
$request->input('coupon_id'),
|
||||||
|
$request->input('bargain_order_id'),
|
||||||
) : $orderService->verifyShoppingCartOrder(
|
) : $orderService->verifyShoppingCartOrder(
|
||||||
$user,
|
$user,
|
||||||
$request->input('shopping_cart'),
|
$request->input('shopping_cart'),
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Exceptions\BizException;
|
|
||||||
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
||||||
use Illuminate\Auth\Authenticatable;
|
use Illuminate\Auth\Authenticatable;
|
||||||
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
|
||||||
|
|
@ -410,6 +409,14 @@ class User extends Model implements AuthorizableContract, AuthenticatableContrac
|
||||||
return $this->belongsToMany(User::class, 'user_infos', 'inviter_id', 'user_id');
|
return $this->belongsToMany(User::class, 'user_infos', 'inviter_id', 'user_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户的砍价单
|
||||||
|
*/
|
||||||
|
public function bargainOrders()
|
||||||
|
{
|
||||||
|
return $this->hasMany(BargainOrder::class);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取我的粉丝总数
|
* 获取我的粉丝总数
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -253,8 +253,8 @@ class OrderService
|
||||||
$sku = $product['sku'];
|
$sku = $product['sku'];
|
||||||
$qty = $product['quantity'];
|
$qty = $product['quantity'];
|
||||||
|
|
||||||
// 支付金额 = 商品总额 - 优惠券折扣金额- 会员折扣金额
|
// 支付金额 = 商品总额 - 优惠券折扣金额- 会员折扣金额 - 砍价金额
|
||||||
$totalAmount = $product['total_amount'] - $product['coupon_discount_amount'] - $product['vip_discount_amount'];
|
$totalAmount = $product['total_amount'] - $product['coupon_discount_amount'] - $product['vip_discount_amount'] - $product['bargain_amount'];
|
||||||
|
|
||||||
$orderProducts[] = [
|
$orderProducts[] = [
|
||||||
'gift_for_sku_id' => null,
|
'gift_for_sku_id' => null,
|
||||||
|
|
@ -277,6 +277,7 @@ class OrderService
|
||||||
'coupon_discount_amount' => $product['coupon_discount_amount'],
|
'coupon_discount_amount' => $product['coupon_discount_amount'],
|
||||||
'vip_discount_amount' => $product['vip_discount_amount'],
|
'vip_discount_amount' => $product['vip_discount_amount'],
|
||||||
'total_amount' => $totalAmount,
|
'total_amount' => $totalAmount,
|
||||||
|
'bargain_amount'=> $product['bargain_amount'],
|
||||||
'created_at' => $order->created_at,
|
'created_at' => $order->created_at,
|
||||||
'updated_at' => $order->updated_at,
|
'updated_at' => $order->updated_at,
|
||||||
];
|
];
|
||||||
|
|
@ -466,6 +467,7 @@ class OrderService
|
||||||
'coupon_discount_amount' => 0,
|
'coupon_discount_amount' => 0,
|
||||||
'vip_discount_amount' => 0,
|
'vip_discount_amount' => 0,
|
||||||
'total_amount' => 0,
|
'total_amount' => 0,
|
||||||
|
'bargain_amount'=> 0,
|
||||||
'created_at' => $order->created_at,
|
'created_at' => $order->created_at,
|
||||||
'updated_at' => $order->updated_at,
|
'updated_at' => $order->updated_at,
|
||||||
'is_gift'=> true,
|
'is_gift'=> true,
|
||||||
|
|
@ -495,9 +497,10 @@ class OrderService
|
||||||
* @param int $quantity
|
* @param int $quantity
|
||||||
* @param int|null $shippingAddressId
|
* @param int|null $shippingAddressId
|
||||||
* @param int|null $couponId
|
* @param int|null $couponId
|
||||||
|
* @param int|null $bargainOrderId
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function verifyQuickOrder(User $user, int $skuId, int $quantity, ?int $shippingAddressId = null, ?int $couponId = null)
|
public function verifyQuickOrder(User $user, int $skuId, int $quantity, ?int $shippingAddressId = null, ?int $couponId = null, ?int $bargainOrderId = null)
|
||||||
{
|
{
|
||||||
$sku = ProductSku::online()->findOrFail($skuId);
|
$sku = ProductSku::online()->findOrFail($skuId);
|
||||||
|
|
||||||
|
|
@ -506,7 +509,7 @@ class OrderService
|
||||||
'quantity' => $quantity,
|
'quantity' => $quantity,
|
||||||
];
|
];
|
||||||
|
|
||||||
return $this->verifyOrder($user, [$product], $shippingAddressId, $couponId);
|
return $this->verifyOrder($user, [$product], $shippingAddressId, $couponId, $bargainOrderId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -533,9 +536,10 @@ class OrderService
|
||||||
* @param array $products
|
* @param array $products
|
||||||
* @param int|null $shippingAddressId
|
* @param int|null $shippingAddressId
|
||||||
* @param int|null $couponId
|
* @param int|null $couponId
|
||||||
|
* @param int|null $bargainOrderId
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function verifyOrder(User $user, array $products, ?int $shippingAddressId = null, ?int $couponId = null): array
|
protected function verifyOrder(User $user, array $products, ?int $shippingAddressId = null, ?int $couponId = null, ?int $bargainOrderId = null): array
|
||||||
{
|
{
|
||||||
// 获取收货地址
|
// 获取收货地址
|
||||||
$shippingAddress = $this->getShippingAddress($user, $shippingAddressId);
|
$shippingAddress = $this->getShippingAddress($user, $shippingAddressId);
|
||||||
|
|
@ -547,7 +551,12 @@ class OrderService
|
||||||
$coupon = $user->coupons()->onlyAvailable()->findOrFail($couponId);
|
$coupon = $user->coupons()->onlyAvailable()->findOrFail($couponId);
|
||||||
}
|
}
|
||||||
|
|
||||||
$mapProducts = $this->mapProducts($user, $products, $coupon);
|
$bargainOrder = null;
|
||||||
|
if ($bargainOrderId) {
|
||||||
|
$bargainOrder = $user->bargainOrders()->findOrFail($bargainOrderId);
|
||||||
|
}
|
||||||
|
|
||||||
|
$mapProducts = $this->mapProducts($user, $products, $coupon, $bargainOrder);
|
||||||
|
|
||||||
// 是否支持配送
|
// 是否支持配送
|
||||||
$shippingSupported = true;
|
$shippingSupported = true;
|
||||||
|
|
@ -568,9 +577,10 @@ class OrderService
|
||||||
$vipDiscountAmount,
|
$vipDiscountAmount,
|
||||||
$couponDiscountAmount,
|
$couponDiscountAmount,
|
||||||
$salesValue,
|
$salesValue,
|
||||||
|
$bargainAmount,
|
||||||
) = $this->calculateFees($mapProducts);
|
) = $this->calculateFees($mapProducts);
|
||||||
|
|
||||||
$totalAmount = $productsTotalAmount - $couponDiscountAmount - $vipDiscountAmount;
|
$totalAmount = $productsTotalAmount - $couponDiscountAmount - $vipDiscountAmount - $bargainAmount;
|
||||||
|
|
||||||
if ($totalAmount < 0) {
|
if ($totalAmount < 0) {
|
||||||
$totalAmount = 0;
|
$totalAmount = 0;
|
||||||
|
|
@ -593,6 +603,7 @@ class OrderService
|
||||||
'vip_discount_amount' => trim_trailing_zeros(bcdiv($vipDiscountAmount, 100, 2)), // 会员折扣金额
|
'vip_discount_amount' => trim_trailing_zeros(bcdiv($vipDiscountAmount, 100, 2)), // 会员折扣金额
|
||||||
'coupon_discount_amount' => trim_trailing_zeros(bcdiv($couponDiscountAmount, 100, 2)), // 优惠券折扣金额
|
'coupon_discount_amount' => trim_trailing_zeros(bcdiv($couponDiscountAmount, 100, 2)), // 优惠券折扣金额
|
||||||
'total_amount' => trim_trailing_zeros(bcdiv($totalAmount, 100, 2)), // 实付金额
|
'total_amount' => trim_trailing_zeros(bcdiv($totalAmount, 100, 2)), // 实付金额
|
||||||
|
'bargain_amount' => trim_trailing_zeros(bcdiv($bargainAmount, 100, 2)), //砍价金额
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -608,12 +619,14 @@ class OrderService
|
||||||
$vipDiscountAmount = 0;
|
$vipDiscountAmount = 0;
|
||||||
$couponDiscountAmount = 0;
|
$couponDiscountAmount = 0;
|
||||||
$salesValue = 0;
|
$salesValue = 0;
|
||||||
|
$bargainAmount = 0;
|
||||||
|
|
||||||
foreach ($products as $product) {
|
foreach ($products as $product) {
|
||||||
$productsTotalAmount += $product['total_amount'];
|
$productsTotalAmount += $product['total_amount'];
|
||||||
$vipDiscountAmount += $product['vip_discount_amount'];
|
$vipDiscountAmount += $product['vip_discount_amount'];
|
||||||
$couponDiscountAmount += $product['coupon_discount_amount'];
|
$couponDiscountAmount += $product['coupon_discount_amount'];
|
||||||
$salesValue = bcadd($salesValue, $product['total_sales_value']);
|
$salesValue = bcadd($salesValue, $product['total_sales_value']);
|
||||||
|
$bargainAmount += $product['bargain_amount'];
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
|
@ -621,6 +634,7 @@ class OrderService
|
||||||
$vipDiscountAmount,
|
$vipDiscountAmount,
|
||||||
$couponDiscountAmount,
|
$couponDiscountAmount,
|
||||||
$salesValue,
|
$salesValue,
|
||||||
|
$bargainAmount,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -675,14 +689,13 @@ class OrderService
|
||||||
* @param \App\Models\User $user
|
* @param \App\Models\User $user
|
||||||
* @param array $products
|
* @param array $products
|
||||||
* @param \App\Models\UserCoupon|null $coupon
|
* @param \App\Models\UserCoupon|null $coupon
|
||||||
* @param \App\Models\BargainOrder|null $bargainOrder
|
|
||||||
* @return array
|
* @return array
|
||||||
*
|
*
|
||||||
* @throws \App\Exceptions\BizException
|
* @throws \App\Exceptions\BizException
|
||||||
*/
|
*/
|
||||||
protected function mapProducts(User $user, array $products, ?UserCoupon $coupon, ?BargainOrder $bargainOrder): array
|
protected function mapProducts(User $user, array $products, ?UserCoupon $coupon, ?BargainOrder $bargainOrder = null): array
|
||||||
{
|
{
|
||||||
$_products = collect($products)->map(function ($item) use ($user, $bargainOrder) {
|
$_products = collect($products)->map(function ($item) use ($user) {
|
||||||
$sku = $item['sku'];
|
$sku = $item['sku'];
|
||||||
|
|
||||||
return array_merge($item, [
|
return array_merge($item, [
|
||||||
|
|
@ -696,11 +709,20 @@ class OrderService
|
||||||
'total_amount' => $sku->sell_price * $item['quantity'],
|
'total_amount' => $sku->sell_price * $item['quantity'],
|
||||||
// 总销售值
|
// 总销售值
|
||||||
'total_sales_value' => bcmul($sku->sales_value, $item['quantity'], 2),
|
'total_sales_value' => bcmul($sku->sales_value, $item['quantity'], 2),
|
||||||
//填入砍价金额
|
//砍价金额
|
||||||
'bargain_amount'=> $bargainOrder?->bargain_price ?? 0,
|
'bargain_amount' => 0,
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//处理砍价金额
|
||||||
|
if ($bargainOrder) {
|
||||||
|
$bargainDiscountAmount = $this->getBargainDiscountAmount($bargainOrder, $_products->all());
|
||||||
|
$_products = $_products->map(function ($item) use ($bargainDiscountAmount) {
|
||||||
|
$item['bargain_amount'] = $bargainDiscountAmount[$item['sku']->id] ?? 0;
|
||||||
|
return $item;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if ($coupon === null) {
|
if ($coupon === null) {
|
||||||
return $_products->all();
|
return $_products->all();
|
||||||
}
|
}
|
||||||
|
|
@ -808,6 +830,49 @@ class OrderService
|
||||||
return $amounts;
|
return $amounts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
protected function getBargainDiscountAmount(BargainOrder $bargainOrder, array $products): array
|
||||||
|
{
|
||||||
|
// 砍价的商品
|
||||||
|
$amounts = [];
|
||||||
|
foreach ($products as $item) {
|
||||||
|
$sku = $item['sku'];
|
||||||
|
|
||||||
|
$amount = $item['total_amount'] - $item['vip_discount_amount'];
|
||||||
|
|
||||||
|
// 仅保留商品真实总额大于0的商品
|
||||||
|
if ($amount > 0) {
|
||||||
|
$amounts[$sku->id] = $amount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 全部商品总额
|
||||||
|
$totalAmount = array_sum($amounts);
|
||||||
|
|
||||||
|
|
||||||
|
$bargainAmount = $bargainOrder->bargain_price ?? 0;
|
||||||
|
|
||||||
|
// 待计算优惠的商品总数
|
||||||
|
$i = count($amounts);
|
||||||
|
|
||||||
|
foreach ($amounts as &$amount) {
|
||||||
|
$i--;
|
||||||
|
|
||||||
|
if ($i > 0) {
|
||||||
|
$amount = (int) bcdiv(bcmul($bargainAmount, $amount, 0), $totalAmount, 2);
|
||||||
|
$bargainAmount -= $amount;
|
||||||
|
} else {
|
||||||
|
$amount = $bargainAmount;
|
||||||
|
}
|
||||||
|
|
||||||
|
unset($amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $amounts;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据购物车获取商品
|
* 根据购物车获取商品
|
||||||
*
|
*
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue