]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
ERP-66 Добавить открытие смены без плана в воркботе
authorMarina Zozirova <marina.zozirova@erp-flowers.ru>
Fri, 28 Jun 2024 10:53:22 +0000 (10:53 +0000)
committerAleksey Filippov <aleksey.filippov@erp-flowers.ru>
Fri, 28 Jun 2024 10:53:22 +0000 (10:53 +0000)
erp24/actions/timetable/StartShiftStepOneAction.php
erp24/actions/timetable/StartShiftStepTwoAction.php
erp24/api3/core/services/ClientService.php
erp24/api3/core/services/TimetableService.php
erp24/api3/modules/v1/controllers/ClientController.php
erp24/api3/modules/v1/requests/timetable/Fact.php
erp24/records/AdminCheckin.php
erp24/records/TimetableFactModel.php

index 3dbf471ba5152b199ff80987156621e2ad30e485..937d5b00b9297b10bc604e8956ae0dd833a761ac 100755 (executable)
@@ -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;
index 85d51a0467d722a842fe40cfb0dbd8ac0a4fc6f6..6ca09b3b273f8ce18ba4d75bdd01dc42464a34ef 100755 (executable)
@@ -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;
index ee39c7e254ecdb7a159815d0608123e11f293828..61135a75008c5ba4a3908eb3420185880157127c 100644 (file)
@@ -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
index a91eb490c673167a60eec990f28a808614fbfc6b..1c620730996bc96bf70cfc3d0eff88ad991858d5 100644 (file)
@@ -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')) {
index 428c04e0f469470fa2d59d671b3019cd3571d814..95e69660658a006910f0ab719d45a1c8c49a572b 100644 (file)
@@ -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
index 8823e5d727f342ded64f6ffdbbd352d385b0fee5..108cec2839de491a841638ae13800201b45902c5 100644 (file)
@@ -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'],
         ];
     }
 
index 1d7b01785609e5bb7ef2df9dcf688aaee9d52edf..3d128208b915e6033fcf4b3e2b88a06cda45fb92 100755 (executable)
@@ -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'],
index 48d3304245b6b0ef19872ad6bd548321242040e7..2aca4bdacd1b3c89d8a63e3a7c0c86c41db20324 100644 (file)
@@ -1,8 +1,7 @@
 <?php
 
-declare(strict_types=1);
 
-namespace app\records;
+namespace yii_app\records;
 
 use Throwable;
 use yii\db\ActiveRecord;
@@ -101,7 +100,7 @@ class TimetableFactModel extends 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])
@@ -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());
         }
     }