133 lines
4.0 KiB
PHP
133 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services;
|
|
|
|
use App\Enums\PayWay;
|
|
use App\Events\OrderPaid;
|
|
use App\Exceptions\BizException;
|
|
use App\Models\Order;
|
|
use App\Models\OrderLog;
|
|
use App\Services\OrderService as EndpointOrderService;
|
|
|
|
class OrderService
|
|
{
|
|
/**
|
|
* 订单调整价格
|
|
*
|
|
* @return void
|
|
*/
|
|
public function adminReduceOrder(Order $order, int $reduceAmount)
|
|
{
|
|
if ($order->isPending()) {
|
|
$needReduceAmount = $order->total_amount - $reduceAmount + $order->reduced_amount;
|
|
$res = $order->where('updated_at', $order->updated_at)->update([
|
|
'reduced_amount' => $needReduceAmount,
|
|
'total_amount' => $reduceAmount,
|
|
]);
|
|
if ($res === 0) {
|
|
throw new BizException('订单已发生改变');
|
|
}
|
|
|
|
//更新订单商品优惠金额
|
|
//分解到订单中除赠品外的商品
|
|
$orderProducts = $order->products;
|
|
$needReduceOrderProducts = [];
|
|
$totalAmount = 0;
|
|
foreach ($orderProducts as $orderProduct) {
|
|
if (!$orderProduct->isGift()) {
|
|
$needReduceOrderProducts[] = $orderProduct;
|
|
$totalAmount += $orderProduct->total_amount;
|
|
}
|
|
}
|
|
|
|
$i = count($needReduceOrderProducts);
|
|
foreach ($needReduceOrderProducts as $reduceOrderProduct) {
|
|
$i --;
|
|
|
|
if ($i > 0) {
|
|
$amount = (int) bcdiv(bcmul($needReduceAmount, $reduceOrderProduct->total_amount, 0), $totalAmount, 2);
|
|
$needReduceAmount -= $amount;
|
|
$totalAmount -= $reduceOrderProduct->total_amount;
|
|
} else {
|
|
$amount = $needReduceAmount;
|
|
}
|
|
|
|
$newTotalAmount = $reduceOrderProduct->total_amount + $reduceOrderProduct->reduced_amount - $amount;
|
|
if ($newTotalAmount < 0) {
|
|
$newTotalAmount = 0;
|
|
}
|
|
|
|
// 更新订单商品的金额
|
|
$reduceOrderProduct->update([
|
|
'reduced_amount' => $amount,
|
|
'total_amount'=> $newTotalAmount,
|
|
]);
|
|
}
|
|
|
|
OrderLog::create([
|
|
'order_id'=>$order->id,
|
|
'content'=> '调整订单支付价格为:¥'.bcdiv($reduceAmount, 100, 2),
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 后台支付订单
|
|
*
|
|
* @param Order $order
|
|
* @return void
|
|
*/
|
|
public function adminPay(Order $order)
|
|
{
|
|
if ($order->isPending()) {
|
|
//操作订单状态-需要调整为统一支付方法
|
|
$orderService = new EndpointOrderService();
|
|
$orderService->pay($order, PayWay::Offline);
|
|
// 记录操作日志
|
|
OrderLog::create([
|
|
'order_id'=> $order->id,
|
|
'content'=> '修改订单状态为【已支付】',
|
|
]);
|
|
//注册支付成功事件
|
|
OrderPaid::dispatch($order);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 添加后台备注
|
|
*
|
|
* @param Order $order
|
|
* @param string $remark
|
|
* @return void
|
|
*/
|
|
public function adminRemark(Order $order, string $remark)
|
|
{
|
|
//操作订单状态-需要调整为统一支付方法
|
|
$order->update([
|
|
'remark' => $remark,
|
|
]);
|
|
|
|
OrderLog::create([
|
|
'order_id'=>$order->id,
|
|
'content'=> '修改订单备注:'.$remark,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 修改订单收货信息
|
|
*
|
|
* @param Order $order
|
|
* @param array $params
|
|
* @return void
|
|
*/
|
|
public function adminEditConsignee(Order $order, array $params)
|
|
{
|
|
$oldOrderConsignee = $order->consignee_name.','.$order->consignee_telephone.','.$order->consignee_zone.$order->consignee_address;
|
|
$order->update($params);
|
|
OrderLog::create([
|
|
'order_id'=>$order->id,
|
|
'content'=> '修改订单收货信息<br\>原收货信息:'.$oldOrderConsignee,
|
|
]);
|
|
}
|
|
}
|