<?php

namespace RxConnexionBundle\Entity;

use Doctrine\Common\Persistence\ObjectManager;
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;

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

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

    /**
     * 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();
        $this->transferInventoryItemDeliveryInventorySerialNos = new ArrayCollection();
        $this->inventorySerialNos = 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))) : '';
    }

	public function cascadeTransferInventoryItemDeliveryInventorySerialNos(ArrayCollection $transferInventoryItemDeliveryInventorySerialNos, ObjectManager $em)
	{
		/**@var TransferInventoryItemDeliveryInventorySerialNoEntity $transferInventoryItemDeliveryInventorySerialNo*/

		// CHECK FOR NEWLY SUBMITTED ITEM
		foreach ($transferInventoryItemDeliveryInventorySerialNos as $transferInventoryItemDeliveryInventorySerialNo) {
			if(!$this->getTransferInventoryItemDeliveryInventorySerialNos()->contains($transferInventoryItemDeliveryInventorySerialNo)){
				$newItem = new TransferInventoryItemDeliveryInventorySerialNoEntity();
				$newItem->setTransferInventoryItemDelivery($this);
				$newItem->setInventorySerialNo($transferInventoryItemDeliveryInventorySerialNo->getInventorySerialNo());
				$this->addTransferInventoryItemDeliveryInventorySerialNo($newItem);
				$em->persist($newItem);
			}
		}
    }

	public function cascadeTransferInventoryItemDeliveryInventorySerialNos_byTransferInventoryDeliveryItemTransferInventoryItemInvSerialNo(ArrayCollection $transferInventoryDeliveryItemTransferInventoryItemInvSerialNos, ObjectManager $em)
	{

		/**@var TransferInventoryDeliveryItemTransferInventoryItemInvSerialNoEntity $transferInventoryItemInventorySerialNo*/
		/**@var TransferInventoryItemDeliveryInventorySerialNoEntity $transferInventoryItemDeliveryInventorySerialNo*/
		/**@var TransferInventoryDeliveryItemTransferInventoryItemInvSerialNoEntity $transferInventoryDeliveryItemTransferInventoryItemInvSerialNo*/

		// CHECK FOR REMOVAL
		foreach ($this->getTransferInventoryItemDeliveryInventorySerialNos() as $transferInventoryItemDeliveryInventorySerialNo) {
			$exists = false;

			foreach ($transferInventoryDeliveryItemTransferInventoryItemInvSerialNos as $transferInventoryDeliveryItemTransferInventoryItemInvSerialNo) {
				// IF EXISTS, SKIP CASCADE BECAUSE IT IS STILL IN THE ONE TO MANY
				if($transferInventoryDeliveryItemTransferInventoryItemInvSerialNo === $transferInventoryItemDeliveryInventorySerialNo->getTransferInventoryDeliveryItemTransferInventoryItemInvSerialNo()) {
					$exists = true;
					break;
				}
			}

			// IF NOT FOUND IN THE SUBMITTED ARRAY COLLECTION, REMOVE IT
			if(!$exists) {
				$this->removeTransferInventoryItemDeliveryInventorySerialNo($transferInventoryItemDeliveryInventorySerialNo);
				$transferInventoryItemDeliveryInventorySerialNo->getInventorySerialNo()->setStatus(InventorySerialNoEntity::STATUS_4);
				$em->remove($transferInventoryItemDeliveryInventorySerialNo);
				foreach ($transferInventoryItemDeliveryInventorySerialNo->getTransferInventoryItemDelivery()->getInventorySerialNos() as $inventorySerialNo) {
					if($inventorySerialNo->getTransferInventoryItemDelivery() === $this && $inventorySerialNo->getSerialNo() === $transferInventoryItemDeliveryInventorySerialNo->getInventorySerialNo()->getSerialNo()) {
						$em->remove($inventorySerialNo);
						break;
					}
				}
			}

		}

		foreach ($transferInventoryDeliveryItemTransferInventoryItemInvSerialNos as $transferInventoryDeliveryItemTransferInventoryItemInvSerialNo) {
			$exists = false;
			foreach ($this->getTransferInventoryItemDeliveryInventorySerialNos() as $transferInventoryItemDeliveryInventorySerialNo) {
				// IF EXISTS, SKIP PERSIST BECAUSE IT IS ALREADY IN THE ONE TO MANY
				if($transferInventoryItemDeliveryInventorySerialNo->getTransferInventoryDeliveryItemTransferInventoryItemInvSerialNo() === $transferInventoryDeliveryItemTransferInventoryItemInvSerialNo) {
					$exists = true;
					break;
				}
			}

			// IF NOT FOUND IN THE EXISTING ONE TO MANY, ADD IT
			if(!$exists) {
				$newIsn = new TransferInventoryItemDeliveryInventorySerialNoEntity();
				$newIsn->setTransferInventoryItemDelivery($this);
				$newIsn->setInventorySerialNo($transferInventoryDeliveryItemTransferInventoryItemInvSerialNo->getTransferInventoryItemInventorySerialNo()->getInventorySerialNo());
				$newIsn->setTransferInventoryDeliveryItemTransferInventoryItemInvSerialNo($transferInventoryDeliveryItemTransferInventoryItemInvSerialNo);
				$em->persist($newIsn);
				$this->addTransferInventoryItemDeliveryInventorySerialNo($newIsn);
				$transferInventoryDeliveryItemTransferInventoryItemInvSerialNo->getTransferInventoryItemInventorySerialNo()->getInventorySerialNo()->addTransferInventoryItemDeliveryInventorySerialNo($newIsn);
				$transferInventoryDeliveryItemTransferInventoryItemInvSerialNo->getTransferInventoryItemInventorySerialNo()->getInventorySerialNo()->setStatus(InventorySerialNoEntity::STATUS_5);
			}

		}


		return $this;
	}


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

    /**
     * Add transferInventoryItemDeliveryInventorySerialNo
     *
     * @param \RxConnexionBundle\Entity\TransferInventoryItemDeliveryInventorySerialNoEntity $transferInventoryItemDeliveryInventorySerialNo
     *
     * @return TransferInventoryItemDeliveryEntity
     */
    public function addTransferInventoryItemDeliveryInventorySerialNo(\RxConnexionBundle\Entity\TransferInventoryItemDeliveryInventorySerialNoEntity $transferInventoryItemDeliveryInventorySerialNo)
    {
        $this->transferInventoryItemDeliveryInventorySerialNos[] = $transferInventoryItemDeliveryInventorySerialNo;

        return $this;
    }

    /**
     * Remove transferInventoryItemDeliveryInventorySerialNo
     *
     * @param \RxConnexionBundle\Entity\TransferInventoryItemDeliveryInventorySerialNoEntity $transferInventoryItemDeliveryInventorySerialNo
     */
    public function removeTransferInventoryItemDeliveryInventorySerialNo(\RxConnexionBundle\Entity\TransferInventoryItemDeliveryInventorySerialNoEntity $transferInventoryItemDeliveryInventorySerialNo)
    {
        $this->transferInventoryItemDeliveryInventorySerialNos->removeElement($transferInventoryItemDeliveryInventorySerialNo);
    }

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

    /**
     * Add inventorySerialNo
     *
     * @param \RxConnexionBundle\Entity\InventorySerialNoEntity $inventorySerialNo
     *
     * @return TransferInventoryItemDeliveryEntity
     */
    public function addInventorySerialNo(\RxConnexionBundle\Entity\InventorySerialNoEntity $inventorySerialNo)
    {
        $this->inventorySerialNos[] = $inventorySerialNo;

        return $this;
    }

    /**
     * Remove inventorySerialNo
     *
     * @param \RxConnexionBundle\Entity\InventorySerialNoEntity $inventorySerialNo
     */
    public function removeInventorySerialNo(\RxConnexionBundle\Entity\InventorySerialNoEntity $inventorySerialNo)
    {
        $this->inventorySerialNos->removeElement($inventorySerialNo);
    }

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