<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ClientPaymentReportsController extends Controller
{
    
    public function index(){
        $data = [
            'page_title' => 'Client Payment Report',
            'current_user' => session('current_user')
        ];
        return view('reports.index', $data);
    }
    public function getClientPaymentsJson(Request $request)
    {
        #$this->log_query();
        $payment_arr = \DB::table('client_finances')
            ->join('auth_users AS staffcreate', 'staffcreate.id', '=', 'client_finances.user_id')
            ->join('clients', 'clients.id', '=', 'client_finances.client_id')
            ->select('client_finances.id', 'clients.name AS clientName', 'client_finances.invoice_number', 'client_finances.invoice_amount', 'client_finances.invoice_date', 'client_finances.services', 'client_finances.invoice_status', 'client_finances.remarks',  'staffcreate.name As createdBy', 'client_finances.created_at')
            ->orderBy('client_finances.created_at', 'DESC')
            ->paginate(15);

        if($request->has('keyword')){
            $keyword = $request->keyword;
            $payment_arr = \DB::table('client_finances')
            ->join('auth_users AS staffcreate', 'staffcreate.id', '=', 'client_finances.user_id')
            ->join('clients', 'clients.id', '=', 'client_finances.client_id')
            ->select('client_finances.id', 'clients.name AS clientName', 'client_finances.invoice_number', 'client_finances.invoice_amount', 'client_finances.invoice_date', 'client_finances.services', 'client_finances.invoice_status', 'client_finances.remarks',  'staffcreate.name As createdBy', 'client_finances.created_at')
            ->whereRaw("client_finances.invoice_amount LIKE '%$keyword%' OR client_finances.invoice_status LIKE '%$keyword%' OR client_finances.invoice_number LIKE '%$keyword%' OR client_finances.invoice_amount LIKE '%$keyword%' OR staffcreate.name LIKE '%$keyword%' OR client_finances.created_at LIKE '%$keyword%'")
            ->orderBy('client_finances.created_at', 'DESC')
            ->paginate(15);
        }     
        return response()->json($payment_arr);
    }
}
