<?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'");
        $today_ret = app('db')->select("SELECT SUM(`ret_amount`) as tret FROM `inv_return` WHERE `ret_cdate`='$today' AND `ret_cuser`='$ref'");
            
                        
        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-0; $pcount = $today_pinv[0]->count; }
        
        if(is_null($today_ret[0]->tret)){
            $pret = 0; 
        }else{ $pret = $today_ret[0]->tret-0; }
        
        
         $tot_tday[0] = array('today_total_inv'=>$ttot,'today_inv_count'=>$tcount,'today_total_payment'=>$ptot,'today_paym_count'=>$pcount,'today_return'=>$pret);
            
            
        $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);
        
        $depo_id = $user_data[0]->user_depoid;
            $gt_depot = app('db')->select("SELECT `dep_name` FROM `mes_depot` WHERE `dep_id`='$depo_id'");
            $depo_name = $gt_depot[0]->dep_name;
        
        $tot_tday[2] = array('outstanding'=>$out,'out_count'=>$count,'depo'=>$depo_name);
        
         $dataa = response()->json($tot_tday);
            return $dataa;   
    }
    
    function daily_sum(Request $request){
        
        $headers = $request->header('Authorization');
        $dt = new User;
        $user_data = $dt->getBearerToken($headers);
        $uid = $user_data[0]->user_id;
        
        $fdate = $request->input('fdate');
        $tdate = $request->input('tdate');
        if($fdate==""){
            $fdate = date("Y-m-d");
        }
        if($tdate==""){
            $tdate = date("Y-m-d");
        }
        
         $tinv_data =   app('db')->select("SELECT SUM(`po_final_tot`)  as tinv FROM `mes_qto` LEFT JOIN `mes_customer` ON `mes_qto`.`po_sup_id` = `mes_customer`.`cust_id` 
         WHERE `po_ref_id` = '$uid' AND `po_cdate` >= '$fdate'  AND `po_cdate` <= '$tdate'
         AND `po_auth` = 1 AND `po_final_tot`!=`return_amnt`"); 
            $invoice_tot = $tinv_data[0]->tinv;
            
        $cash_inv =   app('db')->select("SELECT SUM(`pay_amount`) as range_pay FROM `mes_payment` 
        LEFT JOIN `mes_qto` on `mes_payment`.`pay_invid` = `mes_qto`.`po_id` WHERE `mes_payment`.`pay_user`='$uid' 
        AND `mes_payment`.`pay_date`>='$fdate' AND `mes_payment`.`pay_date`<='$tdate' AND `mes_qto`.`po_cdate`>='$fdate' AND `mes_qto`.`po_cdate`<='$tdate';"); 
            $cash_sale = $cash_inv[0]->range_pay;
            
        $credit_sale = $invoice_tot - $cash_sale;
        
        $tpay_data =   app('db')->select("SELECT sum(`pay_amount`) as tpay FROM `mes_payment_main` WHERE  `mes_payment_main`.`pay_date` >= '$fdate' AND `mes_payment_main`.`pay_date` <= '$tdate'
        AND `pay_user`='$uid' AND `pay_stetus`=1"); 
            $pay_tot = $tpay_data[0]->tpay;
            
        $chq_data =   app('db')->select("SELECT sum(`pay_amount`) as tpay FROM `mes_payment_main` WHERE  `mes_payment_main`.`pay_date` >= '$fdate' AND `mes_payment_main`.`pay_date` <= '$tdate'
        AND `pay_user`='$uid' AND `pay_stetus`=1 AND `pay_type`!=0"); 
            $pay_chq = $chq_data[0]->tpay;
            
        $exp_data =   app('db')->select("SELECT sum(`amount`) as tpay FROM `daily_exp` LEFT JOIN `mes_expreson` ON `mes_expreson`.`exp_id` = `daily_exp`.`reason` 
            WHERE `daily_exp`.`ref_id` = '$uid' AND `daily_exp`.`dexp_date` <= '$fdate' AND `daily_exp`.`dexp_date` >= '$tdate' AND `daily_exp`.`dexp_stetus` = 0"); 
            $exp_amount = $exp_data[0]->tpay;
        
            
        
        $tret_data =   app('db')->select("SELECT sum(`ret_amount`) as tret FROM `inv_return`  WHERE `inv_return`.`ret_cuser` = '$uid' AND `inv_return`.`ret_cdate` >= '$fdate' AND `inv_return`.`ret_cdate` <= '$tdate' AND `inv_return`.`ret_stetus` = 1"); 
            $ret_tot = $tret_data[0]->tret;
            
        
        
         $link = "https://manchieedecoco.com/dailysalsesumrylumen?id=".$uid;
         if($invoice_tot!=null){
             $invoice_tot = $invoice_tot;
         }else{
             $invoice_tot = "0";
         }
         
         if($cash_sale!=null){
             $cash_sale = $cash_sale;
         }else{
             $cash_sale = "0";
         }
         
         if($credit_sale!=null){    $credit_sale = $credit_sale; }else{        $credit_sale = "0";       }
         if($pay_tot!=null){    $pay_tot = $pay_tot; }else{        $pay_tot = "0";       }
         if($pay_chq!=null){    $pay_chq = $pay_tot; }else{        $pay_chq = "0";       }
         if($exp_amount!=null){    $exp_amount = $pay_tot; }else{        $exp_amount = "0";       }
         
         $data['invoice_tot'] = money_format("%.2n", $invoice_tot);
         $data['cash_sale'] = money_format("%.2n", $cash_sale);
         $data['credit_sale'] = money_format("%.2n", $credit_sale);
         $data['pay_tot'] = money_format("%.2n", $pay_tot);
         $data['pay_chq'] = money_format("%.2n", $pay_chq);
         $data['exp_amount'] = money_format("%.2n", $exp_amount);
         $data['balance'] = money_format("%.2n", ($pay_tot-$exp_amount));
         $data['link'] = $link;
        
        $dataa = response()->json($data);
            return $dataa;
        
    } 
    
    
    function daily_sum_pdf(Request $request){
        
        $headers = $request->header('Authorization');
        $dt = new User;
        $user_data = $dt->getBearerToken($headers);
        $uid = $user_data[0]->user_id;
        
        $date = $request->input('date');
       
        
         $link = "https://manchieedecoco.com/dailysalsesumrylumen?id=".$date."&ref=".$uid;

        
        $dataa = response()->json($link);
            return $dataa;
        
    }
    
}
