<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;

use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Model implements AuthenticatableContract, AuthorizableContract, JWTSubject
{
    use Authenticatable, Authorizable;
   
public $timestamps = false; 
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
   protected $fillable=[
        'name','email','password'

    ];
    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password',
    ];

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }
    
    function getBearerToken($hd) {
    $headers = $hd;
    // HEADER: Get the access token from the header
    if (!empty($headers)) {
        if (preg_match('/Bearer\s(\S+)/', $headers, $matches)) {
            //return $matches[1];
            $token = $matches[1];
               $data = (json_decode(base64_decode(str_replace('_', '/', str_replace('-','+',explode('.', $token)[1])))));
               $uid = $data->sub;
               $results = app('db')->select("SELECT * from `users` where `id` ='$uid'");
               $uname = $results[0]->name;
               
               $user_data = app('db')->select("SELECT `user_id`,`user_code`,`user_fname`,`user_lname`,`user_name`,`user_lvl`,`user_depoid` from `mes_user` where `user_name` ='$uname'");
               return $user_data;
        }
    }
    return null;
    }
    
    function outstand($cust){
        
        //$tt = app('db')->select("SELECT SUM(`po_final_tot`) as tot,SUM(`return_amnt`) as ret FROM `mes_qto` 
       // WHERE `po_sup_id`='$cust' AND `po_payment`=0");
        $tt = app('db')->select("SELECT SUM(`po_final_tot`) as tot,SUM(`return_amnt`) as ret FROM `mes_qto` 
        WHERE `po_sup_id`='$cust'");
        $paym = app('db')->select("SELECT SUM(`pay_amount`) as totp FROM `mes_payment_main` WHERE `pay_cust_id`='$cust' AND `pay_authrize`=1 AND `pay_stetus`=1");
        
        if($tt[0]->tot){$tot = $tt[0]->tot;}else{$tot = 0;}
        if($tt[0]->ret){$ret = $tt[0]->ret;}else{$ret = 0;}
        if($paym[0]->totp){$totp = $paym[0]->totp;}else{$totp = 0;}
        
        $out = $tot-($totp+$ret);
        //(int)$var;
        return (int)$out;
        
    }
}
