mirror of
https://gitee.com/hzchunfen/erp.git
synced 2025-12-01 14:40:44 +00:00
Merge branch 'feature' into yezhenman
This commit is contained in:
commit
2d913c605c
@ -3,14 +3,16 @@
|
|||||||
namespace App\Http\Controllers\Goods;
|
namespace App\Http\Controllers\Goods;
|
||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Http\Resources\GoodsSkuResource;
|
use App\Http\Requests\GoodsSkuRequest;
|
||||||
|
use App\Http\Resources\GoodsResource;
|
||||||
use App\Models\Log as LogModel;
|
use App\Models\Log as LogModel;
|
||||||
|
use App\Utils\FormatUtils;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Validator;
|
use Illuminate\Support\Facades\Validator;
|
||||||
use Illuminate\Validation\Rule;
|
|
||||||
use App\Models\Goods;
|
use App\Models\Goods;
|
||||||
use App\Models\GoodsSku;
|
use App\Http\Requests\GoodsRequest;
|
||||||
|
use App\Models\DailyStockRecord;
|
||||||
|
|
||||||
class GoodsController extends Controller
|
class GoodsController extends Controller
|
||||||
{
|
{
|
||||||
@ -25,39 +27,16 @@ class GoodsController extends Controller
|
|||||||
|
|
||||||
public function index(Request $request)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
$goods = Goods::query()->filter()->get()->toArray();
|
$goods = Goods::query()->get(['id', 'title', 'img_url', 'type_id', 'brand_id', 'goods_code']);
|
||||||
$goodsIds = array_column($goods, 'id');
|
|
||||||
// 状态变更时间查询,日志
|
|
||||||
$day = date('Y-m-d'); //早上7点之前是昨天,7点之后是今天
|
|
||||||
$goodsSkus = GoodsSku::query()
|
|
||||||
->whereIn('goods_id', $goodsIds)
|
|
||||||
->filter()
|
|
||||||
->with(['goods' => function ($query) {
|
|
||||||
$query->with(['type:id,name', 'brand:id,name']);
|
|
||||||
}])
|
|
||||||
->with(['daily' => function ($query) use ($day){
|
|
||||||
$query->where('day', $day)->with(['daily:id,sku_id,day,arrived_today_num,loss_num,inventory']);
|
|
||||||
}])
|
|
||||||
->paginate();
|
|
||||||
|
|
||||||
return GoodsSkuResource::collection($goodsSkus);
|
return GoodsResource::collection($goods);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
$validator = Validator::make($request->all(), [
|
$goodsRules = (new GoodsRequest())->rules();
|
||||||
'title' => ['required', 'string', 'max:255'],
|
$skuRules = (new GoodsSkuRequest())->arrayRules('skus.*.');
|
||||||
'img_url' => ['required', 'string', 'max:255'],
|
$validator = Validator::make($request->all(), array_merge($goodsRules, ['skus' => ['required', 'array']], $skuRules));
|
||||||
'type_id' => ['required', 'integer', 'exists:goods_types,id'],
|
|
||||||
'brand_id' => ['integer', 'exists:goods_brands,id'],
|
|
||||||
'goods_code' => ['required', 'alpha_dash', 'max:32', 'unique:goods,goods_code'],
|
|
||||||
'skus' => ['required', 'array'],
|
|
||||||
'skus.*.title' => ['required', 'string', 'max:255'],
|
|
||||||
'skus.*.sku_code' => ['required', 'distinct', 'alpha_dash', 'max:32'],
|
|
||||||
'skus.*.status' => ['required', 'integer', Rule::in([0, 1, 2])],
|
|
||||||
'skus.*.num' => ['required', 'integer', 'max:10'],
|
|
||||||
'skus.*.cost' => ['required', 'numeric', 'max:10'],
|
|
||||||
]);
|
|
||||||
if ($validator->fails()) {
|
if ($validator->fails()) {
|
||||||
$this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
|
$this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
|
||||||
|
|
||||||
@ -65,6 +44,9 @@ class GoodsController extends Controller
|
|||||||
}
|
}
|
||||||
DB::beginTransaction();
|
DB::beginTransaction();
|
||||||
try {
|
try {
|
||||||
|
if (!empty($request->goods_id)) {
|
||||||
|
$goods = Goods::query()->find($request->goods_id);
|
||||||
|
} else {
|
||||||
$goods = new Goods();
|
$goods = new Goods();
|
||||||
$goods->title = $request->title;
|
$goods->title = $request->title;
|
||||||
$goods->img_url = $request->img_url;
|
$goods->img_url = $request->img_url;
|
||||||
@ -72,15 +54,26 @@ class GoodsController extends Controller
|
|||||||
$goods->brand_id = $request->brand_id;
|
$goods->brand_id = $request->brand_id;
|
||||||
$goods->goods_code = $request->goods_code;
|
$goods->goods_code = $request->goods_code;
|
||||||
$goods->save();
|
$goods->save();
|
||||||
|
}
|
||||||
$goodsSkus = [];
|
$goodsSkus = [];
|
||||||
foreach ($request->skus as $item) {
|
foreach ($request->skus as $item) {
|
||||||
$item['goods_id'] = $goods->id;
|
$item['goods_id'] = $goods->id;
|
||||||
|
$item['reference_price'] = $item['cost'] * 1.5;
|
||||||
$goodsSkus[] = $item;
|
$goodsSkus[] = $item;
|
||||||
}
|
}
|
||||||
$collection = $goods->skus()->createMany($goodsSkus);
|
$collection = $goods->skus()->createMany($goodsSkus)->toArray();
|
||||||
DB::commit();
|
$this->setAfterUpdate($collection);
|
||||||
$this->setAfterUpdate($collection->toArray());
|
|
||||||
$this->addLog(0, 'add');
|
$this->addLog(0, 'add');
|
||||||
|
$newRecords = [];
|
||||||
|
foreach ($collection as $sku) {
|
||||||
|
$newRecords[] = [
|
||||||
|
'sku_id' => $sku['id'],
|
||||||
|
'day' => FormatUtils::date(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
$record = new DailyStockRecord();
|
||||||
|
$record->batchInsert($newRecords);
|
||||||
|
DB::commit();
|
||||||
} catch (\Exception $exception) {
|
} catch (\Exception $exception) {
|
||||||
DB::rollBack();
|
DB::rollBack();
|
||||||
$this->res = [
|
$this->res = [
|
||||||
|
|||||||
@ -3,15 +3,21 @@
|
|||||||
namespace App\Http\Controllers\Goods;
|
namespace App\Http\Controllers\Goods;
|
||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Http\Requests\GoodsRequest;
|
||||||
|
use App\Http\Requests\GoodsSkuRequest;
|
||||||
|
use App\Models\Goods;
|
||||||
use App\Models\Log as LogModel;
|
use App\Models\Log as LogModel;
|
||||||
|
use App\Utils\FormatUtils;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use App\Models\GoodsSku;
|
use App\Models\GoodsSku;
|
||||||
use App\Http\Resources\GoodsSkuResource;
|
use App\Http\Resources\GoodsSkuResource;
|
||||||
use App\Imports\GoodsSkusImport;
|
use App\Imports\GoodsSkusImport;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Validator;
|
use Illuminate\Support\Facades\Validator;
|
||||||
use Illuminate\Validation\Rule;
|
use Illuminate\Validation\Rule;
|
||||||
use Illuminate\Validation\ValidationException;
|
use Illuminate\Validation\ValidationException;
|
||||||
use Maatwebsite\Excel\Facades\Excel;
|
use Maatwebsite\Excel\Facades\Excel;
|
||||||
|
use App\Models\DailyStockRecord;
|
||||||
|
|
||||||
class GoodsSkusController extends Controller
|
class GoodsSkusController extends Controller
|
||||||
{
|
{
|
||||||
@ -27,7 +33,22 @@ class GoodsSkusController extends Controller
|
|||||||
|
|
||||||
public function index(Request $request)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
return new GoodsSkuResource(GoodsSku::query()->get(['id', 'title']));
|
$goods = Goods::query()->filter()->get()->toArray();
|
||||||
|
$goodsIds = array_column($goods, 'id');
|
||||||
|
// 状态变更时间查询,日志
|
||||||
|
$day = FormatUtils::date();
|
||||||
|
$goodsSkus = GoodsSku::query()
|
||||||
|
->whereIn('goods_id', $goodsIds)
|
||||||
|
->filter()
|
||||||
|
->with(['goods' => function ($query) {
|
||||||
|
$query->with(['type:id,name', 'brand:id,name']);
|
||||||
|
}])
|
||||||
|
->with(['daily' => function ($query) use ($day) {
|
||||||
|
$query->where('day', $day)->with(['daily:id,sku_id,day,arrived_today_num,loss_num,inventory']);
|
||||||
|
}])
|
||||||
|
->paginate();
|
||||||
|
|
||||||
|
return GoodsSkuResource::collection($goodsSkus);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function show($id)
|
public function show($id)
|
||||||
@ -41,109 +62,238 @@ class GoodsSkusController extends Controller
|
|||||||
|
|
||||||
public function update($id, Request $request)
|
public function update($id, Request $request)
|
||||||
{
|
{
|
||||||
$data = $request->all();
|
$goodsRules = (new GoodsRequest())->arrayRules('goods.');
|
||||||
$this->validateUpdate($data);
|
$skuRules = (new GoodsSkuRequest())->arrayRules('sku.');
|
||||||
$sku = GoodsSku::query()->find($id);
|
$validator = Validator::make($request->all(), array_merge($goodsRules, $skuRules));
|
||||||
$this->setBeforeUpdate($sku->toArray());
|
|
||||||
$sku->update($data);
|
|
||||||
$this->setAfterUpdate($sku->toArray());
|
|
||||||
$this->addLog($id, 'update');
|
|
||||||
|
|
||||||
return new GoodsSkuResource($sku);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function batchUpdate(Request $request)
|
|
||||||
{
|
|
||||||
$appendRules = [
|
|
||||||
'*.id' => [
|
|
||||||
'required',
|
|
||||||
Rule::exists('goods_skus', 'id'),
|
|
||||||
],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function updateField($id, Request $request)
|
|
||||||
{
|
|
||||||
$appendRules = [
|
|
||||||
'updateField' => [
|
|
||||||
'required',
|
|
||||||
Rule::in(['reference_price', 'reserve', 'loss_num', 'status'])
|
|
||||||
],
|
|
||||||
];
|
|
||||||
if ($request->has('loss_num')) {
|
|
||||||
$appendRules['reason'] = ['required', 'string'];
|
|
||||||
}
|
|
||||||
$this->validateUpdate($request->all(), $appendRules);
|
|
||||||
$updateField = \request('updateField');
|
|
||||||
$sku = GoodsSku::query()->find($id);
|
|
||||||
$this->setBeforeUpdate($sku->$updateField);
|
|
||||||
$sku->$updateField = $request->$updateField;
|
|
||||||
$sku->save();
|
|
||||||
$this->setAfterUpdate($sku->$updateField);
|
|
||||||
$this->addLog($id, $updateField);
|
|
||||||
|
|
||||||
return new GoodsSkuResource($sku);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function updateStock($id, Request $request)
|
|
||||||
{
|
|
||||||
$this->validateUpdate($request->all());
|
|
||||||
$sku = GoodsSku::query()->where('id', $id)->get(['id', 'two_days_ago_num', 'yesterday_num']);
|
|
||||||
if ($sku->two_days_ago_num != \request('t wo_days_ago_num') || $sku->yesterday_num != \request('yesterday_num')) {
|
|
||||||
$this->setBeforeUpdate($sku->toArray());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private function validateUpdate($data, $appendRules = [])
|
|
||||||
{
|
|
||||||
$rules = [
|
|
||||||
'*.two_days_ago_num' => [
|
|
||||||
'sometimes',
|
|
||||||
'integer',
|
|
||||||
],
|
|
||||||
'*.yesterday_num' => [
|
|
||||||
'sometimes',
|
|
||||||
'integer',
|
|
||||||
],
|
|
||||||
'*.arrived_today_num' => [
|
|
||||||
'sometimes',
|
|
||||||
'integer',
|
|
||||||
],
|
|
||||||
'*.cost' => [
|
|
||||||
'sometimes',
|
|
||||||
'numeric',
|
|
||||||
'gt:0'
|
|
||||||
],
|
|
||||||
'*.reference_price' => [
|
|
||||||
'sometimes',
|
|
||||||
'numeric',
|
|
||||||
'gt:0'
|
|
||||||
],
|
|
||||||
'*.reserve' => [
|
|
||||||
'sometimes',
|
|
||||||
'integer',
|
|
||||||
],
|
|
||||||
'*.loss_num' => [
|
|
||||||
'sometimes',
|
|
||||||
'integer',
|
|
||||||
],
|
|
||||||
'*.inventory' => [
|
|
||||||
'sometimes',
|
|
||||||
'integer',
|
|
||||||
],
|
|
||||||
'*.status' => [
|
|
||||||
'sometimes',
|
|
||||||
'integer',
|
|
||||||
Rule::in([0, 1, 2])
|
|
||||||
],
|
|
||||||
];
|
|
||||||
$validator = Validator::make($data, array_merge($rules, $appendRules));
|
|
||||||
if ($validator->fails()) {
|
if ($validator->fails()) {
|
||||||
$this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
|
$this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
|
||||||
|
|
||||||
return response($this->res, $this->res['httpCode']);
|
return response($this->res, $this->res['httpCode']);
|
||||||
}
|
}
|
||||||
|
DB::beginTransaction();
|
||||||
|
try {
|
||||||
|
// 商品规格更新
|
||||||
|
$sku = GoodsSku::query()->find($id);
|
||||||
|
$this->setBeforeUpdate($sku->toArray());
|
||||||
|
$sku->update($request->sku);
|
||||||
|
$this->setAfterUpdate($sku->toArray());
|
||||||
|
$this->addLog($id, 'update');
|
||||||
|
// 商品更新
|
||||||
|
$goods = Goods::query()->find($sku->goods_id);
|
||||||
|
$this->setBeforeUpdate($goods->toArray());
|
||||||
|
$goods->update($request->goods);
|
||||||
|
$this->setAfterUpdate($goods->toArray());
|
||||||
|
$this->addLog($sku->goods_id, 'update', 'goods');
|
||||||
|
DB::commit();
|
||||||
|
} catch (\Exception $exception) {
|
||||||
|
DB::rollBack();
|
||||||
|
$this->res = [
|
||||||
|
'httpCode' => 400,
|
||||||
|
'errorCode' => 400416,
|
||||||
|
'errorMessage' => $exception->getMessage(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return response($this->res, $this->res['httpCode']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function batchUpdate(Request $request)
|
||||||
|
{
|
||||||
|
$appendRules = [
|
||||||
|
'updateType' => ['required', 'string', Rule::in(['newest', 'inventory', 'stock'])],
|
||||||
|
'skus' => ['required', 'array'],
|
||||||
|
'skus.*.id' => [
|
||||||
|
'required',
|
||||||
|
Rule::exists('goods_skus', 'id'),
|
||||||
|
],
|
||||||
|
];
|
||||||
|
$validator = $this->validateUpdate($request->all(), $appendRules);
|
||||||
|
if ($validator->fails()) {
|
||||||
|
$this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
|
||||||
|
|
||||||
|
return response($this->res, $this->res['httpCode']);
|
||||||
|
}
|
||||||
|
$function = $request->updateType;
|
||||||
|
|
||||||
|
return $this->$function($request);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function newest($request)
|
||||||
|
{
|
||||||
|
DB::beginTransaction();
|
||||||
|
try {
|
||||||
|
$logs = [];
|
||||||
|
foreach ($request->skus as $sku) {
|
||||||
|
$costLog = $arrivedLog = [
|
||||||
|
'module' => 'goods',
|
||||||
|
'action' => $request->getMethod(),
|
||||||
|
'target_type' => 'goods_sku',
|
||||||
|
'target_id' => $sku['id'],
|
||||||
|
'user_id' => $request->user()->id
|
||||||
|
];
|
||||||
|
// 成本
|
||||||
|
$goodsSku = GoodsSku::query()->where('id', $sku['id'])->first(['id', 'cost']);
|
||||||
|
$costLog['target_field'] = 'cost';
|
||||||
|
$costLog['before_update'] = $goodsSku->cost;
|
||||||
|
$goodsSku->cost = $sku['cost'];
|
||||||
|
$goodsSku->reference_price = $sku['cost'] * 1.5;
|
||||||
|
$goodsSku->save();
|
||||||
|
$costLog['after_update'] = $goodsSku->cost;
|
||||||
|
$logs[] = $costLog;
|
||||||
|
// 今日到货
|
||||||
|
$record = DailyStockRecord::query()->where('sku_id', $sku['id'])->where('day', FormatUtils::date())->first(['id', 'arrived_today_num']);
|
||||||
|
$arrivedLog['target_field'] = 'arrived_today_num';
|
||||||
|
$arrivedLog['before_update'] = $record->arrived_today_num;
|
||||||
|
$record->arrived_today_num = $sku['arrived_today_num'];
|
||||||
|
$record->save();
|
||||||
|
$arrivedLog['after_update'] = $record->arrived_today_num;
|
||||||
|
$logs[] = $arrivedLog;
|
||||||
|
}
|
||||||
|
$log = new LogModel();
|
||||||
|
$log->batchInsert($logs);
|
||||||
|
DB::commit();
|
||||||
|
} catch (\Exception $exception) {
|
||||||
|
DB::rollBack();
|
||||||
|
$this->res = [
|
||||||
|
'httpCode' => 400,
|
||||||
|
'errorCode' => 400500,
|
||||||
|
'errorMessage' => $exception->getMessage(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return response($this->res, $this->res['httpCode']);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function inventory($request)
|
||||||
|
{
|
||||||
|
DB::beginTransaction();
|
||||||
|
try {
|
||||||
|
$logs = [];
|
||||||
|
foreach ($request->skus as $sku) {
|
||||||
|
$inventoryLog = [
|
||||||
|
'module' => 'goods',
|
||||||
|
'action' => $request->getMethod(),
|
||||||
|
'target_type' => 'goods_sku',
|
||||||
|
'target_id' => $sku['id'],
|
||||||
|
'user_id' => $request->user()->id
|
||||||
|
];
|
||||||
|
// 今日到货
|
||||||
|
$record = DailyStockRecord::query()->where('sku_id', $sku['id'])->where('day', FormatUtils::date())->first(['id', 'inventory']);
|
||||||
|
$inventoryLog['target_field'] = 'inventory';
|
||||||
|
$inventoryLog['before_update'] = $record->inventory;
|
||||||
|
$record->inventory = $sku['inventory'];
|
||||||
|
$record->save();
|
||||||
|
$inventoryLog['after_update'] = $record->inventory;
|
||||||
|
$logs[] = $inventoryLog;
|
||||||
|
}
|
||||||
|
$log = new LogModel();
|
||||||
|
$log->batchInsert($logs);
|
||||||
|
DB::commit();
|
||||||
|
} catch (\Exception $exception) {
|
||||||
|
DB::rollBack();
|
||||||
|
$this->res = [
|
||||||
|
'httpCode' => 400,
|
||||||
|
'errorCode' => 400500,
|
||||||
|
'errorMessage' => $exception->getMessage(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return response($this->res, $this->res['httpCode']);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function stock($skus)
|
||||||
|
{
|
||||||
|
$update = reset($skus);
|
||||||
|
DB::beginTransaction();
|
||||||
|
try {
|
||||||
|
$sku = GoodsSku::query()->where('id', $update['id'])->get(['id', 'two_days_ago_num', 'yesterday_num']);
|
||||||
|
$record = DailyStockRecord::query()
|
||||||
|
->where('sku_id', $sku->id)
|
||||||
|
->where('day', FormatUtils::date())
|
||||||
|
->first();
|
||||||
|
$this->setBeforeUpdate([
|
||||||
|
'two_days_ago_num' => $sku->two_days_ago_num,
|
||||||
|
'yesterday_num' => $sku->yesterday_num,
|
||||||
|
'arrived_today_num' => $record->arrived_today_num,
|
||||||
|
]);
|
||||||
|
$sku->two_days_ago_num = $update['two_days_ago_num'];
|
||||||
|
$sku->yesterday_num = $update['two_days_ago_num'];
|
||||||
|
$sku->save();
|
||||||
|
$record->arrived_today_num = $update['arrived_today_num'];
|
||||||
|
$record->save();
|
||||||
|
$this->setAfterUpdate([
|
||||||
|
'two_days_ago_num' => $sku->two_days_ago_num,
|
||||||
|
'yesterday_num' => $sku->yesterday_num,
|
||||||
|
'arrived_today_num' => $record->arrived_today_num,
|
||||||
|
]);
|
||||||
|
$this->addLog($sku->id, 'stock');
|
||||||
|
DB::commit();
|
||||||
|
} catch (\Exception $exception) {
|
||||||
|
DB::rollBack();
|
||||||
|
$this->res = [
|
||||||
|
'httpCode' => 400,
|
||||||
|
'errorCode' => 400416,
|
||||||
|
'errorMessage' => $exception->getMessage(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return response($this->res, $this->res['httpCode']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updateField($id, Request $request)
|
||||||
|
{
|
||||||
|
$rules = [
|
||||||
|
'updateField' => [
|
||||||
|
'required',
|
||||||
|
Rule::in(['reference_price', 'reserve', 'loss_num', 'status'])
|
||||||
|
],
|
||||||
|
'reference_price' => [
|
||||||
|
'sometimes',
|
||||||
|
'numeric',
|
||||||
|
'gt:0'
|
||||||
|
],
|
||||||
|
'reserve' => [
|
||||||
|
'sometimes',
|
||||||
|
'integer',
|
||||||
|
],
|
||||||
|
'loss_num' => [
|
||||||
|
'sometimes',
|
||||||
|
'integer',
|
||||||
|
],
|
||||||
|
'reason' => [
|
||||||
|
'sometimes',
|
||||||
|
'required',
|
||||||
|
'string'
|
||||||
|
],
|
||||||
|
'status' => [
|
||||||
|
'sometimes',
|
||||||
|
'required',
|
||||||
|
'integer',
|
||||||
|
Rule::in([0, 1, 2])],
|
||||||
|
];
|
||||||
|
$validator = Validator::make($request->all(), $rules);
|
||||||
|
if ($validator->fails()) {
|
||||||
|
$this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
|
||||||
|
|
||||||
|
return response($this->res, $this->res['httpCode']);
|
||||||
|
}
|
||||||
|
$updateField = \request('updateField');
|
||||||
|
if ('loss_num' === $updateField) {
|
||||||
|
$model = DailyStockRecord::query()
|
||||||
|
->where('sku_id', $id)
|
||||||
|
->where('day', FormatUtils::date())
|
||||||
|
->first(['id', 'loss_num']);
|
||||||
|
$this->log->message = $request->input('reason', '');
|
||||||
|
} else {
|
||||||
|
$model = GoodsSku::query()->find($id);
|
||||||
|
}
|
||||||
|
$this->setBeforeUpdate($model->$updateField);
|
||||||
|
$model->$updateField = $request->$updateField;
|
||||||
|
$model->save();
|
||||||
|
$this->setAfterUpdate($model->$updateField);
|
||||||
|
$this->addLog($id, $updateField);
|
||||||
|
|
||||||
|
return response($this->res, $this->res['httpCode']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
|
|||||||
47
app/Http/Requests/GoodsRequest.php
Normal file
47
app/Http/Requests/GoodsRequest.php
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class GoodsRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function authorize()
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function rules()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => ['sometimes', 'required', 'integer', 'exists:goods,id'],
|
||||||
|
'title' => ['required', 'string', 'max:255'],
|
||||||
|
'img_url' => ['required', 'string', 'max:255'],
|
||||||
|
'type_id' => ['required', 'integer', 'exists:goods_types,id'],
|
||||||
|
'brand_id' => ['integer', 'exists:goods_brands,id'],
|
||||||
|
'goods_code' => ['required', 'alpha_dash', 'max:32', Rule::unique('goods')->ignore(request('goods_id'))],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function arrayRules($arrayName)
|
||||||
|
{
|
||||||
|
$arrayRules = [];
|
||||||
|
$rules = $this->rules();
|
||||||
|
foreach ($rules as $key => $val) {
|
||||||
|
$arrayRules[$arrayName . $key] = $val;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $arrayRules;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,30 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Http\Requests;
|
|
||||||
|
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
|
||||||
|
|
||||||
class GoodsSkuPost extends FormRequest
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Determine if the user is authorized to make this request.
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function authorize()
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the validation rules that apply to the request.
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function rules()
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
//
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
65
app/Http/Requests/GoodsSkuRequest.php
Normal file
65
app/Http/Requests/GoodsSkuRequest.php
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class GoodsSkuRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function authorize()
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function rules()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => ['sometimes', 'required', 'integer', 'exists:goods_skus,id'],
|
||||||
|
'goods_id' => ['sometimes', 'required', 'integer', 'exists:goods,id'],
|
||||||
|
'title' => ['required', 'string', 'max:255'],
|
||||||
|
'sku_code' => ['required', 'distinct', 'alpha_dash', 'max:32'],
|
||||||
|
'status' => ['required', 'integer', Rule::in([0, 1, 2])],
|
||||||
|
'num' => ['required', 'integer'],
|
||||||
|
'cost' => ['required', 'numeric'],
|
||||||
|
'reference_price' => [
|
||||||
|
'sometimes',
|
||||||
|
'numeric',
|
||||||
|
'gt:0'
|
||||||
|
],
|
||||||
|
'reserve' => [
|
||||||
|
'sometimes',
|
||||||
|
'integer',
|
||||||
|
],
|
||||||
|
'loss_num' => [
|
||||||
|
'sometimes',
|
||||||
|
'integer',
|
||||||
|
],
|
||||||
|
'inventory' => [
|
||||||
|
'sometimes',
|
||||||
|
'integer',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function arrayRules($arrayName)
|
||||||
|
{
|
||||||
|
$arrayRules = [];
|
||||||
|
$rules = $this->rules();
|
||||||
|
foreach ($rules as $key => $val) {
|
||||||
|
$arrayRules[$arrayName . $key] = $val;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $arrayRules;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -15,6 +15,8 @@ class Goods extends Model
|
|||||||
'brand_id',
|
'brand_id',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
protected $guarded = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 多规格
|
* 多规格
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -7,8 +7,11 @@ class FormatUtils
|
|||||||
/**
|
/**
|
||||||
* 格式化为树形结构
|
* 格式化为树形结构
|
||||||
* @param $menus
|
* @param $menus
|
||||||
* @param $parentId
|
* @param $parentValue
|
||||||
* @param $juniorTitle //下级标题
|
* @param string $juniorTitle //下级标题
|
||||||
|
* @param string $parentKey
|
||||||
|
* @param string $subKey
|
||||||
|
* @param string $useKey
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public static function formatTreeData($menus, $parentValue, $juniorTitle = 'children', $parentKey = 'parent_id', $subKey = 'id', $useKey = '')
|
public static function formatTreeData($menus, $parentValue, $juniorTitle = 'children', $parentKey = 'parent_id', $subKey = 'id', $useKey = '')
|
||||||
@ -30,4 +33,18 @@ class FormatUtils
|
|||||||
}
|
}
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 今天7点到明天7点算作今天
|
||||||
|
*/
|
||||||
|
public static function date()
|
||||||
|
{
|
||||||
|
$time = time();
|
||||||
|
$inventoryTime = strtotime(date('Y-m-d 07:00:00'));
|
||||||
|
if ($time < $inventoryTime) {
|
||||||
|
$time = strtotime('-1 day');
|
||||||
|
}
|
||||||
|
|
||||||
|
return date('Y-m-d', $time);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,6 +22,7 @@ class CreateLogsTable extends Migration
|
|||||||
$table->string('target_field')->nullable()->comment('目标字段');
|
$table->string('target_field')->nullable()->comment('目标字段');
|
||||||
$table->text('before_update')->nullable()->comment('更新前数据');
|
$table->text('before_update')->nullable()->comment('更新前数据');
|
||||||
$table->text('after_update')->nullable()->comment('更新后数据');
|
$table->text('after_update')->nullable()->comment('更新后数据');
|
||||||
|
$table->text('message')->nullable()->comment('备注信息');
|
||||||
$table->bigInteger('user_id')->comment('操作人id');
|
$table->bigInteger('user_id')->comment('操作人id');
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
});
|
});
|
||||||
|
|||||||
@ -4,6 +4,7 @@ use App\Http\Controllers\Auth\LoginController;
|
|||||||
use App\Http\Controllers\Role\RolesController;
|
use App\Http\Controllers\Role\RolesController;
|
||||||
use App\Http\Controllers\UploadController;
|
use App\Http\Controllers\UploadController;
|
||||||
use App\Http\Controllers\Shop\ShopsController;
|
use App\Http\Controllers\Shop\ShopsController;
|
||||||
|
use App\Http\Controllers\Goods\GoodsSkusController;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
@ -28,6 +29,8 @@ Route::middleware('auth:api')->group(function () {
|
|||||||
Route::resource('goods', 'Goods\GoodsController', ['only' => ['index', 'store']]);
|
Route::resource('goods', 'Goods\GoodsController', ['only' => ['index', 'store']]);
|
||||||
// 商品规格
|
// 商品规格
|
||||||
Route::resource('goods_skus', 'Goods\GoodsSkusController', ['only' => ['index', 'show', 'update', 'store']]);
|
Route::resource('goods_skus', 'Goods\GoodsSkusController', ['only' => ['index', 'show', 'update', 'store']]);
|
||||||
|
Route::patch('batch/goods_skus', [GoodsSkusController::class, 'batchUpdate'])->name('goods_sku.batch_update');
|
||||||
|
Route::patch('single/goods_skus/{id}', [GoodsSkusController::class, 'updateField'])->name('goods_sku.single_update');
|
||||||
// 店铺
|
// 店铺
|
||||||
Route::resource('shops', 'Shop\ShopsController', ['only' => ['index', 'store', 'show', 'update', 'destroy']]);
|
Route::resource('shops', 'Shop\ShopsController', ['only' => ['index', 'store', 'show', 'update', 'destroy']]);
|
||||||
Route::get('shop_platforms', [ShopsController::class, 'getPlatList'])->name('plat.list');
|
Route::get('shop_platforms', [ShopsController::class, 'getPlatList'])->name('plat.list');
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user