<?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;

/**
 * @ORM\Entity(repositoryClass="RxConnexionBundle\Repository\CompanyRepository")
 * @ORM\Table(name="company", indexes={@ORM\Index(name="IDX_SEARCH_000", columns={"description"})})
 */
 
class CompanyEntity extends BaseEntity
{

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

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

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

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

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

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

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

    /**
     * @ORM\ManyToOne(targetEntity="AccountEntity", inversedBy="companies")
     * @ORM\JoinColumn(name="account_id", referencedColumnName="id")
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $account;

    /**
     * @ORM\OneToMany(targetEntity="BranchEntity", mappedBy="company")
     */
    protected $branches;

    /**
     * @ORM\OneToMany(targetEntity="AccountUserCompanyEntity", mappedBy="company")
     */
    protected $accountUserCompanies;

    /**
     * @ORM\OneToMany(targetEntity="HealthFacilityEntity", mappedBy="company")
     */
    protected $healthFacilities;

    /**
     * @ORM\OneToMany(targetEntity="ItemEntity", mappedBy="itemCompany")
     */
    protected $items;

    /**
     * @ORM\OneToMany(targetEntity="PortalEntity", mappedBy="company")
     */
    protected $portals;

    /**
     * @ORM\OneToMany(targetEntity="RxConnexionBundle\Entity\FaxingOptionEntity", mappedBy="company")
     */
    protected $faxingOptions;

    /**
     * @ORM\OneToMany(targetEntity="RxConnexionBundle\Entity\LetterTemplateEntity", mappedBy="company")
     */
    protected $letterTemplates;

    /**
     * @ORM\OneToMany(targetEntity="RxConnexionBundle\Entity\NoteTemplateEntity", mappedBy="company")
     */
    protected $noteTemplates;

    public function __construct()
    {
        $this->branches = new ArrayCollection();
        $this->accountUserCompanies = new ArrayCollection();
        $this->healthFacilities = new ArrayCollection();
        $this->items = new ArrayCollection();
        $this->portals = new ArrayCollection();
        $this->faxingOptions = new ArrayCollection();
    }

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

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

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


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

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

        return 'uploads/company/photo';
    }

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

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

    /**
     * get photoWebPath
     *
     * @return string
     */
    public function getPhotoWebPath() {

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

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

    /**
     * Get accountAndDescription
     *
     * @return string
     */
    public function getAccountAndDescription() {

        return ($this->account ? $this->account->getIdAndDescription() . ' | ' : '') . $this->description;
    }
    
	/*--------------------------------------------------------------------------------------------------------*/
	/*					Setters and Getters																	  */
	/*--------------------------------------------------------------------------------------------------------*/

    /**
     * Set code
     *
     * @param string $code
     *
     * @return CompanyEntity
     */
    public function setCode($code)
    {
        $this->code = $code;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set account
     *
     * @param \RxConnexionBundle\Entity\AccountEntity $account
     *
     * @return CompanyEntity
     */
    public function setAccount(\RxConnexionBundle\Entity\AccountEntity $account = null)
    {
        $this->account = $account;

        return $this;
    }

    /**
     * Get account
     *
     * @return \RxConnexionBundle\Entity\AccountEntity
     */
    public function getAccount()
    {
        return $this->account;
    }

    /**
     * Add branch
     *
     * @param \RxConnexionBundle\Entity\BranchEntity $branch
     *
     * @return CompanyEntity
     */
    public function addBranch(\RxConnexionBundle\Entity\BranchEntity $branch)
    {
        $this->branches[] = $branch;

        return $this;
    }

    /**
     * Remove branch
     *
     * @param \RxConnexionBundle\Entity\BranchEntity $branch
     */
    public function removeBranch(\RxConnexionBundle\Entity\BranchEntity $branch)
    {
        $this->branches->removeElement($branch);
    }

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

    /**
     * Add accountUserCompany
     *
     * @param \RxConnexionBundle\Entity\AccountUserCompanyEntity $accountUserCompany
     *
     * @return CompanyEntity
     */
    public function addAccountUserCompany(\RxConnexionBundle\Entity\AccountUserCompanyEntity $accountUserCompany)
    {
        $this->accountUserCompanies[] = $accountUserCompany;

        return $this;
    }

    /**
     * Remove accountUserCompany
     *
     * @param \RxConnexionBundle\Entity\AccountUserCompanyEntity $accountUserCompany
     */
    public function removeAccountUserCompany(\RxConnexionBundle\Entity\AccountUserCompanyEntity $accountUserCompany)
    {
        $this->accountUserCompanies->removeElement($accountUserCompany);
    }

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

    /**
     * Add healthFacility
     *
     * @param \RxConnexionBundle\Entity\CompanyEntity $healthFacility
     *
     * @return CompanyEntity
     */
    public function addHealthFacility(\RxConnexionBundle\Entity\CompanyEntity $healthFacility)
    {
        $this->healthFacilities[] = $healthFacility;

        return $this;
    }

    /**
     * Remove healthFacility
     *
     * @param \RxConnexionBundle\Entity\CompanyEntity $healthFacility
     */
    public function removeHealthFacility(\RxConnexionBundle\Entity\CompanyEntity $healthFacility)
    {
        $this->healthFacilities->removeElement($healthFacility);
    }

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

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

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

        return $this;
    }

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

    /**
     * Set oldSysId
     *
     * @param string $oldSysId
     *
     * @return CompanyEntity
     */
    public function setOldSysId($oldSysId)
    {
        $this->oldSysId = $oldSysId;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set photoDescription
     *
     * @param string $photoDescription
     *
     * @return CompanyEntity
     */
    public function setPhotoDescription($photoDescription)
    {
        $this->photoDescription = $photoDescription;

        return $this;
    }

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

    /**
     * Set parsedPhotoDescription
     *
     * @param string $parsedPhotoDescription
     *
     * @return CompanyEntity
     */
    public function setParsedPhotoDescription($parsedPhotoDescription)
    {
        $this->parsedPhotoDescription = $parsedPhotoDescription;

        return $this;
    }

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

    /**
     * Set lastOrNumber
     *
     * @param integer $lastOrNumber
     *
     * @return CompanyEntity
     */
    public function setLastOrNumber($lastOrNumber)
    {
        $this->lastOrNumber = $lastOrNumber;

        return $this;
    }

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

    /**
     * Add item
     *
     * @param \RxConnexionBundle\Entity\ItemEntity $item
     *
     * @return CompanyEntity
     */
    public function addItem(\RxConnexionBundle\Entity\ItemEntity $item)
    {
        $this->items[] = $item;

        return $this;
    }

    /**
     * Remove item
     *
     * @param \RxConnexionBundle\Entity\ItemEntity $item
     */
    public function removeItem(\RxConnexionBundle\Entity\ItemEntity $item)
    {
        $this->items->removeElement($item);
    }

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

    /**
     * Add portal
     *
     * @param \RxConnexionBundle\Entity\PortalEntity $portal
     *
     * @return CompanyEntity
     */
    public function addPortal(\RxConnexionBundle\Entity\PortalEntity $portal)
    {
        $this->portals[] = $portal;

        return $this;
    }

    /**
     * Remove portal
     *
     * @param \RxConnexionBundle\Entity\PortalEntity $portal
     */
    public function removePortal(\RxConnexionBundle\Entity\PortalEntity $portal)
    {
        $this->portals->removeElement($portal);
    }

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

    /**
     * Add faxingOption
     *
     * @param \RxConnexionBundle\Entity\FaxingOptionEntity $faxingOption
     *
     * @return CompanyEntity
     */
    public function addFaxingOption(\RxConnexionBundle\Entity\FaxingOptionEntity $faxingOption)
    {
        $this->faxingOptions[] = $faxingOption;

        return $this;
    }

    /**
     * Remove faxingOption
     *
     * @param \RxConnexionBundle\Entity\FaxingOptionEntity $faxingOption
     */
    public function removeFaxingOption(\RxConnexionBundle\Entity\FaxingOptionEntity $faxingOption)
    {
        $this->faxingOptions->removeElement($faxingOption);
    }

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

    /**
     * Add letterTemplate
     *
     * @param \RxConnexionBundle\Entity\LetterTemplateEntity $letterTemplate
     *
     * @return CompanyEntity
     */
    public function addLetterTemplate(\RxConnexionBundle\Entity\LetterTemplateEntity $letterTemplate)
    {
        $this->letterTemplates[] = $letterTemplate;

        return $this;
    }

    /**
     * Remove letterTemplate
     *
     * @param \RxConnexionBundle\Entity\LetterTemplateEntity $letterTemplate
     */
    public function removeLetterTemplate(\RxConnexionBundle\Entity\LetterTemplateEntity $letterTemplate)
    {
        $this->letterTemplates->removeElement($letterTemplate);
    }

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

    /**
     * Add noteTemplate
     *
     * @param \RxConnexionBundle\Entity\NoteTemplateEntity $noteTemplate
     *
     * @return CompanyEntity
     */
    public function addNoteTemplate(\RxConnexionBundle\Entity\NoteTemplateEntity $noteTemplate)
    {
        $this->noteTemplates[] = $noteTemplate;

        return $this;
    }

    /**
     * Remove noteTemplate
     *
     * @param \RxConnexionBundle\Entity\NoteTemplateEntity $noteTemplate
     */
    public function removeNoteTemplate(\RxConnexionBundle\Entity\NoteTemplateEntity $noteTemplate)
    {
        $this->noteTemplates->removeElement($noteTemplate);
    }

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