update CardRegister full CRUD
This commit is contained in:
194
controllers/CardRegisterController.php
Normal file
194
controllers/CardRegisterController.php
Normal file
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
|
||||
namespace app\controllers;
|
||||
|
||||
use Yii;
|
||||
use app\models\Staff;
|
||||
use app\models\StaffSearch;
|
||||
use app\models\Department;
|
||||
use app\models\common;
|
||||
use yii\web\Controller;
|
||||
use yii\filters\VerbFilter;
|
||||
use yii\helpers\Html;
|
||||
use yii\helpers\Url;
|
||||
|
||||
/**
|
||||
* StaffController implements the CRUD actions for Staff model.
|
||||
*/
|
||||
class CardRegisterController 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'],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function actionIndex() {
|
||||
$this->view->title = 'Đăng ký thẻ';
|
||||
$searchModel = new StaffSearch();
|
||||
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
|
||||
$dataProvider->query->andFilterWhere(["<>", "card_number", 0]);
|
||||
|
||||
return $this->render('index', [
|
||||
'searchModel' => $searchModel,
|
||||
'dataProvider' => $dataProvider,
|
||||
"departmentArray" => Department::departmentArray()
|
||||
]);
|
||||
}
|
||||
|
||||
public function actionCreate() {
|
||||
$model = new Staff();
|
||||
Yii::$app->response->format = "json";
|
||||
if (Yii::$app->request->post()) {
|
||||
$data = Yii::$app->request->post();
|
||||
$check = Staff::findOne(['card_number' => $data['CardNumber']]);
|
||||
if ($check)
|
||||
return ["status" => false, "type" => "card"];
|
||||
$staff = $model->findOne($data["Staff"]);
|
||||
$staff->card_number = $data["CardNumber"];
|
||||
$staff->card_register_time = time();
|
||||
if ($staff->save()) {
|
||||
common::insertSystemLogs(["action" => "register", "description" => "Đăng ký thẻ mới: " . $staff->name, "type" => Yii::$app->controller->id]);
|
||||
return ["status" => true];
|
||||
} else
|
||||
return ["status" => false, "type" => "error"];
|
||||
} else {
|
||||
return [
|
||||
"title" => Html::tag("i", "", ["class" => "fa fa-plus-square"]) . " Thêm",
|
||||
"form" => $this->renderPartial("form", [
|
||||
"model" => $model,
|
||||
"url" => Url::to(["create"]),
|
||||
"staffArray" => Staff::staffArray()
|
||||
])
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
public function actionUpdate($id) {
|
||||
$model = $this->findModel($id);
|
||||
Yii::$app->response->format = "json";
|
||||
if (Yii::$app->request->post()) {
|
||||
$data = Yii::$app->request->post();
|
||||
$check = Staff::findOne(['card_number' => $data['CardNumber']]);
|
||||
if ($check && $check->id != $id)
|
||||
return ["status" => false, "type" => "card"];
|
||||
$model->card_number = $data["CardNumber"];
|
||||
$model->card_register_time = time();
|
||||
if ($model->save()) {
|
||||
common::insertSystemLogs(["action" => "update", "description" => "Thay đổi đăng ký thẻ: " . $model->name, "type" => Yii::$app->controller->id]);
|
||||
return ["status" => true];
|
||||
} else
|
||||
return ["status" => false, "type" => "error"];
|
||||
} else {
|
||||
return [
|
||||
"title" => Html::tag("i", "", ["class" => "fa fa-edit"]) . " Tùy chỉnh",
|
||||
"form" => $this->renderPartial("form", [
|
||||
"model" => $model,
|
||||
"url" => Url::to(["update", "id" => $id]),
|
||||
"staffArray" => [$model->id => $model->code . "-" . $model->name]
|
||||
])
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
public function actionDelete() {
|
||||
if (Yii::$app->request->post()) {
|
||||
$lists = Yii::$app->request->post("lists");
|
||||
Staff::updateAll(["card_number" => 0], ["IN", "id", $lists]);
|
||||
common::insertSystemLogs(["action" => "delete", "description" => "Xóa đăng ký thẻ: " . count($lists) . " nhân viên", "type" => Yii::$app->controller->id]);
|
||||
}
|
||||
}
|
||||
|
||||
protected function findModel($id) {
|
||||
if (($model = Staff::findOne($id)) !== null) {
|
||||
return $model;
|
||||
}
|
||||
|
||||
throw new NotFoundHttpException('The requested page does not exist.');
|
||||
}
|
||||
|
||||
public function actionExport() {
|
||||
$objPHPExcel = new \PHPExcel();
|
||||
$objPHPExcel->setActiveSheetIndex(0);
|
||||
$toExcelFile[] = ["Mã nhân viên", "Tên nhân viên", "Số thẻ", "Phòng ban", "Ngày dăng ký thẻ"];
|
||||
$staffs = Staff::find()->andWhere(["<>", "card_number", 0])->all();
|
||||
$departmentArray = Department::departmentArray();
|
||||
foreach ($staffs as $k => $v) {
|
||||
$ExportData[] = $v->code;
|
||||
$ExportData[] = $v->name;
|
||||
$ExportData[] = $v->card_number;
|
||||
$ExportData[] = isset($departmentArray[$v->department_id]) ? $departmentArray[$v->department_id] : "";
|
||||
$ExportData[] = date("H:i:s d/m/Y", $v->card_register_time);
|
||||
$toExcelFile[] = $ExportData;
|
||||
unset($ExportData);
|
||||
}
|
||||
$totals = count($staffs) + 1;
|
||||
$activeSheet = $objPHPExcel->getActiveSheet();
|
||||
$activeSheet->getStyle("A1:E" . $totals)->getFont()->setName('Time New Roman')->setSize(10);
|
||||
$activeSheet->getStyle("A1:E1")->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:E" . $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="card_register_' . date("YmdHis") . '.xlsx"');
|
||||
header('Cache-Control: max-age=0');
|
||||
common::SaveViaTempFile($objWriter);
|
||||
exit();
|
||||
}
|
||||
|
||||
public function actionLogs() {
|
||||
if (Yii::$app->request->isAjax) {
|
||||
Yii::$app->response->format = "json";
|
||||
return [
|
||||
"title" => Html::tag("i", "", ["class" => "fa fa-file"]) . " Ghi nhận hệ thống",
|
||||
"form" => \app\widgets\SystemLogsView::widget(['type' => Yii::$app->controller->id])
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -59,9 +59,11 @@ class StaffController extends Controller {
|
||||
$check = Staff::findOne(['code' => $data['Code']]);
|
||||
if ($check)
|
||||
return ["status" => false, "type" => "code"];
|
||||
$check = Staff::findOne(['card_number' => $data['CardNumber']]);
|
||||
if ($check)
|
||||
return ["status" => false, "type" => "card"];
|
||||
if ($data['CardNumber'] !== "" && $data['CardNumber'] !== "0") {
|
||||
$check = Staff::findOne(['card_number' => $data['CardNumber']]);
|
||||
if ($check)
|
||||
return ["status" => false, "type" => "card"];
|
||||
}
|
||||
if ($model->create($data)) {
|
||||
common::insertSystemLogs(["action" => "insert", "description" => "Thêm mới nhân viên: " . $data["Name"], "type" => Yii::$app->controller->id]);
|
||||
return ["status" => true];
|
||||
@@ -88,11 +90,16 @@ class StaffController extends Controller {
|
||||
$check = Staff::findOne(['code' => $data['Code']]);
|
||||
if ($check && $check->id != $id)
|
||||
return ["status" => false, "type" => "code"];
|
||||
$check = Staff::findOne(['card_number' => $data['CardNumber']]);
|
||||
if ($check && $check->id != $id)
|
||||
return ["status" => false, "type" => "card"];
|
||||
if ($data['CardNumber'] !== "" && $data['CardNumber'] !== "0") {
|
||||
$check = Staff::findOne(['card_number' => $data['CardNumber']]);
|
||||
if ($check && $check->id != $id)
|
||||
return ["status" => false, "type" => "card"];
|
||||
}
|
||||
$model->name = $data["Name"];
|
||||
$model->code = $data["Code"];
|
||||
if ($model->card_number !== $data["CardNumber"]) {
|
||||
$model->card_register_time = time();
|
||||
}
|
||||
$model->card_number = $data["CardNumber"] != "" ? $data["CardNumber"] : 0;
|
||||
$model->department_id = $data["Department"];
|
||||
$model->gender = $data["Gender"];
|
||||
@@ -239,6 +246,7 @@ class StaffController extends Controller {
|
||||
$val["I"] !== "" ? date_format(date_create_from_format('d/m/Y', $val["I"]), 'U') : 0,
|
||||
$val["J"],
|
||||
time(),
|
||||
time(),
|
||||
time()
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user