86 lines
2.0 KiB
PHP
Executable File
86 lines
2.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class Dealer extends Model
|
|
{
|
|
use HasFactory, SoftDeletes;
|
|
protected $fillable = [
|
|
'dealer_code', 'name', 'address', 'pic'
|
|
];
|
|
|
|
/**
|
|
* Get all of the transactions for the Dealer
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function transactions()
|
|
{
|
|
return $this->hasMany(Transaction::class, 'dealer_id', 'id');
|
|
}
|
|
|
|
public function opnames(){
|
|
return $this->hasMany(Opname::class);
|
|
}
|
|
|
|
public function outgoingMutations()
|
|
{
|
|
return $this->hasMany(Mutation::class, 'from_dealer_id');
|
|
}
|
|
|
|
public function incomingMutations()
|
|
{
|
|
return $this->hasMany(Mutation::class, 'to_dealer_id');
|
|
}
|
|
|
|
public function stocks()
|
|
{
|
|
return $this->hasMany(Stock::class);
|
|
}
|
|
|
|
public function products()
|
|
{
|
|
return $this->belongsToMany(Product::class, 'stocks', 'dealer_id', 'product_id')
|
|
->withPivot('quantity')
|
|
->withTimestamps();
|
|
}
|
|
|
|
/**
|
|
* Get all work prices for this dealer
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function workPrices()
|
|
{
|
|
return $this->hasMany(WorkDealerPrice::class);
|
|
}
|
|
|
|
/**
|
|
* Get price for specific work
|
|
*
|
|
* @param int $workId
|
|
* @return WorkDealerPrice|null
|
|
*/
|
|
public function getPriceForWork($workId)
|
|
{
|
|
return $this->workPrices()
|
|
->where('work_id', $workId)
|
|
->active()
|
|
->first();
|
|
}
|
|
|
|
/**
|
|
* Get all active work prices for this dealer
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function activeWorkPrices()
|
|
{
|
|
return $this->hasMany(WorkDealerPrice::class)->active();
|
|
}
|
|
}
|