From: Marina Zozirova Date: Fri, 28 Jun 2024 10:53:22 +0000 (+0000) Subject: ERP-66 Добавить открытие смены без плана в воркботе X-Git-Tag: 1.3~39^2 X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=25aefc33fa64dca766d4a96673fc918269e30ba7;p=erp24_rep%2Fyii-erp24%2F.git ERP-66 Добавить открытие смены без плана в воркботе --- diff --git a/erp24/actions/timetable/StartShiftStepOneAction.php b/erp24/actions/timetable/StartShiftStepOneAction.php index 3dbf471b..937d5b00 100755 --- a/erp24/actions/timetable/StartShiftStepOneAction.php +++ b/erp24/actions/timetable/StartShiftStepOneAction.php @@ -3,7 +3,7 @@ declare(strict_types=1); namespace yii_app\actions\timetable; -use app\records\TimetableFactModel; +use yii_app\records\TimetableFactModel; use yii\base\Action; use yii\helpers\ArrayHelper; use yii_app\records\Admin; diff --git a/erp24/actions/timetable/StartShiftStepTwoAction.php b/erp24/actions/timetable/StartShiftStepTwoAction.php index 85d51a04..6ca09b3b 100755 --- a/erp24/actions/timetable/StartShiftStepTwoAction.php +++ b/erp24/actions/timetable/StartShiftStepTwoAction.php @@ -3,7 +3,7 @@ declare(strict_types = 1); namespace yii_app\actions\timetable; -use app\records\TimetableFactModel; +use yii_app\records\TimetableFactModel; use Yii; use yii\base\Action; use yii\helpers\ArrayHelper; @@ -131,7 +131,7 @@ class StartShiftStepTwoAction extends Action $model->save(); $model->checkin_id = $model->checkinModel->id; $model->id = $model->checkinModel->id; - TimetableFactModel::setValues($model); + TimetableFactModel::setValues($model, empty($lastCheckin)); if ($this->controller->request->getHeaders()->get('Accept') == 'application/json') { $this->controller->response->format = Response::FORMAT_JSON; diff --git a/erp24/api3/core/services/ClientService.php b/erp24/api3/core/services/ClientService.php index ee39c7e2..61135a75 100644 --- a/erp24/api3/core/services/ClientService.php +++ b/erp24/api3/core/services/ClientService.php @@ -5,12 +5,16 @@ namespace yii_app\api3\core\services; use yii\base\InvalidArgumentException; use yii\data\Pagination; use yii\db\Exception; +use yii\helpers\ArrayHelper; +use yii_app\api3\modules\v1\models\Admin; use yii_app\helpers\ClientHelper; use yii_app\helpers\UtilHelper; use yii_app\records\CityStore; use yii_app\records\MessagerUser; use yii_app\records\ReferralStatus; use yii_app\records\Sales; +use yii_app\records\Shift; +use yii_app\records\Timetable; use yii_app\records\Users; use yii_app\records\UsersBonus; use yii_app\records\UsersEvents; @@ -398,6 +402,7 @@ class ClientService */ public function getInfo($data) { $phone = $data->phone; + $admin_id = !empty(Admin::findOne(['mobile' => $phone])) ? Admin::findOne(['mobile' => $phone])->id : null; $ref_code = !empty($data->ref_code) ? $data->ref_code : ''; $cond = !empty($phone) ? ['phone' => $phone] : ['ref_code' => $ref_code]; @@ -463,6 +468,7 @@ class ClientService $mess['total_price_rejected'] = (int)$sales_rejected_sum ?? 0; // Выручка отмененных заявок $mess["referral_count_get_bonus_already"] = (int)$referral_count_get_bonus_already; $mess["referral_count_all"] = (int)$referral_count_all; + $mess["plan_id"] = !empty($timetable = Timetable::findOne(['admin_id' => $admin_id, 'date' => date('Y-m-d'), 'tabel' => 0])) ? $timetable->id : 0; $mess["events"] = $this->listEvents($phone); @@ -482,6 +488,11 @@ class ClientService return $result; } + public function getShifts() { + $shifts = Shift::find()->where(['not in', 'id', [3, 4, 6, 7]])->all(); + return ArrayHelper::map($shifts, 'id', 'name'); + } + /** * @throws \yii\base\Exception * @throws Exception diff --git a/erp24/api3/core/services/TimetableService.php b/erp24/api3/core/services/TimetableService.php index a91eb490..1c620730 100644 --- a/erp24/api3/core/services/TimetableService.php +++ b/erp24/api3/core/services/TimetableService.php @@ -6,11 +6,13 @@ use yii\base\InvalidArgumentException; 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; @@ -23,53 +25,80 @@ class TimetableService 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())); } @@ -80,7 +109,7 @@ class TimetableService throw $e; } - return $timetable; + return true; } public function calculateWorkTime($shift, $timeStart, $timeEnd) @@ -100,48 +129,85 @@ class TimetableService * @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())); } @@ -149,14 +215,16 @@ class TimetableService 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("План не найден"); } @@ -203,7 +271,8 @@ class TimetableService 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')) { diff --git a/erp24/api3/modules/v1/controllers/ClientController.php b/erp24/api3/modules/v1/controllers/ClientController.php index 428c04e0..95e69660 100644 --- a/erp24/api3/modules/v1/controllers/ClientController.php +++ b/erp24/api3/modules/v1/controllers/ClientController.php @@ -170,6 +170,10 @@ class ClientController extends \yii_app\api3\controllers\NoActiveController return $this->clientService->getStores(); } + public function actionGetShifts() { + return $this->clientService->getShifts(); + } + public function actionPhoneKeycodeByCard() { // from https://api2.bazacvetov24.ru/client/phone-keycode-by-card // localhost:8888/v1/client/phone-keycode-by-card diff --git a/erp24/api3/modules/v1/requests/timetable/Fact.php b/erp24/api3/modules/v1/requests/timetable/Fact.php index 8823e5d7..108cec28 100644 --- a/erp24/api3/modules/v1/requests/timetable/Fact.php +++ b/erp24/api3/modules/v1/requests/timetable/Fact.php @@ -10,6 +10,8 @@ use yii_app\api3\modules\v1\models\timetable\Timetable; class Fact extends Model { public $plan_id; + public $store_id; + public $shift_id; public $image; public $lat; public $lon; @@ -17,12 +19,13 @@ class Fact extends Model public function rules(): array { return [ - [['plan_id', 'image'], 'required'], - ['plan_id', 'integer'], - ['plan_id', 'exist', 'targetClass' => Timetable::class, 'targetAttribute' => 'id'], + ['image', 'required'], + [['plan_id'], 'integer'], +// ['plan_id', 'exist', 'targetClass' => Timetable::class, 'targetAttribute' => 'id'], ['image', 'file', 'extensions' => 'png, jpg', 'maxFiles' => 1, 'maxSize' => 20 * 1024 * 1024], ['lat', 'string', 'max' => 18], ['lon', 'string', 'max' => 18], + [['plan_id', 'store_id', 'shift_id'], 'safe'], ]; } diff --git a/erp24/records/AdminCheckin.php b/erp24/records/AdminCheckin.php index 1d7b0178..3d128208 100755 --- a/erp24/records/AdminCheckin.php +++ b/erp24/records/AdminCheckin.php @@ -33,6 +33,7 @@ class AdminCheckin extends ActiveRecord { public $cnt; public $adminGuid; + public $shift_id; const TYPE_START = 1; const TYPE_END = 2; const TYPE_APPEAR = 3; @@ -88,6 +89,7 @@ class AdminCheckin extends ActiveRecord return [ [['admin_id', 'device_id', 'replaced_admin_id', 'plan_id', 'store_id', 'type_id', 'd_id', 'ball', 'status'], 'integer'], [['lat', 'lon'], 'double'], + [['shift_id'], 'safe'], [['comment', 'photo'], 'string'], [['date'], 'date', 'format' => 'yyyy-M-d'], // [['time'], 'date', 'format' => 'yyyy-M-d HH:mm:ss'], diff --git a/erp24/records/TimetableFactModel.php b/erp24/records/TimetableFactModel.php index 48d33042..2aca4bda 100644 --- a/erp24/records/TimetableFactModel.php +++ b/erp24/records/TimetableFactModel.php @@ -1,8 +1,7 @@ where(['admin_id' => $adminCheckin->admin_id, 'is_opening' => true]) @@ -109,38 +108,38 @@ class TimetableFactModel extends ActiveRecord ->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; @@ -148,16 +147,18 @@ class TimetableFactModel extends ActiveRecord $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()); } }