<?php

namespace RxConnexionBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use RxConnexionBundle\Entity\Base\BaseEntity;
use JMS\Serializer\Annotation\Groups;
use JMS\Serializer\Annotation\MaxDepth;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer;

/**
 * @ORM\Entity(repositoryClass="RxConnexionBundle\Repository\AccountingEncodeRepository")
 * @ORM\Table(name="accounting_encode")
 */

class AccountingEncodeEntity extends BaseEntity
{
    const TMP_UPLOAD_DIR = __DIR__ . '/../../../web/uploads/tmp-file';

    /**
     * @ORM\Column(name="is_deleted", type="boolean")
     */
    protected $isDeleted;

    /**
     * @ORM\Column(name="encode_date", type="datetime", nullable=true)
     * @Groups("forAudit")
     */
    protected $encodeDate;

    /**
     * @ORM\Column(name="check_date", type="datetime", nullable=true)
     * @Groups("forAudit")
     */
    protected $checkDate;

    /**
     * @ORM\Column(name="file_desc", type="string", nullable=true)
     * @Groups("forAudit")
     */
    protected $fileDesc;

    /**
     * @ORM\Column(name="parsed_file_desc", type="string", nullable=true)
     * @Groups("forAudit")
     */
    protected $parsedFileDesc;

    /**
     * @ORM\ManyToOne(targetEntity="RxConnexionBundle\Entity\AccountingFileEntity", inversedBy="accountingEncodes")
     * @ORM\JoinColumn(name="accounting_file_id", referencedColumnName="id")
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $accountingFile;


    public function __construct($data=null)
    {
        if(!is_null($data)) {
            $this->id = $data->getId();
        }
    }

    /**
     * Set createdAt
     *
     * @ORM\PrePersist
     */
    public function setCreatedAtValue()
    {
        parent::setCreatedAtValue();
        $this->isDeleted = 0;
    }

    /**
     * Remove the file from the disk
     *
     * @ORM\PreRemove
     */
    public function removeFile() {

        $file = $this->getUploadRootDir() . '/' . $this->parsedFileDesc;
        if(!empty($this->parsedFileDesc) && file_exists($file)) unlink($file);
    }

    /*--------------------------------------------------------------------------------------------------------*/
    /*					User Defined Setters and Getters													  */
    /*--------------------------------------------------------------------------------------------------------*/

    /**
     * Get uploadDir
     *
     * @return string
     */
    public function getUploadDir() {

        return 'uploads/accounting/files';
    }

    /**
     * Get uploadRootDir
     *
     * @return string
     */
    public function getUploadRootDir() {

        return __DIR__ . '/../../../web/' . $this->getUploadDir();
    }

    /**
     * Get fileDescWithLink
     *
     * @return string
     */
    public function getFileDescWithLink() {
        $path = '/' . $this->getUploadDir() . '/' . $this->parsedFileDesc ;
        return "<a href='$path' target='_blank'>$this->fileDesc</a>";
    }

    /**
     * get fileWebPath
     *
     * @return string
     */
    public function getFileWebPath() {

        $parsedDesc = $this->parsedFileDesc;
        $file = $this->getUploadRootDir() . '/' . $parsedDesc;

        if(!empty($parsedDesc) && file_exists($file)) {
            return '/' . $this->getUploadDir() . '/' . $parsedDesc;
        } else {
            return '';
        }
    }


    /**
     * Get encodeDateAsString
     *
     * @return string
     */
    public function getEncodeDateAsString() {

        $dttst = new DateTimeToStringTransformer();
        return !empty($this->encodeDate) ? date('m/d/Y', strtotime($dttst->transform($this->encodeDate))) : '';
    }

    /**
     * Get encodeDateAsString
     *
     * @return string
     */
    public function getCheckDateAsString() {

        $dttst = new DateTimeToStringTransformer();
        return !empty($this->checkDate) ? date('m/d/Y', strtotime($dttst->transform($this->checkDate))) : '';
    }


    /*--------------------------------------------------------------------------------------------------------*/
    /*					Setters and Getters																	  */
    /*--------------------------------------------------------------------------------------------------------*/



    /**
     * Set isDeleted
     *
     * @param boolean $isDeleted
     *
     * @return AccountingEncodeEntity
     */
    public function setIsDeleted($isDeleted)
    {
        $this->isDeleted = $isDeleted;

        return $this;
    }

    /**
     * Get isDeleted
     *
     * @return boolean
     */
    public function getIsDeleted()
    {
        return $this->isDeleted;
    }

    /**
     * Set encodeDate
     *
     * @param \DateTime $encodeDate
     *
     * @return AccountingEncodeEntity
     */
    public function setEncodeDate($encodeDate)
    {
        $this->encodeDate = empty($encodeDate) ? NULL : new \DateTime($encodeDate);

        return $this;
    }

    /**
     * Get encodeDate
     *
     * @return \DateTime
     */
    public function getEncodeDate()
    {
        return $this->encodeDate;
    }

    /**
     * Set checkDate
     *
     * @param \DateTime $checkDate
     *
     * @return AccountingEncodeEntity
     */
    public function setCheckDate($checkDate)
    {
        $this->checkDate = empty($checkDate) ? NULL : new \DateTime($checkDate);

        return $this;
    }

    /**
     * Get checkDate
     *
     * @return \DateTime
     */
    public function getCheckDate()
    {
        return $this->checkDate;
    }

    /**
     * Set fileDesc
     *
     * @param string $fileDesc
     *
     * @return AccountingEncodeEntity
     */
    public function setFileDesc($fileDesc)
    {
        $this->fileDesc = $fileDesc;

        return $this;
    }

    /**
     * Get fileDesc
     *
     * @return string
     */
    public function getFileDesc()
    {
        return $this->fileDesc;
    }

    /**
     * Set parsedFileDesc
     *
     * @param string $parsedFileDesc
     *
     * @return AccountingEncodeEntity
     */
    public function setParsedFileDesc($parsedFileDesc)
    {
        $this->parsedFileDesc = $parsedFileDesc;

        return $this;
    }

    /**
     * Get parsedFileDesc
     *
     * @return string
     */
    public function getParsedFileDesc()
    {
        return $this->parsedFileDesc;
    }

    /**
     * Set accountingFile
     *
     * @param \RxConnexionBundle\Entity\AccountingFileEntity $accountingFile
     *
     * @return AccountingEncodeEntity
     */
    public function setAccountingFile(\RxConnexionBundle\Entity\AccountingFileEntity $accountingFile = null)
    {
        $this->accountingFile = $accountingFile;

        return $this;
    }

    /**
     * Get accountingFile
     *
     * @return \RxConnexionBundle\Entity\AccountingFileEntity
     */
    public function getAccountingFile()
    {
        return $this->accountingFile;
    }
}
