<?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\PatientReferringProviderRepository")
 * @ORM\Table(name="patient_referring_provider")
 */

class PatientReferringProviderEntity extends BaseEntity
{
    /**
     * @ORM\Column(name="start_date", type="datetime")
     * @Groups("forAudit")
     */
    protected $startDate;

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

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

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

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

    /**
     * @ORM\ManyToOne(targetEntity="ReferringProviderEntity", inversedBy="patientReferringProvider")
     * @ORM\JoinColumn(name="referring_provider_id", referencedColumnName="id")
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $referringProvider;

    /**
     * @ORM\OneToMany(targetEntity="PatientLetterEntity", mappedBy="patientReferringProvider", cascade={"remove"})
     */
    protected $patientLetters;

    /**
     * @ORM\OneToMany(targetEntity="PatientLetterCcEntity", mappedBy="patientReferringProvider", cascade={"remove"})
     */
    protected $patientLetterCcs;

    /**
     * @ORM\OneToMany(targetEntity="AppointmentNoticeFaxEntity", mappedBy="patientReferringProvider")
     */
    protected $appointmentNoticeFaxes;


    public function __construct(){
        $this->patientLetters = new ArrayCollection();
        $this->appointmentNoticeFaxes = new ArrayCollection();
    }

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



    /*--------------------------------------------------------------------------------------------------------*/
    /*					Use Defined Setters and Getters														  */
    /*--------------------------------------------------------------------------------------------------------*/
    /**
     * Get startDateAsString
     *
     * @return string
     */
    public function getStartDateAsString() {

        $dttst = new DateTimeToStringTransformer();
        return !empty($this->startDate) ? date('m/d/Y', strtotime($dttst->transform($this->startDate))) : '';
    }
    /**
     * Get endDateAsString
     *
     * @return string
     */
    public function getEndDateAsString() {

        $dttst = new DateTimeToStringTransformer();
        return !empty($this->endDate) ? date('m/d/Y', strtotime($dttst->transform($this->endDate))) : '';
    }
    
    /**
     * Get faxNumberForletter
     * 
     * @return string
     */
    public function getFaxNumberForLetter(){
        $referringProvider = $this->getReferringProvider();
        $receiverFaxNum = $referringProvider->getFaxNo();
        $receiverFaxNumTwo = $referringProvider->getFaxNoTwo();
        $patient = $this->getPatient();
        if($receiverFaxNum == '' || $receiverFaxNum == null){
            foreach ($patient->getPatientReferringHealthFacilities() as $prhf){
                foreach ($referringProvider->getReferringHealthFacilityProviders() as $rhfp){
                    if($prhf->getReferringHealthFacility()->getId() == $rhfp->getReferringHealthFacility()->getId()){
                        $receiverFaxNum = $prhf->getReferringHealthFacility()->getFaxNo();
                    }
                }
            }
        }
        if($receiverFaxNumTwo == '' || $receiverFaxNumTwo == null){
            foreach ($patient->getPatientReferringHealthFacilities() as $prhf){
                foreach ($referringProvider->getReferringHealthFacilityProviders() as $rhfp){
                    if($prhf->getReferringHealthFacility()->getId() == $rhfp->getReferringHealthFacility()->getId()){
                        $receiverFaxNumTwo = $prhf->getReferringHealthFacility()->getFaxNoTwo();
                    }
                }
            }
        }
        return !empty($receiverFaxNum) ? $receiverFaxNum : $receiverFaxNumTwo;
    }
    
    /*--------------------------------------------------------------------------------------------------------*/
    /*					Setters and Getters																	  */
    /*--------------------------------------------------------------------------------------------------------*/

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

        return $this;
    }

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

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

        return $this;
    }

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


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

    /**
     * Set createdBy
     *
     * @param string $createdBy
     *
     * @return PatientReferringProviderEntity
     */
    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 PatientReferringProviderEntity
     */
    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 PatientReferringProviderEntity
     */
    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 PatientReferringProviderEntity
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

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


    /**
     * Set patient
     *
     * @param \RxConnexionBundle\Entity\PatientEntity $patient
     *
     * @return PatientReferringProviderEntity
     */
    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 referringProvider
     *
     * @param \RxConnexionBundle\Entity\ReferringProviderEntity $referringProvider
     *
     * @return PatientReferringProviderEntity
     */
    public function setReferringProvider(\RxConnexionBundle\Entity\ReferringProviderEntity $referringProvider = null)
    {
        $this->referringProvider = $referringProvider;

        return $this;
    }

    /**
     * Get referringProvider
     *
     * @return \RxConnexionBundle\Entity\ReferringProviderEntity
     */
    public function getReferringProvider()
    {
        return $this->referringProvider;
    }

    /**
     * Add patientLetter
     *
     * @param \RxConnexionBundle\Entity\PatientLetterEntity $patientLetter
     *
     * @return PatientReferringProviderEntity
     */
    public function addPatientLetter(\RxConnexionBundle\Entity\PatientLetterEntity $patientLetter)
    {
        $this->patientLetters[] = $patientLetter;

        return $this;
    }

    /**
     * Remove patientLetter
     *
     * @param \RxConnexionBundle\Entity\PatientLetterEntity $patientLetter
     */
    public function removePatientLetter(\RxConnexionBundle\Entity\PatientLetterEntity $patientLetter)
    {
        $this->patientLetters->removeElement($patientLetter);
    }

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

    /**
     * Add appointmentNoticeFax
     *
     * @param \RxConnexionBundle\Entity\AppointmentNoticeFaxEntity $appointmentNoticeFax
     *
     * @return PatientReferringProviderEntity
     */
    public function addAppoinmentNoticeFax(\RxConnexionBundle\Entity\AppointmentNoticeFaxEntity $appointmentNoticeFax)
    {
        $this->appointmentNoticeFaxes[] = $appointmentNoticeFax;

        return $this;
    }

    /**
     * Remove appointmentNoticeFax
     *
     * @param \RxConnexionBundle\Entity\AppointmentNoticeFaxEntity $appointmentNoticeFax
     */
    public function removeAppoinmentNoticeFax(\RxConnexionBundle\Entity\AppointmentNoticeFaxEntity $appointmentNoticeFax)
    {
        $this->appointmentNoticeFaxes->removeElement($appointmentNoticeFax);
    }

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

    /**
     * Add appointmentNoticeFax
     *
     * @param \RxConnexionBundle\Entity\AppointmentNoticeFaxEntity $appointmentNoticeFax
     *
     * @return PatientReferringProviderEntity
     */
    public function addAppointmentNoticeFax(\RxConnexionBundle\Entity\AppointmentNoticeFaxEntity $appointmentNoticeFax)
    {
        $this->appointmentNoticeFaxes[] = $appointmentNoticeFax;

        return $this;
    }

    /**
     * Remove appointmentNoticeFax
     *
     * @param \RxConnexionBundle\Entity\AppointmentNoticeFaxEntity $appointmentNoticeFax
     */
    public function removeAppointmentNoticeFax(\RxConnexionBundle\Entity\AppointmentNoticeFaxEntity $appointmentNoticeFax)
    {
        $this->appointmentNoticeFaxes->removeElement($appointmentNoticeFax);
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Add patientLetterCc
     *
     * @param \RxConnexionBundle\Entity\PatientLetterCcEntity $patientLetterCc
     *
     * @return PatientReferringProviderEntity
     */
    public function addPatientLetterCc(\RxConnexionBundle\Entity\PatientLetterCcEntity $patientLetterCc)
    {
        $this->patientLetterCcs[] = $patientLetterCc;

        return $this;
    }

    /**
     * Remove patientLetterCc
     *
     * @param \RxConnexionBundle\Entity\PatientLetterCcEntity $patientLetterCc
     */
    public function removePatientLetterCc(\RxConnexionBundle\Entity\PatientLetterCcEntity $patientLetterCc)
    {
        $this->patientLetterCcs->removeElement($patientLetterCc);
    }

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