give_tmc.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  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\UserDevice;
  7. use yii\helpers\Url;
  8. $userId = (int) Yii::$app->request->post('user');
  9. $inventoryNum = Yii::$app->request->post('inventoryNum');
  10. $tmc = Tmc::findOne(['inventory_num' => $inventoryNum]);
  11. if ($userId && $tmc) {
  12. $userDevice = UserDevice::findOne(['employee' => $userId]) ?: new UserDevice();
  13. $fieldName = $userDevice->getFieldNameById($tmc->tmc_type);
  14. if (!$userDevice->$fieldName) {
  15. //Пишем в логи факт выдачи утстройства
  16. $log = new Log();
  17. $log->employee = $userId;
  18. $log->tmc = $tmc->id;
  19. $log->issue_date = (new DateTime())->format('y-m-d H:i:s');
  20. $result = $log->save();
  21. $userDevice->$fieldName = $log->id;
  22. $userDevice->employee = $userId;
  23. $userDevice->save();
  24. $tmc->status = TmcStatus::TMC_STATUS_ISSUED;
  25. $tmc->employee = $userId;
  26. $tmc->save();
  27. }
  28. }
  29. Yii::$app->response->redirect(Url::to($_SERVER['HTTP_REFERER']))->send();