Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Bid an existing Products model.
- * If bid is successful, the browser will be redirected to the 'viewBid' page.
- * @param string $memberId
- * @param string $productId
- * @return mixed
- */
- public function actionBidProduct($memberId, $productId)
- {
- $memberId = Yii::$app->user->identity->username;
- $products = Products::findOne($productId);
- $productsPicture = Productspicture::find()->where(['Products_id' => $productId])->all();
- $bidding = Bidding::findOne($productId);
- $shippingIds = $products->getProductsHasShipping()->all();
- $shippings = [];
- foreach($shippingIds as $shippingId){
- array_push($shippings, Shipping::findOne($shippingId->Shipping_id));
- }
- $newBid = new Bid(['scenario' => 'bid']);
- if($newBid->load(Yii::$app->request->post()))
- {
- $newBid->Member_username = $memberId;
- $newBid->Products_id = $productId;
- if($newBid->maxPrice > $bidding->minValue){ //Check new bid higher than old value
- if($newBid->validate()){
- $oldBid = Bid::find()->where(['Products_id' => $productId])->orderBy(['maxPrice' => SORT_DESC])->one();
- if($oldBid == null){ //First time bid
- $newBid->isWinner = true;
- if($newBid->spread < $bidding->minSpread){
- $newBid->spread = $bidding->minSpread;
- }
- while($newBid->price - $bidding->minValue < 0.01 || $newBid->price < $bidding->minValue){
- $newBid->price = $newBid->price + $newBid->spread;
- }
- if($newBid->price > $newBid->maxPrice){
- $newBid->price = $newBid->maxPrice;
- }
- $bidding->minValue = $newBid->price;
- $newBid->save();
- $bidding->update();
- return $this->render('viewBid', array(
- 'newBid' => $newBid,
- ));
- }else{
- if($newBid->maxPrice > $oldBid->maxPrice){ //New bid won
- $newBid->isWinner = true;
- $oldBid->price = $oldBid->maxPrice;
- $oldBid->isWinner = false;
- if($newBid->spread < $bidding->minSpread){
- $newBid->spread = $bidding->minSpread;
- }
- while($newBid->price - $oldBid->maxPrice < 0.001 || $newBid->price < $oldBid->maxPrice){
- $newBid->price = $newBid->price + $newBid->spread;
- }
- if($newBid->price > $newBid->maxPrice){
- $newBid->price = $newBid->maxPrice;
- }
- $bidding->minValue = $newBid->price;
- $newBid->save();
- $oldBid->update();
- $bidding->update();
- return $this->render('viewBid', array(
- 'newBid' => $newBid,
- ));
- }else{ //Old bid won
- $newBid->isWinner = false;
- $newBid->price = $newBid->maxPrice;
- if($oldBid->spread < $bidding->minSpread){
- $oldBid->spread = $bidding->minSpread;
- }
- while($oldBid->price - $newBid->maxPrice < 0.01 || $oldBid->price < $newBid->maxPrice){
- $oldBid->price = $oldBid->price + $oldBid->spread;
- }
- if($oldBid->price > $oldBid->maxPrice){
- $oldBid->price = $oldBid->maxPrice;
- }
- $bidding->minValue = $oldBid->price;
- $newBid->save();
- $oldBid->update();
- $bidding->update();
- return $this->render('viewBid', array(
- 'newBid' => $newBid,
- ));
- }
- }
- }
- }
- }
- return $this->render('bid', array(
- 'newBid' => $newBid,
- ));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement