﻿<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Scopes\OrganisationScope;

class Order extends Model
{
    protected $fillable = [
        'order_id',
        'name',
        'email',
        'card_number',
        'card_exp_month',
        'card_exp_year',
        'plan_name',
        'plan_id',
        'price',
        'price_currency',
        'txn_id',
        'payment_status',
        'payment_type',
        'receipt',
        'user_id',
        'is_refund',
        'organisation_id',
    ];


    protected static function booted()
    {
        static::addGlobalScope(new OrganisationScope);
    
      // Automatically set the organisation_id before creating the model
            static::creating(function ($model) {
                if (auth()->check()) {
                    $model->organisation_id = auth()->user()->organisation_id;
                }
            });
    }
    public static function total_orders()
    {
        return Order::count();
    }

    public static function total_orders_price()
    {
        return Order::sum('price');
    }

    public function total_coupon_used()
    {
        return $this->hasOne('App\Models\UserCoupon', 'order', 'order_id');
    }