MOON
Server: Apache
System: Linux e2e-78-16.ssdcloudindia.net 3.10.0-1160.45.1.el7.x86_64 #1 SMP Wed Oct 13 17:20:51 UTC 2021 x86_64
User: imensosw (1005)
PHP: 7.4.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/imensosw/www/ezwork/app/Http/Controllers/UserBankAccountController.php
<?php

/**
 * UserBankAccountController class file
 *
 * PHP Version 7.2
 *
 * @category Controller
 * @package  Ez
 * @author   Imenso Software <admin@imensosoftware.com>
 * @license  http://imensosoftware/license.php GNU Public License
 * @link     http://imensosoftware.com/recipes
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use DB;
use App\Models\UserBankAccount;

/**
 * UserBankAccountController class
 *
 * The class manage the User Bank Account
 *
 * @category UserBankAccountController
 * @package  Ez
 * @author   Imenso Software <admin@imensosoftware.com>
 * @license  http://imensosoftware/license.php GNU Public License
 * @link     http://imensosoftware.com/recipes
 */

class UserBankAccountController extends Controller
{


    /**
     * GetUserBankAccounts function gets the bank account details of
     * specified user and return it in json format.
     *
     * @param  \Illuminate\Http\Request  request
     * @return json
     */
    public function getUserBankAccounts(Request $request): \Illuminate\Http\JsonResponse
    {
        $user_id            = Auth::user()->id;
        $request['user_id'] = $user_id;
        $userBankAccounts   = UserBankAccount::getUserBankAccounts($request);
        return response()->json(
            [
                'status'           => 'success',
                'userBankAccounts' => $userBankAccounts,
            ],
            200
        );
    }
    //  end getUserBankAccounts()


    /**
     * Insert function inserts the bank details of specified user in the
     * storage and return it in json format.
     *
     * @param  \Illuminate\Http\Request  request
     * @return json
     */
    public function insert(Request $request): \Illuminate\Http\JsonResponse
    {
        $user_id = Auth::user()->id;
        if ($request->payment_method_type_id == 2) {
            $validator = Validator::make(
                $request->all(),
                [
                    'paypal_id'              => 'required|unique:user_bank_accounts,paypal_id',
                    'payment_method_type_id' => 'required',
                ],
                [
                    'paypal_id.required'              => 'Please enter paypal id!',
                    'paypal_id.unique'   => 'PayPal id already exist!',
                    'payment_method_type_id.required' => 'Please select payment method!',
                ]
            );

            if ($validator->fails()) {
                return response()->json(
                    [
                    'status'  => 'error',
                    'message' => 'Please provide required field!.',
                    'errors'  => $validator->errors(),
                    ],
                    422
                );
            }

            $insertData = [
            'user_id'                => $user_id,
            'paypal_id'              => $request->paypal_id,
            'payment_method_type_id' => $request->payment_method_type_id,
            ];
        } else {
            $validator = Validator::make(
                $request->all(),
                [
                    'bank_name'              => 'required',
                    'beneficiary_name'       => 'required',
                    'account_no'             => 'required|numeric:6|unique:user_bank_accounts,account_no',
                    'ifsc_code'              => 'required',
                    'branch_address'         => 'required',
                    'payment_method_type_id' => 'required',
                ],
                [
                    'bank_name.required'              => 'Please enter bank name!',
                    'beneficiary_name.required'       => 'Please enter beneficiary name!',
                    'account_no.required'             => 'Please enter account no!',
                    'account_no.numeric'              => 'Account no must be digit!',
                    'account_no.unique'               => 'Account no. already exist!',
                    'ifsc_code.required'              => 'Please enter ifsc code!',
                    'branch_address.required'         => 'Please enter branch address!',
                    'payment_method_type_id.required' => 'Please select payment method!',
                ]
            );

            if ($validator->fails()) {
                return response()->json(
                    [
                    'status'  => 'error',
                    'message' => 'Please provide required field!.',
                    'errors'  => $validator->errors(),
                    ],
                    422
                );
            }

            $insertData = [
            'user_id'                => $user_id,
            'bank_name'              => $request->bank_name,
            'beneficiary_name'       => $request->beneficiary_name,
            'account_no'             => $request->account_no,
            'ifsc_code'              => $request->ifsc_code,
            'branch_address'         => $request->branch_address,
            'payment_method_type_id' => $request->payment_method_type_id,
            'bank_proof_type_id'     => 1,
            'bank_proof_file'        => 'a.png',
            ];
        }
        

        if ($request->is_primary == 1) {
            UserBankAccount::where('user_id', $user_id)->update(['is_primary' => 0]);
            $insertData['is_primary'] = $request->is_primary;
        }

        UserBankAccount::create($insertData);

        $data['user_id'] = $user_id;

        $userIdentification = \App\Models\UserIdentification::getUserIdentification(['user_id' => $user_id]);
        $uIdfy = 0;
        $uAdfy = 0;
        if($userIdentification)
        {
            $uIdfy = $userIdentification->photo_proof_type_id;
            $uAdfy = $userIdentification->address_proof_type_id;
        }
        if ($userIdentification && $uIdfy > 0 && $uAdfy > 0) {
            \App\Models\Profile::where('user_id', $user_id)->where('translator_status_id', 6)
                ->update(['translator_status_id' => 7]);
        }
        $userBankAccounts = UserBankAccount::getUserBankAccounts($data);

        $translatorStatus = \App\Models\Profile::select(
            'profiles.translator_status_id',
            'translator_status.translator_status_name as translator_status_name'
        )
            ->join(
                'translator_status',
                'translator_status.id',
                '=',
                'profiles.translator_status_id'
            )->where('profiles.user_id', $user_id)->first();

        return response()->json(
            [
                'status'                 => 'success',
                'message'                => 'Payment method added successfully.',
                'translator_status_id'   => $translatorStatus->translator_status_id,
                'translator_status_name' => $translatorStatus->translator_status_name,
                'userBankAccounts'       => $userBankAccounts,
            ],
            200
        );
    }
    //  end insert()


    /**
     * Update function updates the bank details of the specified user in storage
     * and return in json format.
     *
     * @param  \Illuminate\Http\Request  request
     * @return json
     */
    public function update(Request $request): \Illuminate\Http\JsonResponse
    {
        $user_id = Auth::user()->id;

        if ($request->payment_method_type_id == 2) {
            $validator = Validator::make(
                $request->all(),
                [
                    'paypal_id'              => 'required|unique:user_bank_accounts,paypal_id,
                ' . $request->id,
                    'payment_method_type_id' => 'required',
                ],
                [
                    'paypal_id.required'              => 'Please enter paypal id!',
                    'paypal_id.unique'   => 'PayPal id already exist!',
                    'payment_method_type_id.required' => 'Please select payment method!',
                ]
            );

            if ($validator->fails()) {
                return response()->json(
                    [
                    'status'  => 'error',
                    'message' => 'Please provide required field!.',
                    'errors'  => $validator->errors(),
                    ],
                    422
                );
            }

            $updateData = [
                'paypal_id'              => $request->paypal_id,
                'payment_method_type_id' => $request->payment_method_type_id,
                'bank_name'              => null,
                'beneficiary_name'       => null,
                'account_no'             => null,
                'ifsc_code'              => null,
                'branch_address'         => null,
                'bank_proof_type_id'     => null,
                'bank_proof_file'        => null,
            ];
        } else {
            $validator = Validator::make(
                $request->all(),
                [
                    'bank_name'              => 'required',
                    'beneficiary_name'       => 'required',
                    'account_no'             => 'required|numeric:6|unique:user_bank_accounts,account_no,' . $request->id,
                    'ifsc_code'              => 'required',
                    'branch_address'         => 'required',
                    'payment_method_type_id' => 'required',
                ],
                [
                    'bank_name.required'              => 'Please enter bank name!',
                    'beneficiary_name.required'       => 'Please enter beneficiary name!',
                    'account_no.required'             => 'Please enter account no!',
                    'account_no.numeric'              => 'Account no must be digit!',
                    'account_no.unique'               => 'Account no. already exist!',
                    'ifsc_code.required'              => 'Please enter ifsc code!',
                    'branch_address.required'         => 'Please enter branch address!',
                    'payment_method_type_id.required' => 'Please select payment method!',
                ]
            );

            if ($validator->fails()) {
                return response()->json(
                    [
                    'status'  => 'error',
                    'message' => 'Please provide required field!.',
                    'errors'  => $validator->errors(),
                    ],
                    422
                );
            }

            $updateData = [
                'bank_name'              => $request->bank_name,
                'beneficiary_name'       => $request->beneficiary_name,
                'account_no'             => $request->account_no,
                'ifsc_code'              => $request->ifsc_code,
                'branch_address'         => $request->branch_address,
                'payment_method_type_id' => $request->payment_method_type_id,
                'bank_proof_type_id'     => 1,
                'bank_proof_file'        => 'a.png',
            ];
        }

        if ($request->is_primary == 1) {
            UserBankAccount::where('user_id', $user_id)->update(['is_primary' => 0]);
            $updateData['is_primary'] = $request->is_primary;
        }
        
        UserBankAccount::where('id', $request->id)->update($updateData);
        
        $data['user_id']  = $user_id;
        $userBankAccounts = UserBankAccount::getUserBankAccounts($data);

        return response()->json(
            [
                'status'           => 'success',
                'message'          => 'Payment method updated successfully.',
                'userBankAccounts' => $userBankAccounts,
            ],
            200
        );
    }
    //  end update()


    /**
     * Delete function deletes the bank account details of specified
     * user from the storage and return json.
     *
     * @param  \Illuminate\Http\Request  request
     * @return json
     */
    public function delete(Request $request): \Illuminate\Http\JsonResponse
    {
        $validator = Validator::make(
            $request->all(),
            ['id' => 'required'],
            ['id.required' => 'Error in delete! Please try again.']
        );

        if ($validator->fails()) {
            return response()->json(
                [
                    'status'       => 'error',
                    'message'      => 'Error in delete! Please try again.',
                    'showErrorPop' => true,
                    'errors'       => $validator->errors(),
                ],
                422
            );
        }
        $message = '';
        $row = UserBankAccount::find($request->id);

        if ($row->status_id == 1) {
            UserBankAccount::where('id', $request->id)->update(['delete_status_id' => 1]);
            $message = 'Your payment method delete request has been sent to admin for approval.';
        } else {
            UserBankAccount::where('id', $request->id)->delete();
            $message = 'Payment method deleted successfully.';
        }

        $data['user_id']  = Auth::user()->id;
        $userBankAccounts = UserBankAccount::getUserBankAccounts($data);
        return response()->json(
            [
                'status'           => 'success',
                'message'          => $message,
                'userBankAccounts' => $userBankAccounts,
            ],
            200
        );
    }
    //  end delete()
}
//  end class