public function create() { $response = array('success' => false); if (Request::ajax()) { $postData = Input::all(); $tabletId = $postData['tabletId']; $economyValue = (double) $postData['economyValue']; $rules = array('tabletId' => array('required', 'not_in:0', 'exists:tablets,id'), 'economyValue' => array('required', 'numeric', 'min:0')); $messages = array('tabletId.required' => 'Please select a tablet.', 'tabletId.not_in' => 'Please select a tablet.', 'tabletId.exists' => 'An error occured. Please try later.', 'economyValue.required' => 'Please enter savings value.', 'economyValue.numeric' => 'Savings must be a numeric value. Ex: 90, 3.42', 'economyValue.min' => 'Savings must have a positive value.'); $validator = Validator::make($postData, $rules, $messages); if ($validator->fails()) { $messages = $validator->messages(); $response['tabletMsg'] = $messages->first('tabletId'); $response['economyValueMsg'] = $messages->first('economyValue'); return Response::json($response); } $tablet = Tablet::find($tabletId); if ($tablet->id) { $initialCurrentSum = $tablet->current_sum; $tablet->current_sum = $initialCurrentSum - $economyValue; $initialEconomies = $tablet->economies; $tablet->economies = $initialEconomies + $economyValue; $tablet->save(); $response['success'] = true; Event::fire('economy.create.success', array($economyValue, $tabletId)); } } return Response::json($response); }
/** * Tablet close success page * @return string */ public function closeSuccess() { if (Session::has('closedTabletId')) { $closedTabletId = Session::get('closedTabletId'); $tablet = Tablet::find($closedTabletId); return View::make('tablet/close_success', array('tablet' => $tablet)); } return Redirect::to('dashboard'); }
/** * Delete prediction action * @return string */ public function delete() { $response = array('success' => false); if (Request::ajax()) { $predictionIds = Input::get('predictionIds'); $tabletId = null; if ($predictionIds) { $predictionIdsArray = $this->_getPredictionIdsArray($predictionIds); foreach ($predictionIdsArray as $predictionId) { $prediction = Prediction::find($predictionId); if (!$tabletId) { $tablet = Tablet::find($prediction->tablet_id); } $predictionExpenses = $prediction->expenses; foreach ($predictionExpenses as $expense) { $tablet->total_expenses = $tablet->total_expenses - $expense->value; $tablet->current_sum = $tablet->current_sum + $expense->value; } $tablet->save(); } Prediction::destroy($predictionIdsArray); $response['success'] = true; } } return Response::json($response); }
if ($prediction->id) { $tablet = $prediction->tablet; $tablet->current_sum = $tablet->current_sum - $expense->value; $tablet->save(); } }); /** * Update Prediction value upon expense.create.success */ Event::listen('expense.create.success', function ($expense) { $predictionId = $expense->prediction_id; $prediction = Prediction::find($predictionId); if ($prediction->id) { $currentValue = (double) $prediction->value; $expenseValue = (double) $expense->value; $prediction->value = $currentValue - $expenseValue; $prediction->save(); } }); /** * Update current_sum on Tablet upon income.create.success */ Event::listen('income.create.success', function ($incomeValue, $tabletId) { $tablet = Tablet::find($tabletId); if ($tablet->id) { $oldCurrentSum = $tablet->current_sum; $newCurrentSum = $oldCurrentSum + $incomeValue; $tablet->current_sum = $newCurrentSum; $tablet->save(); } });