Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. /**
  2. * Bid an existing Products model.
  3. * If bid is successful, the browser will be redirected to the 'viewBid' page.
  4. * @param string $memberId
  5. * @param string $productId
  6. * @return mixed
  7. */
  8. public function actionBidProduct($memberId, $productId)
  9. {
  10. $memberId = Yii::$app->user->identity->username;
  11. $products = Products::findOne($productId);
  12. $productsPicture = Productspicture::find()->where(['Products_id' => $productId])->all();
  13. $bidding = Bidding::findOne($productId);
  14. $shippingIds = $products->getProductsHasShipping()->all();
  15. $shippings = [];
  16. foreach($shippingIds as $shippingId){
  17. array_push($shippings, Shipping::findOne($shippingId->Shipping_id));
  18. }
  19. $newBid = new Bid(['scenario' => 'bid']);
  20. if($newBid->load(Yii::$app->request->post()))
  21. {
  22. $newBid->Member_username = $memberId;
  23. $newBid->Products_id = $productId;
  24. if($newBid->maxPrice > $bidding->minValue){ //Check new bid higher than old value
  25. if($newBid->validate()){
  26. $oldBid = Bid::find()->where(['Products_id' => $productId])->orderBy(['maxPrice' => SORT_DESC])->one();
  27. if($oldBid == null){ //First time bid
  28. $newBid->isWinner = true;
  29. if($newBid->spread < $bidding->minSpread){
  30. $newBid->spread = $bidding->minSpread;
  31. }
  32. while($newBid->price - $bidding->minValue < 0.01 || $newBid->price < $bidding->minValue){
  33. $newBid->price = $newBid->price + $newBid->spread;
  34. }
  35. if($newBid->price > $newBid->maxPrice){
  36. $newBid->price = $newBid->maxPrice;
  37. }
  38. $bidding->minValue = $newBid->price;
  39. $newBid->save();
  40. $bidding->update();
  41. return $this->render('viewBid', array(
  42. 'newBid' => $newBid,
  43. ));
  44. }else{
  45. if($newBid->maxPrice > $oldBid->maxPrice){ //New bid won
  46. $newBid->isWinner = true;
  47. $oldBid->price = $oldBid->maxPrice;
  48. $oldBid->isWinner = false;
  49. if($newBid->spread < $bidding->minSpread){
  50. $newBid->spread = $bidding->minSpread;
  51. }
  52. while($newBid->price - $oldBid->maxPrice < 0.001 || $newBid->price < $oldBid->maxPrice){
  53. $newBid->price = $newBid->price + $newBid->spread;
  54. }
  55. if($newBid->price > $newBid->maxPrice){
  56. $newBid->price = $newBid->maxPrice;
  57. }
  58. $bidding->minValue = $newBid->price;
  59. $newBid->save();
  60. $oldBid->update();
  61. $bidding->update();
  62. return $this->render('viewBid', array(
  63. 'newBid' => $newBid,
  64. ));
  65. }else{ //Old bid won
  66. $newBid->isWinner = false;
  67. $newBid->price = $newBid->maxPrice;
  68. if($oldBid->spread < $bidding->minSpread){
  69. $oldBid->spread = $bidding->minSpread;
  70. }
  71. while($oldBid->price - $newBid->maxPrice < 0.01 || $oldBid->price < $newBid->maxPrice){
  72. $oldBid->price = $oldBid->price + $oldBid->spread;
  73. }
  74. if($oldBid->price > $oldBid->maxPrice){
  75. $oldBid->price = $oldBid->maxPrice;
  76. }
  77. $bidding->minValue = $oldBid->price;
  78. $newBid->save();
  79. $oldBid->update();
  80. $bidding->update();
  81. return $this->render('viewBid', array(
  82. 'newBid' => $newBid,
  83. ));
  84. }
  85. }
  86. }
  87. }
  88. }
  89. return $this->render('bid', array(
  90. 'newBid' => $newBid,
  91. ));
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement