<?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 Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer;

/**
 * @ORM\Entity(repositoryClass="RxConnexionBundle\Repository\ItemRepository")
 * @ORM\Table(name="item", indexes={@ORM\Index(name="IDX_SEARCH_000", columns={"description"})})
 */

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

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

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

    /**
     * @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="is_deleted", type="boolean")
     */
    protected $isDeleted;

    /**
     * @ORM\OneToOne(targetEntity="DeviceEntity", mappedBy="item")
     */
    protected $device;

    /**
     * @ORM\ManyToOne(targetEntity="ItemSubCategoryEntity", inversedBy="items")
     * @ORM\JoinColumn(name="item_subcategory_id", referencedColumnName="id")
     * @Groups("forAudit")
     */
    protected $itemSubCategory;

    /**
     * @ORM\ManyToOne(targetEntity="CompanyEntity", inversedBy="items")
     * @ORM\JoinColumn(name="item_company_id", referencedColumnName="id")
     * @Groups("forAudit")
     */
    protected $itemCompany;

    /**
     * @ORM\OneToMany(targetEntity="PatientBillingBreakdownEntity", mappedBy="item")
     */
    protected $patientBillingBreakdowns;

    /**
     * @ORM\OneToMany(targetEntity="ItemPriceEntity", mappedBy="item")
     */
    protected $itemPrices;

    /**
     * @ORM\OneToMany(targetEntity="RxConnexionBundle\Entity\InventoryEntity", mappedBy="item")
     */
    protected $inventories;

    /**
     * @ORM\OneToMany(targetEntity="RxConnexionBundle\Entity\AddInventoryItemEntity", mappedBy="item")
     */
    protected $addInventoryItems;

    /**
     * @ORM\OneToMany(targetEntity="RxConnexionBundle\Entity\TransferInventoryItemDefaultEntity", mappedBy="item")
     */
    protected $transferInventoryItemDefaults;

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

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

    /*--------------------------------------------------------------------------------------------------------*/
    /*					User 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 todayPrice
     *
     *@return string
     */

    public function getTodayPrice(){

        $todayPrice = 0;

        foreach ($this->itemPrices as $itemPrice){
            if($itemPrice->getEndDate() >= new \DateTime()){
                $todayPrice = $itemPrice->getPrice();
            }
        }

        return $todayPrice;
    }

    /**
     * Get inventoryCtrByBranchIdAndStatus
     *
     * @param $branchId
     * @param $status
     *
     * @return string
     */
    public function getInventoryCtrByBranchIdAndStatus($branchId, $status) {

        $ctr = 0;

        foreach($this->inventories as $inventory) {
            if($inventory->getHealthFacility()->getId() == $branchId && $inventory->getStatus() == $status) {
                $ctr++;
            }
        }

        return $ctr;
    }

    /**
     * Get inventoriesByBranchIdAndStatus
     *
     * @param $branchId
     * @param $status
     *
     * @return array
     */
    public function getInventoriesByBranchIdAndStatus($branchId, $status) {
        $inventories = array();
        foreach($this->inventories as $inventory) {
            if($inventory->getHealthFacility()->getId() == $branchId && $status === $inventory->getStatus() && $inventory->getQuantity() > 0 && !empty($inventory->getLocation())) {
                $inventories[] =$inventory;
            }
        }

        return $inventories;
    }

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


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

        return $this;
    }

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

    /**
     * Set type
     *
     * @param string $type
     *
     * @return ItemEntity
     */
    public function setType($type)
    {
        $this->type = $type;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set startDate
     *
     * @param \DateTime $startDate
     *
     * @return ItemEntity
     */
    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 ItemEntity
     */
    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 ItemEntity
     */
    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 ItemEntity
     */
    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 ItemEntity
     */
    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 ItemEntity
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

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

    /**
     * Add device
     *
     * @param \RxConnexionBundle\Entity\DeviceEntity $device
     *
     * @return ItemEntity
     */
    public function addDevice(\RxConnexionBundle\Entity\DeviceEntity $device)
    {
        $this->device[] = $device;

        return $this;
    }

    /**
     * Remove device
     *
     * @param \RxConnexionBundle\Entity\DeviceEntity $device
     */
    public function removeDevice(\RxConnexionBundle\Entity\DeviceEntity $device)
    {
        $this->device->removeElement($device);
    }

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

    /**
     * Set device
     *
     * @param \RxConnexionBundle\Entity\DeviceEntity $device
     *
     * @return ItemEntity
     */
    public function setDevice(\RxConnexionBundle\Entity\DeviceEntity $device = null)
    {
        $this->device = $device;

        return $this;
    }

    /**
     * Add patientBillingBreakdown
     *
     * @param \RxConnexionBundle\Entity\PatientBillingBreakdownEntity $patientBillingBreakdown
     *
     * @return ItemEntity
     */
    public function addPatientBillingBreakdown(\RxConnexionBundle\Entity\PatientBillingBreakdownEntity $patientBillingBreakdown)
    {
        $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 isDeleted
     *
     * @param boolean $isDeleted
     *
     * @return ItemEntity
     */
    public function setIsDeleted($isDeleted)
    {
        $this->isDeleted = $isDeleted;

        return $this;
    }

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

    /**
     * Set itemSubCategory
     *
     * @param \RxConnexionBundle\Entity\ItemSubCategoryEntity $itemSubCategory
     *
     * @return ItemEntity
     */
    public function setItemSubCategory(\RxConnexionBundle\Entity\ItemSubCategoryEntity $itemSubCategory = null)
    {
        $this->itemSubCategory = $itemSubCategory;

        return $this;
    }

    /**
     * Get itemSubCategory
     *
     * @return \RxConnexionBundle\Entity\ItemSubCategoryEntity
     */
    public function getItemSubCategory()
    {
        return $this->itemSubCategory;
    }

    /**
     * Add itemPrice
     *
     * @param \RxConnexionBundle\Entity\ItemPriceEntity $itemPrice
     *
     * @return ItemEntity
     */
    public function addItemPrice(\RxConnexionBundle\Entity\ItemPriceEntity $itemPrice)
    {
        $this->itemPrices[] = $itemPrice;

        return $this;
    }

    /**
     * Remove itemPrice
     *
     * @param \RxConnexionBundle\Entity\ItemPriceEntity $itemPrice
     */
    public function removeItemPrice(\RxConnexionBundle\Entity\ItemPriceEntity $itemPrice)
    {
        $this->itemPrices->removeElement($itemPrice);
    }

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

    /**
     * Set itemCompany
     *
     * @param \RxConnexionBundle\Entity\CompanyEntity $itemCompany
     *
     * @return ItemEntity
     */
    public function setItemCompany(\RxConnexionBundle\Entity\CompanyEntity $itemCompany = null)
    {
        $this->itemCompany = $itemCompany;

        return $this;
    }

    /**
     * Get itemCompany
     *
     * @return \RxConnexionBundle\Entity\CompanyEntity
     */
    public function getItemCompany()
    {
        return $this->itemCompany;
    }

    /**
     * Add addInventoryItem
     *
     * @param \RxConnexionBundle\Entity\ItemEntity $addInventoryItem
     *
     * @return ItemEntity
     */
    public function addAddInventoryItem(\RxConnexionBundle\Entity\ItemEntity $addInventoryItem)
    {
        $this->addInventoryItems[] = $addInventoryItem;

        return $this;
    }

    /**
     * Remove addInventoryItem
     *
     * @param \RxConnexionBundle\Entity\ItemEntity $addInventoryItem
     */
    public function removeAddInventoryItem(\RxConnexionBundle\Entity\ItemEntity $addInventoryItem)
    {
        $this->addInventoryItems->removeElement($addInventoryItem);
    }

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

    /**
     * Add inventory
     *
     * @param \RxConnexionBundle\Entity\InventoryEntity $inventory
     *
     * @return ItemEntity
     */
    public function addInventory(\RxConnexionBundle\Entity\InventoryEntity $inventory)
    {
        $this->inventories[] = $inventory;

        return $this;
    }

    /**
     * Remove inventory
     *
     * @param \RxConnexionBundle\Entity\InventoryEntity $inventory
     */
    public function removeInventory(\RxConnexionBundle\Entity\InventoryEntity $inventory)
    {
        $this->inventories->removeElement($inventory);
    }

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

    /**
     * Add transferInventoryItemDefault
     *
     * @param \RxConnexionBundle\Entity\TransferInventoryItemDefaultEntity $transferInventoryItemDefault
     *
     * @return ItemEntity
     */
    public function addTransferInventoryItemDefault(\RxConnexionBundle\Entity\TransferInventoryItemDefaultEntity $transferInventoryItemDefault)
    {
        $this->transferInventoryItemDefaults[] = $transferInventoryItemDefault;

        return $this;
    }

    /**
     * Remove transferInventoryItemDefault
     *
     * @param \RxConnexionBundle\Entity\TransferInventoryItemDefaultEntity $transferInventoryItemDefault
     */
    public function removeTransferInventoryItemDefault(\RxConnexionBundle\Entity\TransferInventoryItemDefaultEntity $transferInventoryItemDefault)
    {
        $this->transferInventoryItemDefaults->removeElement($transferInventoryItemDefault);
    }

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