LotsController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. 'type' => 'NULL'
  140. ]);
  141. $model->updateAttributes(['dogovor_id' => $dogovor_id]);
  142. }
  143. $data = json_decode($model->serialize_data, true);
  144. $model->updateAttributes(['serialize_data' => json_encode($data)]);
  145. return $this->redirect(['view', 'id' => $model->id]);
  146. } else {
  147. return $this->render('create', ['model' => $model]);
  148. }
  149. }
  150. public function actionConfirm($id, $status)
  151. {
  152. if (Yii::$app->user->can('admin') || Yii::$app->user->can('org')) {
  153. $model = $this->findModel($id);
  154. Yii::$app->response->format = Response::FORMAT_JSON;
  155. return
  156. $model->updateAttributes(['status' => $status,]);
  157. }else{
  158. return $this->redirect(['view', 'id' => $id]);
  159. }
  160. }
  161. public function actionCircle_down($id)
  162. {
  163. if (Yii::$app->user->can('admin') || Yii::$app->user->can('org')) {
  164. $model = $this->findModel($id);
  165. Yii::$app->db->createCommand("UPDATE lots SET step_down=step_down-1 WHERE id=:id")
  166. ->bindValue(':id', $id)
  167. ->execute();
  168. }
  169. return $this->redirect(['view', 'id' => $id]);
  170. }
  171. public function actionCircle_up($id)
  172. {
  173. if (Yii::$app->user->can('admin') || Yii::$app->user->can('org')) {
  174. $model = $this->findModel($id);
  175. Yii::$app->db->createCommand("UPDATE lots SET step_down=step_down+1 WHERE id=:id")
  176. ->bindValue(':id', $id)
  177. ->execute();
  178. }
  179. return $this->redirect(['view', 'id' => $id]);
  180. }
  181. public function actionUpdate($id)
  182. {
  183. $model = $this->findModel($id);
  184. $files = new Files();
  185. $files->uploads = '../uploads/lots/';
  186. $fileName = $files->uploadFile();
  187. if ($model->status == 2) {
  188. Yii::$app->session->setFlash('danger', Yii::t('app', 'LotEditNoSuccess ID'));
  189. return $this->redirect(['index']);
  190. } elseif ($model->status == 3) {
  191. Yii::$app->session->setFlash('danger', Yii::t('app', 'LotEditNoReject ID'));
  192. return $this->redirect(['index']);
  193. }
  194. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  195. if ($fileName) {
  196. $docs_id = $files->saveFile([
  197. 'name' => $fileName,
  198. 'path' => $files->uploads,
  199. 'user_id' => Yii::$app->user->identity->id,
  200. 'lot_id' => $model->id,
  201. 'auction_id' => null,
  202. 'type' => 'lot',
  203. ]);
  204. $model->updateAttributes(['docs_id' => $docs_id]);
  205. $model->save();
  206. $model->upload();
  207. }
  208. $dogovor = UploadedFile::getInstance($files, 'project_dogovor');
  209. if(isset($dogovor))
  210. {
  211. $dogovor->name = $files->transliteration($dogovor->name);
  212. $path = Yii::$app->params['uploadPath'].$dogovor->name;
  213. $dogovor->saveAs($path);
  214. $dogovor_id = $files->saveFile([
  215. 'name'=>$dogovor->name,
  216. 'path'=>Yii::$app->params['uploadPath'],
  217. 'user_id'=>Yii::$app->user->identity->id,
  218. 'auction_id'=>'null',
  219. 'lot_id'=>$model->id,
  220. 'type' => 'NULL'
  221. ]);
  222. $model->updateAttributes(['dogovor_id' => $dogovor_id]);
  223. }
  224. $data = json_decode($model->serialize_data, true);
  225. $model->updateAttributes(['serialize_data' => json_encode($data)]);
  226. return $this->redirect(['view', 'id' => $model->id]);
  227. } else {
  228. return $this->render('update', [
  229. 'model' => $model,
  230. ]);
  231. }
  232. }
  233. public function actionDelete($id)
  234. {
  235. $model = $this->findModel($id);
  236. if ($model->status == 2) {
  237. Yii::$app->session->setFlash('danger', Yii::t('app', 'LotEditNoSuccess ID'));
  238. return $this->redirect(['index']);
  239. } elseif ($model->status == 3) {
  240. Yii::$app->session->setFlash('danger', Yii::t('app', 'LotEditNoReject ID'));
  241. return $this->redirect(['index']);
  242. }
  243. if ($model->status == 1) {
  244. if ($model->auction) {
  245. $model->auction->delete();
  246. }
  247. Yii::createObject(Eventlog::className())->PutLog([
  248. 'user_id' => Yii::$app->user->identity->id,
  249. 'ip' => Yii::$app->request->getRemoteIP(),
  250. 'auk_id' => $model->id,
  251. 'action' => Yii::t('app', 'OrgDeleteAuk ID {action}',
  252. ['action' => $model->aukname]),
  253. ]);
  254. $model->lot_lock = 0;
  255. $this->findModel($id)->delete();
  256. Yii::$app->session->setFlash('danger', Yii::t('app', 'Auction deleted successfully'));
  257. return $this->redirect(['index']);
  258. } else {
  259. return $this->render('/message', [
  260. 'title' => \Yii::t('app', 'Not Permission'),
  261. 'module' => $this->module,
  262. ]);
  263. }
  264. }
  265. public function actionAuction($id, $type)
  266. {
  267. if(Yii::$app->user->can('admin') || Yii::$app->user->can('org'))
  268. {
  269. // print_r($_POST);die();
  270. $model = $this->findModel($id);
  271. if($model->status==2 && $model->lot_lock==0 || $model->status==4 && $model->lot_lock==0)
  272. {
  273. // if(Yii::$app->request->post('date_stop'))
  274. // {
  275. // $date_stop = Yii::$app->request->post('date_stop');
  276. // $time_cont = Yii::$app->request->post('time_cont');
  277. // }
  278. // else
  279. // {
  280. // $date = strtotime($model->auction_date) + 7200;
  281. // $date_stop = date("Y-m-d H:i:s", $date);
  282. // }
  283. //$date = new \DateTime($model->auction_date);
  284. $setData = json_decode($model->serialize_data);
  285. $parts = array_pad(explode(':', $setData->time_step_change), 3, 0);
  286. $timeStepFormat = sprintf(
  287. "%02d:%02d:%02d.00000",
  288. (int)$parts[0],
  289. (int)$parts[1],
  290. (int)$parts[2]
  291. );
  292. // create auction
  293. $insert = [
  294. 'user_id' => $model->user_id,
  295. 'name' => $model->aukname,
  296. 'lot_id' => $model->id,
  297. 'lot_num' => $model->num,
  298. 'date_start' => $model->auction_date,
  299. 'bidding_date' => $model->bidding_date,
  300. 'date_stop' => $setData->date_stop,
  301. 'type_id' => $setData->type,
  302. 'time_step_down' => $timeStepFormat,
  303. 'time_cont'=> $setData->time_cont,
  304. 'step_down'=> $setData->step_change,
  305. 'temp_step_down'=>$setData->step_change,
  306. //'date_stop' => $date->modify('+1 day')->format('Y-m-d H:i:s'),
  307. ];
  308. // echo '<pre>';
  309. // print_r(json_decode($model->serialize_data));
  310. // print_r($insert);
  311. // echo '</pre>';
  312. // die();
  313. $auction = Yii::createObject(Auctions::className())->CreateAuction($insert);
  314. Subscriptions::createNewsletter($auction);
  315. /* Узгоджено організатором */
  316. if($model->status==4)
  317. {
  318. $model->updateAttributes(['lot_lock' => '0']);
  319. }
  320. $model->updateAttributes(['lot_lock' => '1']);
  321. $notes_org = Yii::t('app','AuctionCreatedOnLot ID') .": ". $model->aukname ." / " .
  322. Yii::t('app','LotNumber ID'). $model->num . " " . $model->name;
  323. Yii::createObject(Messages::className())->CreateMessage(['user_id' => $model->user_id, 'notes' => $notes_org]);
  324. Yii::$app->session->setFlash('success', Yii::t('app', 'AuctionCreated ID'));
  325. return $this->redirect(['index']);
  326. } elseif ($model->status == 1 || $model->status == 3) {
  327. Yii::$app->session->setFlash('warning', Yii::t('app', 'NeedConfirmLot ID'));
  328. return $this->redirect(['view', 'id' => $model->id]);
  329. }
  330. else
  331. {
  332. Yii::$app->session->setFlash('warning', Yii::t('app', 'LotInAuction ID'));
  333. return $this->redirect(['view', 'id' => $model->id]);
  334. }
  335. } else {
  336. Yii::$app->session->setFlash('success', Yii::t('app', 'not permission2'));
  337. return $this->redirect(['view', 'id' => $model->id]);
  338. }
  339. }
  340. public function actionStepChange()
  341. {
  342. $id = Yii::$app->request->post('id');
  343. $value = Yii::$app->request->post('value');
  344. $model = Lots::findOne(['id' => $id]);
  345. $model->step_down = $value;
  346. if (Yii::$app->request->post() && $model->save()) {
  347. return $this->redirect(['/lots/view', 'id' => $id]);
  348. }
  349. }
  350. public function actionTimeStepChange()
  351. {
  352. $id = Yii::$app->request->post('id');
  353. $value = Yii::$app->request->post('concat_val');
  354. $model = Lots::findOne(['id' => $id]);
  355. $model->time_step_down = $value;
  356. if (Yii::$app->request->post() && $model->save()) {
  357. return $this->redirect(['/lots/view', 'id' => $id]);
  358. }
  359. }
  360. public function actionClone($id)
  361. {
  362. $model = $this->findModel($id);
  363. // print_r($model->attributes);
  364. // exit;
  365. $attributes = $model->attributes;
  366. $attributes['id'] = null;
  367. $attributes['status'] = 1;
  368. $attributes['lot_lock'] = 0;
  369. $attributes['docs_id'] = null;
  370. $clone = new Lots($attributes);
  371. if ($clone->load(Yii::$app->request->post()) && $clone->save()) {
  372. if ((Yii::$app->request->post('agreeCopyFile')) && $model->file) {
  373. $clone->updateAttributes(['docs_id' => $model->file->copy($clone->id)]);
  374. } else {
  375. $clone->updateAttributes(['docs_id' => null]);
  376. }
  377. if (Yii::$app->request->post('agreeCopyImg')) {
  378. foreach ($model->images as $image) {
  379. $image->copy($clone->id);
  380. }
  381. foreach ($model->thumbnails as $thumbnail) {
  382. $thumbnail->copy($clone->id);
  383. }
  384. }
  385. return $this->redirect(['view', 'id' => $clone->id]);
  386. } else {
  387. return $this->render('clone', [
  388. 'model' => $model,
  389. ]);
  390. }
  391. }
  392. protected function findModel($id)
  393. {
  394. if (($model = Lots::findOne($id)) !== null) {
  395. return $model;
  396. } else {
  397. throw new NotFoundHttpException('The requested page does not exist . ');
  398. }
  399. }
  400. }