<?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 Doctrine\ORM\Mapping\HasLifecycleCallbacks;
use JMS\Serializer\Annotation\Groups;
use JMS\Serializer\Annotation\MaxDepth;

/**
 * @ORM\Entity(repositoryClass="RxConnexionBundle\Repository\PatientBillingRefundRepository")
 * @ORM\HasLifecycleCallbacks
 * @ORM\Table(name="patient_billing_refund")
 */

class PatientBillingRefundEntity extends BaseEntity
{
    /**
     * @ORM\Column(name="refund_date", type="datetime")
     * @Groups("forAudit")
     */
    protected $refundDate;

    /**
     * @ORM\Column(name="amount", type="decimal", precision=10, scale=2, nullable=true)
     * @Groups("forAudit")
     */
    protected $amount;

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

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

    /**
     * @ORM\ManyToOne(targetEntity="PatientBillingEntity", inversedBy="patientBillingRefunds")
     * @ORM\JoinColumn(name="patient_billing_id", referencedColumnName="id")
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $patientBilling;

    /**
     * @ORM\OneToMany(targetEntity="PatientRefundBreakdownEntity", mappedBy="patientBillingRefund")
     */
    protected $patientRefundBreakdowns;

    /**
     * @ORM\OneToMany(targetEntity="RxConnexionBundle\Entity\AccountingBillingEntity", mappedBy="patientBillingRefund", cascade={"remove"})
     */
    protected $accountingBillings;

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


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

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

    /**
     * @ORM\PostPersist
     */
    public function postPersist() {
        $this->patientBilling->addPatientBillingRefund($this);
    }


    /**
     * @ORM\PreRemove
     */
    public function preRemove() {
        $this->patientBilling->setAmountsValue();
    }

    /**
     * @ORM\PreFlush
     */
    public function preFlush() {
        $this->patientBilling->setAmountsValue();
        $this->patientBilling->updateBillingStatus();
    }

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

    /**
     * Get refundDateAsString
     *
     * @return string
     */
    public function getRefundDateAsString() {

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


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



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

        return $this;
    }

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

    /**
     * Set amount
     *
     * @param string $amount
     *
     * @return PatientBillingRefundEntity
     */
    public function setAmount($amount)
    {
        $this->amount = str_replace(',', '', $amount);

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set patientBilling
     *
     * @param \RxConnexionBundle\Entity\PatientBillingEntity $patientBilling
     *
     * @return PatientBillingRefundEntity
     */
    public function setPatientBilling(\RxConnexionBundle\Entity\PatientBillingEntity $patientBilling = null)
    {
        $this->patientBilling = $patientBilling;

        return $this;
    }

    /**
     * Get patientBilling
     *
     * @return \RxConnexionBundle\Entity\PatientBillingEntity
     */
    public function getPatientBilling()
    {
        return $this->patientBilling;
    }

    /**
     * Set patient
     *
     * @param \RxConnexionBundle\Entity\PatientEntity $patient
     *
     * @return PatientBillingRefundEntity
     */
    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;
    }

    /**
     * Add patientRefundBreakdown
     *
     * @param \RxConnexionBundle\Entity\PatientPaymentBreakdownEntity $patientRefundBreakdown
     *
     * @return PatientBillingRefundEntity
     */
    public function addPatientRefundBreakdown(\RxConnexionBundle\Entity\PatientPaymentBreakdownEntity $patientRefundBreakdown)
    {
        $this->patientRefundBreakdowns[] = $patientRefundBreakdown;

        return $this;
    }

    /**
     * Remove patientRefundBreakdown
     *
     * @param \RxConnexionBundle\Entity\PatientPaymentBreakdownEntity $patientRefundBreakdown
     */
    public function removePatientRefundBreakdown(\RxConnexionBundle\Entity\PatientPaymentBreakdownEntity $patientRefundBreakdown)
    {
        $this->patientRefundBreakdowns->removeElement($patientRefundBreakdown);
    }

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

    /**
     * Set accountingBillings
     *
     * @param \RxConnexionBundle\Entity\AccountingBillingEntity $accountingBillings
     *
     * @return PatientBillingRefundEntity
     */
    public function setAccountingBillings(\RxConnexionBundle\Entity\AccountingBillingEntity $accountingBillings = null)
    {
        $this->accountingBillings = $accountingBillings;

        return $this;
    }

    /**
     * Get accountingBillings
     *
     * @return \RxConnexionBundle\Entity\AccountingBillingEntity
     */
    public function getAccountingBillings()
    {
        return $this->accountingBillings;
    }

    /**
     * Add accountingBilling
     *
     * @param \RxConnexionBundle\Entity\AccountingBillingEntity $accountingBilling
     *
     * @return PatientBillingRefundEntity
     */
    public function addAccountingBilling(\RxConnexionBundle\Entity\AccountingBillingEntity $accountingBilling)
    {
        $this->accountingBillings[] = $accountingBilling;

        return $this;
    }

    /**
     * Remove accountingBilling
     *
     * @param \RxConnexionBundle\Entity\AccountingBillingEntity $accountingBilling
     */
    public function removeAccountingBilling(\RxConnexionBundle\Entity\AccountingBillingEntity $accountingBilling)
    {
        $this->accountingBillings->removeElement($accountingBilling);
    }
}
