Yii::$app->params["time"]) $this->redirect(["/dashboard"]); if (Yii::$app->user->isGuest) return $this->redirect(['/site/login']); } /** * {@inheritdoc} */ public function behaviors() { return [ 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => ['POST'], ], ], ]; } public function actionIndex($type = "today", $from = "", $to = "") { $f = $t = 0; $this->view->title = 'Tất cả sự kiện'; if ($type === "today" && $from === "" && $to === "") { $f = date_format(date_create_from_format('H:i:s d/m/Y', "00:00:00 " . date("d/m/Y")), 'U'); $t = date_format(date_create_from_format('H:i:s d/m/Y', "23:59:59 " . date("d/m/Y")), 'U'); $this->view->title = 'Sự kiện hôm nay'; } if ($type === "3days" && $from === "" && $to === "") { $f = date_format(date_create_from_format('H:i:s d/m/Y', "00:00:00 " . date("d/m/Y")), 'U'); $f = $f - 60 * 60 * 24 * 2; $t = date_format(date_create_from_format('H:i:s d/m/Y', "23:59:59 " . date("d/m/Y")), 'U'); $this->view->title = 'Sự kiện 3 ngày gần đây'; } if ($type === "thisWeek" && $from === "" && $to === "") { $dayOfWeek = date("w"); $f = date_format(date_create_from_format('H:i:s d/m/Y', "00:00:00 " . date("d/m/Y")), 'U'); $f = $f - 60 * 60 * 24 * ($dayOfWeek - 1); $t = date_format(date_create_from_format('H:i:s d/m/Y', "23:59:59 " . date("d/m/Y")), 'U'); $t = $t + 60 * 60 * 24 * (7 - $dayOfWeek); $this->view->title = 'Sự kiện tuần này'; } if ($type === "lastWeek" && $from === "" && $to === "") { $dayOfWeek = date("w"); $f = date_format(date_create_from_format('H:i:s d/m/Y', "00:00:00 " . date("d/m/Y")), 'U'); $f = $f - 60 * 60 * 24 * ($dayOfWeek - 1) - 60 * 60 * 24 * 7; $t = date_format(date_create_from_format('H:i:s d/m/Y', "23:59:59 " . date("d/m/Y")), 'U'); $t = $t + 60 * 60 * 24 * (7 - $dayOfWeek) - 60 * 60 * 24 * 7; $this->view->title = 'Sự kiện tuần trước'; } if ($from !== "" && $to !== "") { $f = date_format(date_create_from_format('H:i:s d/m/Y', $from), 'U'); $t = date_format(date_create_from_format('H:i:s d/m/Y', $to), 'U'); } $searchModel = new LogsSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); if ($f != 0 && $t != 0) { $dataProvider->query->andWhere(["BETWEEN", 'time', $f, $t]); } $dataProvider->query->orderBy(["time" => SORT_DESC]); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'deviceArray' => Device::deviceArray(), 'stateArray' => Logs::$stateArray, 'eventTypeArray' => EventType::eventTypeArray(), 'departmentArray' => Department::departmentArray(), 'f' => $f, 't' => $t ]); } public function actionStatistics($day = "") { $day = $day === "" ? date("d/m/Y") : $day; $f = date_format(date_create_from_format('H:i:s d/m/Y', "00:00:00 " . $day), 'U'); $t = date_format(date_create_from_format('H:i:s d/m/Y', "23:59:59 " . $day), 'U'); $query = Logs::find(); $query->andWhere(["BETWEEN", 'time', $f, $t]); $query->orderBy(["time" => SORT_ASC]); $logs = $query->all(); $staffID = []; $staffLogs = []; foreach ($logs as $key => $value) { if ($value->staff_id && $value->event_type == 0) { $staffID[] = $value->staff_id; $staffLogs[$value->staff_id][] = $value->time; } } $staffID = array_unique($staffID); $this->view->title = 'Báo cáo chấm công'; $searchModel = new StaffSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); $dataProvider->query->andWhere(["IN", "id", $staffID]); return $this->render('statistics', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, "departmentArray" => Department::departmentArray(), "day" => $day, "staffLogs" => $staffLogs ]); } public function actionExport($from = "", $to = "") { $f = date_format(date_create_from_format('H:i:s d/m/Y', $from), 'U'); $t = date_format(date_create_from_format('H:i:s d/m/Y', $to), 'U'); $objPHPExcel = new \PHPExcel(); $objPHPExcel->setActiveSheetIndex(0); $toExcelFile[] = ["Thời gian", "Mã nhân viên", "Tên nhân viên", "Phòng ban", "Mã thẻ", "Thiết bị", "Cửa", "Trạng thái vào/ra", "Mô tả sự kiện"]; $query = Logs::find(); $query->andWhere(["BETWEEN", 'time', $f, $t]); $query->orderBy(["time" => SORT_DESC]); $logs = $query->all(); $departmentArray = Department::departmentArray(); $deviceArray = Device::deviceArray(); $stateArray = Logs::$stateArray; $eventTypeArray = EventType::eventTypeArray(); foreach ($logs as $k => $v) { $staff = $v->staff; $ExportData[] = date("H:i:s d/m/Y", $v->time); $ExportData[] = $staff ? $staff->code : ""; $ExportData[] = $staff ? $staff->name : ""; $ExportData[] = $staff && isset($departmentArray[$staff->department_id]) ? $departmentArray[$staff->department_id] : ""; $ExportData[] = $v->card_number; $ExportData[] = isset($deviceArray[$v->device_id]) ? $deviceArray[$v->device_id] : ""; $ExportData[] = isset($deviceArray[$v->device_id]) ? $deviceArray[$v->device_id] . "-" . $v->door_id : ""; $ExportData[] = isset($stateArray[$v->in_out_state]) ? $stateArray[$v->in_out_state] : ""; $ExportData[] = isset($eventTypeArray[$v->event_type]) ? $eventTypeArray[$v->event_type] : ""; $toExcelFile[] = $ExportData; unset($ExportData); } $totals = count($logs) + 1; $activeSheet = $objPHPExcel->getActiveSheet(); $activeSheet->getStyle("A1:I" . $totals)->getFont()->setName('Time New Roman')->setSize(10); $activeSheet->getStyle("A1:I1")->applyFromArray([ 'fill' => array( 'type' => \PHPExcel_Style_Fill::FILL_SOLID, 'color' => array('rgb' => '7ac3ec') ) ]); $rowCount = 1; for ($i = 0; $i < count($toExcelFile); $i++) { $column = 'A'; $row = $toExcelFile[$i]; for ($j = 0; $j < count($row); $j++) { if (!isset($row[$j])) $value = NULL; elseif ($row[$j] != "") $value = strip_tags($row[$j]); else $value = ""; $activeSheet->setCellValue($column . $rowCount, $value); $column = chr(ord($column) + 1); } $rowCount++; } $activeSheet->getStyle("A1:I" . $totals)->applyFromArray([ 'alignment' => [ 'vertical' => \PHPExcel_Style_Alignment::VERTICAL_CENTER, ], 'borders' => [ 'allborders' => [ 'style' => \PHPExcel_Style_Border::BORDER_THIN ] ] ]); $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); ob_end_clean(); header('Content-type: application/vnd.ms-excel'); header('Content-Disposition: attachment; filename="logs_' . date("YmdHis") . '.xlsx"'); header('Cache-Control: max-age=0'); common::SaveViaTempFile($objWriter); exit(); } protected function findModel($id) { if (($model = Logs::findOne($id)) !== null) { return $model; } throw new NotFoundHttpException('The requested page does not exist.'); } public function actionExportStatisticsForm() { if (Yii::$app->request->isAjax) { Yii::$app->response->format = "json"; return [ "title" => "Xuất dữ liệu chấm công tháng", "form" => $this->renderPartial("export", [ "url" => Url::to(['export-statistics']) ]) ]; } } public function formatLogs($month) { $month = $month === "" ? date("m/Y") : $month; $m = explode("/", $month); $totalsDayOfMonth = cal_days_in_month(CAL_GREGORIAN, $m[0], $m[1]); $f = date_format(date_create_from_format('H:i:s d/m/Y', "00:00:00 01/" . $month), 'U'); $t = date_format(date_create_from_format('H:i:s d/m/Y', "23:59:59 " . $totalsDayOfMonth . "/" . $month), 'U'); $query = Logs::find(); $query->andWhere(['event_type' => 0]); $query->andWhere(["BETWEEN", "time", $f, $t]); $query->orderBy(["time" => SORT_ASC]); $logs = $query->all(); $datas = []; foreach ($logs as $key => $value) { $datas[$value->staff_id][date("d", $value->time)][] = $value->time; } return $datas; } public function actionExportStatistics($month = "") { $month = $month === "" ? date("m/Y") : $month; $m = explode("/", $month); $totalsDayOfMonth = cal_days_in_month(CAL_GREGORIAN, $m[0], $m[1]); $datas = $this->formatLogs($month); $objPHPExcel = new \PHPExcel(); $objPHPExcel->setActiveSheetIndex(0); $header = ["STT", "Mã nhân viên", "Tên nhân viên", "Phòng ban", "Chỉ số"]; for ($i = 1; $i <= $totalsDayOfMonth; $i++) { $header[] = strval($i); } $header[] = "Tổng công"; $header[] = "Tổng giờ công"; $toExcelFile[] = $header; $departmentArray = Department::departmentArray(); $staffs = Staff::find()->all(); $count = 1; foreach ($staffs as $key => $value) { $stats = []; for ($i = 1; $i <= $totalsDayOfMonth; $i++) { $in = $out = $manHour = $manDay = 0; if (isset($datas[$value->id][sprintf("%02d", $i)])) { $in = $datas[$value->id][sprintf("%02d", $i)][0]; $totalLogsOfDay = count($datas[$value->id][sprintf("%02d", $i)]); if ($totalLogsOfDay > 1) $out = $datas[$value->id][sprintf("%02d", $i)][$totalLogsOfDay - 1]; if ($in && $out) { $manHour = number_format(($out - $in) / (60 * 60), 2); $manDay = number_format(($out - $in) / (60 * 60) / 8, 2); } } $stats[$i] = ["in" => $in, "out" => $out, "manHour" => $manHour, "manDay" => $manDay]; } $ExportData[] = $count++; $ExportData[] = $value->code; $ExportData[] = $value->name; $ExportData[] = isset($departmentArray[$value->department_id]) ? $departmentArray[$value->department_id] : ""; $ExportData[] = "Vào"; for ($i = 1; $i <= $totalsDayOfMonth; $i++) { $ExportData[] = $stats[$i]["in"] ? date("H:i", $stats[$i]["in"]) : ""; } $toExcelFile[] = $ExportData; unset($ExportData); $ExportData[] = ""; $ExportData[] = ""; $ExportData[] = ""; $ExportData[] = ""; $ExportData[] = "Ra"; for ($i = 1; $i <= $totalsDayOfMonth; $i++) { $ExportData[] = $stats[$i]["out"] ? date("H:i", $stats[$i]["out"]) : ""; } $toExcelFile[] = $ExportData; unset($ExportData); $ExportData[] = ""; $ExportData[] = ""; $ExportData[] = ""; $ExportData[] = ""; $ExportData[] = "GC"; for ($i = 1; $i <= $totalsDayOfMonth; $i++) { $ExportData[] = $stats[$i]["manHour"] ? $stats[$i]["manHour"] : ""; } $toExcelFile[] = $ExportData; unset($ExportData); $ExportData[] = ""; $ExportData[] = ""; $ExportData[] = ""; $ExportData[] = ""; $ExportData[] = "TC"; for ($i = 1; $i <= $totalsDayOfMonth; $i++) { $ExportData[] = $stats[$i]["manDay"] ? $stats[$i]["manDay"] : ""; } $toExcelFile[] = $ExportData; unset($ExportData); } $totals = count($staffs) + 1; $activeSheet = $objPHPExcel->getActiveSheet(); $activeSheet->getColumnDimension('A')->setWidth(6); $activeSheet->getColumnDimension('B')->setWidth(10); // $activeSheet->getStyle("A1:I" . $totals)->getFont()->setName('Time New Roman')->setSize(10); // $activeSheet->getStyle("A1:I1")->applyFromArray([ // 'fill' => array( // 'type' => \PHPExcel_Style_Fill::FILL_SOLID, // 'color' => array('rgb' => '7ac3ec') // ) // ]); $rowCount = 2; for ($i = 0; $i < count($toExcelFile); $i++) { $column = 'A'; $column2 = ''; $row = $toExcelFile[$i]; for ($j = 0; $j < count($row); $j++) { if (!isset($row[$j])) $value = NULL; elseif ($row[$j] != "") $value = strip_tags($row[$j]); else $value = ""; $activeSheet->setCellValue($column . $column2 . $rowCount, $value); if ($column === "Z") { $column = "A"; $column2 = "A"; } elseif ($column2 !== "") { $column2 = chr(ord($column2) + 1); } else { $column = chr(ord($column) + 1); } } $rowCount++; } $tempCol = $column; $tempCol2 = $column2; if ($column !== "A") { $tempCol = chr(ord($tempCol) - 1); } if ($column2 !== "") { $tempCol2 = chr(ord($tempCol2) - 1); } $lastDayCol = $tempCol . $tempCol2; $beforeMaxCol = $column . $column2; if ($column2 !== "") { $column2 = chr(ord($column2) + 1); } if ($column !== "A") { $column = chr(ord($column) + 1); } $maxCol = $column . $column2; $activeSheet->setCellValue("A1", "BẢNG CHẤM CÔNG CHI TIẾT THÁNG " . $month); $activeSheet->mergeCells('A1:' . $maxCol . '1'); $activeSheet->getStyle("A1:" . $maxCol . "1")->applyFromArray([ 'fill' => array( 'type' => \PHPExcel_Style_Fill::FILL_SOLID, 'color' => array('rgb' => '7ac3ec') ), 'font' => [ 'bold' => true ], 'alignment' => [ 'horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER ] ]); $currentRow = 3; foreach ($staffs as $key => $value) { $activeSheet->mergeCells('A' . $currentRow . ':A' . ($currentRow + 3)); $activeSheet->mergeCells('B' . $currentRow . ':B' . ($currentRow + 3)); $activeSheet->mergeCells('C' . $currentRow . ':C' . ($currentRow + 3)); $activeSheet->mergeCells('D' . $currentRow . ':D' . ($currentRow + 3)); $activeSheet->getStyle("A" . $currentRow . ":" . $maxCol . ($currentRow + 3))->applyFromArray([ 'borders' => [ 'top' => [ 'style' => \PHPExcel_Style_Border::BORDER_THIN ], 'bottom' => [ 'style' => \PHPExcel_Style_Border::BORDER_THIN ] ] ]); $activeSheet->setCellValue($beforeMaxCol . $currentRow, "=SUM(F" . ($currentRow + 3) . ":" . $lastDayCol . ($currentRow + 3) . ")"); $activeSheet->setCellValue($maxCol . $currentRow, "=SUM(F" . ($currentRow + 2) . ":" . $lastDayCol . ($currentRow + 2) . ")"); $activeSheet->mergeCells($beforeMaxCol . $currentRow . ':' . $beforeMaxCol . ($currentRow + 3)); $activeSheet->mergeCells($maxCol . $currentRow . ':' . $maxCol . ($currentRow + 3)); $currentRow += 4; } $maxRow = ($totals * 4) + 2; $activeSheet->getRowDimension('2')->setRowHeight(30); $activeSheet->getStyle("A2:" . $maxCol . "2")->applyFromArray(['alignment' => ['wrap' => true], 'font' => ['bold' => true]]); $activeSheet->getStyle("A2:B" . $maxRow)->applyFromArray(['alignment' => ['horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER]]); $activeSheet->getStyle("E2:E" . $maxRow)->applyFromArray(['alignment' => ['horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER]]); $activeSheet->getStyle("A2:E" . $maxRow)->applyFromArray(['alignment' => ['vertical' => \PHPExcel_Style_Alignment::VERTICAL_CENTER]]); $activeSheet->getStyle("F2:" . $maxCol . $maxRow)->applyFromArray(['alignment' => ['vertical' => \PHPExcel_Style_Alignment::VERTICAL_CENTER, 'horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER]]); // $activeSheet->getStyle("A1:I" . $totals)->applyFromArray([ // 'alignment' => [ // 'vertical' => \PHPExcel_Style_Alignment::VERTICAL_CENTER, // ], // 'borders' => [ // 'allborders' => [ // 'style' => \PHPExcel_Style_Border::BORDER_THIN // ] // ] // ]); $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); ob_end_clean(); header('Content-type: application/vnd.ms-excel'); header('Content-Disposition: attachment; filename="bang_cham_cong_' . date("YmdHis") . '.xlsx"'); header('Cache-Control: max-age=0'); common::SaveViaTempFile($objWriter); exit(); } }