<?php

namespace App\Models;

use DB;
use Illuminate\Database\Eloquent\Model;

class JMSContent extends Model
{
    public $table      = 'jms_content';
    public $timestamps = false;

    /*
     * Date: 10-10-2022
     * Author: Imran Khan
     * Parameter: param
     * Description:  this function will return the JMS article list
     */

    public static function GetJMSContentList($param = null,$join = null)
    {  
        $article = DB::table('jms_content as jc');
			
		$article = $article->addSelect('jc.*', 'j.journal_id', 'j.title', 'a.article_id', 'a.volume_id', 'a.art_type as article_type_id');
        $article = $article->join('journal as j' , 'jc.journal_code', '=', 'j.jms_jcode'); 
        $article = $article->leftjoin('article as a', 'a.jms_ref_no', '=', 'jc.jms_ref_no');    
        
        if (!empty($param['jms_ref_no'])) {
            $article = $article->where('jc.jms_ref_no', 'like', '%' . $param['jms_ref_no'] . '%');
        }
        
        if (!empty($param['journal_id'])) {
            $article = $article->where('jc.jms_ref_no', 'like', '%' . $param['journal_id'] . '%');
        }
        
        if (isset($param['status']) && $param['status'] <> 4) {
            $article = $article->where('jc.status', '=', $param['status']);
        }

        if (isset($param['article_type']) && $param['article_type'] <> 7) {
            $article = $article->where('jc.is_epub', '=', $param['article_type']);
        }
        
        $article = $article->where('j.journal_status', '<>', 2);
        
        if (!empty($param['paginate'])) 
        {
            $article = $article->orderBy('datetime', 'desc');
            $article = $article->orderBy('status', 'asc');
            $perPage    =   $article->count();
            $article = $article->paginate($perPage);
        }
        else
        {
            $article = $article->orderBy('datetime', 'DESC');
            $article = $article->orderBy('status', 'asc');
            $article = $article->paginate(10);
        }

        return $article;
    }

    /*
     * Date: 10-10-2022
     * Author: Imran Khan
     * Parameter: id
     * Description: This function return all rows of article by id
     */
    public static function GetJMSContentId($id)
    {
        $article = DB::table('jms_content')->where('id', '=', $id)->first();
        return $article;
    }

    /*
     * Date: 24-10-2022
     * Author: Imran Khan
     * Parameter: reference no
     * Description: This function return all rows of article by reference no
     */
    public static function GetJMSContentByRef($data)
    {
        $article = DB::table('jms_content')->where('jms_ref_no', '=', $data['jms_ref_no'])->where('is_epub', '=', $data['is_epub'])->first();
        return $article;
    }

/*
 * Date: 03-10-2022
 * Author: Imran Khan
 * Parameter: data
 * Description:  this function will add data in JMS Content
 */
    public static function AddJMSContent($data) {
        //dd($data);
        $id = DB::table('jms_content')->insert($data);
        return $id;
    }

    /*
     * Date: 24-10-2000
     * Author: Imran Khan
     * Parameter: id
     * Description:  this function will update the JMS Content
     */
    public static function UpdateJMSContent($data)
    {
        DB::table('jms_content')->where('jms_ref_no', $data['jms_ref_no'])->where('is_epub', $data['is_epub'])->update($data);
    }

}
