<?php

class AlokasiBOPTNBPPTNBH extends Phalcon\Mvc\Model
{
    /**
     *
     * @var integer
     */
    public $id;
    /**
     *
     * @var integer
     */
    public $perguruan_tinggi_id;
    /**
     *
     * @var integer
     */
    public $tahun;
    /**
     *
     * @var float
     */
    public $total_alokasi;
    /**
     *
     * @var datetime
     */
    public $created_at;
    /**
     *
     * @var datetime
     */
    public $updated_at;
    /**
     *
     * @var datetime
     */
    public $deleted_at;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSchema('jaga');
        $this->setSource('alokasi_boptn_bpptnbh');
    }

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

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return AlokasiBOPTNBPPTNBH[]|AlokasiBOPTNBPPTNBH|\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 AlokasiBOPTNBPPTNBH|\Phalcon\Mvc\Model\ResultInterface
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    public function toArrayDetail()
    {
        $result = $this->toArray();

        $result['nama_perguruan_tinggi'] = '';
        $result['tipe_alokasi'] = '';
        if(!empty($this->perguruan_tinggi_id)) {
            $pt = PerguruanTinggi::findFirst(
                [
                    'id_pt = :id:',
                    "bind" => [
                        'id' => $this->perguruan_tinggi_id
                    ]
                ]
            );

            if(!empty($pt)) {
                $result['nama_perguruan_tinggi'] = $pt->nama;
            }

            $ptnbh = ListPTNBH::findFirst(
                [
                    "perguruan_tinggi_id = :id: and deleted_at is null",
                    "bind" => [
                        'id' => $this->perguruan_tinggi_id
                    ]
                ]
            );

            if(!empty($ptnbh)) {
                $result['tipe_alokasi'] = 'BPPTNBH';
            } else {
                $result['tipe_alokasi'] = 'BOPTN';
            }
        }

        return $result;
    }
}

?>