<?php

class PancekKppbcUser extends \Phalcon\Mvc\Model
{

    /**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var integer
     */
    public $kppbc_id;

    /**
     *
     * @var integer
     */
    public $um_id;

    /**
     *
     * @var string
     */
    public $created_at;

    /**
     *
     * @var string
     */
    public $updated_at;

    /**
     *
     * @var string
     */
    public $deleted_at;

    /**
     *
     * @var string
     */
    public $user_type;

    const USER_TYPE_VERIFIATOR = 'VERIFIKATOR';
    const USER_TYPE_ASSIGNOR = 'ASSIGNOR';

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSchema("jaga");
        $this->setSource("pancek_kppbc_user");
        $this->belongsTo(
        "um_id",
        \UmUser::class,
        "um_id",
            [
                'reusable' => true, // cache related data
                'alias'    => 'umUser',
            ]
        );
        $this->belongsTo(
            "kppbc_id",
            \PancekKppbc::class,
            "id",
            [
                'reusable' => true, // cache related data
                'alias'    => 'kppbc',
            ]
        );
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'pancek_kppbc_user';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return PancekKppbcUser[]|PancekKppbcUser|\Phalcon\Mvc\Model\ResultSetInterface
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return PancekKppbcUser|\Phalcon\Mvc\Model\ResultInterface
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    public static function findVerifikatorOnlyBy($umId) {
        return self::findBy($umId, self::USER_TYPE_VERIFIATOR);
    }

    public static function findAssignorOnlyBy($umId) {
        return self::findBy($umId, self::USER_TYPE_ASSIGNOR);
    }

    public static function findBy($umId, $userType) {
        return self::find([
            'conditions' => 'um_id = :um_id: AND deleted_at IS NULL AND user_type = :user_type:',
            'bind' => [
                'um_id' => $umId,
                'user_type' => $userType,
            ]
        ]);
    }



}
