mein-sterntours/dev/hotfix-2026-04-18/BookingImport.php

172 lines
No EOL
8.1 KiB
PHP

<?php
namespace App\Services;
use App\Models\Lead;
use App\Models\Booking;
use App\Models\Customer;
use App\Models\Arrangement;
use App\Models\Participant;
use App\Models\TravelBooking;
use App\Models\BookingServiceItem;
use App\Repositories\DraftRepository;
class BookingImport
{
public static function importFrom(TravelBooking $travel_booking){
// ---- createCustomer
$data = [
'salutation_id' => $travel_booking->salutation_id,
'name' => $travel_booking->last_name,
'firstname' => $travel_booking->first_name,
'street' => $travel_booking->street,
'zip' => $travel_booking->zipcode,
'city' => $travel_booking->city,
'country_id' => $travel_booking->country_id,
'phone' => $travel_booking->phone,
'phonemobile' => $travel_booking->mobile,
'email' => $travel_booking->email
];
$customer = Customer::create($data);
// ---- createLead
$data = [
'customer_id' => $customer->id,
'request_date' => $travel_booking->created,
'travelperiod_start' => $travel_booking->selected_start_date,
'travelperiod_end' => $travel_booking->selected_end_date,
'remarks' => $travel_booking->comments,
'sf_guard_user_id' => 15,
'is_closed' => true,
'initialcontacttype_id' => 14,
'status_id' => 7,
'website_id' => 1,
];
$lead = Lead::create($data);
$lead->updateNextDueDate();
$comfort = false;
if(isset($travel_booking->drafts['comfort']) && $travel_booking->drafts['comfort']){
$comfort = true;
}
$data = [
'booking_date' => $travel_booking->created->format('Y-m-d'),
'customer_id' => $customer->id,
'lead_id' => $lead->id,
'new_drafts' => $travel_booking->drafts === null ? 0 : 1,
'sf_guard_user_id' => 15,
'branch_id' => 4,
'pax' => $travel_booking->selected_adults,
'title' => isset($travel_booking->selected_travel['travel_title']) ? $travel_booking->selected_travel['travel_title'] : "",
'comfort' => $comfort,
'start_date' => $travel_booking->selected_start_date->format('Y-m-d'),
'end_date' => $travel_booking->selected_end_date->format('Y-m-d'),
'website_id' => 1,
'travel_number' => isset($travel_booking->selected_travel['travel_number']) ? $travel_booking->selected_travel['travel_number'] : null,
/*'participant_name' => isset($travel_booking->participants[0]['last_name']) ? $travel_booking->participants[0]['last_name'] : null,
'participant_firstname' => isset($travel_booking->participants[0]['first_name']) ? $travel_booking->participants[0]['first_name'] : null,
'participant_birthdate' => isset($travel_booking->participants[0]['birthday']) ? date( "Y-m-d", strtotime($travel_booking->participants[0]['birthday'])) : null,
'participant_salutation_id' => isset($travel_booking->participants[0]['gender']) ? $travel_booking->participants[0]['gender'] : null,
'nationality_id' => isset($travel_booking->participants[0]['nationality']) ? $travel_booking->participants[0]['nationality'] : null,*/
'participant_name' => null,
'participant_firstname' => null,
'participant_birthdate' => null,
'participant_salutation_id' => null,
'nationality_id' => null,
'price' => $travel_booking->price,
'price_total' => $travel_booking->price_total,
'deposit_total' => $travel_booking->deposit_total,
'final_payment' => $travel_booking->final_payment,
'final_payment_date' => $travel_booking->final_payment_date->format('Y-m-d'),
'travel_country_id' => isset($travel_booking->selected_travel['travel_country_id'][0]) ? $travel_booking->selected_travel['travel_country_id'][0] : null,
'travel_category_id' => isset($travel_booking->selected_travel['travel_category_id']) ? $travel_booking->selected_travel['travel_category_id'] : null,
'travelagenda_id' => isset($travel_booking->selected_travel['travelagenda_id']) ? $travel_booking->selected_travel['travelagenda_id'] : null,
'travel_company_id' => isset($travel_booking->selected_travel['travel_company_id']) ? $travel_booking->selected_travel['travel_company_id'] : 4,
'insurance_offer' => $travel_booking->insurance_offer,
];
//createBooking
$booking = Booking::create($data);
//createTraveler
if($travel_booking->participants){
foreach ($travel_booking->participants as $key => $participant){
Participant::create([
'booking_id' => $booking->id,
'participant_name' => $participant['last_name'],
'participant_firstname' => $participant['first_name'],
'participant_birthdate' => date( "Y-m-d", strtotime($participant['birthday'])),
'participant_salutation_id' => $participant['gender'],
'participant_child' => $participant['child'],
'nationality_id' =>$participant['nationality'],
]);
}
}
//createServiceItem //service_items
if($travel_booking->service_items){
foreach ($travel_booking->service_items as $key => $service_item){
BookingServiceItem::create([
'booking_id' => $booking->id,
'travel_company_id' => $service_item['travel_company_id'],
'service_price' => $service_item['service_price'],
'service_price_refund' => 0,
'commission' => $service_item['commission'],
'travel_date' => $service_item['travel_date'],
'name' => $service_item['name'],
'is_commission_locked' => 0,
]);
}
}
//createServiceItem //insurances
if($travel_booking->insurances){
foreach ($travel_booking->insurances as $service_item){
BookingServiceItem::create([
'booking_id' => $booking->id,
'travel_company_id' => $service_item['travel_company_id'],
'service_price' => $service_item['price'],
'service_price_refund' => 0,
'commission' => $service_item['commission'],
'travel_date' => $service_item['travel_date'],
'name' => $service_item['name'],
'is_commission_locked' => 0,
]);
}
}
//createArrangement
if($travel_booking->arrangements) {
foreach ($travel_booking->arrangements as $key => $arrangement){
Arrangement::create([
'state' => isset($arrangement['state']) ? $arrangement['state'] : null,
'begin' => isset($arrangement['end']) ? $arrangement['end'] : null,
'end' => isset($arrangement['end']) ? $arrangement['end'] : null,
'type_s' => isset($arrangement['type_s']) ? $arrangement['type_s'] : null,
'data_s' => isset($arrangement['data_s']) ? $arrangement['data_s'] : null,
'view_position' => isset($arrangement['view_position']) ? $arrangement['view_position'] : null,
'booking_id' => $booking->id,
'type_id' => $arrangement['type_id'],
'in_pdf' => isset($arrangement['in_pdf']) ? $arrangement['in_pdf'] : null,
]);
}
}
//createDrafts
if($travel_booking->drafts) {
$draftRepo = new DraftRepository($booking);
$draftRepo->create_drafts_from_booking($booking->id, $travel_booking->drafts);
}
$travel_booking->crm_booking_id = $booking->id;
$travel_booking->save();
return $booking;
}
}