回筐逻辑修改
This commit is contained in:
@@ -182,7 +182,7 @@ class OrderController extends BaseMiniController
|
||||
$previewMap = [];
|
||||
$flatItems = [];
|
||||
foreach ($paginator->getCollection() as $order) {
|
||||
foreach ($order->items as $item) {
|
||||
foreach ($order->items->take(3) as $item) {
|
||||
$flatItems[] = [
|
||||
'order_id' => $order->id,
|
||||
'product_name' => $item->product_name,
|
||||
|
||||
@@ -2,124 +2,41 @@
|
||||
|
||||
namespace App\Http\Controllers\Recon;
|
||||
|
||||
use App\Exceptions\RepositoryException;
|
||||
use App\Http\Requests\Recon\ContainerReturnFormRequest;
|
||||
use App\Models\ContainerReturnModel;
|
||||
use App\Models\StoreModel;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Modules\AnnoRoute\Attribute\DeleteRoute;
|
||||
use Modules\AnnoRoute\Attribute\GetRoute;
|
||||
use Modules\AnnoRoute\Attribute\PostRoute;
|
||||
use Modules\AnnoRoute\Attribute\RequestAttribute;
|
||||
use Modules\Common\Http\Controllers\BaseController;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* 回筐记录(压筐=生成账单自动写入只读;回筐=门店退回手动登记,扣减门店待回数量)
|
||||
* 压回筐记录(周转筐/托盘跟账单走,生成账单时自动写入完整快照,只读)
|
||||
*
|
||||
* 数量正数=压筐(附加金额),负数=回筐(抵扣金额)
|
||||
*/
|
||||
#[RequestAttribute('/recon/container-return', 'recon.containerReturn')]
|
||||
class ContainerReturnController extends BaseController
|
||||
{
|
||||
protected array $searchField = [
|
||||
'store_id' => '=',
|
||||
'type' => '=',
|
||||
'return_date' => 'betweenDate',
|
||||
'bill_id' => '=',
|
||||
'created_at' => 'betweenDate',
|
||||
];
|
||||
|
||||
/** 回筐记录列表 */
|
||||
/** 压回筐记录列表 */
|
||||
#[GetRoute(authorize: 'query')]
|
||||
public function query(Request $request): JsonResponse
|
||||
{
|
||||
$params = $request->all();
|
||||
$pageSize = $params['pageSize'] ?? 10;
|
||||
$data = $this->buildSearch($params, ContainerReturnModel::query()->with([
|
||||
'store:id,name,pending_box_num,pending_tray_num',
|
||||
'bill:id,bill_no',
|
||||
'store:id,name',
|
||||
'bill:id,bill_no,bill_date',
|
||||
'operator:id,nickname',
|
||||
]))
|
||||
->orderBy('return_date', 'desc')
|
||||
->orderBy('id', 'desc')
|
||||
->paginate($pageSize)
|
||||
->toArray();
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 回筐登记:门店退回周转筐/托盘,扣减门店待回数量(超回拒绝)
|
||||
* @throws Throwable
|
||||
*/
|
||||
#[PostRoute(authorize: 'create')]
|
||||
public function create(ContainerReturnFormRequest $request): JsonResponse
|
||||
{
|
||||
$data = $request->validated();
|
||||
$boxNum = (int) $data['box_num'];
|
||||
$trayNum = (int) $data['tray_num'];
|
||||
if ($boxNum === 0 && $trayNum === 0) {
|
||||
throw new RepositoryException('周转筐与周转托盘数量不能同时为 0');
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($data, $boxNum, $trayNum, $request) {
|
||||
$store = StoreModel::query()->lockForUpdate()->find((int) $data['store_id']);
|
||||
if (empty($store)) {
|
||||
throw new RepositoryException('门店不存在');
|
||||
}
|
||||
if ($boxNum > (int) $store->pending_box_num) {
|
||||
throw new RepositoryException(
|
||||
'回筐数量超过该门店待回筐数量(当前待回 ' . (int) $store->pending_box_num . ' 个)'
|
||||
);
|
||||
}
|
||||
if ($trayNum > (int) $store->pending_tray_num) {
|
||||
throw new RepositoryException(
|
||||
'回托盘数量超过该门店待回托盘数量(当前待回 ' . (int) $store->pending_tray_num . ' 个)'
|
||||
);
|
||||
}
|
||||
|
||||
$store->pending_box_num = (int) $store->pending_box_num - $boxNum;
|
||||
$store->pending_tray_num = (int) $store->pending_tray_num - $trayNum;
|
||||
$store->save();
|
||||
|
||||
ContainerReturnModel::create([
|
||||
'store_id' => $store->id,
|
||||
'bill_id' => 0,
|
||||
'type' => ContainerReturnModel::TYPE_RETURN,
|
||||
'box_num' => $boxNum,
|
||||
'tray_num' => $trayNum,
|
||||
'return_date' => $data['return_date'],
|
||||
'operator_id' => (int) $request->user()->id,
|
||||
'remark' => (string) ($data['remark'] ?? ''),
|
||||
]);
|
||||
|
||||
return $this->success([], '回筐已登记');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除回筐记录(仅手动登记的回筐记录可删,删除后恢复门店待回数量;压筐记录随账单生成不允许删除)
|
||||
* @throws Throwable
|
||||
*/
|
||||
#[DeleteRoute(route: '/{id}', authorize: 'delete', where: ['id' => '[0-9]+'])]
|
||||
public function delete(int $id): JsonResponse
|
||||
{
|
||||
$record = ContainerReturnModel::find($id);
|
||||
if (empty($record)) {
|
||||
throw new RepositoryException('回筐记录不存在');
|
||||
}
|
||||
if ($record->type !== ContainerReturnModel::TYPE_RETURN) {
|
||||
throw new RepositoryException('压筐记录由账单生成,不允许删除');
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($record) {
|
||||
$store = StoreModel::query()->lockForUpdate()->find($record->store_id);
|
||||
if ($store !== null) {
|
||||
$store->pending_box_num = (int) $store->pending_box_num + (int) $record->box_num;
|
||||
$store->pending_tray_num = (int) $store->pending_tray_num + (int) $record->tray_num;
|
||||
$store->save();
|
||||
}
|
||||
$record->delete();
|
||||
|
||||
return $this->success([], '回筐记录已删除,门店待回数量已恢复');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user