complete sotry 3.6 with qa tests
This commit is contained in:
@@ -4,53 +4,78 @@ namespace App\Services;
|
||||
|
||||
use App\Models\Consultation;
|
||||
use Carbon\Carbon;
|
||||
use Spatie\IcalendarGenerator\Components\Calendar;
|
||||
use Spatie\IcalendarGenerator\Components\Event;
|
||||
use Spatie\IcalendarGenerator\Enums\ParticipationStatus;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class CalendarService
|
||||
{
|
||||
private const DEFAULT_DURATION_MINUTES = 45;
|
||||
|
||||
/**
|
||||
* Generate an ICS calendar file for a consultation.
|
||||
*/
|
||||
public function generateIcs(Consultation $consultation): string
|
||||
{
|
||||
$consultation->load('user');
|
||||
$consultation->loadMissing('user');
|
||||
|
||||
$startDateTime = Carbon::parse($consultation->booking_date)
|
||||
->setTimeFromTimeString($consultation->booking_time);
|
||||
$endDateTime = $startDateTime->copy()->addHour();
|
||||
$user = $consultation->user;
|
||||
$locale = $user?->preferred_language ?? 'ar';
|
||||
|
||||
$locale = $consultation->user?->preferred_language ?? 'ar';
|
||||
$startDateTime = Carbon::parse(
|
||||
$consultation->booking_date->format('Y-m-d').' '.$consultation->booking_time
|
||||
);
|
||||
$duration = $consultation->duration ?? self::DEFAULT_DURATION_MINUTES;
|
||||
$endDateTime = $startDateTime->copy()->addMinutes($duration);
|
||||
|
||||
$eventName = $locale === 'ar'
|
||||
? 'استشارة قانونية - مكتب ليبرا للمحاماة'
|
||||
: 'Legal Consultation - Libra Law Firm';
|
||||
$title = $locale === 'ar'
|
||||
? 'استشارة مع مكتب ليبرا للمحاماة'
|
||||
: 'Consultation with Libra Law Firm';
|
||||
|
||||
$description = $this->buildDescription($consultation, $locale);
|
||||
$location = $this->getLocation($locale);
|
||||
|
||||
$event = Event::create()
|
||||
->name($eventName)
|
||||
->description($description)
|
||||
->uniqueIdentifier("consultation-{$consultation->id}@libra.ps")
|
||||
->createdAt(now())
|
||||
->startsAt($startDateTime)
|
||||
->endsAt($endDateTime)
|
||||
->organizer('info@libra.ps', 'Libra Law Firm');
|
||||
$uid = sprintf('consultation-%d@libra.ps', $consultation->id);
|
||||
|
||||
if ($consultation->user?->email) {
|
||||
$event->attendee(
|
||||
$consultation->user->email,
|
||||
$consultation->user->full_name,
|
||||
ParticipationStatus::Accepted
|
||||
);
|
||||
}
|
||||
$ics = [
|
||||
'BEGIN:VCALENDAR',
|
||||
'VERSION:2.0',
|
||||
'PRODID:-//Libra Law Firm//Consultation Booking//EN',
|
||||
'CALSCALE:GREGORIAN',
|
||||
'METHOD:REQUEST',
|
||||
'BEGIN:VTIMEZONE',
|
||||
'TZID:Asia/Jerusalem',
|
||||
'BEGIN:STANDARD',
|
||||
'DTSTART:19701025T020000',
|
||||
'RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU',
|
||||
'TZOFFSETFROM:+0300',
|
||||
'TZOFFSETTO:+0200',
|
||||
'END:STANDARD',
|
||||
'BEGIN:DAYLIGHT',
|
||||
'DTSTART:19700329T020000',
|
||||
'RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1FR',
|
||||
'TZOFFSETFROM:+0200',
|
||||
'TZOFFSETTO:+0300',
|
||||
'END:DAYLIGHT',
|
||||
'END:VTIMEZONE',
|
||||
'BEGIN:VEVENT',
|
||||
'UID:'.$uid,
|
||||
'DTSTAMP:'.gmdate('Ymd\THis\Z'),
|
||||
'DTSTART;TZID=Asia/Jerusalem:'.$startDateTime->format('Ymd\THis'),
|
||||
'DTEND;TZID=Asia/Jerusalem:'.$endDateTime->format('Ymd\THis'),
|
||||
'SUMMARY:'.$this->escapeIcs($title),
|
||||
'DESCRIPTION:'.$this->escapeIcs($description),
|
||||
'LOCATION:'.$this->escapeIcs($location),
|
||||
'STATUS:CONFIRMED',
|
||||
'SEQUENCE:0',
|
||||
'BEGIN:VALARM',
|
||||
'TRIGGER:-PT1H',
|
||||
'ACTION:DISPLAY',
|
||||
'DESCRIPTION:Reminder',
|
||||
'END:VALARM',
|
||||
'END:VEVENT',
|
||||
'END:VCALENDAR',
|
||||
];
|
||||
|
||||
$calendar = Calendar::create('Libra Law Firm')
|
||||
->productIdentifier('-//Libra Law Firm//Consultation Booking//EN')
|
||||
->event($event);
|
||||
|
||||
return $calendar->get();
|
||||
return implode("\r\n", $ics);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,30 +83,69 @@ class CalendarService
|
||||
*/
|
||||
private function buildDescription(Consultation $consultation, string $locale): string
|
||||
{
|
||||
$lines = [];
|
||||
|
||||
$type = $consultation->consultation_type?->value ?? 'free';
|
||||
|
||||
if ($locale === 'ar') {
|
||||
$description = "استشارة قانونية مع مكتب ليبرا للمحاماة\n\n";
|
||||
$description .= "العميل: {$consultation->user?->full_name}\n";
|
||||
$description .= 'التاريخ: '.Carbon::parse($consultation->booking_date)->translatedFormat('l, d M Y')."\n";
|
||||
$description .= 'الوقت: '.Carbon::parse($consultation->booking_time)->format('g:i A')."\n";
|
||||
|
||||
if ($consultation->consultation_type?->value === 'paid' && $consultation->payment_amount) {
|
||||
$description .= "المبلغ: {$consultation->payment_amount} شيكل\n";
|
||||
$lines[] = 'رقم الحجز: '.$consultation->id;
|
||||
$lines[] = 'نوع الاستشارة: '.($type === 'free' ? 'مجانية' : 'مدفوعة');
|
||||
if ($type === 'paid' && $consultation->payment_amount) {
|
||||
$lines[] = 'المبلغ: '.number_format($consultation->payment_amount, 2).' شيكل';
|
||||
}
|
||||
|
||||
$description .= "\nللتواصل: info@libra.ps";
|
||||
$lines[] = '';
|
||||
$lines[] = 'للاستفسارات:';
|
||||
$lines[] = 'مكتب ليبرا للمحاماة';
|
||||
$lines[] = 'libra.ps';
|
||||
} else {
|
||||
$description = "Legal Consultation with Libra Law Firm\n\n";
|
||||
$description .= "Client: {$consultation->user?->full_name}\n";
|
||||
$description .= 'Date: '.Carbon::parse($consultation->booking_date)->format('l, d M Y')."\n";
|
||||
$description .= 'Time: '.Carbon::parse($consultation->booking_time)->format('g:i A')."\n";
|
||||
|
||||
if ($consultation->consultation_type?->value === 'paid' && $consultation->payment_amount) {
|
||||
$description .= "Amount: {$consultation->payment_amount} ILS\n";
|
||||
$lines[] = 'Booking Reference: '.$consultation->id;
|
||||
$lines[] = 'Consultation Type: '.ucfirst($type);
|
||||
if ($type === 'paid' && $consultation->payment_amount) {
|
||||
$lines[] = 'Amount: '.number_format($consultation->payment_amount, 2).' ILS';
|
||||
}
|
||||
|
||||
$description .= "\nContact: info@libra.ps";
|
||||
$lines[] = '';
|
||||
$lines[] = 'For inquiries:';
|
||||
$lines[] = 'Libra Law Firm';
|
||||
$lines[] = 'libra.ps';
|
||||
}
|
||||
|
||||
return $description;
|
||||
return implode('\n', $lines);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the office location based on locale.
|
||||
*/
|
||||
private function getLocation(string $locale): string
|
||||
{
|
||||
return config('libra.office_address.'.$locale, 'Libra Law Firm');
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape special characters for ICS format.
|
||||
*/
|
||||
private function escapeIcs(string $text): string
|
||||
{
|
||||
// Order matters: backslash must be escaped first
|
||||
return str_replace(
|
||||
['\\', ',', ';'],
|
||||
['\\\\', '\,', '\;'],
|
||||
$text
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a download response for the ICS file.
|
||||
*/
|
||||
public function generateDownloadResponse(Consultation $consultation): Response
|
||||
{
|
||||
$content = $this->generateIcs($consultation);
|
||||
$filename = sprintf(
|
||||
'consultation-%s.ics',
|
||||
$consultation->booking_date->format('Y-m-d')
|
||||
);
|
||||
|
||||
return response($content)
|
||||
->header('Content-Type', 'text/calendar; charset=utf-8')
|
||||
->header('Content-Disposition', 'attachment; filename="'.$filename.'"');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user