<?php

namespace App\Http\Controllers\pembelian;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Yajra\Datatables\Datatables;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Session;

class HutangAwalPembelianController extends Controller
{

	public function __construct(){     
        $this->middleware(function ($request, $next) {
        $url = request()->segment(1);
        $kode_group = Session::get('kode_group');
        $d_menu = DB::table("tb_menu as a")->join("tb_role as b","a.kode_menu","b.kode_menu")->where([['a.link_menu',$url],['b.kode_group',$kode_group],['b.status_role','1'],['a.active','1']])->orderBy('a.sort_menu','asc')->count();
        if(Session::get('decode') <> 1)
         {
            Session::flash('message', 'Silahkan login dengan username dan password anda.!!');
             return redirect()->to('login');
         }
         if($d_menu == 0){
            Session::flush();
            Session::flash('message', 'Anda Tidak Punya Akses ke Halaman ini.!!');
             return redirect()->to('login');
         }
        return $next($request); 
        });
    }

    public function index(){
    	return view('pembelian.HutangAwalPembelianList');
    }

    function tambah(){
        $data['cara_bayar'] = DB::table('ms_carabayar')->where(array(['tipe', 'K'], ['cara', 'like', '%pembelian kredit%']))->get();
        $data['sumber_dana'] =  DB::table('ms_perkiraan')->whereIn('parent',['3', '7'])->orderBy('kode_perkiraan', 'asc')->get();

        return view('pembelian.HutangAwalPembelianTambah')->with('data', $data); 
    }

    function get_data(){
    	$tahun = date('Y');
        $arr = array();
    	$d_data = DB::table('view_pembelian_hutang as v')->select(DB::raw('v.*'))->whereYear('v.tanggal', '<' ,$tahun);

    	if($d_data->count() > 0){
    		foreach($d_data->get() as $d){
    			$aksi = '';
		        $total_beli = $d->nominal_netto;
		        $total_bayar = $d->total_bayar; //+$d->total_bayar_hutang;

		        $aksi = '<a class="btn bg-success-400" type="button" href="'.url('pembelian/hutangawal_beli/bayar/'.$d->id_pembelian).'"><i class="icon-cash2"></i></a>';

                if($total_bayar < $total_beli){
                	
                    $total_akhir = $d->total_bayar+$d->total_bayar_hutang;

                    if($total_akhir == 0){
		               $bayar = 0;
		            }else if($total_akhir < $total_beli){
		                $bayar = 1;
		            }else{
                        $bayar = 2;
                    }

		            $arr[] = array('id_pembelian' => $d->id_pembelian,
                            'no_faktur' => $d->no_faktur,
                            'tanggal' => tgl_full($d->tanggal, '2'),
                            'tanggal_faktur' => tgl_full($d->tanggal_faktur, '2'),
                            'id_penyedia' => $d->id_penyedia,
                            'id_transaksi' => $d->id_transaksi,
                            'id_carabayar' => $d->id_carabayar,
                            'status_id' => $d->status,
                            'status' => status_bayar($bayar),
                            'netto' => rupiah2($d->nominal_netto),
                            'total_bayar' => rupiah2($total_akhir),
                            'nama_penyedia' => $d->nama_penyedia,
                            'aksi' => $aksi);
                }
    		}
    	}else{
    		$arr = array();
    	}

    	$data = new Collection($arr);
        return Datatables::of($data)
                ->make(true);
    }

    function bayar($id){
        $data['id'] = $id;
        $data['cara_bayar'] = DB::table('ms_carabayar')->where(array(['tipe', 'K'], ['cara', 'like', '%pembelian kredit%']))->get();
        $data['sumber_dana'] =  DB::table('ms_perkiraan')->whereIn('parent',['3', '7'])->orderBy('kode_perkiraan', 'asc')->get();

       return view('pembelian.HutangAwalPembelianAdd')->with('data', $data); 
    }

    function get_detailbayar($id){
        $d_data = DB::table('view_pembelian_hutang as v')->where('v.id_pembelian', $id);

        if($d_data->count() > 0){
            $data['detail'] = $d_data->first();
            $data['status'] = '1';
        }else{
            $data['status'] = '1';
        }

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

    function get_bayar($id){
        $d_awal_bayar = DB::table('view_pembelian_hutang')->where('id_pembelian', $id)->whereIn('jenis_bayar', [1,3]);
        $d_bayar = DB::table('pem_hutangbeli_bayar as phb')->join('ms_perkiraan as mp', 'mp.id', 'phb.id_akun')->join('ms_carabayar as mcb', 'mcb.id_carabayar', 'phb.id_carabayar')->where('phb.id_pembelian', $id)->select(DB::raw('phb.*, mp.kode_perkiraan, mp.nama_perkiraan, mcb.cara cara_bayar, mcb.tipe tipe_bayar'));
        $data['bayar'] = array();

        if($d_awal_bayar->count() > 0){
            $bayar_awal  = $d_awal_bayar->first();
            
            $data['bayar'][] = array('id_hutangbeli_bayar' => 'nominal_bayar',
                                'id_pembelian' => $bayar_awal->id_pembelian,
                                'id_akun' => '',
                                'id_carabayar' => $bayar_awal->id_carabayar,
                                'kode_perkiraan' =>  $bayar_awal->kode_perkiraan.' - '.$bayar_awal->nama_perkiraan,
                                'tgl_bayar' => tgl_full($bayar_awal->tanggal, ''),
                                'jenis' => $bayar_awal->cara_bayar.' - '.$bayar_awal->tipe_bayar,
                                'jum_bayar' => (float) $bayar_awal->nominal_bayar
            );
        }

        foreach($d_bayar->get() as $d){
            $data['bayar'][] = array('id_hutangbeli_bayar' => $d->id_hutangbeli_bayar,
                                    'id_pembelian' => $d->id_pembelian,
                                    'id_akun' => $d->id_akun,
                                    'id_carabayar' => $d->id_carabayar,
                                    'kode_perkiraan' => $d->kode_perkiraan.' - '.$d->nama_perkiraan,
                                    'tgl_bayar' => tgl_full($d->tgl_bayar, ''),
                                    'jenis' => $d->cara_bayar.' - '.$d->tipe_bayar,
                                    'jum_bayar' => $d->jum_bayar);
        }

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

    function simpan(Request $request){
        $id_hutangbeli_bayar = $request->get('id_hutangbeli_bayar');
        $data['id_pembelian'] = $request->get('id_beli');
        $data['tgl_bayar'] = tgl_full($request->get('tanggal'), '2');
        $data['jum_bayar'] = $request->get('jum_bayar');
        $data['id_akun'] = $request->get('id_akun');
        $data['id_carabayar'] = $request->get('id_carabayar');

        if($id_hutangbeli_bayar == ''){
            DB::table('pem_hutangbeli_bayar')->insert($data);
        }else{
            DB::table('pem_hutangbeli_bayar')->where('id_hutangbeli_bayar', $id_hutangbeli_bayar)->update($data);
        }

        return response()->json(array('status' => '1'));
    }

    function hapus(Request $request){
        $id = $request->get('id');

        DB::table('pem_hutangbeli_bayar')->where(array('id_hutangbeli_bayar' => $id))->delete();

        return response()->json(array('status' => '1'));
    }
}
