<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class LocationProduct extends Model
{

    protected $table = 'location_products';
    
    public $timestamps = false;
    protected $fillable = [
        'location_id','product_id','grade_id','standard_id'
    ];


    public function product()
    {
        return $this->belongsTo('App\Product', 'product_id');
    }

    public function location()
    {
        return $this->belongsTo('App\Location', 'location_id');
    }

    public function grade()
    {
        return $this->belongsTo('App\GradeMaster','grade_id');
    }

    public function standard()
    {
        return $this->belongsTo('App\StandardMaster','standard_id');
    }

    public function productList($location_id)
    {
        return LocationProduct::where('location_id','=',$location_id)->distinct()->get(['product_id']);
    }

    public function gradeName($gid)
    {
        return GradeMaster::find($gid)->grade;
    }

    public function locationMaserGradeShow($product_id,$location_id)
    {
        $grades = GradeMaster::select();
        $stanrds = LocationProduct::join('standard_master','standard_master.id','=','location_products.standard_id')
                    ->where('location_products.product_id','=',$product_id)
                    ->where('location_products.location_id','=',$location_id)
                    ->selectRaw('standard_master.standard,location_products.grade_id')
                    ->get();
        $ths = $this;
        $colctn = $stanrds->map(function($std) use ($ths) {
            return $std->standard.'-'.$this->gradeName($std->grade_id);
        });
        $stanrds_str =  implode(',', $colctn->toArray());
        return $stanrds_str;
    }
}	
