From e0366b9d8cfc0692f7c12dc4b09570a6031cce2f Mon Sep 17 00:00:00 2001 From: iIronside Date: Tue, 20 Dec 2022 19:24:38 +0300 Subject: [PATCH] add find reports by date to api --- common/models/Reports.php | 13 ++ docs/api/reports.md | 127 ++++++++++++++++++ .../api/controllers/ReportsController.php | 46 +++++++ .../modules/api/models/ReportSearchForm.php | 9 +- 4 files changed, 194 insertions(+), 1 deletion(-) diff --git a/common/models/Reports.php b/common/models/Reports.php index 76b6f4f..150fb06 100755 --- a/common/models/Reports.php +++ b/common/models/Reports.php @@ -123,4 +123,17 @@ class Reports extends \yii\db\ActiveRecord $user_card = UserCard::findOne(['id' => $data->user_card_id]); return $user_card->fio; } + + /** + * @return \yii\db\ActiveQuery + */ + public function getReportsTask() + { + return $this->hasMany(ReportsTask::className(), ['report_id' => 'id']); + } + + public function calculateOrderTime() + { + return ReportsTask::find()->where(['report_id' => $this->id])->sum('hours_spent'); + } } diff --git a/docs/api/reports.md b/docs/api/reports.md index 8efee42..32feef2 100644 --- a/docs/api/reports.md +++ b/docs/api/reports.md @@ -57,6 +57,14 @@ Изменить отчёт + + + reports-by-date + + + Отчёты по датам + + ### Список @@ -432,3 +440,122 @@

`https://guild.craft-group.xyz/api/reports/update?id=18&created_at=2021-09-17&today=0&difficulties=diff&tomorrow=new task&status=1` + +### Отчёты по датам + +`https://guild.craft-group.xyz/api/reports/reports-by-date` + +

+ Для получения списка дат за которые есть отчёты необходимо отправить GET запрос на URL https://guild.craft-group.xyz/api/reports/reports-by-date +

+ +

+ Параметры: +

+ + + + + + + + + + + + + + + + + + + + + +
+ Параметры + + Значение + + Обязательный +
+ fromDate + + Дата начала периода + + Да +
+ toDate + + Дата конца периода + + Да +
+ user_id + + Id пользователя. По умолчанию будет выведен список для текущего пользователя, + при передаче параметра будет выведен список для заданного пользователя + + Нет +
+

+ Пример запроса: +

+ +`http://guild.loc/api/reports/reports-by-date?fromDate=2022-12-1&toDate=2022-12-31&user_id=1 task&status=1` + +

+ Возвращаемые параметры: id - идентификатор отчёта, date - дата отчёта, +spendTime - время затраченое на все задачи в отчёте +

+ +```json5 +[ + { + "date": "2022-12-16", + "id": 50, + "spendTime": 8 + }, + { + "date": "2022-12-16", + "id": 51, + "spendTime": 8 + }, + { + "date": "2022-12-16", + "id": 52, + "spendTime": 8 + }, + { + "date": "2022-12-01", + "id": 53, + "spendTime": null + }, + { + "date": "2022-12-10", + "id": 54, + "spendTime": null + }, + { + "date": "2022-12-16", + "id": 55, + "spendTime": null + }, + { + "date": "2022-12-31", + "id": 56, + "spendTime": null + }, + { + "date": "2022-12-31", + "id": 57, + "spendTime": null + }, + { + "date": "2022-12-31", + "id": 58, + "spendTime": null + } +] +``` \ No newline at end of file diff --git a/frontend/modules/api/controllers/ReportsController.php b/frontend/modules/api/controllers/ReportsController.php index 7eab802..f7a03df 100755 --- a/frontend/modules/api/controllers/ReportsController.php +++ b/frontend/modules/api/controllers/ReportsController.php @@ -5,12 +5,14 @@ namespace frontend\modules\api\controllers; use common\models\Reports; use common\models\ReportsTask; use common\models\UserCard; +use DateTime; use frontend\modules\api\models\ReportSearchForm; use JsonException; use Yii; use yii\filters\auth\CompositeAuth; use yii\filters\auth\HttpBearerAuth; use yii\filters\ContentNegotiator; +use yii\helpers\ArrayHelper; use yii\web\BadRequestHttpException; use yii\web\NotFoundHttpException; use yii\web\Response; @@ -165,4 +167,48 @@ class ReportsController extends ApiController return $reportsModel->toArray(); } + /** + * @throws NotFoundHttpException + */ + public function actionReportsByDate($fromDate, $toDate, $user_id = null) + { + if (!$this->checkDate($fromDate) || !$this->checkDate($toDate)) { + throw new BadRequestHttpException('Wrong date format'); + } + + $params = Yii::$app->request->get(); + $userId = $user_id ?? Yii::$app->user->id; + /** @var UserCard $userCard */ + $userCard = UserCard::find()->where(['id_user' => $userId])->one(); + + if (!$userCard) { + throw new NotFoundHttpException('User not found'); + } + + $reportsModel = new ReportSearchForm(); + $reportsModel->attributes = $params; + $reportsModel->user_id = $userCard->id; + + $reports = $reportsModel->findByDate(); + 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; + } + } diff --git a/frontend/modules/api/models/ReportSearchForm.php b/frontend/modules/api/models/ReportSearchForm.php index 4d0a783..d70970b 100755 --- a/frontend/modules/api/models/ReportSearchForm.php +++ b/frontend/modules/api/models/ReportSearchForm.php @@ -4,7 +4,7 @@ namespace frontend\modules\api\models; use common\models\Reports; -use frontend\modules\card\models\UserCard; +use common\models\ReportsTask; use yii\base\Model; class ReportSearchForm extends Model @@ -65,4 +65,11 @@ class ReportSearchForm extends Model return $data; } + + public function findByDate(): array + { + return Reports::find() + ->where(['between', 'reports.created_at', $this->fromDate, $this->toDate]) + ->all(); + } } \ No newline at end of file