use yii\db\Exception;
use yii\helpers\Json;
use yii\web\NotFoundHttpException;
+use yii_app\api3\modules\v1\models\Admin;
use yii_app\api3\modules\v1\models\timetable\Timetable;
use yii_app\api3\modules\v1\requests\timetable\Fact;
use yii_app\records\AdminCheckin;
use yii_app\records\AdminDevice;
use yii_app\records\AdminGroup;
+use yii_app\records\TimetableFactModel;
use yii_app\records\TimetableV3;
use yii_app\records\TimetableWorkbot;
public function create($data)
{
/** @var $data Fact */
- $timetable = Timetable::findOne(['id' => $data->plan_id, 'tabel' => 0]);
- if(!$timetable) {
- throw new NotFoundHttpException("План не найден");
- }
+// $timetable = Timetable::findOne(['id' => $data->plan_id, 'tabel' => 0]);
+// if(!$timetable) {
+// throw new NotFoundHttpException("План не найден");
+// }
+//
+// if ($timetable->date != date("Y-m-d") &&
+// $timetable->date != date("Y-m-d", strtotime("-1 day", time()))) {
+// throw new InvalidArgumentException("План ссылается на другую дату");
+// }
+ $currentDate = date('Y-m-d H:i:s'); // вынести в хелпер
+ $admin_id = \Yii::$app->user->id;
- if ($timetable->date != date("Y-m-d") &&
- $timetable->date != date("Y-m-d", strtotime("-1 day", time()))) {
- throw new InvalidArgumentException("План ссылается на другую дату");
+ //убрать после согласования оплаты подработчиков
+ if (Admin::findOne($admin_id)->group_id === AdminGroup::GROUP_WORKERS && !$data->plan_id) {
+ throw new \Exception('Подработчики не могут открыть смены без плана!');
}
- $currentDate = date('Y-m-d H:i:s'); // вынести в хелпер
-
$transaction = \Yii::$app->db->beginTransaction();
try {
- $fact = new Timetable($timetable);
- unset($fact['id']);
- $fact->isNewRecord = true;
- $fact->plan_id = $timetable->id;
- $fact->datetime_start = $currentDate;
- $fact->datetime_end = $currentDate;
- $fact->tabel = 1;
- $fact->save();
- if ($fact->getErrors()) {
- throw new \Exception(Json::encode($fact->getErrors()));
+ if ($data->plan_id) {
+ $timetable = Timetable::findOne($data->plan_id);
+
+ $admin_id = $timetable->admin_id;
+ $fact = new Timetable($timetable);
+ unset($fact['id']);
+ $fact->isNewRecord = true;
+ $fact->plan_id = $timetable->id;
+ $fact->datetime_start = $currentDate;
+ $fact->datetime_end = $currentDate;
+ $fact->tabel = 1;
+ $fact->save();
+ if ($fact->getErrors()) {
+ throw new \Exception(Json::encode($fact->getErrors()));
+ }
}
- $imagePath = $data->uploadImage($timetable->admin_id);
+ $imagePath = $data->uploadImage($admin_id);
+
if (!$imagePath) {
throw new InvalidArgumentException("Не удалось загрузить картинку");
}
$checkIn = new AdminCheckin();
- $checkIn->admin_id = $timetable->admin_id;
- $checkIn->plan_id = $timetable->id;
- $checkIn->store_id = $timetable->store_id;
$checkIn->ball = 5;
$checkIn->comment = "";
- $checkIn->d_id = $timetable->admin->group_id;
- $checkIn->date = $timetable->date;
$checkIn->time = $currentDate;
$checkIn->lat = $data->lat;
$checkIn->lon = $data->lon;
$checkIn->photo = $imagePath;
- $checkIn->device_id = AdminDevice::findOne(['admin_id' => $timetable->admin_id])->id ?? 0;
$checkIn->type_id = $checkIn->d_id == AdminGroup::GROUP_ADMINISTRATORS ? AdminCheckin::TYPE_APPEAR : AdminCheckin::TYPE_START;
$checkIn->status = 0;
- $checkIn->save();
+
+ if ($data->plan_id) {
+ $checkIn->admin_id = $timetable->admin_id;
+ $checkIn->plan_id = $timetable->id;
+ $checkIn->store_id = $timetable->store_id;
+ $checkIn->d_id = $timetable->admin->group_id;
+ $checkIn->date = $timetable->date;
+ $checkIn->device_id = AdminDevice::findOne(['admin_id' => $timetable->admin_id])->id ?? 0;
+ $checkIn->shift_id = $timetable->shift_id;
+ } else {
+ $checkIn->admin_id = $admin_id;
+ $checkIn->plan_id = null;
+ $checkIn->store_id = $data->store_id;
+ $checkIn->d_id = Admin::findOne($admin_id)->group_id;
+ $checkIn->date = date('Y-m-d');
+ $checkIn->device_id = AdminDevice::findOne(['admin_id' => $admin_id])->id ?? 0;
+ $checkIn->shift_id = $data->shift_id;
+ }
+
+ if ($checkIn->save()) {
+ TimetableFactModel::setValues($checkIn, true);
+ }
+
if ($checkIn->getErrors()) {
throw new \Exception(Json::encode($checkIn->getErrors()));
}
throw $e;
}
- return $timetable;
+ return true;
}
public function calculateWorkTime($shift, $timeStart, $timeEnd)
* @throws \yii\base\Exception
* @throws Exception
*/
- public function close($data) {
+ public function close($data)
+ {
/** @var $data Fact */
- $timetable = Timetable::findOne(['plan_id' => $data->plan_id, 'tabel' => 1]);
- if(!$timetable) {
- throw new NotFoundHttpException("Факт не найден");
+
+ if ($data->plan_id) {
+ $timetable = Timetable::findOne(['plan_id' => $data->plan_id, 'tabel' => 1]);
+ if (!$timetable) {
+ throw new NotFoundHttpException("Факт не найден");
+ }
}
$currentDate = date('Y-m-d H:i:s');
+ $admin_id = $data->plan_id ? $timetable->admin_id : \Yii::$app->user->id;
- $checkInFirst = AdminCheckin::find()->where(['plan_id' => $timetable->plan_id])->orderBy(['time' => SORT_ASC])->one();
+ //убрать после согласования оплаты подработчиков
+ if (Admin::findOne($admin_id)->group_id === AdminGroup::GROUP_WORKERS && !$data->plan_id) {
+ throw new \InvalidArgumentException('Подработчики не могут открыть смены без плана!');
+ }
+
+ $checkInFirst = $data->plan_id ?
+ AdminCheckin::find()->where(['plan_id' => $timetable->plan_id])->orderBy(['time' => SORT_ASC])->one() :
+ AdminCheckin::find()->where(['admin_id' => $admin_id])->orderBy(['id' => SORT_DESC])->one();
if ($checkInFirst && (strtotime($checkInFirst->time) + 60 * 60 > strtotime($currentDate))) {
throw new InvalidArgumentException("Между началом и концом смены должно пройти минимум один час");
}
- $timetable->datetime_end = $currentDate;
- $timetable->save();
- if ($timetable->getErrors()) {
- throw new \Exception(Json::encode($timetable->getErrors()));
+ if ($data->plan_id) {
+ $timetable->datetime_end = $currentDate;
+ $timetable->save();
+ if ($timetable->getErrors()) {
+ throw new \Exception(Json::encode($timetable->getErrors()));
+ }
}
- $imagePath = $data->uploadImage($timetable->admin_id);
+ $imagePath = $data->uploadImage($admin_id);
if (!$imagePath) {
throw new InvalidArgumentException("Не удалось загрузить картинку");
}
$checkIn = new AdminCheckin();
- $checkIn->admin_id = $timetable->admin_id;
- $checkIn->plan_id = $timetable->plan_id;
- $checkIn->store_id = $timetable->store_id;
+
+ if ($data->plan_id) {
+ $checkIn->plan_id = $timetable->plan_id;
+ $checkIn->store_id = $timetable->store_id;
+ $checkIn->d_id = $timetable->admin->group_id;
+ $checkIn->date = $timetable->date;
+ } else {
+
+ //остается буквально на день чтобы не посыпались ошибки из-за открытых смен до выката функционала
+ $plan = Timetable::find()
+ ->andWhere(['admin_id' => $admin_id])
+ ->andWhere(['between', 'date', date('Y-m-d', strtotime('-1 day')), date('Y-m-d')])
+ ->andWhere(['tabel' => 1])
+ ->orderBy('id desc')
+ ->one();
+ $checkIn->shift_id = $plan->shift_id;
+
+ $checkIn->plan_id = null;
+ $checkIn->store_id = $checkInFirst->store_id;
+ $checkIn->d_id = $checkInFirst->d_id;
+ $checkIn->date = $checkInFirst->date;
+ }
+
$checkIn->ball = 5;
$checkIn->comment = "";
- $checkIn->d_id = $timetable->admin->group_id;
- $checkIn->date = $timetable->date;
+ $checkIn->admin_id = $admin_id;
$checkIn->time = $currentDate;
$checkIn->lat = $data->lat;
$checkIn->lon = $data->lon;
$checkIn->photo = $imagePath;
$checkIn->status = 0;
- $checkIn->device_id = AdminDevice::findOne(['admin_id' => $timetable->admin_id])->id ?? 0;
+ $checkIn->device_id = AdminDevice::findOne(['admin_id' => $admin_id])->id ?? 0;
$checkIn->type_id = $checkIn->d_id == AdminGroup::GROUP_ADMINISTRATORS ? AdminCheckin::TYPE_APPEAR : AdminCheckin::TYPE_END;
- $checkIn->save();
+
+ if ($checkIn->save()) {
+ TimetableFactModel::setValues($checkIn, false);
+ }
+
if ($checkIn->getErrors()) {
throw new \Exception(Json::encode($checkIn->getErrors()));
}
return true;
}
+
/**
* @throws \yii\base\Exception
* @throws Exception
*/
- public function appear($data) {
+ public function appear($data)
+ {
/** @var $data Fact */
$timetable = Timetable::findOne(['id' => $data->plan_id, 'tabel' => 0]);
- if(!$timetable) {
+ if (!$timetable) {
throw new NotFoundHttpException("План не найден");
}
return $timetable;
}
- public function delete($plan_id, $comment, $removed_by) {
+ public function delete($plan_id, $comment, $removed_by)
+ {
$timetable = Timetable::find()->where(['id' => $plan_id])->one();
/* @var $timetable Timetable */
if ($timetable && $timetable->datetime_start > date('Y-m-d H:i:s')) {
<?php
-declare(strict_types=1);
-namespace app\records;
+namespace yii_app\records;
use Throwable;
use yii\db\ActiveRecord;
];
}
- public static function setValues($adminCheckin)
+ public static function setValues($adminCheckin, $is_start)
{
if ($model = self::find()
->where(['admin_id' => $adminCheckin->admin_id, 'is_opening' => true])
->andWhere(['status' => AdminCheckin::TYPE_START])
->one()) {
+ $model->is_opening = false;
+ $model->is_close = true;
$model->date_end = date("Y-m-d", strtotime($adminCheckin->time));
$model->time_end = date("H:i:s", strtotime($adminCheckin->time));
- $model->is_close = true;
- $model->is_opening = false;
$model->work_time = min(abs(strtotime($model->date_end . $model->time_end) + 600 - strtotime($model->date_start . $model->time_start)) / 3600, self::WORK_HOURS_TIME);
$model->status = AdminCheckin::TYPE_END;
$model->checkin_end_id = $adminCheckin->id;
-
} else {
$model = new TimetableFactModel();
+ $model->is_opening = true;
+ $model->is_close = false;
$model->date_end = null;
$model->date_start = date("Y-m-d", strtotime($adminCheckin->time));
$model->time_start = date("H:i:s", strtotime($adminCheckin->time));
- $model->is_opening = true;
$model->status = AdminCheckin::TYPE_START;
$model->checkin_start_id = $adminCheckin->id;
- $timetable = Timetable::findOne(['date' => $adminCheckin->date]);
-
+ $timetable = Timetable::findOne(['plan_id' => $adminCheckin->plan_id]);
if (!empty($timetable)) {
$model->tabel = $timetable->id;
$model->plan_id = $timetable->plan_id;
+ $model->shift_id = $timetable->shift_id;
$model->admin_id_add = $timetable->admin_id_add;
$model->comment = $timetable->comment;
$model->date_add = $timetable->date_add;
-
} else {
$model->tabel = 1;
$model->plan_id = null;
$model->admin_id_add = null;
$model->comment = null;
$model->date_add = null;
+ $model->shift_id = $adminCheckin->shift_id;
}
$model->admin_id = $adminCheckin->admin_id;
$model->d_id = $adminCheckin->d_id;
$model->store_id = $adminCheckin->store_id;
$model->date_shift = $adminCheckin->date;
- $model->shift_id = $adminCheckin->shift_id;
$model->salary_shift = !empty($salary = EmployeePayment::findOne(['admin_id' => $adminCheckin->admin_id])) ? $salary->daily_payment : null;
- $model->price_hour = $model->salary_shift / ($model->d_id == AdminGroup::GROUP_ADMINISTRATORS ? 8 : 12);
+ $model->price_hour = round($model->salary_shift / ($model->d_id == AdminGroup::GROUP_ADMINISTRATORS ? 8 : 12), 2);
}
+ $model->is_opening = $is_start;
+
if ($model->validate()) {
$model->save();
- } else {
- var_dump($model->getErrors());
- die();
+ }
+
+ if ($model->getErrors()) {
+ throw new \Exception($model->getErrors());
}
}