From 2a8b009c122a6a2b22e66008f9920e244cdc568b Mon Sep 17 00:00:00 2001 From: dongpd Date: Sat, 10 Oct 2020 16:11:19 +0700 Subject: [PATCH] update Staff full CRUD --- access_control.sql | 57 +++- assets/StaffAsset.php | 23 ++ controllers/StaffController.php | 262 ++++++++++++++++++ helpers/CommonGrid.php | 12 +- helpers/StaffGrid.php | 31 +++ models/Staff.php | 111 ++++++++ models/StaffSearch.php | 78 ++++++ .../yiisoft/yii2-app/layouts/header.php | 2 +- .../yiisoft/yii2-app/layouts/left.php | 4 +- views/staff/form.tpl | 95 +++++++ views/staff/import.tpl | 55 ++++ views/staff/index.tpl | 84 ++++++ .../1602319719_staff_20201010154609.xlsx | Bin 0 -> 6485 bytes .../1602320488_staff_20201010155211.xlsx | Bin 0 -> 8455 bytes .../1602320930_staff_20201010155211.xlsx | Bin 0 -> 8455 bytes .../1602320972_staff_20201010155211.xlsx | Bin 0 -> 8455 bytes web/js/common.js | 25 +- web/js/staff.js | 225 +++++++++++++++ 18 files changed, 1050 insertions(+), 14 deletions(-) create mode 100644 assets/StaffAsset.php create mode 100644 controllers/StaffController.php create mode 100644 helpers/StaffGrid.php create mode 100644 models/Staff.php create mode 100644 models/StaffSearch.php create mode 100644 views/staff/form.tpl create mode 100644 views/staff/import.tpl create mode 100644 views/staff/index.tpl create mode 100644 web/data/excel/1602319719_staff_20201010154609.xlsx create mode 100644 web/data/excel/1602320488_staff_20201010155211.xlsx create mode 100644 web/data/excel/1602320930_staff_20201010155211.xlsx create mode 100644 web/data/excel/1602320972_staff_20201010155211.xlsx create mode 100644 web/js/staff.js diff --git a/access_control.sql b/access_control.sql index 8fdac2f8..9d544c54 100644 --- a/access_control.sql +++ b/access_control.sql @@ -3,7 +3,7 @@ -- https://www.phpmyadmin.net/ -- -- Host: 127.0.0.1 --- Generation Time: Oct 10, 2020 at 07:36 AM +-- Generation Time: Oct 10, 2020 at 11:10 AM -- Server version: 10.4.8-MariaDB -- PHP Version: 7.1.33 @@ -158,6 +158,37 @@ CREATE TABLE `device` ( -- -------------------------------------------------------- +-- +-- Table structure for table `staff` +-- + +CREATE TABLE `staff` ( + `id` int(11) NOT NULL, + `code` int(11) NOT NULL, + `name` varchar(100) NOT NULL, + `card_number` int(11) NOT NULL DEFAULT 0, + `department_id` int(11) NOT NULL DEFAULT 0, + `gender` varchar(10) NOT NULL, + `birthday` int(11) NOT NULL DEFAULT 0, + `email` varchar(100) DEFAULT NULL, + `phone` varchar(20) DEFAULT NULL, + `date_in` int(11) NOT NULL DEFAULT 0, + `address` text DEFAULT NULL, + `created_at` int(11) NOT NULL DEFAULT 0, + `modified_at` int(11) NOT NULL DEFAULT 0 +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- +-- Dumping data for table `staff` +-- + +INSERT INTO `staff` (`id`, `code`, `name`, `card_number`, `department_id`, `gender`, `birthday`, `email`, `phone`, `date_in`, `address`, `created_at`, `modified_at`) VALUES +(1, 1, 'Phạm Đức Đông', 1000001, 1, 'M', 691231867, '', '', 1602319867, '', 1602319867, 1602319867), +(2, 2, 'Ngô Văn Dũng', 10000002, 1, 'M', 608719882, '', '', 1602319882, '', 1602319882, 1602319882), +(3, 3, 'Hoàng Hồng Sơn', 10000003, 1, 'M', 622544932, NULL, NULL, 1602320932, NULL, 1602320932, 1602320932); + +-- -------------------------------------------------------- + -- -- Table structure for table `system_logs` -- @@ -190,7 +221,15 @@ INSERT INTO `system_logs` (`id`, `user_id`, `time`, `action`, `description`, `ty (12, 1, 1602226793, 'delete', 'Xóa khu vực: Khu vực 1', 'area'), (13, 1, 1602226793, 'delete', 'Xóa khu vực: Khu vực 2', 'area'), (14, 1, 1602227006, 'insert', 'Thêm mới khu vực: Khu vực 2', 'area'), -(15, 1, 1602227907, 'insert', 'Thêm mới khu vực: Khu vực 1', 'area'); +(15, 1, 1602227907, 'insert', 'Thêm mới khu vực: Khu vực 1', 'area'), +(16, 1, 1602316450, 'insert', 'Thêm mới nhân viên: Phạm Đức Đông', 'staff'), +(17, 1, 1602317528, 'update', 'Chỉnh sửa nhân viên: Phạm Đức Đông', 'staff'), +(18, 1, 1602318491, 'update', 'Chỉnh sửa nhân viên: Phạm Đức Đông', 'staff'), +(19, 1, 1602318858, 'insert', 'Thêm mới nhân viên: Ngô Văn Dũng', 'staff'), +(20, 1, 1602318893, 'delete', 'Xóa 2 nhân viên', 'staff'), +(21, 1, 1602319867, 'insert', 'Thêm mới nhân viên: Phạm Đức Đông', 'staff'), +(22, 1, 1602319882, 'insert', 'Thêm mới nhân viên: Ngô Văn Dũng', 'staff'), +(23, 1, 1602320932, 'import', 'Nhập dữ liệu: 1 nhân viên mới', 'staff'); -- -------------------------------------------------------- @@ -269,6 +308,12 @@ ALTER TABLE `department` ALTER TABLE `device` ADD PRIMARY KEY (`id`); +-- +-- Indexes for table `staff` +-- +ALTER TABLE `staff` + ADD PRIMARY KEY (`id`); + -- -- Indexes for table `system_logs` -- @@ -304,11 +349,17 @@ ALTER TABLE `department` ALTER TABLE `device` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; +-- +-- AUTO_INCREMENT for table `staff` +-- +ALTER TABLE `staff` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4; + -- -- AUTO_INCREMENT for table `system_logs` -- ALTER TABLE `system_logs` - MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=16; + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=24; -- -- AUTO_INCREMENT for table `user` diff --git a/assets/StaffAsset.php b/assets/StaffAsset.php new file mode 100644 index 00000000..2157f901 --- /dev/null +++ b/assets/StaffAsset.php @@ -0,0 +1,23 @@ +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 = 'Nhân viên'; + $searchModel = new StaffSearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + "departmentArray" => Department::departmentArray(), + "genderArray" => Staff::$genderArray + ]); + } + + public function actionCreate() { + $model = new Staff(); + Yii::$app->response->format = "json"; + if (Yii::$app->request->post()) { + $data = Yii::$app->request->post(); + $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 ($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]; + } 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"]), + "departmentArray" => Department::departmentArray(), + "genderArray" => Staff::$genderArray + ]) + ]; + } + } + + 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(['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"]; + $model->name = $data["Name"]; + $model->code = $data["Code"]; + $model->card_number = $data["CardNumber"] != "" ? $data["CardNumber"] : 0; + $model->department_id = $data["Department"]; + $model->gender = $data["Gender"]; + $model->birthday = date_format(date_create_from_format('d/m/Y', $data["BirthDay"]), 'U'); + $model->email = $data["Email"]; + $model->phone = $data["Phone"]; + $model->date_in = date_format(date_create_from_format('d/m/Y', $data["DateIn"]), 'U'); + $model->address = $data["Address"]; + $model->modified_at = time(); + if ($model->save()) { + common::insertSystemLogs(["action" => "update", "description" => "Chỉnh sửa nhân viên: " . $data["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]), + "departmentArray" => Department::departmentArray(), + "genderArray" => Staff::$genderArray + ]) + ]; + } + } + + public function actionDelete() { + if (Yii::$app->request->post()) { + $lists = Yii::$app->request->post("lists"); + Staff::deleteAll(["IN", "id", $lists]); + common::insertSystemLogs(["action" => "delete", "description" => "Xóa " . 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", "Giới tính", "Ngày sinh", "Email", "Điện thoại", "Ngày bắt đầu làm việc", "Địa chỉ"]; + $staffs = Staff::find()->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[] = $v->gender; + $ExportData[] = date("d/m/Y", $v->birthday); + $ExportData[] = $v->email; + $ExportData[] = $v->phone; + $ExportData[] = date("d/m/Y", $v->date_in); + $ExportData[] = $v->address; + $toExcelFile[] = $ExportData; + unset($ExportData); + } + $totals = count($staffs) + 1; + $activeSheet = $objPHPExcel->getActiveSheet(); + $activeSheet->getStyle("A1:J" . $totals)->getFont()->setName('Time New Roman')->setSize(10); + $activeSheet->getStyle("A1:J1")->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:J" . $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="staff_' . date("YmdHis") . '.xlsx"'); + header('Cache-Control: max-age=0'); + common::SaveViaTempFile($objWriter); + exit(); + } + + public function actionUpload() { + if (Yii::$app->request->post()) { + $common = new common(); + $fileUploads = $common->UploadFile("file", ["XLS", "XLSX"], "excel"); + $file_type = \PHPExcel_IOFactory::identify($fileUploads); + $objReader = \PHPExcel_IOFactory::createReader($file_type); + $objPHPExcel = $objReader->load($fileUploads); + $sheet_data = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true); + Yii::$app->response->format = 'json'; + return [ + "title" => Html::tag("i", "", ["class" => "fa fa-upload"]) . " Nhập", + "form" => $this->renderPartial("import", [ + "data" => $sheet_data, + "model" => new Staff() + ]) + ]; + } + } + + public function actionImport() { + if (Yii::$app->request->post()) { + $post = Yii::$app->request->post("lists"); + $datas = []; + foreach ($post as $key => $value) { + $val = json_decode($value, true); + $parent = Department::findOne(["name" => $val["D"]]); + $datas[] = [ + $val["A"], + $val["B"], + $val["C"], + $parent ? $parent->code : 1, + in_array($val["E"], ["M", "F"]) ? $val["E"] : "M", + $val["F"] !== "" ? date_format(date_create_from_format('d/m/Y', $val["F"]), 'U') : 0, + $val["G"], + $val["H"], + $val["I"] !== "" ? date_format(date_create_from_format('d/m/Y', $val["I"]), 'U') : 0, + $val["J"], + time(), + time() + ]; + } + $model = new Staff(); + $model->multiCreate($datas); + common::insertSystemLogs(["action" => "import", "description" => "Nhập dữ liệu: " . count($post) . " nhân viên mới", "type" => Yii::$app->controller->id]); + return; + } + } + + 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]) + ]; + } + } + +} diff --git a/helpers/CommonGrid.php b/helpers/CommonGrid.php index 344e7a39..f9a24406 100644 --- a/helpers/CommonGrid.php +++ b/helpers/CommonGrid.php @@ -62,18 +62,18 @@ class CommonGrid { }; } - public static function checkbox($type) { - return function($model) use ($type) { - if ($model->id == 1) + public static function checkbox($type, $haveRoot = true) { + return function($model) use ($type, $haveRoot) { + if ($model->id == 1 && $haveRoot) return ""; return ""; }; } - public static function rows($type) { - return function($model, $index, $widget, $grid) use ($type) { + public static function rows($type, $bigSize = false) { + return function($model, $index, $widget, $grid) use ($type, $bigSize) { return [ - "ondblclick" => "common.form(this, '{$type}');", + "ondblclick" => "common.form(this, '{$type}', {$bigSize});", "style" => "cursor: pointer;", "data" => [ "href" => Url::to(["update", "id" => $model->id]) diff --git a/helpers/StaffGrid.php b/helpers/StaffGrid.php new file mode 100644 index 00000000..493e8ed1 --- /dev/null +++ b/helpers/StaffGrid.php @@ -0,0 +1,31 @@ +department_id]) ? $array[$model->department_id] : ""; + }; + } + + public static function gender($array) { + return function($model) use ($array) { + return isset($array[$model->gender]) ? $array[$model->gender] : ""; + }; + } + + public static function birthday() { + return function($model) { + return date("d/m/Y", $model->birthday); + }; + } + + public static function dateIn() { + return function($model) { + return date("d/m/Y", $model->date_in); + }; + } + +} diff --git a/models/Staff.php b/models/Staff.php new file mode 100644 index 00000000..09492dca --- /dev/null +++ b/models/Staff.php @@ -0,0 +1,111 @@ + 100], + [['gender'], 'string', 'max' => 10], + [['phone'], 'string', 'max' => 20], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() { + return [ + 'id' => 'ID', + 'code' => 'Mã nhân viên', + 'name' => 'Tên nhân viên', + 'card_number' => 'Số thẻ', + 'department_id' => 'Phòng ban', + 'gender' => 'Giới tính', + 'birthday' => 'Ngày sinh', + 'email' => 'Email', + 'phone' => 'Điện thoại', + 'date_in' => 'Ngày bắt đầu làm việc', + 'address' => 'Địa chỉ', + 'created_at' => 'Thời gian tạo', + 'modified_at' => 'Thời gian sửa', + ]; + } + + public static $genderArray = ["M" => "Nam", "F" => "Nữ"]; + + public function create($data) { + $r = $this->load([ + "code" => $data["Code"], + "name" => $data["Name"], + "card_number" => $data["CardNumber"] != "" ? $data["CardNumber"] : 0, + "department_id" => $data["Department"], + "gender" => $data["Gender"], + "birthday" => date_format(date_create_from_format('d/m/Y', $data["BirthDay"]), 'U'), + "email" => $data["Email"], + "phone" => $data["Phone"], + "date_in" => date_format(date_create_from_format('d/m/Y', $data["DateIn"]), 'U'), + "address" => $data["Address"], + "created_at" => time(), + "modified_at" => time() + ], ''); + if ($r) { + try { + $this->save(); + return $this->id; + } catch (\Exception $ex) { + return false; + } + } + } + + public function multiCreate($datas) { + $field = ['code', 'name', 'card_number', 'department_id', 'gender', 'birthday', 'email', 'phone', 'date_in', 'address', 'created_at', 'modified_at']; + static::getDb()->createCommand()->batchInsert($this->tableName(), $field, $datas)->execute(); + return; + } + + public function checkStatusImport($data) { + if ($this->findOne(["code" => $data["A"]])) + return ["status" => false, "description" => "Mã nhân viên đã tồn tại"]; + if ($this->findOne(["card_number" => $data["C"]])) + return ["status" => false, "description" => "Số thẻ đã tồn tại"]; + if (!Department::findOne(["name" => $data["D"]])) + return ["status" => true, "description" => "Phòng ban không tồn tại"]; + return ["status" => true, "description" => ""]; + } + +} diff --git a/models/StaffSearch.php b/models/StaffSearch.php new file mode 100644 index 00000000..ff99b60c --- /dev/null +++ b/models/StaffSearch.php @@ -0,0 +1,78 @@ + $query, + ]); + + $this->load($params); + + if (!$this->validate()) { + // uncomment the following line if you do not want to return any records when validation fails + // $query->where('0=1'); + return $dataProvider; + } + + // grid filtering conditions + $query->andFilterWhere([ + 'id' => $this->id, + 'code' => $this->code, + 'card_number' => $this->card_number, + 'department_id' => $this->department_id, + 'birthday' => $this->birthday, + 'date_in' => $this->date_in, + 'created_at' => $this->created_at, + 'modified_at' => $this->modified_at, + ]); + + $query->andFilterWhere(['like', 'name', $this->name]) + ->andFilterWhere(['like', 'gender', $this->gender]) + ->andFilterWhere(['like', 'email', $this->email]) + ->andFilterWhere(['like', 'phone', $this->phone]) + ->andFilterWhere(['like', 'address', $this->address]); + + return $dataProvider; + } + +} diff --git a/vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app/layouts/header.php b/vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app/layouts/header.php index 1d69f2d5..90699534 100644 --- a/vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app/layouts/header.php +++ b/vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app/layouts/header.php @@ -20,7 +20,7 @@ use yii\widgets\ActiveForm;