<?php

namespace RxConnexionBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
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\PatientBillingRepository")
 * @ORM\HasLifecycleCallbacks
 * @ORM\Table(name="patient_billing")
 */
class PatientBillingEntity extends BaseEntity
{
    const PAYER_TYPE_SELF_PAY = 'Self Pay';
    const PAYER_TYPE_INSURANCE = 'Insurance';
    const PAYER_TYPE_FINANCING = 'Financing';
    const BILLING_STATUS_PENDING_BILLING = 'Pending Billing';
    const BILLING_STATUS_PENDING_PAYMENT = 'Pending Payment';
    const BILLING_STATUS_PAID = 'Paid';
    const BILLING_STATUS_VOID = 'Void';
    const BILLING_STATUS_RETURNED = 'Returned';
    const BILLING_STATUS_REFUND = 'Refunded';

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

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

    /**
     * @ORM\Column(name="billing_date", type="datetime")
     * @Groups("forAudit")
     */
    protected $billingDate;

    /**
     * @ORM\Column(name="due_date", type="datetime")
     * @Groups("forAudit")
     */
    protected $dueDate;

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

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

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

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

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

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

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

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

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

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

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

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

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

    /**
     * @ORM\Column(name="prev_sys_invoice_no", type="integer", nullable=true)
     */
    protected $prevSysInvoiceNo;

    /**
     * @ORM\Column(name="prev_sys_invoice_branch", type="string", length=20, nullable=true)
     */
    protected $prevSysInvoiceBranch;

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

    /**
     * @ORM\Column(name="is_payment_plan", type="boolean", nullable=true)
     */
    protected $isPaymentPlan;

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

    /**
     * @ORM\ManyToOne(targetEntity="PatientInsuranceEntity", inversedBy="patientBillings")
     * @ORM\JoinColumn(name="patient_insurance_id", referencedColumnName="id")
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $patientInsurance;


    /**
     * @ORM\ManyToOne(targetEntity="HealthFacilityEntity", inversedBy="patientBillings")
     * @ORM\JoinColumn(name="health_facility_id", referencedColumnName="id")
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $healthFacility;

    /**
     * @ORM\ManyToOne(targetEntity="HealthFacilityEntity", inversedBy="addressForPatientBillings")
     * @ORM\JoinColumn(name="health_facility_for_address_id", referencedColumnName="id")
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $healthFacilityForAddress;

    /**
     * @ORM\OneToOne(targetEntity="PatientPaymentEntity", inversedBy="patientBillingFinancing")
     * @ORM\JoinColumn(name="patient_payment_id_financing", referencedColumnName="id")
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $patientPaymentFinancing;

    /**
     * @ORM\ManyToOne(targetEntity="PatientFileEntity", inversedBy="patientFileBillings")
     * @ORM\JoinColumn(name="patient_file_id", referencedColumnName="id")
     * @Groups("forAudit")
     * @MaxDepth(1)
     */
    protected $patientFile;

    /**
     * @ORM\OneToMany(targetEntity="PatientBillingBreakdownEntity", mappedBy="patientBilling", orphanRemoval=true, cascade={"persist", "remove"})
     * @Groups("forAudit")
     */
    protected $patientBillingBreakdowns;

    /**
     * @ORM\OneToMany(targetEntity="PatientPaymentEntity", mappedBy="patientBilling")
     */
    protected $patientPayments;

    /**
     * @ORM\OneToOne(targetEntity="PatientBillingFinancingPaymentDetailEntity", mappedBy="patientBilling", cascade={"remove"})
     */
    protected $patientBillingFinancingPaymentDetail;

    /**
     * @ORM\OneToMany(targetEntity="PatientBillingRefundEntity", mappedBy="patientBilling")
     */
    protected $patientBillingRefunds;

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

    /**
     * @ORM\OneToMany(targetEntity="RxConnexionBundle\Entity\PatientBillingInventoryEntity", mappedBy="patientBilling", cascade={"persist","remove"}, orphanRemoval=true)
     */
    protected $patientBillingInventories;

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

    /**
     * @ORM\PreFlush
     */
    public function preFlush() {
        if(($this->billingStatus != self::BILLING_STATUS_VOID) && ($this->billingStatus != self::BILLING_STATUS_REFUND) && ($this->billingStatus != self::BILLING_STATUS_RETURNED)){
            $this->updateBillingStatus();
        }
    }

    public function __construct()
    {
        $this->patientBillingBreakdowns = new ArrayCollection();
        $this->patientPayments = new ArrayCollection();
        $this->patientBillingRefunds = new ArrayCollection();
    }

    /**
     * Update billingStatus
     */
    public function updateBillingStatus() {
        $currentBillingStatus = $this->billingStatus;
        $this->billingStatus = null;
        $status = null;
        if(count($this->getPatientBillingBreakdowns())){
            if($this->amountBalance == 0){
                $this->billingStatus = self::BILLING_STATUS_PAID;
            }else{
                if($currentBillingStatus === self::BILLING_STATUS_VOID){
                    $this->billingStatus = self::BILLING_STATUS_VOID;
                }elseif ($currentBillingStatus === self::BILLING_STATUS_RETURNED){
                    $this->billingStatus = self::BILLING_STATUS_RETURNED;
                }elseif ($currentBillingStatus === self::BILLING_STATUS_REFUND){
                    $this->billingStatus = self::BILLING_STATUS_REFUND;
                } else {
                    $this->billingStatus = self::BILLING_STATUS_PENDING_PAYMENT;
                }
            }
        }else{
            if($this->getPatientPaymentFinancing()){
                if($this->getPatientPaymentFinancing()->getPayType() === PatientPaymentEntity::PAY_TYPE_FINANCING){
                    $this->billingStatus = self::BILLING_STATUS_PENDING_PAYMENT;
                }else{
                    $this->billingStatus = self::BILLING_STATUS_PENDING_BILLING;
                }
            }
        }
    }

    /**
     * Update billingIfReturned
     */
    public function updateBillingIfReturned($id = null) {

        if($this->patientBillingBreakdowns->count()) {
            foreach ($this->patientBillingBreakdowns as $patientBillingBreakdown) {
                if($patientBillingBreakdown->getId() == $id) {
                    $this->amountDue = $this->getAmountDue() - $patientBillingBreakdown->getAmount();
                }
            }

//            $ctr = 0;
//            foreach ($this->patientBillingBreakdowns as $patientBillingBreakdown) {
//                if($patientBillingBreakdown->getId() == $id) {
//                  if($patientBillingBreakdown->getStatus() == 'Return') {
//                        if (1 == $ctr) {
//                            $this->billingStatus = self::BILLING_STATUS_RETURNED;
//                        }
//                    }
//                }
//                $ctr++;
//
//                var_dump($ctr);
//                die();
//            }
        }
    }


    /*--------------------------------------------------------------------------------------------------------*/
    /*					Use Defined Methods         														  */
    /*--------------------------------------------------------------------------------------------------------*/

    /**
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function setAmountsValue() {
        /*if($this->payerType != PatientBillingEntity::PAYER_TYPE_FINANCING){*/
            $this->amountPaid = 0;
            $this->amountRefunded = 0;
            if($this->patientPayments->count()) {
                foreach($this->patientPayments as $patientPayment) {
                    if($patientPayment->getPaymentStatus() != PatientPaymentEntity::PAYMENT_STATUS_VOID && $patientPayment->getIsDeleted() != 1 && $patientPayment->getPaymentStatus() != PatientPaymentEntity::PAYMENT_STATUS_PENDING){
                        $this->amountPaid += $patientPayment->getAmount();
                    }
                }
            }

            if($this->patientBillingRefunds->count()) {
                foreach($this->patientBillingRefunds as $patientBillingRefund) {
                    if ($patientBillingRefund->getIsDeleted() == 0) {
                        $this->amountRefunded += $patientBillingRefund->getAmount();
                    }

                }
            }

        /*}*/
        //OLD COMPUTATION
//        $this->amountBalance = ($this->amountDue - $this->amountPaid) + $this->amountRefunded;
//        $this->amountPaid = $this->amountPaid - $this->amountRefunded;

        $this->amountBalance = ($this->amountDue - $this->amountPaid);


//        /**@var PatientBillingBreakdownEntity $patientBillingBreakdown*/
//        foreach($this->patientBillingBreakdowns as $patientBillingBreakdown){
//            $patientBillingBreakdown->setAmountValue();
//        }
        $this->setInitialInvoiceAmountValues();
    }

    /**
     * Update setAmtPaidAmtBalanceRefund
     */
    public function setAmtPaidAmtBalanceRefund() {
        if ($this->patientBillingRefunds->count() > 0 && $this->getBillingStatus() == self::BILLING_STATUS_PENDING_PAYMENT) {
            $this->amountBalance = 0;
            $this->amountPaid = $this->amountPaid + $this->amountRefunded;
        }
    }

    /**
     * Set initialInvoiceAmountValues
     * @return boolean
    */
    public function setInitialInvoiceAmountValues(){
        if(empty($this->invoiceAmountDue) || is_null($this->invoiceAmountDue)){
            $this->invoiceAmountDue = $this->amountDue;
        }
        if(empty($this->invoiceAmountBalance) || is_null($this->invoiceAmountBalance)){
            $this->invoiceAmountBalance = $this->amountBalance;
        }
        /**@var PatientBillingBreakdownEntity $patientBillingBreakdown*/
        foreach($this->patientBillingBreakdowns as $patientBillingBreakdown){
            $patientBillingBreakdown->setInitialInvoiceAmountValues();
        }
        return true;
    }

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

    /**
     * get billingDateAsString
     *
     * @return string
     */
    public function getBillingDateAsString() {

        $dttst = new DateTimeToStringTransformer();

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

    /**
     * get dueDateAsString
     *
     * @return string
     */
    public function getDueDateAsString() {

        $dttst = new DateTimeToStringTransformer();

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

    /**
     * Get invoiceNoAndBillingDateAndBalance
     *
     * @return string
     */
    public function getInvoiceNoAndBillingDateAndBalance() {

        return ($this->invoiceNo ? $this->invoiceNo : $this->prevSysInvoiceNo . '[ imported ]') . ' - ' . $this->getBillingDateAsString() . ' - ' . number_format($this->amountBalance, 2);
    }

    /**
     * get patientPaymentsNotVoid
     *
     * @return array
     */
    public function getPatientPaymentsNotVoid() {

        $arr = array();
        foreach($this->getPatientPayments() as $patientPayment){
            if($patientPayment->getPaymentStatus() !== PatientPaymentEntity::PAYMENT_STATUS_VOID){
                $arr[] = $patientPayment;
            }
        }

        return $arr;

    }

    /**
     * get demandLetterDateAsString
     *
     * @return string
     */
    public function getDemandLetterDateAsString() {

        $dttst = new DateTimeToStringTransformer();

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

    /**
     * get insuranceClaimDateAsString
     *
     * @return string
     */
    public function getInsuranceClaimDateAsString() {

        $dttst = new DateTimeToStringTransformer();

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

    /**
     * Get itemIdIsInInventory
     * @param $item_id
     * @return bool
     */
    public function getItemIdIsInInventory($item_id)
    {
        /**@var PatientBillingInventoryEntity $patientBillingInventory*/
        foreach ($this->getPatientBillingInventories() as $patientBillingInventory) {
            if($patientBillingInventory->getInventory()->getItem()->getId() === $item_id) {
                return true;
            }
        }
        return false;
    }

    /**
     * Get patientBillingInventoryById
     * @param $id
     * @return mixed|null
     */
    public function getPatientBillingInventoryById($id)
    {
        foreach ($this->getPatientBillingInventories() as $patientBillingInventory) {
            if($patientBillingInventory->getId() == $id) {
                return $patientBillingInventory;
            }
        }
        return null;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

    /**
     * Set createdBy
     *
     * @param string $createdBy
     *
     * @return PatientBillingEntity
     */
    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 PatientBillingEntity
     */
    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 PatientBillingEntity
     */
    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 PatientBillingEntity
     */
    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 PatientBillingEntity
     */
    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 patientBillingBreakdown
     *
     * @param \RxConnexionBundle\Entity\PatientBillingBreakdownEntity $patientBillingBreakdown
     *
     * @return PatientBillingEntity
     */
    public function addPatientBillingBreakdown(\RxConnexionBundle\Entity\PatientBillingBreakdownEntity $patientBillingBreakdown)
    {
        $patientBillingBreakdown->setPatientBilling($this);

        $this->patientBillingBreakdowns[] = $patientBillingBreakdown;

        return $this;
    }

    /**
     * Remove patientBillingBreakdown
     *
     * @param \RxConnexionBundle\Entity\PatientBillingBreakdownEntity $patientBillingBreakdown
     */
    public function removePatientBillingBreakdown(\RxConnexionBundle\Entity\PatientBillingBreakdownEntity $patientBillingBreakdown)
    {
        $this->patientBillingBreakdowns->removeElement($patientBillingBreakdown);
    }

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

    /**
     * Set amountDue
     *
     * @param string $amountDue
     *
     * @return PatientBillingEntity
     */
    public function setAmountDue($amountDue)
    {
        $this->amountDue = $amountDue;

        return $this;
    }

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

    /**
     * Set invoiceNo
     *
     * @param integer $invoiceNo
     *
     * @return PatientBillingEntity
     */
    public function setInvoiceNo($invoiceNo)
    {
        $this->invoiceNo = $invoiceNo;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set payerType
     *
     * @param string $payerType
     *
     * @return PatientBillingEntity
     */
    public function setPayerType($payerType)
    {
        $this->payerType = $payerType;

        return $this;
    }

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

    /**
     * Set patientInsurance
     *
     * @param \RxConnexionBundle\Entity\PatientInsuranceEntity $patientInsurance
     *
     * @return PatientBillingEntity
     */
    public function setPatientInsurance(\RxConnexionBundle\Entity\PatientInsuranceEntity $patientInsurance = null)
    {
        $this->patientInsurance = $patientInsurance;

        return $this;
    }

    /**
     * Get patientInsurance
     *
     * @return \RxConnexionBundle\Entity\PatientInsuranceEntity
     */
    public function getPatientInsurance()
    {
        return $this->patientInsurance;
    }

    /**
     * Set amountPaid
     *
     * @param string $amountPaid
     *
     * @return PatientBillingEntity
     */
    public function setAmountPaid($amountPaid)
    {
        $this->amountPaid = $amountPaid;

        return $this;
    }

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

    /**
     * Set amountBalance
     *
     * @param string $amountBalance
     *
     * @return PatientBillingEntity
     */
    public function setAmountBalance($amountBalance)
    {
        $this->amountBalance = $amountBalance;

        return $this;
    }

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

    /**
     * Add patientPayment
     *
     * @param \RxConnexionBundle\Entity\PatientPaymentEntity $patientPayment
     *
     * @return PatientBillingEntity
     */
    public function addPatientPayment(\RxConnexionBundle\Entity\PatientPaymentEntity $patientPayment)
    {
        $this->patientPayments[] = $patientPayment;

        return $this;
    }

    /**
     * Remove patientPayment
     *
     * @param \RxConnexionBundle\Entity\PatientPaymentEntity $patientPayment
     */
    public function removePatientPayment(\RxConnexionBundle\Entity\PatientPaymentEntity $patientPayment)
    {
        $this->patientPayments->removeElement($patientPayment);
    }

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

    /**
     * Set prevSysInvoiceNo
     *
     * @param integer $prevSysInvoiceNo
     *
     * @return PatientBillingEntity
     */
    public function setPrevSysInvoiceNo($prevSysInvoiceNo)
    {
        $this->prevSysInvoiceNo = $prevSysInvoiceNo;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set patientPaymentFinancing
     *
     * @param \RxConnexionBundle\Entity\PatientPaymentEntity $patientPaymentFinancing
     *
     * @return PatientBillingEntity
     */
    public function setPatientPaymentFinancing(\RxConnexionBundle\Entity\PatientPaymentEntity $patientPaymentFinancing = null)
    {
        $this->patientPaymentFinancing = $patientPaymentFinancing;

        return $this;
    }

    /**
     * Get patientPaymentFinancing
     *
     * @return \RxConnexionBundle\Entity\PatientPaymentEntity
     */
    public function getPatientPaymentFinancing()
    {
        return $this->patientPaymentFinancing;
    }

    /**
     * Set billingStatus
     *
     * @param string $billingStatus
     *
     * @return PatientBillingEntity
     */
    public function setBillingStatus($billingStatus)
    {
        $this->billingStatus = $billingStatus;

        return $this;
    }

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

    /**
     * Set prevSysInvoiceBranch
     *
     * @param string $prevSysInvoiceBranch
     *
     * @return PatientBillingEntity
     */
    public function setPrevSysInvoiceBranch($prevSysInvoiceBranch)
    {
        $this->prevSysInvoiceBranch = $prevSysInvoiceBranch;

        return $this;
    }

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

    /**
     * Set healthFacility
     *
     * @param \RxConnexionBundle\Entity\HealthFacilityEntity $healthFacility
     *
     * @return PatientBillingEntity
     */
    public function setHealthFacility(\RxConnexionBundle\Entity\HealthFacilityEntity $healthFacility = null)
    {
        $this->healthFacility = $healthFacility;

        return $this;
    }

    /**
     * Get healthFacility
     *
     * @return \RxConnexionBundle\Entity\HealthFacilityEntity
     */
    public function getHealthFacility()
    {
        return $this->healthFacility;
    }

    /**
     * Set healthFacilityForAddress
     *
     * @param \RxConnexionBundle\Entity\HealthFacilityEntity $healthFacilityForAddress
     *
     * @return PatientBillingEntity
     */
    public function setHealthFacilityForAddress(\RxConnexionBundle\Entity\HealthFacilityEntity $healthFacilityForAddress = null)
    {
        $this->healthFacilityForAddress = $healthFacilityForAddress;

        return $this;
    }

    /**
     * Get healthFacilityForAddress
     *
     * @return \RxConnexionBundle\Entity\HealthFacilityEntity
     */
    public function getHealthFacilityForAddress()
    {
        return $this->healthFacilityForAddress;
    }

    /**
     * Set patientBillingFinancingPaymentDetail
     *
     * @param \RxConnexionBundle\Entity\PatientBillingFinancingPaymentDetailEntity $patientBillingFinancingPaymentDetail
     *
     * @return PatientBillingEntity
     */
    public function setPatientBillingFinancingPaymentDetail(\RxConnexionBundle\Entity\PatientBillingFinancingPaymentDetailEntity $patientBillingFinancingPaymentDetail = null)
    {
        $this->patientBillingFinancingPaymentDetail = $patientBillingFinancingPaymentDetail;

        return $this;
    }

    /**
     * Get patientBillingFinancingPaymentDetail
     *
     * @return \RxConnexionBundle\Entity\PatientBillingFinancingPaymentDetailEntity
     */
    public function getPatientBillingFinancingPaymentDetail()
    {
        return $this->patientBillingFinancingPaymentDetail;
    }


     /**
      * Set comment
      *
      * @param string $comment
      *
      * @return PatientBillingEntity
      */
    public function setComment($comment)
    {
        $this->comment = $comment;

        return $this;
    }

    /**
     * Get comment
     *
     * @return string
     */
    public function getComment()
    {
        return $this->comment;

    }

    /**
     * Set isPaymentPlan
     *
     * @param boolean $isPaymentPlan
     *
     * @return PatientBillingEntity
     */
    public function setIsPaymentPlan($isPaymentPlan)
    {
        $this->isPaymentPlan = $isPaymentPlan;

        return $this;
    }

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

    /**
     * Set endorsedTo
     *
     * @param string $endorsedTo
     *
     * @return PatientBillingEntity
     */
    public function setEndorsedTo($endorsedTo)
    {
        $this->endorsedTo = $endorsedTo;

        return $this;
    }

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

    /**
     * Set demandLetterDate
     *
     * @param \DateTime $demandLetterDate
     *
     * @return PatientBillingEntity
     */
    public function setDemandLetterDate($demandLetterDate)
    {
        $this->demandLetterDate = empty($demandLetterDate) ? null : new \DateTime($demandLetterDate);

        return $this;
    }

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

    /**
     * Set insuranceClaimDate
     *
     * @param \DateTime $insuranceClaimDate
     *
     * @return PatientBillingEntity
     */
    public function setInsuranceClaimDate($insuranceClaimDate)
    {
        $this->insuranceClaimDate = empty($insuranceClaimDate) ? null : new \DateTime($insuranceClaimDate);

        return $this;
    }

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

    /**
     * Set serialNo
     *
     * @param integer $serialNo
     *
     * @return PatientBillingEntity
     */
    public function setSerialNo($serialNo)
    {
        $this->serialNo = $serialNo;

        return $this;
    }

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

    /**
     * Add patientBillingRefund
     *
     * @param \RxConnexionBundle\Entity\PatientBillingRefundEntity $patientBillingRefund
     *
     * @return PatientBillingEntity
     */
    public function addPatientBillingRefund(\RxConnexionBundle\Entity\PatientBillingRefundEntity $patientBillingRefund)
    {
        $this->patientBillingRefunds[] = $patientBillingRefund;

        return $this;
    }

    /**
     * Remove patientBillingRefund
     *
     * @param \RxConnexionBundle\Entity\PatientBillingRefundEntity $patientBillingRefund
     */
    public function removePatientBillingRefund(\RxConnexionBundle\Entity\PatientBillingRefundEntity $patientBillingRefund)
    {
        $this->patientBillingRefunds->removeElement($patientBillingRefund);
    }

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

    /**
     * Get patientBillingRefundsNotDeleted
     *
     * @return array
     */
    public function getPatientBillingRefundsNotDeleted()
    {
        $notDeletedObj = array();
        foreach ($this->patientBillingRefunds as $patientBillingRefund){
            if($patientBillingRefund->getIsDeleted() != 1) {
             $notDeletedObj[] = $patientBillingRefund;
            }
        }

        return $notDeletedObj;
    }

    /**
     * Set amountRefunded
     *
     * @param string $amountRefunded
     *
     * @return PatientBillingEntity
     */
    public function setAmountRefunded($amountRefunded)
    {
        $this->amountRefunded = $amountRefunded;

        return $this;
    }

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

    /**
     * Add patientPaymentBreakdown
     *
     * @param \RxConnexionBundle\Entity\PatientPaymentBreakdownEntity $patientPaymentBreakdown
     *
     * @return PatientBillingEntity
     */
    public function addPatientPaymentBreakdown(\RxConnexionBundle\Entity\PatientPaymentBreakdownEntity $patientPaymentBreakdown)
    {
        $this->patientPaymentBreakdowns[] = $patientPaymentBreakdown;

        return $this;
    }

    /**
     * Remove patientPaymentBreakdown
     *
     * @param \RxConnexionBundle\Entity\PatientPaymentBreakdownEntity $patientPaymentBreakdown
     */
    public function removePatientPaymentBreakdown(\RxConnexionBundle\Entity\PatientPaymentBreakdownEntity $patientPaymentBreakdown)
    {
        $this->patientPaymentBreakdowns->removeElement($patientPaymentBreakdown);
    }

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

    /**
     * Set invoiceAmountDue
     *
     * @param string $invoiceAmountDue
     *
     * @return PatientBillingEntity
     */
    public function setInvoiceAmountDue($invoiceAmountDue)
    {
        $this->invoiceAmountDue = $invoiceAmountDue;

        return $this;
    }

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

    /**
     * Set invoiceAmountBalance
     *
     * @param string $invoiceAmountBalance
     *
     * @return PatientBillingEntity
     */
    public function setInvoiceAmountBalance($invoiceAmountBalance)
    {
        $this->invoiceAmountBalance = $invoiceAmountBalance;

        return $this;
    }

    /**
     * Get invoiceAmountBalance
     *
     * @return string
     */
    public function getInvoiceAmountBalance()
    {
        $amountPaid = 0;
        /**@var PatientPaymentEntity $patientPayment*/
        foreach($this->patientPayments as $patientPayment){
            if($patientPayment->getPaymentStatus() == PatientPaymentEntity::PAYMENT_STATUS_POSTED){
                $amountPaid += $patientPayment->getAmount();
            }
        }
        return $this->invoiceAmountDue - $amountPaid;
        return $this->invoiceAmountBalance;
    }

    /**
     * Set patientFile
     *
     * @param \RxConnexionBundle\Entity\PatientFileEntity $patientFile
     *
     * @return PatientBillingEntity
     */
    public function setPatientFile(\RxConnexionBundle\Entity\PatientFileEntity $patientFile = null)
    {
        $this->patientFile = $patientFile;

        return $this;
    }

    /**
     * Get patientFile
     *
     * @return \RxConnexionBundle\Entity\PatientFileEntity
     */
    public function getPatientFile()
    {
        return $this->patientFile;
    }

    /**
     * Set accountingBillings
     *
     * @param \RxConnexionBundle\Entity\AccountingBillingEntity $accountingBillings
     *
     * @return PatientBillingEntity
     */
    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 PatientBillingEntity
     */
    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);
    }

    /**
     * Add patientBillingInventory
     *
     * @param \RxConnexionBundle\Entity\PatientBillingInventoryEntity $patientBillingInventory
     *
     * @return PatientBillingEntity
     */
    public function addPatientBillingInventory(\RxConnexionBundle\Entity\PatientBillingInventoryEntity $patientBillingInventory)
    {
        $patientBillingInventory->setPatientBilling($this);
        $this->patientBillingInventories[] = $patientBillingInventory;

        return $this;
    }

    /**
     * Remove patientBillingInventory
     *
     * @param \RxConnexionBundle\Entity\PatientBillingInventoryEntity $patientBillingInventory
     */
    public function removePatientBillingInventory(\RxConnexionBundle\Entity\PatientBillingInventoryEntity $patientBillingInventory)
    {
        $this->patientBillingInventories->removeElement($patientBillingInventory);
    }

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