<?php

class PajakPenagihan extends Phalcon\Mvc\Model
{
    /**
     *
     * @var integer
     */
    public $id;
    /**
     *
     * @var integer
     */
    public $instansi_id;
    /**
     *
     * @var integer
     */
    public $tahun;
    /**
     *
     * @var integer
     */
    public $bulan;
    /**
     *
     * @var double
     */
    public $tunggakan_pajak;
    /**
     *
     * @var double
     */
    public $capaian_penagihan_pajak;
    /**
     *
     * @var integer
     */
    public $created_by;
    /**
     *
     * @var datetime
     */
    public $created_at;
    /**
     *
     * @var integer
     */
    public $updated_by;
    /**
     *
     * @var datetime
     */
    public $updated_at;
    /**
     *
     * @var integer
     */
    public $deleted_by;
    /**
     *
     * @var datetime
     */
    public $deleted_at;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSchema('jaga');
        $this->setSource('pajak_penagihan');
        $this->hasMany(
            'id',
            PajakPenagihanDetail::class,
            'pajak_penagihan_id',
            ['alias' => 'Details']
        );
    }

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

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

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

        $result['nama_instansi'] = '';
        if(!empty($this->instansi_id)) {
            $instansi = Instansi::findFirst($this->instansi_id);
            
            if(!empty($instansi)) {
                $result['nama_instansi'] = $instansi->nama;
            }
        }

        $result['created_by_name'] = '';
        if(!empty($this->created_by)) {
            $createdBy = UmUser::findFirst(
                [
                    'um_id = :id:',
                    "bind" => [
                        "id" => $this->created_by
                    ]
                ]
            );

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

        $result['updated_by_name'] = '';
        if(!empty($this->updated_by)) {
            $updatedBy = UmUser::findFirst(
                [
                    'um_id = :id:',
                    "bind" => [
                        "id" => $this->updated_by
                    ]
                ]
            );

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

        $tempBulan = $result['bulan'];
        $result['bulan'] = DateHelper::convertMonthDigittoString($tempBulan);

        $result['total_attachments'] = AttachmentAsetPajak::count(
            [
                "tipe_submodul = :tipe_submodul: and data_id = :data_id: and deleted_at is null",
                "bind" => [
                    "tipe_submodul" => AttachmentAsetPajak::SUB_MODUL_PAJAK_PENAGIHAN,
                    "data_id" => $this->id
                ]
            ]
        );

        return $result;
    }

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

        $result['nama_instansi'] = '';
        if(!empty($this->instansi_id)) {
            $instansi = Instansi::findFirst($this->instansi_id);
            
            if(!empty($instansi)) {
                $result['nama_instansi'] = $instansi->nama;
            }
        }

        $result['jenis_pajak'] = '';
        if(!empty($this->id_jenis_pajak)) {
            $jenisPajak = MasterJenisPajak::findFirst($this->id_jenis_pajak);

            if(!empty($jenisPajak)) {
                $result['jenis_pajak'] = $jenisPajak->jenis_pajak;
            }
        }

        $result['created_by_name'] = '';
        if(!empty($this->created_by)) {
            $createdBy = UmUser::findFirst(
                [
                    'um_id = :id:',
                    "bind" => [
                        "id" => $this->created_by
                    ]
                ]
            );

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

        $result['updated_by_name'] = '';
        if(!empty($this->updated_by)) {
            $updatedBy = UmUser::findFirst(
                [
                    'um_id = :id:',
                    "bind" => [
                        "id" => $this->updated_by
                    ]
                ]
            );

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

        $tempBulan = $result['bulan'];
        $result['bulan'] = DateHelper::convertMonthDigittoString($tempBulan);

        $result['attachments'] = [];
        $mapAttachment = AttachmentAsetPajak::find(
            [
                "tipe_submodul = :tipe_submodul: and data_id = :data_id: and deleted_at is null",
                "bind" => [
                    "tipe_submodul" => AttachmentAsetPajak::SUB_MODUL_PAJAK_PENAGIHAN,
                    "data_id" => $this->id
                ]
            ]
        );

        if(!empty($mapAttachment)) {
            $arrIdAttachment = [0];
            foreach ($mapAttachment as $item ) {
                $arrIdAttachment[] = $item->attachment_id;
            }

            $result['attachments'] = Attachments::find(
                [
                    "id in ({list_id:array}) and deleted_at is null",
                    "bind" => [
                        "list_id" => $arrIdAttachment
                    ]
                ]
            )
            ->toArray();
        }

        return $result;
    }
}

?>