/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // validate request $this->validate($request, ['date' => 'required|date_format:d/m/Y', 'account_from' => 'required|integer|min:1|different:account_to', 'account_to' => 'required|integer|min:1', 'amount' => 'required|numeric|min:0.01', 'comment' => 'max:255']); // check if accounts exists $account_from = User::find(Auth::user()->id)->accounts->find($request->account_from); $account_to = User::find(Auth::user()->id)->accounts->find($request->account_to); if ($account_from === null || $account_to === null) { // stuff to pass into view $title = "Error"; $errmsg = "The account does not exist."; return view('errors.error', compact('errmsg', 'title', 'heading')); } // start new transaction DB::transaction(function () use($request, $account_from, $account_to) { // create new transfer record $transfer = new Transfer(); // format date $date = date('Y-m-d', strtotime(str_replace('/', '-', $request->date))); // assign data $transfer->user_id = Auth::user()->id; $transfer->date = $date; $transfer->account_from = $request->account_from; $transfer->account_to = $request->account_to; $transfer->amount = $request->amount; $transfer->comment = $request->comment; // save transfer $transfer->save(); // update acounts balances $account_from = User::find(Auth::user()->id)->accounts->find($request->account_from); $account_to = User::find(Auth::user()->id)->accounts->find($request->account_to); $account_from->balance = round(($account_from->balance * 100 - $request->amount * 100) / 100, 2); $account_to->balance = round(($account_to->balance * 100 + $request->amount * 100) / 100, 2); $account_from->save(); $account_to->save(); }); // flash message session()->flash('flash_message', 'Transfer executed successfull.'); // redirect to transfers list return redirect()->route('transfers.index'); }
public function postIndex() { $user = Auth::user(); $id = Input::get('payment'); /* *There is an invisible field called form that tells what form was submitted * */ if (Input::get('form') == 'trans') { $monthNum = substr(Input::get('date'), 0, 2); $month = date('M', mktime(0, 0, 0, $monthNum, 10)); $year = substr(Input::get('date'), -4); $transaction = new Transaction(); $transaction->userID = $user->id; $transaction->date = Input::get('date'); $transaction->amount = Input::get('amount'); $transaction->typeID = Input::get('type'); $transaction->note = Input::get('note'); $transaction->year = $year; $transaction->month = $month; if ($id == 'cash') { $month = Month::where('userID', $user->id)->where('name', $month)->where('year', date("Y"))->first(); $month->cash -= Input::get('amount'); $month->save(); $transaction->accountID = 0; } else { $transaction->accountID = $id; $account = Account::find($id); //add for credit subtract for bank if ($account->accountType == 'c') { $account->balance += Input::get('amount'); } else { $account->balance -= Input::get('amount'); } $account->save(); } $transaction->save(); return redirect('options')->with('message', 'Transaction added successfully.'); } else { if (Input::get('form') == 'type') { $type = new Type(); $type->userID = $user->id; $type->name = Input::get('name'); $type->save(); return redirect('options')->with('message', 'Category added successfully.'); } else { if (Input::get('form') == 'payment') { $monthNum = substr(Input::get('date'), 0, 2); $month = date('M', mktime(0, 0, 0, $monthNum, 10)); $year = substr(Input::get('date'), -4); $amount = Input::get('amount'); $bankID = Input::get('bank'); $ccID = Input::get('payment'); $note = Input::get('note'); $date = Input::get('date'); if ($ccID == 'cash') { $month2 = Month::where('userID', $user->id)->where('name', $month)->first(); $month2->cash = $month2->cash + $amount; $month2->save(); if ($bankID == 'cash') { $month2 = Month::where('userID', $user->id)->where('name', $month)->first(); $month2->cash = $month2->cash - $amount; $month2->save(); } else { $bank = Account::find($bankID); $bank->balance = $bank->balance - $amount; $bank->save(); } $transfer = new Transfer(); $transfer->userID = $user->id; $transfer->creditAccountID = $bankID; $transfer->debitAccountID = 0; $transfer->amount = $amount; $transfer->note = $note; $transfer->date = $date; $transfer->year = $year; $transfer->month = $month; $transfer->save(); } else { $cc = Account::find($ccID); if ($bankID == 'cash') { $month2 = Month::where('userID', $user->id)->where('name', $month)->first(); $month2->cash = $month2->cash - $amount; $month2->save(); } else { $bank = Account::find($bankID); $bank->balance = $bank->balance - $amount; $bank->save(); } if ($cc->accountType == 'b') { //transfer $cc->balance = $cc->balance + $amount; $transfer = new Transfer(); $transfer->userID = $user->id; $transfer->creditAccountID = $bankID; $transfer->debitAccountID = $ccID; $transfer->amount = $amount; $transfer->note = $note; $transfer->date = $date; $transfer->year = $year; $transfer->month = $month; $transfer->save(); } else { //payment $cc->balance = $cc->balance - $amount; $payment = new Payment(); $payment->userID = $user->id; $payment->creditAccountID = $bankID; $payment->debitAccountID = $ccID; $payment->amount = $amount; $payment->note = $note; $payment->date = $date; $payment->year = $year; $payment->month = $month; $payment->save(); } $cc->save(); } return redirect('options')->with('message', 'Payment saved successfully.'); } else { if (Input::get('form') == 'cc') { $cc = new Account(); $cc->userID = $user->id; $cc->name = Input::get('name'); $cc->balance = Input::get('balance'); $cc->creditLimit = Input::get('limit'); $cc->statementDay = Input::get('date'); $cc->accountType = 'c'; $cc->save(); return redirect('options')->with('message', 'Credit Card added successfully.'); } else { if (Input::get('form') == 'bank') { $bank = new Account(); $bank->userID = $user->id; $bank->name = Input::get('name'); $bank->balance = Input::get('balance'); $bank->accountType = 'b'; $bank->save(); return redirect('options')->with('message', 'Bank Account added successfully.'); } else { if (Input::get('form') == 'income') { $date = Input::get('date'); $monthNum = substr($date, 0, 2); $month = date('M', mktime(0, 0, 0, $monthNum, 10)); $amount = Input::get('amount'); $bankID = Input::get('bank'); $year = substr(Input::get('date'), -4); $income = new Income(); $income->userID = $user->id; $income->month = $month; $income->amount = $amount; $income->note = Input::get('note'); $income->date = $date; $income->year = $year; if ($bankID == "cash") { $income->accountID = 0; $month = Month::where('userID', $user->id)->where('name', $month)->first(); $month->cash = $month->cash + $amount; $month->save(); } else { $income->accountID = $bankID; $bank = Account::find($bankID); $bank->balance = $bank->balance + $amount; $bank->save(); } $income->save(); return redirect('options')->with('message', 'Income added successfully.'); } } } } } } }
public function paymentPostIndex($type, $id) { $user = Auth::user(); $date = Input::get('date'); $amount = Input::get('amount'); $bankID = Input::get('bank'); $accountID = Input::get('payment'); $note = Input::get('note'); //add intial trans back onto if ($type == 0) { //tansfer $trans = Transfer::where('id', $id)->where('userID', $user->id)->first(); if ($trans->creditAccountID == 0) { $month = Month::where('userID', $user->id)->where('name', date('M'))->first(); $month->cash = $month->cash + $trans->amount; $month->save(); } else { $debit = Account::find($trans->creditAccountID); $debit->balance = $debit->balance + $trans->amount; $debit->save(); } if ($trans->debitAccountID == 0) { $month = Month::where('userID', $user->id)->where('name', date('M'))->first(); $month->cash = $month->cash - $trans->amount; $month->save(); } else { $debit = Account::find($trans->debitAccountID); $debit->balance = $debit->balance + $trans->amount; $debit->save(); } } else { //payment $trans = Payment::where('id', $id)->where('userID', $user->id)->first(); //add initial payment amount back on if ($trans->creditAccountID == 0) { $month = Month::where('userID', $user->id)->where('name', date('M'))->first(); $month->cash = $month->cash + $trans->amount; $month->save(); } else { $credit = Account::find($trans->creditAccountID); $credit->balance = $credit->balance + $trans->amount; $credit->save(); } //add initial payment back on $debit = Account::find($trans->debitAccountID); $debit->balance = $debit->balance + $trans->amount; $debit->save(); } $trans->forceDelete(); //setup as new transfer/payment $monthNum = substr(Input::get('date'), 0, 2); $month = date('M', mktime(0, 0, 0, $monthNum, 10)); if ($accountID == 'cash') { $month2 = Month::where('userID', $user->id)->where('name', $month)->first(); $month2->cash = $month2->cash + $amount; $month2->save(); if ($bankID == 'cash') { $month2 = Month::where('userID', $user->id)->where('name', $month)->first(); $month2->cash = $month2->cash - $amount; $month2->save(); } else { $bank = Account::find($bankID); $bank->balance = $bank->balance - $amount; $bank->save(); } $transfer = new Transfer(); $transfer->userID = $user->id; $transfer->creditAccountID = $bankID; $transfer->debitAccountID = 0; $transfer->amount = $amount; $transfer->note = $note; $transfer->date = $date; $transfer->month = $month; $transfer->save(); } else { $cc = Account::find($accountID); if ($bankID == 'cash') { $month2 = Month::where('userID', $user->id)->where('name', $month)->first(); $month2->cash = $month2->cash - $amount; $month2->save(); } else { $bank = Account::find($bankID); $bank->balance = $bank->balance - $amount; $bank->save(); } if ($cc->accountType == 'b') { //transfer $cc->balance = $cc->balance + $amount; $transfer = new Transfer(); $transfer->userID = $user->id; $transfer->creditAccountID = $bankID; $transfer->debitAccountID = $accountID; $transfer->amount = $amount; $transfer->note = $note; $transfer->date = $date; $transfer->month = $month; $transfer->save(); } else { //payment $cc->balance = $cc->balance - $amount; $payment = new Payment(); $payment->userID = $user->id; $payment->creditAccountID = $bankID; $payment->debitAccountID = $accountID; $payment->amount = $amount; $payment->note = $note; $payment->date = $date; $payment->month = $month; $payment->save(); } $cc->save(); } return redirect('home')->with('message', 'Transfer/Payment updated successfully.'); }