IMMREX7

aku nok ndi : /home/spdtg/www/schoolmis/app/Http/Controllers/Student/
File Up :
aku nok ndi : /home/spdtg/www/schoolmis/app/Http/Controllers/Student/TransportFeeController.php

<?php

namespace App\Http\Controllers\Student;

use Illuminate\Http\Request;
use App\Http\PaymentCalculation;
use DB;
use PDF;
use Auth;
use Session;
use Carbon\Carbon;
use App\Http\Cashfree;

class TransportFeeController extends StudentController {

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request) {
        $student = \App\AdmEntry::where('idStudent', '=', Session::get('idStudent'))->first();
        $stdtransport = \App\StudentTransport::where('idStudent', '=', $student->idStudent)->where('idFinancialYear', '=', $student->idFinancialYear)->first();
        if($student->idSchool == 25){
            return $this->royalTransportFee($student,$stdtransport,$request);
        }
        if($student->idSchool == 140){
            $plan = "";

            if($request->get('paymode') != null)
            $plan = $request->get('paymode');
            
            $paidHeaders= \App\BusTransactionDetail::join('busfeeheads', 'bus_transaction_details.idBusFeehead', '=', 'busfeeheads.idBusFeehead')->select('feeheadLabel')
            ->where('idStudent', '=', $student->idStudent)->where(function($query) {
                $query->whereNull('status');
                $query->orWhere('status', '=', 'In-Process');
                $query->orWhere('status', '=', 'Cleared');
                $query->orWhere('status', '=', 'Success');
            })->get();
    
            if(count($paidHeaders) == 0){
                $lessHeaders = \App\BusLesserTransactionDetail::join('busfeeheads', 'bus_lesser_transaction_details.idBusFeehead', '=', 'busfeeheads.idBusFeehead')->select('feeheadLabel')
                ->where('idStudent', '=', $student->idStudent)->where(function($query) {
                    $query->whereNull('status');
                    $query->orWhere('status', '=', 'In-Process');
                    $query->orWhere('status', '=', 'Cleared');
                    $query->orWhere('status', '=', 'Success');
                })->get();
    
                if(count($lessHeaders) > 0){
                    foreach($lessHeaders as $transaction){
                        if (str_contains($transaction->feeheadLabel, 'Plan A')) {
                            $plan = 'Plan A';
                            break;
                        }else if (str_contains($transaction->feeheadLabel, 'Plan B')) {
                            $plan = 'Plan B';
                            break;
                        }
                    }
                }
            }else{
                foreach($paidHeaders as $transaction){
                    if (str_contains($transaction->feeheadLabel, 'Plan A')) {
                        $plan = 'Plan A';
                        break;
                    }else if (str_contains($transaction->feeheadLabel, 'Plan B')) {
                        $plan = 'Plan B';
                        break;
                    }
                }
            }
            
            $feeheads = \App\BusFeehead::where('idStop', '=', $stdtransport->idStop)
                    ->where('feeheadLabel', '=', $plan)
                    ->orderBy('toDate')
                    ->get();
            $feeheader_ids = $feeheads->pluck('idBusFeehead')->toArray();
            $feetotal = $feeheads->sum('amount');
            $feeheader_ids = $feeheads->pluck('idFeehead')->toArray();
            $prevaccess_fee = DB::table('excess_transaction')
                            ->join('student_transaction', 'excess_transaction.idTransaction', '=', 'student_transaction.idTransaction')
                            ->where('idStudent', '=', $student->idStudent)
                            ->where('idFinancialYear', '=', $student->idFinancialYear)
                            ->where('excess_transaction.isActive', '=', 'Y')
                            ->orderBy('excess_transaction.idTransaction', 'desc')->first();
            $lesser_transaction = DB::table('lesser_transaction')
                            ->join('lesser_transaction_details', 'lesser_transaction_details.idLesserTransaction', '=', 'lesser_transaction.idLesserTransaction')
                            ->where('lesser_transaction.idStudent', '=', $student->idStudent)
                            ->where('lesser_transaction.idFinancialYear', '=', $student->idFinancialYear)
                            ->whereIn('idFeehead', $feeheader_ids)
                            ->where(function($query) {
                                $query->whereNull('lesser_transaction.status');
                                $query->orWhere('lesser_transaction.status', '=', 'Cleared');
                                $query->orWhere('lesser_transaction.status', '=', 'In-Process');
                                $query->orWhere('lesser_transaction.status', '=', 'Success');
                            })
                            ->select(DB::raw('SUM(totalPaid) as totalPaid'))
                            ->first();
            $paidfees_tot = \App\BusTransaction::where('idStudent', '=', $student->idStudent)
                            ->where(function($query) {
                                $query->whereNull('status');
                                $query->orWhere('status', '=', 'Cleared');
                                $query->orWhere('status', '=', 'In-Process');
                                $query->orWhere('status', '=', 'Success');
                            })
                            ->select('paymentMode', 'paymentDate', 'idBusTransaction', DB::raw('SUM(totalPaid) as totalPaid'), DB::raw('SUM(discount) as discount'), DB::raw('SUM(fine) as fine'), DB::raw('SUM(penaltyAmount) as penaltyAmount'))->first();
            $paidfees = \App\BusTransaction::where('idStudent', '=', $student->idStudent)
                            ->where(function($query) {
                                $query->whereNull('status');
                                $query->orWhere('status', '=', 'In-Process');
                                $query->orWhere('status', '=', 'Cleared');
                                $query->orWhere('status', '=', 'Success');
                            })->get();
            $lesserfees = \App\BusLesserTransaction::where('idStudent', '=', $student->idStudent)
                            ->where(function($query) {
                                $query->whereNull('status');
                                $query->orWhere('status', '=', 'In-Process');
                                $query->orWhere('status', '=', 'Cleared');
                                $query->orWhere('status', '=', 'Success');
                            })->get();
            $paymentMode = "easebuzz";
            return view('students.transportfees.bus_feedetails_mhws', compact('paymentMode','lesser_transaction', 'lesserfees', 'feetotal', 'paidfees_tot', 'student', 'paidfees', 'feeheads', 'excesspaid', 'penalty', 'plan', 'prevaccess_fee'));
        }else{
            $feetotal=0;
            $feeheader_ids=array();
            $feeheads=array();
            if(isset($stdtransport->idStop)){
            $feeheads = \App\BusFeehead::where('idStop', '=', $stdtransport->idStop)
                    ->orderBy('toDate')
                    ->get();
            $feeheader_ids = $feeheads->pluck('idBusFeehead')->toArray();
            $feetotal = $feeheads->sum('amount');
            }
            $prevaccess_fee = DB::table('bus_excess_transaction')
                            ->join('bus_transaction', 'bus_excess_transaction.idBusTransaction', '=', 'bus_transaction.idBusTransaction')
                            ->where('idStudent', '=', $student->idStudent)
                            ->where('idFinancialYear', '=', $student->idFinancialYear)
                            ->where('bus_excess_transaction.isActive', '=', 'Y')
                            ->orderBy('bus_excess_transaction.idBusTransaction', 'desc')->first();
            $lesser_transaction = DB::table('bus_lesser_transaction')
                    ->join('bus_lesser_transaction_details', 'bus_lesser_transaction_details.idBusLesserTransaction', '=', 'bus_lesser_transaction.idBusLesserTransaction')
                    ->where('bus_lesser_transaction.idStudent', '=', $student->idStudent)
                    ->where('bus_lesser_transaction.idFinancialYear', '=', $student->idFinancialYear)
                    ->whereIn('idBusFeehead', $feeheader_ids)
                    ->where(function($query) {
                        $query->whereNull('bus_lesser_transaction.status');
                        $query->orWhere('bus_lesser_transaction.status', '=', 'Cleared');
                        $query->orWhere('bus_lesser_transaction.status', '=', 'In-Process');
                        $query->orWhere('bus_lesser_transaction.status', '=', 'Success');
                    })
                    ->select(DB::raw('SUM(totalPaid) as totalPaid'))
                    ->first();
            $paidfees_tot = \App\BusTransaction::where('idStudent', '=', $student->idStudent)
                            ->where(function($query) {
                                $query->whereNull('status');
                                $query->orWhere('status', '=', 'Cleared');
                                $query->orWhere('status', '=', 'In-Process');
                                $query->orWhere('status', '=', 'Success');
                            })
                            ->select('paymentMode', 'paymentDate', 'idBusTransaction', DB::raw('SUM(totalPaid) as totalPaid'), DB::raw('SUM(discount) as discount'), DB::raw('SUM(fine) as fine'), DB::raw('SUM(penaltyAmount) as penaltyAmount'))->first();
    
            $paidfees = \App\BusTransaction::where('idStudent', '=', $student->idStudent)
                            ->where(function($query) {
                                $query->whereNull('status');
                                $query->orWhere('status', '=', 'In-Process');
                                $query->orWhere('status', '=', 'Cleared');
                                $query->orWhere('status', '=', 'Success');
                            })->get();
            $lesserfees = \App\BusLesserTransaction::where('idStudent', '=', $student->idStudent)
                            ->where(function($query) {
                                $query->whereNull('status');
                                $query->orWhere('status', '=', 'In-Process');
                                $query->orWhere('status', '=', 'Cleared');
                                $query->orWhere('status', '=', 'Success');
                            })->get();
            return view('students.transportfees.bus_feedetails', compact('lesser_transaction', 'prevaccess_fee', 'lesserfees', 'stdtransport', 'feetotal', 'paidfees_tot', 'student', 'paidfees', 'feeheads', 'excesspaid', 'penalty', 'feeheadwiseamount'));
        }
    }

    private function royalTransportFee($student,$stdtransport,$request){
        $lesser_transaction = [];
        $feeheads = [];
        $feetotal = 0;
        $plan = '';

        $prevaccess_fee = DB::table('bus_excess_transaction')
                        ->join('bus_transaction', 'bus_excess_transaction.idBusTransaction', '=', 'bus_transaction.idBusTransaction')
                        ->where('idStudent', '=', $student->idStudent)
                        ->where('idFinancialYear', '=', $student->idFinancialYear)
                        ->where('bus_excess_transaction.isActive', '=', 'Y')
                        ->orderBy('bus_excess_transaction.idBusTransaction', 'desc')->first();

        $paidfees_tot = \App\BusTransaction::where('idStudent', '=', $student->idStudent)
                        ->where(function($query) {
                            $query->whereNull('status');
                            $query->orWhere('status', '=', 'Cleared');
                            $query->orWhere('status', '=', 'In-Process');
                            $query->orWhere('status', '=', 'Success');
                        })
                        ->where('idFinancialYear', '=', $student->idFinancialYear)
                        ->select('paymentMode', 'paymentDate', 'idBusTransaction', DB::raw('SUM(totalPaid) as totalPaid'), DB::raw('SUM(discount) as discount'), DB::raw('SUM(fine) as fine'), DB::raw('SUM(penaltyAmount) as penaltyAmount'))->first();

        $paidfees = \App\BusTransaction::where('idStudent', '=', $student->idStudent)
                        ->where('idFinancialYear', '=', $student->idFinancialYear)
                        ->where(function($query) {
                            $query->whereNull('status');
                            $query->orWhere('status', '=', 'In-Process');
                            $query->orWhere('status', '=', 'Cleared');
                            $query->orWhere('status', '=', 'Success');
                        })->get();

        $lesserfees = \App\BusLesserTransaction::where('idStudent', '=', $student->idStudent)
                        ->where('idFinancialYear', '=', $student->idFinancialYear)
                        ->where(function($query) {
                            $query->whereNull('status');
                            $query->orWhere('status', '=', 'In-Process');
                            $query->orWhere('status', '=', 'Cleared');
                            $query->orWhere('status', '=', 'Success');
                        })->get();
        
        if($paidfees->count() > 0){
            foreach($paidfees as $paidfee){
                $transactions = \App\BusTransactionDetail::where('idBusTransaction','=',$paidfee->idBusTransaction)->get();
                foreach($transactions as $transaction){
                    $header = \DB::table('busfeeheads')->where('idBusFeehead',$transaction->idBusFeehead)->first();
                    if (str_contains($header->feeheadLabel, 'Plan A')) {
                        $plan = 'Plan A';
                        break;
                    }else if (str_contains($header->feeheadLabel, 'Plan B')) {
                        $plan = 'Plan B';
                        break;
                    }else if (str_contains($header->feeheadLabel, 'Plan C')) {
                        $plan = 'Plan C';
                        break;
                    }
                }

                if($plan != '') break;
            }
        }

        if($plan == '')
        if($lesserfees->count() > 0){
            foreach($lesserfees as $paidfee){
                $transactions = \App\BusLesserTransactionDetails::where('idBusLesserTransaction','=',$paidfee->idBusLesserTransaction)->get();
                foreach($transactions as $transaction){
                    $header = \DB::table('busfeeheads')->where('idBusFeehead',$transaction->idBusFeehead)->first();
                    if (str_contains($header->feeheadLabel, 'Plan A')) {
                        $plan = 'Plan A';
                        break;
                    }else if (str_contains($header->feeheadLabel, 'Plan B')) {
                        $plan = 'Plan B';
                        break;
                    }else if (str_contains($header->feeheadLabel, 'Plan C')) {
                        $plan = 'Plan C';
                        break;
                    }
                }

                if($plan != '') break;
            }
        }

        $type = '';
            
        if($request->get('paymode') != null && $plan == ''){
            $type = $request->get('paymode');
            $feeheads = \DB::table('busfeeheads')->where('idStop', '=', $stdtransport->idStop)
                    ->where('idRoute', '=', $stdtransport->idRoute)
                    ->where('feeheadLabel', 'LIKE', $type.'%')
                    ->orderBy('toDate')
                    ->get();
            $feetotal = $feeheads->sum('amount');
            $feeheader_ids = $feeheads->pluck('idBusFeehead')->toArray();
            $lesser_transaction = DB::table('bus_lesser_transaction')
                ->join('bus_lesser_transaction_details', 'bus_lesser_transaction.idBusLesserTransaction', '=', 'bus_lesser_transaction_details.idBusLesserTransaction')
                ->where('bus_lesser_transaction.idStudent', '=', $student->idStudent)
                ->where('bus_lesser_transaction.idFinancialYear', '=', $student->idFinancialYear)
                ->whereIn('idBusFeehead', $feeheader_ids)
                ->where(function($query) {
                    $query->whereNull('bus_lesser_transaction.status');
                    $query->orWhere('bus_lesser_transaction.status', '=', 'Cleared');
                    $query->orWhere('bus_lesser_transaction.status', '=', 'In-Process');
                    $query->orWhere('bus_lesser_transaction.status', '=', 'Success');
                })
                ->select(DB::raw('SUM(totalPaid) as totalPaid'),DB::raw('SUM(bus_lesser_transaction_details.discount) as discount'))
                ->first();

        }else if($plan != ''){
            $feeheads = \DB::table('busfeeheads')->where('idStop', '=', $stdtransport->idStop)
                    ->where('idRoute', '=', $stdtransport->idRoute)
                    ->where('feeheadLabel', 'LIKE', $plan.'%')
                    ->orderBy('toDate')
                    ->get();   
            $feetotal = $feeheads->sum('amount');
            $feeheader_ids = $feeheads->pluck('idBusFeehead')->toArray();
            $lesser_transaction = DB::table('bus_lesser_transaction')
            ->join('bus_lesser_transaction_details', 'bus_lesser_transaction_details.idBusLesserTransaction', '=', 'bus_lesser_transaction.idBusLesserTransaction')
            ->where('bus_lesser_transaction.idStudent', '=', $student->idStudent)
            ->where('bus_lesser_transaction.idFinancialYear', '=', $student->idFinancialYear)
            ->whereIn('idBusFeehead', $feeheader_ids)
            ->where(function($query) {
                $query->whereNull('bus_lesser_transaction.status');
                $query->orWhere('bus_lesser_transaction.status', '=', 'Cleared');
                $query->orWhere('bus_lesser_transaction.status', '=', 'In-Process');
                $query->orWhere('bus_lesser_transaction.status', '=', 'Success');
            })
            ->select(DB::raw('SUM(totalPaid) as totalPaid'),DB::raw('SUM(bus_lesser_transaction_details.discount) as discount'))
            ->first();
        }

        $feeData = PaymentCalculation::calculateBusFee($student,$request,$stdtransport);
        $sessionPending = [];
        $idStudentSession = null;
        $isCollectionAllowed = "Y";
        $fetchSession = \App\FinancialYear::where('idFinancialYear','<',$student->idFinancialYear)->where('idSchool', '=',$student->idSchool)->orderBy('idFinancialYear','DESC')->get();
        if(count($fetchSession) > 0){
            foreach($fetchSession as $var){
                $studentSession = \App\AdmEntry::where('idSchool', '=', $student->idSchool)->where('idFinancialYear', '=', $var->idFinancialYear)->where('ecNo', '=', $student->ecNo)->where('isActive','Y')->first();
                if($studentSession != null){
                    $sessionPending[$var->financialYearName] = PaymentCalculation::getOutstandingAmount($studentSession,$request);
                }
            }                
        }
        return view('students.transportfees.royal_feedetails', compact('feeData','lesser_transaction', 'lesserfees', 'feetotal', 'paidfees_tot', 'student', 'paidfees', 'feeheads', 'excesspaid', 'penalty', 'feeheadwiseamount', 'prevaccess_fee','type','plan','stdtransport'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create() {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request) {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id) {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id) {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id) {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id) {
        //
    }

    public function getFeewiseData($id, $studentId) {
        $feeheader_ids = array_map('intval', explode(',', $id));
        //return $feeheader_ids;
        $student = \App\AdmEntry::where('idStudent', '=', $studentId)->first();
        $chqbounce = \App\BusFailedTransaction::where('idStudent', '=', $student->idStudent)
                ->where('idFinancialYear', '=', $student->idFinancialYear)
                ->where('status', '=', 'Bounced')
                ->where('chqFineStatus', '=', '0')
                ->get();

        $penaltyAmount = 0;
        $amountDiscount = 0;
        if (isset($chqbounce)) {
            foreach ($chqbounce as $chqFine)
                $penaltyAmount = $penaltyAmount + $chqFine->chequeBounceCharge;
        }

        $excess_transaction = DB::table('bus_excess_transaction')
                        ->join('bus_transaction', 'bus_excess_transaction.idBusTransaction', '=', 'bus_transaction.idBusTransaction')
                        ->where('idStudent', '=', $student->idStudent)
                        ->where('idFinancialYear', '=', $student->idFinancialYear)
                        ->where(function($query) {
                            $query->whereNull('status');
                            $query->orWhere('status', '=', 'Cleared');
                            $query->orWhere('status', '=', 'In-Process');
                            $query->orWhere('status', '=', 'Success');
                        })
                        ->where('bus_excess_transaction.isActive', '=', 'Y')
                        ->orderBy('bus_excess_transaction.idBusTransaction', 'desc')->first();

        $excessAmount = 0;
        if (isset($excess_transaction))
            $excessAmount = $excess_transaction->excessAmount;


        $selectedHeaders = \App\BusFeehead::whereIn('idBusFeehead', $feeheader_ids)->get();
        $totalAmount = 0;

        $lessPaid = 0;
        $lesser_transaction = DB::table('bus_lesser_transaction')
                ->join('bus_lesser_transaction_details', 'bus_lesser_transaction_details.idBusLesserTransaction', '=', 'bus_lesser_transaction.idBusLesserTransaction')
                ->where('bus_lesser_transaction.idStudent', '=', $student->idStudent)
                ->where('bus_lesser_transaction.idFinancialYear', '=', $student->idFinancialYear)
                ->whereIn('idBusFeehead', $feeheader_ids)
                ->where(function($query) {
                    $query->whereNull('bus_lesser_transaction.status');
                    $query->orWhere('bus_lesser_transaction.status', '=', 'Cleared');
                    $query->orWhere('bus_lesser_transaction.status', '=', 'In-Process');
                    $query->orWhere('bus_lesser_transaction.status', '=', 'Success');
                })
                ->select(DB::raw('SUM(totalPaid) as totalPaid'))
                ->first();

        if (isset($lesser_transaction))
            $lessPaid = $lesser_transaction->totalPaid;

        for ($i = 0; $i < sizeof($selectedHeaders); $i++) {

            $stepFine = 0;
            $stepDiscount = 0;
            $stepAmount = 0;
            $headerObject = $selectedHeaders[$i];
            $date = Carbon::parse($headerObject['toDate']);
            $now = Carbon::now();
            $diff = $date->diffInDays($now, false);
            if ($diff > 0) {
                if ($headerObject['fine'] != null && $headerObject['fine'] != 0.00) {
                    $stepFine = ( $headerObject['fine'] * $diff);
                }
                if ($headerObject['flatFine'] != null) {
                    $stepFine = $stepFine + $headerObject['flatFine'];
                }
            }

            $discountfees = DB::table('busfee_discounts')->where('idFinancialYear', '=', $student->idFinancialYear)->where('idStudent', '=', $student->idStudent)->where('idBusFeehead', '=', $headerObject['idBusFeehead'])->where('isActive', 'Y')->get();

            if (isset($discountfees)) {
                foreach ($discountfees as $discounts) {
                    $stepDiscount = $stepDiscount + $discounts->amount;
                    $amountDiscount = $amountDiscount + $stepDiscount;
                }
            }

            $totalAmount = $totalAmount + ( $headerObject['amount'] + $stepFine );
            $totalAmount = $totalAmount - $stepDiscount;
        }

        $totalAmount = $totalAmount + $penaltyAmount;
        $totalAmount = $totalAmount - $lessPaid;

        if ($totalAmount < $excessAmount)
            return 0;
        else
            return $totalAmount - $excessAmount;
    }


    public function printReceipt($id) {
        $student = \App\AdmEntry::where('idStudent', '=', Session::get('idStudent'))->first();
        $school = \App\School::where('idSchool', '=', $student->idSchool)->first();
        $transaction = \App\BusTransaction::where('idBusTransaction', '=', $id)->first();
        $stdtransport = \App\StudentTransport::where('idStudent', '=', $student->idStudent)->where('idFinancialYear', '=', $student->idFinancialYear)->first();
        $feeheads = \App\BusFeehead::where('idStop', '=', $stdtransport->idStop)
                ->orderBy('toDate')
                ->get();
        $feetotal = $feeheads->sum('amount');

        $pdf = PDF::loadView('schools.buses.print_receipt', ['margin_top' => 20], compact('transaction', 'school', 'student', 'feetotal'));
        return $pdf->stream('feereceipt.pdf');
    }
    
    public function printLessPaidReceipt($id) {
        $student = \App\AdmEntry::where('idStudent', '=', Session::get('idStudent'))->first();
        $school = \App\School::where('idSchool', '=', $student->idSchool)->first();
        $transaction = \App\BusLesserTransaction::where('idBusLesserTransaction', '=', $id)->first();
        $stdtransport = \App\StudentTransport::where('idStudent', '=', $student->idStudent)->where('idFinancialYear', '=', $student->idFinancialYear)->first();
        $feeheads = \App\BusFeehead::where('idStop', '=', $stdtransport->idStop)
                ->orderBy('toDate')
                ->get();
        $feetotal = $feeheads->sum('amount');
        $pdf = PDF::loadView('schools.buses.print_lesseramt_receipt', ['margin_top' => 20], compact('transaction', 'school', 'student', 'feetotal'));
        return $pdf->stream('feereceipt.pdf');
    }

    function checkPaymentStatus($orderId) {
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => "https://api.cashfree.com/pg/orders/" . $orderId,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_HTTPHEADER => [
                "Accept: application/json",
                "Content-Type: application/json",
                "x-api-version: 2022-01-01",
                "x-client-id: 1928412d1f5bed99a3fb8e3528148291",
                "x-client-secret: 5de10b32c7b12616eaf0576da075c2d173c28ed9"
            ],
        ]);
        $response = curl_exec($curl);
        $info = curl_getinfo($curl, CURLINFO_HEADER_OUT);
        $err = curl_error($curl);
        curl_close($curl);
        if ($err) {
            return "";
        } else {
            $result = json_decode($response, true);
            return $result;
        }
    }


    public function cashfreePaymentStatus(Request $request) {
        $response = $this->checkPaymentStatus($request->get('order_id'));
        if ($response['order_status'] == "PAID") {
            $orderInfo = DB::table('order_receipts')->where('order_id', $request->get('order_id'))->first();
            $stdtransaction = new \App\BusTransaction();
            $stdtransaction->idStudent = $orderInfo->idStudent;
            $stdtransaction->idFinancialYear = $orderInfo->idFinancialYear;
            $stdtransaction->paymentDate = today_date();
            $stdtransaction->totalPaid = $orderInfo->order_amount;
            $stdtransaction->paymentMode = 'Online';
            $stdtransaction->receiptNo = 'RCPONL' . $orderInfo->idOrder;
            $stdtransaction->idSchool = $orderInfo->idSchool;
            $stdtransaction->status = 'Success';
            $stdtransaction->penaltyAmount = 0.00;
            $stdtransaction->fine = $orderInfo->fine_amount;
            $stdtransaction->discount = 0.00;
            if(DB::table('bus_transaction')->where('paymentDate', today_date())->where('idStudent', $orderInfo->idStudent)->where('paymentMode', 'Online')->where('idFinancialYear', $orderInfo->idFinancialYear)->where('totalPaid', $orderInfo->order_amount)->doesntExist())
                $stdtransaction->save();
            else{
                return response()->json(['message' => 'Payment Webhook Exists']);
            }
            
            $totalFine = 0;
            $totalDiscount = 0;
            //for loop for headers
            $totalDiscount=0;
            if($orderInfo->isMultipleFeehead == "Y")
            $selectedHeaders = json_decode($orderInfo->feeheads, true);
            else 
            $selectedHeaders = array(
                $orderInfo->idFeehead
            );
                foreach($selectedHeaders as $headers){
                    if(isset($headers)){
                        $subheaders = \App\BusFeehead::where('idBusFeehead','=',$headers)->get()->toArray();
                        foreach($subheaders as $headerIds)
                            {
                                $stepFine = 0;
                                $stdtransaction_detail = new \App\BusTransactionDetail();
                                $stdtransaction_detail->idStudent = $orderInfo->idStudent;
                                $stdtransaction_detail->idBusTransaction = $stdtransaction->idBusTransaction;
                                $stdtransaction_detail->idBusFeehead = $headerIds['idBusFeehead'];
                                $stdtransaction_detail->paymentDate = today_date();
                                $stdtransaction_detail->amountPaid = $headerIds['amount'];
                                $date = Carbon::parse($headerIds['toDate']);
                                $now = Carbon::now();
                                $diff = $date->diffInDays($now, false);
                                if ($diff > 0) {
                                    if ($headerIds['fine'] != null && $headerIds['fine'] != 0.00) {
                                        $stepFine = ( $headerIds['fine'] * $diff);
                                    }
                                    if ($headerIds['flatFine'] != null) {
                                        $stepFine = $stepFine + $headerIds['flatFine'];
                                    }
                                }
                                $stdtransaction_detail->fine = 0;
                                $stdtransaction_detail->discount = 0.00;
                                $stdtransaction_detail->save();
                        }
                    }
                }
                if($totalDiscount >0){
                    $stdtransaction->discount = $totalDiscount;
                    $stdtransaction->update(); 
                }
            return response()->json(['message' => 'Payment Done']);
        }else{
            return response()->json(['message' => 'Failed to verify your payment.']);
        }
    }

    public function generateBusCashfreeOrder(Request $request) {
                $student = \App\AdmEntry::where('idStudent', '=', $request->idStudent)->first();
                $penaltyAmount = 0;
                $payAmount = 0;
                $now = Carbon::now();
                $order = new \App\OrderReceipt();
                $order->fill($request->all());
                $order->idSchool = $student->idSchool;
                $order->idClass = $student->idClass;
                $order->type = "Bus";
                $order->order_note = 'Online Fee Submission';
                $order->notes = json_encode($request->all());
                $order->dateOfTransaction = today();
                if (count($request->selectedHeaders) > 1) {
                    $order->isMultipleFeehead = 'Y';
                    $order->feeheads = implode(",", $request->selectedHeaders);
        
                    foreach ($request->selectedHeaders as $idfee) {
                        $details = \App\BusFeehead::where('idBusFeehead', '=', $idfee)->first();
                        $totalAmount = $details->amount;
                        $fineAmount = 0;
                        $lastDate = Carbon::parse($details->toDate);
                        $diff = $lastDate->diffInDays($now, false);
                        if ($diff > 0) {
                            if ($details->fine != null)
                                $fineAmount = $diff * $details->fine + $fineAmount;
                            if ($details->flatFine != null) {
                                $fineAmount = $details->flatFine + $fineAmount;
                            }
                        }
                        $payAmount = $payAmount + $totalAmount + $fineAmount;
                        $penaltyAmount = $penaltyAmount + $fineAmount;
                        $order->order_amount = $payAmount;
                        $order->fine_amount = $penaltyAmount;
                    }
                } else {
                    $order->isMultipleFeehead = 'N';
                    $order->idFeehead = $request->selectedHeaders['0'];
                    foreach ($request->selectedHeaders as $idfee) {
                        $details = \App\BusFeehead::where('idBusFeehead', '=', $idfee)->first();
                        $totalAmount = $details->amount;
                        $fineAmount = 0;
                        $lastDate = Carbon::parse($details->toDate);
                        $diff = $lastDate->diffInDays($now, false);
                        if ($diff > 0) {
                            if ($details->fine != null)
                                $fineAmount = $diff * $details->fine + $fineAmount;

                            if ($details->flatFine != null) {
                                $fineAmount = $details->flatFine + $fineAmount;
                            }
                        }
                        $payAmount = $payAmount + $totalAmount + $fineAmount;
                        $penaltyAmount = $penaltyAmount + $fineAmount;
                        $order->order_amount = $payAmount;
                        $order->fine_amount = $penaltyAmount;
                    }
                }
                $order->save();
                $totalAmount = $payAmount . '00';
                return Cashfree::generateOrder($order, $student);
            }

}

Copyright © 2021 - 2025 IMMREX7