<?php

namespace RxConnexionBundle\Entity;

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

/**
 * @ORM\Entity(repositoryClass="RxConnexionBundle\Repository\PatientFileRepository")
 * @ORM\Table(name="patient_file")
 */

class PatientFileEntity extends BaseEntity
{

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

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

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

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

    /**
     * @ORM\ManyToOne(targetEntity="PatientEntity", inversedBy="files")
     * @ORM\JoinColumn(name="patient_id", referencedColumnName="id")
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $patient;

    /**
     * @ORM\ManyToOne(targetEntity="FileTypeEntity", inversedBy="patientFiles")
     * @ORM\JoinColumn(name="file_type_id", referencedColumnName="id")
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $fileType;

    /**
     * @ORM\ManyToOne(targetEntity="RxConnexionBundle\Entity\FaxingOptionEntity", inversedBy="patientFiles")
     * @ORM\JoinColumn(name="faxing_option_id", referencedColumnName="id")
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $faxingOption;

    /**
     * @ORM\ManyToOne(targetEntity="RxConnexionBundle\Entity\FaxTranEntity", inversedBy="patientFiles")
     * @ORM\JoinColumn(name="fax_tran_id", referencedColumnName="id")
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $faxTran;

    /**
     * @ORM\OneToMany(targetEntity="PatientBillingEntity", mappedBy="patientFile")
     * @Groups("forAudit")
     */
    protected $patientFileBillings;

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

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

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

    /*--------------------------------------------------------------------------------------------------------*/
    /*					Use Defined Setters and Getters														  */
    /*--------------------------------------------------------------------------------------------------------*/

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

        return 'uploads/patient/files';
    }

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

        return 'uploads/patient/billing';
    }

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

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

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

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

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

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

    /**
     * Set description
     *
     * @param string $description
     *
     * @return PatientFileEntity
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

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

    /**
     * Set parsedDescription
     *
     * @param string $parsedDescription
     *
     * @return PatientFileEntity
     */
    public function setParsedDescription($parsedDescription)
    {
        $this->parsedDescription = $parsedDescription;

        return $this;
    }

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

    /**
     * Set note
     *
     * @param string $note
     *
     * @return PatientFileEntity
     */
    public function setNote($note)
    {
        $this->note = $note;

        return $this;
    }

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

    /**
     * Set patient
     *
     * @param \RxConnexionBundle\Entity\PatientEntity $patient
     *
     * @return PatientFileEntity
     */
    public function setPatient(\RxConnexionBundle\Entity\PatientEntity $patient = null)
    {
        $this->patient = $patient;

        return $this;
    }

    /**
     * Get patient
     *
     * @return \RxConnexionBundle\Entity\PatientEntity
     */
    public function getPatient()
    {
        return $this->patient;
    }

    /**
     * Set fileType
     *
     * @param \RxConnexionBundle\Entity\FileTypeEntity $fileType
     *
     * @return PatientFileEntity
     */
    public function setFileType(\RxConnexionBundle\Entity\FileTypeEntity $fileType = null)
    {
        $this->fileType = $fileType;

        return $this;
    }

    /**
     * Get fileType
     *
     * @return \RxConnexionBundle\Entity\FileTypeEntity
     */
    public function getFileType()
    {
        return $this->fileType;
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set createdBy
     *
     * @param string $createdBy
     *
     * @return PatientFileEntity
     */
    public function setCreatedBy($createdBy)
    {
        $this->createdBy = $createdBy;

        return $this;
    }

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

    /**
     * Set createdAt
     *
     * @param \DateTime $createdAt
     *
     * @return PatientFileEntity
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;

        return $this;
    }

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

    /**
     * Set updatedBy
     *
     * @param string $updatedBy
     *
     * @return PatientFileEntity
     */
    public function setUpdatedBy($updatedBy)
    {
        $this->updatedBy = $updatedBy;

        return $this;
    }

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

    /**
     * Set updatedAt
     *
     * @param \DateTime $updatedAt
     *
     * @return PatientFileEntity
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

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

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

        return $this;
    }

    /**
     * Get isDeleted
     *
     * @return boolean
     */
    public function getIsDeleted()
    {
        return $this->isDeleted;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->patientBilling = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add patientBilling
     *
     * @param \RxConnexionBundle\Entity\PatientBillingEntity $patientBilling
     *
     * @return PatientFileEntity
     */
    public function addPatientBilling(\RxConnexionBundle\Entity\PatientBillingEntity $patientBilling)
    {
        $this->patientBilling[] = $patientBilling;

        return $this;
    }

    /**
     * Remove patientBilling
     *
     * @param \RxConnexionBundle\Entity\PatientBillingEntity $patientBilling
     */
    public function removePatientBilling(\RxConnexionBundle\Entity\PatientBillingEntity $patientBilling)
    {
        $this->patientBilling->removeElement($patientBilling);
    }

    /**
     * Get patientBilling
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getPatientBilling()
    {
        return $this->patientBilling;
    }

    /**
     * Add patientFileBilling
     *
     * @param \RxConnexionBundle\Entity\PatientBillingEntity $patientFileBilling
     *
     * @return PatientFileEntity
     */
    public function addPatientFileBilling(\RxConnexionBundle\Entity\PatientBillingEntity $patientFileBilling)
    {
        $this->patientFileBillings[] = $patientFileBilling;

        return $this;
    }

    /**
     * Remove patientFileBilling
     *
     * @param \RxConnexionBundle\Entity\PatientBillingEntity $patientFileBilling
     */
    public function removePatientFileBilling(\RxConnexionBundle\Entity\PatientBillingEntity $patientFileBilling)
    {
        $this->patientFileBillings->removeElement($patientFileBilling);
    }

    /**
     * Get patientFileBillings
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getPatientFileBillings()
    {
        return $this->patientFileBillings;
    }

    /**
     * Set faxingOption
     *
     * @param \RxConnexionBundle\Entity\FaxingOptionEntity $faxingOption
     *
     * @return PatientFileEntity
     */
    public function setFaxingOption(\RxConnexionBundle\Entity\FaxingOptionEntity $faxingOption = null)
    {
        $this->faxingOption = $faxingOption;

        return $this;
    }

    /**
     * Get faxingOption
     *
     * @return \RxConnexionBundle\Entity\FaxingOptionEntity
     */
    public function getFaxingOption()
    {
        return $this->faxingOption;
    }

    /**
     * Set faxTran
     *
     * @param \RxConnexionBundle\Entity\FaxTranEntity $faxTran
     *
     * @return PatientFileEntity
     */
    public function setFaxTran(\RxConnexionBundle\Entity\FaxTranEntity $faxTran = null)
    {
        $this->faxTran = $faxTran;

        return $this;
    }

    /**
     * Get faxTran
     *
     * @return \RxConnexionBundle\Entity\FaxTranEntity
     */
    public function getFaxTran()
    {
        return $this->faxTran;
    }
}
