create feature sa create list claim and price to work per dealer
This commit is contained in:
@@ -48,4 +48,38 @@ class Dealer extends Model
|
||||
->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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,12 @@ class Transaction extends Model
|
||||
{
|
||||
use HasFactory, SoftDeletes;
|
||||
protected $fillable = [
|
||||
"user_id", "user_sa_id", "work_id", "form", "spk", "police_number", "warranty", "date", "qty", "status", "dealer_id"
|
||||
"user_id", "user_sa_id", "work_id", "form", "spk", "police_number", "warranty", "date", "qty", "status", "dealer_id",
|
||||
"claimed_at", "claimed_by"
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'claimed_at' => 'datetime',
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
@@ -54,4 +54,52 @@ class Work extends Model
|
||||
{
|
||||
return $this->belongsTo(Category::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all dealer prices for this work
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function dealerPrices()
|
||||
{
|
||||
return $this->hasMany(WorkDealerPrice::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get price for specific dealer
|
||||
*
|
||||
* @param int $dealerId
|
||||
* @return WorkDealerPrice|null
|
||||
*/
|
||||
public function getPriceForDealer($dealerId)
|
||||
{
|
||||
return $this->dealerPrices()
|
||||
->where('dealer_id', $dealerId)
|
||||
->active()
|
||||
->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get price for specific dealer (including soft deleted)
|
||||
*
|
||||
* @param int $dealerId
|
||||
* @return WorkDealerPrice|null
|
||||
*/
|
||||
public function getPriceForDealerWithTrashed($dealerId)
|
||||
{
|
||||
return $this->dealerPrices()
|
||||
->withTrashed()
|
||||
->where('dealer_id', $dealerId)
|
||||
->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all active prices for this work
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function activeDealerPrices()
|
||||
{
|
||||
return $this->hasMany(WorkDealerPrice::class)->active();
|
||||
}
|
||||
}
|
||||
|
||||
81
app/Models/WorkDealerPrice.php
Normal file
81
app/Models/WorkDealerPrice.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class WorkDealerPrice extends Model
|
||||
{
|
||||
use HasFactory, SoftDeletes;
|
||||
|
||||
protected $fillable = [
|
||||
'work_id',
|
||||
'dealer_id',
|
||||
'price',
|
||||
'currency',
|
||||
'is_active'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'price' => 'decimal:2',
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the work associated with the price
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function work()
|
||||
{
|
||||
return $this->belongsTo(Work::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the dealer associated with the price
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function dealer()
|
||||
{
|
||||
return $this->belongsTo(Dealer::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to get only active prices
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeActive($query)
|
||||
{
|
||||
return $query->where('is_active', true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get formatted price with currency
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFormattedPriceAttribute()
|
||||
{
|
||||
return number_format($this->price, 0, ',', '.') . ' ' . $this->currency;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get price for specific work and dealer
|
||||
*
|
||||
* @param int $workId
|
||||
* @param int $dealerId
|
||||
* @return WorkDealerPrice|null
|
||||
*/
|
||||
public static function getPriceForWorkAndDealer($workId, $dealerId)
|
||||
{
|
||||
return static::where('work_id', $workId)
|
||||
->where('dealer_id', $dealerId)
|
||||
->active()
|
||||
->first();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user