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-01-19 16:29:06 +03:00
|
|
|
public function actionIndex($user_card_id)
|
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
|
|
|
/**
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
*/
|
2023-01-19 14:30:49 +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
|
|
|
}
|
|
|
|
|
2021-07-28 17:08:08 +03:00
|
|
|
public function actionCreate()
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
*/
|
2023-01-19 14:30:49 +03:00
|
|
|
public function actionReportsByDate($fromDate, $toDate, $user_card_id = null)
|
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();
|
2023-01-19 14:30:49 +03:00
|
|
|
$userId = $user_card_id ?? Yii::$app->user->id;
|
2022-12-20 19:24:38 +03:00
|
|
|
/** @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;
|
2023-01-19 14:30:49 +03:00
|
|
|
$reportsModel->user_card_id = $userCard->id;
|
2022-12-20 19:24:38 +03:00
|
|
|
|
2023-01-19 16:29:06 +03:00
|
|
|
// return $reportsModel;
|
|
|
|
|
|
|
|
$reports = $reportsModel->reportsByDate();
|
2023-01-19 16:40:01 +03:00
|
|
|
return $reports;
|
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
|
|
|
}
|