HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/immoginis.komma.pro/app/Komma/Properties/PropertyService.php
<?php

/**
 * Short description for the file.
 *
 * @author      Tim Van Samang <timvansamang@komma.pro>
 * @copyright   (c) 2012-2015, Komma Mediadesign
 */

namespace Komma\Properties;


use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Komma\Properties\Models\Property;

class PropertyService
{

    public $citySets = [
        1 => [3950],
        2 => [3990],
        3 => [3900, 3910],
        4 => [3930],
        5 => [3940, 3941],
        6 => [3530],
    ];


    protected $fields = ['city', 'goal', 'available', 'min_price', 'max_price'];

    public function getProperty($id)
    {
        return Property::where('omnicasa_id', '!=', 0)
            ->where('id', '=', $id)
            ->with('routes')->get();
    }

    public function getPropertyByOmnicasaId($omnicasa_id) {
        return Property::where('omnicasa_id', '=', $omnicasa_id)
            ->with('routes')
            ->first();
    }

    public function getProjectPropertyByOmnicasaId($omnicasa_id) {
        return Property::where('omnicasa_id', '=', $omnicasa_id)
            ->where('filter_type', '=', 'project')
            ->with('routes')
            ->first();
    }

    public function getProperties($id = 0)
    {
        //Base Query
        //Properties that are active
        $properties = Property::where('active', '=', 1)
            ->where('omnicasa_id', '!=', 0);

        //Get one specific property
        if($id != 0)
        {
            $properties = $properties->where('id', '=', $id);
        }


        //City
//        if(\Input::has('city') && \Input::get('city') != '')
//        {
//            $properties = $properties->where('city', '=', \Input::get('city'));
//        }
//        elseif(null !== \Input::Segment(2))
//        {
//            $properties = $properties->where(function ($query)
//            {
//                $query->where('city', '=', \Input::Segment(2));
//                $cityParts = explode('-', \Input::Segment(2));
//                $query->orWhere('city', 'like', $cityParts[0] . '%');
//            });
//        }

        // If a second segments is here , then it's a city index page
        if( \Input::Segment(2) !== null)
        {
            $properties = $properties->where(function ($query)
            {
                $query->where('city', '=', \Input::Segment(2));
                $cityParts = explode('-', \Input::Segment(2));
                $query->orWhere('city', 'like', $cityParts[0] . '%');
            });
        }

        // CitySet
        if(\Input::has('citySet') && \Input::get('citySet') != '' && \Input::get('citySet') != 0)
        {
            $citySetId = \Input::get('citySet');

            // If we have defined the set load it
            if(isset($this->citySets[$citySetId])) $postals = $this->citySets[$citySetId];

            // Else it will be the postal itself
            else $postals = [$citySetId];

            $properties = $properties->whereIn('postal_code', $postals);
        }

        //Min price
        if(\Input::has('min_price') && \Input::get('min_price') != '')
        {
            $properties = $properties->where('price', '>=', \Input::get('min_price'));
        }

        //Max price
        if(\Input::has('max_price') && \Input::get('max_price') != '' && \Input::get('max_price') != '0')
        {
            $properties = $properties->where('price', '<=', \Input::get('max_price'));
        }
        //Goals type  // set to rent when done
        if(\Input::has('goal') && \Input::get('goal') != '0')
        {
            $properties = $properties->where('goal', '=', \Input::get('goal'));
        }
        $properties = $properties->where('goal', '!=','reference');


        //Availability
        if(\Input::has('available') && \Input::get('available') != '0')
        {
            $properties = $properties->where('status', '=', \Input::get('available'));
        }

        $properties = $properties->where(function ($query)
        {
            foreach ($this->getTypeFilters() as $typeFilterKey => $typeFilterName)
            {
                if(\Input::has($typeFilterKey) && \Input::get($typeFilterKey) == 1)
                {
                    $query->orWhere('filter_type', '=', $typeFilterKey);
                }
            }
        });


        //Sort
        $sort = 'price';
        $order = 'asc';
        if(\Input::get('order') == 'desc') $order = 'desc';
        if(\Input::get('sort') == 'added_date') $sort = 'created_at';
        $properties = $properties->orderBy('omnicasa_id', 'desc');

        $properties = $properties->with('routes')->get();

        //temp
        return $properties;

    }

    public function getProjectProperties($projectId)
    {
        $properties = Property::where('active', '=', 1)
            ->where('project_id', '=', $projectId)
            ->orderBy('omnicasa_id', 'asc')
            ->orderBy('omnicasa_id', 'asc')
            ->with('routes')
            ->get();

        return $properties;
    }

    public function getFormInfo()
    {
        $formInfo = [];
        $formInfo['filter_types'] = $this->getTypeFilters();

        return $formInfo;
    }

    public function updateFilterQuery()
    {

        \Session::forget('result');

        foreach ($this->fields as $field)
        {
            \Session::put('result.' . $field, \Input::get($field));
        }

        $filters = [];
        foreach ($this->getTypeFilters() as $typeFilterKey => $typeFilterName)
        {
            if(\Input::get($typeFilterKey) == 1)
            {
                $filters[] = $typeFilterKey;
            }
        }

        \Session::put('result.filters', $filters);
    }

    public function getTypeFilters()
    {
        return [
            'house'      => 'Woning',
            //'new'        => 'Nieuwbouw',
            'project'    => 'Project',
            'apartment'  => 'Appartement',
            'commercial' => 'Commercieel',
            'ground'     => 'Grond'

        ];
    }

    public function getStatusDescription($status)
    {
        switch ($status)
        {
            case 1:
                $statusName = "Nieuw";
                break;
            case 2:
                $statusName = "Optie genomen";
                break;
            case 3:
                $statusName = "Verkocht";
                break;
            case 4:
                $statusName = "Verhuurd";
                break;
            case 5:
                $statusName = "Nieuwe voorwaarden";
                break;
            case 6:
                $statusName = "Place";
                break;
            case 7:
                $statusName = "Open";
                break;
            case 8:
                $statusName = "Toegekend";
                break;
            case 9:
                $statusName = "Nieuwe prijs";
                break;
            case 10:
                $statusName = "Compromis";
                break;
            case 11:
                $statusName = "Lijfrente";
                break;
            case 12:
                $statusName = "Verbintenis aanvaard";
                break;
            case 0:
            default:
                $statusName = "";
                break;
        }

        return $statusName;
    }

    public function getCities($goal = 'sale')
    {
        $cities = Property::where('active', '=', 1)
            ->whereNotNull('goal')
            ->where('goal', '!=', '')
            ->where('status', '!=', 3)
            ->orderBy('city')
            ->groupBy('city')
            ->groupBy('goal')
            ->select('active', 'city', 'goal', 'status')
            ->get();

        if($cities->count() == 0) return null;

        $cityArray = [
            'sale' => ['' => 'Alle gemeentes'],
            'rent' => ['' => 'Alle gemeentes']
        ];

        foreach ($cities as $city)
        {
            if( ! isset($city->goal)) continue;
            $cityArray[$city->goal][strtolower($city->city)] = strtolower($city->city);
        }

        return $cityArray;
    }

    public function getCitySets() {

        $usedPostalCodes = [];

        $baseSet = [
            (object)[
                'id' => 0,
                'label' => trans('citySets.all')
            ]
        ];

        $orderedSet = [];

        foreach ($this->citySets as $key => $postals) {

            // Append all used postals to array
            foreach ($postals as $postal) $usedPostalCodes[] = $postal;

            // Add city set to array
            $orderedSet[Str::slug(trans('citySets.' . $key))] = (object)[
                'id' => $key,
                'label' => trans('citySets.' . $key)
            ];
        }

        $uniqueCityProperties = Property::where('active', '=', 1)
            ->whereNotNull('goal')
            ->where('goal', '!=', '')
            ->where('status', '!=', 3)
            ->orderBy('city')
            ->groupBy('city')
            ->groupBy('goal')
            ->select('active', 'city', 'goal', 'status', 'postal_code')
            ->whereNotIn('postal_code', $usedPostalCodes)
            ->get();

        foreach ($uniqueCityProperties as $uniqueCityProperty) {

            // Add city set to array
            $orderedSet[Str::slug($uniqueCityProperty->city)] = (object)[
                'id' => $uniqueCityProperty->postal_code,
                'label' => ucwords(strtolower($uniqueCityProperty->city))
            ];

        }

        // Order by key name
        ksort($orderedSet);

        $citysets = array_merge($baseSet, $orderedSet);

        return $citysets;

    }

    public function getReferences($id = 0)
    {
        //Base Query
        //Properties that are active
        $properties = Property::where('active', '=', 1)
            ->where('omnicasa_id', '!=', 0)
            ->where('goal', 'reference');


        $properties = $properties->where(function ($query)
        {
            foreach ($this->getTypeFilters() as $typeFilterKey => $typeFilterName)
            {
                if(\Input::has($typeFilterKey) && \Input::get($typeFilterKey) == 1)
                {
                    $query->orWhere('filter_type', '=', $typeFilterKey);
                }
            }
        });

        if(\Input::get('order') == 'desc') $order = 'desc';
        if(\Input::get('sort') == 'added_date') $sort = 'created_at';
        $properties = $properties->orderBy('omnicasa_id', 'desc');

        $properties = $properties->with('routes')->get();

        //temp
        return $properties;

    }

    public function decodeProperties($properties)
    {

        foreach ($properties as $key => $property)
        {
            $decoded = json_decode($property->raw_json);

            if(!isset($decoded)) {
                Log::error('PropertyService: Property ' . $property->id . ' has not raw json/incomplete json.');
                unset($properties[$key]);
                continue;
            }

            $properties[$key]->route = '';
            if($property->routes->count() > 0) $properties[$key]->route = $property->routes->first()->route;
            $properties[$key]->decoded_info = $decoded;
            $properties[$key]->status_description = $this->getStatusDescription($property->status);
            $properties[$key]->city = ucfirst(strtolower($property->city));
            $properties[$key]->format_price = number_format($property->price, 0, ',', '.');

            //Sold
            if($properties[$key]->status == 3)
            {
                $properties[$key]->decoded_info->PublishPrice = false;
            }
            //Rented
            if($properties[$key]->status == 4)
            {
                $properties[$key]->decoded_info->PublishPrice = false;
            }

            /*$diff = '';
            if ($decoded->CreatedDate != '') {
                $diff .= 'sinds ';

                $date = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $property->decoded_info->CreatedDate);
                $now = \Carbon\Carbon::now();
                $dagen = $date->diffInDays($now);
                if ($dagen > 100) {
                    $months = round($dagen / 30.5);
                    if ($months > 24) {
                        $years = round($dagen / 365);
                        $diff .= $years . ' jaar';
                    } else {
                        $diff .= $months . ' maanden';
                    }
                } else {
                    if ($dagen == 1) {
                        $diff .= $dagen . ' dag';
                    }
                    $diff .= $dagen . ' dagen';
                }
            }


            $properties[$key]->different = $diff;*/
        }

        return $properties;
    }

}