| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?php
- namespace App\Imports;
- use App\Services\CommodityService;
- use Carbon\Carbon;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Cache;
- use Maatwebsite\Excel\Concerns\ToCollection;
- use Maatwebsite\Excel\Concerns\WithHeadingRow;
- use Maatwebsite\Excel\Imports\HeadingRowFormatter;
- HeadingRowFormatter::default('none');
- class StoreCheckingReceiveImport implements ToCollection,WithHeadingRow
- {
- /** @var bool $isOverride
- * 是否覆盖
- */
- protected $isOverride;
- public function __construct($isOverride)
- {
- $this->isOverride=$isOverride ? true : false;
- }
- /**
- * @param Collection $collection
- * @return bool
- * @throws \Exception
- */
- public function collection(Collection $collection)
- {
- $row = $collection->first();
- $header = [
- "货主","SKU","商品名称","条码","数量","生产日期","失效日期","批号","唯一码"
- ];
- foreach ($header as $str){
- if (!($row[$str] ?? false)){
- Cache::put("storeCheckingReceive",["success"=>false, "data"=>"表头不存在“".$str."”"],86400);
- return false;
- }
- }
- $owner_name = null;
- $items = [];
- $errors = [];
- $commodities = [];
- $barcodes = [];
- //去重 筛选 错误
- foreach ($collection as $index => $item){
- if (!$owner_name && $item["货主"]) $owner_name = $item["货主"];
- if (!$item["条码"] || !$item["数量"] || !is_numeric($item["数量"])){
- array_push($errors,($index+2)." 行:条码或数量不存在");
- continue;
- }
- if ($item["生产日期"]){
- if (is_numeric($item["生产日期"])){
- $diff = intval($item["生产日期"]);
- $today=new Carbon('1900-01-01');
- $day = $today->addDays($diff-2);
- $item["生产日期"] = $day;
- }else{
- if (!(strtotime($item["生产日期"]) ? true : false)){
- array_push($errors,($index+2)." 行:生产日期格式错误");
- continue;
- }
- }
- }
- if ($item["失效日期"] && !(strtotime($item["失效日期"]) ? true : false)){
- if (is_numeric($item["失效日期"])){
- $diff = intval($item["失效日期"]);
- $today=new Carbon('1900-01-01');
- $day = $today->addDays($diff-2);
- $item["失效日期"] = $day;
- }else{
- if (!(strtotime($item["失效日期"]) ? true : false)){
- array_push($errors,($index+2)." 行:失效日期格式错误");
- continue;
- }
- }
- }
- $isUniqueCommodity = isset($commodities[$item["条码"]]);
- if ($isUniqueCommodity){
- $line = &$items[$commodities[$item["条码"]]];
- if ($line["produced_at"] == $item["生产日期"] && $line["invalid_at"] == $item["失效日期"]
- && $line["batch_code"] == $item["批号"] && $line["unique_code"] == $item["唯一码"]) {
- $line["imported_amount"] += $item["数量"];
- array_push($errors, ($index + 2) . " 行:重复数据已合并数量");
- continue;
- }
- }
- array_push($items,[
- "bin_number" => $item["格口号"],
- "imported_amount" => $item["数量"],
- "produced_at" => $item["生产日期"],
- "invalid_at" => $item["失效日期"],
- "batch_code" => $item["批号"],
- "unique_code" => $item["唯一码"],
- "SKU" => $item["SKU"],
- "商品名称" => $item["商品名称"],
- "条码" => $item["条码"],
- "lineno" => $index+2,
- ]);
- if ($isUniqueCommodity){
- if (is_array($commodities[$item["条码"]])) array_push($commodities[$item["条码"]], count($items)-1);
- else $commodities[$item["条码"]] = [$commodities[$item["条码"]], count($items)-1];
- }else{
- $commodities[$item["条码"]] = count($items)-1 ;
- array_push($barcodes,$item["条码"]);
- }
- }
- //不存在货主
- if (!$owner_name){
- Cache::put("storeCheckingReceive",["success"=>false, "data"=>"货主为空,不允许录入,至少为一行标明货主!"],86400);
- return false;
- }
- //使用条码获取商品ID
- $commodityBarCodes = app('commodityBarcodeService')->getCommodities($barcodes);
- foreach ($commodityBarCodes as $barcode){
- if (!is_array($commodities[$barcode->code])){
- $commodities[$barcode->code] = [$commodities[$barcode->code]];
- }
- foreach ($commodities[$barcode->code] as $index){
- $items[$index]["commodity_id"] = $barcode->commodity_id;
- unset($items[$index]["SKU"]);
- unset($items[$index]["商品名称"]);
- unset($items[$index]["条码"]);
- unset($items[$index]["lineno"]);
- }
- unset($commodities[$barcode->code]);
- }
- $owner = app('ownerService')->firstOrCreate(["name"=>$owner_name],["name"=>$owner_name, "code"=>$owner_name]);
- //填充商品ID,未找到项流转下一步,空SKU删除该行并返回错误
- $skus = [];
- $skuParam = [];
- foreach ($commodities as $key => $value){
- if (!is_array($value)){
- $value = [$value];
- }
- foreach ($value as $i => $index){
- if (!$items[$index]["SKU"]){
- array_push($errors, ($items[$index]["lineno"]) . " 行:条码未找到商品时SKU不得为空");
- unset($items[$index]);
- if ($i == count($value)-1) unset($commodities[$key]);
- continue;
- }
- if (isset($skus[$items[$index]["SKU"]])) array_push($skus, $index);
- else{
- $skus[$items[$index]["SKU"]] = [$index];
- array_push($skuParam,$items[$index]["SKU"]);
- }
- }
- }
- //使用货主+SKU获取商品ID,将条码补录
- $barcodes = [];
- /** @var CommodityService $commodityService */
- $commodityService = app('commodityService');
- if (count($skuParam) > 0)$commodities = $commodityService->get(["owner_id"=>$owner->id,"sku"=>$skuParam]);
- foreach ($commodities as $commodity){
- foreach ($skus[$commodity->sku] as $i => $index){
- $items[$index]["commodity_id"] = $commodity->id;
- if ($i == 0){
- array_push($barcodes,[
- "code" => $items[$index]["条码"],
- "commodity_id" => $commodity->id,
- "created_at" => date('Y-m-d H:i:s')
- ]);
- }
- unset($items[$index]["SKU"]);
- unset($items[$index]["商品名称"]);
- unset($items[$index]["条码"]);
- unset($items[$index]["lineno"]);
- }
- unset($skus[$commodity->sku]);
- }
- if (count($barcodes) > 0) app('commodityBarcodeService')->insert($barcodes);
- //未找到商品录入商品
- $commodities = [];
- $skuParam = [];
- $barcodes = [];
- foreach ($skus as $sku => $arr){
- $data = &$items[$arr[0]];
- array_push($commodities,[
- "name" => $data["商品名称"],
- "sku" => $data["SKU"],
- "owner_id" => $owner->id,
- "created_at" => date('Y-m-d H:i:s'),
- ]);
- array_push($skuParam, $sku);
- }
- if (count($commodities) > 0)$commodityService->insert($commodities);
- if (count($skuParam) > 0)$commodities = $commodityService->get(["owner_id"=>$owner->id,"sku"=>$skuParam]);
- foreach ($commodities as $commodity){
- foreach ($skus[$commodity->sku] as $i => $index){
- $items[$index]["commodity_id"] = $commodity->id;
- if ($i == 0){
- array_push($barcodes,[
- "code" => $items[$index]["条码"],
- "commodity_id" => $commodity->id,
- "created_at" => date('Y-m-d H:i:s')
- ]);
- }
- unset($items[$index]["SKU"]);
- unset($items[$index]["商品名称"]);
- unset($items[$index]["条码"]);
- unset($items[$index]["lineno"]);
- }
- unset($skus[$commodity->sku]);
- }
- if (count($barcodes) > 0) app('commodityBarcodeService')->insert($barcodes);
- if (count($items) < 1){
- Cache::put("storeCheckingReceive",["success"=>false, "errors"=>$errors],86400);
- return false;
- }
- $storeCheckingReceive = app('storeCheckingReceiveService')->create([
- "owner_id" => $owner->id,
- "created_at" => date('Y-m-d H:i:s'),
- 'status' => "已导入",
- ]);
- $storeCheckingReceive->owner_name = $owner_name;
- foreach ($items as &$item){
- $item["store_checking_receive_id"] = $storeCheckingReceive->id;
- }
- app('storeCheckingReceiveItemService')->insert($items);
- Cache::put("storeCheckingReceive",["success"=>true,"data"=>$storeCheckingReceive,"errors"=>$errors],86400);
- return true;
- }
- }
|