public function doUpload() { $rules = array('uploadDate' => 'required', 'contMonth' => 'required', 'contYear' => 'required'); $validation = Validator::make(Input::all(), $rules); if ($validation->fails()) { return redirect('/UploadContributions')->with('error_message', $validation->errors()->first())->withInput(); } if (is_uploaded_file($_FILES['contributionsUpload']['tmp_name'])) { //Import uploaded file to Database $handle = fopen($_FILES['contributionsUpload']['tmp_name'], "r"); $num = 0; $uploadDate = Input::get('uploadDate'); $uploadMonth = Input::get('contMonth'); $uploadYear = Input::get('contYear'); //Explode date to array $inidate = explode('/', $uploadDate); //Check if contribution period exists if (DB::table('upload_history')->where('Contribution_Period', [$uploadMonth . ' ' . $uploadYear])->count() > 0) { return View::make('Operations.uploadContributions')->with('success_message', 'An upload has already been made for the specified contribution period.'); } //Insert contributions from uploaded file while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { if ($num > 0) { if ($this->registerStaff($data[0], $data[1])) { $cont = new Contributions(); $cont->Contribution_Period = $uploadMonth . ' ' . $uploadYear; $cont->Staff_ID = $data[0]; $cont->Contribution_Date = $inidate[2] . '-' . $inidate[0] . '-' . $inidate[1]; $cont->Employer_Contribution = $data[2]; $cont->Employee_Contribution = $data[3]; $cont->Created_By = Auth::user()->get_user_id(); $cont->save(); } else { return View::make('Operations.uploadContributions')->with('success_message', 'The upload failed to complete. Some staff details could not be identified. Please try again.'); } } $num++; } if (DB::table('contributions')->where('Contribution_Period', [$uploadMonth . ' ' . $uploadYear])->count() > 0) { $employer_contribution = DB::table('contributions')->where('contributions.Contribution_Period', [$uploadMonth . ' ' . $uploadYear])->sum('Employer_Contribution'); $employee_contribution = DB::table('contributions')->where('contributions.Contribution_Period', [$uploadMonth . ' ' . $uploadYear])->sum('Employee_Contribution'); $history = new UploadHistory(); $history->Contribution_Period = $uploadMonth . ' ' . $uploadYear; $history->Upload_Date = $inidate[2] . '-' . $inidate[0] . '-' . $inidate[1]; $history->Total_Contributions_Amount = $employer_contribution + $employee_contribution; $history->Total_Contributions = $num - 1; $history->Status = false; $history->Uploaded_By = Auth::user()->get_user_id(); $history->save(); fclose($handle); return View::make('Operations.uploadContributions')->with('success_message', $num - 1 . ' recordeds uploaded successfully.'); } else { return View::make('Operations.uploadContributions')->with('success_message', $num - 1 . 'No contributions were found.'); } } else { return View::make('Operations.contributions')->with('contributions', Contributions::all()); } }