<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class WorkOrder extends Model
{

	use SoftDeletes;
	
	protected $hidden = ['created_at', 'updated_at', 'deleted_at'];
	
	protected $appends = ['recurring_formatted', 'last_created_visit_formatted', 'number', 'billing'];
	
	public function company()
	{
		return $this->belongsTo('App\Company');
	}
	
	public function created_by()
	{
		return $this->belongsTo('App\Employee')->select(['id', 'name'])->withTrashed();
	}
	
	public function updated_by()
	{
		return $this->belongsTo('App\Employee')->select(['id', 'name'])->withTrashed();
	}
	
	public function notes()
	{
		return $this->morphMany('App\Note', 'notable');
	}
	
	public function customer()
	{
		return $this->belongsTo('App\Customer')->select(['id', 'created_at', 'name', 'contacts', 'preferences', 'status', 'about'])->withTrashed();
	}
	
	public function property()
	{
		return $this->belongsTo('App\Property')->withTrashed();
	}
	
	public function work_order_schedule()
	{
		return $this->hasOne('App\WorkOrderSchedule')->withTrashed();
	}
	
	public function work_order_visits()
	{
		return $this->hasMany('App\WorkOrderVisit');
	}
	
	public function invoices()
	{
		return $this->hasMany('App\Invoice');
	}
	
	public function employees()
	{
		return $this->belongsToMany('App\Employee', 'work_order_employees')->select(['company_id', 'name', 'preferences', 'one_signal_push_id', 'receive_push_notification'])->withTrashed();
	}
	
	public function services()
	{
		return $this->belongsToMany('App\Service', 'work_order_services')->withPivot('qty')->withTrashed();
	}
	
	public function products()
	{
		return $this->belongsToMany('App\Product', 'work_order_products')->withPivot('qty')->withTrashed();
	}
	
	public function getRecurringFormattedAttribute()
    {
		return ($this->attributes['recurring']) ? 'Yes' : 'No';
    }
	
	public function getLastCreatedVisitFormattedAttribute()
    {
		if (isset($this->next_robot_visits)) {
			return date('m/d/Y', strtotime($this->next_robot_visits));
		}
        return null;
    }
	
	public function getBillingAttribute()
    {
		if (isset($this->payment_frequency)) {
			if ($this->payment_frequency == 'SERVICE') {
				return 'By Service (Customer will be billed according to the number of visits done)';
			} else if ($this->payment_frequency == 'MONTHLY') {
				return 'Monthly (Only one visit will be billed monthly)';
			}
		}
        return null;
    }
	
	public function getNumberAttribute()
    {
		return '#WO' . $this->attributes['id'];
    }
	
	protected static function boot() {
		
		parent::boot();

		static::deleting(function($work_order) {
			
			$work_order->work_order_schedule()->delete();
			
			$work_order_services = WorkOrderService::where('work_order_id', $work_order->id)->get();
			
			foreach ($work_order_services as $work_order_service) {
				$work_order_service->delete();
			}
			
			$work_order_products = WorkOrderProduct::where('work_order_id', $work_order->id)->get();
			
			foreach ($work_order_products as $work_order_product) {
				$work_order_product->delete();
			}
			
			$work_order_employees = WorkOrderEmployee::where('work_order_id', $work_order->id)->get();
			
			foreach ($work_order_employees as $work_order_employee) {
				$work_order_employee->delete();
			}
			
			$work_order_visits = WorkOrderVisit::where('work_order_id', $work_order->id)
														->where('date', '>=', date('Y-m-d'))
															->get();
			
			foreach ($work_order_visits as $work_order_visit) {
				$work_order_visit->delete();
			}

		});
		
	}

}
