<?php

namespace App\Http\Controllers;
use App\Panjar;
use App\Cawangan;
use App\DenoPanjar;
use App\Audit;


use Carbon\Carbon;

use Illuminate\Http\Request;

class PanjarController extends Controller
{
   public function list($kodcaw)
   {
      $harini = Carbon::now();
      $harini = new Carbon;

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

      $panjar=Panjar::on($cawangan['mysql'])
               ->where('tarikh','=',$harini->toDateString())
               ->get();

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

   public function listpanjar($kodcaw)
   {
      $harini = Carbon::now();
      $harini = new Carbon;

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

      $panjar=Panjar::on($cawangan['mysql'])
               ->orderBy('recno','desc')
               ->first();

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

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

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

      $panjar=Panjar::on($cawangan['mysql'])
               ->where('nobaucer','=',$request->nobaucer)
               ->first();

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

   public function listmulaakhir($kodcaw,$mula,$akhir)
   {
   
      $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();

      $panjar=Panjar::on($cawangan['mysql'])
               ->where('tarikh','>=',$mula)
               ->where('tarikh','<=',$akhir)
               ->orderBy('tarikh','desc')
               ->orderBy('nobaucer','desc')

               ->get();

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

   public function update($kodcaw,$norujukan,Request $request)
   {
      $harini = Carbon::now();
      $harini = new Carbon;

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

      $kelulusan=Panjar::on($cawangan['mysql'])
               ->where('tarikh','=',$harini->toDateString())
               ->where('norujukan','=',$norujukan)
               ->update(array(
                        'pelulus' => $request->pelulus,
                        'masalulus'=> $harini->toTimeString()
                  
                     ));

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

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

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

      $panjar=Panjar::on($cawangan['mysql'])
               ->create([ 
                        'tarikh'=> $harini->toDateString(),
                        'deskripsi'=> $request->deskripsi,
                        'bayaran' => $request->bayaran,
                        'tag' => $request->tag,
                        'kategori' => $request->kategori,
                        'id' => $request->id,
                        'bakiperubatan' => $request->bakiperubatan,
                        'nobaucer' => $request->nobaucer,
                        'nobil' => $request->nobil,
                        'tuntutoleh' => $request->tuntutoleh
      ]);


      // $audit = new Audit();
      // $audit->users = $request->tuntutoleh;
      // $audit->actions = "Daftar Perbelanjaan";
      // $audit->new_data = $panjar;
      // $audit->kodcaw = $kodcaw;
      // $audit->created_at = $harini;
      // $audit->updated_at = $harini;
      // $audit->save();

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

   public function delete(REQUEST $request,$kodcaw)
   {
      $harini = Carbon::now();
      $harini = new Carbon;

      $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
      $panjar = Panjar::on($cawangan['mysql'])->where('recno','=',$request->recno)->first();
      Panjar::on($cawangan['mysql'])->where('recno','=',$request->recno)->delete();

      $audit = new Audit();
      $audit->users = $request->users;
      $audit->actions = "Hapus Perbelanjaan";
      $audit->old_data = $panjar;
      $audit->kodcaw = $kodcaw;
      $audit->created_at = $harini;
      $audit->updated_at = $harini;
      $audit->save();

      return response('Batal Berjaya', 200);
   }

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

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

      $current = Carbon::now();
      $current = new Carbon($request->tarikh);

      $baikidanselenggara = Panjar::on($cawangan['mysql'])
      // ->where('tarikh','>=', $request->mula)
      ->where('tarikh','<=', $request->tarikh)
      ->where('tag', '=', 2)
      ->whereYear('tarikh','=',$current->year)
      ->sum('bayaran');

      $perubatan = Panjar::on($cawangan['mysql'])
      // ->where('tarikh','>=', $request->mula)
      ->where('tarikh','<=', $request->tarikh)
      ->where('tag', '=', 1)
      ->whereYear('tarikh','=',$current->year)
      ->sum('bayaran'); 

      $keraian = Panjar::on($cawangan['mysql'])
      // ->where('tarikh','>=', $request->mula)
      ->where('tarikh','<=', $request->tarikh)
      ->where('tag', '=', 3)
      ->whereYear('tarikh','=',$current->year)
      ->sum('bayaran');

      if($kodcaw == 'APM')
      {
         $perubatan = $perubatan - 10;
      }

      $arraycalculation = ['bds' => $baikidanselenggara, 'perubatan' => $perubatan, 'keraian' => $keraian];

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

   }

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

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

      $current = Carbon::now();
      $current = new Carbon($request->tarikh);



      $baikidanselenggara = Panjar::on($cawangan['mysql'])
      // ->where('tarikh','>=', $request->mula)
      ->where('tarikh','>=', $request->mula)
      ->where('tarikh','<=', $request->hingga)
      ->where('tag', '=', 2)
      ->sum('bayaran');

      $perubatan = Panjar::on($cawangan['mysql'])
      // ->where('tarikh','>=', $request->mula)
      ->where('tarikh','>=', $request->mula)
      ->where('tarikh','<=', $request->hingga)
      ->where('tag', '=', 1)
      ->sum('bayaran'); 

      $keraian = Panjar::on($cawangan['mysql'])
      // ->where('tarikh','>=', $request->mula)
      ->where('tarikh','>=', $request->mula)
      ->where('tarikh','<=', $request->hingga)
      ->where('tag', '=', 3)
      ->sum('bayaran');


      $arraycalculation = ['bds' => $baikidanselenggara, 'perubatan' => $perubatan, 'keraian' => $keraian];

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

   }

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

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


       $denopanjar=DenoPanjar::on('mysql7')->create([ 
             
            'tarikh' => $current->toDateString(),
            'masa' => $current->toTimeString(),
            'kodcaw' => $kodcaw,
            'kodlaluan' => $request->kodlaluan,
            'serahan' => $request->serahan,
            'bd1' => $request->bd1,
            'bd5' => $request->bd5,
            'bd10' => $request->bd10,
            'bd20' => $request->bd20,
            'bd50' => $request->bd50,
            'bd100' => $request->bd100,
            'txtring1' => $request->txtring1,
            'txtring5' => $request->txtring5,
            'txtring10' => $request->txtring10,
            'txtring20' => $request->txtring20,
            'txtring50' => $request->txtring50,
            'txtring100' => $request->txtring100,
            'txtring1000' => $request->txtring1000,
            'txtsen1' => $request->txtsen1,
            'txtsen5' => $request->txtsen5,
            'txtsen10' => $request->txtsen10,
            'txtsen20' => $request->txtsen20,
            'txtsen50' => $request->txtsen50,
            'jenis' =>$request->jenis,
            'recno' =>$request->recno,
            'update' => '0'
       ]);

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

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


       $denopanjar = DenoPanjar::on('mysql7')->where('kodcaw','=', $kodcaw)->where('recno','=', $request->recno)->get();

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

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


       $denopanjar=DenoPanjar::on('mysql7')
                     ->where('recno','=', $request->recno)
                     ->update(array(
                        'pelulus' => $request->pelulus,
                        'masalulus'=> $harini->toTimeString()
                     ));

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

   


}
