/**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function bill_class($fee_schedule_code)
 {
     // dd($fee_schedule_code);
     $table = 'fee_sch_' . session()->get('current_session') . '_' . session()->get('current_term');
     $fee_schedule = DB::table($table)->where('fee_schedule_code', $fee_schedule_code)->first();
     $amount = DB::table($table)->where('fee_schedule_code', $fee_schedule_code)->sum('amount');
     $students = Student::where('class_id', $fee_schedule->class_id)->get();
     $school = School::find(1);
     //get all studenta ids who are eligible for the parent discount
     if ($school->parent_discount == 1) {
         $children_number = DiscountPolicy::find(1)->children_number;
         if ($children_number == null) {
             session()->flash('flash_message', 'Please, set the parent discount values. Go to Billing-> Discount Policies.');
             return redirect()->back();
         }
         $parent_discount_eligible = Helper::getParentDiscountEligibles();
         sort($parent_discount_eligible);
     }
     // dd($parent_discount_eligible);
     foreach ($students as $student) {
         $discount = 0;
         //get parent discount
         if ($school->parent_discount == 1) {
             if (in_array($student->id, $parent_discount_eligible)) {
                 $discount = Helper::calculateParentDiscount($student->id, $fee_schedule_code);
                 // dd($discount);
             }
         }
         //calculate total amount due to be paid
         $total = $amount - $discount;
         try {
             \DB::table('invoices_' . \Session::get('current_session') . '_' . \Session::get('current_term'))->insert(['student_id' => $student->id, 'fee_schedule_code' => $fee_schedule_code, 'invoice_number' => str_replace('-', '', $fee_schedule_code) . str_pad($student->id, 3, '0', STR_PAD_LEFT), 'amount' => $amount, 'discount' => $discount, 'balance' => Helper::getStudentCurrentBalance($student->id), 'total' => $total]);
         } catch (\Illuminate\Database\QueryException $e) {
             $errorCode = $e->errorInfo[1];
             if ($errorCode == 1062) {
                 session()->flash('flash_message', 'Hey, these guys have been invoiced');
                 return \Redirect::back();
             }
         }
     }
     //change the status of the fee schedule after billing class
     DB::table($table)->where('fee_schedule_code', $fee_schedule_code)->update(['status_id' => 8]);
     return redirect()->to('billing/invoices');
 }
 public function update_staff_policy(Request $request)
 {
     // dd($request);
     $discountPolicy = DiscountPolicy::find(2);
     $discountPolicy->children_number = $request->children_number;
     $discountPolicy->all_wards = $request->all_wards;
     $discountPolicy->dont_divide = $request->dont_divide;
     $discountPolicy->type = $request->type;
     $discountPolicy->discount_duration = $request->discount_duration;
     $discountPolicy->ward_to_deduct = $request->ward_to_deduct;
     $discountPolicy->percentage_value = $request->percentage_value;
     $discountPolicy->affected_elements = json_encode($request->affected_elements);
     $discountPolicy->sum_value = $request->sum_value;
     $discountPolicy->save();
     return redirect()->back();
 }