fix actionFindByDate in ReportsController

This commit is contained in:
iIronside 2023-01-19 14:30:49 +03:00
parent bbc11fc559
commit 097ecfe087
3 changed files with 26 additions and 18 deletions

View File

@ -119,7 +119,7 @@
</tr> </tr>
<tr> <tr>
<td> <td>
user_id user_card_id
</td> </td>
<td> <td>
Идентификатор карточки пользователя отчета. Идентификатор карточки пользователя отчета.
@ -130,7 +130,7 @@
Пример запроса: Пример запроса:
</p> </p>
`https://guild.craft-group.xyz/api/reports/index?fromDate=2021-08-01&toDate=2021-08-31&user_id=2&limit=3&offset=2` `https://guild.craft-group.xyz/api/reports/index?fromDate=2021-08-01&toDate=2021-08-31&user_card_id=2&limit=3&offset=2`
### Один отчет ### Один отчет
`https://guild.craft-group.xyz/api/reports/{id}` `https://guild.craft-group.xyz/api/reports/{id}`
@ -488,7 +488,7 @@
</tr> </tr>
<tr> <tr>
<td> <td>
user_id user_card_id
</td> </td>
<td> <td>
Id пользователя. По умолчанию будет выведен список для текущего пользователя, Id пользователя. По умолчанию будет выведен список для текущего пользователя,
@ -503,7 +503,7 @@
Пример запроса: Пример запроса:
</p> </p>
`http://guild.loc/api/reports/reports-by-date?fromDate=2022-12-1&toDate=2022-12-31&user_id=1 task&status=1` `http://guild.loc/api/reports/reports-by-date?fromDate=2022-12-1&toDate=2022-12-31&user_card_id=1 task&status=1`
<p> <p>
Возвращаемые параметры: id - идентификатор отчёта, date - дата отчёта, Возвращаемые параметры: id - идентификатор отчёта, date - дата отчёта,

View File

@ -72,7 +72,7 @@ class ReportsController extends ApiController
/** /**
* @throws NotFoundHttpException * @throws NotFoundHttpException
*/ */
public function actionFindByDate(): array public function actionFindByDate()//: array
{ {
$reportsModel = new ReportSearchForm(); $reportsModel = new ReportSearchForm();
@ -82,11 +82,19 @@ class ReportsController extends ApiController
} }
$reportsModel->attributes = $params; $reportsModel->attributes = $params;
$reportsModel->byDate = true; $reportsModel->date = $params['date'];
if (!$this->checkDate($reportsModel->date)) {
throw new BadRequestHttpException('Wrong date format');
}
// return $reportsModel->date;
// return $reportsModel->user_card_id;
if(!$reportsModel->validate()){ if(!$reportsModel->validate()){
return $reportsModel->errors; return $reportsModel->errors;
} }
return $reportsModel->findByDate(); return $reportsModel->findByDate();
} }
@ -170,14 +178,14 @@ class ReportsController extends ApiController
/** /**
* @throws NotFoundHttpException * @throws NotFoundHttpException
*/ */
public function actionReportsByDate($fromDate, $toDate, $user_id = null) public function actionReportsByDate($fromDate, $toDate, $user_card_id = null)
{ {
if (!$this->checkDate($fromDate) || !$this->checkDate($toDate)) { if (!$this->checkDate($fromDate) || !$this->checkDate($toDate)) {
throw new BadRequestHttpException('Wrong date format'); throw new BadRequestHttpException('Wrong date format');
} }
$params = Yii::$app->request->get(); $params = Yii::$app->request->get();
$userId = $user_id ?? Yii::$app->user->id; $userId = $user_card_id ?? Yii::$app->user->id;
/** @var UserCard $userCard */ /** @var UserCard $userCard */
$userCard = UserCard::find()->where(['id_user' => $userId])->one(); $userCard = UserCard::find()->where(['id_user' => $userId])->one();
@ -187,7 +195,7 @@ class ReportsController extends ApiController
$reportsModel = new ReportSearchForm(); $reportsModel = new ReportSearchForm();
$reportsModel->attributes = $params; $reportsModel->attributes = $params;
$reportsModel->user_id = $userCard->id; $reportsModel->user_card_id = $userCard->id;
$reports = $reportsModel->findByDate(); $reports = $reportsModel->findByDate();
return ArrayHelper::toArray($reports , [ return ArrayHelper::toArray($reports , [

View File

@ -13,7 +13,7 @@ class ReportSearchForm extends Model
public $offset; public $offset;
public $fromDate; public $fromDate;
public $toDate; public $toDate;
public $user_id; public $user_card_id;
/** /**
* @var false * @var false
*/ */
@ -24,10 +24,10 @@ class ReportSearchForm extends Model
{ {
$this->limit = 10; $this->limit = 10;
$this->offset = 0; $this->offset = 0;
$this->user_id = null; $this->user_card_id = null;
$this->toDate = date('Y-m-d', time()); $this->toDate = date('Y-m-d', time());
$this->fromDate = date('Y-m-01', time()); $this->fromDate = date('Y-m-d', time());
$this->date = date('Y-m-d'); $this->date = date('Y-m-d');
$this->byDate = false; $this->byDate = false;
@ -38,8 +38,8 @@ class ReportSearchForm extends Model
{ {
return [ return [
[['byDate'], 'safe'], [['byDate'], 'safe'],
[['fromDate', 'toDate', 'date'], 'date', 'format' => 'php:Y-m-d'], // [['fromDate', 'toDate', 'date'], 'date', 'format' => 'Y-m-d'],
[['limit', 'offset', 'user_id'], 'integer', 'min' => 0], [['limit', 'offset', 'user_card_id'], 'integer', 'min' => 0],
]; ];
} }
@ -57,8 +57,8 @@ class ReportSearchForm extends Model
$queryBuilder->limit($this->limit) $queryBuilder->limit($this->limit)
->offset($this->offset); ->offset($this->offset);
if (isset($this->user_id)) { if (isset($this->user_card_id)) {
$queryBuilder->andWhere(['user_card_id' => $this->user_id]); $queryBuilder->andWhere(['user_card_id' => $this->user_card_id]);
} }
$data = $queryBuilder->asArray()->all(); $data = $queryBuilder->asArray()->all();
@ -69,8 +69,8 @@ class ReportSearchForm extends Model
public function findByDate(): array public function findByDate(): array
{ {
return Reports::find() return Reports::find()
->where(['between', 'reports.created_at', $this->fromDate, $this->toDate]) ->where(['user_card_id' => $this->user_card_id])
->andWhere(['user_card_id' => $this->user_id]) ->andWhere(['created_at' => $this->date])
->all(); ->all();
} }
} }