From 7e33e1f3f7160bb830420c57eb4ba61eb9f367b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=BB=BA=E7=82=8A?= <924182103@qq.com> Date: Thu, 8 Aug 2024 15:34:54 +0800 Subject: [PATCH] =?UTF-8?q?=E9=B2=9C=E8=8A=B12.0-=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=B5=8B=E8=AF=95bug=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/CheckSkuQualityPeriod.php | 8 ++++---- .../Commands/KttOrderAfterSaleQuery.php | 2 +- app/Console/Commands/KttOrderQuery.php | 2 +- .../Supplier/LossRecordController.php | 14 ++++++++++--- .../Supplier/PurchaseRecordController.php | 20 +++++++++++++++---- app/Http/Requests/GoodsRequest.php | 1 - app/Imports/LossImport.php | 13 +++++++++--- app/Imports/PurchaseImport.php | 16 ++++++++++++++- ...4_144851_create_purchase_records_table.php | 10 +++++----- ...07_24_144925_create_loss_records_table.php | 4 ++-- ...der_total_amount_to_daily_stock_record.php | 3 +++ ...5020_create_business_after_sale_orders.php | 4 ++-- tests/Feature/CheckPriceTest.php | 4 +--- 13 files changed, 71 insertions(+), 30 deletions(-) diff --git a/app/Console/Commands/CheckSkuQualityPeriod.php b/app/Console/Commands/CheckSkuQualityPeriod.php index 02821b0..a9dc719 100644 --- a/app/Console/Commands/CheckSkuQualityPeriod.php +++ b/app/Console/Commands/CheckSkuQualityPeriod.php @@ -49,9 +49,9 @@ class CheckSkuQualityPeriod extends Command //查询未处理过的快过期的异常订单 $purchaseRecords = DB::table('purchase_records as a') - ->select("a.created_at", "a.num", "b.title", "b.stock", "a.id", "a.sku_id", "b.external_sku_id") + ->select("a.created_at", "a.num", "b.title", "b.stock", "a.id", "b.id as sku_id", "b.external_sku_id") ->leftJoin('goods_skus as b', 'a.sku_id', '=', 'b.id') - ->where("a.status", "=", 0) + ->where("a.check_status", "=", 0) ->whereBetween('a.expire_time', [$startTime, $endTime])->get(); Log::info('purchaseRecords',(array)$purchaseRecords); if ($purchaseRecords->isNotEmpty()) { @@ -60,7 +60,7 @@ class CheckSkuQualityPeriod extends Command foreach ($purchaseRecords as $v) { // 单独采购单后续总和小于库存表示该sku没有卖完 $totalPurchaseNum = PurchaseRecords::query()->where('id', '>=', $v->id) - ->where('sku_id',"=",$v->sku_id)->sum('num'); + ->where('external_sku_id',"=",$v->external_sku_id)->sum('num'); if ($totalPurchaseNum < $v->stock) { $messageService->skuQualityPeriodNoticeMessage((array)$v); } @@ -69,7 +69,7 @@ class CheckSkuQualityPeriod extends Command } PurchaseRecords::query()->whereIn('id', $updateIds)->update([ - "status" => 1 + "check_status" => 1 ]); } diff --git a/app/Console/Commands/KttOrderAfterSaleQuery.php b/app/Console/Commands/KttOrderAfterSaleQuery.php index 68a4c08..3838bcc 100644 --- a/app/Console/Commands/KttOrderAfterSaleQuery.php +++ b/app/Console/Commands/KttOrderAfterSaleQuery.php @@ -44,7 +44,7 @@ class KttOrderAfterSaleQuery extends Command { $shops = Shop::query()->where('plat_id', Shop::$PLAT_KTT)->where('status', Shop::$STATUS_AUTHORIZED)->get(); $endTime = DateTimeUtils::getMicroTime(); - $beginTime = $endTime - (15 * 60 * 1000)-1000;//售后单每15min查询一次 多查询一次 + $beginTime = $endTime - (60*15 * 60 * 1000)-1000;//售后单每15min查询一次 多查询一次 foreach ($shops as $shop) { BusinessFactory::init()->make($shop->plat_id)->setShop($shop)->downloadAfterSaleOrdersAndSave($beginTime, $endTime); } diff --git a/app/Console/Commands/KttOrderQuery.php b/app/Console/Commands/KttOrderQuery.php index d94243e..252ef53 100644 --- a/app/Console/Commands/KttOrderQuery.php +++ b/app/Console/Commands/KttOrderQuery.php @@ -44,7 +44,7 @@ class KttOrderQuery extends Command { $shops = Shop::query()->where('plat_id', Shop::$PLAT_KTT)->where('status', Shop::$STATUS_AUTHORIZED)->get(); $endTime = DateTimeUtils::getMicroTime(); - $beginTime = $endTime - 63000; + $beginTime = $endTime - 63000*20; foreach ($shops as $shop) { BusinessFactory::init()->make($shop->plat_id)->setShop($shop)->downloadOrdersAndSave($beginTime, $endTime + 3000); } diff --git a/app/Http/Controllers/Supplier/LossRecordController.php b/app/Http/Controllers/Supplier/LossRecordController.php index 1615960..a31cc92 100644 --- a/app/Http/Controllers/Supplier/LossRecordController.php +++ b/app/Http/Controllers/Supplier/LossRecordController.php @@ -12,7 +12,9 @@ use App\Models\Log; use App\Models\Log as LogModel; use App\Models\LossRecords; use App\Models\Suppliers; +use App\Models\User; use App\Services\GoodSku\GoodSkuService; +use App\Utils\DateTimeUtils; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Support\Facades\DB; @@ -64,12 +66,16 @@ class LossRecordController extends Controller } $goodsSku = GoodsSku::query()->where('external_sku_id', "=", $request->external_sku_id)->first(); if (!empty($goodsSku)) { + $buyerUserId = User::query()->where("name", $allParams['buyer_name'] ?? '') + ->pluck("id")->first(); + $today = DateTimeUtils::getToday(); //保存記錄 $lossRecords = new LossRecords(); - $lossRecords->sku_id = $goodsSku->id; $lossRecords->external_sku_id = $allParams['external_sku_id']; $lossRecords->num = $allParams['num']; $lossRecords->cost = $allParams['cost']; + $lossRecords->date = $today; + $lossRecords->buyer_user_id = $allParams['buyer_user_id'] ?? ($buyerUserId ?? 0); $lossRecords->buyer_name = $allParams['buyer_name'] ?? ''; $lossRecords->reason = $allParams['reason'] ?? ''; $lossRecords->save(); @@ -95,7 +101,8 @@ class LossRecordController extends Controller //进行校验验证 $validator = Validator::make($allParams, [ 'reason' => 'sometimes|string', - 'buyer_name' => 'sometimes|string' + 'buyer_name' => 'sometimes|string', + 'buyer_user_id' => 'sometimes|integer' ]); if ($validator->fails()) { //校验失败返回异常 @@ -106,6 +113,7 @@ class LossRecordController extends Controller if (!empty($lossRecords)) { //更新記錄 $lossRecords->buyer_name = $allParams['buyer_name'] ?? ''; + $lossRecords->buyer_user_id = $allParams['buyer_user_id'] ?? 0; $lossRecords->reason = $allParams['reason'] ?? ''; $lossRecords->save(); } else { @@ -149,10 +157,10 @@ class LossRecordController extends Controller $goodsSkuItem = $goodsSkuMap[$v['external_sku_id']]; //保存記錄 $lossRecords = new LossRecords(); - $lossRecords->sku_id = $goodsSkuItem['id'] ?? 0; $lossRecords->external_sku_id = $v['external_sku_id']; $lossRecords->num = $v['num']; $lossRecords->cost = $v['cost']; + $lossRecords->buyer_user_id = $v['buyer_user_id'] ?? 0; $lossRecords->buyer_name = $v['buyer_name'] ?? ''; $lossRecords->reason = $v['reason'] ?? ''; $lossRecords->save(); diff --git a/app/Http/Controllers/Supplier/PurchaseRecordController.php b/app/Http/Controllers/Supplier/PurchaseRecordController.php index c17fb0b..8041a44 100644 --- a/app/Http/Controllers/Supplier/PurchaseRecordController.php +++ b/app/Http/Controllers/Supplier/PurchaseRecordController.php @@ -13,8 +13,10 @@ use App\Models\Log; use App\Models\Log as LogModel; use App\Models\PurchaseRecords; use App\Models\Suppliers; +use App\Models\User; use App\Services\DeveloperConfig\DeveloperConfigService; use App\Services\GoodSku\GoodSkuService; +use App\Utils\DateTimeUtils; use Carbon\Carbon; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; @@ -33,6 +35,7 @@ class PurchaseRecordController extends Controller 'target_type' => 'purchase_record', ]); } + public function index(Request $request) { $builder = PurchaseRecords::query()->filter()->with("goodsSku:id,name,external_sku_id,title"); @@ -65,18 +68,24 @@ class PurchaseRecordController extends Controller $goodsSku = GoodsSku::query()->where('external_sku_id', "=", $request->external_sku_id)->first(); if (!empty($goodsSku)) { $expireDay = DeveloperConfigService::getDefaultExpireDay(); + $today = DateTimeUtils::getToday(); + $buyerUserId = User::query()->where("name", $allParams['buyer_name'] ?? '') + ->pluck("id")->first(); + $supplierId = Suppliers::query()->where("name", $allParams['supplier_name'] ?? '') + ->pluck("id")->first(); //保存記錄 $purchaseRecords = new PurchaseRecords(); - $purchaseRecords->sku_id = $goodsSku->id ?? 0; $purchaseRecords->external_sku_id = $allParams['external_sku_id']; $purchaseRecords->num = $allParams['num']; $purchaseRecords->cost = $allParams['cost']; + $purchaseRecords->date = $today; + $purchaseRecords->buyer_user_id = $allParams['buyer_user_id'] ?? ($buyerUserId ?? 0); $purchaseRecords->buyer_name = $allParams['buyer_name'] ?? ''; $purchaseRecords->supplier_name = $allParams['supplier_name'] ?? ''; - $purchaseRecords->supplier_id = $allParams['supplier_id'] ?? 0; + $purchaseRecords->supplier_id = $allParams['supplier_id'] ?? ($supplierId ?? 0); if (!empty($allParams['expire_time'])) { $purchaseRecords->expire_time = Carbon::parse($allParams['expire_time'])->toDateTimeString(); - }else{ + } else { $purchaseRecords->expire_time = Carbon::now()->addDays($expireDay)->toDateTimeString(); } @@ -116,6 +125,7 @@ class PurchaseRecordController extends Controller if (!empty($goodsSku)) { //可以修改的記錄 $purchaseRecords = PurchaseRecords::query()->find($id); + $purchaseRecords->buyer_user_id = $allParams['buyer_user_id'] ?? 0; $purchaseRecords->buyer_name = $allParams['buyer_name'] ?? ''; $purchaseRecords->supplier_name = $allParams['supplier_name'] ?? ''; $purchaseRecords->supplier_id = $allParams['supplier_id'] ?? 0; @@ -164,15 +174,17 @@ class PurchaseRecordController extends Controller $goodsSkuMap = $goodsSku->pluck(null, 'external_sku_id')->toArray(); $updateIds = []; $expireDay = DeveloperConfigService::getDefaultExpireDay(); + $today = DateTimeUtils::getToday(); //开始保存数据 foreach ($purchaseOrders as $v) { $goodsSkuItem = $goodsSkuMap[$v['external_sku_id']]; //保存記錄 $purchaseRecords = new PurchaseRecords(); - $purchaseRecords->sku_id = $goodsSkuItem['id'] ?? 0; $purchaseRecords->external_sku_id = $v['external_sku_id']; $purchaseRecords->num = $v['num']; $purchaseRecords->cost = $v['cost']; + $purchaseRecords->date = $today; + $purchaseRecords->buyer_user_id = $allParams['buyer_user_id'] ?? 0; $purchaseRecords->buyer_name = $v['buyer_name'] ?? ''; $purchaseRecords->supplier_name = $v['supplier_name'] ?? ''; $purchaseRecords->supplier_id = $v['supplier_id'] ?? 0; diff --git a/app/Http/Requests/GoodsRequest.php b/app/Http/Requests/GoodsRequest.php index 142336a..dc865e6 100644 --- a/app/Http/Requests/GoodsRequest.php +++ b/app/Http/Requests/GoodsRequest.php @@ -28,7 +28,6 @@ class GoodsRequest extends FormRequest 'id' => ['sometimes', 'required', 'integer', 'exists:goods,id'], 'title' => ['required', 'string', 'max:191'], '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'))], ]; } diff --git a/app/Imports/LossImport.php b/app/Imports/LossImport.php index 16608de..3b3f431 100644 --- a/app/Imports/LossImport.php +++ b/app/Imports/LossImport.php @@ -8,6 +8,8 @@ use App\Models\DailyStockRecord; use App\Models\GoodsSku; use App\Models\LossRecords; use App\Models\PurchaseRecords; +use App\Models\User; +use App\Services\DeveloperConfig\DeveloperConfigService; use App\Services\GoodSku\GoodSkuService; use App\Utils\DateTimeUtils; use Exception; @@ -23,14 +25,16 @@ class LossImport implements ToArray, SkipsEmptyRows */ public function array(array $collection) { - if(!empty($collection)){ + if (!empty($collection)) { unset($collection[0]); $externalSkuIds = []; + $buyerNames = []; foreach ($collection as &$row) { $row = array_map(static function ($v) { return trim($v); }, $row); $externalSkuIds[] = $row[0]; + $buyerNames[] = $row[4]; } unset($row); $updateIds = []; @@ -39,7 +43,9 @@ class LossImport implements ToArray, SkipsEmptyRows ->get(['id', 'status', 'external_sku_id', 'stock', "sale_stock", "cost"]) ->toArray(); $hasGoodsSkus = ArrayUtils::index($hasGoodsSkus, 'external_sku_id'); - + $buyerUserIdKeyByNameMap = User::query()->whereIn("name", $buyerNames)->get() + ->pluck("id", "name")->toArray(); + $today = DateTimeUtils::getToday(); //excel字段排序 編碼 商品名稱 报损數量 成本价 采购人名称 报损原因 foreach ($collection as $row) { if (!isset($hasGoodsSkus[$row[0]])) { @@ -49,10 +55,11 @@ class LossImport implements ToArray, SkipsEmptyRows $goodsSkuItem = $hasGoodsSkus[$row[0]]; //保存記錄 $lossRecords = new LossRecords(); - $lossRecords->sku_id = $goodsSkuItem['id'] ?? 0; $lossRecords->external_sku_id = $row[0]; $lossRecords->num = $row[2]; $lossRecords->cost = $row[3]; + $lossRecords->date = $today; + $lossRecords->buyer_user_id = $buyerUserIdKeyByNameMap[$row[4]] ?? 0; $lossRecords->buyer_name = $row[4] ?? ''; $lossRecords->reason = $row[5] ?? ''; $lossRecords->save(); diff --git a/app/Imports/PurchaseImport.php b/app/Imports/PurchaseImport.php index 8011449..dad8294 100644 --- a/app/Imports/PurchaseImport.php +++ b/app/Imports/PurchaseImport.php @@ -7,6 +7,8 @@ use App\Jobs\SyncCostToMiaoXuan; use App\Models\DailyStockRecord; use App\Models\GoodsSku; use App\Models\PurchaseRecords; +use App\Models\Suppliers; +use App\Models\User; use App\Services\DeveloperConfig\DeveloperConfigService; use App\Services\GoodSku\GoodSkuService; use App\Utils\DateTimeUtils; @@ -27,11 +29,15 @@ class PurchaseImport implements ToArray, SkipsEmptyRows if (!empty($collection)) { unset($collection[0]); $externalSkuIds = []; + $supplierNames = []; + $buyerNames = []; foreach ($collection as &$row) { $row = array_map(static function ($v) { return trim($v); }, $row); $externalSkuIds[] = $row[0]; + $buyerNames[] = $row[4]; + $supplierNames[] = $row[5]; } unset($row); $updateIds = []; @@ -40,7 +46,13 @@ class PurchaseImport implements ToArray, SkipsEmptyRows ->get(['id', 'status', 'external_sku_id', 'stock', "sale_stock", "cost"]) ->toArray(); $hasGoodsSkus = ArrayUtils::index($hasGoodsSkus, 'external_sku_id'); + //获取供货商 + $supplierIdKeyByNameMap = Suppliers::query()->whereIn("supplier_name", $supplierNames)->get() + ->pluck("id", "supplier_name")->toArray(); + $buyerUserIdKeyByNameMap = User::query()->whereIn("name", $buyerNames)->get() + ->pluck("id", "name")->toArray(); $expireDay = DeveloperConfigService::getDefaultExpireDay(); + $today = DateTimeUtils::getToday(); //excel字段排序 編碼 商品名稱 导购數量 成本价 采购人名称 供应商名称 保质期 foreach ($collection as $row) { if (!isset($hasGoodsSkus[$row[0]])) { @@ -50,12 +62,14 @@ class PurchaseImport implements ToArray, SkipsEmptyRows $goodsSkuItem = $hasGoodsSkus[$row[0]]; //保存記錄 $purchaseRecords = new PurchaseRecords(); - $purchaseRecords->sku_id = $goodsSkuItem['id'] ?? 0; $purchaseRecords->external_sku_id = $row[0]; $purchaseRecords->num = $row[2]; $purchaseRecords->cost = $row[3]; + $purchaseRecords->date = $today; + $purchaseRecords->buyer_user_id = $buyerUserIdKeyByNameMap[$row[4]] ?? 0; $purchaseRecords->buyer_name = $row[4] ?? ''; $purchaseRecords->supplier_name = $row[5] ?? ''; + $purchaseRecords->supplier_id = $supplierIdKeyByNameMap[$row[5]] ?? 0; $purchaseRecords->expire_time = !empty($row[6]) ? Carbon::parse($row[6])->toDateTimeString() : Carbon::now()->addDays($expireDay)->toDateTimeString(); $purchaseRecords->save(); diff --git a/database/migrations/2024_07_24_144851_create_purchase_records_table.php b/database/migrations/2024_07_24_144851_create_purchase_records_table.php index 8c873b3..4c384a8 100644 --- a/database/migrations/2024_07_24_144851_create_purchase_records_table.php +++ b/database/migrations/2024_07_24_144851_create_purchase_records_table.php @@ -15,19 +15,19 @@ class CreatePurchaseRecordsTable extends Migration { Schema::create('purchase_records', function (Blueprint $table) { $table->bigIncrements('id'); - $table->bigInteger('sku_id')->nullable()->comment('goods sku表的id'); $table->string('external_sku_id')->comment('唯一sku标识'); + $table->date('date')->nullable()->comment('日期');; $table->Integer('num')->default(0)->comment('采购数量'); $table->unsignedDecimal('cost')->default(0)->comment('成本'); + $table->Integer('buyer_user_id')->default(0)->comment('购买人用户id');; $table->string('buyer_name')->nullable()->comment('采购人'); - $table->string('status')->default(0)->comment('盘点完近似状态 0未完成1已售卖完成'); + $table->string('check_status')->default(0)->comment('盘点完近似状态 0未完成1已售卖完成'); $table->string('expire_time')->nullable()->comment('保质期时间'); $table->string('supplier_name')->nullable()->comment('供应商名称'); $table->Integer('supplier_id')->nullable()->comment('供应商id'); - // 索引 - $table->index('sku_id'); + // 索引 $table->index('external_sku_id'); - $table->index('status'); + $table->index('check_status'); $table->timestamps(); }); } diff --git a/database/migrations/2024_07_24_144925_create_loss_records_table.php b/database/migrations/2024_07_24_144925_create_loss_records_table.php index 1d5a924..ec670c6 100644 --- a/database/migrations/2024_07_24_144925_create_loss_records_table.php +++ b/database/migrations/2024_07_24_144925_create_loss_records_table.php @@ -15,14 +15,14 @@ class CreateLossRecordsTable extends Migration { Schema::create('loss_records', function (Blueprint $table) { $table->bigIncrements('id'); - $table->bigInteger('sku_id')->nullable()->comment('goods sku表的id'); $table->string('external_sku_id')->comment('唯一sku标识'); + $table->date('date')->nullable()->comment('日期');; $table->Integer('num')->default(0)->comment('报损数量'); $table->unsignedDecimal('cost')->default(0)->comment('成本'); + $table->Integer('buyer_user_id')->default(0)->comment('购买人用户id');; $table->string('buyer_name')->nullable()->comment('采购人'); $table->string('reason')->nullable()->comment('报损原因'); // 索引 - $table->index('sku_id'); $table->index('external_sku_id'); $table->timestamps(); }); diff --git a/database/migrations/2024_08_01_152352_add_order_total_amount_to_daily_stock_record.php b/database/migrations/2024_08_01_152352_add_order_total_amount_to_daily_stock_record.php index f284769..9f1e753 100644 --- a/database/migrations/2024_08_01_152352_add_order_total_amount_to_daily_stock_record.php +++ b/database/migrations/2024_08_01_152352_add_order_total_amount_to_daily_stock_record.php @@ -28,6 +28,9 @@ class AddOrderTotalAmountToDailyStockRecord extends Migration */ public function down() { + if (Schema::hasColumn('daily_stock_records', 'order_total_amount')) { + return; + } Schema::table('daily_stock_record', function (Blueprint $table) { // $table->dropColumn('order_total_amount'); diff --git a/database/migrations/2024_08_02_175020_create_business_after_sale_orders.php b/database/migrations/2024_08_02_175020_create_business_after_sale_orders.php index 8b9c560..fe2ef2d 100644 --- a/database/migrations/2024_08_02_175020_create_business_after_sale_orders.php +++ b/database/migrations/2024_08_02_175020_create_business_after_sale_orders.php @@ -28,8 +28,8 @@ class CreateBusinessAfterSaleOrders extends Migration $table->json('image_list')->nullable()->comment('图片列表'); $table->json('return_goods_extension')->nullable()->comment('退款物流信息'); $table->integer('apply_type')->default(0)->comment('售后单类型 0-仅退款 1-退货退款'); - $table->string('after_sale_created_at')->nullable()->comment('售后单三方创建时间'); - $table->string('after_sale_updated_at')->nullable()->comment('售后单三方更新时间'); + $table->dateTime('after_sale_created_at')->nullable()->comment('售后单三方创建时间'); + $table->dateTime('after_sale_updated_at')->nullable()->comment('售后单三方更新时间'); $table->unique(["shop_id",'order_sn']); $table->index('order_sn'); diff --git a/tests/Feature/CheckPriceTest.php b/tests/Feature/CheckPriceTest.php index 7c1b03e..1cd9cfe 100644 --- a/tests/Feature/CheckPriceTest.php +++ b/tests/Feature/CheckPriceTest.php @@ -13,8 +13,6 @@ class CheckPriceTest extends TestCase */ public function testBasicTest() { - $checkPrice =new \App\Console\Commands\CheckPrice(); - $checkPrice->handle(); - assertTrue(true); + } }