<?php

namespace App\Http\Controllers;

use App\CawanganDetail;
use App\Cawangan;
use App\Denominasi;
use App\Services\CashFlow\TellerServices;
use Illuminate\Http\Request;
use App\Services\Miscellaneous\PanjarServices;
use Log;
use Carbon\Carbon;

class DenominasiController extends Controller
{
   public function cashdeposit($kodcaw,Request $request){
      $teller = new TellerServices($kodcaw);
      $response = $teller->CashDeposit($request);

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

   public function cashwithdraw($kodcaw,Request $request){
      $teller = new TellerServices($kodcaw);
      $response = $teller->CashWithdrawals($request);

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

   public function createpanjar($kodcaw,Request $request){
      $teller = new PanjarServices($kodcaw);
      $response = $teller->create($request);

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

   public function deletepanjar($kodcaw,Request $request){
      $teller = new PanjarServices($kodcaw);
      $response = $teller->delete($request);

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

   public function getPerubatanAmount($kodcaw,Request $request){
      $teller = new PanjarServices($kodcaw);
      $response = $teller->getPerubatanAmount($request->tuntutoleh);

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

   public function listdenominasi($kodcaw)
   {
   $current = Carbon::now();
   $current = new Carbon();
   
    $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
    $data=Denominasi::on($cawangan['mysql'])->where('tarikh','=',$current->toDateString())->limit(1)->get();
    
    return response()->json($data);
    
   }

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

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

      //Wang Initialize
      $bd1 = ($request->bd1 * 100);       
      $bd20 = ($request->bd20 * 1000);
      $bd5 = ($request->bd5 * 500);
      $bd50 = ($request->bd50 * 5000);
      $bd10 = ($request->bd10 * 1000);
      $bd100 = ($request->bd100 * 5000);
      $k01 = ($request->txtsen1 * 0.01);
      $k05 = ($request->txtsen5 * 0.05);
      $k010 = ($request->txtsen10 * 0.10);
      $k020 = ($request->txtsen20 * 0.20);
      $k050 = ($request->txtsen50 * 0.50);
      $k1 = ($request->txtring1 * 1);
      $k5 = ($request->txtring5 * 5);
      $k10 = ($request->txtring10 * 10);
      $k20 = ($request->txtring20 * 20);
      $k50 = ($request->txtring50 * 50);
      $k100 = ($request->txtring100 * 100);

      //Total Wang

      $totalbundle = $bd1 + $bd20 + $bd5 + $bd50 + $bd10 + $bd100;
      $totalkeping = $k01 + $k05 + $k010 + $k020 + $k050 + $k1 + $k5 + $k10 + $k20 + $k50 + $k100;

      $totaljumlah = $totalbundle + $totalkeping;
                 

      $denominasiupdate=Denominasi::on($cawangan['mysql'])->where([['tarikh','=',$current->toDateString()]])->update(array(

         'bd1' => $request->bd1,
         'b1'=> $bd1,
         'bd5'=> $request->bd5,
         'b5'=> $bd5,
         'bd10'=> $request->bd10,
         'b10'=> $bd10,
         'bd20'=> $request->bd20,
         'b20'=> $bd20,
         'bd50'=> $request->bd50,
         'b50'=> $bd50,
         'bd100'=> $request->bd100,
         'b100'=> $bd100,
         'txtring1'=> $request->txtring1,
         'k1'=> $k1,
         'txtring5'=> $request->txtring5,
         'k5'=> $k5,
         'txtsen1'=> $request->txtsen1,
         'k01'=> $k01,
         'txtsen5'=> $request->txtsen5,
         'k05'=> $k05,
         'txtsen10'=> $request->txtsen10,
         'k010'=> $k010,
         'txtsen20'=> $request->txtsen20,
         'k020'=> $k020,
         'txtsen50'=> $request->txtsen50,
         'k050'=> $k050,
         'txtring10'=> $request->txtring10,
         'k10'=> $k10,
         'txtring20'=> $request->txtring20,
         'k20'=> $k20,
         'txtring50'=> $request->txtring50,
         'k50'=> $k50,
         'txtring100'=> $request->txtring100,
         'k100'=> $k100,
         'txtring1000'=> $request->txtring1000,
         'k1000'=> $request->k1000,
         'jumlahbundle'=> $totalbundle,
         'txtjumlah'=> $totaljumlah,
         'jumlahkeping' => $totalkeping
   
      ) );

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

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

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

      if($deno)
      {
         $savedeno = Denominasi::on($cawangan['mysql'])->create([ 
            
            'tarikh' => $current->toDateString(),
            'bd1' => $deno['bd1'],
            'b1' => $deno['b1'],
            'bd5' => $deno['bd5'],
            'b5' => $deno['b5'],
            'bd10' => $deno['bd10'],
            'b10' => $deno['b10'],
            'bd20' => $deno['bd20'],
            'b20' => $deno['b20'],
            'bd50' => $deno['bd50'],
            'b50' => $deno['b50'],
            'bd100' => $deno['bd100'],
            'b100' => $deno['b100'],
            'txtring1' => $deno['txtring1'],
            'k1' => $deno['k1'],
            'txtring5' => $deno['txtring5'],
            'k5' => $deno['k5'],
            'txtsen1' => $deno['txtsen1'],
            'k01' => $deno['k01'],
            'txtsen5' => $deno['txtsen5'],
            'k05' => $deno['k05'],
            'txtsen10' => $deno['txtsen10'],
            'k010' => $deno['k010'],
            'txtsen20' => $deno['txtsen20'],
            'k020' => $deno['k020'],
            'txtsen50' => $deno['txtsen50'],
            'k050' => $deno['k050'],
            'txtring10' => $deno['txtring10'],
            'k10' => $deno['k10'],
            'txtring20' => $deno['txtring20'],
            'k20' => $deno['k20'],
            'txtring50' => $deno['txtring50'],
            'k50' => $deno['k50'],
            'txtring100' => $deno['txtring100'],
            'k100' => $deno['k100'],
            'txtring1000' => $deno['txtring1000'],
            'k1000' => $deno['k1000'],
            'jumlahbundle' => $deno['jumlahbundle'],
            'txtjumlah' => $deno['txtjumlah'],
            'lebihan' => $deno['lebihan'],
            'nota' => $deno['nota'],
            'jumlahkeping' => $deno['jumlahkeping']
         ]);
      }else{
         $savedeno = Denominasi::on($cawangan['mysql'])->create([ 
            
            'tarikh' => $current->toDateString(),
            'bd1' => 0,
            'b1' => 0,
            'bd5' => 0,
            'b5' => 0,
            'bd10' => 0,
            'b10' => 0,
            'bd20' => 0,
            'b20' => 0,
            'bd50' => 0,
            'b50' => 0,
            'bd100' => 0,
            'b100' => 0,
            'txtring1' => 0,
            'k1' => 0,
            'txtring5' => 0,
            'k5' => 0,
            'txtsen1' => 0,
            'k01' => 0,
            'txtsen5' => 0,
            'k05' => 0,
            'txtsen10' => 0,
            'k010' => 0,
            'txtsen20' => 0,
            'k020' => 0,
            'txtsen50' => 0,
            'k050' => 0,
            'txtring10' => 0,
            'k10' => 0,
            'txtring20' => 0,
            'k20' => 0,
            'txtring50' => 0,
            'k50' => 0,
            'txtring100' => 0,
            'k100' => 0,
            'txtring1000' => 0,
            'k1000' => 0,
            'jumlahbundle' => 0,
            'txtjumlah' => 0,
            'lebihan' => 0,
            'nota' => 0,
            'jumlahkeping' => 0
         ]);
      }
      

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

   public function listtarikhdenominasi($kodcaw,Request $request)
   {
   $current = Carbon::now();
   $current = new Carbon();
   
    $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
    $data=Denominasi::on($cawangan['mysql'])->where('tarikh','=',$request->tarikh)->orderby('tarikh','desc')->limit(1)->get();
    
    return response()->json($data);
    
   }
}