Auctions.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. /**
  5. * This is the model class for table "auctions".
  6. *
  7. * @property integer $id
  8. * @property integer $user_id
  9. * @property integer $lot_id
  10. * @property integer $date_start
  11. * @property integer $date_stop
  12. * @property integer $last_price
  13. * @property integer $last_user
  14. * @property integer $last_date
  15. * @property string $time_step_down
  16. * @var $user User
  17. * @var $lots Lots
  18. */
  19. class Auctions extends \yii\db\ActiveRecord
  20. {
  21. /**
  22. * @inheritdoc
  23. */
  24. public static function tableName()
  25. {
  26. return 'auctions';
  27. }
  28. /**
  29. * @inheritdoc
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['user_id', 'lot_id', 'date_start', 'date_stop', 'last_price', 'last_user', 'last_date'], 'required'],
  35. [['user_id', 'lot_id', 'last_user','type','lot_num','status','type_id','temp_step_down','step_down'], 'integer'],
  36. [['userName', 'lot', 'lotName','date_start', 'date_stop', 'last_price', 'last_date','name','bidding_date','time_step_down'], 'safe'],
  37. ];
  38. }
  39. /**
  40. * @inheritdoc
  41. */
  42. public function attributeLabels()
  43. {
  44. return [
  45. 'id' => Yii::t('app', 'ID'),
  46. 'name' => Yii::t('app', 'AukName ID'),
  47. 'user_id' => Yii::t('app', 'организатор'),
  48. 'lot_id' => Yii::t('app', 'lot_id'),
  49. 'lot_num' => Yii::t('app', 'Lot ID'),
  50. 'date_start' => Yii::t('app', 'AucStart ID'),
  51. 'date_stop' => Yii::t('app', 'DateStop ID'),
  52. 'bidding_date' => Yii::t('app', 'endBidding ID'),
  53. 'last_price' => Yii::t('app', 'текущая цена'),
  54. 'last_user' => Yii::t('app', 'ставка последнего юзера'),
  55. 'last_date' => Yii::t('app', 'время последней ставки'),
  56. 'lotName' => Yii::t('app', 'Lot ID'),
  57. 'status' => Yii::t('app', 'Status ID'),
  58. 'userName' => Yii::t('app', 'OrgName ID'),
  59. 'type' => Yii::t('app', 'AucType ID'),
  60. 'type_id' => Yii::t('app', 'AucType ID'),
  61. 'time_step_down' => Yii::t('app', 'Time Step'),
  62. 'lot' => Yii::t('app', 'Lot'),
  63. ];
  64. }
  65. /* public function afterDelete()
  66. {
  67. parent::afterDelete();
  68. }*/
  69. public function CreateAuction($insert)
  70. {
  71. //$valid = $this->LotValidate($insert['lot_id']);
  72. $model = new Auctions();
  73. if (empty($insert['step_down'])) { $insert['step_down']=3; } // потом поправить на ауке на повышение
  74. if (empty($insert['time_step_down'])) { $insert['time_step_down']="00:10"; }
  75. $model->setAttributes([
  76. 'user_id' => $insert['user_id'],
  77. 'name' => $insert['name'],
  78. 'lot_id' => $insert['lot_id'],
  79. 'lot_num' => $insert['lot_num'],
  80. 'date_start' => $insert['date_start'],
  81. 'bidding_date' => $insert['bidding_date'],
  82. 'last_date' => $insert['date_start'],
  83. 'date_stop' => $insert['date_stop'],
  84. 'type_id' => $insert['type_id'],
  85. 'step_down' => $insert['step_down'],
  86. 'temp_step_down' => $insert['step_down'],
  87. 'time_step_down' => $insert['time_step_down'],
  88. 'status' => '0',
  89. ]);
  90. $model->save(false);
  91. return $model;
  92. }
  93. private function LotValidate($lot_id)
  94. {
  95. $command = Yii::$app->db->createCommand("SELECT * from auctions WHERE lot_id=:id");
  96. $command->bindValue(':id', $lot_id);
  97. $result = $command->queryOne();
  98. if(!empty($result))
  99. {
  100. return true;
  101. }
  102. else
  103. {
  104. return false;
  105. }
  106. }
  107. public function getLot()
  108. {
  109. return $this->hasOne(Lots::className(), ['id' => 'lot_id']);
  110. }
  111. /* public function getLotStartPrice()
  112. {
  113. return $this->hasOne(Lots::className(),['start_price' => 'lot_id']);
  114. }*/
  115. public function getCategory(){
  116. return $this->hasOne(Category::className(), ['id' => 'category_id'])->via('lot');
  117. }
  118. public function getUser()
  119. {
  120. return $this->hasOne(User::className(), ['id' => 'user_id']);
  121. }
  122. public function getTrade()
  123. {
  124. return $this->hasMany(Trade::className(), ['auk_id' => 'id']);
  125. }
  126. public function getWinners(){
  127. return $this->hasMany(Trade::className(), ['auk_id' => 'id']);
  128. }
  129. public function setFlags($value)
  130. {
  131. Yii::$app->session->set('user.flags',$value);
  132. }
  133. private function SendTime($last_date,$date_stop,$date_start)
  134. {
  135. if($this->type_id==1)
  136. {
  137. if ((strtotime($date_stop) - strtotime($last_date)) < 601) {
  138. $step = 600;
  139. $diff = strtotime(date("Y-m-d H:i:s")) - strtotime($last_date);
  140. //$past_time = sprintf('%02d:%02d:%02d', $diff / 3600, ($diff % 3600) / 60, $diff % 60);
  141. $diff_time = $step - $diff; // 1 Hours perenesti v adminky // 7200 for
  142. return $diff_time;
  143. } else {
  144. $step = strtotime($date_stop) - strtotime($date_start);
  145. $diff = strtotime(date("Y-m-d H:i:s")) - strtotime($date_start);
  146. //$past_time = sprintf('%02d:%02d:%02d', $diff / 3600, ($diff % 3600) / 60, $diff % 60);
  147. $diff_time = $step - $diff; // 1 Hours perenesti v adminky // 7200 for
  148. return $diff_time;
  149. }
  150. }
  151. if($this->type_id==2)
  152. {
  153. list($time,$null)=explode(".",$this->time_step_down);
  154. sscanf($time, "%d:%d:%d", $hours, $minutes, $seconds);
  155. $time_seconds = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;
  156. $m_step = $time_seconds + 1;
  157. if ((strtotime($date_stop) - strtotime($last_date)) < $m_step) {
  158. $step = $time_seconds;
  159. $diff = strtotime(date("Y-m-d H:i:s")) - strtotime($last_date);
  160. //$past_time = sprintf('%02d:%02d:%02d', $diff / 3600, ($diff % 3600) / 60, $diff % 60);
  161. $diff_time = $step - $diff; // 1 Hours perenesti v adminky // 7200 for
  162. return $diff_time;
  163. } else {
  164. $step = strtotime($date_stop) - strtotime($date_start);
  165. $diff = strtotime(date("Y-m-d H:i:s")) - strtotime($date_start);
  166. //$past_time = sprintf('%02d:%02d:%02d', $diff / 3600, ($diff % 3600) / 60, $diff % 60);
  167. $diff_time = $step - $diff; // 1 Hours perenesti v adminky // 7200 for
  168. return $diff_time;
  169. }
  170. }
  171. }
  172. private function SendPerc($last_date,$date_stop,$date_start)
  173. {
  174. if($this->type_id==1)
  175. {
  176. $diff = $this->SendTime($last_date,$date_stop,$date_start);
  177. if((strtotime($date_stop) - strtotime($last_date)) < 601)
  178. {
  179. $step = 600;
  180. }
  181. else
  182. {
  183. $step = strtotime($date_stop) - strtotime($date_start);
  184. }
  185. $perc = 100 * $diff / $step; //100-100*у/х 100*х/у-100
  186. return round($perc,2);
  187. }
  188. if($this->type_id==2)
  189. {
  190. //$m_step = 121;
  191. $diff = $this->SendTime($last_date,$date_stop,$date_start);
  192. if((strtotime($date_stop) - strtotime($last_date)) < 601)
  193. {
  194. $step = 600;
  195. }
  196. else
  197. {
  198. $step = strtotime($date_stop) - strtotime($date_start);
  199. }
  200. $perc = 100 * $diff / $step; //100-100*у/х 100*х/у-100
  201. return round($perc,2);
  202. }
  203. }
  204. private function CheckRole()
  205. {
  206. if(Yii::$app->user->can('admin'))
  207. {
  208. return true;
  209. }
  210. if(Yii::$app->user->can('watcher'))
  211. {
  212. return false;
  213. }
  214. else {
  215. $sql = Yii::$app->db->createCommand("SELECT status from bidding WHERE auction_id=:auction_id and user_id=:user_id");
  216. $sql->bindValues([':auction_id' => $this->id, ':user_id' => Yii::$app->user->identity->id]);
  217. $res = $sql->queryOne();
  218. if ($res == false) {
  219. return false;
  220. } elseif ($res['status'] == "2") // 2=reject
  221. {
  222. return false;
  223. } elseif ($res['status'] == "0") // 0=default
  224. {
  225. return false;
  226. } elseif ($res['status'] == "1") // 0=accept
  227. {
  228. return true;
  229. }
  230. }
  231. }
  232. // for rest api
  233. public function fields()
  234. {
  235. return [
  236. 'id',
  237. 'user_id',
  238. 'user_role' => function() {
  239. return $this->CheckRole();
  240. },
  241. 'lot_id',
  242. 'type_id' => function () {
  243. return $this->type_id;
  244. },
  245. 'member_user_id' => function () {
  246. return Yii::$app->user->identity->id;
  247. },
  248. 'date_start' => function($model)
  249. {
  250. return Yii::$app->formatter->asTime($model->date_start, "php:d.m.Y H:i:s");
  251. },
  252. 'date_stop' => function($model)
  253. {
  254. return Yii::$app->formatter->asTime($model->date_stop, "php:d.m.Y H:i:s");
  255. },
  256. 'start_price' => function()
  257. {
  258. return $this->lot->start_price;
  259. },
  260. 'step' => function()
  261. {
  262. return $this->lot->step;
  263. },
  264. 'step_money' => function()
  265. {
  266. $step_money = $this->lot->start_price / 100 * $this->lot->step;
  267. return round($step_money,2);
  268. },
  269. 'delta_money' => function()
  270. {
  271. if($this->type_id==1) {
  272. $delta_money = $this->last_price - $this->lot->start_price <= 0 ? 0 : $this->last_price - $this->lot->start_price;
  273. return round($delta_money, 2);
  274. }
  275. if($this->type_id==2) {
  276. $delta_money = $this->lot->start_price - $this->last_price <= 0 ? 0 : $this->lot->start_price - $this->last_price;
  277. return round($delta_money, 2);
  278. }
  279. },
  280. 'delta_perc' => function()
  281. {
  282. if($this->type_id==1) {
  283. $delta_perc = ($this->last_price - $this->lot->start_price) / ($this->lot->start_price / 100) <= 0 ? 0 : ($this->last_price - $this->lot->start_price) / ($this->lot->start_price / 100);
  284. return round($delta_perc, 2);
  285. }
  286. if($this->type_id==2) {
  287. $delta_perc = ($this->lot->start_price - $this->last_price) / ($this->lot->start_price / 100) <= 0 ? 0 : ($this->lot->start_price - $this->last_price) / ($this->lot->start_price / 100);
  288. return round($delta_perc, 2);
  289. }
  290. },
  291. 'last_user',
  292. 'next_price' => function()
  293. {
  294. //$next_price = $this->last_price + ($this->lot->start_price / 100 * $this->lot->step);
  295. //return round($next_price, 2);
  296. if($this->type_id==1) {
  297. if($this->last_price=="0")
  298. {
  299. $next_price = $this->lot->start_price + ($this->lot->start_price / 100 * $this->lot->step);
  300. return round($next_price, 2); }
  301. //return round($this->lot->start_price, 2);}
  302. else
  303. $next_price = $this->last_price + ($this->lot->start_price / 100 * $this->lot->step);
  304. return round($next_price, 2);
  305. }
  306. if($this->type_id==2) {
  307. if ($this->last_price == "0" && $this->last_user == "0" && $this->temp_step_down != "0") {
  308. return round($this->lot->start_price, 2);
  309. }
  310. elseif ($this->last_user == "0")
  311. {
  312. $next_price = $this->last_price; //- ($this->lot->start_price / 100 * $this->lot->step);
  313. return round($next_price, 2);
  314. }
  315. else
  316. {
  317. $next_price = $this->last_price + ($this->lot->start_price / 100 * $this->lot->step);
  318. return round($next_price, 2);
  319. }
  320. }
  321. },
  322. 'time' => function()
  323. {
  324. $diff = $this->SendTime($this->last_date, $this->date_stop, $this->date_start); // $this->date_start for
  325. if($diff > 0)
  326. {
  327. return $diff;
  328. }
  329. else
  330. {
  331. return 0;
  332. }
  333. },
  334. //'diff' => function()
  335. //{
  336. // return strtotime($this->date_stop) - strtotime($this->last_date);
  337. //},
  338. 'perc' => function()
  339. {
  340. $diff = $this->SendPerc($this->last_date,$this->date_stop, $this->date_start);
  341. if($diff > 0)
  342. {
  343. return $diff;
  344. }
  345. else
  346. {
  347. return 0;
  348. }
  349. },
  350. 'last_price',
  351. 'status',
  352. 'current_time' => function($model){
  353. return time();
  354. },
  355. ];
  356. }
  357. public function extraFields()
  358. {
  359. return [
  360. //'date_start',
  361. //'date_stop',
  362. ];
  363. }
  364. public static function statusNames(){
  365. return [
  366. '0' => Yii::t('app','pMakeBidding ID'),
  367. '1' => Yii::t('app','pWaitAuction ID'),
  368. '2' => Yii::t('app','pMakeAuction ID'),
  369. '3' => Yii::t('app','pMakeFinal ID'),
  370. '4' => Yii::t('app','pEndTorg ID')
  371. ];
  372. }
  373. }