LotsController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. namespace app\controllers;
  3. use app\models\Bills;
  4. use app\models\Images;
  5. use PhpOffice\PhpWord\Shared\ZipArchive;
  6. use Yii;
  7. use app\models\Files;
  8. use app\models\Lots;
  9. use app\models\Messages;
  10. use app\models\LotSearch;
  11. use app\models\Auctions;
  12. use yii\helpers\ArrayHelper;
  13. use yii\web\Controller;
  14. use yii\web\NotFoundHttpException;
  15. use yii\filters\VerbFilter;
  16. use yii\web\Response;
  17. use yii\web\UploadedFile;
  18. use yii\filters\AccessControl;
  19. use app\models\Subscriptions;
  20. use app\models\Eventlog;
  21. /**
  22. * LotsController implements the CRUD actions for Lots model.
  23. */
  24. class LotsController extends Controller
  25. {
  26. public function behaviors()
  27. {
  28. return [
  29. 'verbs' => [
  30. 'class' => VerbFilter::className(),
  31. 'actions' => [
  32. 'delete' => ['post'],
  33. ],
  34. ],
  35. 'access' => [
  36. 'class' => AccessControl::className(),
  37. 'only' => ['index', 'create', 'view', 'update', 'delete', 'upload', 'download', 'confirm', 'auction'],
  38. 'rules' => [
  39. [
  40. 'allow' => true,
  41. 'actions' => ['index', 'create', 'view', 'update', 'delete', 'upload', 'download', 'confirm', 'auction'],
  42. 'roles' => ['@'],
  43. ],
  44. ],
  45. ],
  46. ];
  47. }
  48. /**
  49. * Lists all Lots models.
  50. * @return mixed
  51. */
  52. public function init()
  53. {
  54. if (@!Yii::$app->user->identity->confirmed_at && (@Yii::$app->user->identity->role == 1)) {
  55. return $this->redirect('/registration/organizer');
  56. }
  57. $this->layout = '@app/views/layouts/backend/user';
  58. parent::init();
  59. }
  60. public function actionIndex()
  61. {
  62. if (Yii::$app->user->can('org') || Yii::$app->user->can('admin')) {
  63. $searchModel = new LotSearch();
  64. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  65. return $this->render('index', [
  66. 'searchModel' => $searchModel,
  67. 'dataProvider' => $dataProvider,
  68. ]);
  69. } else {
  70. throw new NotFoundHttpException('The requested page does not exist.');
  71. }
  72. }
  73. /**
  74. * Displays a single Lots model.
  75. * @param integer $id
  76. * @return mixed
  77. */
  78. public function actionView($id)
  79. {
  80. $files = new Files();
  81. $files->uploads = '../uploads/bids/';
  82. if (Yii::$app->request->isPost && false != ($file = UploadedFile::getInstances($files, 'file'))) {
  83. $fileName = $files->uploadFile();
  84. $model = $this->findModel($id);
  85. // $file->name = $files->transliteration($file->name);
  86. //$files->name = $file->name;
  87. // $path = Yii::$app->params['uploadPath'].$file->name;
  88. if ($fileName) {
  89. $files->saveFile([
  90. 'name' => $fileName,
  91. 'path' => $files->uploads,
  92. 'user_id' => Yii::$app->user->identity->id,
  93. 'auction_id' => $model->auction->id,
  94. 'lot_id' => $model->id,
  95. 'type' => 'bid',
  96. ]);
  97. Yii::$app->session->setFlash('success', Yii::t('app', 'FileUploaded ID'));
  98. } else {
  99. Yii::$app->session->setFlash('danger', Yii::t('app', 'Cannot save file'));
  100. }
  101. }
  102. return $this->render('view', [
  103. 'model' => $this->findModel($id),
  104. ]);
  105. }
  106. public function actionCreate()
  107. {
  108. $model = new Lots();
  109. $files = new Files();
  110. $files->uploads = '../uploads/lots/';
  111. $fileName = $files->uploadFile();
  112. // print_r(Yii::$app->request->post());
  113. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  114. if ($fileName) {
  115. $docs_id = $files->saveFile([
  116. 'name' => $fileName,
  117. 'path' => $files->uploads,
  118. 'user_id' => Yii::$app->user->identity->id,
  119. 'lot_id' => $model->id,
  120. 'auction_id' => null,
  121. 'type' => 'lot',
  122. ]);
  123. $model->updateAttributes(['docs_id' => $docs_id]);
  124. $model->save();
  125. $model->upload();
  126. }
  127. $dogovor = UploadedFile::getInstance($files, 'project_dogovor');
  128. if(isset($dogovor))
  129. {
  130. $dogovor->name = $files->transliteration($dogovor->name);
  131. $path = Yii::$app->params['uploadPath'].$dogovor->name;
  132. $dogovor->saveAs($path);
  133. $dogovor_id = $files->saveFile([
  134. 'name'=>$dogovor->name,
  135. 'path'=>Yii::$app->params['uploadPath'],
  136. 'user_id'=>Yii::$app->user->identity->id,
  137. 'auction_id'=>'null',
  138. 'lot_id'=>$model->id,
  139. <<<<<<< HEAD
  140. 'type' => 'project_dogovor'
  141. =======
  142. 'type' => 'NULL'
  143. >>>>>>> 5bd9408fb32cecb076e639952fbeaaed915e69ce
  144. ]);
  145. $model->updateAttributes(['dogovor_id' => $dogovor_id]);
  146. }
  147. $data = json_decode($model->serialize_data, true);
  148. $model->updateAttributes(['serialize_data' => json_encode($data)]);
  149. return $this->redirect(['view', 'id' => $model->id]);
  150. } else {
  151. return $this->render('create', ['model' => $model]);
  152. }
  153. }
  154. public function actionConfirm($id, $status)
  155. {
  156. if (Yii::$app->user->can('admin') || Yii::$app->user->can('org')) {
  157. $model = $this->findModel($id);
  158. Yii::$app->response->format = Response::FORMAT_JSON;
  159. return
  160. $model->updateAttributes(['status' => $status,]);
  161. }else{
  162. return $this->redirect(['view', 'id' => $id]);
  163. }
  164. }
  165. public function actionCircle_down($id)
  166. {
  167. if (Yii::$app->user->can('admin') || Yii::$app->user->can('org')) {
  168. $model = $this->findModel($id);
  169. Yii::$app->db->createCommand("UPDATE lots SET step_down=step_down-1 WHERE id=:id")
  170. ->bindValue(':id', $id)
  171. ->execute();
  172. }
  173. return $this->redirect(['view', 'id' => $id]);
  174. }
  175. public function actionCircle_up($id)
  176. {
  177. if (Yii::$app->user->can('admin') || Yii::$app->user->can('org')) {
  178. $model = $this->findModel($id);
  179. Yii::$app->db->createCommand("UPDATE lots SET step_down=step_down+1 WHERE id=:id")
  180. ->bindValue(':id', $id)
  181. ->execute();
  182. }
  183. return $this->redirect(['view', 'id' => $id]);
  184. }
  185. public function actionUpdate($id)
  186. {
  187. $model = $this->findModel($id);
  188. $files = new Files();
  189. $files->uploads = '../uploads/lots/';
  190. $fileName = $files->uploadFile();
  191. if ($model->status == 2) {
  192. Yii::$app->session->setFlash('danger', Yii::t('app', 'LotEditNoSuccess ID'));
  193. return $this->redirect(['index']);
  194. } elseif ($model->status == 3) {
  195. Yii::$app->session->setFlash('danger', Yii::t('app', 'LotEditNoReject ID'));
  196. return $this->redirect(['index']);
  197. }
  198. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  199. if ($fileName) {
  200. $docs_id = $files->saveFile([
  201. 'name' => $fileName,
  202. 'path' => $files->uploads,
  203. 'user_id' => Yii::$app->user->identity->id,
  204. 'lot_id' => $model->id,
  205. 'auction_id' => null,
  206. 'type' => 'lot',
  207. ]);
  208. $model->updateAttributes(['docs_id' => $docs_id]);
  209. $model->save();
  210. $model->upload();
  211. }
  212. $dogovor = UploadedFile::getInstance($files, 'project_dogovor');
  213. if(isset($dogovor))
  214. {
  215. $dogovor->name = $files->transliteration($dogovor->name);
  216. $path = Yii::$app->params['uploadPath'].$dogovor->name;
  217. $dogovor->saveAs($path);
  218. $dogovor_id = $files->saveFile([
  219. 'name'=>$dogovor->name,
  220. 'path'=>Yii::$app->params['uploadPath'],
  221. 'user_id'=>Yii::$app->user->identity->id,
  222. 'auction_id'=>'null',
  223. 'lot_id'=>$model->id,
  224. <<<<<<< HEAD
  225. 'type' => 'project_dogovor'
  226. =======
  227. 'type' => 'NULL'
  228. >>>>>>> 5bd9408fb32cecb076e639952fbeaaed915e69ce
  229. ]);
  230. $model->updateAttributes(['dogovor_id' => $dogovor_id]);
  231. }
  232. $data = json_decode($model->serialize_data, true);
  233. $model->updateAttributes(['serialize_data' => json_encode($data)]);
  234. return $this->redirect(['view', 'id' => $model->id]);
  235. } else {
  236. return $this->render('update', [
  237. 'model' => $model,
  238. ]);
  239. }
  240. }
  241. public function actionDelete($id)
  242. {
  243. $model = $this->findModel($id);
  244. if ($model->status == 2) {
  245. Yii::$app->session->setFlash('danger', Yii::t('app', 'LotEditNoSuccess ID'));
  246. return $this->redirect(['index']);
  247. } elseif ($model->status == 3) {
  248. Yii::$app->session->setFlash('danger', Yii::t('app', 'LotEditNoReject ID'));
  249. return $this->redirect(['index']);
  250. }
  251. if ($model->status == 1) {
  252. if ($model->auction) {
  253. $model->auction->delete();
  254. }
  255. Yii::createObject(Eventlog::className())->PutLog([
  256. 'user_id' => Yii::$app->user->identity->id,
  257. 'ip' => Yii::$app->request->getRemoteIP(),
  258. 'auk_id' => $model->id,
  259. 'action' => Yii::t('app', 'OrgDeleteAuk ID {action}',
  260. ['action' => $model->aukname]),
  261. ]);
  262. $model->lot_lock = 0;
  263. $this->findModel($id)->delete();
  264. Yii::$app->session->setFlash('danger', Yii::t('app', 'Auction deleted successfully'));
  265. return $this->redirect(['index']);
  266. } else {
  267. return $this->render('/message', [
  268. 'title' => \Yii::t('app', 'Not Permission'),
  269. 'module' => $this->module,
  270. ]);
  271. }
  272. }
  273. public function actionAuction($id, $type)
  274. {
  275. if(Yii::$app->user->can('admin') || Yii::$app->user->can('org'))
  276. {
  277. // print_r($_POST);die();
  278. $model = $this->findModel($id);
  279. if($model->status==2 && $model->lot_lock==0 || $model->status==4 && $model->lot_lock==0)
  280. {
  281. // if(Yii::$app->request->post('date_stop'))
  282. // {
  283. // $date_stop = Yii::$app->request->post('date_stop');
  284. // $time_cont = Yii::$app->request->post('time_cont');
  285. // }
  286. // else
  287. // {
  288. // $date = strtotime($model->auction_date) + 7200;
  289. // $date_stop = date("Y-m-d H:i:s", $date);
  290. // }
  291. //$date = new \DateTime($model->auction_date);
  292. $setData = json_decode($model->serialize_data);
  293. $parts = array_pad(explode(':', $setData->time_step_change), 3, 0);
  294. $timeStepFormat = sprintf(
  295. "%02d:%02d:%02d.00000",
  296. (int)$parts[0],
  297. (int)$parts[1],
  298. (int)$parts[2]
  299. );
  300. // create auction
  301. $insert = [
  302. 'user_id' => $model->user_id,
  303. 'name' => $model->aukname,
  304. 'lot_id' => $model->id,
  305. 'lot_num' => $model->num,
  306. 'date_start' => $model->auction_date,
  307. 'bidding_date' => $model->bidding_date,
  308. 'date_stop' => $setData->date_stop,
  309. 'type_id' => $setData->type,
  310. 'time_step_down' => $timeStepFormat,
  311. 'time_cont'=> $setData->time_cont,
  312. 'step_down'=> $setData->step_change,
  313. 'temp_step_down'=>$setData->step_change,
  314. //'date_stop' => $date->modify('+1 day')->format('Y-m-d H:i:s'),
  315. ];
  316. // echo '<pre>';
  317. // print_r(json_decode($model->serialize_data));
  318. // print_r($insert);
  319. // echo '</pre>';
  320. // die();
  321. $auction = Yii::createObject(Auctions::className())->CreateAuction($insert);
  322. Subscriptions::createNewsletter($auction);
  323. /* Узгоджено організатором */
  324. if($model->status==4)
  325. {
  326. $model->updateAttributes(['lot_lock' => '0']);
  327. }
  328. $model->updateAttributes(['lot_lock' => '1']);
  329. $notes_org = Yii::t('app','AuctionCreatedOnLot ID') .": ". $model->aukname ." / " .
  330. Yii::t('app','LotNumber ID'). $model->num . " " . $model->name;
  331. Yii::createObject(Messages::className())->CreateMessage(['user_id' => $model->user_id, 'notes' => $notes_org]);
  332. Yii::$app->session->setFlash('success', Yii::t('app', 'AuctionCreated ID'));
  333. return $this->redirect(['index']);
  334. } elseif ($model->status == 1 || $model->status == 3) {
  335. Yii::$app->session->setFlash('warning', Yii::t('app', 'NeedConfirmLot ID'));
  336. return $this->redirect(['view', 'id' => $model->id]);
  337. }
  338. else
  339. {
  340. Yii::$app->session->setFlash('warning', Yii::t('app', 'LotInAuction ID'));
  341. return $this->redirect(['view', 'id' => $model->id]);
  342. }
  343. } else {
  344. Yii::$app->session->setFlash('success', Yii::t('app', 'not permission2'));
  345. return $this->redirect(['view', 'id' => $model->id]);
  346. }
  347. }
  348. public function actionStepChange()
  349. {
  350. $id = Yii::$app->request->post('id');
  351. $value = Yii::$app->request->post('value');
  352. $model = Lots::findOne(['id' => $id]);
  353. $model->step_down = $value;
  354. if (Yii::$app->request->post() && $model->save()) {
  355. return $this->redirect(['/lots/view', 'id' => $id]);
  356. }
  357. }
  358. public function actionTimeStepChange()
  359. {
  360. $id = Yii::$app->request->post('id');
  361. $value = Yii::$app->request->post('concat_val');
  362. $model = Lots::findOne(['id' => $id]);
  363. $model->time_step_down = $value;
  364. if (Yii::$app->request->post() && $model->save()) {
  365. return $this->redirect(['/lots/view', 'id' => $id]);
  366. }
  367. }
  368. public function actionClone($id)
  369. {
  370. $model = $this->findModel($id);
  371. // print_r($model->attributes);
  372. // exit;
  373. $attributes = $model->attributes;
  374. $attributes['id'] = null;
  375. $attributes['status'] = 1;
  376. $attributes['lot_lock'] = 0;
  377. $attributes['docs_id'] = null;
  378. $clone = new Lots($attributes);
  379. if ($clone->load(Yii::$app->request->post()) && $clone->save()) {
  380. if ((Yii::$app->request->post('agreeCopyFile')) && $model->file) {
  381. $clone->updateAttributes(['docs_id' => $model->file->copy($clone->id)]);
  382. } else {
  383. $clone->updateAttributes(['docs_id' => null]);
  384. }
  385. if (Yii::$app->request->post('agreeCopyImg')) {
  386. foreach ($model->images as $image) {
  387. $image->copy($clone->id);
  388. }
  389. foreach ($model->thumbnails as $thumbnail) {
  390. $thumbnail->copy($clone->id);
  391. }
  392. }
  393. return $this->redirect(['view', 'id' => $clone->id]);
  394. } else {
  395. return $this->render('clone', [
  396. 'model' => $model,
  397. ]);
  398. }
  399. }
  400. protected function findModel($id)
  401. {
  402. if (($model = Lots::findOne($id)) !== null) {
  403. return $model;
  404. } else {
  405. throw new NotFoundHttpException('The requested page does not exist . ');
  406. }
  407. }
  408. }