124 lines
4.3 KiB
PHP
124 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Endpoint\Api\Http\Controllers\Order;
|
|
|
|
use App\Endpoint\Api\Http\Controllers\Controller;
|
|
use App\Helpers\Paginator;
|
|
use App\Models\{Order, OrderPre, Tag};
|
|
use App\Endpoint\Api\Http\Resources\{OrderResource, OrderResourceCollection};
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
use App\Exceptions\BizException;
|
|
|
|
class UnlineController extends Controller
|
|
{
|
|
/**
|
|
* 线下订单列表
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$perPage = Paginator::resolvePerPage('per_page', 20, 50);
|
|
|
|
$orders = Order::with('products')->filter($request->all())->where('source_type', OrderPre::class)->latest('id')->simplePaginate($perPage);
|
|
|
|
return OrderResourceCollection::make($orders);
|
|
}
|
|
|
|
/**
|
|
* 线下订单详细
|
|
*/
|
|
public function show($id)
|
|
{
|
|
$order = Order::where('source_type', OrderPre::class)->findOrFail($id);
|
|
$order->load('products');
|
|
|
|
return OrderResource::make($order);
|
|
}
|
|
|
|
/**
|
|
* 提货
|
|
*/
|
|
public function package($id, Request $request)
|
|
{
|
|
$user = $request->user();
|
|
if (!$user->userInfo->is_company) {
|
|
throw new BizException('非内部员工');
|
|
}
|
|
$request->validate([
|
|
'products' => 'required|array',
|
|
'products.*.id' => 'required',
|
|
'products.*.amount' => 'required',
|
|
], [
|
|
'products.required' => '发货商品必填',
|
|
]);
|
|
$order = Order::where('source_type', OrderPre::class)->findOrFail($id);
|
|
$store = $order->store;
|
|
|
|
// 订单来源为 帮客户下单 order_pres
|
|
try {
|
|
DB::beginTransaction();
|
|
$order_products = $order->products;
|
|
$tag = Tag::firstOrCreate([
|
|
'type' => Tag::TYPE_STORE_STOCK,
|
|
'name' => '提货'
|
|
]);
|
|
// 根据 order_pres 发货数量, 自动发货
|
|
$service_package = new \App\Admin\Services\OrderPackageService();
|
|
// order_product_id: 订单商品ID, quantity: 发货数量
|
|
$package_params = [];
|
|
foreach($request->input('products') as $item) {
|
|
$order_product = $order_products->firstWhere('id', $item['id']);
|
|
if ($order_product) {
|
|
array_push($package_params, [
|
|
'order_product_id' => $order_product->id,
|
|
'quantity' => $item['amount'],
|
|
]);
|
|
// 添加出库记录
|
|
$amount = $item['amount'];
|
|
$sku_id = $order_product->sku_id;
|
|
$product = $store->productSkus()->findOrFail($sku_id);
|
|
if ($product->pivot->amount - $amount < 0) {
|
|
throw new BizException('店铺的 ' . $product->name .' 库存不足');
|
|
}
|
|
|
|
$store->productSkus()->updateExistingPivot($product->id, [
|
|
'amount' => $product->pivot->amount - $amount
|
|
]);
|
|
$store->stockLogs()->create([
|
|
'operator_type' => get_class($user),
|
|
'operator_id' => $user->id,
|
|
'source_type' => Order::class,
|
|
'source_id' => $order->id,
|
|
'amount' => 0-$amount,
|
|
'product_sku_id' => $product->id,
|
|
'remarks' => '店铺提货',
|
|
'tag_id' => $tag->id
|
|
]);
|
|
}
|
|
}
|
|
// 发货
|
|
if (count($package_params) === 0) {
|
|
throw new BizException('提货商品不存在');
|
|
}
|
|
$package = $service_package->createPackage($order, [
|
|
'shipping_company' => '提货',
|
|
'shipping_number' => 'unline',
|
|
'packages' => $package_params
|
|
]);
|
|
$package->update([
|
|
'inviter_id' => $user->id
|
|
]);
|
|
DB::commit();
|
|
return response()->noContent();
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
report($th);
|
|
throw new BizException($th->getMessage());
|
|
}
|
|
}
|
|
}
|