update api open door

This commit is contained in:
dongpd 2022-10-18 08:38:54 +07:00
parent d6d7dc3719
commit c8e0bb7795
4 changed files with 103 additions and 4 deletions

View File

@ -9,6 +9,7 @@ use yii\helpers\FileHelper;
use app\models\CaptureLogs;
use app\models\ListManagement;
use app\models\common;
use app\models\Schedule;
/**
* CardController implements the CRUD actions for Card model.
@ -582,6 +583,12 @@ class ApiController extends Controller {
if ($value['action'] == "remove_info")
$result = $this->removeInfo($value);
if ($value['action'] == "insert_door_open")
$result = $this->insertSchedule($value);
if ($value['action'] == "remove_door_open")
$result = $this->removeSchedule($value);
if ($value['action'] == "reset_all") {
\Yii::$app->db->createCommand()->truncateTable('capture_logs')->execute();
\Yii::$app->db->createCommand()->truncateTable('list_management')->execute();
@ -592,7 +599,7 @@ class ApiController extends Controller {
if (Yii::$app->params['autoSyncLog'])
file_put_contents("logs.txt", $count . "\t" . $value['action'] . "\t" . $value['files_name'][0] . "\t" . ($result ? "true" : "false") . "\t" . $processTime . "\t" . $value['name'] . "\n", FILE_APPEND);
if (in_array($value['action'], ['insert_image', 'remove_image', 'insert_info', 'remove_info', 'reset_all'])) {
if (in_array($value['action'], ['insert_image', 'remove_image', 'insert_info', 'remove_info', 'reset_all', 'insert_door_open', 'remove_door_open'])) {
if ($result)
$infomation[] = [
"obj_Log" => $value['obj_Log'],
@ -764,4 +771,92 @@ class ApiController extends Controller {
return false;
}
public function insertSchedule($data) {
$model = new Schedule();
$info = ListManagement::findOne(['staff_id' => $data['id']]);
$schedule = Schedule::findOne(['staff_id' => $info->id, 'id_door_calendar' => $data['id_door_calendar']]);
if ($schedule) {
$schedule->from_time = $data['time_in'];
$schedule->to_time = $data['time_out'];
$schedule->from_date = $this->formatDateSchedule($data['day_in']);
$schedule->to_date = $this->formatDateSchedule($data['day_out']);
$schedule->date_of_week = $this->formatDayOfWeekSchedule($data['day_of_week']);
return $schedule->save();
} else {
return $model->create([
'staff_id' => $info->id,
'from_time' => $data['time_in'],
'to_time' => $data['time_out'],
'from_date' => $this->formatDateSchedule($data['day_in']),
'to_date' => $this->formatDateSchedule($data['day_out']),
'date_of_week' => $this->formatDayOfWeekSchedule($data['day_of_week']),
'id_door_calendar' => $data['id_door_calendar']
]);
}
}
public function removeSchedule($data) {
$info = ListManagement::findOne(['staff_id' => $data['id']]);
if ($info) {
$schedule = \app\models\Schedule::findOne([
'id_door_calendar' => $data['id_door_calendar'],
'staff_id' => $info->id
]);
if ($schedule)
$schedule->delete();
}
return true;
}
public function formatDateSchedule($date) {
$temp = explode(" ", $date);
$parse = explode("-", $temp[0]);
return $parse[2] . "/" . $parse[1] . "/" . $parse[0];
}
public function formatDayOfWeekSchedule($dayOfWeek) {
$days = [];
$temp = explode("|", $dayOfWeek);
foreach ($temp as $key => $value) {
if ($value != "")
$days[] = intval($value) + 2;
}
return implode(",", $days);
}
public function actionCheckOpenDoor() {
if (Yii::$app->request->post()) {
$post = Yii::$app->request->bodyParams;
$info = ListManagement::findOne(["staff_id" => $post["object_id"]]);
$timeGet = date_format(date_create_from_format('Y-m-d H:i:s', $post['timeget']), 'U');
$openDoor = 0;
if ($info) {
$schedules = Schedule::find()->andWhere(["staff_id" => $info->id])->all();
foreach ($schedules as $key => $value) {
$fromDate = date_format(date_create_from_format('H:i:s d/m/Y', "00:00:00 " . $value->from_date), 'U');
$toDate = date_format(date_create_from_format('H:i:s d/m/Y', "23:59:59 " . $value->to_date), 'U');
if ($timeGet > $fromDate && $timeGet < $toDate) {
$dateOfWeek = explode(",", $value->date_of_week);
$weekDay = intval(date("w", $timeGet)) + 1;
if ($weekDay == 1)
$weekDay = 8;
if (in_array($weekDay, $dateOfWeek)) {
$fromTime = date_format(date_create_from_format('H:i:s d/m/Y', $value->from_time . ":00 " . date("d/m/Y", $timeGet)), 'U');
$toTime = date_format(date_create_from_format('H:i:s d/m/Y', $value->to_time . ":59 " . date("d/m/Y", $timeGet)), 'U');
if ($timeGet > $fromTime && $timeGet < $toTime)
$openDoor = 1;
}
}
}
}
Yii::$app->response->format = "json";
return [
"status" => 10000,
"data" => [
"open_door" => $openDoor
]
];
}
}
}

BIN
db/app.db

Binary file not shown.

View File

@ -112,7 +112,7 @@ class ListManagementGrid {
$btnEdit = "<i class='fa fa-edit text-green' onclick='formEditSchedule(this);' data-href='" . Url::to(['/list-management/edit-schedule', 'id' => $value->id]) . "'></i>";
$btnDelete = "<i class='fa fa-remove text-red' onclick='formDeleteSchedule(this);' data-href='" . Url::to(['/list-management/delete-schedule', 'id' => $value->id]) . "'></i>";
$btn = "<span class='btn-schedule'>" . $btnEdit . " " . $btnDelete . "</span>";
$html .= "<li class='schedule-item'>[" . $value->from_time . "-" . $value->to_time . "] " . $value->from_date . " - " . $value->to_date . " " . $btn . "</li>";
$html .= "<li class='schedule-item'>[" . $value->from_time . "-" . $value->to_time . "] " . $value->from_date . " - " . $value->to_date . " [" . $value->date_of_week . "] " . $btn . "</li>";
}
$html .= "</ul>";
return $html;

View File

@ -14,6 +14,7 @@ use Yii;
* @property string $from_date
* @property string $to_date
* @property string $date_of_week
* @property int $id_door_calendar
*/
class Schedule extends \yii\db\ActiveRecord {
@ -30,6 +31,7 @@ class Schedule extends \yii\db\ActiveRecord {
public function rules() {
return [
[['staff_id', 'from_time', 'to_time', 'from_date', 'to_date', 'date_of_week'], 'string'],
[['id_door_calendar'], 'integer'],
];
}
@ -45,6 +47,7 @@ class Schedule extends \yii\db\ActiveRecord {
'from_date' => 'From Date',
'to_date' => 'To Date',
'date_of_week' => 'Date Of Week',
'id_door_calendar' => 'id_door_calendar'
];
}
@ -56,12 +59,13 @@ class Schedule extends \yii\db\ActiveRecord {
public function create($data) {
$r = $this->load([
'staff_id' => $data['staff_id'],
'staff_id' => strval($data['staff_id']),
'from_time' => $data['from_time'],
'to_time' => $data['to_time'],
'from_date' => $data['from_date'],
'to_date' => $data['to_date'],
'date_of_week' => $data['date_of_week']
'date_of_week' => $data['date_of_week'],
'id_door_calendar' => $data['id_door_calendar']
], '');
if ($r) {
try {