Beispiel #1
0
 public function makeTransaction($amount, $description, $txid = NULL, $status = 1)
 {
     //status: 0 = error 1 = success 2 = pending
     $transaction = new Transaction();
     if ($txid == NULL) {
         // generating txid
         $txid = "";
         $seed = str_split('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');
         while (true) {
             shuffle($seed);
             foreach (array_rand($seed, 9) as $k) {
                 $txid .= $seed[$k];
             }
             try {
                 $t = Transaction::findorfail(array('txid', $txid));
             } catch (ModelNotFoundException $e) {
                 break;
             }
         }
     }
     // insert transaction record
     $transaction->txid = $txid;
     $transaction->user_id = $this->id;
     $transaction->amount = $amount;
     $transaction->description = $description;
     $transaction->status = $status;
     // process yeah
     $transaction->save();
 }
 public function makeTransaction($keyID, $typeName, $customAmount = "0")
 {
     if (!($user = User::byKey($keyID))) {
         return "false";
     }
     $transType = TransactionType::byName($typeName);
     if (is_null($transType->cost)) {
         if ($customAmount > 0) {
             $cost = $customAmount;
         } else {
             $cost = 0;
         }
     } else {
         $cost = $transType->cost;
     }
     if (is_null($transType->permission) || $user->has($transType->permission)) {
         if ($transType->cost <= $user->balance) {
             if ($cost > 0) {
                 $trans = new Transaction();
                 $trans->transaction_type_id = $transType->id;
                 $trans->user_id = $user->id;
                 $trans->amount = '-' . $cost;
                 if ($trans->save()) {
                     return "true";
                 }
             }
         }
     }
     return \Illuminate\Http\Response::create('false', 418);
 }
 /**
  * Creates a new Transaction model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Transaction();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
 public static function create()
 {
     $login = Yii::$app->getSession()->get('payer.login');
     $accountType = Yii::$app->getSession()->get('payer.account_type');
     $payAmount = Yii::$app->request->get('pay_amount');
     $transaction = new Transaction();
     $transaction->pay_id = uniqid(uniqid());
     $transaction->is_fulfilled = false;
     $transaction->login = $login;
     $transaction->pay_amount = $payAmount;
     $transaction->account_type = $accountType;
     $transaction->save();
     return $transaction;
 }
 /**
  * This function is for when a user donates money to another user via their profile.
  *
  * @param Request $request
  */
 public function processGift(Request $request)
 {
     $giftTransactionType = TransactionType::where(['name' => 'user_gift'])->get()->first();
     $recievingUser = User::find($request->input('user_id'));
     $sendingTransaction = new Transaction(['user_id' => $this->user->id, 'transaction_type_id' => $giftTransactionType->id, 'amount' => -$request->input('amount'), 'description' => "You sent money to " . $recievingUser->name]);
     $recievingTransaction = new Transaction(['user_id' => $request->input('user_id'), 'transaction_type_id' => $giftTransactionType->id, 'amount' => $request->input('amount'), 'description' => "You recieved a gift from " . $this->user->name]);
     if ($sendingTransaction->save()) {
         $recievingTransaction->save();
         Flash::success('You have sent ' . $recievingTransaction->present()->amount . ' to ' . $recievingTransaction->user->name);
     } else {
         Flash::error('There was an error, please contact support.');
     }
     return redirect('/');
 }
 /**
  * Todo: Do validations
  * POST /api/transactions
  * @param Request $request
  * @return Response
  */
 public function store(Request $request)
 {
     $transaction = new Transaction($request->only(['date', 'description', 'merchant', 'total', 'type', 'reconciled', 'minutes']));
     //Make sure total is negative for expense, negative for transfers from, and positive for income
     if ($transaction->type === 'expense' && $transaction->total > 0) {
         $transaction->total *= -1;
     } else {
         if ($transaction->type === 'income' && $transaction->total < 0) {
             $transaction->total *= -1;
         } else {
             if ($transaction->type === 'transfer' && $request->get('direction') === Transaction::DIRECTION_FROM) {
                 $transaction->total *= -1;
             }
         }
     }
     $transaction->account()->associate(Account::find($request->get('account_id')));
     $transaction->user()->associate(Auth::user());
     $transaction->save();
     if ($transaction->type !== 'transfer') {
         $this->budgetTransactionRepository->attachBudgetsWithDefaultAllocation($transaction, $request->get('budget_ids'));
     }
     //Fire event
     event(new TransactionWasCreated($transaction));
     $transaction = $this->transform($this->createItem($transaction, new TransactionTransformer()))['data'];
     return response($transaction, Response::HTTP_CREATED);
 }
 /**
  *
  * @param $user
  */
 private function createTransfer($user)
 {
     $from_account = Account::whereUserId($user->id)->offset(1)->first();
     $to_account = Account::whereUserId($user->id)->first();
     $date = Config::get('budgets.dateBeforeStartingDateForIncomeTransactions');
     $total = 100;
     $description = $this->faker->sentence(1);
     $transaction = new Transaction(['type' => 'transfer', 'date' => $date, 'account_id' => $from_account->id, 'description' => $description, 'merchant' => NULL, 'total' => $total * -1, 'reconciled' => 0, 'allocated' => 0, 'created_at' => $date]);
     $transaction->user()->associate($user);
     $transaction->save();
     $transaction = new Transaction(['type' => 'transfer', 'date' => $date, 'account_id' => $to_account->id, 'description' => $description, 'merchant' => NULL, 'total' => $total, 'reconciled' => 0, 'allocated' => 0, 'created_at' => $date]);
     $transaction->user()->associate($user);
     $transaction->save();
 }