<?php

namespace App\Http\Controllers;

use App\Cawangan;
use App\Tunai;
use App\Gadai;
use App\AddTunai;
use App\RequestTeller;
use App\TunaiGadTeb;
use Illuminate\Http\Request;
use Carbon\Carbon;

class TunaiGadTebController extends Controller
{
    public function getCurrentTunaiGadTeb($kodcaw,Request $request)
    {
        $current = Carbon::now();
        $current = new Carbon();

        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();

        $tunaiGadteb=TunaiGadTeb::on($cawangan['mysql'])
        ->where([['kodcaw','=',$kodcaw],['tarikh','=',$current->toDateString()],['kodlaluan','=',$request->kodlaluan]])
        ->orderBy('tarikh','desc')
        ->first();
        return response()->json($tunaiGadteb);
    }

    public function UpdateTunaiGadTeb($kodcaw, Request $request)
    {
        $current = Carbon::now();
        $current = new Carbon();
       
        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
        $updatetunaiGD=TunaiGadTeb::on($cawangan['mysql'])->where( [['tarikh','=', $current->toDateString()],['kodlaluan','=', $request->kodlaluan] ])->
        update(array(
             'gadai'=>$request->gadai,
             'tebus'=>$request->tebus,
             'upah'=>$request->upah
          
        ));
           
        return response()->json($updatetunaiGD, 200);
    }

    public function createTunaiGadTeb($kodcaw, Request $request)
    {
        $current = Carbon::now();
        $current = new Carbon();
        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();

        $tunaigadteb=TunaiGadTeb::on($cawangan['mysql'])->create([
          'tarikh'=>$current->toDateString(),
          'kodcaw'=>$request->kodcaw,
          'kodlaluan'=>$request->teller,
          'prkod'=>$request->prkod,
          'gadai'=>$request->gadai,
          'tebus'=>$request->tebus,
          'upah'=>$request->upah,
          'lain'=>$request->lain,
          'bakilelong'=>$request->bakilelong,
          'byrbakilelong'=>$request->byrbakilelong

        ]);

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