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