<?php 

namespace App\model;
use Illuminate\Database\Eloquent\Model;
use DB;

class Report extends Model 
{
    public $timestamps = false;
    protected $guarded = array();
    protected $table = 'reports';    
    protected $primaryKey = 'report_id';    
    protected $hidden = array('report_tax');
    protected $fillable = ['report_date', 'report_market_place_id', 'report_order_id', 'report_item_sku', 'report_selling_price', 'report_shipping_cost', 'report_net_cost','report_item_quantity', 'report_tax', 'report_source_id','report_market_place_comminsion'];
    
    static function market_place_reports($report_type,$date_from,$date_to)
    {
        if($report_type == 'market_place')
        {
            return DB::table('reports')
            ->leftJoin('market_places', 'reports.report_market_place_id', '=', 'market_places.id') 
            ->leftJoin('sources', 'reports.report_source_id', '=', 'sources.id')
            ->select('*')
            ->whereBetween('reports.report_date',[$date_from,$date_to])
            ->orderBy('reports.report_date')        
            ->orderBy('market_places.name')
            ->orderBy('sources.source_name')        
            ->get();
        }
        elseif ($report_type == 'monthly')
        {
            return DB::table('reports')
            ->leftJoin('market_places', 'reports.report_market_place_id', '=', 'market_places.id') 
            ->leftJoin('sources', 'reports.report_source_id', '=', 'sources.id')
            ->select('*')
            ->whereBetween('reports.report_date',[$date_from,$date_to])
            ->orderBy('market_places.name')        
            ->orderBy('reports.report_date')  
            ->orderBy('sources.source_name')        
            ->get();
        }
        else
        {
            return DB::table('reports')
            ->leftJoin('market_places', 'reports.report_market_place_id', '=', 'market_places.id') 
            ->leftJoin('sources', 'reports.report_source_id', '=', 'sources.id')
            ->select('*')
            ->whereBetween('reports.report_date',[$date_from,$date_to])
            ->orderBy('reports.report_date')        
            ->orderBy('sources.source_name')
            ->orderBy('market_places.name')
            ->get();
        }
    }
    
    
    static function find_order_id($order_id,$current_date)
    {       
        $start_date = date("Y-m-d",strtotime($current_date."-1 month"));  
        return DB::table('reports')
        ->select('report_order_id')
        ->whereBetween('report_date',array($start_date,$current_date))
        ->where('report_order_id', '=', $order_id)
        ->get();
    }
    
    
//    static function show_reports()
//    {
//        return DB::table('reports as r')
//        ->leftJoin('market_places as m', 'r.report_market_place_id', '=', 'm.id') 
//        ->leftJoin('sources as s', 'r.report_source_id', '=', 's.id')
//        ->select('*')
//        ->orderBy('r.report_id', 'desc')
//        ->take(10)
//        ->get(); 
//    }
    
    
    static function find_an_report($order_id)
    {
        return DB::table('reports')
        ->where('report_order_id','=',$order_id)
        ->leftJoin('market_places', 'reports.report_market_place_id', '=', 'market_places.id') 
        ->leftJoin('sources', 'reports.report_source_id', '=', 'sources.id')
        ->get();
    }
    
    
}