<?php



namespace App\Models;



use DB;

use Auth;

use Illuminate\Database\Eloquent\Model;



class Cart extends Model

{



	public $table = 'uc_cart_products';

	public $timestamps = false;

    



     /*

     * Date: 16-01-2019

     * Author: Arshad

     * Parameter: $data

     * Description:  this function will add the cart items

     */

    public static function AddToCart($data)

    {  



      $count = DB::table('uc_cart_products')->where('cart_id',$data['cart_id'])->where('nid',$data['nid'])->count();

      if($count>0){

        return false;

      }

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

        return true;



    }



    /*

     * Date: 18-01-2019

     * Author: Arshad

     * Parameter: id

     * Description:  this function will delete the selected Cart Item

     */

    public static function DeleteCartItem($cart_item_id)

    {



        DB::table('uc_cart_products')->where('cart_item_id', $cart_item_id)->delete();



        return $cart_item_id;

    }



     /**

     * Empty a cart of its contents.

     */

    public static function uc_cart_empty($cart_id)

    {

        if (is_null($cart_id) || empty($cart_id)) {

            return;

        }



        DB::table('uc_cart_products')->where('cart_id', $cart_id)->delete();

    }





    /*

     * Date: 19-01-2019

     * Author: Arshad

     * Parameter: $cid,$data

     * Description:  this function will Update the Order comments

     */

    public static function UpdateCart($cid,$data)

    {

        DB::statement("UPDATE IGNORE uc_cart_products SET cart_id = ".$data['cart_id']." where cart_id = '".$cid."'");

        /*DB::table('uc_cart_products')

        ->where('cart_id', $cid)

        ->update($data);        

        return true;*/

    }



    /*

     * Date: 16-01-2019

     * Author: Arshad

     * Parameter: $cart_id

     * Description:  this function will return the cart products list

     */

    public static function GetCartProducts($cart_id)

    {

        //$products=DB::table('uc_cart_products')->where('cart_id','=',$cart_id)->get();



        $sql="select *

            from (

            select c.*,a.title item_title,v.volume_name volume,j.journal_id promotion_content_id,j.title journal_ebook_title,i.title issue,if(a.is_epub=0,j.price,j.epub_price) as item_cost,if(a.is_epub=0,j.price,j.epub_price) as item_price, 'article' as item_type,'N' as discount from uc_cart_products c 

              inner join article a on a.nid=c.nid

              inner join volume v on v.volume_id=a.volume_id

              inner join journal j on j.journal_id=a.journal_id

              left join issue i on i.issue_id=a.issue_id 

              WHERE c.cart_id ='$cart_id'



              union 



              select c.*,ch.title item_title,v.volume_name volume,e.ebook_id promotion_content_id,e.title journal_ebook_title,'' as issue,v.chapter_price item_cost,v.chapter_price as item_price,'chapter' as item_type,'N' as discount from uc_cart_products c 

              inner join chapter ch on ch.nid=c.nid

              inner join ebook_volume v on v.ebook_volume_id=ch.ebook_volume_id

              inner join ebook e on e.ebook_id=v.ebook_id              

              WHERE c.cart_id ='$cart_id'



              union 



              select c.*,CONCAT(e.title,' ',v.volume_name) as item_title,v.volume_name volume,e.ebook_id promotion_content_id,e.title journal_ebook_title,'' as issue,v.ebook_price item_cost, v.ebook_price as item_price,'ebook_volume' as item_type,'N' as discount from uc_cart_products c               

              inner join ebook_volume v on v.nid=c.nid

              inner join ebook e on e.ebook_id=v.ebook_id              

              WHERE c.cart_id ='$cart_id'



              ) a

        order by cart_item_id desc



              ";



        $products = DB::select($sql);    



        return $products;

    }





    public static function total(){



      $user = Auth::user();



      if ($user) {

          if (session()->has('uc_cart_id')){            

            $cdata['cart_id'] = $user->id;

            Cart::UpdateCart(session()->get('uc_cart_id'), $cdata);

          }

          $cart_id =  $user->id;

      } elseif (!session()->has('uc_cart_id')) {

          session(['uc_cart_id' => md5(uniqid(rand(), true))]);          

      }

      $cart_id = isset($cart_id) ? $cart_id : session()->get('uc_cart_id');



      $cart_count = DB::table('uc_cart_products')->whereRaw("cart_id='$cart_id'")->count();

      return $cart_count;

    }







    /*

     * Date: 13-06-2019

     * Author: Arshad

     * Parameter: $data array

     * Description:  this function will add the rating/comments of an order

     */



    public static function AddOrderRatingComments($data)

    {

      

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

      return true;



    }





}

