230 lines
6.8 KiB
PHP
230 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace App\Endpoint\Api\Http\Controllers\Dealer;
|
|
|
|
use App\Endpoint\Api\Http\Controllers\Controller;
|
|
use App\Endpoint\Api\Http\Resources\Dealer\OrderResource;
|
|
use App\Endpoint\Api\Http\Resources\Dealer\OrderSimpleResource;
|
|
use App\Exceptions\BizException;
|
|
use App\Helpers\Paginator as PaginatorHelper;
|
|
use App\Models\DealerOrder;
|
|
use App\Models\DealerProduct;
|
|
use App\Services\Dealer\OrderService;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
|
|
class OrderController extends Controller
|
|
{
|
|
/**
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return void
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$cate = $request->input('cate', 'purchase');//获取订单类别
|
|
$user = $request->user();
|
|
switch ($cate) {
|
|
case 'consignor'://发货单
|
|
$query = $user->dealerConsignOrders();
|
|
break;
|
|
default://采购单
|
|
$query = $user->dealerOrders();
|
|
break;
|
|
}
|
|
$perPage = PaginatorHelper::resolvePerPage('per_page', 20, 50);
|
|
|
|
$orders = $query->with('products')
|
|
->filter($request->all())
|
|
->latest('id')
|
|
->simplePaginate($perPage);
|
|
|
|
return OrderSimpleResource::collection($orders);
|
|
}
|
|
|
|
public function store(Request $request, OrderService $orderService)
|
|
{
|
|
$input = $request->validate([
|
|
'shipping_address_id' => ['bail', 'required', 'int'],
|
|
'product_id'=>['bail', 'required', 'int', 'min:0'],
|
|
'num'=>['bail', 'required', 'int', 'min:1'],
|
|
], [], [
|
|
'product_id' => '商品',
|
|
'num' => '数量',
|
|
'shipping_address_id' => '收货地址',
|
|
]);
|
|
$product = DealerProduct::online()->findOrFail($input['product_id']);
|
|
try {
|
|
DB::beginTransaction();
|
|
$order = $orderService->createOrder($request->user(), $product, $input['num'], $input['shipping_address_id']);
|
|
DB::commit();
|
|
} catch (BizException $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
report($th);
|
|
throw new BizException('下单失败,请稍后再试');
|
|
}
|
|
|
|
return OrderResource::make($order);
|
|
}
|
|
|
|
public function show($id, Request $request)
|
|
{
|
|
$order = DealerOrder::findOrFail($id);
|
|
$userId = $request->user()->id;
|
|
//既不是采购人,也不是发货人
|
|
if (!$order->canCurd($userId)) {
|
|
throw new BizException('订单未找到');
|
|
}
|
|
|
|
return OrderResource::make($order);
|
|
}
|
|
|
|
/**
|
|
* 计算订单价格
|
|
*
|
|
* @return void
|
|
*/
|
|
public function totalAmount(Request $request, OrderService $orderService)
|
|
{
|
|
$input = $request->validate([
|
|
'product_id'=>['bail', 'required', 'int', 'min:0'],
|
|
'num'=>['bail', 'required', 'int', 'min:1'],
|
|
]);
|
|
|
|
$product = DealerProduct::online()->findOrFail($input['product_id']);
|
|
|
|
return response()->json([
|
|
'total_amount'=> $orderService->totalAmount($request->user(), $product, $input['num']),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 确认接单
|
|
*
|
|
* @param [type] $id
|
|
* @param Request $request
|
|
* @return void
|
|
*/
|
|
public function confirmOrder($id, Request $request, OrderService $orderService)
|
|
{
|
|
$order = DealerOrder::findOrFail($id);
|
|
$userId = $request->user()->id;
|
|
//不是发货人
|
|
if (!$order->isConsignor($userId)) {
|
|
throw new BizException('订单未找到');
|
|
}
|
|
|
|
$orderService->confirmOrder($order);
|
|
return response()->noContent();
|
|
}
|
|
|
|
/**
|
|
* 确认打款
|
|
*
|
|
* @return void
|
|
*/
|
|
public function payOrder($id, Request $request, OrderService $orderService)
|
|
{
|
|
$order = DealerOrder::findOrFail($id);
|
|
$userId = $request->user()->id;
|
|
//不是采购人
|
|
if (!$order->isUser($userId)) {
|
|
throw new BizException('订单未找到');
|
|
}
|
|
|
|
$input = $request->validate([
|
|
'pay_image' => ['bail', 'required', 'string'],
|
|
]);
|
|
$orderService->payOrder($order, $input['pay_image']);
|
|
return response()->noContent();
|
|
}
|
|
|
|
/**
|
|
* 确认收款+发货
|
|
*
|
|
* @return void
|
|
*/
|
|
public function paidOrder($id, Request $request, OrderService $orderService)
|
|
{
|
|
$order = DealerOrder::findOrFail($id);
|
|
$userId = $request->user()->id;
|
|
//不是发货人
|
|
if (!$order->isConsignor($userId)) {
|
|
throw new BizException('订单未找到');
|
|
}
|
|
try {
|
|
DB::beginTransaction();
|
|
$orderService->paidOrder($order);//确认收款
|
|
$orderService->shippingOrder($order);//确认发货
|
|
DB::commit();
|
|
} catch (QueryException $e) {
|
|
DB::rollBack();
|
|
if (strpos($e->getMessage(), 'Numeric value out of range') !== false) {
|
|
$e = new BizException('当前可发货库存不足');
|
|
}
|
|
|
|
throw $e;
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
report($th);
|
|
throw new BizException('操作失败,请刷新后再试');
|
|
}
|
|
return response()->noContent();
|
|
}
|
|
|
|
/**
|
|
* 确认收货
|
|
*
|
|
* @return void
|
|
*/
|
|
public function shippingedOrder($id, Request $request, OrderService $orderService)
|
|
{
|
|
$order = DealerOrder::findOrFail($id);
|
|
$userId = $request->user()->id;
|
|
//不是收货人
|
|
if (!$order->isUser($userId)) {
|
|
throw new BizException('订单未找到');
|
|
}
|
|
try {
|
|
DB::beginTransaction();
|
|
$orderService->shippingedOrder($order);
|
|
DB::commit();
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
report($th);
|
|
throw new BizException('操作失败,请刷新后再试');
|
|
}
|
|
return response()->noContent();
|
|
}
|
|
|
|
/**
|
|
* 取消订单
|
|
*
|
|
* @param [type] $id
|
|
* @param Request $request
|
|
* @return void
|
|
*/
|
|
public function cancelOrder($id, Request $request, OrderService $orderService)
|
|
{
|
|
$order = DealerOrder::findOrFail($id);
|
|
$userId = $request->user()->id;
|
|
if (!$order->isUser($userId)) {
|
|
throw new BizException('订单未找到');
|
|
}
|
|
try {
|
|
DB::beginTransaction();
|
|
$orderService->cancelOrder($order);
|
|
DB::commit();
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
report($th);
|
|
throw new BizException('操作失败,请刷新后再试');
|
|
}
|
|
return response()->noContent();
|
|
}
|
|
}
|