<?php

namespace App\Http\Controllers;

use App\Marhun;
use App\Jemas;
use App\Cawangan;
use Illuminate\Http\Request;
use App\Audit;
use App\HargaEmas;

use Carbon\Carbon;

class MarhunController extends Controller
{

    public function showAllMarhuns()
    {
        return response()->json(Marhun::all());
    }

    public function showOneMarhun($norujukan)
    {
        $cawangan_all = Cawangan::on('mysql7')->get();
        $marhun_all = [];
        foreach($cawangan_all as $index=>$cawangan){
            $marhun = Marhun::on($cawangan['mysql'])->where('norujukan','=',$norujukan)->get();
            if(sizeof($marhun) != 0){
                array_push($marhun_all,$marhun);
            }
        }

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

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

        $marhun = Marhun::on($cawangan['mysql'])->where('norujukan',$request->norujukan)->distinct()->pluck('karat')->toArray();

        $mutu = HargaEmas::on('mysql7')->whereIn('karat',$marhun)->pluck('keterangan')->toArray();

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

    public function getMarhunName (Request $request){
        $jemas = Jemas::on('mysql7')->where('marhun','=',$request->marhun)->first();

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

    public function getAllJemas(){
        $jemas = Jemas::on('mysql7')->orderBy('ordernum', 'asc')->get();

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

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

        $harga = HargaEmas::on('mysql7')->where('karat',$request->karat)->first();

        $hargaemas = $harga['harga'];
        if(isset($request->jenisAT)){
            if($request->jenisAT == 'hargalama'){
                $hargaemas = $request->harga;
                // $nilai_marhun = $request->berat_emas * $request->harga;
            }else $hargaemas = $harga['harga'];
            // $nilai_marhun = $request->berat_emas * $harga['harga'];
        }else $hargaemas = $harga['harga']; 
        
        $nilai_marhun = $request->berat_emas * $hargaemas;
        
        $marhunToCreate = [
            'kodcaw'=> $kodcaw,
            'norujukan'=> $request->norujukan,
            't_gadai' => $current->toDateString(),
            'bil' => $request->kuantiti,
            'marhun' => $request->marhun,
            'nota' => $request->nota,
            'tag_permata' => $request->tag_permata != null ? $request->tag_permata : 0,
            'berat_batu_permata' => $request->berat_batu_permata != null ? $request->berat_batu_permata : '',
            'karat' => $request->karat,
            'berat' => $request->berat_emas,
            'harga' => $hargaemas,
            'n_marhun' => $nilai_marhun
        ];

        // Set value berat_batu_permata to null if value passed is null
        if($marhunToCreate['berat_batu_permata'] == '') {
            $marhunToCreate['berat_batu_permata'] = null;
        };

        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
        $marhun = Marhun::on($cawangan['mysql'])->create($marhunToCreate);

        $audit = new Audit();
        $audit->users = $request->teller;
        $audit->actions = 'Daftar Marhun';
        $audit->norujukan = $request->norujukan;
        $audit->new_data = $marhun;
        $audit->kodcaw = $kodcaw;
        $audit->created_at = $current;
        $audit->updated_at = $current;
        $audit->save();

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

    public function update($id, Request $request)
    {
        $marhun = Marhun::findOrFail($id);
        $marhun->update($request->all());

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

    public function delete($id)
    {
        Marhun::findOrFail($id)->delete();
        return response('Deleted Successfully', 200);
    }

    public function deletemarhun($kodcaw,$norujukan,Request $request)
    {

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

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

        $old_data = Marhun::on($cawangan['mysql'])->where([['recno','=',$request->recno],['norujukan','=',$norujukan]])->first();

        $jemas = Marhun::on($cawangan['mysql'])->where([['recno','=',$request->recno],['norujukan','=',$norujukan]])->delete();

        $audit = new Audit();
        $audit->users = $request->teller;
        $audit->actions = 'Hapus Marhun';
        $audit->norujukan = $norujukan;
        $audit->old_data = $old_data;
        $audit->kodcaw = $kodcaw;
        $audit->created_at = $current;
        $audit->updated_at = $current;
        $audit->save();

        return response('Deleted Successfully', 200);
    }

    public function editmarhun($kodcaw,$norujukan,Request $request)
    {

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

        $harga = HargaEmas::on('mysql7')->where('karat',$request->karat)->first();
        $nilai_marhun = $request->berat * $harga['harga'];

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

        $old_data = Marhun::on($cawangan['mysql'])->where([['recno','=',$request->recno],['norujukan','=',$norujukan]])->first();

        $marhunToUpdate = [
            'karat' => $request->karat,
            'marhun' => $request->marhun,
            'nota' => $request->nota,
            'tag_permata' => $request->tag_permata,
            'berat_batu_permata' => $request->berat_batu_permata,
            'berat' => $request->berat,
            'harga' => $harga['harga'],
            'n_marhun' => $nilai_marhun,
            'bil' => $request->kuantiti,
        ];

        // Set value berat_batu_permata to null if value passed is null
        if($marhunToUpdate['berat_batu_permata'] == '') {
            $marhunToUpdate['berat_batu_permata'] = null;
        };

        $jemas = Marhun::on($cawangan['mysql'])->where([['recno','=',$request->recno],['norujukan','=',$norujukan]])->update($marhunToUpdate);

        $new_data = Marhun::on($cawangan['mysql'])->where([['recno','=',$request->recno],['norujukan','=',$norujukan]])->first();

        $audit = new Audit();
        $audit->users = $request->teller;
        $audit->actions = 'Ubah Marhun';
        $audit->norujukan = $norujukan;
        $audit->old_data = $old_data;
        $audit->new_data = $new_data;
        $audit->kodcaw = $kodcaw;
        $audit->created_at = $current;
        $audit->updated_at = $current;
        $audit->save();

        return response('Edit Successfully', 200);
    }

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

        $marhun = Marhun::on($cawangan['mysql'])
                ->select('marhun.norujukan','gadai.nokp','customer.nama','marhun.marhun','marhun.nota','marhun.berat','marhun.karat','marhun.harga','marhun.n_marhun','marhun.t_gadai','marhun.tag_permata', 'marhun.berat_batu_permata')
                ->rightJoin('gadai', 'gadai.norujukan', '=', 'marhun.norujukan')
                ->rightJoin('master_erahn.customer as customer', function($join) {
                    $join->on('customer.kpbaru', '=', 'gadai.nokp')
                    ->orOn('customer.kplama', '=', 'gadai.nokp');
                    })                
                ->where('marhun.t_gadai','=',$tarikh)
                ->orderBy('marhun.t_gadai')
                ->get();

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

    public function getLaporanMarhunTerkumpul($kodcaw,$tarikh){

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

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

        $marhun = Marhun::on($cawangan['mysql'])
                ->select('marhun.norujukan','gadai.sttebus','gadai.pinjaman','gadai.nokp','customer.nama','marhun.marhun','marhun.nota','marhun.berat','marhun.karat','marhun.harga','marhun.n_marhun','marhun.t_gadai')
                ->rightJoin('gadai', 'gadai.norujukan', '=', 'marhun.norujukan')
                 ->rightJoin('master_erahn.customer as customer', function($join) {
                    $join->on('customer.kpbaru', '=', 'gadai.nokp')
                    ->orOn('customer.kplama', '=', 'gadai.nokp');
                 })
                ->where('marhun.t_gadai','<=',$tarikh)
                ->whereYear('marhun.t_gadai','=',$current->year)
                ->where('gadai.sttebus','=','')
                ->orderBy('marhun.norujukan')
                ->get();

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

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

        $marhuns = Marhun::on($cawangan['mysql'])->where('norujukan',$request->norujukan)->get();

        $totalnilaimarhun = 0;

        foreach($marhuns as $index=>$marhun){
            $harga = HargaEmas::on('mysql7')->where('karat',$marhun->karat)->first();
            $nilai_marhun = $marhun->berat * $harga->harga;
            $totalnilaimarhun = $totalnilaimarhun + $nilai_marhun;
        }

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

    public function getMarhunCaw($kodcaw,$norujukan)
    {
        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
        $data = Marhun::on($cawangan['mysql'])
        ->where('norujukan','=', $norujukan)
        ->sum('marhun.n_marhun');
        return response()->json($data,200);
        
    }
}
