|
|
|
@ -2,7 +2,6 @@
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
@ -23,17 +22,11 @@ class ServiceType extends Model
|
|
|
|
|
'average_price',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
//to do: date between?
|
|
|
|
|
|
|
|
|
|
public function getQuantityAttribute($created_at = null, $created_until = null): int
|
|
|
|
|
{
|
|
|
|
|
return $this->productServices()
|
|
|
|
|
->when($created_at, function (Builder $query) use ($created_at) {
|
|
|
|
|
return $query->whereDate('created_at', '>=', $created_at);
|
|
|
|
|
})
|
|
|
|
|
->when($created_until, function (Builder $query) use ($created_until) {
|
|
|
|
|
return $query->whereDate('created_at', '<=', $created_until);
|
|
|
|
|
})
|
|
|
|
|
->when($created_at, fn ($query) => $query->whereDate('created_at', '>=', $created_at))
|
|
|
|
|
->when($created_until, fn ($query) => $query->whereDate('created_at', '<=', $created_until))
|
|
|
|
|
->sum('amount');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -41,12 +34,8 @@ class ServiceType extends Model
|
|
|
|
|
{
|
|
|
|
|
return number_format(
|
|
|
|
|
$this->productServices()
|
|
|
|
|
->when($created_at, function (Builder $query) use ($created_at) {
|
|
|
|
|
return $query->whereDate('created_at', '>=', $created_at);
|
|
|
|
|
})
|
|
|
|
|
->when($created_until, function (Builder $query) use ($created_until) {
|
|
|
|
|
return $query->whereDate('created_at', '<=', $created_until);
|
|
|
|
|
})
|
|
|
|
|
->when($created_at, fn ($query) => $query->whereDate('created_at', '>=', $created_at))
|
|
|
|
|
->when($created_until, fn ($query) => $query->whereDate('created_at', '<=', $created_until))
|
|
|
|
|
->sum('amount_price'),
|
|
|
|
|
2
|
|
|
|
|
);
|
|
|
|
@ -54,30 +43,31 @@ class ServiceType extends Model
|
|
|
|
|
|
|
|
|
|
public function getSalesPercentageAttribute($created_at = null, $created_until = null): float
|
|
|
|
|
{
|
|
|
|
|
$total = ProductService::when($created_at, function (Builder $query) use ($created_at) {
|
|
|
|
|
return $query->whereDate('created_at', '>=', $created_at);
|
|
|
|
|
})
|
|
|
|
|
->when($created_until, function (Builder $query) use ($created_until) {
|
|
|
|
|
return $query->whereDate('created_at', '<=', $created_until);
|
|
|
|
|
})
|
|
|
|
|
->count();
|
|
|
|
|
|
|
|
|
|
$part = ProductService::where('service_type_id', $this->id)
|
|
|
|
|
->when($created_at, function (Builder $query) use ($created_at) {
|
|
|
|
|
return $query->whereDate('created_at', '>=', $created_at);
|
|
|
|
|
})
|
|
|
|
|
->when($created_until, function (Builder $query) use ($created_until) {
|
|
|
|
|
return $query->whereDate('created_at', '<=', $created_until);
|
|
|
|
|
})
|
|
|
|
|
->count();
|
|
|
|
|
|
|
|
|
|
return round(($part / $total) * 100, 2);
|
|
|
|
|
$query = ProductService::query()
|
|
|
|
|
->when($created_at, fn ($query) => $query->whereDate('created_at', '>=', $created_at))
|
|
|
|
|
->when($created_until, fn ($query) => $query->whereDate('created_until', '<=', $created_until));
|
|
|
|
|
|
|
|
|
|
$total = $query->count();
|
|
|
|
|
|
|
|
|
|
$part = $query->where('service_type_id', $this->id)->count();
|
|
|
|
|
|
|
|
|
|
if ($total == 0) {
|
|
|
|
|
return 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return round(($part / $total) * 100, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getAveragePriceAttribute($created_at = null, $created_until = null): string
|
|
|
|
|
{
|
|
|
|
|
return number_format(floatval($this->getAmountAttribute($created_at, $created_until)) /
|
|
|
|
|
$this->getQuantityAttribute($created_at, $created_until), 2);
|
|
|
|
|
$quantity = $this->getQuantityAttribute($created_at, $created_until);
|
|
|
|
|
$amount = $this->getAmountAttribute($created_at, $created_until);
|
|
|
|
|
|
|
|
|
|
if ($quantity == 0) {
|
|
|
|
|
return '0.0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return number_format($quantity / $amount, 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function productServices(): HasMany
|
|
|
|
|