2021-07-05 15:30:35 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace frontend\modules\api\controllers;
|
|
|
|
|
2021-07-28 17:08:08 +03:00
|
|
|
use common\models\Reports;
|
2021-11-29 18:16:28 +03:00
|
|
|
use common\models\ReportsTask;
|
2021-11-30 16:54:04 +03:00
|
|
|
use common\models\UserCard;
|
2022-12-20 19:24:38 +03:00
|
|
|
use DateTime;
|
2021-07-28 17:08:08 +03:00
|
|
|
use frontend\modules\api\models\ReportSearchForm;
|
|
|
|
use Yii;
|
2022-12-20 19:24:38 +03:00
|
|
|
use yii\helpers\ArrayHelper;
|
2021-07-28 17:08:08 +03:00
|
|
|
use yii\web\BadRequestHttpException;
|
|
|
|
use yii\web\NotFoundHttpException;
|
2021-07-05 15:30:35 +03:00
|
|
|
|
2021-11-29 18:21:50 +03:00
|
|
|
class ReportsController extends ApiController
|
2021-07-05 15:30:35 +03:00
|
|
|
{
|
2023-11-03 12:01:29 +03:00
|
|
|
/**
|
|
|
|
* @OA\Get(path="/user-response/index",
|
|
|
|
* summary="Поиск отчётов по промежутку дат",
|
|
|
|
* description="Осуществляет поиск отчётов пользователя по промежутку дат",
|
|
|
|
* security={
|
|
|
|
* {"bearerAuth": {}}
|
|
|
|
* },
|
|
|
|
* tags={"Reports"},
|
|
|
|
* @OA\RequestBody(
|
|
|
|
* @OA\MediaType(
|
|
|
|
* mediaType="application/x-www-form-urlencoded",
|
|
|
|
* @OA\Schema(
|
|
|
|
* required={"user_card_id"},
|
|
|
|
* @OA\Property(
|
|
|
|
* property="user_card_id",
|
|
|
|
* type="integer",
|
|
|
|
* description="Идентификатор карты(профиля) пользователя",
|
|
|
|
* nullable=false,
|
|
|
|
* ),
|
|
|
|
* @OA\Property(
|
|
|
|
* property="fromDate",
|
|
|
|
* type="DateTime",
|
|
|
|
* description="Дата начала поиска",
|
|
|
|
* nullable=false,
|
|
|
|
* ),
|
|
|
|
* @OA\Property(
|
|
|
|
* property="toDate",
|
|
|
|
* type="DateTime",
|
|
|
|
* description="Дата конца периода поиска",
|
|
|
|
* nullable=false,
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
*
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Возвращает объект Запроса",
|
|
|
|
* @OA\MediaType(
|
|
|
|
* mediaType="application/json",
|
|
|
|
* @OA\Schema(ref="#/components/schemas/ReportsResponseExample"),
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @param $user_card_id
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function actionIndex($user_card_id): array
|
2021-07-05 15:30:35 +03:00
|
|
|
{
|
2021-07-28 17:08:08 +03:00
|
|
|
$reportsModel = new ReportSearchForm();
|
|
|
|
|
|
|
|
$params = Yii::$app->request->get();
|
|
|
|
$reportsModel->attributes = $params;
|
|
|
|
|
2023-01-19 16:29:06 +03:00
|
|
|
$reportsModel->limit = $params['limit'] ?? $reportsModel->limit;
|
|
|
|
$reportsModel->offset = $params['offset'] ?? $reportsModel->offset;
|
|
|
|
|
2021-07-28 17:08:08 +03:00
|
|
|
if(!$reportsModel->validate()){
|
|
|
|
return $reportsModel->errors;
|
|
|
|
}
|
|
|
|
|
2023-01-19 16:29:06 +03:00
|
|
|
return $reportsModel->byParams();
|
2021-11-29 18:16:28 +03:00
|
|
|
}
|
|
|
|
|
2022-02-15 12:42:56 +03:00
|
|
|
/**
|
2023-11-03 12:01:29 +03:00
|
|
|
* @OA\Get(path="/user-response/find-by-date",
|
|
|
|
* summary="Поиск отчётов по дате",
|
|
|
|
* description="Осуществляет поиск отчётов пользователя по дате",
|
|
|
|
* security={
|
|
|
|
* {"bearerAuth": {}}
|
|
|
|
* },
|
|
|
|
* tags={"Reports"},
|
|
|
|
* @OA\RequestBody(
|
|
|
|
* @OA\MediaType(
|
|
|
|
* mediaType="application/x-www-form-urlencoded",
|
|
|
|
* @OA\Schema(
|
|
|
|
* required={"user_card_id"},
|
|
|
|
* @OA\Property(
|
|
|
|
* property="user_card_id",
|
|
|
|
* type="integer",
|
|
|
|
* description="Идентификатор карты(профиля) пользователя",
|
|
|
|
* nullable=false,
|
|
|
|
* ),
|
|
|
|
* @OA\Property(
|
|
|
|
* property="date",
|
|
|
|
* type="DateTime",
|
|
|
|
* description="Дата поиска",
|
|
|
|
* nullable=false,
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
*
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Возвращает объект Запроса",
|
|
|
|
* @OA\MediaType(
|
|
|
|
* mediaType="application/json",
|
|
|
|
* @OA\Schema(ref="#/components/schemas/ReportsResponseExample"),
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @throws BadRequestHttpException
|
2022-02-15 12:42:56 +03:00
|
|
|
* @throws NotFoundHttpException
|
|
|
|
*/
|
2023-11-03 12:01:29 +03:00
|
|
|
public function actionFindByDate(): array
|
2022-02-15 12:42:56 +03:00
|
|
|
{
|
|
|
|
$reportsModel = new ReportSearchForm();
|
|
|
|
|
|
|
|
$params = Yii::$app->request->get();
|
|
|
|
if(!isset($params['user_card_id']) or !isset($params['date'])){
|
|
|
|
throw new NotFoundHttpException('Required parameter are missing!');
|
|
|
|
}
|
|
|
|
|
|
|
|
$reportsModel->attributes = $params;
|
2023-01-19 14:30:49 +03:00
|
|
|
$reportsModel->date = $params['date'];
|
|
|
|
|
|
|
|
if (!$this->checkDate($reportsModel->date)) {
|
|
|
|
throw new BadRequestHttpException('Wrong date format');
|
|
|
|
}
|
|
|
|
|
2022-02-15 12:42:56 +03:00
|
|
|
if(!$reportsModel->validate()){
|
|
|
|
return $reportsModel->errors;
|
|
|
|
}
|
2023-01-19 14:30:49 +03:00
|
|
|
|
2023-01-19 13:55:28 +03:00
|
|
|
return $reportsModel->findByDate();
|
2022-02-15 12:42:56 +03:00
|
|
|
}
|
|
|
|
|
2023-11-03 12:01:29 +03:00
|
|
|
/**
|
|
|
|
* @OA\Post(path="/reports/create",
|
|
|
|
* summary="Создание отчёта",
|
|
|
|
* description="Метод для создания нового отчёта",
|
|
|
|
* security={
|
|
|
|
* {"bearerAuth": {}}
|
|
|
|
* },
|
|
|
|
* tags={"Reports"},
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="difficulties",
|
|
|
|
* in="query",
|
|
|
|
* required=false,
|
|
|
|
* description="Описание сложностей возникших при выполнении задач",
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="tomorrow",
|
|
|
|
* in="query",
|
|
|
|
* required=false,
|
|
|
|
* description="Описание планов на завтра",
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="created_at",
|
|
|
|
* in="query",
|
|
|
|
* required=false,
|
|
|
|
* description="Дата создания",
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="status",
|
|
|
|
* in="query",
|
|
|
|
* required=false,
|
|
|
|
* description="Статус",
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="integer",
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="user_card_id",
|
|
|
|
* in="query",
|
|
|
|
* required=false,
|
|
|
|
* description="ID карты(профиля) пользователя",
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="integer",
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="project_id",
|
|
|
|
* in="query",
|
|
|
|
* required=false,
|
|
|
|
* description="ID проекта",
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="integer",
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="company_id",
|
|
|
|
* in="query",
|
|
|
|
* required=false,
|
|
|
|
* description="ID компании",
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="integer",
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="tasks[]",
|
|
|
|
* in="query",
|
|
|
|
* required=false,
|
|
|
|
* description="Масив задач. ",
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="array",
|
|
|
|
* @OA\Items(
|
|
|
|
* type="object",
|
|
|
|
* @OA\Property(
|
|
|
|
* property="task",
|
|
|
|
* description="Название задачи",
|
|
|
|
* type="string",
|
|
|
|
* ),
|
|
|
|
* @OA\Property(
|
|
|
|
* property="hours_spent",
|
|
|
|
* description="Затраченное количество часов",
|
|
|
|
* type="string",
|
|
|
|
* ),
|
|
|
|
* @OA\Property(
|
|
|
|
* property="minutes_spent",
|
|
|
|
* description="Затраченное количество минут",
|
|
|
|
* type="string",
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Возвращает объект Запроса",
|
|
|
|
* @OA\MediaType(
|
|
|
|
* mediaType="application/json",
|
|
|
|
* @OA\Schema(ref="#/components/schemas/ReportsResponseCreateExample"),
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @throws BadRequestHttpException
|
|
|
|
*/
|
|
|
|
public function actionCreate(): array
|
2021-07-28 17:08:08 +03:00
|
|
|
{
|
2021-09-07 16:32:57 +03:00
|
|
|
$params = Yii::$app->request->post();
|
2021-11-29 18:16:28 +03:00
|
|
|
if (!isset($params['tasks'])){
|
|
|
|
throw new BadRequestHttpException('Нет параметра tasks');
|
|
|
|
}
|
|
|
|
|
2021-11-30 16:54:04 +03:00
|
|
|
if(!isset($params['user_card_id'])){
|
|
|
|
$userCard = UserCard::find()->where(['id_user' => Yii::$app->user->id])->one();
|
|
|
|
if($userCard){
|
2022-12-16 20:18:25 +03:00
|
|
|
$params['user_card_id'] = $userCard->id;
|
2021-11-30 16:54:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-16 20:18:25 +03:00
|
|
|
$reportsModel = new Reports();
|
|
|
|
$reportsModel->attributes = $params;
|
|
|
|
if(!$reportsModel->validate() || !$reportsModel->save()){
|
|
|
|
throw new BadRequestHttpException(json_encode($reportsModel->errors));
|
|
|
|
}
|
2021-07-28 17:08:08 +03:00
|
|
|
|
2022-12-16 20:18:25 +03:00
|
|
|
$tasks = $params['tasks'];
|
|
|
|
foreach ($tasks as $task) {
|
|
|
|
$reportsTask = new ReportsTask();
|
|
|
|
$reportsTask->attributes = $task;
|
|
|
|
$reportsTask->report_id = $reportsModel->id;
|
|
|
|
$reportsTask->created_at = $reportsTask->created_at ?? strtotime($reportsModel->created_at);
|
|
|
|
$reportsTask->status = $reportsTask->status ?? 1;
|
2021-11-30 16:54:04 +03:00
|
|
|
|
2022-12-16 20:18:25 +03:00
|
|
|
if(!$reportsTask->validate() || !$reportsTask->save()){
|
|
|
|
throw new BadRequestHttpException(json_encode($reportsTask->errors));
|
2022-12-16 18:54:13 +03:00
|
|
|
}
|
|
|
|
}
|
2021-07-28 17:08:08 +03:00
|
|
|
|
2022-12-16 20:18:25 +03:00
|
|
|
return array_merge($reportsModel->toArray());
|
2021-07-28 17:08:08 +03:00
|
|
|
}
|
|
|
|
|
2023-11-03 12:01:29 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @OA\Get(path="/user-response/delete",
|
|
|
|
* summary="Удаление отчёта",
|
|
|
|
* description="Осуществляет удаление отчёта",
|
|
|
|
* security={
|
|
|
|
* {"bearerAuth": {}}
|
|
|
|
* },
|
|
|
|
* tags={"Reports"},
|
|
|
|
* @OA\RequestBody(
|
|
|
|
* @OA\MediaType(
|
|
|
|
* mediaType="application/x-www-form-urlencoded",
|
|
|
|
* @OA\Schema(
|
|
|
|
* required={"id"},
|
|
|
|
* @OA\Property(
|
|
|
|
* property="id",
|
|
|
|
* type="integer",
|
|
|
|
* description="Идентификатор отчётая",
|
|
|
|
* nullable=false,
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
*
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Возвращает true в случае успеха",
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
* @throws \Throwable
|
|
|
|
* @throws \yii\db\StaleObjectException
|
|
|
|
*/
|
2021-07-28 17:08:08 +03:00
|
|
|
public function actionDelete()
|
|
|
|
{
|
|
|
|
$id = Yii::$app->request->get('id');
|
|
|
|
|
|
|
|
$report = Reports::findOne($id);
|
|
|
|
|
|
|
|
if(null === $report) {
|
|
|
|
throw new NotFoundHttpException('Report not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
if(false === ($report->delete())) {
|
2023-01-19 16:29:06 +03:00
|
|
|
throw new \RuntimeException('Report not deleted');
|
2021-07-28 17:08:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-03 12:01:29 +03:00
|
|
|
/**
|
|
|
|
* @OA\Get(path="/reports/update",
|
|
|
|
* summary="Обновление отчёта",
|
|
|
|
* description="Метод для Обновления отчёта",
|
|
|
|
* security={
|
|
|
|
* {"bearerAuth": {}}
|
|
|
|
* },
|
|
|
|
* tags={"Reports"},
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="id",
|
|
|
|
* in="query",
|
|
|
|
* required=true,
|
|
|
|
* description="ID отчёта",
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="integer",
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="created_at",
|
|
|
|
* in="query",
|
|
|
|
* required=false,
|
|
|
|
* description="Дата создания (yyyy-mm-dd)",
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="DateTime",
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="today",
|
|
|
|
* in="query",
|
|
|
|
* required=false,
|
|
|
|
* description="Сделанное сегодня",
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="difficulties",
|
|
|
|
* in="query",
|
|
|
|
* required=false,
|
|
|
|
* description="Описание сложностей возникших при выполнении задач",
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="tomorrow",
|
|
|
|
* in="query",
|
|
|
|
* required=false,
|
|
|
|
* description="Описание планов на завтра",
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="status",
|
|
|
|
* in="query",
|
|
|
|
* required=false,
|
|
|
|
* description="Статус",
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="integer",
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Возвращает объект Запроса",
|
|
|
|
* @OA\MediaType(
|
|
|
|
* mediaType="application/json",
|
|
|
|
* @OA\Schema(ref="#/components/schemas/ReportsResponseCreateExample"),
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @throws BadRequestHttpException
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
*/
|
2021-07-28 17:08:08 +03:00
|
|
|
public function actionUpdate(): array
|
|
|
|
{
|
|
|
|
$params = Yii::$app->request->get();
|
|
|
|
|
|
|
|
$reportsModel = Reports::findone($params['id']);
|
|
|
|
if(!isset($reportsModel)) {
|
|
|
|
throw new NotFoundHttpException('report not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($params['user_card_id'])) {
|
2023-01-19 16:29:06 +03:00
|
|
|
throw new \RuntimeException('constraint by user_card_id');
|
2021-07-28 17:08:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$reportsModel->attributes = $params;
|
|
|
|
|
|
|
|
if(!$reportsModel->validate()){
|
|
|
|
throw new BadRequestHttpException(json_encode($reportsModel->errors));
|
|
|
|
}
|
|
|
|
|
|
|
|
$reportsModel->save();
|
2021-07-05 15:30:35 +03:00
|
|
|
|
2021-07-28 17:08:08 +03:00
|
|
|
return $reportsModel->toArray();
|
2021-07-05 15:30:35 +03:00
|
|
|
}
|
|
|
|
|
2022-12-20 19:24:38 +03:00
|
|
|
/**
|
2023-11-03 12:01:29 +03:00
|
|
|
* @param $fromDate
|
|
|
|
* @param $toDate
|
|
|
|
* @param $user_card_id
|
|
|
|
* @return array|array[]|object|object[]|string[]
|
|
|
|
* @throws BadRequestHttpException
|
2022-12-20 19:24:38 +03:00
|
|
|
*/
|
2023-11-04 18:30:01 +03:00
|
|
|
public function actionReportsByDate($fromDate, $toDate, $user_card_id)
|
2022-12-20 19:24:38 +03:00
|
|
|
{
|
|
|
|
if (!$this->checkDate($fromDate) || !$this->checkDate($toDate)) {
|
|
|
|
throw new BadRequestHttpException('Wrong date format');
|
|
|
|
}
|
|
|
|
|
|
|
|
$params = Yii::$app->request->get();
|
|
|
|
|
|
|
|
$reportsModel = new ReportSearchForm();
|
|
|
|
$reportsModel->attributes = $params;
|
2023-01-19 17:16:43 +03:00
|
|
|
$reportsModel->user_card_id = $user_card_id;
|
2022-12-20 19:24:38 +03:00
|
|
|
|
2023-01-19 16:29:06 +03:00
|
|
|
$reports = $reportsModel->reportsByDate();
|
2022-12-20 19:24:38 +03:00
|
|
|
return ArrayHelper::toArray($reports , [
|
|
|
|
'common\models\Reports' => [
|
|
|
|
'date' => 'created_at',
|
|
|
|
'id',
|
|
|
|
'spendTime' => function (Reports $report) {
|
|
|
|
return $report->calculateOrderTime();
|
|
|
|
},
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function checkDate($date): bool
|
|
|
|
{
|
|
|
|
$checkedDate = DateTime::createFromFormat('Y-m-d', $date);
|
|
|
|
$date_errors = DateTime::getLastErrors();
|
|
|
|
if (!empty($date_errors['warning_count']) || !empty($date_errors['error_count'])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2021-07-05 15:30:35 +03:00
|
|
|
}
|