use Yii;
use yii\helpers\Json;
use yii_app\records\Admin;
-use yii_app\records\AdminGradeHistory;
+use yii_app\records\AdminGroup;
use yii_app\records\ApiCronTest;
use yii_app\records\CityStore;
use yii_app\records\CityStoreParams;
use yii_app\records\Products1c;
use yii_app\records\Sales;
use yii_app\records\SalesProducts;
-use yii_app\records\Timetable;
-use yii_app\records\TimetableFact;
+use yii_app\records\TimetableFactModel;
class DataTestController extends BaseController
'operation' => $sale['operation'],
'status' => $sale['status'],
'skidka' => (float)$sale['skidka'],
+ 'seller_id' => $sale['seller_id'] ?? null,
'products' => []
];
} else {
$shiftType = (int)($input['shift_type'] ?? 0);
try {
- $nextDateStr = (new \DateTime($date))->modify('+1 day')->format('Y-m-d');
+ $nextDate = (new \DateTime($date))->modify('+1 day')->format('Y-m-d');
- // Только фактически работавшие (не отпуск, не больничный, не выходной)
- $query = TimetableFact::find()
- ->select(['admin_id'])
+ $query = TimetableFactModel::find()
+ ->select(['admin_id', 'admin_group_id', 'date_start', 'time_start', 'date_end', 'time_end'])
->andWhere(['store_id' => $storeId])
- ->andWhere(['slot_type_id' => [Timetable::TIMESLOT_WORK, Timetable::TIMESLOT_INTERNSHIP]]);
+ ->andWhere(['date' => $date]);
if ($shiftType === 1) {
- $query->andWhere(['between', 'datetime_start',
- $date . ' 08:00:00',
- $date . ' 19:59:59',
- ]);
+ // Дневная: смена начата и закрыта в тот же день
+ $query->andWhere(['date_end' => $date]);
} elseif ($shiftType === 2) {
- $query->andWhere(['OR',
- ['between', 'datetime_start', $date . ' 20:00:00', $date . ' 23:59:59'],
- ['between', 'datetime_start', $nextDateStr . ' 00:00:00', $nextDateStr . ' 07:59:59'],
- ]);
- } else {
- $query->andWhere(['between', 'datetime_start',
- $date . ' 00:00:00',
- $date . ' 23:59:59',
- ]);
+ // Ночная: смена переходит за полночь
+ $query->andWhere(['date_end' => $nextDate]);
}
- $rows = $query->asArray()->all();
+ $rows = $query->indexBy('admin_id')->asArray()->all();
if (empty($rows)) {
return $this->asJson(['success' => true, 'data' => []]);
}
- $adminIds = array_unique(array_column($rows, 'admin_id'));
+ $adminIds = array_keys($rows);
$admins = Admin::find()
->select(['id', 'name_full', 'employee_position_id'])
->asArray()
->all();
+ $groupIds = array_filter(array_unique(array_column($rows, 'admin_group_id')));
+ $groups = [];
+ if (!empty($groupIds)) {
+ $groups = AdminGroup::find()
+ ->select(['id', 'name'])
+ ->where(['id' => $groupIds])
+ ->indexBy('id')
+ ->asArray()
+ ->all();
+ }
+
$positionIds = array_filter(array_unique(array_column($admins, 'employee_position_id')));
$positions = [];
if (!empty($positionIds)) {
->all();
}
- $gradeRows = AdminGradeHistory::find()
- ->alias('agh')
- ->select(['agh.admin_id', 'g.id AS grade_id', 'g.name AS grade_name'])
- ->innerJoin('grade g', 'g.id = agh.grade_id')
- ->where(['agh.admin_id' => $adminIds])
- ->andWhere(['agh.closed_at' => '2100-01-01 00:00:00'])
- ->asArray()
- ->all();
-
- $grades = array_column($gradeRows, null, 'admin_id');
-
$exportRows = ExportImportTable::find()
->select(['entity_id', 'export_val'])
->where([
$sellerIds = array_column($exportRows, 'export_val', 'entity_id');
$result = [];
- foreach ($adminIds as $adminId) {
+ foreach ($rows as $adminId => $row) {
$admin = $admins[$adminId] ?? [];
+ $groupId = $row['admin_group_id'] ?? null;
$positionId = $admin['employee_position_id'] ?? null;
- $grade = $grades[$adminId] ?? null;
+ $dateStart = $row['date_start'] && $row['time_start']
+ ? $row['date_start'] . ' ' . $row['time_start']
+ : null;
+ $dateEnd = $row['date_end'] && $row['time_end']
+ ? $row['date_end'] . ' ' . $row['time_end']
+ : null;
$result[] = [
- 'admin_id' => $adminId,
+ 'admin_id' => (int)$adminId,
'name' => $admin['name_full'] ?? null,
'seller_id' => $sellerIds[$adminId] ?? null,
+ 'admin_group_id' => $groupId ? (int)$groupId : null,
+ 'admin_group_name' => $groupId ? ($groups[$groupId]['name'] ?? null) : null,
'employee_position_id' => $positionId,
'position_name' => $positionId ? ($positions[$positionId]['name'] ?? null) : null,
- 'grade_id' => $grade['grade_id'] ?? null,
- 'grade_name' => $grade['grade_name'] ?? null,
+ 'shift_start' => $dateStart,
+ 'shift_end' => $dateEnd,
];
}