AIParking_Intops_Server/controllers/LogsUnknowController.php
2020-02-26 14:09:01 +07:00

309 lines
11 KiB
PHP

<?php
namespace app\controllers;
use Yii;
use app\models\LogsUnknow;
use app\models\LogsUnknowSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* LogsUnknowController implements the CRUD actions for LogsUnknow model.
*/
class LogsUnknowController extends Controller {
public function init() {
parent::init();
if (Yii::$app->user->isGuest) {
return $this->redirect(['/site/login']);
}
}
/**
* {@inheritdoc}
*/
public function behaviors() {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all LogsUnknow models.
* @return mixed
*/
public function actionIndex($from = "", $to = "") {
if ($from === "") {
$from = "00:00 " . date("d/m/Y");
}
if ($to === "") {
$to = "23:59 " . date("d/m/Y");
}
$this->view->title = "Thống kê xe chưa rõ nguồn gốc";
$searchModel = new LogsUnknowSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$f = date_format(date_create_from_format('H:i d/m/Y', $from), 'U');
$t = date_format(date_create_from_format('H:i d/m/Y', $to), 'U');
$dataProvider->query->andWhere(["OR", ["BETWEEN", 'time_in', $f, $t], ["BETWEEN", 'time_out', $f, $t]]);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'from' => $from,
'to' => $to
]);
}
/**
* Displays a single LogsUnknow model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id) {
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new LogsUnknow model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate() {
$model = new LogsUnknow();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing LogsUnknow model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id) {
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing LogsUnknow model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id) {
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the LogsUnknow model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return LogsUnknow the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id) {
if (($model = LogsUnknow::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
public function actionSave() {
if (Yii::$app->request->post()) {
$post = Yii::$app->request->post();
$logs = LogsUnknow::findOne($post['id']);
$field = $post['field'];
if ($logs) {
$logs->$field = $post['value'];
$logs->save();
}
return "<div style='cursor:pointer;' onclick='openForm(this, \"{$field}\");' data-id='{$post['id']}' data-text='{$post['value']}'>{$post['value']}</div>";
}
}
public function actionExport($from = "", $to = "") {
if ($from === "") {
$from = "00:00 " . date("d/m/Y");
}
if ($to === "") {
$to = "23:59 " . date("d/m/Y");
}
$f = date_format(date_create_from_format('H:i d/m/Y', $from), 'U');
$t = date_format(date_create_from_format('H:i d/m/Y', $to), 'U');
$lists = LogsUnknow::find()->andWhere(["OR", ["BETWEEN", 'time_in', $f, $t], ["BETWEEN", 'time_out', $f, $t]])->orderBy(['time_in' => SORT_ASC])->all();
$objPHPExcel = new \PHPExcel();
$count = 1;
$objPHPExcel->setActiveSheetIndex(0);
$toExcelFile[] = [
"STT",
"Loại xe",
"Tên công ty",
"Lái xe",
"Điện thoại",
"CMT",
"Factory",
"SEAL_NO",
"Biến số",
"Ảnh biển vào",
"Thời gian vào",
"Ảnh biển ra",
"Thời gian ra",
"Nội dung khác"
];
foreach ($lists as $k => $v) {
$ExportData[] = $count++;
$ExportData[] = "";
$ExportData[] = "";
$ExportData[] = "";
$ExportData[] = "";
$ExportData[] = "";
$ExportData[] = $v->factory;
$ExportData[] = $v->seal_no;
$ExportData[] = $v->plate;
$ExportData[] = "";
$ExportData[] = $v->time_in == 0 ? "" : date("H:i:s d/m/Y", $v->time_in);
$ExportData[] = "";
$ExportData[] = $v->time_out == 0 ? "" : date("H:i:s d/m/Y", $v->time_out);
$ExportData[] = $v->note;
if ($v->time_in) {
$objDrawing = new \PHPExcel_Worksheet_Drawing();
$objDrawing->setPath("./data/uploads/" . $v->plate_image_in);
$objDrawing->setWidth(105);
$objDrawing->setCoordinates('J' . ($k + 3));
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
}
if ($v->time_out) {
$objDrawing = new \PHPExcel_Worksheet_Drawing();
$objDrawing->setPath("./data/uploads/" . $v->plate_image_out);
$objDrawing->setWidth(105);
$objDrawing->setCoordinates('L' . ($k + 3));
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
}
$toExcelFile[] = $ExportData;
unset($ExportData);
}
$totals = count($lists) + 2;
$activeSheet = $objPHPExcel->getActiveSheet();
$activeSheet->setCellValue("A1", "DANH SÁCH XE KHÔNG RÕ NGUỒN GỐC RA VÀO TỪ {$from} ĐẾN {$to}");
$activeSheet->mergeCells('A1:N1');
$activeSheet->getRowDimension('1')->setRowHeight(50);
$activeSheet->getStyle("A2:N2")->getFont()->setBold(true);
$activeSheet->getStyle("A1")->getFont()->setBold(true)->setName('Time New Roman')->setSize(13);
$activeSheet->getStyle("A2:N" . $totals)->getFont()->setName('Time New Roman')->setSize(10);
$activeSheet->getColumnDimension('A')->setWidth(5);
$activeSheet->getColumnDimension('B')->setWidth(10);
$activeSheet->getColumnDimension('C')->setWidth(30);
$activeSheet->getColumnDimension('D')->setWidth(15);
$activeSheet->getColumnDimension('E')->setWidth(15);
$activeSheet->getColumnDimension('F')->setWidth(15);
$activeSheet->getColumnDimension('G')->setWidth(12);
$activeSheet->getColumnDimension('H')->setWidth(10);
$activeSheet->getColumnDimension('I')->setWidth(12);
$activeSheet->getColumnDimension('J')->setWidth(15);
$activeSheet->getColumnDimension('K')->setWidth(15);
$activeSheet->getColumnDimension('L')->setWidth(15);
$activeSheet->getColumnDimension('M')->setWidth(15);
$activeSheet->getColumnDimension('N')->setWidth(20);
$style = array(
'alignment' => array(
'horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
'vertical' => \PHPExcel_Style_Alignment::VERTICAL_CENTER,
'wrap' => true
)
);
$activeSheet->getStyle("A1:N2")->applyFromArray($style);
$activeSheet->getStyle("A2:N2")->applyFromArray([
'fill' => array(
'type' => \PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb' => '00c0ef')
)
]);
$rowCount = 2;
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("A2:N" . $totals)->applyFromArray([
'alignment' => [
'horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
'vertical' => \PHPExcel_Style_Alignment::VERTICAL_CENTER,
'wrap' => true
],
'borders' => [
'allborders' => [
'style' => \PHPExcel_Style_Border::BORDER_THIN
]
]
]);
$activeSheet->getStyle("D3:D" . $totals)->applyFromArray([
'alignment' => [
'horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
'wrap' => true
]
]);
$activeSheet->setCellValue("B" . ($totals + 3), "Tổng số lượt xe vào:");
$activeSheet->setCellValue("B" . ($totals + 4), "Tổng số lượt xe ra:");
$activeSheet->mergeCells("B" . ($totals + 3) . ":C" . ($totals + 3));
$activeSheet->mergeCells("B" . ($totals + 4) . ":C" . ($totals + 4));
$activeSheet->setCellValue("D" . ($totals + 3), LogsUnknow::find()->andWhere(["BETWEEN", "time_in", $f, $t])->count());
$activeSheet->setCellValue("D" . ($totals + 4), LogsUnknow::find()->andWhere(["BETWEEN", "time_out", $f, $t])->count());
$activeSheet->getStyle("B" . ($totals + 3) . ":B" . ($totals + 5))->getFont()->setBold(true)->setItalic(true)->setName('Time New Roman')->setSize(11);
$activeSheet->getStyle("D" . ($totals + 3) . ":D" . ($totals + 5))->getFont()->setItalic(true)->setName('Time New Roman')->setSize(11);
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="bao-cao.xlsx"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
exit();
}
}