<?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\TransferInventoryItemDeliveryRepository")
 * @ORM\Table(name="transfer_inventory_item_delivery")
 * @ORM\HasLifecycleCallbacks()
 */

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

    /**
     * @ORM\Column(name="quantity", type="decimal", precision=12, scale=2)
     */
    protected $quantity;

    /**
     * @ORM\Column(name="receive_date", type="datetime", nullable=true)
     */
    protected $receiveDate;
    
    /**
     * @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="TransferInventoryItemEntity", inversedBy="transferInventoryItemDeliveries")
     * @ORM\JoinColumn(name="transfer_inventory_item_id", referencedColumnName="id")
     */
    protected $transferInventoryItem;

    /**
     * @ORM\ManyToOne(targetEntity="LocationEntity", inversedBy="transferInventoryItemDeliveries")
     * @ORM\JoinColumn(name="location_id", referencedColumnName="id")
     */
    protected $location;

    /**
     * @ORM\OneToMany(targetEntity="InventoryMovementEntity", mappedBy="transferInventoryItemDelivery", cascade={"remove"})
     */
    protected $inventoryMovements;

    /**
     * @ORM\OneToMany(targetEntity="InventoryPriceEntity", mappedBy="transferInventoryItemDelivery")
     */
    protected $inventoryPrices;

    /**
     * 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();
    }

    /**
     * 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->quantity = $data->getQuantity();
            $this->receiveDate = $data->getReceiveDateAsString();
            $this->transferInventoryItem = $data->getTransferInventoryItem();
            $this->location = $data->getLocation();
        }

        $this->inventoryMovements = new ArrayCollection();
        $this->inventoryPrices = new ArrayCollection();
    }

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

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

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

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

    /**
     * Set quantity
     *
     * @param string $quantity
     * @return TransferInventoryItemDeliveryEntity
     */
    public function setQuantity($quantity)
    {
        $this->quantity = str_replace(',', '', $quantity);

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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


    /**
     * Set transferInventoryItem
     *
     * @param \RxConnexionBundle\Entity\TransferInventoryItemEntity $transferInventoryItem
     *
     * @return TransferInventoryItemDeliveryEntity
     */
    public function setTransferInventoryItem(\RxConnexionBundle\Entity\TransferInventoryItemEntity $transferInventoryItem = null)
    {
        $this->transferInventoryItem = $transferInventoryItem;

        return $this;
    }

    /**
     * Get transferInventoryItem
     *
     * @return \RxConnexionBundle\Entity\TransferInventoryItemEntity
     */
    public function getTransferInventoryItem()
    {
        return $this->transferInventoryItem;
    }

    /**
     * Set location
     *
     * @param \RxConnexionBundle\Entity\LocationEntity $location
     *
     * @return TransferInventoryItemDeliveryEntity
     */
    public function setLocation(\RxConnexionBundle\Entity\LocationEntity $location = null)
    {
        $this->location = $location;

        return $this;
    }

    /**
     * Get location
     *
     * @return \RxConnexionBundle\Entity\LocationEntity
     */
    public function getLocation()
    {
        return $this->location;
    }

    /**
     * Add inventoryMovement
     *
     * @param \RxConnexionBundle\Entity\InventoryMovementEntity $inventoryMovement
     *
     * @return TransferInventoryItemDeliveryEntity
     */
    public function addInventoryMovement(\RxConnexionBundle\Entity\InventoryMovementEntity $inventoryMovement)
    {
        $this->inventoryMovements[] = $inventoryMovement;

        return $this;
    }

    /**
     * Remove inventoryMovement
     *
     * @param \RxConnexionBundle\Entity\InventoryMovementEntity $inventoryMovement
     */
    public function removeInventoryMovement(\RxConnexionBundle\Entity\InventoryMovementEntity $inventoryMovement)
    {
        $this->inventoryMovements->removeElement($inventoryMovement);
    }

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

    /**
     * Add inventoryPrice
     *
     * @param \RxConnexionBundle\Entity\InventoryPriceEntity $inventoryPrice
     *
     * @return TransferInventoryItemDeliveryEntity
     */
    public function addInventoryPrice(\RxConnexionBundle\Entity\InventoryPriceEntity $inventoryPrice)
    {
        $this->inventoryPrices[] = $inventoryPrice;

        return $this;
    }

    /**
     * Remove inventoryPrice
     *
     * @param \RxConnexionBundle\Entity\InventoryPriceEntity $inventoryPrice
     */
    public function removeInventoryPrice(\RxConnexionBundle\Entity\InventoryPriceEntity $inventoryPrice)
    {
        $this->inventoryPrices->removeElement($inventoryPrice);
    }

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