'Anfrage von STERN TOURS', 1 => 'Angebot versandt', 2 => 'Rückfrage Agentur', 3 => 'Rückfrage Kunden', 5 => 'Nachfrage Angebot', 7 => 'gebucht', 8 => 'Absage', 9 => 'Option', 10 => 'Angebot erstellen', 11 => 'Storno durch VA', 12 => 'Storno durch Kunde', 13 => 'Reiseanmeldung verschickt', ]; protected $connection = 'mysql_stern'; protected $table = 'travel_user_booking_fewos'; protected $casts = [ 'travel_user_id' => 'int', 'fewo_lodging_id' => 'int', 'fewo_reservation_id' => 'int', 'persons' => 'int', 'adults' => 'int', 'children' => 'int', 'daily_prices' => 'array', //! 'price_travel' => 'float', 'price_deposit' => 'float', 'price_service' => 'float', 'price_balance' => 'float', 'price_extra' => 'float', 'price_total' => 'float', 'travel_booking_fewo_channel_id' => 'int', 'is_calendar_fewo_direct' => 'bool', 'is_calendar_traum_fewo' => 'bool', 'is_calendar_hrs' => 'bool', 'is_calendar_stern_tours' => 'bool', 'status' => 'int', 'send_user_mail' => 'array', 'send_service_mail' => 'array', 'send_info_mail' => 'array', 'send_employee_mail' => 'array', 'booking_date' => 'datetime', 'from_date' => 'datetime', 'to_date' => 'datetime', 'deleted_at' => 'datetime', ]; protected $fillable = [ 'travel_user_id', 'fewo_lodging_id', 'fewo_reservation_id', 'invoice_number', 'adults', 'children', 'persons', 'booking_date', 'from_date', 'to_date', 'daily_prices', 'price_travel', 'price_balance', 'price_extra', 'price_travel_total', 'price_deposit', 'price_service', 'price_total', 'travel_booking_fewo_channel_id', 'notice', 'is_calendar_fewo_direct', 'is_calendar_traum_fewo', 'is_calendar_hrs', 'is_calendar_stern_tours', 'status', 'status_text' ]; protected static function boot() { parent::boot(); static::deleting(function($model) { foreach ($model->customer_fewo_mails as $relation) { $relation->delete(); } }); } public static $customer_mail_dirs = [ 11 => ['name' => 'Entwürfe', 'icon'=>'ion-md-create'], 12 => ['name' => 'Papierkorb', 'icon'=>'ion-md-trash'], ]; public function fewo_lodging() { return $this->belongsTo(\App\Models\FewoLodging::class); } public function fewo_reservation() { return $this->belongsTo(\App\Models\FewoReservation::class, 'fewo_reservation_id'); } public function travel_booking_fewo_channel() { return $this->belongsTo(\App\Models\TravelBookingFewoChannel::class); } public function travel_user() { return $this->belongsTo(\App\Models\TravelUser::class); } public function customer() { return $this->belongsTo(\App\Models\TravelUser::class, 'travel_user_id'); } public function booking_files() { return $this->hasMany(TravelUserBookingFile::class, 'travel_user_booking_fewo_id'); } public function customer_fewo_mails() { return $this->hasMany(CustomerFewoMail::class, 'travel_user_booking_fewo_id', 'id'); } public function customer_fewo_mails_sent_at() { return $this->hasMany(CustomerFewoMail::class, 'travel_user_booking_fewo_id')->orderBy('sent_at', 'ASC'); } public function customer_fewo_mail_last() { return $this->hasOne(CustomerFewoMail::class, 'travel_user_booking_fewo_id')->latest(); } public function booking_fewo_notices() { return $this->hasMany(TravelUserBookingFewoNotice::class, 'travel_user_booking_fewo_id')->orderBy('updated_at', 'DESC'); } public function getStatuesName(){ if(isset(self::$statues[$this->status])){ return self::$statues[$this->status]; } return ""; } public function getCheckedBadgeCalendar(){ $back = ""; $back .= $this->is_calendar_fewo_direct ? ' ' : ' '; $back .= $this->is_calendar_traum_fewo ? ' ' : ' '; $back .= $this->is_calendar_hrs ? ' ' : ' '; $back .= $this->is_calendar_stern_tours ? ' ' : ' '; return $back; } public function getPersonsAttribute() { $this->attributes['persons'] = $this->adults + $this->children; return $this->attributes['persons']; } public function setPersonsAttribute($value) { $this->attributes['persons'] = $this->adults + $this->children; } public function getAdultsAttribute() { return isset($this->attributes['adults']) ? $this->attributes['adults'] : 0; } public function setAdultsAttribute( $value ) { $this->attributes['persons'] = $this->adults + $this->children; $this->attributes['adults'] = $value; } public function getChildrenAttribute() { return isset($this->attributes['children']) ? $this->attributes['children'] : 0; } public function setChildrenAttribute( $value ) { $this->attributes['persons'] = $this->adults + $this->children; $this->attributes['children'] = $value; } public static function getStatuesOptions($setKey = false){ $options = self::$statues; $ret = ''; foreach ($options as $key => $option){ $attr = ($key == $setKey) ? 'selected="selected"' : ''; $ret .= '\n'; } return $ret; } public function getBookingDateAttribute() { return isset($this->attributes['booking_date']) ? Carbon::parse($this->attributes['booking_date'])->format(Util::formatDateDB()) : NULL; } public function setBookingDateAttribute( $value ) { $this->attributes['booking_date'] = isset($value) ? (new Carbon($value))->format('Y-m-d') : NULL; } //from_date public function getFromDateAttribute() { return isset($this->attributes['from_date']) ? Carbon::parse($this->attributes['from_date'])->format(Util::formatDateDB()) : NULL; } public function setFromDateAttribute( $value ) { $this->attributes['from_date'] = isset($value) ? (new Carbon($value))->format('Y-m-d') : NULL; } public function getFromDateRaw() { return isset($this->attributes['from_date']) ? $this->attributes['from_date'] : ""; } //to_date public function getToDateAttribute() { return isset($this->attributes['to_date']) ? Carbon::parse($this->attributes['to_date'])->format(Util::formatDateDB()) : NULL; } public function setToDateAttribute( $value ) { $this->attributes['to_date'] = isset($value) ? (new Carbon($value))->format('Y-m-d') : NULL; } public function getToDateRaw() { return isset($this->attributes['to_date']) ? $this->attributes['to_date'] : ""; } //price_travel public function setPriceTravelAttribute($value) { $this->attributes['price_travel'] = Util::_clean_float($value); } public function getPriceTravelAttribute($value) { return isset($this->attributes['price_travel']) ? number_format(($this->attributes['price_travel']), 2, ',', '.') : 0; } public function getPriceTravelRaw() { return isset($this->attributes['price_travel']) ? $this->attributes['price_travel'] : 0; } //price_extra public function setPriceExtraAttribute($value) { $this->attributes['price_extra'] = Util::_clean_float($value); } public function getPriceExtraAttribute($value) { if(!$value){ return 0; } return number_format(($this->attributes['price_extra']), 2, ',', '.'); } public function getPriceExtraRaw() { return isset($this->attributes['price_extra']) ? $this->attributes['price_extra'] : 0; } //price_balance public function setPriceBalanceAttribute($value) { $this->attributes['price_balance'] = Util::_clean_float($value); } public function getPriceBalanceAttribute($value) { return isset($this->attributes['price_balance']) ? number_format(($this->attributes['price_balance']), 2, ',', '.') : 0; } public function getPriceBalanceRaw() { return isset($this->attributes['price_balance']) ? $this->attributes['price_balance'] : 0; } //price_travel_total public function setPriceTravelTotalAttribute($value) { $this->attributes['price_travel_total'] = Util::_clean_float($value); } public function getPriceTravelTotalAttribute($value) { if(!isset($this->attributes['price_travel_total']) || $this->attributes['price_travel_total'] == 0){ $this->attributes['price_travel_total'] = $this->getPriceTravelRaw() + $this->getPriceBalanceRaw() + $this->getPriceExtraRaw(); } return isset($this->attributes['price_travel_total']) ? number_format(($this->attributes['price_travel_total']), 2, ',', '.') : 0; } public function getPriceTravelTotalRaw() { if(!isset($this->attributes['price_travel_total']) || $this->attributes['price_travel_total'] == 0){ $this->attributes['price_travel_total'] = $this->getPriceTravelRaw() + $this->getPriceBalanceRaw() + $this->getPriceExtraRaw(); } return isset($this->attributes['price_travel_total']) ? $this->attributes['price_travel_total'] : 0; } //price_deposit public function setPriceDepositAttribute($value) { $this->attributes['price_deposit'] = Util::_clean_float($value); } public function getPriceDepositAttribute($value) { return isset($this->attributes['price_deposit']) ? number_format(($this->attributes['price_deposit']), 2, ',', '.') : 0; } public function getPriceDepositRaw() { return isset($this->attributes['price_deposit']) ? $this->attributes['price_deposit'] : 0; } //price_service public function setPriceServiceAttribute($value) { $this->attributes['price_service'] = Util::_clean_float($value); } public function getPriceServiceAttribute($value) { return isset($this->attributes['price_service']) ? number_format(($this->attributes['price_service']), 2, ',', '.') : 0; } public function getPriceServiceRaw() { return isset($this->attributes['price_service']) ? $this->attributes['price_service'] : 0; } //price_total public function setPriceTotalAttribute($value) { $this->attributes['price_total'] = Util::_clean_float($value); } public function getPriceTotalAttribute($value) { return isset($this->attributes['price_total']) ? number_format(($this->attributes['price_total']), 2, ',', '.') : 0; } public function getPriceTotalRaw() { return isset($this->attributes['price_total']) ? $this->attributes['price_total'] : 0; } public function calculate_price(){ $this->attributes['price_travel_total'] = $this->getPriceTravelRaw() + $this->getPriceBalanceRaw() + $this->getPriceExtraRaw(); $this->attributes['price_total'] = $this->getPriceTravelTotalRaw() + $this->getPriceServiceRaw() + $this->getPriceDepositRaw(); } public function getPriceTravelTotalFirstPay(){ if($this->attributes['price_travel'] == 0){ return 0; } $first_pay = ($this->getPriceTravelTotalRaw() + $this->getPriceServiceRaw()) / 2; return number_format(($first_pay), 2, ',', '.'); } public function getPriceTravelTotalSecondPay(){ if($this->attributes['price_travel'] == 0){ return 0; } $total_pay = ($this->getPriceTravelTotalRaw() + $this->getPriceServiceRaw()); $first_pay = $total_pay/2; return number_format(($total_pay - $first_pay + $this->getPriceDepositRaw()), 2, ',', '.'); } public function getPriceTravelTotalPay(){ if($this->attributes['price_travel'] == 0){ return 0; } $total_pay = ($this->getPriceTravelTotalRaw() + $this->getPriceServiceRaw()); return number_format(($total_pay + $this->getPriceDepositRaw()), 2, ',', '.'); } public function getDepartureInDays() { if(isset($this->attributes['from_date'])){ return Carbon::now()->diffInDays(Carbon::parse($this->attributes['from_date']), false); } return 0; } public function isFromDateAfterDays($days = 30){ if(($this->getDepartureInDays() - $days) > 0){ return true; } return false; } public function getLastChangeAt(){ if(!isset($this->attributes['last_change_at']) || !$this->attributes['last_change_at']){ return ""; } return Carbon::parse($this->attributes['last_change_at'])->format("H:i d.m.Y"); } public function getBookingDateYear(){ return Carbon::parse($this->booking_date)->format('Y'); } //get Invoice Name / Paths / ... public function getInvoiceFileName(){ if($this->invoice_number) { return Util::sanitize($this->invoice_number).".pdf"; } return false; } public function getInvoicePath(){ $dir = $this->getBookingDateYear()."/"; if(!Storage::disk('fewo_invoices')->exists( $dir )){ Storage::disk('fewo_invoices')->makeDirectory($dir); //creates directory } $path = Storage::disk('fewo_invoices')->path(''); return $path.$dir; } public function getInvoiceDir(){ $dir = $this->getBookingDateYear()."/"; if(!Storage::disk('fewo_invoices')->exists( $dir )){ Storage::disk('fewo_invoices')->makeDirectory($dir); //creates directory } return $dir; } public function isInvoice(){ if($this->invoice_number){ $dir = $this->getBookingDateYear()."/"; $filename = $this->getInvoiceFileName(); if(Storage::disk('fewo_invoices')->exists( $dir.$filename )){ return true; } } return false; } public function getInvoicePathFile(){ $dir = $this->getBookingDateYear()."/"; $filename = $this->getInvoiceFileName(); if(Storage::disk('fewo_invoices')->exists( $dir.$filename )){ return Storage::disk('fewo_invoices')->path($dir.$filename); } return false; } public function getInvoiceUrlFile(){ $dir = $this->getBookingDateYear()."/"; $filename = $this->getInvoiceFileName(); if(Storage::disk('fewo_invoices')->exists( $dir.$filename )){ return Storage::disk('fewo_invoices')->url($dir.$filename); } return false; } public function getInvoiceLastModified(){ $dir = $this->getBookingDateYear()."/"; $filename = $this->getInvoiceFileName(); if(Storage::disk('fewo_invoices')->exists( $dir.$filename )){ return Carbon::createFromTimestamp(Storage::disk('fewo_invoices')->lastModified($dir.$filename))->format("H:i d.m.Y"); } return false; } public function isChangeLowerInvoiceCreate(){ $dir = $this->getBookingDateYear()."/"; $filename = $this->getInvoiceFileName(); if(Storage::disk('fewo_invoices')->exists( $dir.$filename )){ return Carbon::createFromTimestamp(Storage::disk('fewo_invoices')->lastModified($dir.$filename))->gt($this->last_change_at); } return false; } //get TravelInfos Name / Paths / ... public function getTravelInfoFileName(){ if($this->invoice_number) { return "Anreiseinfo-".Util::sanitize($this->invoice_number).".pdf"; } return false; } public function getTravelInfoPath(){ $dir = $this->getBookingDateYear()."/"; if(!Storage::disk('fewo_infos')->exists( $dir )){ Storage::disk('fewo_infos')->makeDirectory($dir); //creates directory } $path = Storage::disk('fewo_infos')->path(''); return $path.$dir; } public function getTravelInfoDir(){ $dir = $this->getBookingDateYear()."/"; if(!Storage::disk('fewo_infos')->exists( $dir )){ Storage::disk('fewo_infos')->makeDirectory($dir); //creates directory } return $dir; } public function isTravelInfo(){ if($this->invoice_number){ $dir = $this->getBookingDateYear()."/"; $filename = $this->getTravelInfoFileName(); if(Storage::disk('fewo_infos')->exists( $dir.$filename )){ return true; } } return false; } public function getTravelInfoPathFile(){ $dir = $this->getBookingDateYear()."/"; $filename = $this->getTravelInfoFileName(); if(Storage::disk('fewo_infos')->exists( $dir.$filename )){ return Storage::disk('fewo_infos')->path($dir.$filename); } return false; } public function getTravelInfoUrlFile(){ $dir = $this->getBookingDateYear()."/"; $filename = $this->getTravelInfoFileName(); if(Storage::disk('fewo_infos')->exists( $dir.$filename )){ return Storage::disk('fewo_infos')->url($dir.$filename); } return false; } public function getTravelInfoLastModified(){ $dir = $this->getBookingDateYear()."/"; $filename = $this->getTravelInfoFileName(); if(Storage::disk('fewo_infos')->exists( $dir.$filename )){ return Carbon::createFromTimestamp(Storage::disk('fewo_infos')->lastModified($dir.$filename))->format("H:i d.m.Y"); } return false; } public function isChangeLowerTravelInfoCreate(){ $dir = $this->getBookingDateYear()."/"; $filename = $this->getTravelInfoFileName(); if(Storage::disk('fewo_invoices')->exists( $dir.$filename )){ return Carbon::createFromTimestamp(Storage::disk('fewo_infos')->lastModified($dir.$filename))->gt($this->last_change_at); } return false; } //get strings public function getBookingUserAddress(){ if($this->travel_user){ $company = $this->travel_user->company ? $this->travel_user->company ."\n" : ""; $title = $this->travel_user->title ? $this->travel_user->title."\n" : ""; $nationality = $this->travel_user->travel_nationality_id ? "\n".$this->travel_user->travel_nationality->name : ""; return $company.$title.$this->travel_user->first_name." ".$this->travel_user->last_name."\n".$this->travel_user->street."\n".$this->travel_user->zipcode." ".$this->travel_user->city.$nationality; } return ""; } public function getNameAddressLocation($sep = "\n"){ if($this->fewo_lodging){ $name = trim($this->fewo_lodging->single_name).$sep; $address = trim($this->fewo_lodging->adress1); $address .= $this->fewo_lodging->adress2 ? " ".$this->fewo_lodging->adress2.$sep : ", "; $city = trim($this->fewo_lodging->zipcode)." ".trim($this->fewo_lodging->city); return $name.$address.$city; } return ""; } public function getUserSalutation(){ $salutation = __('Dear customer'); if($this->travel_user){ if($this->travel_user->salutation_id == 1){ $salutation = __('Dear Sir')." ".$this->travel_user->last_name; } if($this->travel_user->salutation_id == 2){ $salutation = __('Dear Mrs')." ".$this->travel_user->last_name; } if($this->travel_user->salutation_id == 4){ $salutation = __('Dear customer')." ".$this->travel_user->last_name; } } return $salutation; } public function getServiceMailSubject(){ if($this->fewo_lodging){ return "Neue Anmietung ".$this->fewo_lodging->name." mit Anreise ".$this->from_date; } return "Neue Anmietung Anreise ".$this->from_date; } public function getServiceMailContent(){ return $this->adults." + ".$this->children." (Erwachsene + Kinder)\n". $this->from_date." - ".$this->to_date." ".$this->travel_user->first_name." ".$this->travel_user->last_name; } public function getEmployeeMailSubject(){ if($this->fewo_lodging){ return "CRM Fewo Buchung: ".$this->fewo_lodging->name." | Anreise ".$this->from_date; } return "CRM Fewo Buchung | Anreise ".$this->from_date; } public function getEmployeeMailContent(){ return ($this->travel_user_id ? "Kunde: ".route('travel_user_detail', [$this->travel_user_id])."\n" : ""). "Buchung: ".route('travel_user_booking_fewo_detail', [$this->id])."\n". $this->travel_user->first_name." ".$this->travel_user->last_name." | ".$this->from_date." - ".$this->to_date." | ".$this->adults." + ".$this->children." (Erwachsene + Kinder)"; } public function isSendUserMail(){ if($this->send_user_mail != NULL){ return true; } return false; } public function isSendInfoMail(){ if($this->send_info_mail != NULL){ return true; } return false; } public function isSendServiceMail(){ if($this->send_service_mail != NULL){ return true; } return false; } public function countCustomerMailsBy($dir, $subdir=false){ if($dir === 11){ return $this->customer_fewo_mails->where('draft', true)->where('dir', '!=', 12)->count(); } if($subdir){ return $this->customer_fewo_mails->where('dir', $dir)->where('subdir', $subdir)->count(); } return $this->customer_fewo_mails->where('dir', $dir)->count(); } }