AuctionsController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. namespace app\controllers;
  3. use app\models\Bidding;
  4. use app\models\Lots;
  5. use app\models\Messages;
  6. use app\models\Eventlog;
  7. use dektrium\user\Mailer;
  8. use Yii;
  9. use app\models\Auctions;
  10. use app\models\AuctionsSearch;
  11. use yii\web\Controller;
  12. use yii\web\NotFoundHttpException;
  13. use yii\filters\VerbFilter;
  14. use DateTime;
  15. use app\models\Subscriptions;
  16. use yii\helpers\ArrayHelper;
  17. /**
  18. * AuctionsController implements the CRUD actions for Auctions model.
  19. */
  20. class AuctionsController extends Controller
  21. {
  22. public function behaviors()
  23. {
  24. return [
  25. 'verbs' => [
  26. 'class' => VerbFilter::className(),
  27. 'actions' => [
  28. 'delete' => ['post'],
  29. ],
  30. ],
  31. ];
  32. }
  33. /**
  34. * Lists all Auctions models.
  35. * @return mixed
  36. */
  37. public function init()
  38. {
  39. /*if(Yii::$app->user->isGuest)
  40. {
  41. Yii::$app->getSession()->setFlash('warning', Yii::t('app', 'EnterWatcher ID'));
  42. return $this->redirect('/user/login');
  43. }*/
  44. if(@!Yii::$app->user->identity->confirmed_at && (@Yii::$app->user->identity->role == 1)){
  45. return $this->redirect('/registration/organizer');
  46. }
  47. $this->layout = '@app/views/layouts/backend/user';
  48. parent::init();
  49. }
  50. private function getIP()
  51. {
  52. return $_SERVER['REMOTE_ADDR'];
  53. }
  54. public function actionIndex()
  55. {
  56. $searchModel = new AuctionsSearch();
  57. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  58. return $this->render('index', [
  59. 'searchModel' => $searchModel,
  60. 'dataProvider' => $dataProvider,
  61. ]);
  62. }
  63. /**
  64. * Displays a single Auctions model.
  65. * @param integer $id
  66. * @return mixed
  67. */
  68. public function actionView($id)
  69. {
  70. if(Yii::$app->user->isGuest)
  71. {
  72. Yii::$app->getSession()->setFlash('success', Yii::t('app', 'EnterWatcher ID'));
  73. return $this->redirect('/user/login');
  74. }
  75. $model = $this->findModel($id);
  76. if($model->user_id==Yii::$app->user->identity->id) {
  77. Yii::createObject(Eventlog::className())->PutLog([
  78. 'user_id' => Yii::$app->user->identity->id,
  79. 'ip' => $this->getIP(),
  80. 'auk_id' => $model->id,
  81. 'action' => Yii::t('app','OrgEnterAuk ID'),
  82. ]);
  83. $model->setFlags($id);
  84. return $this->render('view', [
  85. 'model' => $model,
  86. ]);
  87. }
  88. if(Yii::$app->user->can('admin'))
  89. {
  90. Yii::createObject(Eventlog::className())->PutLog([
  91. 'user_id' => Yii::$app->user->identity->id,
  92. 'ip' => $this->getIP(),
  93. 'auk_id' => $model->id,
  94. 'action' => Yii::t('app','EnterAuk ID'),
  95. ]);
  96. $model->setFlags($id);
  97. return $this->render('view', [
  98. 'model' => $model,
  99. ]);
  100. }
  101. if(Yii::$app->user->can('watcher'))
  102. {
  103. Yii::createObject(Eventlog::className())->PutLog([
  104. 'user_id' => Yii::$app->user->identity->id,
  105. 'ip' => $this->getIP(),
  106. 'auk_id' => $model->id,
  107. 'action' => Yii::t('app','WatcherEnterAuk ID'),
  108. ]);
  109. $model->setFlags($id);
  110. return $this->render('view', [
  111. 'model' => $model,
  112. ]);
  113. }
  114. else
  115. {
  116. $sql = Yii::$app->db->createCommand("SELECT status from bidding WHERE auction_id=:auction_id and user_id=:user_id");
  117. $sql->bindValues([':auction_id' => $model->id,':user_id' => Yii::$app->user->identity->id]);
  118. $res = $sql->queryOne();
  119. if($res==false)
  120. {
  121. Yii::$app->getSession()->setFlash('danger', Yii::t('app', 'NeedBidding ID'));
  122. return $this->redirect('/auctions/index');
  123. }
  124. elseif($res['status']=="2") // 2=reject
  125. {
  126. Yii::$app->getSession()->setFlash('danger', Yii::t('app', 'BidRejected ID'));
  127. return $this->redirect('/auctions/index');
  128. }
  129. elseif($res['status']=="0") // 0=default
  130. {
  131. Yii::$app->getSession()->setFlash('warning', Yii::t('app', 'BidWaiting ID'));
  132. return $this->redirect('/auctions/index');
  133. }
  134. elseif($res['status']=="1") // 0=accept
  135. {
  136. Yii::createObject(Eventlog::className())->PutLog([
  137. 'user_id' => Yii::$app->user->identity->id,
  138. 'ip' => $this->getIP(),
  139. 'auk_id' => $model->id,
  140. 'action' => Yii::t('app','EnterAuk ID'),
  141. ]);
  142. $model->setFlags($id);
  143. return $this->render('view', [
  144. 'model' => $model,
  145. ]);
  146. }
  147. else
  148. {
  149. throw new NotFoundHttpException('Page not found');
  150. }
  151. }
  152. }
  153. /**
  154. * Creates a new Auctions model.
  155. * If creation is successful, the browser will be redirected to the 'view' page.
  156. * @return mixed
  157. */
  158. public function actionCreate()
  159. {
  160. print_r(Yii::$app->request->post());
  161. exit;
  162. if(Yii::$app->user->isGuest)
  163. {
  164. return $this->redirect('/user/login');
  165. }
  166. if(Yii::$app->user->can('admin') && Yii::$app->user->can('org')) {
  167. $model = new Auctions();
  168. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  169. return $this->redirect(['view', 'id' => $model->id]);
  170. } else {
  171. return $this->render('create', [
  172. 'model' => $model,
  173. ]);
  174. }
  175. }
  176. else
  177. {
  178. return $this->redirect(['index']);
  179. }
  180. }
  181. /**
  182. * Updates an existing Auctions model.
  183. * If update is successful, the browser will be redirected to the 'view' page.
  184. * @param integer $id
  185. * @return mixed
  186. */
  187. public function actionUpdate($id)
  188. {
  189. if(Yii::$app->user->isGuest)
  190. {
  191. return $this->redirect('/user/login');
  192. }
  193. if(Yii::$app->user->can('admin')) {
  194. $model = $this->findModel($id);
  195. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  196. return $this->redirect(['view', 'id' => $model->id]);
  197. } else {
  198. return $this->render('update', [
  199. 'model' => $model,
  200. ]);
  201. }
  202. }
  203. else
  204. {
  205. return $this->redirect(['index']);
  206. }
  207. }
  208. /**
  209. * Deletes an existing Auctions model.
  210. * If deletion is successful, the browser will be redirected to the 'index' page.
  211. * @param integer $id
  212. * @return mixed
  213. */
  214. public function actionDelete($id)
  215. {
  216. if(Yii::$app->user->isGuest)
  217. {
  218. return $this->redirect('/user/login');
  219. }
  220. if(Yii::$app->user->can('admin')) {
  221. $lotId = $this->findModel($id)->lot_id;
  222. $lots = Lots::findOne($lotId);
  223. $lots->updateAttributes(['lot_lock' => 0]);
  224. $this->findModel($id)->delete();
  225. return $this->redirect(['index']);
  226. }
  227. else
  228. {
  229. return $this->redirect(['index']);
  230. }
  231. }
  232. public function actionTradelog($id)
  233. {
  234. $user_id = '0';
  235. $query = Yii::$app->db->createCommand('SELECT * FROM trade_logs WHERE auk_id=:id'); //AND user_id=:user_id'
  236. $query->bindValues([':id' => $id]); //':user_id' => $user_id]
  237. $result = $query->queryAll();
  238. $filename = "../uploads/temp/tradelog-".$id.$user_id.date('Ymdhis').".doc";
  239. $file = fopen($filename,'w+');
  240. foreach ($result as $item => $value)
  241. {
  242. fputs($file,$value['date']." ".$value['comment']."\n");
  243. }
  244. fclose($file);
  245. Yii::$app->response->SendFile($filename);
  246. }
  247. public function actionBid($id, $special = null)
  248. {
  249. if(Yii::$app->user->isGuest)
  250. {
  251. return $this->redirect('/user/login');
  252. }
  253. $model = $this->findModel($id);
  254. $bid_date = new DateTime($model->bidding_date);
  255. $now_date = new DateTime(date("Y-m-d H:i:s"));
  256. //var_dump($bid_date->diff($now_date));
  257. if($bid_date < $now_date)
  258. {
  259. Yii::$app->session->setFlash('danger', Yii::t('app', 'BiddingTimeEnd ID'));
  260. return $this->redirect(['/lots/view', 'id' => $model->lot_id]);
  261. }
  262. // test for documents
  263. $sql = Yii::$app->db->createCommand("SELECT id from files WHERE auction_id=:auction_id and user_id=:user_id order by id desc");
  264. $sql->bindValues([':auction_id' => $model->id,':user_id' => Yii::$app->user->identity->id]);
  265. $result = $sql->queryOne();
  266. if(!$result)
  267. {
  268. Yii::$app->session->setFlash('danger', Yii::t('app', 'NeedDownloadFiles ID'));
  269. return $this->redirect(['/lots/view', 'id' => $model->lot_id]);
  270. }
  271. else
  272. {
  273. $file_id = $result['id'];
  274. }
  275. // test for repeat
  276. $sql = Yii::$app->db->createCommand("SELECT * from bidding WHERE auction_id=:auction_id and user_id=:user_id");
  277. $sql->bindValues([':auction_id' => $model->id,':user_id' => Yii::$app->user->identity->id]);
  278. $result = $sql->queryOne();
  279. if(!$result)
  280. {
  281. Yii::createObject(Bidding::className())->CreateBid([
  282. 'auction_id' => $model->id,
  283. 'user_id' => Yii::$app->user->identity->id,
  284. 'org_id'=>$model->user_id,
  285. 'file_id'=>$file_id,
  286. 'special_price' => ArrayHelper::getValue(YII::$app->request->post('Bidding'), 'special_price', 0)
  287. ]);
  288. $notes_org = Yii::$app->user->identity->at_org ." ". Yii::t('app', 'NotesOrg ID') .": ". $model->name ." / ".
  289. Yii::t('app','LotNumber ID'). $model->lot_num ." ". $model->lot->name; //Yii::t('app', 'Notes2Org ID');
  290. $notes_self = Yii::t('app', 'NotesMember ID') .": ". $model->name. " / ". Yii::t('app','Lot ID'). " " .$model->lot_num ." ". $model->lot->name;
  291. Yii::createObject(Messages::className())->CreateMessage(['user_id' => $model->user_id, 'notes' => $notes_org]);
  292. Subscriptions::subscribe($model->category->id, Yii::$app->user->getId());
  293. // отправка на почту (временно)
  294. //$mailer = Yii::$container->get(Mailer::className());
  295. //$mailer->sendMessage("vanouub@meta.ua", $notes_org, 'welcome.php');
  296. Yii::createObject(Messages::className())->CreateMessage(['user_id' => Yii::$app->user->identity->id, 'notes' => $notes_self]);
  297. Yii::$app->session->setFlash('success', Yii::t('app', 'BidWellDone ID'));
  298. return $this->redirect(['index', 'id' => Yii::$app->user->identity->id]);
  299. }
  300. else
  301. {
  302. Yii::$app->session->setFlash('warning', Yii::t('app', 'BidExist ID'));
  303. return $this->redirect(['index', 'id' => Yii::$app->user->identity->id]);
  304. }
  305. }
  306. /**
  307. * Finds the Auctions model based on its primary key value.
  308. * If the model is not found, a 404 HTTP exception will be thrown.
  309. * @param integer $id
  310. * @return Auctions the loaded model
  311. * @throws NotFoundHttpException if the model cannot be found
  312. */
  313. protected function findModel($id)
  314. {
  315. if (($model = Auctions::findOne($id)) !== null) {
  316. return $model;
  317. } else {
  318. throw new NotFoundHttpException('The requested page does not exist.');
  319. }
  320. }
  321. }