<?php



namespace App\Models;



use DB;

use Illuminate\Database\Eloquent\Model;



class Consortia extends Model

{

    public $table      = 'consortia';

    public $timestamps = false;



    /*

     * Date: 11-10-2018

     * Author: Muhammad Arshad

     * Parameter: Consortia name

     * Description:  this function will return the list of  Consortia

     */

    public static function GetConsortiaList($param = null)

    {

        $Consortia = DB::table('consortia')

            ->select('*');

        

        if (!empty($param['search'])) {

            $search   = $param['search'];

            $Consortia = $Consortia->where('consortia.name', 'like', '%' . $search . '%');

        }

        $Consortia = $Consortia->where('consortia.status', 'Active');

        $Consortia = $Consortia->orderBy('consortia.name', 'DESC');

        $Consortia = $Consortia->paginate(10);



        return $Consortia;

    }



    

    /*

     * Date: 11-10-2018

     * Author: Muhammad Arshad

     * Parameter: Consortia id

     * Description:  this function will return the Consortia detail by id

     */



    public static function GetConsortiaById($id)

    {

        $result   = DB::table('consortia')->where('consortia_id', '=', $id)->get();

        $Consortia = $result->toArray()[0];

        return $Consortia;

    }



   

   



    /*

     * Date: 30-08-2018

     * Author: Muhammad Arshad

     * Parameter: data

     * Description:  this function will update the Consortia

     */



    public static function UpdateConsortia($data)

    {



        DB::table('consortia')

            ->where('id', $data['id'])

            ->update($data);

    }



    /*

     * Date: 30-08-2018

     * Author: Muhammad Arshad

     * Parameter: data

     * Description:  this function will delete the Consortia temporarily

     */

    public static function RemoveConsortiaById($consortia_id)

    {



        //DB::table('Consortia')->where('id', '=', $id)->delete();

        DB::table('consortia')

            ->where('consortia_id', $consortia_id)

            ->update(['status' => 'Deleted']);

    }



     /*

     * Date: 12-10-2018

     * Author: Muhammad Arshad

     * Parameter: Consortia id

     * Description:  this function will return the list of  organizations of a consortia

     */



    public static function GetConsortiaOrganizationsListById($consortia_id){



        $result   = DB::table('consortia_organization as c')

        ->join('users as u', 'u.id', 'c.user_id')

        ->where('c.consortia_id', '=', $consortia_id)

        ->get();

        

        return $result;



    }



     /*

     * Date: 12-10-2018

     * Author: Muhammad Arshad

     * Parameter: username/orgname

     * Description:  this function will return the All  organizations list

     */



    public static function getAllOgranization($param){



        $username = $param['username'];

        $utype = $param['utype'];

        $arr= array();

        if($utype==0){

            $result = DB::select(DB::raw("select o.uid,o.org_name as names from organization o

                                left join consortia_organization co  on o.uid = co.user_id

                                where co.user_id is  null and o.user_name = '$username' order by o.org_name

                                  "));

        }else if($utype==1){//organization user case

            $result = DB::select(DB::raw("select o.uid,o.org_name as names from organization o

                                left join consortia_organization co on o.uid = co.user_id

                                where co.user_id is null and  o.org_name like '%$username%'

                                order by o.org_name"));

        }else{

        $result = DB::select(DB::raw("select o.uid,o.org_name as names from organization o

                                left join consortia_organization co on o.uid = co.user_id

                                where co.user_id is null  order by o.org_name"));

        }

        $orglist = $result;

        if($orglist)

        {

            //$arr[-1] = 'Select An Organization';

            foreach($orglist as $data) {

                

                $arr[$data->uid] = trim(ucwords(mb_strtolower($data->names)));

            }

        }

        else

        {

            $arr = null;

        }

        

        return $arr;

    }



    public static function AddConsortiaOrganization($data){



       DB::table('consortia_organization')->insert($data);

       return true;

    }



    public static function DeleteConsortiaOrganization($consortia_id){



         DB::table('consortia_organization')

            ->where('consortia_id', $consortia_id)

            ->delete();

        return true;

    }



}

