d2d.emegrendeles.hu/app/Services/DeliveryCalendarService.php
2026-04-15 06:42:55 +02:00

77 lines
2.3 KiB
PHP

<?php
namespace App\Services;
use App\Models\DeliveryCalendarOverride;
use App\Models\ProfitCenter;
use App\Models\ProfitCenterSupplierSchedule;
use App\Models\Supplier;
use Carbon\Carbon;
use Illuminate\Support\Collection;
class DeliveryCalendarService extends ServiceBase
{
public function __construct(
protected WorkCalendarService $workCalendarService
) {
}
/**
* Eldönti, hogy egy adott Profit Center, Beszállító és Dátum kombináció esetén van-e szállítás.
*/
public function isDeliveryDay(ProfitCenter $pc, Supplier $supplier, Carbon $date): bool
{
$date = $date->startOfDay();
// 1. Prioritás: Beszállítói felülbírálás (Override)
$override = DeliveryCalendarOverride::where('supplier_id', $supplier->id)
->whereDate('date', $date)
->first();
if ($override) {
return $override->is_delivery_day;
}
// 2. Prioritás: Hivatalos ünnepnap (WorkCalendar)
// Ha aznap nem hivatalos munkanap van, akkor nincs szállítás (kivéve ha az 1. pont felülbírálta).
if (! $this->workCalendarService->isWorkDay($date)) {
return false;
}
// 3. Prioritás: Heti sablon (Schedule)
$assignment = ProfitCenterSupplierSchedule::where('profit_center_id', $pc->id)
->where('supplier_id', $supplier->id)
->with('deliverySchedule')
->first();
if (! $assignment || ! $assignment->deliverySchedule || ! $assignment->deliverySchedule->is_active) {
return false;
}
$dayOfWeek = strtolower($date->format('l')); // 'monday', 'tuesday', etc.
return (bool) $assignment->deliverySchedule->{$dayOfWeek};
}
/**
* Visszaadja az elérhető szállítási napokat egy adott időszakban.
*
* @return Collection<Carbon>
*/
public function getAvailableDeliveryDates(ProfitCenter $pc, Supplier $supplier, Carbon $start, Carbon $end): Collection
{
$dates = collect();
$current = $start->copy()->startOfDay();
$end = $end->copy()->startOfDay();
while ($current <= $end) {
if ($this->isDeliveryDay($pc, $supplier, $current)) {
$dates->push($current->copy());
}
$current->addDay();
}
return $dates;
}
}