<?php


namespace ModulesGarden\ProxmoxAddon\App\Helper;


use Illuminate\Database\Capsule\Manager as DB;
use ModulesGarden\ProxmoxAddon\App\Models\VmModel;

trait VmidHelper
{
    public function vmidExistInWhmcs($vmid)
    {
        //vps
        $cfv   = 'tblcustomfieldsvalues';
        $cfn   = 'tblcustomfields';
        $h     = 'tblhosting';
        $query = DB::table($cfv)
            ->rightJoin($cfn, "{$cfn}.id", "=", "{$cfv}.fieldid")
            ->leftJoin($h, "{$h}.id", "=", "{$cfv}.relid")
            ->where("{$cfn}.fieldname", "like", "vmid%")
            ->where("{$cfv}.value", $vmid)
            ->whereIn("{$h}.domainstatus", ['Active', "Suspended"]);
        if ($query->count())
        {
            return true;
        }
        //cloud
        try
        {
            $query = VmModel::where("vmid", $vmid);
            return $query->count() > 0;
        }
        catch (\Exception $ex)
        { // table does not exist
        }
        return false;
    }
}