public static function createSchoolFeesPaymentTable($session, $term_id) { try { //we are using fee_sch_ here istead of fee_schedules_ cos primary keys are too long using the the latter thereby hrowing an error // create new record for this table in the termly records table $school_fees_payment = new SchoolFeesPayment(); $school_fees_payment->table_name = 'sch_fee_pay_' . $session . '_' . $term_id; $school_fees_payment->session = $session; $school_fees_payment->term = $term_id; $school_fees_payment->save(); \Schema::create('sch_fee_pay_' . $session . '_' . $term_id, function (Blueprint $table) { $table->increments('id'); $table->integer('student_id'); $table->string('fee_schedule_code')->nullable(); $table->integer('outstanding_balance')->default(0); $table->double('amount', 15, 2)->default(0.0); $table->text('elements_paid_for'); $table->string('session'); $table->integer('term_id'); $table->integer('class_id'); $table->integer('transaction_mode_id')->nullable(); $table->string('transaction_id')->nullable(); $table->integer('status_id')->default(1); $table->softDeletes(); $table->timestamps(); }); } catch (\Illuminate\Database\QueryException $e) { $errorCode = $e->errorInfo[1]; if ($errorCode == 1050) { $error_msg[] = 'Fee Schedule table for chosen session and term already exists.'; // session()->flash('flash_message', 'Invoices table for chosen session and term already exists.'); // return \Redirect::back()->withInput(); } } }
public function pay_invoice(Request $request) { // dd($request->all()); $data['title'] = 'Pay School Fees'; $data['fees'] = 1; $class_id = $request->class_id; $student_id = $request->student_id; $fee_schedule_code = strval($class_id) . session()->get('current_session') . strval(session()->get('current_term')); $session = session()->get('current_session'); $term = session()->get('current_term'); $data['transaction_modes'] = TransactionMode::lists('transaction_mode', 'id')->prepend('Please Select'); //current session invoice table $table = 'invoices_' . $session . '_' . $term; $data['student_fee_elements'] = []; //==============get previous sessions fees not yet paid+++++++++++++ $sessions = CurrentTerm::orderBy('created_at', 'desc')->where('session', '!=', session()->get('current_session'))->orWhere('term', '!=', session()->get('current_term'))->take(9)->groupby('session')->get(); // dd($sessions); $outstandings = []; $invoices_amt = 0; $payment_amt = 0; // foreach ($sessions as $session) { $invoices = Invoice::where('session', '!=', session()->get('current_session'))->orWhere('term', '!=', session()->get('current_term'))->get(); // dd($invoices); //get all payments associated with an invoice foreach ($invoices as $invoice) { $payment_amt = 0; $student_invoice = DB::table($invoice->table_name)->where(['student_id' => $student_id])->first(); //for every invoice, get payment associated with that invoice if ($student_invoice !== null) { $sch_fee_payment = SchoolFeesPayment::where(['session' => $invoice->session, 'term' => $invoice->term])->first(); $paid_amt = DB::table($sch_fee_payment->table_name)->where(['student_id' => $student_id])->sum('amount'); //get class id from fee schedule code $outstanding_class_id = substr($student_invoice->fee_schedule_code, 0, 1); //get oustanding amount $outstanding_balance = $student_invoice->total - $paid_amt; //if outstanding amt is not greater than zero then student has paid fully for that invoice therefore dont show on page if ($outstanding_balance > 0) { $session_fees = ['session' => $invoice->session, 'term' => $invoice->term, 'outstanding_balance' => $outstanding_balance, 'sch_fee_payment_table_name' => $sch_fee_payment->table_name, 'outstanding_fee_schedule_code' => $student_invoice->fee_schedule_code, 'outstanding_class_id' => $outstanding_class_id]; array_push($outstandings, $session_fees); } } } // } if ($outstandings == []) { $outstandings = null; } $data['outstandings'] = $outstandings; //===========GET THIS SESSIONS FEES++++++++++++++++++ //get fee schedule elements for current session $fee_sch_table = 'fee_sch_' . session()->get('current_session') . '_' . session()->get('current_term'); //get student invoice including exmepted fee elements $data['student_invoice'] = \DB::table($table)->where(['student_id' => $student_id, 'fee_schedule_code' => $fee_schedule_code])->first(); // dd($data['student_invoice'] ); // //check if invoice has been generated for this student if ($data['student_invoice'] !== null) { $already_paid = []; $paid_amount = 0; $data['exempted_elements'] = json_decode($data['student_invoice']->exempted_fee_elements); if ($data['exempted_elements'] == null) { $data['exempted_elements'] = []; } $sch_fee_payment = SchoolFeesPayment::where(['session' => session()->get('current_session'), 'term' => session()->get('current_term')])->first(); $payment_histories = DB::table($sch_fee_payment->table_name)->where(['student_id' => $student_id])->get(); // dd($payment_histories); foreach ($payment_histories as $payment_history) { $already_paid[] = json_decode($payment_history->elements_paid_for); $paid_amount += $payment_history->amount; } $already_paid = array_flatten($already_paid); // dd($already_paid); if ($already_paid !== null) { $data['already_paid'] = $already_paid; } else { $data['already_paid'] = []; } //get oustanding amount $outstanding_balance = $data['student_invoice']->total - $paid_amount; if ($outstanding_balance > 0) { $data['student_fee_elements'] = DB::table($fee_sch_table)->where(['fee_schedule_code' => $fee_schedule_code])->join('fee_elements', 'fee_elements.id', '=', $fee_sch_table . '.fee_element_id')->get(); } } else { session()->flash('flash_message', 'An invoice has not been generated for this term for this student.'); } if ($data['outstandings'] !== null || $data['student_invoice'] !== null) { $sch_fee_payments = SchoolFeesPayment::all(); foreach ($sch_fee_payments as $sch_fee_payment) { $term_payments = DB::table($sch_fee_payment->table_name)->where('student_id', $student_id)->orderBy('created_at', 'desc')->get(); $payments[] = $term_payments; } // dd(array_flatten($payments)); $data['payments'] = array_flatten($payments); $data['student_info'] = Student::find($student_id); $data['classes'] = studentClass::lists('name', 'id')->prepend('Please Select'); $data['students'] = Student::all(); return view('payments.payments', $data); } else { session()->flash('flash_message', 'An invoice has not been generated for this student.'); return redirect()->route('accounts.payments.index'); } }