BiddingController.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. namespace app\controllers;
  3. use app\models\Auctions;
  4. use Yii;
  5. use app\models\Files;
  6. use app\models\Bidding;
  7. use app\models\Messages;
  8. use app\models\Eventlog;
  9. use yii\data\ActiveDataProvider;
  10. use yii\web\Controller;
  11. use yii\web\NotFoundHttpException;
  12. use yii\filters\VerbFilter;
  13. use yii\web\UploadedFile;
  14. use DateTime;
  15. class BiddingController extends Controller
  16. {
  17. public function behaviors()
  18. {
  19. return [
  20. 'verbs' => [
  21. 'class' => VerbFilter::className(),
  22. 'actions' => [
  23. 'delete' => ['post'],
  24. ],
  25. ],
  26. ];
  27. }
  28. public function init(){
  29. if(@!Yii::$app->user->identity->confirmed_at && (@Yii::$app->user->identity->role == 1)){
  30. return $this->redirect('/registration/organizer');
  31. }
  32. $this->layout = '@app/views/layouts/backend/user';
  33. parent::init();
  34. }
  35. public function actionConfirm($id)
  36. {
  37. $model = $this->findModel($id);
  38. $model->setAttribute('status','1');
  39. if(($model->special_price > $model->auction->lot->start_price)
  40. && ($model->special_price > $model->auction->special_price) && in_array($model->auction->status, [0, 1]) ){
  41. $auc = Auctions::find()->where(['id' => $model->auction_id])->one();
  42. $auc->special_user = $model->user_id;
  43. $auc->special_price = $model->special_price;
  44. $auc->save(false);
  45. }
  46. $model->save(false);
  47. Yii::createObject(Eventlog::className())->PutLog([
  48. 'user_id' => Yii::$app->user->identity->id,
  49. 'ip' => $_SERVER['REMOTE_ADDR'],
  50. 'auk_id' => $model->id,
  51. 'action' => Yii::t('app','ConfirmationOfBidding ID'),
  52. ]);
  53. $notes = Yii::t('app','BiddingConfirm ID').": ".$model->auction->name." / ".
  54. Yii::t('app','LotNumber ID')." ".
  55. $model->auction->lot_num ." ". $this->getLotName($model->auction->lot_id);
  56. Yii::createObject(Messages::className())->CreateMessage(['user_id' => $model->user_id, 'notes' => $notes]);
  57. return $this->redirect(['index', 'id' => Yii::$app->user->identity->id]);
  58. }
  59. public function actionIndex()
  60. {
  61. if(Yii::$app->user->can('org') || Yii::$app->user->can('admin'))
  62. {
  63. //$dataProvider = new ActiveDataProvider([
  64. // 'query' => Bidding::find()->where(['org_id' => Yii::$app->user->identity->id]),
  65. //]);
  66. // Obnylator Biddings
  67. //Yii::$app->db->createCommand("UPDATE bidding SET readed=1 WHERE org_id=:user_id")->bindValue(":user_id",Yii::$app->user->identity->id)->execute();
  68. // Pagination
  69. $totalCount = Bidding::find()->where(['org_id' => Yii::$app->user->getId()])->count();
  70. $query = Bidding::find()
  71. ->joinWith([
  72. 'auction',
  73. 'lot',
  74. 'user'])
  75. //->where(['bidding.org_id' => Yii::$app->user->getId()])
  76. ->orderBy(['bidding.id' => SORT_DESC]);
  77. // $query = '
  78. // SELECT
  79. // bidding.id
  80. // bidding.created_at,
  81. // auctions.date_start,
  82. // auctions.name as aukname,
  83. // lots.name,
  84. // user.at_org,
  85. // bidding.status,
  86. // bidding.file_id
  87. // FROM `bidding`
  88. // LEFT JOIN auctions
  89. // ON (bidding.auction_id=auctions.id)
  90. // LEFT JOIN lots
  91. // ON (auctions.lot_id=lots.id)
  92. // LEFT JOIN user
  93. // ON (bidding.user_id=user.id)
  94. // WHERE bidding.org_id=:org_id
  95. // ORDER by bidding.id DESC';
  96. $dataProvider = new ActiveDataProvider([
  97. 'query' => $query,
  98. 'pagination' => [
  99. 'pageSize' => 40,
  100. ]
  101. ]);
  102. return $this->render('index', [
  103. 'dataProvider' => $dataProvider,
  104. ]);
  105. }
  106. else
  107. {
  108. throw new NotFoundHttpException('The requested page does not exist.');
  109. }
  110. }
  111. public function actionView()
  112. {
  113. Yii::$app->db->createCommand("UPDATE bidding SET readed=1 WHERE user_id=:user_id")->bindValue(":user_id",Yii::$app->user->identity->id)->execute();
  114. $dataProvider = new ActiveDataProvider([
  115. 'query' => Bidding::find()->where(['user_id' => Yii::$app->user->identity->id,])->orderBy('created_at DESC'),
  116. ]);
  117. return $this->render('view', [
  118. 'dataProvider' => $dataProvider,
  119. ]);
  120. }
  121. public function actionUpdate($id)
  122. {
  123. $model = $this->findModel($id);
  124. if(Yii::$app->user->can('member'))
  125. {
  126. $bid_date = new DateTime($model->auction->bidding_date);
  127. $now_date = new DateTime(date("Y-m-d H:i:s"));
  128. var_dump($bid_date->diff($now_date));
  129. if($bid_date < $now_date)
  130. {
  131. return $this->redirect(['view']);
  132. }
  133. if (Yii::$app->request->post())
  134. {
  135. $files = new Files();
  136. $file = UploadedFile::getInstance($files, 'file');
  137. $file->name = $files->transliteration($file->name);
  138. if(isset($file))
  139. {
  140. $path = Yii::$app->params['uploadPath'].$file->name;
  141. $file->saveAs($path);
  142. $files->updateFile([
  143. 'name'=>$file->name,
  144. 'file_id'=>$model->file_id]
  145. );
  146. $notes = Yii::$app->user->identity->at_org." ". Yii::t('app','BiddingEdit ID').": ".
  147. $model->auction->name ." / ". Yii::t('app','LotNumber ID')." ".
  148. $model->auction->lot_num ." ". $this->getLotName($model->auction->lot_id);
  149. $self_notes = Yii::t('app','BiddingEditSelf ID').": ".
  150. $model->auction->name." / ".
  151. Yii::t('app','LotNumber ID')." ".
  152. $model->auction->lot_num ." ". $this->getLotName($model->auction->lot_id);
  153. Yii::createObject(Messages::className())->CreateMessage(['user_id' => $model->org_id, 'notes' => $notes]);
  154. Yii::createObject(Messages::className())->CreateMessage(['user_id' => Yii::$app->user->identity->id, 'notes' => $self_notes]);
  155. return $this->redirect(['view']);
  156. }
  157. else
  158. {
  159. print "no file";
  160. }
  161. }
  162. else
  163. {
  164. return $this->render('update', [
  165. 'model' => $model,
  166. ]);
  167. }
  168. }
  169. else
  170. {
  171. throw new NotFoundHttpException('The requested page does not exist.');
  172. }
  173. }
  174. public function actionReject($id)
  175. {
  176. $model = $this->findModel($id);
  177. $model->setAttribute('status','2');
  178. $model->save(false);
  179. //by ivakhnov
  180. $notes = Yii::t('app','BidRejected ID').": ".$model->auction->name." / ".
  181. Yii::t('app','LotNumber ID')." ".
  182. $model->auction->lot_num ." ". $this->getLotName($model->auction->lot_id);
  183. //-----------
  184. Yii::createObject(Messages::className())->CreateMessage(['user_id' => $model->user_id, 'notes' => $notes]);
  185. return $this->redirect(['index', 'id' => Yii::$app->user->identity->id]);
  186. }
  187. public function actionDelete($id)
  188. {
  189. $model = $this->findModel($id);
  190. $model->delete();
  191. if(Yii::$app->user->can('member'))
  192. {
  193. $bid_date = new DateTime($model->auction->bidding_date);
  194. $now_date = new DateTime(date("Y-m-d H:i:s"));
  195. var_dump($bid_date->diff($now_date));
  196. if ($bid_date < $now_date) {
  197. return $this->redirect(['view']);
  198. }
  199. }
  200. $notes = Yii::$app->user->identity->at_org." ". Yii::t('app','BiddingDelete ID').": ".
  201. $model->auction->name ." / ". Yii::t('app','LotNumber ID')." ".
  202. $model->auction->lot_num ." ". $this->getLotName($model->auction->lot_id);
  203. $self_notes = Yii::t('app','BiddingDeleteSelf ID').": ".
  204. $model->auction->name." / ".
  205. Yii::t('app','LotNumber ID')." ".
  206. $model->auction->lot_num ." ". $this->getLotName($model->auction->lot_id);
  207. Yii::createObject(Messages::className())->CreateMessage(['user_id' => $model->org_id, 'notes' => $notes]);
  208. Yii::createObject(Messages::className())->CreateMessage(['user_id' => Yii::$app->user->identity->id, 'notes' => $self_notes]);
  209. $this->findModel($id)->delete();
  210. if(Yii::$app->user->can('org'))
  211. {
  212. return $this->redirect(['index']);
  213. }
  214. if(Yii::$app->user->can('member'))
  215. {
  216. return $this->redirect(['bidding/view']);
  217. }
  218. }
  219. protected function findModel($id)
  220. {
  221. if (($model = Bidding::findOne($id)) !== null) {
  222. return $model;
  223. } else {
  224. throw new NotFoundHttpException('The requested page does not exist.');
  225. }
  226. }
  227. public function getLotName($id)
  228. {
  229. $lotName = Yii::$app->db->createCommand("SELECT name from lots WHERE id=:id")->bindValue(':id',$id);
  230. $result = $lotName->queryOne();
  231. return $result['name'];
  232. }
  233. }