<?php

namespace RxConnexionBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer;

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

class AddInventoryEntity
{
    /**
     * @ORM\Column(type="bigint")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(name="add_date", type="datetime")
     */
    protected $addDate;

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

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

    /**
     * @ORM\Column(name="created_by", type="string", length=50)
     */
    protected $createdBy;

    /**
     * @ORM\Column(name="created_at", type="datetime")
     */
    protected $createdAt;

    /**
     * @ORM\Column(name="updated_by", type="string", length=50)
     */
    protected $updatedBy;

    /**
     * @ORM\Column(name="updated_at", type="datetime")
     */
    protected $updatedAt;

    /**
     * @ORM\ManyToOne(targetEntity="RxConnexionBundle\Entity\HealthFacilityEntity", inversedBy="addInventories")
     * @ORM\JoinColumn(name="health_facility_id", referencedColumnName="id")
     */
    protected $healthFacility;

//    /**
//     * @ORM\ManyToOne(targetEntity="SupplierEntity", inversedBy="addInventories")
//     * @ORM\JoinColumn(name="supplier_id", referencedColumnName="id")
//     */
//    protected $supplier;

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

    /**
     * @ORM\OneToMany(targetEntity="AddInventoryPaymentEntity", mappedBy="addInventory")
     */
    protected $addInventoryPayments;

    /**
     * Set createdAt
     *
     * @ORM\PrePersist
     */
    public function setCreatedAtValue()
    {
        $this->createdBy = isSet($_COOKIE['username']) ? $_COOKIE['username'] : 'System';
        $this->createdAt = new \DateTime();
        $this->updatedBy = isSet($_COOKIE['username']) ? $_COOKIE['username'] : 'System';
        $this->updatedAt = new \DateTime();
        $this->status = 'New';
    }

    /**
     * Set updatedAt
     *
     * @ORM\PreUpdate
     */
    public function setUpdatedAtValue()
    {
        $this->updatedBy = $_COOKIE['username'];
        $this->updatedAt = new \DateTime();
    }

    public function __construct($data=null)
    {
        if(!is_null($data)) {
            $this->id = $data->getId();
            $this->addDate = $data->getAddDateAsString();
            $this->note = $data->getNote();
            $this->status = $data->getStatus();
            $this->healthFacility = $data->getBranch();
//            $this->supplier = $data->getSupplier();
        }

        $this->addInventoryItems = new ArrayCollection();
        $this->addInventoryPayments = new ArrayCollection();
    }

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

    /**
     * Get idEncoded
     *
     * @return string
     */
    public function getIdEncoded() {

        return base64_encode($this->id);
    }

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

    /**
     * Get addInventoryNoParsed
     *
     * @return string
     */
    public function getAddInventoryNoParsed() {

        return sprintf('%012d', $this->id);
    }

    /**
     * Get totalPrice
     *
     * @return string
     */
    public function getTotalPrice() {

        $price = 0;

        foreach($this->addInventoryItems as $addInventoryItem) {
            $price += ($addInventoryItem->getPrice() * $addInventoryItem->getQuantity());
        }

        return $price;
    }

    /**
     * Get totalPayment
     *
     * @return string
     */
    public function getTotalPayment() {

        $payment = 0;

        foreach($this->addInventoryPayments as $addInventoryPayment) {
            $payment += $addInventoryPayment->getAmount();
        }

        return $payment;
    }

    /**
     * Get paymentBalance
     *
     * @return string
     */
    public function getPaymentBalance() {

        return $this->getTotalPrice() - $this->getTotalPayment();
    }

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



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

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

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set status
     *
     * @param string $status
     *
     * @return AddInventoryEntity
     */
    public function setStatus($status)
    {
        $this->status = $status;

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

    /**
     * Set healthFacility
     *
     * @param \RxConnexionBundle\Entity\HealthFacilityEntity $healthFacility
     *
     * @return AddInventoryEntity
     */
    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;
    }

    /**
     * Add addInventoryPayment
     *
     * @param \RxConnexionBundle\Entity\AddInventoryPaymentEntity $addInventoryPayment
     *
     * @return AddInventoryEntity
     */
    public function addAddInventoryPayment(\RxConnexionBundle\Entity\AddInventoryPaymentEntity $addInventoryPayment)
    {
        $this->addInventoryPayments[] = $addInventoryPayment;

        return $this;
    }

    /**
     * Remove addInventoryPayment
     *
     * @param \RxConnexionBundle\Entity\AddInventoryPaymentEntity $addInventoryPayment
     */
    public function removeAddInventoryPayment(\RxConnexionBundle\Entity\AddInventoryPaymentEntity $addInventoryPayment)
    {
        $this->addInventoryPayments->removeElement($addInventoryPayment);
    }

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