erp/app/Imports/InventoryImport.php

104 lines
3.5 KiB
PHP
Raw Normal View History

2022-09-08 01:02:07 +08:00
<?php
namespace App\Imports;
2023-09-06 17:02:48 +08:00
use App\Jobs\SyncCostToMiaoXuan;
2022-09-08 01:02:07 +08:00
use App\Models\DailyStockRecord;
use App\Models\GoodsSku;
2023-04-21 17:46:59 +08:00
use App\Models\TodayPrice;
2022-09-08 01:02:07 +08:00
use App\Utils\DateTimeUtils;
use Exception;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
2023-04-21 17:46:59 +08:00
use Maatwebsite\Excel\Concerns\ToArray;
2022-09-08 01:02:07 +08:00
use App\Utils\ArrayUtils;
2022-09-17 01:53:58 +08:00
use App\Events\StockUpdateEvent;
2022-09-08 01:02:07 +08:00
2023-04-21 17:46:59 +08:00
class InventoryImport implements ToArray, SkipsEmptyRows
2022-09-08 01:02:07 +08:00
{
/**
* @throws Exception
*/
2023-04-21 17:46:59 +08:00
public function array(array $collection)
2022-09-08 01:02:07 +08:00
{
2023-04-21 17:46:59 +08:00
$header = $collection[0];
2022-09-08 01:02:07 +08:00
unset($collection[0]);
2023-04-21 17:46:59 +08:00
$externalSkuId = [];
2022-09-08 01:02:07 +08:00
foreach ($collection as &$row) {
$row = array_map(static function ($v) {
return trim($v);
}, $row);
2023-04-21 17:46:59 +08:00
$externalSkuId[] = $row[0];
2022-09-08 01:02:07 +08:00
}
unset($row);
2023-04-21 17:46:59 +08:00
$updateIds = $todayPrice = [];
2022-09-08 01:02:07 +08:00
$day = DateTimeUtils::getToday();
$dateTime = date('Y-m-d H:i:s');
2023-01-27 23:03:38 +08:00
$hasGoodsSkus = GoodsSku::query()
2023-04-21 17:46:59 +08:00
->whereIn('external_sku_id', $externalSkuId)
->get(['id', 'status', 'external_sku_id'])
2023-01-27 23:03:38 +08:00
->toArray();
2023-04-21 17:46:59 +08:00
$hasGoodsSkus = ArrayUtils::index($hasGoodsSkus, 'external_sku_id');
2022-09-17 01:53:58 +08:00
foreach ($collection as $row) {
2023-04-21 17:46:59 +08:00
if (!isset($hasGoodsSkus[$row[0]])) {
2022-09-17 01:53:58 +08:00
continue;
}
2023-04-21 17:46:59 +08:00
$goodsSku = $hasGoodsSkus[$row[0]];
2023-01-27 23:03:38 +08:00
if ('下架' === $goodsSku['status']) {
GoodsSku::query()->where('id', $goodsSku['id'])->update([
2023-04-21 17:46:59 +08:00
'stock' => $row[2] + $row[3],
'cost' => $row[4],
2023-01-27 23:03:38 +08:00
'status' => 1,
]);
} else {
GoodsSku::query()->where('id', $goodsSku['id'])->update([
2023-04-21 17:46:59 +08:00
'stock' => $row[2] + $row[3],
'cost' => $row[4],
2023-01-27 23:03:38 +08:00
]);
2022-09-17 01:53:58 +08:00
}
2023-09-06 17:02:48 +08:00
SyncCostToMiaoXuan::dispatch($row[0], $row[4]);
2023-01-27 23:03:38 +08:00
$updateIds[] = $goodsSku['id'];
DailyStockRecord::query()->where('sku_id', $goodsSku['id'])->where('day', $day)->update([
2023-04-21 17:46:59 +08:00
'arrived_today_num' => $row[3],
'inventory' => $row[2],
2022-09-17 01:53:58 +08:00
'inventory_time' => $dateTime
]);
2023-04-21 17:46:59 +08:00
$shopPrice = [];
foreach ($row as $i => $v) {
2023-04-23 09:50:52 +08:00
if ($i > 4) {
2023-04-21 17:46:59 +08:00
$shopPrice[$header[$i]] = $v;
}
}
$todayPrice[] = [
'day' => $day,
'external_sku_id' => $goodsSku['external_sku_id'],
'shop_price' => json_encode($shopPrice, 256)
];
}
if ($todayPrice) {
TodayPrice::query()->delete();
$model = new TodayPrice();
$model->batchInsert($todayPrice);
2022-09-08 01:02:07 +08:00
}
2023-01-27 23:03:38 +08:00
sleep(2);
2022-09-08 01:02:07 +08:00
$onSkuIds = GoodsSku::query()
2023-04-18 11:22:16 +08:00
->where('is_combination', 0)
2022-11-03 17:02:24 +08:00
->where('status', '>', 0)
2022-09-08 01:02:07 +08:00
->pluck('id')
->toArray();
2022-09-14 13:18:25 +08:00
$downSkuIds = array_diff($onSkuIds, $updateIds);
if ($downSkuIds) {
$goodsSkus = GoodsSku::query()->whereIn('id', $downSkuIds)
->get(['id', 'yesterday_num', 'stock'])
->toArray();
foreach ($goodsSkus as $goodsSku) {
GoodsSku::query()->where('id', $goodsSku['id'])->update([
'yesterday_num' => $goodsSku['yesterday_num'] - $goodsSku['stock'],
'stock' => 0,
]);
}
2022-09-08 01:02:07 +08:00
}
2022-11-08 10:26:16 +08:00
sleep(2);
2022-11-04 16:45:22 +08:00
event(new StockUpdateEvent($onSkuIds, 1));
2022-09-08 01:02:07 +08:00
}
}