<?php

namespace App\Http\Controllers;

use App\TransaksiKeuntungan;
use Illuminate\Http\Request;
use Carbon\Carbon;
use DB;

class TransaksiKeuntunganController extends Controller
{
    public function getTransaksiKeuntunganByRujukan($norujukan){
        $transaksi = TransaksiKeuntungan::on('mysql7')->where('norujukan','=',$norujukan)->get();

        return response()->json($transaksi);
    }

    public function getTransaksiKeuntunganByRujukanLatest($norujukan){
        $transaksi = TransaksiKeuntungan::on('mysql7')->where('norujukan','=',$norujukan)->orderBy('id','desc')->first();

        return response()->json($transaksi);
    }

    public function getLatestTransaksi_Untung($norujukan)  //added byy fariz 20210413
    {
        $transaksi = TransaksiKeuntungan::on('mysql7')->where('norujukan','=',$norujukan)->orderBy('id','desc')->limit(1)->get();
        return response()->json($transaksi);
    }
  
  
    public function GetjumlahBayaranPendahuluan($norujukan)
    {
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh = $current->toDateString();
    
        $transaction=DB::connection('mysql7')->select( "SELECT SUM(bayaran_pembiayaan) AS total_byr_pendahuluan FROM transaksi_keuntungan  WHERE norujukan='".$norujukan."' AND  DATE( tarikh_bayaran) = '".$tarikh."'" );
        return response()->json($transaction);
    }

    public function createTransaksiKeuntungan(Request $request){
        $transaksi = TransaksiKeuntungan::on('mysql7')->create([
            'norujukan' => $request->norujukan,
            'bayaran_keuntungan' =>$request->bayaran_keuntungan,
            'tarikh_bayaran'=> $request->tarikh_bayaran,
            'tempoh'=> $request->tempoh,
            'kodcaw'=> $request->kodcaw
        ]);

        return response()->json($transaksi,201);
    }
}
