]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
получение работников на смене
authorVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Wed, 18 Mar 2026 12:52:05 +0000 (15:52 +0300)
committerVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Wed, 18 Mar 2026 12:52:05 +0000 (15:52 +0300)
erp24/api2/controllers/DataTestController.php

index 81e81971f14cd10dc57c10d682fcdb835f89b0cb..4e428b082239cd6b1f2492d4c979cf4e2900007a 100644 (file)
@@ -7,6 +7,10 @@ use Exception;
 use Yii;
 use yii\helpers\Json;
 use yii_app\records\ApiCronTest;
+use yii_app\records\ExportImportTable;
+use yii_app\records\Shift;
+use yii_app\records\TimetableFact;
+use yii_app\records\Timetable;
 
 
 class DataTestController extends BaseController {
@@ -309,4 +313,115 @@ class DataTestController extends BaseController {
             ]);
         }
     }
+
+    /**
+     * Возвращает список сотрудников, вышедших на смену согласно фактическому табелю.
+     *
+     * POST /api2/data-test/get-shift-workers
+     *
+     * Тело запроса:
+     * {
+     *   "store_id": 5,
+     *   "date": "2025-10-10",
+     *   "shift_type": 1
+     * }
+     *
+     * shift_type:
+     * 0 = все смены
+     * 1 = дневная (Shift::DAY)
+     * 2 = ночная (Shift::NIGHT)
+     *
+     * Ответ:
+     * {
+     *   "success": true,
+     *   "data": [
+     *     {
+     *       "admin_id": 42,
+     *       "name": "Иванова А.В.",
+     *       "seller_id": "56524cb1-4763-11ea-8cce-b42e991aff6c"
+     *     }
+     *   ]
+     * }
+     */
+    public function actionGetShiftWorkers(): array
+    {
+        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
+
+        $request = Yii::$app->request->getRawBody();
+
+        try {
+            $input = Json::decode($request);
+        } catch (Exception $ex) {
+            return $this->asJson(['success' => false, 'error' => 'Invalid JSON body']);
+        }
+
+        if (empty($input['store_id'])) {
+            return $this->asJson(['success' => false, 'error' => 'store_id is required']);
+        }
+
+        if (empty($input['date'])) {
+            return $this->asJson(['success' => false, 'error' => 'date is required']);
+        }
+
+        $storeId   = (int) $input['store_id'];
+        $date      = $input['date'];
+        $shiftType = (int) ($input['shift_type'] ?? 0);
+
+        try {
+            $shiftIds = Shift::getShiftIdsByShiftType($shiftType);
+
+            $rows = TimetableFact::find()
+                ->select(['timetable.admin_id'])
+                ->with('admin')
+                ->andWhere([
+                    'store_id'     => $storeId,
+                    'date'         => $date,
+                    'shift_id'     => $shiftIds,
+                    'slot_type_id' => [Timetable::TIMESLOT_WORK, Timetable::TIMESLOT_INTERNSHIP],
+                ])
+                ->asArray()
+                ->all();
+
+            if (empty($rows)) {
+                return $this->asJson(['success' => true, 'data' => []]);
+            }
+
+            $adminIds = array_unique(array_column($rows, 'admin_id'));
+
+            // Получаем имена сотрудников
+            $admins = \yii_app\records\Admin::find()
+                ->select(['id', 'name_full'])
+                ->where(['id' => $adminIds])
+                ->indexBy('id')
+                ->asArray()
+                ->all();
+
+            // Получаем seller_id (GUID из 1С) для каждого сотрудника
+            $exportRows = ExportImportTable::find()
+                ->select(['entity_id', 'export_val'])
+                ->where([
+                    'entity'    => 'admin',
+                    'entity_id' => $adminIds,
+                    'export_id' => 1,
+                ])
+                ->asArray()
+                ->all();
+
+            $sellerIds = array_column($exportRows, 'export_val', 'entity_id');
+
+            $result = [];
+            foreach ($adminIds as $adminId) {
+                $result[] = [
+                    'admin_id'  => $adminId,
+                    'name'      => $admins[$adminId]['name_full'] ?? null,
+                    'seller_id' => $sellerIds[$adminId] ?? null,
+                ];
+            }
+
+            return $this->asJson(['success' => true, 'data' => $result]);
+
+        } catch (Exception $e) {
+            return $this->asJson(['success' => false, 'error' => $e->getMessage()]);
+        }
+    }
 }