mein-sterntours/app/Models/TravelUserBookingFewo.php
2019-05-08 14:05:35 +02:00

578 lines
19 KiB
PHP

<?php
/**
* Created by Reliese Model.
* Date: Thu, 21 Mar 2019 13:40:13 +0100.
*/
namespace App\Models;
use Carbon;
use Illuminate\Database\Eloquent\Model;
use Storage;
use App\Services\Util;
/**
* Class TravelUserBookingFewo
*
* @property int $id
* @property int $travel_user_id
* @property int $fewo_lodging_id
* @property string $invoice_number
* @property int $persons
* @property int $adults
* @property int $children
* @property \Carbon\Carbon $booking_date
* @property \Carbon\Carbon $from_date
* @property \Carbon\Carbon $to_date
* @property string $daily_prices
* @property float $price_travel
* @property float $price_deposit
* @property float $price_service
* @property float $price_total
* @property int $travel_booking_fewo_channel_id
* @property string $notice
* @property bool $is_calendar_fewo_direct
* @property bool $is_calendar_hrs
* @property bool $is_calendar_stern_tours
* @property int $status
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property string $deleted_at
* @property \App\Models\FewoLodging $fewo_lodging
* @property \App\Models\TravelBookingFewoChannel $travel_booking_fewo_channel
* @property \App\Models\TravelUser $travel_user
* @package App\Models
* @method static bool|null forceDelete()
* @method static \Illuminate\Database\Query\Builder|\App\Models\TravelUserBookingFewo onlyTrashed()
* @method static bool|null restore()
* @method static \Illuminate\Database\Query\Builder|\App\Models\TravelUserBookingFewo withTrashed()
* @method static \Illuminate\Database\Query\Builder|\App\Models\TravelUserBookingFewo withoutTrashed()
* @mixin \Eloquent
*/
class TravelUserBookingFewo extends Model
{
use \Illuminate\Database\Eloquent\SoftDeletes;
protected static $statues = [
0 => '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_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',
];
protected $dates = [
'booking_date',
'from_date',
'to_date'
];
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_hrs',
'is_calendar_stern_tours',
'status',
'status_text'
];
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 getStatuesName(){
if(isset(self::$statues[$this->status])){
return self::$statues[$this->status];
}
return "";
}
public function getCheckedBadgeCalendar(){
$back = "";
$back .= $this->is_calendar_fewo_direct ? ' <span class="badge badge-pill badge-success"><i class="far fa-check"></i></span>' : ' <span class="badge badge-pill badge-danger"><i class="far fa-times"></i></span>';
$back .= $this->is_calendar_hrs ? ' <span class="badge badge-pill badge-success"><i class="far fa-check"></i></span>' : ' <span class="badge badge-pill badge-danger"><i class="far fa-times"></i></span>';
$back .= $this->is_calendar_stern_tours ? ' <span class="badge badge-pill badge-success"><i class="far fa-check"></i></span>' : ' <span class="badge badge-pill badge-danger"><i class="far fa-times"></i></span>';
return $back;
}
public function getPersonsAttribute($value)
{
$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 .= '<option value="'.$key.'" '.$attr.'>'.$option.'</option>\n';
}
return $ret;
}
public function getBookingDateAttribute($value)
{
if(!$value){ return ""; }
return Carbon::parse($value)->format(Util::formatDateDB());
}
public function setBookingDateAttribute( $value ) {
$this->attributes['booking_date'] = isset($value) ? (new Carbon($value))->format('Y-m-d') : NULL;
}
//from_date
public function getFromDateAttribute($value)
{
if(!$value){ return ""; }
return Carbon::parse($value)->format(Util::formatDateDB());
}
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($value)
{
if(!$value){ return ""; }
return Carbon::parse($value)->format(Util::formatDateDB());
}
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)
{
$value = Util::_format_number($value);
$this->attributes['price_travel'] = floatval(str_replace(',', '.', $value));
}
public function getPriceTravelAttribute($value)
{
if(!$value){ return 0; }
return number_format(($this->attributes['price_travel']), 2, ',', '.');
}
public function getPriceTravelRaw()
{
return isset($this->attributes['price_travel']) ? $this->attributes['price_travel'] : 0;
}
//price_extra
public function setPriceExtraAttribute($value)
{
$value = Util::_format_number($value);
$this->attributes['price_extra'] = floatval(str_replace(',', '.', $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)
{
$value = Util::_format_number($value);
$this->attributes['price_balance'] = floatval(str_replace(',', '.', $value));
}
public function getPriceBalanceAttribute($value)
{
if(!$value){ return 0; }
return number_format(($this->attributes['price_balance']), 2, ',', '.');
}
public function getPriceBalanceRaw()
{
return isset($this->attributes['price_balance']) ? $this->attributes['price_balance'] : 0;
}
//price_travel_total
public function setPriceTravelTotalAttribute($value)
{
$value = Util::_format_number($value);
$this->attributes['price_travel_total'] = floatval(str_replace(',', '.', $value));
}
public function getPriceTravelTotalAttribute($value)
{
if(!$value){
$this->attributes['price_travel_total'] = $this->getPriceTravelRaw() + $this->getPriceBalanceRaw() + $this->getPriceExtraRaw();
}
return number_format(($this->attributes['price_travel_total']), 2, ',', '.');
}
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)
{
$value = Util::_format_number($value);
$this->attributes['price_deposit'] = floatval(str_replace(',', '.', $value));
}
public function getPriceDepositAttribute($value)
{
if(!$value){ return 0; }
return number_format(($this->attributes['price_deposit']), 2, ',', '.');
}
public function getPriceDepositRaw()
{
return isset($this->attributes['price_deposit']) ? $this->attributes['price_deposit'] : 0;
}
//price_service
public function setPriceServiceAttribute($value)
{
$value = Util::_format_number($value);
$this->attributes['price_service'] = floatval(str_replace(',', '.', $value));
}
public function getPriceServiceAttribute($value)
{
if(!$value){ return 0; }
return number_format(($this->attributes['price_service']), 2, ',', '.');
}
public function getPriceServiceRaw()
{
return isset($this->attributes['price_service']) ? $this->attributes['price_service'] : 0;
}
//price_total
public function setPriceTotalAttribute($value)
{
$value = Util::_format_number($value);
$this->attributes['price_total'] = floatval(str_replace(',', '.', $value));
}
public function getPriceTotalAttribute($value)
{
if(!$value){ return 0; }
return number_format(($this->attributes['price_total']), 2, ',', '.');
}
public function getPriceTotalRaw()
{
return isset($this->attributes['price_total']) ? $this->attributes['price_total'] : 0;
}
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 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')->getAdapter()->getPathPrefix();
return $path.$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;
}
//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')->getAdapter()->getPathPrefix();
return $path.$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;
}
//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->zip_code." ".$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->zip_code)." ".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;
}else{
$salutation = __('Dear Mrs')." ".$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)";
}
}