<?php

namespace App\Http\Controllers;
use App\Vault;
use App\Cawangan;
use App\Tunai;
use App\AddTunai;
use App\ApprovalWithdrawal;
use App\Audit;
use App\DenoPendSer;
use App\GlDetail;
use App\imbangdugalama;
use App\GlCode;
use App\Panjar;

use Illuminate\Http\Request;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class VaultController extends Controller
{


 public function list($kodcaw)
 {
   $current = Carbon::now();
   $current = new Carbon();

    $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
    $vault=Vault::on($cawangan['mysql'])
            ->where('kodcaw','=',$kodcaw)
            ->where('tarikh','=',$current->toDateString())
            ->orderby('tarikh','desc')->limit(1)->get();

    return response()->json($vault);

 }

 public function listall($kodcaw)
 {
   $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
   $vault=Vault::on($cawangan['mysql'])
           ->where('kodcaw','=',$kodcaw)
           ->orderby('tarikh','desc')->get();

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

 public function updateVault($kodcaw, Request $request)
 {
   $current = Carbon::now();
   $current = new Carbon();
   $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
   $updateVault=Vault::on($cawangan['mysql'])->where([['tarikh','=',$current->toDateString()],['kodcaw','=', $kodcaw]])->update(array(

      'masuk'=>$request->masuk,
      'keluar'=>$request->keluar,
      'bakiakhir'=>$request->bakiakhir,
      'bakieod'=>$request->bakieod

   ) );

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

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

   $vault=Vault::on($cawangan['mysql'])->where('kodcaw','=',$kodcaw)->where('tarikh','=',$current->toDateString())->orderby('tarikh','desc')->first();

   $bakiawal = $vault['bakiawal'];
   $keluar = $vault['keluar'];
   $masuk = $vault['masuk'];
   $bakiakhir = $vault['bakiakhir'];
   $bakieod = $vault['bakieod'];
   $pendahuluan = $vault['pendahuluan'];
   $serahan = $vault['serahan'];
   $totpanjaran = $vault['totpanjar'];

   $totalpanjar = $request->totpanjar + $totpanjaran;

   $bakiakhir = $bakiawal - $keluar + $masuk + $pendahuluan - $serahan - $totalpanjar;

   $updateVault=Vault::on($cawangan['mysql'])->where([['tarikh','=',$current->toDateString()],['kodcaw','=', $kodcaw]])->update(array(
      'totpanjar' => $totalpanjar,
      'bakiakhir'=>$bakiakhir,
      'bakieod'=>$bakiakhir
   ));

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

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

   $vault=Vault::on($cawangan['mysql'])->where('kodcaw','=',$kodcaw)->where('tarikh','=',$current->toDateString())->orderby('tarikh','desc')->first();

   $bakiawal = $vault['bakiawal'];
   $keluar = $vault['keluar'];
   $masuk = $vault['masuk'];
   $bakiakhir = $vault['bakiakhir'];
   $bakieod = $vault['bakieod'];
   $pendahuluan = $vault['pendahuluan'];
   $serahan = $vault['serahan'];
   $totpanjaran = $vault['totpanjar'];

   $totalpanjar = $totpanjaran - $request->totpanjar;

   $bakiakhir = $bakiawal - $keluar + $masuk + $pendahuluan - $serahan - $totalpanjar;

   $updateVault=Vault::on($cawangan['mysql'])->where([['tarikh','=',$current->toDateString()],['kodcaw','=', $kodcaw]])->update(array(
      'totpanjar' => $totalpanjar,
      'bakiakhir'=>$bakiakhir,
      'bakieod'=>$bakiakhir
   ));

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

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

   $vault=Vault::on($cawangan['mysql'])->where('kodcaw','=',$kodcaw)->where('tarikh','=',$current->toDateString())->orderby('tarikh','desc')->first();

   $bakiawal = $vault['bakiawal'];
   $keluar = $vault['keluar'];
   $masuk = $vault['masuk'];
   $bakiakhir = $vault['bakiakhir'];
   $bakieod = $vault['bakieod'];
   $pendahuluan = $vault['pendahuluan'];
   $serahan = $vault['serahan'];
   $totpanjaran = $vault['totpanjar'];

   if($request->jenispendahuluanserahan == 'pendahuluan'){

      $totalpendahuluan = $request->amaun + $pendahuluan;

      $bakiakhir = $bakiawal - $keluar + $masuk + $totalpendahuluan - $serahan - $totpanjaran;

      $updateVault=Vault::on($cawangan['mysql'])->where([['tarikh','=',$current->toDateString()],['kodcaw','=', $kodcaw]])->update(array(
         'pendahuluan' => $totalpendahuluan,
         'bakiakhir' => $bakiakhir,
         'bakieod' => $bakiakhir

      ));
   }else if($request->jenispendahuluanserahan == 'serahan'){

      $totalserahan = $request->amaun + $serahan;

      $bakiakhir = $bakiawal - $keluar  + $masuk + $pendahuluan - $totalserahan - $totpanjaran;

      $updateVault=Vault::on($cawangan['mysql'])->where([['tarikh','=',$current->toDateString()],['kodcaw','=', $kodcaw]])->update(array(
         'serahan' => $totalserahan,
         'bakiakhir' => $bakiakhir,
         'bakieod' => $bakiakhir
      ));
   }

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

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

   $vault=Vault::on($cawangan['mysql'])->where('kodcaw','=',$kodcaw)->where('tarikh','=',$current->toDateString())->orderby('tarikh','desc')->first();

   $bakiawal = $vault['bakiawal'];
   $keluar = $vault['keluar'];
   $masuk = $vault['masuk'];
   $bakiakhir = $vault['bakiakhir'];
   $bakieod = $vault['bakieod'];
   $pendahuluan = $vault['pendahuluan'];
   $serahan = $vault['serahan'];
   $totpanjaran = $vault['totpanjar'];

   $glpend = GlDetail::on($cawangan['mysql'])->where('tarikh','=', $current->toDateString())->sum('pendahuluan');
   $glser = GlDetail::on($cawangan['mysql'])->where('tarikh','=', $current->toDateString())->where('teller','<>','ONLINE')->sum('serahan');

   $bakiakhir = $bakiawal - $keluar + $masuk + $glpend - $glser - $totpanjaran;

   $updateVault=Vault::on($cawangan['mysql'])->where([['tarikh','=',$current->toDateString()],['kodcaw','=', $kodcaw]])->update(array(
      'pendahuluan' => $glpend,
      'serahan' => $glser,
      'bakiakhir'=>$bakiakhir,
      'bakieod'=>$bakiakhir
   ));

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

 public function create($kodcaw,Request $request)
 {
   $current = Carbon::now();
   $current = new Carbon();

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

   $vault = Vault::on($cawangan['mysql'])->orderby('tarikh','desc')->first();

   if($vault)
   {
      $vaultcreate = Vault::on($cawangan['mysql'])->create([

         'kodcaw'=>$request->kodcaw,
         'tarikh'=> $current->toDateString(),
         'bakiawal' => $vault['bakiakhir'],
         'keluar' => 0,
         'masuk' => 0,
         'bakiakhir' => $vault['bakiakhir'],
         'bakieod' => $vault['bakiakhir'],
         'pendahuluan' => 0,
         'serahan' => 0,
         '1sen' => $vault['1sen'],
         '5sen' => $vault['5sen'],
         '10sen' => $vault['10sen'],
         '20sen' => $vault['20sen'],
         '50sen' => $vault['50sen'],
         'rm1' => $vault['rm1'],
         'rm2' => $vault['rm2'],
         'rm5' => $vault['rm5'],
         'rm10' => $vault['rm10'],
         'rm50' => $vault['rm50'],
         'rm100' => $vault['rm100'],
         'rm1000' => $vault['rm1000'],
         'totpanjar' => 0

          ]);
   }else{
      $vaultcreate = Vault::on($cawangan['mysql'])->create([

         'kodcaw'=>$request->kodcaw,
         'tarikh'=> $current->toDateString(),
         'bakiawal' => 0,
         'keluar' => 0,
         'masuk' => 0,
         'bakiakhir' => 0,
         'bakieod' => 0,
         'pendahuluan' => 0,
         'serahan' => 0,
         '1sen' => 0,
         '5sen' => 0,
         '10sen' => 0,
         '20sen' => 0,
         '50sen' => 0,
         'rm1' => 0,
         'rm2' => 0,
         'rm5' => 0,
         'rm10' => 0,
         'rm50' => 0,
         'rm100' => 0,
         'rm1000' => 0,
         'totpanjar' => 0

          ]);
   }


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

//GLDETAIL

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

      $gldetail=GlDetail::on($cawangan['mysql'])->create([
         'kodcaw' => $request->kodcaw,
         'tarikh' => $current->toDateString(),
         'dtacct' => '1000/020',
         'ktacct' => '1000/010',
         'descpt' => 'SERAHAN BAYAR ONLINE',
         'serahan' => $request->amaun,
         'teller' => 'ONLINE',
         'date_created' => $current
      ]);

      //   $audit = new Audit();
      //   $audit->users = $request->teller;
      //   $audit->actions = "Daftar Peti Besi";
      //   $audit->new_data = json_encode($gldetail);
      //   $audit->kodcaw = $kodcaw;
      //   $audit->created_at = $current;
      //   $audit->updated_at = $current;
      //   $audit->save();

      return response()->json($gldetail,201);

   }

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

      if($request->jenisgldetail == 'pendahuluan')
      {
         $gldetail=GlDetail::on($cawangan['mysql'])->create([
            'kodcaw' => $request->kodcaw,
            'tarikh' => $current->toDateString(),
            'dtacct' => $request->dtacct,
            'ktacct' => $request->ktacct,
            'descpt' => $request->keterangan,
            'pendahuluan' => $request->amaun,
            'teller' => $request->teller,
            'date_created' => $current
         ]);
      }else if($request->jenisgldetail == 'serahan')
      {
         $gldetail=GlDetail::on($cawangan['mysql'])->create([
            'kodcaw' => $request->kodcaw,
            'tarikh' => $current->toDateString(),
            'dtacct' => $request->dtacct,
            'ktacct' => $request->ktacct,
            'descpt' => $request->keterangan,
            'serahan' => $request->amaun,
            'teller' => $request->teller,
            'date_created' => $current

         ]);
      }

      $audit = new Audit();
      $audit->users = $request->teller;
      if (isset($request->jenisgldetail) && $request->jenisgldetail == 'pendahuluan') {
         $audit->actions = 'Daftar Pendahuluan';
      } else if (isset($request->jenisgldetail) && $request->jenisgldetail == 'serahan') {
         $audit->actions = 'Daftar Serahan';
      }
      $audit->new_data = json_encode($gldetail);
      $audit->kodcaw = $kodcaw;
      $audit->created_at = $current;
      $audit->updated_at = $current;
      $audit->save();


      return response()->json($gldetail,201);

   }

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

      $gldetail=GlDetail::on($cawangan['mysql'])
      ->where('kodcaw','=',$kodcaw)
      ->orderby('tarikh','desc')->get();

        $audit = new Audit();
      //   $audit->users = $request->input('teller');
        $audit->users = $request->name;
        $audit->actions = "Senarai Peti Besi";
        $audit->new_data = json_encode($gldetail);
        $audit->kodcaw = $kodcaw;
        $audit->created_at = $current;
        $audit->updated_at = $current;
        $audit->save();

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

   public function TunaiDiTangan($kodcaw,Request $request)
   {
     $current = Carbon::now();
     $current = new Carbon();

     $totalbakiakhirtunai = 0;

      $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
      $vault=Vault::on($cawangan['mysql'])
              ->where('kodcaw','=',$kodcaw)
              ->where('tarikh','<=', $request->tarikh)
              ->orderby('tarikh','desc')->first();

      $tunai = Tunai::on($cawangan['mysql'])
               ->where('kodcaw','=',$kodcaw)
               ->where('tarikh','=', $request->tarikh)
               ->get();

      $total = 0;
      foreach($tunai as $index=>$money)
      {
         $total+=$money['baki'];
      }


      if($total != 0)
      {
         $totalbakiakhirtunai = $vault['bakiakhir'] + $total;
      }else
      {
         $totalbakiakhirtunai = $vault['bakiakhir'];
      }

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

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


     $current = Carbon::now();
     $current = new Carbon();

     $totalbakiakhirtunai = 0;

      $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
      $vault=Vault::on($cawangan['mysql'])
              ->where('kodcaw','=',$kodcaw)
              ->where('tarikh','>=', $request->mula)
              ->where('tarikh','<=', $request->hingga)
              ->orderby('tarikh','desc')->first();

      $tunai = Tunai::on($cawangan['mysql'])
               ->where('kodcaw','=',$kodcaw)
               ->where('tarikh','>=', $request->mula)
              ->where('tarikh','<=', $request->hingga)
               ->get();

      $total = 0;
      foreach($tunai as $index=>$money)
      {
         $total+=$money['baki'];
      }


      if($total != 0)
      {
         $totalmasuk = $vault['masuk'] + $vault['pendahuluan'] + $total;
      }else
      {
         $totalmasuk = $vault['masuk'];
      }

      $totalkeluar = $vault['keluar'] + $vault['serahan'] + $vault['totpanjar'];

      $arraytunaiditangan = ['totalmasuk' => $totalmasuk, 'totalkeluar' => $totalkeluar];

      return response()->json($arraytunaiditangan,200);
   }
   public function laporanPetiBesi($kodcaw,$tarikh)
   {
      $current = Carbon::now();
      $current = new Carbon();

      $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
      
      if (!$cawangan) {
         return response()->json(['error' => 'Cawangan tidak dijumpai'], 404);
      }
      
      $vault = Vault::on($cawangan['mysql'])
               ->where('kodcaw','=',$kodcaw)
               ->where('tarikh','=', $tarikh)
               ->orderby('tarikh','desc')
               ->first();

      if (!$vault) {
         return response()->json(['error' => 'Tiada data pada tarikh tersebut'], 404);
      }

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

   public function CalculationPendahuluanSerahan($kodcaw,Request $request)
   {
      $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();

      $serahanpkb = '1000/020';
      $serahanjmc = '1000/026';
      $pendahuluanpkb = '7000/102';
      $pendahuluanjmc = '7000/106';
      $serahankoperasi = '1000/090';

      //Selepas Tawarruq

      $serahanpkbTawarruq = GlDetail::on($cawangan['mysql'])
         ->where('ktacct','=',$serahanpkb)
         ->where('tarikh','<=', $request->tarikh)
         ->where('tarikh','>','2021-03-24')
         ->sum('serahan');


      //Sebelum Tawarruq
      if($kodcaw == 'KB2' || $kodcaw == 'WCY' || $kodcaw == '1STOP' || $kodcaw == 'ARM' || $kodcaw == 'APM' || $kodcaw == '010621' || $kodcaw == '010915' || $kodcaw == '020303')
      {
         $serahanpkb = GlDetail::on($cawangan['mysql'])
            ->where('dtacct','=',$serahanpkb)
            ->where('tarikh','<=', $request->tarikh)
            ->where('tarikh','>','2013-12-31')
            ->sum('amaun');

         $serahanJMC = GlDetail::on($cawangan['mysql'])
            ->where('dtacct','=',$serahanjmc)
            ->where('tarikh','<=', $request->tarikh)
            ->where('tarikh','>','2013-12-31')
            ->sum('amaun');

         $serahanpkblama = imbangdugalama::on($cawangan['mysql'])
            ->sum('serahan_pkb');

         $serahanjmclama = imbangdugalama::on($cawangan['mysql'])
            ->sum('serahan_jmc');

         $totaljmc = $serahanJMC + $serahanjmclama;
         $totalpkb = $serahanpkb + $serahanpkblama;

         $totalserahan = $serahanpkbTawarruq + $totaljmc + $totalpkb;
      }else{

         if($kodcaw == '020301')
            $serahanbaru = '1000/030';
         else
            $serahanbaru = '1000/026';

         $serahanpkb = GlDetail::on($cawangan['mysql'])
            ->where('dtacct','=',$serahanpkb)
            ->where('tarikh','<=', $request->tarikh)
            ->sum('amaun');

         $serahanJMC = GlDetail::on($cawangan['mysql'])
            ->where('dtacct','=',$serahanbaru)
            ->where('tarikh','<=', $request->tarikh)
            ->sum('amaun');

         $totalserahan = $serahanpkbTawarruq + $serahanpkb + $serahanJMC;
      }



      //Selepas Tawarruq

      $pendahuluanPKBtawarruq = GlDetail::on($cawangan['mysql'])
      ->where('ktacct','=',$pendahuluanpkb)
      ->where([['tarikh','<=', $request->tarikh],['tarikh','>','2021-03-24']])
      ->sum('pendahuluan');

      //Sebelum Tawarruq

      $pendahuluanPKB = GlDetail::on($cawangan['mysql'])
         ->where('ktacct','=',$pendahuluanpkb)
         ->where([['tarikh','<=', $request->tarikh],['tarikh','>','2013-12-31']])
         ->sum('amaun');

      $pendahuluanJMC = GlDetail::on($cawangan['mysql'])
         ->where('ktacct','=','7000/106')
         ->where([['tarikh','<=', $request->tarikh],['tarikh','>','2013-12-31']])
         ->sum('amaun');

      $pendahuluanpkblama = imbangdugalama::on($cawangan['mysql'])
         ->sum('pendahuluan_pkb');

      $pendahuluanjmclama = imbangdugalama::on($cawangan['mysql'])
         ->sum('pendahuluan_jmc');

      $total = $pendahuluanPKBtawarruq + $pendahuluanjmclama + $pendahuluanpkblama + $pendahuluanPKB + $pendahuluanJMC;

      $pindahkekpkb = GlCode::on($cawangan['mysql'])
      ->where('glcode','=',$serahankoperasi)
      ->sum('amaun');

      if($kodcaw == '011229')
         $totalserahan -= 0.10;

      if($kodcaw == '011013')
         $totalserahan += 641000;

      $arraypend = ['pendahuluan' => $total,'serahan' => $totalserahan,'pindahkekpkb' => $pindahkekpkb];

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


   public function CalculationPendahuluanSerahanLejer($kodcaw,Request $request)
   {
      $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();

      $serahanpkb = '1000/020';
      $serahanjmc = '1000/026';
      $pendahuluanpkb = '7000/102';
      $pendahuluanjmc = '7000/106';
      $serahankoperasi = '1000/090';

      $mula = $request->mula;
      $hingga = $request->hingga;

      //Selepas Tawarruq

      $serahanpkbTawarruq = GlDetail::on($cawangan['mysql'])
         ->where('ktacct','=',$serahanpkb)
         ->where('tarikh','>=', $mula)
         ->where('tarikh','<=', $hingga)
         ->where('tarikh','>','2021-03-24')
         ->sum('serahan');

      //Sebelum Tawarruq
      if($kodcaw == 'KB2' || $kodcaw == 'WCY' || $kodcaw == '1STOP' || $kodcaw == 'ARM' || $kodcaw == 'APM' || $kodcaw == '010621' || $kodcaw == '010915')
      {
         $serahanpkb = GlDetail::on($cawangan['mysql'])
            ->where('dtacct','=',$serahanpkb)
            ->where('tarikh','>=', $mula)
            ->where('tarikh','<=', $hingga)
            ->where('tarikh','>','2013-12-31')
            ->sum('amaun');

         $serahanJMC = GlDetail::on($cawangan['mysql'])
            ->where('dtacct','=',$serahanjmc)
            ->where('tarikh','>=', $mula)
            ->where('tarikh','<=', $hingga)
            ->where('tarikh','>','2013-12-31')
            ->sum('amaun');


         if($mula <= '2013-12-31')
         {
            $serahanpkblama = imbangdugalama::on($cawangan['mysql'])
               ->sum('serahan_pkb');

            $serahanjmclama = imbangdugalama::on($cawangan['mysql'])
               ->sum('serahan_jmc');

               $totaljmc = $serahanJMC + $serahanjmclama;
               $totalpkb = $serahanpkb + $serahanpkblama;

               $totalserahan = $serahanpkbTawarruq + $totaljmc + $totalpkb;
         }else{

            $totaljmc = $serahanJMC;
            $totalpkb = $serahanpkb;

            $totalserahan = $serahanpkbTawarruq + $totaljmc + $totalpkb;
         }


      }else{

         if($kodcaw == '020301')
            $serahanbaru = '1000/030';
         else
            $serahanbaru = '1000/026';

         $serahanpkb = GlDetail::on($cawangan['mysql'])
            ->where('dtacct','=',$serahanpkb)
                     ->where('tarikh','>=', $mula)
                     ->where('tarikh','<=', $hingga)
            ->sum('amaun');

         $serahanJMC = GlDetail::on($cawangan['mysql'])
            ->where('dtacct','=',$serahanbaru)
                     ->where('tarikh','>=', $mula)
                     ->where('tarikh','<=', $hingga)
            ->sum('amaun');

         $totalserahan = $serahanpkbTawarruq + $serahanpkb + $serahanJMC;
      }

      //Selepas Tawarruq

      $pendahuluanPKBtawarruq = GlDetail::on($cawangan['mysql'])
         ->where('ktacct','=',$pendahuluanpkb)
         ->where([['tarikh','>=', $mula],['tarikh','<=', $hingga],['tarikh','>','2021-03-24']])
         ->sum('pendahuluan');

      //Sebelum Tawarruq

      $pendahuluanPKB = GlDetail::on($cawangan['mysql'])
         ->where('ktacct','=',$pendahuluanpkb)
         ->where([['tarikh','>=', $mula],['tarikh','<=', $hingga],['tarikh','>','2013-12-31']])
         ->sum('amaun');

      $pendahuluanJMC = GlDetail::on($cawangan['mysql'])
         ->where('ktacct','=','7000/106')
         ->where([['tarikh','>=', $mula],['tarikh','<=', $hingga],['tarikh','>','2013-12-31']])
         ->sum('amaun');

      if($mula <= '2013-12-31')
      {
         $pendahuluanpkblama = imbangdugalama::on($cawangan['mysql'])
         ->sum('pendahuluan_pkb');

         $pendahuluanjmclama = imbangdugalama::on($cawangan['mysql'])
            ->sum('pendahuluan_jmc');

         $total = $pendahuluanPKBtawarruq + $pendahuluanjmclama + $pendahuluanpkblama + $pendahuluanPKB + $pendahuluanJMC;

         $pindahkekpkb = GlCode::on($cawangan['mysql'])
         ->where('glcode','=',$serahankoperasi)
         ->sum('amaun');
      }else{

         $total = $pendahuluanPKBtawarruq + $pendahuluanPKB + $pendahuluanJMC;

         $pindahkekpkb = 0;

      }


      $arraypend = ['pendahuluan' => $total,'serahan' => $totalserahan,'pindahkekpkb' => $pindahkekpkb];

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

   public function laporanPetiBesiRange($kodcaw,Request $request)
   {
     $current = Carbon::now();
     $current = new Carbon();

      $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
      $vault=Vault::on($cawangan['mysql'])
              ->where('kodcaw','=',$kodcaw)
              ->where('tarikh','>=', $request->mula)
              ->where('tarikh','<=', $request->hingga)
              ->orderby('tarikh','desc')->get();

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

}
