<?php

namespace App\Http\Controllers;

use App\HargaEmas;
use App\GlCode;
use App\Cawangan;
use App\Glejer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use Carbon\Carbon;
class GlejerController extends Controller
{
    public function getGlejer($kodcaw, Request $request)
    {
        $current = Carbon::now();
        $current = new Carbon();
        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
       
       $glejer=Glejer::on($cawangan['mysql'])->where( [['kodcaw','=',$kodcaw],['tarikh','=',$current->toDateString()],['glcode','=',$request->glcode]])->get();
       return response()->json($glejer);

    }

    public  function Getglejer_Data($tarikh1,$tarikh2,$kodcaw)
    {

        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
        $db=$cawangan['mysql'];
  
        $glejer=DB::connection($db)->select( "SELECT glcode, descpt , SUM(debit)AS total_debit, SUM(kredit) AS total_kredit, SUM(hariini) AS totalharini FROM glejer  where tarikh BETWEEN '".$tarikh1."' and '".$tarikh2."' GROUP BY glcode,descpt ORDER BY glcode asc"  );
        return response()->json($glejer);
          
     }



    public function SumDebit_Glejer($tarikh1,$tarikh2,$kodcaw)
    {

        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
        $sumDebit=Glejer::on($cawangan['mysql'])->selectRaw( 'SUM(Debit) as totalDebit')->whereBetween('tarikh',[$tarikh1,$tarikh2])->get();
        return  response()->json($sumDebit);


    }


    public function SumKredit_Glejer($tarikh1,$tarikh2,$kodcaw)
    {

        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
        $sumDebit=Glejer::on($cawangan['mysql'])->selectRaw( 'SUM(Kredit) as totalKredit')->whereBetween('tarikh',[$tarikh1,$tarikh2])->get();
        return  response()->json($sumDebit);


    }

    public function getSemalam_Glejer($kodcaw, Request $request){
  
        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
        $glejer=Glejer::on($cawangan['mysql'])->where('glcode','=',$request->glcode)->orderBy('tarikh','DESC')->limit(1)->get();

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

    public function Create_Glejer($kodcaw,Request $request){

        $current = Carbon::now();
        $current = new Carbon();
        
        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
        $glejer=Glejer::on($cawangan['mysql'])->create([            
            // 'tarikh' => $current->toDateString(),
            'kodcaw'=> $request->kodcaw,
            'tarikh'=>$current->toDateString(),
            'glcode'=>$request->glcode,
            'descpt'=>$request->descpt,
            'debit'=>$request->debit,
            'kredit'=>$request->kredit,
            'hariini'=>$request->hariini,
            'kelmarin'=>$request->kelmarin,
            'jumlah'=>$request->jumlah,
            'bulanini'=>$request->bulanini,
            'tahunini'=>$request->tahunini,
            'acttype'=>$request->acttype
        ]);
        
        return response()->json($glejer,201);

              
    }


    public function carianBulananTahun_glejer($kodcaw,Request $request)
    {
        $current = Carbon::now();
        $current = new Carbon();
        $yr = Carbon::now()->format('Y');
        $month= Carbon::now()->format('m');
        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();

        $glejerMy=Glejer::on ($cawangan['mysql'])->whereYear('tarikh','=',$yr)->whereMonth('tarikh', '=',$month)->where('glcode', '=',$request->glcode)->
        orderBy('tarikh','ASC')->get();
        return response()->json($glejerMy);
    }

    public function Kemaskini_Glejer($kodcaw, Request $request)
    {

        $current = Carbon::now();
        $current = new Carbon();
   
        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
        $updateGLejer=Glejer::on($cawangan['mysql'])->where([['recno','=',$request->recno],['kodcaw','=', $kodcaw]])->
        update(array(
   
            'debit'=>$request->debit,
            'kredit'=>$request->kredit,
            'hariini'=>$request->hariini,
            'kelmarin'=>$request->kelmarin
        
        ));

        return response()->json($updateGLejer,200);    
    }

    public function carianTahun_glejer($kodcaw,Request $request)
    {
        $current = Carbon::now();
        $current = new Carbon();
        $yr = Carbon::now()->format('Y');
        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
        $db=$cawangan['mysql'];

        $gelejerYear=Glejer::on($cawangan['mysql'])->whereYear('tarikh','=',$yr)->where('glcode', '=',$request->glcode)->
        orderBy('tarikh','ASC')->get();
        return response()->json($gelejerYear);
    }
}
