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/FeeSubmissionController.php

<?php

namespace App\Http\Controllers\Student;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;
use App\Http\Billdesk;
use Carbon\Carbon;
use \App\Http\TransactionResponseBean;
use Auth;
use PDF;

class FeeSubmissionController extends StudentController {

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index() {
        $student = \App\AdmEntry::where('idStudent', '=', Session::get('idStudent'))->first();
        $feeheads = \DB::table('northfeeheads')->where('idStudent', '=', $student->idStudent)
                ->where('idFinancialYear', '=', $student->idFinancialYear)
                ->where('isParent', '=', '0')
                ->where('idType', '=', 'fee')
                ->orderBy('toDate')
                ->get();
        $fineheads = \DB::table('northfines')->where('idSchool', '=', $student->idSchool)->where('idFinancialYear', '=', $student->idFinancialYear)->get();
        $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\StudentTransaction::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', 'idTransaction', 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();
        // dd($paidfees_tot);
        $paidfees = \App\StudentTransaction::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\LesserTransaction::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.onlinefees.sudarshanfeedetails', compact('lesser_transaction', 'lesserfees', 'fineheads', 'feetotal', 'paidfees_tot', 'student', 'paidfees', 'feeheads', 'excesspaid', 'penalty', 'feeheadwiseamount', 'prevaccess_fee'));
    }

    /**
     * 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 paymentSuceesful(Request $request) {
        if (isset($_POST)) {
            if ($this->isValidHash($_POST) == 1) {

                $post_data = explode("|", $_POST['msg']);
                $prevOnlineTransaction = DB::table('student_transaction_online')->where('idReference', $post_data[1])->first();
                $students = \App\AdmEntry::where('idStudent', '=', $prevOnlineTransaction->idStudent)->first();
                $stdtransaction = \App\StudentTransaction::where('receiptNo', '=', $post_data[2])->first();
                if (isset($stdtransaction->totalPaid)) {
                    return redirect('student/feesubmission')->with('status', 'Payment Error');
                }



                if ($post_data[13] == 'NA') {
                    $transactionEnd = Carbon::now();
                    $transactionEnd->setTimezone('Asia/Kolkata');
                } else {
                    $transactionEnd = Carbon::createFromFormat('d-m-Y H:i:s', $post_data[13]);
                    $transactionEnd->setTimezone('Asia/Kolkata');
                }

                $transactionStart = Carbon::createFromFormat('Y-m-d H:i:s', $prevOnlineTransaction->created_at);
                $transactionStart->setTimezone('Asia/Kolkata');


                if ($transactionEnd->diffInMinutes($transactionStart) < 30 && $post_data[14] == '0300') {

                    $total_paid = $post_data[4];
                    DB::beginTransaction();
                    try {

                        $payableAmount = $prevOnlineTransaction->payableAmount;
                        $penaltyAmount = 0;

                        $chqbounce = \App\StudentTransaction::where('idStudent', '=', $request->idStudent)
                                ->where('idFinancialYear', '=', $students->idFinancialYear)
                                ->where('status', '=', 'Bounced')
                                ->where('chqFineStatus', '=', '0')
                                ->get();

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

                        $stdtransaction = new \App\StudentTransaction();
                        $stdtransaction->idStudent = $students->idStudent;
                        $stdtransaction->idFinancialYear = $students->idFinancialYear;
                        $stdtransaction->paymentDate = today_date();
                        $stdtransaction->totalPaid = $post_data[4];
                        $stdtransaction->paymentMode = 'Online';
                        $stdtransaction->receiptNo = $post_data[2];
                        $stdtransaction->idSchool = $students->idSchool;
                        $stdtransaction->status = 'Success';
                        $stdtransaction->penaltyAmount = $penaltyAmount;
                        $stdtransaction->fine = $prevOnlineTransaction->penaltyAmount - $penaltyAmount;
                        $stdtransaction->discount = 0;
                        $stdtransaction->save();

                        $totalFine = 0;
                        $totalDiscount = 0;

                        //for loop for headers
                        $totalDiscount = 0;
                        $selectedHeaders = json_decode($prevOnlineTransaction->idFeehead, true);

                        foreach ($selectedHeaders as $headers) {
                            $subheaders = \App\NorthFeeHead::where('isParent', '=', $headers)->get()->toArray();
                            foreach($subheaders as $headerIds)
                                {
                                    $stdtransaction_detail = new \App\StudentTransactionDetail();
                                    $stdtransaction_detail->idStudent = $students->idStudent;
                                    $stdtransaction_detail->idTransaction = $stdtransaction->idTransaction;
                                    $stdtransaction_detail->idFeehead = $headerIds['idFeehead'];
                                    $stdtransaction_detail->paymentDate = today_date();
        
                                    $stdtransaction_detail->amountPaid = $headerIds['amount'];
                                    $stdtransaction_detail->fine = 0;
                                    $stdtransaction_detail->isNorth = 2;
                                    $stdtransaction_detail->discount = $headerIds['discount'];
                                    $totalDiscount = $totalDiscount + $headerIds['discount'];
                                    $stdtransaction_detail->save();
                                }
                        }

                        if ($totalDiscount > 0) {
                            $stdtransaction->discount = $totalDiscount;
                            $stdtransaction->update();
                        }

                        if (isset($chqbounce)) {
                            foreach ($chqbounce as $chqFine) {
                                $chqFine->chqFineStatus = 1;
                                $chqFine->update();
                            }
                        }
                        DB::commit();
                    } catch (\Exception $e) {
                        DB::rollBack();
                    }
                    DB::table('student_transaction_online')
                            ->where('idOnline', $prevOnlineTransaction->idOnline)
                            ->update([
                                'onlineTransactionId' => $post_data[2],
                                'paymentDate' => today_date(),
                                'code' => $post_data[14],
                                'status' => 'Success',
                                'remarks' => $post_data[5],
                                'errorMessage' => $post_data[24]
                    ]);
                } else {
                    $stdtransaction = new \App\StudentTransaction();
                    $stdtransaction->paymentDate = today_date();
                    $stdtransaction->totalPaid = $post_data[4];
                    $stdtransaction->paymentMode = 'Online';
                    $stdtransaction->receiptNo = $post_data[2];
                    $stdtransaction->idFinancialYear = $students->idFinancialYear;
                    $stdtransaction->idSchool = $students->idSchool;
                    $stdtransaction->idStudent = $students->idStudent;
                    $stdtransaction->status = 'Cancelled';
                    $stdtransaction->fine = 0;
                    $stdtransaction->excessAmountUsed = 0;
                    $stdtransaction->penaltyAmount = 0;
                    $stdtransaction->discount = 0;
                    $stdtransaction->save();

                    DB::table('student_transaction_online')
                            ->where('idOnline', $prevOnlineTransaction->idOnline)
                            ->update([
                                'onlineTransactionId' => $post_data[2],
                                'paymentDate' => today_date(),
                                'code' => $post_data[14],
                                'status' => 'Cancelled',
                                'remarks' => $post_data[5],
                                'errorMessage' => $post_data[24]
                    ]);
                }

                return redirect('student/feesubmission')->with('status', $post_data[14] == '0300' ? 'Payment is successful download the receipt from below ' : 'Payment cancelled or failed due to ' . $post_data[24]);
            } else {
                return redirect('student/feesubmission')->with('status', 'Payment cancelled or failed due to ' . $post_data[24]);
            }
        } else {
            return redirect('student/feesubmission')->with('status', 'Payment cancelled or failed');
        }
    }

    public function paymentBusSuceesful(Request $request) {
        if (isset($_POST)) {
            if ($this->isBusValidHash($_POST) == 1) {

                $post_data = explode("|", $_POST['msg']);
                $prevOnlineTransaction = DB::table('student_transaction_online')->where('idReference', $post_data[1])->first();
                $students = \App\AdmEntry::where('idStudent', '=', $prevOnlineTransaction->idStudent)->first();
                $stdtransaction = \App\StudentTransaction::where('receiptNo', '=', $post_data[2])->first();
                if (isset($stdtransaction->totalPaid)) {
                    return redirect('student/paytransportfees')->with('status', 'Payment Error');
                }



                if ($post_data[13] != 'null' && $post_data[13] != 'NA') {
                    $transactionEnd = Carbon::createFromFormat('d-m-Y H:i:s', $post_data[13]);
                    $transactionEnd->setTimezone('Asia/Kolkata');
                } else {
                    $transactionEnd = Carbon::now();
                    $transactionEnd->setTimezone('Asia/Kolkata');
                }

                $transactionStart = Carbon::createFromFormat('Y-m-d H:i:s', $prevOnlineTransaction->created_at);
                $transactionStart->setTimezone('Asia/Kolkata');


                if ($transactionEnd->diffInMinutes($transactionStart) < 30 && $post_data[14] == '0300') {

                    $total_paid = $post_data[4];
                    DB::beginTransaction();
                    try {

                        $payableAmount = $prevOnlineTransaction->payableAmount;
                        $penaltyAmount = 0;

                        $chqbounce = \App\BusTransaction::where('idStudent', '=', $request->idStudent)
                                ->where('idFinancialYear', '=', $students->idFinancialYear)
                                ->where('status', '=', 'Bounced')
                                ->where('chqFineStatus', '=', '0')
                                ->get();

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

                        $stdtransaction = new \App\BusTransaction();
                        $stdtransaction->idStudent = $students->idStudent;
                        $stdtransaction->idFinancialYear = $students->idFinancialYear;
                        $stdtransaction->paymentDate = today_date();
                        $stdtransaction->totalPaid = $post_data[4];
                        $stdtransaction->paymentMode = 'Online';
                        $stdtransaction->receiptNo = $post_data[2];
                        $stdtransaction->idSchool = $students->idSchool;
                        $stdtransaction->status = 'Success';
                        $stdtransaction->penaltyAmount = $penaltyAmount;
                        $stdtransaction->fine = $prevOnlineTransaction->penaltyAmount - $penaltyAmount;
                        $stdtransaction->discount = 0;
                        $stdtransaction->save();

                        $totalFine = 0;
                        $totalDiscount = 0;

                        //for loop for headers
                        $totalDiscount = 0;
                        $selectedHeaders = json_decode($prevOnlineTransaction->idFeehead, true);

                        foreach ($selectedHeaders as $headers) {
                            $subheaders = \App\NorthFeeHead::where('isParent', '=', $headers)->get()->toArray();
                            
                            foreach($subheaders as $headerIds)
                                {
                                    $stdtransaction_detail = new \App\BusTransactionDetail();
                                    $stdtransaction_detail->idStudent = $students->idStudent;
                                    $stdtransaction_detail->idBusTransaction = $stdtransaction->idBusTransaction;
                                    $stdtransaction_detail->idBusFeehead = $headerIds['idFeehead'];
                                    $stdtransaction_detail->paymentDate = today_date();
        
                                    $stdtransaction_detail->amountPaid = $headerIds['amount'];
                                    $stdtransaction_detail->fine = 0;
                                    $stdtransaction_detail->isNorth = 2;
                                    $stdtransaction_detail->discount = $headerIds['discount'];
                                    $totalDiscount = $totalDiscount + $headerIds['discount'];
                                    $stdtransaction_detail->save();
                                }
                        }

                        if ($totalDiscount > 0) {
                            $stdtransaction->discount = $totalDiscount;
                            $stdtransaction->update();
                        }

                        if (isset($chqbounce)) {
                            foreach ($chqbounce as $chqFine) {
                                $chqFine->chqFineStatus = 1;
                                $chqFine->update();
                            }
                        }
                        DB::commit();
                    } catch (\Exception $e) {
                        DB::rollBack();
                    }
                    DB::table('student_transaction_online')
                            ->where('idOnline', $prevOnlineTransaction->idOnline)
                            ->update([
                                'onlineTransactionId' => $post_data[2],
                                'paymentDate' => today_date(),
                                'code' => $post_data[14],
                                'status' => 'Success',
                                'remarks' => $post_data[5],
                                'errorMessage' => $post_data[24]
                    ]);
                } else {
                    $stdtransaction = new \App\BusTransaction();
                    $stdtransaction->paymentDate = today_date();
                    $stdtransaction->totalPaid = $post_data[4];
                    $stdtransaction->paymentMode = 'Online';
                    $stdtransaction->receiptNo = $post_data[2];
                    $stdtransaction->idFinancialYear = $students->idFinancialYear;
                    $stdtransaction->idSchool = $students->idSchool;
                    $stdtransaction->idStudent = $students->idStudent;
                    $stdtransaction->status = 'Cancelled';
                    $stdtransaction->fine = 0;
                    $stdtransaction->excessAmountUsed = 0;
                    $stdtransaction->penaltyAmount = 0;
                    $stdtransaction->discount = 0;
                    $stdtransaction->save();

                    DB::table('student_transaction_online')
                            ->where('idOnline', $prevOnlineTransaction->idOnline)
                            ->update([
                                'onlineTransactionId' => $post_data[2],
                                'paymentDate' => today_date(),
                                'code' => $post_data[14],
                                'status' => 'Cancelled',
                                'remarks' => $post_data[5],
                                'errorMessage' => $post_data[24]
                    ]);
                }

                return redirect('student/paytransportfees')->with('status', $post_data[14] == '0300' ? 'Payment is successful download the receipt from below ' : 'Payment cancelled or failed due to ' . $post_data[24]);
            } else {
                return redirect('student/paytransportfees')->with('status', 'Payment cancelled or failed due to ' . $post_data[24]);
            }
        } else {
            return redirect('student/paytransportfees')->with('status', 'Payment cancelled or failed');
        }
    }

    public function paymentHostelSuceesful(Request $request) {
        if (isset($_POST)) {
            if ($this->isHostelValidHash($_POST) == 1) {

                $post_data = explode("|", $_POST['msg']);
                $prevOnlineTransaction = DB::table('student_transaction_online')->where('idReference', $post_data[1])->first();
                $students = \App\AdmEntry::where('idStudent', '=', $prevOnlineTransaction->idStudent)->first();
                $stdtransaction = \App\StudentTransaction::where('receiptNo', '=', $post_data[2])->first();
                if (isset($stdtransaction->totalPaid)) {
                    return redirect('student/hostelfee')->with('status', 'Payment Error');
                }



                if ($post_data[13] != 'null' && $post_data[13] != 'NA') {
                    $transactionEnd = Carbon::createFromFormat('d-m-Y H:i:s', $post_data[13]);
                    $transactionEnd->setTimezone('Asia/Kolkata');
                } else {
                    $transactionEnd = Carbon::now();
                    $transactionEnd->setTimezone('Asia/Kolkata');
                }

                $transactionStart = Carbon::createFromFormat('Y-m-d H:i:s', $prevOnlineTransaction->created_at);
                $transactionStart->setTimezone('Asia/Kolkata');


                if ($transactionEnd->diffInMinutes($transactionStart) < 30 && $post_data[14] == '0300') {

                    $total_paid = $post_data[4];
                    DB::beginTransaction();
                    try {

                        $payableAmount = $prevOnlineTransaction->payableAmount;
                        $penaltyAmount = 0;

                        $chqbounce = \App\HostelTransaction::where('idStudent', '=', $request->idStudent)
                                ->where('idFinancialYear', '=', $students->idFinancialYear)
                                ->where('status', '=', 'Bounced')
                                ->where('chqFineStatus', '=', '0')
                                ->get();

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

                        $stdtransaction = new \App\HostelTransaction();
                        $stdtransaction->idStudent = $students->idStudent;
                        $stdtransaction->idFinancialYear = $students->idFinancialYear;
                        $stdtransaction->paymentDate = today_date();
                        $stdtransaction->totalPaid = $post_data[4];
                        $stdtransaction->paymentMode = 'Online';
                        $stdtransaction->receiptNo = $post_data[2];
                        $stdtransaction->idSchool = $students->idSchool;
                        $stdtransaction->status = 'Success';
                        $stdtransaction->penaltyAmount = $penaltyAmount;
                        $stdtransaction->fine = $prevOnlineTransaction->penaltyAmount - $penaltyAmount;
                        $stdtransaction->discount = 0;
                        $stdtransaction->save();

                        $totalFine = 0;
                        $totalDiscount = 0;

                        //for loop for headers
                        $totalDiscount = 0;
                        $selectedHeaders = json_decode($prevOnlineTransaction->idFeehead, true);

                        foreach ($selectedHeaders as $headers) {
                            $subheaders = \App\NorthFeeHead::where('isParent', '=', $headers)->get()->toArray();
                            
                            foreach($subheaders as $headerIds)
                                {
                                $stdtransaction_detail = new \App\HostelTransactionDetail();
                                $stdtransaction_detail->idStudent = $students->idStudent;
                                $stdtransaction_detail->idHostelTransaction = $stdtransaction->idHostelTransaction;
                                $stdtransaction_detail->idFeehead = $headerIds['idFeehead'];
                                $stdtransaction_detail->paymentDate = today_date();
    
                                $stdtransaction_detail->amountPaid = $headerIds['amount'];
                                $stdtransaction_detail->fine = 0;
                                $stdtransaction_detail->isNorth = 2;
                                $stdtransaction_detail->discount = $headerIds['discount'];
                                $totalDiscount = $totalDiscount + $headerIds['discount'];
                                $stdtransaction_detail->save();
                                }
                        }

                        if ($totalDiscount > 0) {
                            $stdtransaction->discount = $totalDiscount;
                            $stdtransaction->update();
                        }

                        if (isset($chqbounce)) {
                            foreach ($chqbounce as $chqFine) {
                                $chqFine->chqFineStatus = 1;
                                $chqFine->update();
                            }
                        }
                        DB::commit();
                    } catch (\Exception $e) {
                        DB::rollBack();
                    }
                    DB::table('student_transaction_online')
                            ->where('idOnline', $prevOnlineTransaction->idOnline)
                            ->update([
                                'onlineTransactionId' => $post_data[2],
                                'paymentDate' => today_date(),
                                'code' => $post_data[14],
                                'status' => 'Success',
                                'remarks' => $post_data[5],
                                'errorMessage' => $post_data[24]
                    ]);
                } else {
                    $stdtransaction = new \App\HostelTransaction();
                    $stdtransaction->paymentDate = today_date();
                    $stdtransaction->totalPaid = $post_data[4];
                    $stdtransaction->paymentMode = 'Online';
                    $stdtransaction->receiptNo = $post_data[2];
                    $stdtransaction->idFinancialYear = $students->idFinancialYear;
                    $stdtransaction->idSchool = $students->idSchool;
                    $stdtransaction->idStudent = $students->idStudent;
                    $stdtransaction->status = 'Cancelled';
                    $stdtransaction->fine = 0;
                    $stdtransaction->excessAmountUsed = 0;
                    $stdtransaction->penaltyAmount = 0;
                    $stdtransaction->discount = 0;
                    $stdtransaction->save();

                    DB::table('student_transaction_online')
                            ->where('idOnline', $prevOnlineTransaction->idOnline)
                            ->update([
                                'onlineTransactionId' => $post_data[2],
                                'paymentDate' => today_date(),
                                'code' => $post_data[14],
                                'status' => 'Cancelled',
                                'remarks' => $post_data[5],
                                'errorMessage' => $post_data[24]
                    ]);
                }

                return redirect('student/hostelfee')->with('status', $post_data[14] == '0300' ? 'Payment is successful download the receipt from below ' : 'Payment cancelled or failed due to ' . $post_data[24]);
            } else {
                return redirect('student/hostelfee')->with('status', 'Payment cancelled or failed due to ' . $post_data[24]);
            }
        } else {
            return redirect('student/hostelfee')->with('status', 'Payment cancelled or failed');
        }
    }

    function isValidHash($response) {
        $post_data = explode("|", $response['msg']);
        $hash_data = $post_data[0];
        for ($i = 1; $i < sizeof($post_data) - 1; $i++) {
            $hash_data .= '|' . ($post_data[$i]);
        }
        $checksum = hash_hmac('sha256', $hash_data, '3MZYkfUVPdW0', false);
        $checksum = strtoupper($checksum);
        $valid_hash = ($post_data[sizeof($post_data) - 1] == $checksum) ? 'Yes' : 'No';

        if ($valid_hash == 'Yes') {
            return 1;
        } else
            return 0;
    }

    function isBusValidHash($response) {
        $post_data = explode("|", $response['msg']);
        $hash_data = $post_data[0];
        for ($i = 1; $i < sizeof($post_data) - 1; $i++) {
            $hash_data .= '|' . ($post_data[$i]);
        }
        $checksum = hash_hmac('sha256', $hash_data, 'tMOaMSP14Aj3AgK5U63YUfl4trCbw34D', false);
        //D4oiuL6drtAx8A15r3IluOuXLuKWBmzO $checksum = hash_hmac('sha256', $hash_data, '97a0BQYvMDkS', false);
        $checksum = strtoupper($checksum);
        $valid_hash = ($post_data[sizeof($post_data) - 1] == $checksum) ? 'Yes' : 'No';

        if ($valid_hash == 'Yes') {
            return 1;
        } else
            return 0;
    }

    function isHostelValidHash($response) {
        $post_data = explode("|", $response['msg']);
        $hash_data = $post_data[0];
        for ($i = 1; $i < sizeof($post_data) - 1; $i++) {
            $hash_data .= '|' . ($post_data[$i]);
        }
        $checksum = hash_hmac('sha256', $hash_data, 'D4oiuL6drtAx8A15r3IluOuXLuKWBmzO', false);
        //D4oiuL6drtAx8A15r3IluOuXLuKWBmzO $checksum = hash_hmac('sha256', $hash_data, 'FxawYNwqoJaM', false);
        $checksum = strtoupper($checksum);
        $valid_hash = ($post_data[sizeof($post_data) - 1] == $checksum) ? 'Yes' : 'No';

        if ($valid_hash == 'Yes') {
            return 1;
        } else
            return 0;
    }

    public function accessInfo(Request $request) {

        $rules = [];
        $rules += [
            'amount' => 'required'
        ];
        $messages = [
            'amount.required' => 'Enter The Amount.'
        ];
        $this->validate($request, $rules, $messages);
        $total_paid = 0;
        $payableAmount = 0;
        $student = \App\AdmEntry::where('idStudent', '=', $request->idStudent)->first();
        $prev_transaction = \App\StudentTransaction::where('idStudent', '=', $request->idStudent)
                        ->where('idFinancialYear', '=', $request->idFinancialYear)
                        ->where(function($query) {
                            $query->whereNull('status');
                            $query->orWhere('status', '=', 'Cleared');
                            $query->orWhere('status', '=', 'In-Process');
                            $query->orWhere('status', '=', 'Success');
                        })
                        ->orderBy('idTransaction', 'desc')->first();

        $total_paid = 0;
        if (isset($request->payable))
            $payableAmount = $request->payable;
        else
            $payableAmount = 0;
        $excessAmount = 0;
        $penaltyAmount = 0;

        if (isset($prev_transaction)) {
            if ($payableAmount > 0)
                $payableAmount = $payableAmount + $prev_transaction->excessAmount;
            else if ($payableAmount == 0)
                $excessAmount = $excessAmount + $prev_transaction->excessAmount;
        }


        $chqbounce = \App\StudentTransaction::where('idStudent', '=', $request->idStudent)
                ->where('idFinancialYear', '=', $request->idFinancialYear)
                ->where('status', '=', 'Bounced')
                ->where('chqFineStatus', '=', '0')
                ->get();
        if (isset($chqbounce)) {
            foreach ($chqbounce as $chqFine)
                $penaltyAmount = $penaltyAmount + $chqFine->chequeBounceCharge;
        }

        $stdtransaction = new \App\StudentTransaction();
        $stdtransaction->fill($request->all());
        $stdtransaction->paymentDate = todays_date();
        $stdtransaction->totalPaid = $payableAmount;
        $stdtransaction->status = 'Success';
        $stdtransaction->paymentMode = 'Online';
        $next_receiptno = student_receiptno();
        $stdtransaction->receiptNo = 'ACSFP000' . $next_receiptno;
        $stdtransaction->idSchool = $student->idSchool;
        $stdtransaction->penaltyAmount = $penaltyAmount;
        if ($payableAmount > 0)
            $stdtransaction->excessAmount = $excessAmount;
        $stdtransaction->fine = 0;
        $stdtransaction->discount = 0;

        $stdtransaction->save();

        $totalFine = 0;
        $totalDiscount = 0;
        foreach ($request->selectedHeaders as $key => $var) {
            $stepFine = 0;
            $stepDiscount = 0;
            $stepAmount = 0;
            $headerObject = \App\FeeHead::where('idFeehead', '=', $var)->first()->toArray();
            $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);
                } else
                if ($headerObject['flatFine'] != null) {
                    $stepFine = $stepFine + $headerObject['flatFine'];
                }
            }

            $discountfees = DB::table('student_discounts')->where('idFinancialYear', '=', $request->idFinancialYear)->where('idStudent', '=', $request->idStudent)->where('idFeehead', '=', $headerObject['idFeehead'])->where('isActive', 'Y')->get();

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


            $stepAmount = ( $headerObject['amount'] + $stepFine );
            $stepAmount = $stepAmount - $stepDiscount;
            $totalFine = $totalFine + $stepFine;

            $stdtransaction_detail = new \App\StudentTransactionDetail();
            $stdtransaction_detail->idStudent = $request->idStudent;
            $stdtransaction_detail->idTransaction = $stdtransaction->idTransaction;
            $stdtransaction_detail->idFeehead = $var;
            if ($request->has('paymentDate') && $request->paymentDate != null) {
                $stdtransaction_detail->paymentDate = $request->paymentDate;
            } else {
                $stdtransaction_detail->paymentDate = todays_date();
            }

            $stdtransaction_detail->amountPaid = $stepAmount;
            $stdtransaction_detail->fine = $stepFine;
            $stdtransaction_detail->discount = $stepDiscount;

            if ($request->paymentMode == 'Cheque' || $request->paymentMode == 'DD') {
                $stdtransaction_detail->status = 'In-Process';
            }
            $stdtransaction_detail->save();
            $excessAmount = $excessAmount - $stepAmount;
        }

        if ($payableAmount == 0)
            $stdtransaction->excessAmount = $excessAmount;

        if (isset($prev_transaction)) {
            if ($payableAmount == 0)
                $stdtransaction->accessPaid = $prev_transaction->excessAmount - $excessAmount;
            else
                $stdtransaction->accessPaid = $prev_transaction->excessAmount;
        } else
            $stdtransaction->accessPaid = 0;

        $stdtransaction->discount = $totalDiscount;
        $stdtransaction->fine = $totalFine;
        $stdtransaction->update();

        if (isset($chqbounce)) {
            foreach ($chqbounce as $chqFine) {
                $chqFine->chqFineStatus = 1;
                $chqFine->update();
            }
        }
        flash('Fees has been submitted successfully!!');
        if ($request->ajax()) {
            return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
        }
    }

    public function paymentInfo(Request $request) {

        $response = $_POST;
        if (is_array($response)) {
            $str = $response['msg'];
        } else if (is_string($response) && strstr($response, 'msg=')) {
            $outputStr = str_replace('msg=', '', $response);
            $outputArr = explode('&', $outputStr);
            $str = $outputArr[0];
        } else {
            $str = $response;
        }

        $transactionResponseBean = new TransactionResponseBean();

        $transactionResponseBean->setResponsePayload($str);
        $transactionResponseBean->setKey("6600681084OHFNJU");
        $transactionResponseBean->setIv("7536264808NGVHQW");

        $response = $transactionResponseBean->getResponsePayload();
        $post_data = explode("|", $response);

        $prevOnlineTransaction = DB::table('student_transaction_online')->where('idReference', explode("=", $post_data[3])[1])->first();

        $students = \App\AdmEntry::where('idStudent', '=', $prevOnlineTransaction->idStudent)->first();

        $stdtransaction = \App\StudentTransaction::where('receiptNo', '=', explode("=", $post_data[5])[1])->first();
        if (isset($stdtransaction)) {
            return redirect('student/payfees')->with('status', 'Payment Error');
        }

        if (explode("=", $post_data[8])[1] == 'NA') {
            $transactionEnd = Carbon::now();
            $transactionEnd->setTimezone('Asia/Kolkata');
        } else {
            $transactionEnd = Carbon::createFromFormat('d-m-Y H:i:s', explode("=", $post_data[8])[1]);
            $transactionEnd->setTimezone('Asia/Kolkata');
        }

        $transactionStart = Carbon::createFromFormat('Y-m-d H:i:s', $prevOnlineTransaction->created_at);
        $transactionStart->setTimezone('Asia/Kolkata');


        if ($transactionEnd->diffInMinutes($transactionStart) < 30 && explode("=", $post_data[0])[1] == '0300') {

            $total_paid = explode("=", $post_data[6])[1];
            $prev_transaction = \App\StudentTransaction::where('idStudent', '=', $students->idStudent)
                            ->where('idFinancialYear', '=', $students->idFinancialYear)
                            ->where(function($query) {
                                $query->whereNull('status');
                                $query->orWhere('status', '=', 'Cleared');
                                $query->orWhere('status', '=', 'In-Process');
                                $query->orWhere('status', '=', 'Success');
                            })
                            ->orderBy('idTransaction', 'desc')->first();

            $payableAmount = $prevOnlineTransaction->payableAmount;

            $excessAmount = $total_paid - $payableAmount;
            $penaltyAmount = 0;

            if (isset($prev_transaction)) {
                if ($payableAmount > 0)
                    $payableAmount = $payableAmount + $prev_transaction->excessAmount;
                else if ($payableAmount == 0)
                    $excessAmount = $excessAmount + $prev_transaction->excessAmount;
            }


            $chqbounce = \App\StudentTransaction::where('idStudent', '=', $request->idStudent)
                    ->where('idFinancialYear', '=', $students->idFinancialYear)
                    ->where('status', '=', 'Bounced')
                    ->where('chqFineStatus', '=', '0')
                    ->get();
            if (isset($chqbounce)) {
                foreach ($chqbounce as $chqFine)
                    $penaltyAmount = $penaltyAmount + $chqFine->chequeBounceCharge;
            }

            $stdtransaction = new \App\StudentTransaction();
            $stdtransaction->idStudent = $students->idStudent;
            $stdtransaction->idFinancialYear = $students->idFinancialYear;
            $stdtransaction->paymentDate = todays_date();
            $stdtransaction->totalPaid = explode("=", $post_data[6])[1];
            $stdtransaction->paymentMode = 'Online';
            $stdtransaction->receiptNo = explode("=", $post_data[5])[1];
            $stdtransaction->idSchool = $students->idSchool;
            $stdtransaction->status = 'Success';
            $stdtransaction->penaltyAmount = $penaltyAmount;
            if ($payableAmount > 0)
                $stdtransaction->excessAmount = $excessAmount;
            $stdtransaction->fine = 0;
            $stdtransaction->discount = 0;
            $stdtransaction->save();

            $totalFine = 0;
            $totalDiscount = 0;
            $selectedHeaders = json_decode($prevOnlineTransaction->idFeehead, true);

            foreach ($selectedHeaders as $var) {
                $stepFine = 0;
                $stepDiscount = 0;
                $stepAmount = 0;
                $headerObject = \App\FeeHead::where('idFeehead', '=', $var)->first()->toArray();
                $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);
                    } else
                    if ($headerObject['flatFine'] != null) {
                        $stepFine = $stepFine + $headerObject['flatFine'];
                    }
                }

                $discountfees = DB::table('student_discounts')->where('idFinancialYear', '=', $students->idFinancialYear)->where('idStudent', '=', $students->idStudent)->where('idFeehead', '=', $headerObject['idFeehead'])->where('isActive', 'Y')->get();

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


                $stepAmount = ( $headerObject['amount'] + $stepFine );
                $stepAmount = $stepAmount - $stepDiscount;
                $totalFine = $totalFine + $stepFine;

                $stdtransaction_detail = new \App\StudentTransactionDetail();
                $stdtransaction_detail->idStudent = $students->idStudent;
                $stdtransaction_detail->idTransaction = $stdtransaction->idTransaction;
                $stdtransaction_detail->idFeehead = $var;
                $stdtransaction_detail->paymentDate = todays_date();
                $stdtransaction_detail->amountPaid = $stepAmount;
                $stdtransaction_detail->fine = $stepFine;
                $stdtransaction_detail->discount = $stepDiscount;
                $stdtransaction_detail->save();

                $excessAmount = $excessAmount - $stepAmount;
            }

            if ($payableAmount == 0)
                $stdtransaction->excessAmount = $excessAmount;

            if (isset($prev_transaction)) {
                if ($payableAmount == 0)
                    $stdtransaction->accessPaid = $prev_transaction->excessAmount - $excessAmount;
                else
                    $stdtransaction->accessPaid = $prev_transaction->excessAmount;
            } else
                $stdtransaction->accessPaid = 0;

            $stdtransaction->discount = $totalDiscount;
            $stdtransaction->fine = $totalFine;
            $stdtransaction->update();

            if (isset($chqbounce)) {
                foreach ($chqbounce as $chqFine) {
                    $chqFine->chqFineStatus = 1;
                    $chqFine->update();
                }
            }

            DB::table('student_transaction_online')
                    ->where('idOnline', $prevOnlineTransaction->idOnline)
                    ->update([
                        'onlineTransactionId' => explode("=", $post_data[5])[1],
                        'paymentDate' => todays_date(),
                        'code' => explode("=", $post_data[0])[1],
                        'status' => 'Success',
                        'remarks' => explode("=", $post_data[1])[1],
                        'errorMessage' => explode("=", $post_data[2])[1]
            ]);
        } else {
            $stdtransaction = new \App\StudentTransaction();
            $stdtransaction->paymentDate = todays_date();
            $stdtransaction->totalPaid = explode("=", $post_data[6])[1];
            $stdtransaction->paymentMode = 'Online';
            $stdtransaction->receiptNo = explode("=", $post_data[5])[1];
            $stdtransaction->idFinancialYear = $students->idFinancialYear;
            $stdtransaction->idSchool = $students->idSchool;
            $stdtransaction->idStudent = $students->idStudent;
            $stdtransaction->status = 'Cancelled';
            $stdtransaction->fine = 0;
            $stdtransaction->accessPaid = 0;
            $stdtransaction->penaltyAmount = 0;
            $stdtransaction->excessAmount = 0;
            $stdtransaction->discount = 0;
            $stdtransaction->save();

            DB::table('student_transaction_online')
                    ->where('idOnline', $prevOnlineTransaction->idOnline)
                    ->update([
                        'onlineTransactionId' => explode("=", $post_data[5])[1],
                        'paymentDate' => todays_date(),
                        'code' => explode("=", $post_data[0])[1],
                        'status' => 'Cancelled',
                        'remarks' => explode("=", $post_data[1])[1],
                        'errorMessage' => explode("=", $post_data[2])[1]
            ]);
        }

        return redirect('student/payfees')->with('status', explode("=", $post_data[0])[1] == '0300' ? 'Payment is successful download the receipt from below ' : 'Payment cancelled or failed due to ' . explode("=", $post_data[2])[1]);
    }

    public function printReceipt($type,$id) {
        $student = \App\AdmEntry::where('idStudent', '=', Session::get('idStudent'))->first();
        $school = \App\School::where('idSchool', '=', $student->idSchool)->first();
        if ($type == 'std') {
            $transaction = \App\StudentTransaction::where('idTransaction', '=', $id)->first();
        }
        if($type == 'bus'){
            $transaction = \App\BusTransaction::where('idBusTransaction', '=', $id)->first();
        }
        if($type == 'hostel'){
            $transaction = \App\HostelTransaction::where('idHostelTransaction', '=', $id)->first();
        }
        $feehead_ids = $transaction->details->pluck('idParent')->toArray();
        $demands = \App\NorthFeeHead::where('isParent', '=', '0')
                ->whereIn('idFeehead', $feehead_ids)
                ->get();

        $class_feeheads = \DB::table('northfeeheads')->where('idClass', '=', $student->idClass)
                ->where('idSection', '=', $student->idSection)
                ->where('studentCategory', '=', $student->studentType)
                ->where('isParent', 0)
                ->whereNull('idStudent');
        $feeheads = \DB::table('northfeeheads')->where('idStudent', '=', $student->idStudent)->where('isParent', 0)
                ->union($class_feeheads)
                ->orderBy('toDate')
                ->get();
        $feetotal = $feeheads->sum('amount');

        $pdf = PDF::loadView('schools.north.transaction.print_receipt', ['margin_top' => 20], compact('demands', 'transaction', 'school', 'student', 'feetotal'));
        return $pdf->stream('feereceipt.pdf');
    }

    public function lesserAmtReceipt($type, $id) {
        $student = \App\AdmEntry::where('idStudent', '=', Session::get('idStudent'))->first();
        $school = \App\School::where('idSchool', '=', $student->idSchool)->first();
        if ($type == 'std') {
            $transaction = \App\LesserTransaction::where('idLesserTransaction', '=', $id)->first();
        }
        if($type == 'bus'){
            $transaction = \App\BusLesserTransaction::where('idBusLesserTransaction', '=', $id)->first();
        }
        if($type == 'hostel'){
            $transaction = \App\HostelLesserTransaction::where('idHostelLesserTransaction', '=', $id)->first();
        }
        $feehead_ids = $transaction->details->pluck('idFeehead')->toArray();
        $demands = \App\NorthFeeHead::where('isParent', '=', '0')
                ->whereIn('idFeehead', $feehead_ids)
                ->get();
        $class_feeheads = \DB::table('northfeeheads')->where('idClass', '=', $student->idClass)
                ->where('idSection', '=', $student->idSection)
                ->where('studentCategory', '=', $student->studentType)
                ->where('isParent', 0)
                ->whereNull('idStudent');
        $feeheads = \DB::table('northfeeheads')->where('idStudent', '=', $student->idStudent)->where('isParent', 0)
                ->union($class_feeheads)
                ->orderBy('toDate')
                ->get();
        $feetotal = $feeheads->sum('amount');

        $pdf = PDF::loadView('schools.north.transaction.print_lesseramt_receipt', ['margin_top' => 20], compact('demands', 'transaction', 'school', 'student', 'feetotal'));
        return $pdf->stream('feereceipt.pdf');
    }

}

Copyright © 2021 - 2025 IMMREX7