mirror of
https://gitee.com/hzchunfen/erp.git
synced 2025-12-01 14:40:44 +00:00
71 lines
2.5 KiB
PHP
71 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Imports;
|
|
|
|
use App\Events\BatchStockUpdateEvent;
|
|
use App\Jobs\SyncCostToMiaoXuan;
|
|
use App\Models\DailyStockRecord;
|
|
use App\Models\GoodsSku;
|
|
use App\Models\PurchaseRecords;
|
|
use App\Utils\DateTimeUtils;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
|
|
use Maatwebsite\Excel\Concerns\ToArray;
|
|
use App\Utils\ArrayUtils;
|
|
|
|
class PurchaseImport implements ToArray, SkipsEmptyRows
|
|
{
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function array(array $collection)
|
|
{
|
|
unset($collection[0]);
|
|
$externalSkuIds = [];
|
|
foreach ($collection as &$row) {
|
|
$row = array_map(static function ($v) {
|
|
return trim($v);
|
|
}, $row);
|
|
$externalSkuIds[] = $row[0];
|
|
}
|
|
unset($row);
|
|
$updateIds = [];
|
|
$hasGoodsSkus = GoodsSku::query()
|
|
->whereIn('external_sku_id', $externalSkuIds)
|
|
->get(['id', 'status', 'external_sku_id', 'stock'])
|
|
->toArray();
|
|
$hasGoodsSkus = ArrayUtils::index($hasGoodsSkus, 'external_sku_id');
|
|
//excel字段排序 編碼 商品名稱 导购數量 成本价 采购人名称 供应商名称
|
|
foreach ($collection as $row) {
|
|
if (!isset($hasGoodsSkus[$row[0]])) {
|
|
continue;
|
|
}
|
|
//执行库存操作
|
|
$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->buyer_name = $row[4] ?? '';
|
|
$purchaseRecords->supplier_name = $row[5] ?? '';
|
|
$purchaseRecords->save();
|
|
|
|
//更新库存
|
|
GoodsSku::query()->where('external_sku_id', $row[0])->update([
|
|
'stock' => $goodsSkuItem['stock'] + $row[2],
|
|
'sale_stock' => $goodsSkuItem['sale_stock'] + $row[2],
|
|
'cost' => number_format(($goodsSkuItem['stock']*$goodsSkuItem['cost']+$row[3]*$row[2])
|
|
/($goodsSkuItem['stock'] + $row[2]),2),
|
|
'status' => 1,
|
|
]);
|
|
$updateIds[] = $hasGoodsSkus['id'];
|
|
}
|
|
Log::info("采购导入内容:",$collection);
|
|
// 批量更新
|
|
event(new BatchStockUpdateEvent($updateIds));
|
|
}
|
|
}
|