LotsController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  113. if ($fileName) {
  114. $model->docs_id = $files->saveFile([
  115. 'name' => $fileName,
  116. 'path' => $files->uploads,
  117. 'user_id' => Yii::$app->user->identity->id,
  118. 'lot_id' => $model->id,
  119. 'auction_id' => null,
  120. 'type' => 'lot',
  121. ]);
  122. $model->save();
  123. $model->upload();
  124. }
  125. return $this->redirect(['view', 'id' => $model->id]);
  126. } else {
  127. return $this->render('create', ['model' => $model]);
  128. }
  129. }
  130. public function actionConfirm($id, $status)
  131. {
  132. if (Yii::$app->user->can('admin') || Yii::$app->user->can('org')) {
  133. $model = $this->findModel($id);
  134. Yii::$app->response->format = Response::FORMAT_JSON;
  135. return
  136. $model->updateAttributes(['status' => $status,]);
  137. }else{
  138. return $this->redirect(['view', 'id' => $id]);
  139. }
  140. }
  141. public function actionCircle_down($id)
  142. {
  143. if (Yii::$app->user->can('admin')) {
  144. $model = $this->findModel($id);
  145. Yii::$app->db->createCommand("UPDATE lots SET step_down=step_down-1 WHERE id=:id")
  146. ->bindValue(':id', $id)
  147. ->execute();
  148. }
  149. return $this->redirect(['view', 'id' => $id]);
  150. }
  151. public function actionCircle_up($id)
  152. {
  153. if (Yii::$app->user->can('admin')) {
  154. $model = $this->findModel($id);
  155. Yii::$app->db->createCommand("UPDATE lots SET step_down=step_down+1 WHERE id=:id")
  156. ->bindValue(':id', $id)
  157. ->execute();
  158. }
  159. return $this->redirect(['view', 'id' => $id]);
  160. }
  161. public function actionUpdate($id)
  162. {
  163. $model = $this->findModel($id);
  164. $files = new Files();
  165. $files->uploads = '../uploads/lots/';
  166. // UploadedFile::getInstances($files, 'file');
  167. $fileName = $files->uploadFile();
  168. if ($model->status == 2) {
  169. Yii::$app->session->setFlash('danger', Yii::t('app', 'LotEditNoSuccess ID'));
  170. return $this->redirect(['index']);
  171. } elseif ($model->status == 3) {
  172. Yii::$app->session->setFlash('danger', Yii::t('app', 'LotEditNoReject ID'));
  173. return $this->redirect(['index']);
  174. }
  175. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  176. if ($fileName) ;
  177. {
  178. $model->docs_id = $files->saveFile([
  179. 'name' => $fileName,
  180. 'path' => $files->uploads,
  181. 'user_id' => Yii::$app->user->identity->id,
  182. 'lot_id' => $model->id,
  183. 'auction_id' => null,
  184. 'type' => 'lot',
  185. ]);
  186. $model->save();
  187. $model->upload();
  188. }
  189. return $this->redirect(['view', 'id' => $model->id]);
  190. } else {
  191. return $this->render('update', [
  192. 'model' => $model,
  193. ]);
  194. }
  195. }
  196. public function actionDelete($id)
  197. {
  198. $model = $this->findModel($id);
  199. if ($model->status == 2) {
  200. Yii::$app->session->setFlash('danger', Yii::t('app', 'LotEditNoSuccess ID'));
  201. return $this->redirect(['index']);
  202. } elseif ($model->status == 3) {
  203. Yii::$app->session->setFlash('danger', Yii::t('app', 'LotEditNoReject ID'));
  204. return $this->redirect(['index']);
  205. }
  206. if ($model->status == 1) {
  207. if ($model->auction) {
  208. $model->auction->delete();
  209. }
  210. Yii::createObject(Eventlog::className())->PutLog([
  211. 'user_id' => Yii::$app->user->identity->id,
  212. 'ip' => Yii::$app->request->getRemoteIP(),
  213. 'auk_id' => $model->id,
  214. 'action' => Yii::t('app', 'OrgDeleteAuk ID {action}',
  215. ['action' => $model->aukname]),
  216. ]);
  217. $model->lot_lock = 0;
  218. $this->findModel($id)->delete();
  219. Yii::$app->session->setFlash('danger', Yii::t('app', 'Auction deleted successfully'));
  220. return $this->redirect(['index']);
  221. } else {
  222. return $this->render('/message', [
  223. 'title' => \Yii::t('app', 'Not Permission'),
  224. 'module' => $this->module,
  225. ]);
  226. }
  227. }
  228. public function actionAuction($id, $type)
  229. {
  230. if(Yii::$app->user->can('admin') || Yii::$app->user->can('org'))
  231. {
  232. // print_r($_POST);die();
  233. $model = $this->findModel($id);
  234. if($model->status==2 && $model->lot_lock==0 || $model->status==4 && $model->lot_lock==0)
  235. {
  236. if(Yii::$app->request->post('date_stop'))
  237. {
  238. $date_stop = Yii::$app->request->post('date_stop');
  239. $time_cont = Yii::$app->request->post('time_cont');
  240. }
  241. else
  242. {
  243. $date = strtotime($model->auction_date) + 7200;
  244. $date_stop = date("Y-m-d H:i:s", $date);
  245. }
  246. //$date = new \DateTime($model->auction_date);
  247. // create auction
  248. $insert = [
  249. 'user_id' => $model->user_id,
  250. 'name' => $model->aukname,
  251. 'lot_id' => $model->id,
  252. 'lot_num' => $model->num,
  253. 'date_start' => $model->auction_date,
  254. 'bidding_date' => $model->bidding_date,
  255. 'date_stop' => $date_stop,
  256. 'type_id' => $type,
  257. 'time_step_down' => $model->time_step_down,
  258. 'time_cont'=> $time_cont,
  259. 'step_down'=> $model->step_down,
  260. 'temp_step_down'=>$model->step_down,
  261. //'date_stop' => $date->modify('+1 day')->format('Y-m-d H:i:s'),
  262. ];
  263. // print_r($insert);die();
  264. $auction = Yii::createObject(Auctions::className())->CreateAuction($insert);
  265. Subscriptions::createNewsletter($auction);
  266. /* Узгоджено організатором */
  267. if($model->status==4)
  268. {
  269. $model->updateAttributes(['lot_lock' => '0']);
  270. }
  271. $model->updateAttributes(['lot_lock' => '1']);
  272. $notes_org = Yii::t('app','AuctionCreatedOnLot ID') .": ". $model->aukname ." / " .
  273. Yii::t('app','LotNumber ID'). $model->num . " " . $model->name;
  274. Yii::createObject(Messages::className())->CreateMessage(['user_id' => $model->user_id, 'notes' => $notes_org]);
  275. Yii::$app->session->setFlash('success', Yii::t('app', 'AuctionCreated ID'));
  276. return $this->redirect(['index']);
  277. } elseif ($model->status == 1 || $model->status == 3) {
  278. Yii::$app->session->setFlash('warning', Yii::t('app', 'NeedConfirmLot ID'));
  279. return $this->redirect(['view', 'id' => $model->id]);
  280. }
  281. else
  282. {
  283. Yii::$app->session->setFlash('warning', Yii::t('app', 'LotInAuction ID'));
  284. return $this->redirect(['view', 'id' => $model->id]);
  285. }
  286. } else {
  287. Yii::$app->session->setFlash('success', Yii::t('app', 'not permission2'));
  288. return $this->redirect(['view', 'id' => $model->id]);
  289. }
  290. }
  291. public function actionStepChange()
  292. {
  293. $id = Yii::$app->request->post('id');
  294. $value = Yii::$app->request->post('value');
  295. $model = Lots::findOne(['id' => $id]);
  296. $model->step_down = $value;
  297. if (Yii::$app->request->post() && $model->save()) {
  298. return $this->redirect(['/lots/view', 'id' => $id]);
  299. }
  300. }
  301. public function actionTimeStepChange()
  302. {
  303. $id = Yii::$app->request->post('id');
  304. $value = Yii::$app->request->post('concat_val');
  305. $model = Lots::findOne(['id' => $id]);
  306. $model->time_step_down = $value;
  307. if (Yii::$app->request->post() && $model->save()) {
  308. return $this->redirect(['/lots/view', 'id' => $id]);
  309. }
  310. }
  311. public function actionClone($id)
  312. {
  313. $model = $this->findModel($id);
  314. $attributes = $model->attributes;
  315. $attributes['id'] = null;
  316. $attributes['status'] = 1;
  317. $attributes['lot_lock'] = 0;
  318. $attributes['docs_id'] = null;
  319. $clone = new Lots($attributes);
  320. if ($clone->load(Yii::$app->request->post()) && $clone->save()) {
  321. if ((Yii::$app->request->post('agreeCopyFile')) && $model->file) {
  322. $clone->updateAttributes(['docs_id' => $model->file->copy($clone->id)]);
  323. } else {
  324. $clone->updateAttributes(['docs_id' => null]);
  325. }
  326. if (Yii::$app->request->post('agreeCopyImg')) {
  327. foreach ($model->images as $image) {
  328. $image->copy($clone->id);
  329. }
  330. foreach ($model->thumbnails as $thumbnail) {
  331. $thumbnail->copy($clone->id);
  332. }
  333. }
  334. return $this->redirect(['view', 'id' => $clone->id]);
  335. } else {
  336. return $this->render('clone', [
  337. 'model' => $model,
  338. ]);
  339. }
  340. }
  341. protected function findModel($id)
  342. {
  343. if (($model = Lots::findOne($id)) !== null) {
  344. return $model;
  345. } else {
  346. throw new NotFoundHttpException('The requested page does not exist . ');
  347. }
  348. }
  349. }