update tính năng thống kê chấm công và xuất dữ liệu chấm công

This commit is contained in:
2020-10-16 17:48:38 +07:00
parent 4ba094ab24
commit 0314fa4158
12 changed files with 425 additions and 5 deletions

View File

@@ -12,6 +12,9 @@ use app\models\Device;
use app\models\EventType;
use app\models\Department;
use app\models\common;
use app\models\StaffSearch;
use yii\helpers\Url;
use app\models\Staff;
/**
* LogsController implements the CRUD actions for Logs model.
@@ -91,6 +94,38 @@ class LogsController extends Controller {
]);
}
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');
@@ -172,4 +207,230 @@ class LogsController extends Controller {
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();
}
}