<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use  App\User;

class UserController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Get the authenticated User.
     *
     * @return Response
     */
    public function profile()
    {
       $this->db->select('mes_qto.*,mes_customer.*,mes_currancy.*,mes_country.*');
            $this->db->from('mes_qto');
            $this->db->join( "mes_customer", "mes_customer.cust_id = mes_qto.po_sup_id", 'left');
            $this->db->join( "mes_currancy", "mes_currancy.curr_id = mes_qto.po_currancy", 'left');
            $this->db->join( "mes_country", "mes_country.cnty_id = mes_customer.cust_country", 'left');
            $this->db->order_by("mes_qto.po_id","desc");
            $hasil=$this->db->get();
            $data= $hasil->result(); 
            return response()->json(['users' =>  User::all()], 200);
    }

    /**
     * Get all User.
     *
     * @return Response
     */
    public function allUsers()
    {
         return response()->json(['users' =>  User::all()], 200);
    }

    /**
     * Get one user.
     *
     * @return Response
     */
    
    public function singleUser($id)
    {
        try {
            $user = User::findOrFail($id);

            return response()->json(['user' => $user], 200);

        } catch (\Exception $e) {

            return response()->json(['message' => 'user not found!'], 404);
        }

    }
    
    function get_user_dt(Request $request){
        $headers = $request->header('Authorization');
        $dt = new User;
        $user_data = $dt->getBearerToken($headers);
       $dataa = response()->json($user_data);
            return $dataa;   
    }
    
    public function dashboard(Request $request){
        $headers = $request->header('Authorization');
        $dt = new User;
        $user_data = $dt->getBearerToken($headers);
        
        $ref = $user_data[0]->user_id;
        $today = date("Y-m-d");
        
        $today_inv = app('db')->select("SELECT SUM(`po_final_tot`) as total, count(`po_id`) as count, SUM(`return_amnt`) as ret from `mes_qto` where `po_ref_id` ='$ref' AND `po_auth` ='1' AND `po_cdate` ='$today'");
        $today_pinv = app('db')->select("SELECT SUM(`pay_amount`) as total, count(`pay_id`) as count from `mes_payment_main` where `pay_ref_id` ='$ref' AND `pay_authrize` ='1' AND `pay_date` ='$today'");
            
                        
        if(is_null($today_inv[0]->total)){
            $ttot = 0; $tcount = 0;
        }else{ $ttot = $today_inv[0]->total-$today_inv[0]->ret; $tcount = $today_inv[0]->count; }
        
        if(is_null($today_pinv[0]->total)){
            $ptot = 0; $pcount = 0;
        }else{ $ptot = $today_pinv[0]->total; $pcount = $today_pinv[0]->count; }
        
        
         $tot_tday[0] = array('today_total_inv'=>$ttot,'today_inv_count'=>$tcount,'today_total_payment'=>$ptot,'today_paym_count'=>$pcount);
            
            
        $first_day_this_month = date('Y-m-01'); 
        $last_day_this_month  = date('Y-m-t');  
        
        $month_inv = app('db')->select("SELECT SUM(`po_final_tot`) as total,SUM(`return_amnt`) as retturn, count(`po_id`) as countt from `mes_qto` where `po_ref_id` ='$ref' AND `po_auth` ='1' AND
        `po_cdate` >='$first_day_this_month' AND `po_cdate` <='$last_day_this_month'");
        
        $month_py = app('db')->select("SELECT SUM(`pay_amount`) as total, count(`pay_id`) as countt from `mes_payment_main` where `pay_ref_id` ='$ref' AND `pay_authrize` ='1' AND
        `pay_date` >='$first_day_this_month' AND `pay_date` <='$last_day_this_month'");
        
        if(is_null($month_inv[0]->total)){
            $mtot = 0; $mcount = 0;
        }else{ $mtot = ($month_inv[0]->total-$month_inv[0]->retturn); $mcount = $month_inv[0]->countt;  }
        
        if(is_null($month_py[0]->total)){
            $mptot = 0; $mpcount = 0;
        }else{ $mptot = $month_py[0]->total; $mpcount = $month_py[0]->countt;  }
        
        
        $tot_tday[1] = array('month_total_inv'=>$mtot,'month_inv_count'=>$mcount,'monthly_total_payment'=>$mptot,'monthly_paym_count'=>$mpcount);
       
       $tt = app('db')->select("SELECT SUM(`po_final_tot`) as tot,SUM(`return_amnt`) as ret FROM `mes_qto` 
        WHERE `po_ref_id`='$ref' AND `po_final_tot`!=`return_amnt` AND `po_payment`=0"); 
        
        $tt_data = app('db')->select("SELECT `po_id` FROM `mes_qto` WHERE `po_ref_id`='$ref' AND `po_final_tot`!=`return_amnt` AND `po_payment`=0");
        
        $inv_p = 0;
        foreach ($tt_data as $row) {
            $paym_data = app('db')->select("SELECT SUM(`pay_amount`) as totp FROM `mes_payment` WHERE `pay_invid`='$row->po_id' AND `pay_authrize`=1 AND `pay_stetus`=1");
            $inv_p = $inv_p + $paym_data[0]->totp;
        }
        
        $paym = app('db')->select("SELECT SUM(`pay_amount`) as totp FROM `mes_payment_main` WHERE `pay_ref_id`='$ref' AND `pay_stetus`=1");
        
        
        $tcount = app('db')->select("SELECT count(`po_id`) as countt FROM `mes_qto` 
        WHERE `po_ref_id`='$ref' AND `po_final_tot`!=`return_amnt` AND `po_payment`=0");
        
        if($tcount[0]->countt){$count = $tcount[0]->countt;}else{$count = 0;}
        if($tt[0]->tot){$tot = $tt[0]->tot;}else{$tot = 0;}
        if($tt[0]->ret){$ret = $tt[0]->ret;}else{$ret = 0;}
        if($inv_p!=""){$totp = $inv_p;}else{$totp = 0;}
        
        $out = $tot-($ret+$totp);
        
        $tot_tday[2] = array('outstanding'=>$out,'out_count'=>$count);
        
         $dataa = response()->json($tot_tday);
            return $dataa;   
    }
    
}
