take_tmc.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php /** Created by Anton on 28.01.2020. */
  2. require '../yiiInit.php';
  3. use app\models\entity\Log;
  4. use app\models\entity\Tmc;
  5. use app\models\entity\TmcStatus;
  6. use app\models\entity\TmcType;
  7. use app\models\entity\UserDevice;
  8. use yii\helpers\Url;
  9. $userId = (int) $_POST['userId'];
  10. $deviceId = (int) $_POST['deviceId'];
  11. $log = new Log();
  12. $log->tmc = $deviceId;
  13. $log->employee = $userId;
  14. $log->receipt_date = (new DateTime())->format('Y-m-d H:i:s');
  15. $log->save();
  16. $tmc = Tmc::findOne($deviceId);
  17. $tmc->status = TmcStatus::TMC_STATUS_IN_STORAGE;
  18. $tmc->employee = 0;
  19. $tmc->save();
  20. $userDevice = UserDevice::findOne(['employee' => $userId]);
  21. switch ($tmc->tmc_type) {
  22. case TmcType::TMC_TYPE_CAMERA:
  23. $userDevice->camera = 0;
  24. break;
  25. case TmcType::TMC_TYPE_OIL_METER:
  26. $userDevice->oil_meter = 0;
  27. break;
  28. case TmcType::TMC_TYPE_PHONE:
  29. $userDevice->phone = 0;
  30. break;
  31. }
  32. if ($userDevice->camera || $userDevice->oil_meter || $userDevice->phone) {
  33. $userDevice->save();
  34. } else {
  35. $userDevice->delete();
  36. }
  37. Yii::$app->response->redirect(Url::to($_SERVER['HTTP_REFERER']))->send();