mein-sterntours/app/Models/Lead.php
2025-04-01 10:40:14 +02:00

414 lines
13 KiB
PHP

<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use Carbon\Carbon;
use App\Services\Passolution;
use App\Models\Lead as ModelsLead;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;
/**
* Class Lead
*
* @property int $id
* @property int $customer_id
* @property Carbon $request_date
* @property Carbon $travelperiod_start
* @property Carbon $travelperiod_end
* @property int $travelperiod_length
* @property int $travelcountry_id
* @property int $travelagenda_id
* @property string $remarks
* @property int $sf_guard_user_id
* @property bool $is_closed
* @property int $initialcontacttype_id
* @property int $searchengine_id
* @property string $searchengine_keywords
* @property int $status_id
* @property Carbon $next_due_date
* @property int $website_id
* @property int $travelcategory_id
* @property Carbon $created_at
* @property Carbon $updated_at
* @property float $price
* @property int $pax
* @property string $participant_name
* @property string $participant_firstname
* @property Carbon $participant_birthdate
* @property int $participant_salutation_id
* @property Customer $customer
* @property InitialContactType $initial_contact_type
* @property Salutation $salutation
* @property Searchengine $searchengine
* @property SfGuardUser $sf_guard_user
* @property Status $status
* @property TravelAgenda $travel_agenda
* @property TravelCategory $travel_category
* @property TravelCountry $travel_country
* @property Website $website
* @property Collection|Booking[] $bookings
* @property Collection|Inquiry[] $inquiries
* @property Collection|LeadParticipant[] $lead_participants
* @property Collection|Offer[] $offers
* @property Collection|StatusHistory[] $status_histories
* @package App\Models
* @property-read int|null $bookings_count
* @property-read int|null $inquiries_count
* @property-read int|null $lead_participants_count
* @property-read int|null $offers_count
* @property-read int|null $status_histories_count
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead query()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereCustomerId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereInitialcontacttypeId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereIsClosed($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereNextDueDate($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereParticipantBirthdate($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereParticipantFirstname($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereParticipantName($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereParticipantSalutationId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead wherePax($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead wherePrice($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereRemarks($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereRequestDate($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereSearchengineId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereSearchengineKeywords($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereSfGuardUserId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereStatusId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereTravelagendaId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereTravelcategoryId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereTravelcountryId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereTravelperiodEnd($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereTravelperiodLength($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereTravelperiodStart($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereWebsiteId($value)
* @property-read \App\Models\Sym\TravelCountry|null $travel_country_crm
* @property bool|null $is_rebook
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Lead whereIsRebook($value)
* @property-read Collection|\App\Models\LeadFile[] $lead_files
* @property-read int|null $lead_files_count
* @property-read \App\Models\LeadMail|null $lead_mail_last
* @property-read Collection|\App\Models\LeadMail[] $lead_mails
* @property-read int|null $lead_mails_count
* @property-read Collection|\App\Models\LeadMail[] $lead_mails_sent_at
* @property-read int|null $lead_mails_sent_at_count
* @property-read Collection|\App\Models\LeadNotice[] $lead_notices
* @property-read int|null $lead_notices_count
* @mixin \Eloquent
*/
class Lead extends Model
{
protected $connection = 'mysql';
protected $table = 'lead';
protected $casts = [
'customer_id' => 'int',
'travelperiod_length' => 'int',
'travelcountry_id' => 'int',
'travelagenda_id' => 'int',
'sf_guard_user_id' => 'int',
'is_closed' => 'bool',
'is_rebook' => 'bool',
'initialcontacttype_id' => 'int',
'searchengine_id' => 'int',
'status_id' => 'int',
'website_id' => 'int',
'travelcategory_id' => 'int',
'price' => 'float',
'pax' => 'int',
'participant_salutation_id' => 'int'
];
protected $dates = [
'request_date',
'travelperiod_start',
'travelperiod_end',
'next_due_date',
'participant_birthdate'
];
protected $fillable = [
'customer_id',
'request_date',
'travelperiod_start',
'travelperiod_end',
'travelperiod_length',
'travelcountry_id',
'travelagenda_id',
'remarks',
'sf_guard_user_id',
'is_closed',
'is_rebook',
'initialcontacttype_id',
'searchengine_id',
'searchengine_keywords',
'status_id',
'next_due_date',
'website_id',
'travelcategory_id',
'price',
'pax',
'participant_name',
'participant_firstname',
'participant_birthdate',
'participant_salutation_id'
];
protected $passolutionPDFs = [];
public static $lead_mail_dirs = [
11 => ['name' => 'Entwürfe', 'icon'=>'ion-md-create'],
12 => ['name' => 'Papierkorb', 'icon'=>'ion-md-trash'],
];
public function updateNextDueDate($date = false){
if(!$date){
$carbon = Carbon::now();
}else{
$carbon = Carbon::parse($date);
}
$this->next_due_date = $carbon->modify('+ '.$this->status->handling_days.' days')->format("Y-m-d");
$this->save();
}
public function customer()
{
return $this->belongsTo(Customer::class);
}
public function initial_contact_type()
{
return $this->belongsTo(InitialContactType::class, 'initialcontacttype_id');
}
public function salutation()
{
return $this->belongsTo(Salutation::class, 'participant_salutation_id');
}
public function searchengine()
{
return $this->belongsTo(Searchengine::class);
}
public function sf_guard_user()
{
return $this->belongsTo(SfGuardUser::class);
}
public function status()
{
return $this->belongsTo(Status::class);
}
public function travel_agenda()
{
return $this->belongsTo(TravelAgenda::class, 'travelagenda_id');
}
public function travel_category()
{
return $this->belongsTo(TravelCategory::class, 'travelcategory_id');
}
//on crm
public function travel_country_crm()
{
return $this->belongsTo('App\Models\Sym\TravelCountry', 'travelcountry_id', 'id');
}
//on stern other DB
public function travel_country()
{
return $this->belongsTo('App\Models\TravelCountry', 'travelcountry_id', 'crm_id');
}
public function website()
{
return $this->belongsTo(Website::class);
}
public function bookings()
{
return $this->hasMany(Booking::class);
}
public function inquiries()
{
return $this->hasMany(Inquiry::class);
}
public function lead_participants()
{
return $this->hasMany(LeadParticipant::class);
}
public function offers()
{
return $this->hasMany(Offer::class);
}
public function status_histories()
{
return $this->hasMany(StatusHistory::class)->orderByDesc('id')->orderByDesc('date');
}
public function lead_files()
{
//no lead_mail_id
return $this->hasMany(LeadFile::class, 'lead_id')->where('lead_mail_id', null);
}
public function lead_mails()
{
return $this->hasMany(LeadMail::class, 'lead_id', 'id');
}
public function lead_mails_sent_at()
{
return $this->hasMany(LeadMail::class, 'lead_id')->orderBy('sent_at', 'ASC');
}
public function lead_mail_last()
{
return $this->hasOne(LeadMail::class, 'lead_id')->latest();
}
public function lead_notices()
{
return $this->hasMany(LeadNotice::class, 'lead_id')->orderBy('updated_at', 'DESC');
}
public static function getSfGuardUserArray(){
return SfGuardUser::where('is_active', 1)->get()->pluck('fullname', 'id');
}
public static function getTravelCountryArray($emtpy = false){
$TravelCountry = TravelCountry::where('active_backend', 1)->orderBy('name')->get()->pluck('name', 'id');
return $emtpy ? $TravelCountry->prepend('-', 0) : $TravelCountry;
}
public static function getTravelCategoryArray($emtpy = false){
$TravelCategory = TravelCategory::orderBy('name')->get()->pluck('name', 'id');
return $emtpy ? $TravelCategory->prepend('-', 0) : $TravelCategory;
}
public static function getTravelAgendaArray($emtpy = false){
$TravelAgenda = TravelAgenda::orderBy('name')->get()->pluck('name', 'id');
return $emtpy ? $TravelAgenda->prepend('-', 0) : $TravelAgenda;
}
public static function getStatusArray($emtpy = false){
$Status = Status::orderBy('name')->get()->pluck('name', 'id');
return $emtpy ? $Status->prepend('-', 0) : $Status;
}
public function getStatusBadge($booking = null)
{
if($this->status_id && $this->status){
$color = $this->status->color;
$icon = "";
if($this->status_id == 14 && $this->is_rebook){
$color = '#94ae59';
$icon = '<i class="fa fa-check-circle"></i> ';
}
if($this->status_id == 14 && !$this->is_rebook){
$icon = '<i class="fa fa-times-circle"></i> ';
}
if($this->status_id == 15){
$icon = '<i class="fa fa-balance-scale"></i> ';
if($booking && $booking->lawyer_date){
return '<span data-order="'.$this->status_id.'"><span class="badge badge-dark" style="background-color: '.$color.'">'.$icon.$booking->lawyer_date->format('d.m.Y').'</span></span>';
}
}
return '<span data-order="'.$this->status_id.'"><span class="badge badge-dark" style="background-color: '.$color.'">'.$icon.$this->status->name.'</span></span>';
}
return '<span data-order="0">-</span>';
}
public function getTravelCountryDestco($badge = true){
$out = "";
if($this->bookings->count()){
$out .= $badge ? '<span class="badge badge-success">' : '';
foreach ($this->bookings as $booking){
if($booking->travel_country_id && $booking->travel_country) {
$out .= $booking->travel_country->destco;
}
}
$out .= $badge ? '</span>' : '';
return $out;
}
if($this->travel_country){
return $badge ? '<span class="badge badge-secondary">'.$this->travel_country->destco.'</span>' : $this->travel_country->destco;
}
return "-";
}
public function countLeadMailsBy($dir, $subdir=false){
if($dir === 11){
return $this->lead_mails->where('draft', true)->where('dir', '!=', 12)->count();
}
if($subdir){
return $this->lead_mails->where('dir', $dir)->where('subdir', $subdir)->count();
}
return $this->lead_mails->where('dir', $dir)->count();
}
public function getPassolutionPDF($create = false, $resync = false){
$nats = [];
if(count($this->passolutionPDFs)){
return $this->passolutionPDFs;
}
if(!$this->travel_country){
return $this->passolutionPDFs;
}
$destco = $this->travel_country->destco;
//default no travel_nationality
$nats['de'] = 'de';
if($this->lead_participants->count()){
foreach ($this->lead_participants as $participant){
if($participant->travel_nationality){
$nats[$participant->travel_nationality->nat] = $participant->travel_nationality->nat;
}
}
}
if(empty($nats)){
$nats['de'] = 'de';
}
foreach ($nats as $nat){
$data = [
'nat' => $nat,
'destco' => $destco,
];
$passolution = new Passolution($data);
$this->passolutionPDFs[] = $passolution->findOrCreatePDF($create, $resync);
}
return $this->passolutionPDFs;
}
public function resyncPassolutionPDF(){
return $this->getPassolutionPDF(true, true);
}
}