add find reports by date to api
This commit is contained in:
parent
3c922a6627
commit
e0366b9d8c
@ -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');
|
||||
}
|
||||
}
|
||||
|
@ -57,6 +57,14 @@
|
||||
Изменить отчёт
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
reports-by-date
|
||||
</td>
|
||||
<td>
|
||||
Отчёты по датам
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
### Список
|
||||
@ -432,3 +440,122 @@
|
||||
</p>
|
||||
|
||||
`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`
|
||||
|
||||
<p>
|
||||
Для получения списка дат за которые есть отчёты необходимо отправить <b>GET</b> запрос на URL https://guild.craft-group.xyz/api/reports/reports-by-date
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Параметры:
|
||||
</p>
|
||||
<table>
|
||||
<tr>
|
||||
<th>
|
||||
Параметры
|
||||
</th>
|
||||
<th>
|
||||
Значение
|
||||
</th>
|
||||
<th>
|
||||
Обязательный
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
fromDate
|
||||
</td>
|
||||
<td>
|
||||
Дата начала периода
|
||||
</td>
|
||||
<th>
|
||||
Да
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
toDate
|
||||
</td>
|
||||
<td>
|
||||
Дата конца периода
|
||||
</td>
|
||||
<th>
|
||||
Да
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
user_id
|
||||
</td>
|
||||
<td>
|
||||
Id пользователя. По умолчанию будет выведен список для текущего пользователя,
|
||||
при передаче параметра будет выведен список для заданного пользователя
|
||||
</td>
|
||||
<th>
|
||||
Нет
|
||||
</th>
|
||||
</tr>
|
||||
</table>
|
||||
<p>
|
||||
Пример запроса:
|
||||
</p>
|
||||
|
||||
`http://guild.loc/api/reports/reports-by-date?fromDate=2022-12-1&toDate=2022-12-31&user_id=1 task&status=1`
|
||||
|
||||
<p>
|
||||
Возвращаемые параметры: id - идентификатор отчёта, date - дата отчёта,
|
||||
spendTime - время затраченое на все задачи в отчёте
|
||||
</p>
|
||||
|
||||
```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
|
||||
}
|
||||
]
|
||||
```
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user