<?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 "no token";
    }
    
    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;
        
    }
    
    function outstand_det($cust){ 
        
       // $invdet = app('db')->select("SELECT `po_id`,`qto_code`,`po_final_tot`,`return_amnt` FROM `mes_qto` 
          //   WHERE `po_sup_id`='$cust' AND `po_payment`=0");
             
             $invdet = app('db')->select("SELECT `po_id`,`qto_code`,`po_final_tot`,`return_amnt` FROM `mes_qto` 
             WHERE `po_sup_id`='$cust'");
             
        $invoice = [];
        
             for($i=0; $i<1; $i++){
                 $inv = $invdet[$i]->po_id;
                 
                 //$payvdet = app('db')->select("SELECT SUM(`pay_amount`)as sum FROM `mes_payment` WHERE `pay_invid`='$inv' AND `pay_authrize`=1 AND `pay_stetus`=1");
                 $payvdet = app('db')->select("SELECT SUM(`pay_amount`)as sum FROM `mes_payment` WHERE `pay_invid`='$inv' AND `pay_stetus`=1 AND `pay_stetus`=0");
                 if($payvdet[0]->sum!=null){
                     $out = $invdet[$i]->po_final_tot - $payvdet[0]->sum;
                 }else{
                     $out = $invdet[$i]->po_final_tot;
                 }
                 
                 
                // $retdet = app('db')->select("SELECT SUM(`ret_amount`) as sum FROM `inv_return` WHERE `ret_invnum`='$inv' AND `ret_stetus`=1 AND `ret_proceed`=1");
                 $retdet = app('db')->select("SELECT SUM(`ret_amount`) as sum FROM `inv_return` WHERE `ret_invnum`='$inv' AND `ret_stetus`=1");
                 if($retdet[0]->sum!=null){
                     $ret = $retdet[0]->sum;
                 }else{
                     $ret = 0;
                 }
                 
                 
                 //$invoice[] = array('inv_id'=>$invdet[$i]->qto_code.$invdet[$i]->po_id,'tot'=>$invdet[$i]->po_final_tot,'out'=>$out,'ret'=>$ret,'bal'=>($out-$ret));
                 $invoice[] = array('inv_id'=>"",'tot'=>0,'out'=>$out,'ret'=>$ret,'bal'=>0);
             }
      
        return $invoice;
        
    }
}
