cqp 2 месяцев назад
Родитель
Сommit
f7a92326ea
1 измененных файлов с 137 добавлено и 71 удалено
  1. 137 71
      app/Service/ImportService.php

+ 137 - 71
app/Service/ImportService.php

@@ -1425,9 +1425,15 @@ class ImportService extends Service
             $allMonthsTs[] = $valMonthTs;
             if (!empty($row[$empIdx])) $allEmpNumbers[] = trim($row[$empIdx]);
         }
+        if (!empty($errors)) return [implode('|', $errors), [], []];
+
         $allMonthsTs = array_unique($allMonthsTs);
         $allEmpNumbers = array_unique($allEmpNumbers);
 
+        //归档
+        list($status, $msg) = ArchiveService::isArchive($allMonthsTs, $user);
+        if(! $status)  return [$msg, [], []];
+
         // --- 2. 批量预加载 ---
         // A. 人员档案
         $empModels = Employee::where('del_time', 0)->whereIn('number', $allEmpNumbers)
@@ -1674,8 +1680,14 @@ class ImportService extends Service
             $allMonthsTs[] = $valMonthTs;
             if (!empty($row[$devIdx])) $allDevCodes[] = trim($row[$devIdx]);
         }
+        if (!empty($errors)) return [implode('|', $errors), [], []];
+
         $allMonthsTs = array_unique($allMonthsTs);
 
+        //归档
+        list($status, $msg) = ArchiveService::isArchive($allMonthsTs, $user);
+        if(! $status)  return [$msg, [], []];
+
         // --- 2. 预加载档案与基准 ---
         $devModels = Device::where('del_time', 0)
             ->whereIn('code', $allDevCodes)
@@ -1950,18 +1962,22 @@ class ImportService extends Service
             $array[$rowIndex][$monthIdx] = $valMonthTs;
             $uniqueMonths[] = $valMonthTs;
         }
+        if (!empty($errors)) return [implode('|', $errors), [], []];
+
+        $allMonthsTs = array_unique($uniqueMonths);
+
+        //归档
+        list($status, $msg) = ArchiveService::isArchive($allMonthsTs, $user);
+        if(! $status)  return [$msg, [], []];
 
         // 查询数据库中已存在的月份单据
-        $existingMonthsMap = [];
-        if (!empty($uniqueMonths)) {
-            $existingMonths = DB::table('monthly_ps_order')
-                ->where('top_depart_id', $user['top_depart_id'])
-                ->where('del_time', 0)
-                ->whereIn('month', array_unique($uniqueMonths))
-                ->pluck('month')
-                ->toArray();
-            $existingMonthsMap = array_fill_keys($existingMonths, true);
-        }
+        $existingMonths = DB::table('monthly_ps_order')
+            ->where('top_depart_id', $user['top_depart_id'])
+            ->where('del_time', 0)
+            ->whereIn('month', $allMonthsTs)
+            ->pluck('month')
+            ->toArray();
+        $existingMonthsMap = array_fill_keys($existingMonths, true);
 
         $excelAggregator = [];
         $update_map = [];
@@ -2193,18 +2209,22 @@ class ImportService extends Service
             $array[$rowIndex][$monthIdx] = $valMonthTs;
             $uniqueMonths[] = $valMonthTs;
         }
+        if (!empty($errors)) return [implode('|', $errors), [], []];
+
+        $allMonthsTs = array_unique($uniqueMonths);
+
+        //归档
+        list($status, $msg) = ArchiveService::isArchive($allMonthsTs, $user);
+        if(! $status)  return [$msg, [], []];
 
         // 预查数据库已存在的月份
-        $existingMonthsMap = [];
-        if (!empty($uniqueMonths)) {
-            $existingMonths = DB::table('monthly_dd_order')
-                ->where('top_depart_id', $user['top_depart_id'])
-                ->where('del_time', 0)
-                ->whereIn('month', array_unique($uniqueMonths))
-                ->pluck('month')
-                ->toArray();
-            $existingMonthsMap = array_fill_keys($existingMonths, true);
-        }
+        $existingMonths = DB::table('monthly_dd_order')
+            ->where('top_depart_id', $user['top_depart_id'])
+            ->where('del_time', 0)
+            ->whereIn('month', $allMonthsTs)
+            ->pluck('month')
+            ->toArray();
+        $existingMonthsMap = array_fill_keys($existingMonths, true);
 
         $excelAggregator = [];
         $update_map = [];
@@ -2380,13 +2400,19 @@ class ImportService extends Service
 
         // --- 步骤 1: 预处理扫描 ---
         foreach ($array as $rowIndex => $row) {
-            list($mStatus, $valMonthTs) = $this->convertExcelCellToDate($row[$monthIdx] ?? '');
-            if ($mStatus) {
-                $valMonthTs = strtotime(date('Y-m-01', $valMonthTs));
-                $array[$rowIndex][$monthIdx] = $valMonthTs;
-                $uniqueMonths[] = $valMonthTs;
+            $valMonthRaw = trim($row[$monthIdx] ?? '');
+            if ($valMonthRaw === '') continue;
+
+            list($mStatus, $valMonthTs) = $this->convertExcelCellToDate($valMonthRaw);
+            if (!$mStatus) {
+                $errors[] = "第" . ($rowIndex + 1) . "行:月份格式错误({$valMonthRaw})";
+                continue;
             }
 
+            $valMonthTs = strtotime(date('Y-m-01', $valMonthTs));
+            $array[$rowIndex][$monthIdx] = $valMonthTs;
+            $uniqueMonths[] = $valMonthTs;
+
             $type = $type_map[$row[$typeIdx] ?? ''] ?? 0;
             $dCode = trim($row[$dCodeIdx] ?? '');
             $iCode = trim($row[$itemIdIdx] ?? '');
@@ -2396,6 +2422,14 @@ class ImportService extends Service
             if ($iCode !== '') $itemCodes[] = $iCode;
         }
 
+        if (!empty($errors)) return [implode('|', $errors), [], []];
+
+        $allMonthsTs = array_unique($uniqueMonths);
+
+        //归档
+        list($status, $msg) = ArchiveService::isArchive($allMonthsTs, $user);
+        if(! $status)  return [$msg, [], []];
+
         // --- 步骤 2: 批量预加载 ---
         $dbEmps = Employee::where('del_time', 0)->where('top_depart_id', $user['top_depart_id'])
             ->whereIn('number', array_unique($empCodes))->pluck('id', 'number')->toArray();
@@ -2428,11 +2462,6 @@ class ImportService extends Service
             $valICode = trim($row[$itemIdIdx] ?? '');
             $valRate = $row[$rateIdx] ?? 0;
 
-            if (!is_numeric($valMonthTs)) {
-                $errors[] = "第{$displayLine}行:月份格式非法";
-                continue;
-            }
-
             // A. 编辑/新增逻辑判定 (核心修正)
             if ($valMCode !== '' && isset($dbOrders[$valMCode])) {
                 $dbOrder = $dbOrders[$valMCode];
@@ -2659,12 +2688,25 @@ class ImportService extends Service
 
         // --- 跨单据预查:提前解析日期并检索库内已存在的工时区间 ---
         $allDates = [];
-        foreach ($array as $row) {
-            list($status, $ts) = $this->convertExcelCellToDate($row[$timeIdx] ?? '');
-            if ($status) $allDates[] = strtotime(date('Y-m-d', $ts));
+        foreach ($array as $rowIndex => $row) {
+            $valMonthRaw = $row[$timeIdx] ?? '';
+            list($status, $ts) = $this->convertExcelCellToDate($valMonthRaw);
+            if(! $status) {
+                $errors[] = "第" . ($rowIndex + 1) . "行:单据日期格式错误({$valMonthRaw})";
+                continue;
+            }
+            $d = strtotime(date('Y-m-d', $ts));
+            $array[$rowIndex][$timeIdx] = $d;
+            $allDates[] = $d;
         }
+        if (!empty($errors)) return [implode('|', $errors), [], [], []];
+
         $allDates = array_unique($allDates);
 
+        //归档
+        list($status, $msg) = ArchiveService::isArchive($allDates, $user);
+        if(! $status)  return [$msg, [], [], []];
+
         // 查询数据库:同公司、同日期、同项目、同人员的已有工时 (排除本次 Excel 涉及到的单号)
         $dbExistingWork = DB::table('daily_pw_order_details as d')
             ->join('daily_pw_order as m', 'd.main_id', '=', 'm.id')
@@ -2690,14 +2732,7 @@ class ImportService extends Service
         foreach ($array as $rowIndex => $row) {
             $displayLine = $rowIndex + 1;
 
-            // A. 日期预处理
-            list($status, $ts) = $this->convertExcelCellToDate($row[$timeIdx] ?? '');
-            if (!$status) {
-                $errors[] = "第{$displayLine}行:单据日期格式错误";
-                continue;
-            }
-            $ts = strtotime(date('Y-m-d', $ts));
-            $array[$rowIndex][$timeIdx] = $ts;
+            $ts = $row[$timeIdx];
 
             $valCode = trim($row[$codeIdx] ?? '');
             $valItemCode = trim($row[$itemIdx] ?? '');
@@ -2925,12 +2960,25 @@ class ImportService extends Service
 
         // 3. 跨单据预查
         $allDates = [];
-        foreach ($array as $row) {
-            list($status, $ts) = $this->convertExcelCellToDate($row[$timeIdx] ?? '');
-            if ($status) $allDates[] = strtotime(date('Y-m-d', $ts));
+        foreach ($array as $rowIndex => $row) {
+            $valMonthRaw = $row[$timeIdx] ?? '';
+            list($status, $ts) = $this->convertExcelCellToDate($valMonthRaw);
+            if(! $status) {
+                $errors[] = "第" . ($rowIndex + 1) . "行:单据日期格式错误({$valMonthRaw})";
+                continue;
+            }
+            $d = strtotime(date('Y-m-d', $ts));
+            $array[$rowIndex][$timeIdx] = $d;
+            $allDates[] = $d;
         }
+        if (!empty($errors)) return [implode('|', $errors), [], [], []];
+
         $allDates = array_unique($allDates);
 
+        //归档
+        list($status, $msg) = ArchiveService::isArchive($allDates, $user);
+        if(! $status)  return [$msg, [], [], []];
+
         $dbExistingWork = DB::table('daily_dw_order_details as d')
             ->join('daily_dw_order as m', 'd.main_id', '=', 'm.id')
             ->where('m.top_depart_id', $topDepartId)
@@ -2954,13 +3002,7 @@ class ImportService extends Service
         foreach ($array as $rowIndex => $row) {
             $displayLine = $rowIndex + 1;
 
-            list($status, $ts) = $this->convertExcelCellToDate($row[$timeIdx] ?? '');
-            if (!$status) {
-                $errors[] = "第{$displayLine}行:单据日期格式错误";
-                continue;
-            }
-            $ts = strtotime(date('Y-m-d', $ts));
-            $array[$rowIndex][$timeIdx] = $ts;
+            $ts = $row[$timeIdx];
 
             $valCode = trim($row[$codeIdx] ?? '');
             $valItemCode = trim($row[$itemIdx] ?? '');
@@ -3152,11 +3194,25 @@ class ImportService extends Service
 
         // --- 2. 日历 & 主表预查 (代码省略,保持原样) ---
         $allDateTs = [];
-        foreach ($array as $row) {
-            list($status, $ts) = $this->convertExcelCellToDate($row[$timeIdx] ?? '');
-            if ($status) $allDateTs[] = strtotime(date('Y-m-d', $ts));
+        foreach ($array as $rowIndex => $row) {
+            $valMonthRaw = $row[$timeIdx] ?? '';
+            list($status, $ts) = $this->convertExcelCellToDate($valMonthRaw);
+            if(! $status) {
+                $errors[] = "第" . ($rowIndex + 1) . "行:单据日期格式错误({$valMonthRaw})";
+                continue;
+            }
+            $d = strtotime(date('Y-m-d', $ts));
+            $array[$rowIndex][$timeIdx] = $d;
+            $allDateTs[] = $d;
         }
+        if (!empty($errors)) return [implode('|', $errors), [], []];
+
         $allDateTs = array_unique($allDateTs);
+
+        //归档
+        list($status, $msg) = ArchiveService::isArchive($allDateTs, $user);
+        if(! $status)  return [$msg, [], []];
+
         $calendarMap = DB::table('calendar_details')->where('del_time', 0)->where('top_depart_id', $topDepartId)
             ->whereIn('time', $allDateTs)->pluck('is_work', 'time')->toArray();
 
@@ -3197,14 +3253,7 @@ class ImportService extends Service
             $line = $rowIndex + 1;
             $rowErrors = []; // 临时收集本行的所有错误
 
-            list($status, $ts) = $this->convertExcelCellToDate($row[$timeIdx] ?? '');
-            $ts = $status ? strtotime(date('Y-m-d', $ts)) : 0;
-            $array[$rowIndex][$timeIdx] = $ts;
-
-            if (!$ts) {
-                $errors[] = "第{$line}行:日期格式错误";
-                continue;
-            }
+            $ts = $row[$timeIdx];
 
             // 1. 单据与编辑逻辑检查
             $valCode = trim($row[$codeIdx] ?? '');
@@ -3416,11 +3465,26 @@ class ImportService extends Service
 
         // 2. 日期、日历、主表预查
         $allDateTs = [];
-        foreach ($array as $row) {
-            list($status, $ts) = $this->convertExcelCellToDate($row[$timeIdx] ?? '');
-            if ($status) $allDateTs[] = strtotime(date('Y-m-d', $ts));
+        foreach ($array as $rowIndex => $row) {
+            $valMonthRaw = $row[$timeIdx] ?? '';
+            list($status, $ts) = $this->convertExcelCellToDate($valMonthRaw);
+            if(! $status) {
+                $errors[] = "第" . ($rowIndex + 1) . "行:单据日期格式错误({$valMonthRaw})";
+                continue;
+            }
+            $d = strtotime(date('Y-m-d', $ts));
+            $array[$rowIndex][$timeIdx] = $d;
+            $allDateTs[] = $d;
         }
+
+        if (!empty($errors)) return [implode('|', $errors), [], []];
+
         $allDateTs = array_unique($allDateTs);
+
+        //归档
+        list($status, $msg) = ArchiveService::isArchive($allDateTs, $user);
+        if(! $status)  return [$msg, [], []];
+
         $calendarMap = DB::table('calendar_details')->where('del_time', 0)->where('top_depart_id', $topDepartId)
             ->whereIn('time', $allDateTs)->pluck('is_work', 'time')->toArray();
 
@@ -3461,11 +3525,7 @@ class ImportService extends Service
             $line = $rowIndex + 1;
             $rowErrors = [];
 
-            list($status, $ts) = $this->convertExcelCellToDate($row[$timeIdx] ?? '');
-            $ts = $status ? strtotime(date('Y-m-d', $ts)) : 0;
-            $array[$rowIndex][$timeIdx] = $ts;
-
-            if (!$ts) { $errors[] = "第{$line}行:日期格式错误"; continue; }
+            $ts = $row[$timeIdx];
 
             // A. 单据存在性逻辑
             $valCode = trim($row[$codeIdx] ?? '');
@@ -3730,6 +3790,12 @@ class ImportService extends Service
 
         if (!empty($errors)) return [implode('|', $errors), [], []];
 
+        $allMonthsTs = array_unique($allMonthsTs);
+
+        //归档
+        list($status, $msg) = ArchiveService::isArchive($allMonthsTs, $user);
+        if(! $status)  return [$msg, [], []];
+
         // --- 2. 批量预加载档案映射 ---
         $dbEmps = DB::table('employee')->where('del_time', 0)->where('top_depart_id', $topDepartId)
             ->whereIn('number', array_unique($allEmpNos))->pluck('id', 'number')->toArray();
@@ -3741,7 +3807,7 @@ class ImportService extends Service
             ->whereIn('code', array_unique($allFeeCodes))->pluck('id', 'code')->toArray();
 
         $existingMonthsMap = DB::table('expense_claims')->where('del_time', 0)->where('top_depart_id', $topDepartId)
-            ->whereIn('month', array_unique($allMonthsTs))->pluck('code', 'month')->toArray();
+            ->whereIn('month', $allMonthsTs)->pluck('code', 'month')->toArray();
 
         $dbOrdersByCode = DB::table('expense_claims')->where('del_time', 0)->where('top_depart_id', $topDepartId)
             ->whereIn('code', array_unique($allCodes))->get()->keyBy('code');