鲜花2.0-库存成本,周数据导出功能修改+增加库存变更记录

This commit is contained in:
杨建炊 2024-08-16 13:26:22 +08:00
parent bf67357601
commit 25f805496d
9 changed files with 38 additions and 28 deletions

View File

@ -82,7 +82,8 @@ class LossRecordController extends Controller
$lossRecords->reason = $allParams['reason'] ?? ''; $lossRecords->reason = $allParams['reason'] ?? '';
$lossRecords->phenomenon = $v['phenomenon'] ?? ''; $lossRecords->phenomenon = $v['phenomenon'] ?? '';
$lossRecords->save(); $lossRecords->save();
$updateIds = GoodSkuService::computeSkuStock($goodsSku->toArray(), ['num' => 0 - $allParams['num'], "cost" => $allParams['cost']]); $updateIds = GoodSkuService::computeSkuStock($goodsSku->toArray()
, ['num' => 0 - $allParams['num'], "cost" => $allParams['cost'],"user_id"=>$request->user()->id]);
event(new BatchStockUpdateEvent($updateIds)); event(new BatchStockUpdateEvent($updateIds));
} else { } else {
$this->res = [ $this->res = [
@ -177,7 +178,8 @@ class LossRecordController extends Controller
$lossRecords->phenomenon = $v['phenomenon'] ?? ''; $lossRecords->phenomenon = $v['phenomenon'] ?? '';
$lossRecords->save(); $lossRecords->save();
$updateIds = GoodSkuService::computeSkuStock($goodsSkuItem, ['num' => 0 - $v['num'], "cost" => $v['cost']]); $updateIds = GoodSkuService::computeSkuStock($goodsSkuItem
, ['num' => 0 - $v['num'], "cost" => $v['cost'],"user_id"=>$request->user()->id]);
$allUpdateIds = array_merge($allUpdateIds, $updateIds); $allUpdateIds = array_merge($allUpdateIds, $updateIds);
} }

View File

@ -98,7 +98,7 @@ class PurchaseRecordController extends Controller
} }
$purchaseRecords->save(); $purchaseRecords->save();
$allParams['user_id'] = $request->user()->id;
$updateIds = GoodSkuService::computeSkuStock($goodsSku->toArray(), $allParams, TargetTypeEnum::PURCHASE); $updateIds = GoodSkuService::computeSkuStock($goodsSku->toArray(), $allParams, TargetTypeEnum::PURCHASE);
event(new BatchStockUpdateEvent($updateIds)); event(new BatchStockUpdateEvent($updateIds));
} else { } else {
@ -165,7 +165,8 @@ class PurchaseRecordController extends Controller
$validator = Validator::make($request->all(), [ $validator = Validator::make($request->all(), [
'purchaseOrders' => 'required|array', 'purchaseOrders' => 'required|array',
'purchaseOrders.*.external_sku_id' => 'required|string', 'purchaseOrders.*.external_sku_id' => 'required|string',
'purchaseOrders.*.num' => 'required|integer']); 'purchaseOrders.*.num' => 'required|integer',
'purchaseOrders.*.cost' => 'required',]);
//参数校验 //参数校验
if ($validator->fails()) { if ($validator->fails()) {
$this->setValidatorFailResponse($validator->getMessageBag()->getMessages()); $this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
@ -200,7 +201,7 @@ class PurchaseRecordController extends Controller
$purchaseRecords->supplier_id = $v['supplier_id'] ?? 0; $purchaseRecords->supplier_id = $v['supplier_id'] ?? 0;
$purchaseRecords->expire_time = Carbon::now()->addDays($expireDay)->toDateTimeString(); $purchaseRecords->expire_time = Carbon::now()->addDays($expireDay)->toDateTimeString();
$purchaseRecords->save(); $purchaseRecords->save();
$v['user_id'] = $request->user()->id;
$updateIds = GoodSkuService::computeSkuStock($goodsSkuItem, $v, TargetTypeEnum::PURCHASE); $updateIds = GoodSkuService::computeSkuStock($goodsSkuItem, $v, TargetTypeEnum::PURCHASE);
$allUpdateIds = array_merge($allUpdateIds, $updateIds); $allUpdateIds = array_merge($allUpdateIds, $updateIds);
} }

View File

@ -26,14 +26,15 @@ class SuppliersController extends Controller
$suppliers = Suppliers::query()->paginate($request->get('per_page')); $suppliers = Suppliers::query()->paginate($request->get('per_page'));
return JsonResource::collection($suppliers); return JsonResource::collection($suppliers);
} }
public function store(Request $request) public function store(Request $request)
{ {
//获取所有参数 //获取所有参数
$allParams =$request->all(); $allParams = $request->all();
//进行校验验证 //进行校验验证
$validator = Validator::make($allParams, [ $validator = Validator::make($allParams, [
'supplier_name' => 'required|string', 'supplier_name' => 'required|string',
'company_name' => 'required|string', 'company_name' => 'required|string',
'address' => 'sometimes|string', 'address' => 'sometimes|string',
'link_tel' => 'sometimes|string', 'link_tel' => 'sometimes|string',
'payment_account' => 'sometimes|string', 'payment_account' => 'sometimes|string',
@ -65,11 +66,11 @@ class SuppliersController extends Controller
public function update($id, Request $request) public function update($id, Request $request)
{ {
//获取所有参数 //获取所有参数
$allParams =$request->all(); $allParams = $request->all();
//进行校验验证 //进行校验验证
$validator = Validator::make($allParams, [ $validator = Validator::make($allParams, [
'supplier_name' => 'required|string', 'supplier_name' => 'required|string',
'company_name' => 'required|string', 'company_name' => 'required|string',
'address' => 'sometimes|string', 'address' => 'sometimes|string',
'link_tel' => 'sometimes|string', 'link_tel' => 'sometimes|string',
'payment_account' => 'sometimes|string', 'payment_account' => 'sometimes|string',

View File

@ -2,18 +2,12 @@
namespace App\Imports; namespace App\Imports;
use App\Events\BatchStockUpdateEvent;
use App\Jobs\SyncCostToMiaoXuan;
use App\Models\DailyStockRecord;
use App\Models\GoodsSku; use App\Models\GoodsSku;
use App\Models\TodayPrice;
use App\Services\GoodSku\GoodSkuService; use App\Services\GoodSku\GoodSkuService;
use App\Utils\DateTimeUtils;
use Exception; use Exception;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows; use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\ToArray; use Maatwebsite\Excel\Concerns\ToArray;
use App\Utils\ArrayUtils;
class InventoryImport implements ToArray, SkipsEmptyRows class InventoryImport implements ToArray, SkipsEmptyRows
{ {

View File

@ -64,10 +64,17 @@ class GoodsSku extends Model
public function getThumbUrlAttribute($value) public function getThumbUrlAttribute($value)
{ {
return json_decode($value, true); return json_decode($value, true);
} }
public function getNameAttribute($value)
{
if(empty($value)){
return $this->attributes['title']??'';
}
return $value;
}
/** /**
* 此规格从属于一个商品 * 此规格从属于一个商品
*/ */

View File

@ -12,6 +12,7 @@ use App\Models\Log as LogModel;
use App\Utils\DateTimeUtils; use App\Utils\DateTimeUtils;
use App\Utils\GeneratorUtils; use App\Utils\GeneratorUtils;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@ -50,6 +51,7 @@ class GoodSkuService
Log::info("库存盘点前完整信息", $skusWithCombinationGoods); Log::info("库存盘点前完整信息", $skusWithCombinationGoods);
Log::info("需要操作的库存数据", $inventoryKeyBySkuIdMap); Log::info("需要操作的库存数据", $inventoryKeyBySkuIdMap);
$batchNumber = GeneratorUtils::generateBatchNumber("import"); $batchNumber = GeneratorUtils::generateBatchNumber("import");
DB::beginTransaction(); DB::beginTransaction();
try { try {
foreach ($skusWithCombinationGoods as $sku) { foreach ($skusWithCombinationGoods as $sku) {
@ -68,8 +70,8 @@ class GoodSkuService
foreach ($inventoryKeyBySkuIdMap as $skuId => $realStock) { foreach ($inventoryKeyBySkuIdMap as $skuId => $realStock) {
//库存修改 盘点是直接覆盖 //库存修改 盘点是直接覆盖
$skuData = GoodsSku::query()->where('id', $skuId)->first(); $skuData = GoodsSku::query()->where('id', $skuId)->first();
if(!empty($skuData)){ if (!empty($skuData)) {
$costLogs[] = static::addStockLog($skuData, [] $costLogs[] = static::addStockLog($skuData
, TargetTypeEnum::INVENTORY, ['stock' => $realStock]); , TargetTypeEnum::INVENTORY, ['stock' => $realStock]);
$skuData->stock = $realStock; $skuData->stock = $realStock;
$skuData->save(); $skuData->save();
@ -142,7 +144,7 @@ class GoodSkuService
->update($updateParam); ->update($updateParam);
$updateIds[] = $goodsSkuItem['id']; $updateIds[] = $goodsSkuItem['id'];
$updateParams[] = $updateParam; $updateParams[] = $updateParam;
$costLogs[] = static::addStockLog($goodsSkuItem, $changeData, $targetType, $updateParam); $costLogs[] = static::addStockLog($goodsSkuItem, $targetType, $updateParam);
} else { } else {
$combinationGood = CombinationGood::query()->with('goodsSkuItem:id,stock,sale_stock') $combinationGood = CombinationGood::query()->with('goodsSkuItem:id,stock,sale_stock')
@ -157,7 +159,7 @@ class GoodSkuService
$updateIds[] = $item['goodsSkuItem']['id']; $updateIds[] = $item['goodsSkuItem']['id'];
$updateParams[] = $updateParam; $updateParams[] = $updateParam;
$costLogs[] = static::addStockLog($item['goodsSkuItem'], $changeData, $targetType, $updateParam); $costLogs[] = static::addStockLog($item['goodsSkuItem'], $targetType, $updateParam);
} }
} }
$log = new LogModel(); $log = new LogModel();
@ -166,14 +168,15 @@ class GoodSkuService
return $updateIds; return $updateIds;
} }
public static function addStockLog($goodsSkuItem, $changeData, $targetType = TargetTypeEnum::LOSS, $updateParam) public static function addStockLog($goodsSkuItem, $targetType = TargetTypeEnum::LOSS, $updateParam)
{ {
$userId = Auth::id();
$costLog = [ $costLog = [
'module' => 'goodSkus', 'module' => 'goodSkus',
'action' => "POST", 'action' => "POST",
'target_type' => $targetType, 'target_type' => $targetType,
'target_id' => $goodsSkuItem['id'] ?? 0, 'target_id' => $goodsSkuItem['id'] ?? 0,
'user_id' => $changeData['user_id'] ?? 999, 'user_id' => $userId ?? 999,
"target_field" => "stock" "target_field" => "stock"
]; ];
$costLog['before_update'] = json_encode($goodsSkuItem); $costLog['before_update'] = json_encode($goodsSkuItem);

View File

@ -15,8 +15,8 @@ class CreatePurchaseRecordsTable extends Migration
{ {
Schema::create('purchase_records', function (Blueprint $table) { Schema::create('purchase_records', function (Blueprint $table) {
$table->bigIncrements('id'); $table->bigIncrements('id');
$table->string('external_sku_id',100)->comment('唯一sku标识'); $table->string('external_sku_id',60)->comment('唯一sku标识');
$table->string('batch_number')->nullable()->comment('批次号'); $table->string('batch_number',60)->nullable()->comment('批次号');
$table->date('date')->nullable()->comment('日期');; $table->date('date')->nullable()->comment('日期');;
$table->Integer('num')->default(0)->comment('采购数量'); $table->Integer('num')->default(0)->comment('采购数量');
$table->unsignedDecimal('cost')->default(0)->comment('成本'); $table->unsignedDecimal('cost')->default(0)->comment('成本');
@ -29,6 +29,7 @@ class CreatePurchaseRecordsTable extends Migration
// 索引 // 索引
$table->index('external_sku_id'); $table->index('external_sku_id');
$table->index('created_at'); $table->index('created_at');
$table->index('batch_number');
$table->timestamps(); $table->timestamps();
}); });
} }

View File

@ -15,8 +15,8 @@ class CreateLossRecordsTable extends Migration
{ {
Schema::create('loss_records', function (Blueprint $table) { Schema::create('loss_records', function (Blueprint $table) {
$table->bigIncrements('id'); $table->bigIncrements('id');
$table->string('external_sku_id',100)->comment('唯一sku标识'); $table->string('external_sku_id',60)->comment('唯一sku标识');
$table->string('batch_number')->nullable()->comment('批次号'); $table->string('batch_number',60)->nullable()->comment('批次号');
$table->date('date')->nullable()->comment('日期'); $table->date('date')->nullable()->comment('日期');
$table->Integer('num')->default(0)->comment('报损数量'); $table->Integer('num')->default(0)->comment('报损数量');
$table->unsignedDecimal('cost')->default(0)->comment('成本'); $table->unsignedDecimal('cost')->default(0)->comment('成本');
@ -28,6 +28,7 @@ class CreateLossRecordsTable extends Migration
// 索引 // 索引
$table->index('external_sku_id'); $table->index('external_sku_id');
$table->index('created_at'); $table->index('created_at');
$table->index('batch_number');
$table->timestamps(); $table->timestamps();
}); });
} }

View File

@ -21,8 +21,8 @@ class CreateSuppliersTable extends Migration
$table->string('link_tel',100)->nullable()->comment('联系方式'); $table->string('link_tel',100)->nullable()->comment('联系方式');
$table->string('payment_account',100)->nullable()->comment('支付方式'); $table->string('payment_account',100)->nullable()->comment('支付方式');
$table->string('supply_type')->nullable()->comment('供应类型'); $table->string('supply_type')->nullable()->comment('供应类型');
$table->string('agent_name')->nullable()->comment('代理人名称'); $table->string('agent_name')->nullable()->comment('开发维护人');
$table->Integer('agent_id')->nullable()->comment('代理人id'); $table->Integer('agent_id')->nullable()->comment('开发维护人id');
$table->index('supplier_name'); $table->index('supplier_name');
$table->timestamps(); $table->timestamps();