/** * Enregistre un nouveau paris * * @return Response */ public function store() { $user = User::getUserWithToken($_GET['token']); $input = Input::all(); $input['user_id'] = $user->id; $validator = Validator::make($input, Bet::$rules, BaseController::$messages); if ($validator->fails()) { return Response::json(array('success' => false, 'payload' => array(), 'error' => $this->errorsArraytoString($validator->messages())), 400); } //On vérifie si la date du match n'est pas dépassé if (new DateTime() > new DateTime(Game::find($input['game_id'])->date)) { return Response::json(array('success' => false, 'payload' => array(), 'error' => "Le date du match est dépassé !"), 400); } $bet = Bet::whereRaw('user_id = ? && game_id = ?', array($input['user_id'], $input['game_id']))->first(); //Si un paris sur le même match pour cet utilisateur existe, erreur envoyée. if ($bet) { return Response::json(array('success' => false, 'payload' => array(), 'error' => "Un paris existe déjà sur ce match !"), 400); } //On vérifie si la somme misé est disponible if ($input['points'] > $user->points) { return Response::json(array('success' => false, 'payload' => array(), 'error' => "Vous avez miser plus de points que vous en avez !"), 400); } $game = Game::find($input['game_id']); //On vérifie si le winner est bien une équipe du match if ($input['winner_id'] != $game->team1_id && $input['winner_id'] != $game->team2_id) { return Response::json(array('success' => false, 'payload' => array(), 'error' => "Veuillez mettre une équipe du match !"), 400); } $bet = Bet::create($input); Transaction::addTransaction($input['user_id'], $bet->id, $input['points'], 'bet'); return Response::json(array('success' => true, 'payload' => $bet->toArray(), 'message' => 'Pari enregistré (' . $bet->points . ' points) sur : ' . $game->team1->name . ' (' . $bet->team1_goals . ') - (' . $bet->team2_goals . ') ' . $game->team2->name)); }
public function setFinished($num_team) { //Si l'équipe une a gagnée, on redistribue les points pour les paris corrects (paris sur l'équipe une) if ($num_team == 1) { foreach (Bet::whereRaw('game_id = ? && winner_id = ?', array($this->id, $this->team1_id))->get() as $bet) { $cote = $this->getTeam1CoteAttribute(); $points = $bet->points * $cote; if ($this->team1_goals == $bet->team1_goals && $this->team2_goals == $bet->team2_goals) { $points += $bet->points / 10 * $cote; } Transaction::addTransaction($bet->user_id, $bet->id, $points, 'gain'); } $this->winner_id = $this->team1_id; //Si l'équipe deux a gagnée, on redistribue les points pour les paris corrects (paris sur l'équipe deux) } else { foreach (Bet::whereRaw('game_id = ? && winner_id = ?', array($this->id, $this->team2_id))->get() as $bet) { $cote = $this->getTeam2CoteAttribute(); $points = $bet->points * $cote; if ($this->team1_goals == $bet->team1_goals && $this->team2_goals == $bet->team2_goals) { $points += $bet->points / 10 * $cote; } Transaction::addTransaction($bet->user_id, $bet->id, $points, 'gain'); } $this->winner_id = $this->team2_id; } ///////////////////////////////////////////////// //******************* ROUND X *****************// ///////////////////////////////////////////////// //On inscrit l'équipe gagnante dans son prochain match $id = $this->stage()->first()->next_stage()->first()->id; $num_game = round($this->stage_game_num / 2); $game = Game::whereRaw("stage_id = ? && stage_game_num = ?", array($id, $num_game))->first(); if ($this->stage_game_num % 2 == 1) { $game->team1_id = $this->winner_id; } else { $game->team2_id = $this->winner_id; } $game->save(); ///////////////////////////////////////////////// //******************* 3e place ****************// ///////////////////////////////////////////////// //Si on est lors des demi, on va définir aussi la 3e finale if ($this->stage()->first()->next_stage()->first()->next_stage == null) { $stage_third = Stage::getThirdStage()->id; $gamme_third = Game::whereRaw('stage_id = ?', array($stage_third))->first(); //Si équipe 1 a gagné on met l'équipe 2 en 3e place if ($num_game == 1) { if ($this->stage_game_num % 2 == 1) { $gamme_third->team1_id = $this->team1_id; } else { $gamme_third->team2_id = $this->team1_id; } } else { if ($this->stage_game_num % 2 == 1) { $gamme_third->team1_id = $this->team2_id; } else { $gamme_third->team2_id = $this->team2_id; } } $gamme_third->save(); } $this->save(); }
public function transaction() { $this->getLoader()->library('Transaction'); $customer = CustomerDAO::getInstance()->getCustomer($this->parameters['customerId']); if ($this->request->server['REQUEST_METHOD'] == 'POST') { if ($this->user->hasPermission('modify', 'sale/customer')) { if ($this->request->post['action'] == 'add') { if ($this->request->post['amount'] < 0) { Transaction::addCredit($this->parameters['customerId'], -$this->request->post['amount'], $customer['base_currency_code'], $this->registry, $this->request->post['description']); $this->data['success'] = $this->language->get('SUCCESS_CREDIT_ADDED'); } elseif ($this->request->post['amount'] > 0) { Transaction::addTransaction(0, $this->parameters['customerId'], $this->request->post['amount'], $customer['base_currency_code'], $this->request->post['description']); $this->data['success'] = $this->language->get('SUCCESS_PAYMENT_ADDED'); } } elseif ($this->request->post['action'] == 'delete') { $modelSaleTransaction = $this->getLoader()->model('sale/transaction'); $transaction = $modelSaleTransaction->getTransaction($this->request->post['transactionId']); if ($transaction['invoice_id'] != 0) { $this->data['error_warning'] = $this->language->get('ERROR_RELATED_INVOICE_EXISTS'); } elseif ($transaction['currency_code'] != $customer['base_currency_code']) { $this->data['error_warning'] = $this->language->get('ERROR_TRANSACTION_AND_CUSTOMER_CURRENCY_DONT_MATCH'); } else { Transaction::deleteTransaction($this->request->post['transactionId']); $this->data['success'] = sprintf($this->language->get('SUCCESS_TRANSACTION_DELETED'), $this->request->post['transactionId']); } } $customer = CustomerDAO::getInstance()->getCustomer($this->parameters['customerId']); } else { $this->data['error_warning'] = $this->language->get('error_permission'); } } if ($this->request->server['REQUEST_METHOD'] == 'POST' && !$this->user->hasPermission('modify', 'sale/customer')) { $this->data['error_warning'] = $this->language->get('error_permission'); } $this->data['text_no_results'] = $this->language->get('text_no_results'); $this->data['text_balance'] = $this->language->get('text_balance'); $this->data['column_date_added'] = $this->language->get('column_date_added'); $this->data['column_description'] = $this->language->get('column_description'); $this->data['column_amount'] = $this->language->get('column_amount'); $this->data['textAction'] = $this->language->get('ACTIONS'); $this->data['textInvoiceId'] = $this->language->get('INVOICE_ID'); $this->data['textTransactionId'] = $this->language->get('TRANSACTION_ID'); $this->getTransactions($customer); $this->getCreditRequests($customer); $this->getResponse()->setOutput($this->render('sale/customerTransaction.tpl.php')); return; if (isset($_REQUEST['page'])) { $page = $_REQUEST['page']; } else { $page = 1; } $this->data['transactions'] = array(); $results = CustomerDAO::getInstance()->getTransactions($_REQUEST['customer_id'], ($page - 1) * 10, 10); foreach ($results as $result) { $actions = array(); $actions[] = array('text' => $this->language->get('DELETE'), 'onclick' => "deleteTransaction(" . $result['customer_transaction_id'] . ");"); $this->data['transactions'][] = array('transactionId' => $result['customer_transaction_id'], 'actions' => $actions, 'amount' => $this->currency->format($result['amount'], $result['currency_code'], 1), 'description' => $result['description'], 'date_added' => $result['date_added'], 'invoiceId' => $result['invoice_id'] ? $result['invoice_id'] : '', 'invoiceUrl' => $this->url->link('sale/invoice/showForm', 'invoiceId=' . $result['invoice_id'] . '&token=' . $this->session->data['token'], 'SSL')); } $this->data['balance'] = $this->currency->format(CustomerDAO::getInstance()->getCustomerBalance($_REQUEST['customer_id']), $customer['base_currency_code'], 1); $transaction_total = CustomerDAO::getInstance()->getTotalTransactions($_REQUEST['customer_id']); $pagination = new Pagination(); $pagination->total = $transaction_total; $pagination->page = $page; $pagination->limit = 10; $pagination->text = $this->language->get('text_pagination'); $pagination->url = $this->url->link('sale/customer/transaction', 'token=' . $this->session->data['token'] . '&customer_id=' . $_REQUEST['customer_id'] . '&page={page}', 'SSL'); $this->data['pagination'] = $pagination->render(); $this->template = 'sale/customer_transaction.tpl'; $this->getResponse()->setOutput($this->render()); }
public static function deleteTransaction($transactionId) { $transaction = Transaction::$instance->getTransaction($transactionId); // Transaction::$instance->modelTransaction->deleteTransaction($transactionId); Transaction::$instance->addTransaction($transaction['invoice_id'], $transaction['customer_id'], -$transaction['amount'], $transaction['currency_code'], 'Cancellation of transaction #' . (int) $transactionId); }