public function store(CreateInvoiceRequest $request) { $user_id = Session::get('user_id'); $user = User::where('id', $user_id)->first(); $invoice = $user->invoice()->create($request->all()); $user_balance = $user->balance; if ($user->balance != 0) { session()->flash('flash_message', 'Your invoice has been created successfully! The amount minus user balance is: ' . ($request->amount - $user->balance)); $user->balance = 0; if ($user->save()) { $discount_invoice = new App\Invoice(); $discount_invoice->name = 'Paid from your balance for ' . $request->name; $discount_invoice->amount = $user_balance * -1; $discount_invoice->user()->associate($user); $discount_invoice->save(); } } else { session()->flash('flash_message', 'Your invoice has been created successfully!'); } $first_root_id = $user->root_id; if ($first_root_id !== null) { $first_root = User::where('id', $first_root_id)->first(); $commission_amount = $request->amount * 0.1; $commission = new App\Commission(); $commission->user()->associate($first_root); $commission->invoice()->associate($invoice); $commission->amount = $commission_amount; $commission->save(); $first_root->balance += $commission_amount; $first_root->save(); if ($first_root->root_id !== null) { $second_root = User::where('id', $first_root->root_id)->first(); $commission_amount = $request->amount * 0.05; $commission = new App\Commission(); $commission->invoice()->associate($invoice); $commission->user()->associate($second_root); $commission->amount = $commission_amount; $commission->save(); $second_root->balance += $commission_amount; $second_root->save(); if ($second_root->root_id !== null) { $third_root = User::where('id', $second_root->root_id)->first(); $commission_amount = $request->amount * 0.02; $commission = new App\Commission(); $commission->invoice()->associate($invoice); $commission->user()->associate($third_root); $commission->amount = $commission_amount; $commission->save(); $third_root->balance += $commission_amount; $third_root->save(); } } } $invoices = Invoice::latest()->get()->take(5); return view('admin', compact('invoices')); }
public function store() { // validate // read more on validation at http://laravel.com/docs/validation $rules = array(); $validator = Validator::make(Input::all(), $rules); // process the login if ($validator->fails()) { return Redirect::to('tariff')->withErrors($validator); } else { // store $commission = new Commission(); $commission->name = Input::get('name'); $commission->tariff_id = Input::get('tariff_id'); $commission->type = Input::get('commission_type'); if (null !== Input::get('nds')) { $commission->nds = Input::get('nds'); } if (null !== Input::get('deduction')) { $commission->deduction = Input::get('deduction'); } if (null !== Input::get('payer')) { $commission->payer = Input::get('payer'); } if (null !== Input::get('additional_sum')) { $commission->additional_sum = Input::get('additional_sum'); } if (null !== Input::get('rate_stitching')) { $commission->rate_stitching = Input::get('rate_stitching'); } if (null !== Input::get('time_of_settlement')) { $commission->time_of_settlement = Input::get('time_of_settlement'); } if (null !== Input::get('commission_value')) { $commission->commission_value = Input::get('commission_value'); } $commission->save(); if (null !== Input::get('range_commission_value')) { foreach (Input::get('range_commission_value') as $key => $range_commission_value) { $commissions_rage = new CommissionsRage(); if ('' !== Input::get('range_commission_min')[$key]) { $commissions_rage->min = Input::get('range_commission_min')[$key]; } if ('' !== Input::get('range_commission_max')[$key]) { $commissions_rage->max = Input::get('range_commission_max')[$key]; } $commissions_rage->value = Input::get('range_commission_value')[$key]; $commissions_rage->commission_id = $commission->id; $commissions_rage->save(); } } // redirect return response()->json($commission->tariff_id); } }