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

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

    /**
     * @ORM\Column(name="dr_no", type="integer")
     */
    protected $drNo;

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

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

    /**
     * @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="TransferInventoryEntity", inversedBy="transferInventoryDeliveries")
     * @ORM\JoinColumn(name="transfer_inventory_id", referencedColumnName="id")
     */
    protected $transferInventory;

    /**
     * @ORM\OneToMany(targetEntity="TransferInventoryDeliveryItemEntity", mappedBy="transferInventoryDelivery", cascade={"persist", "remove"})
     */
    protected $transferInventoryDeliveryItems;

    /**
     * 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()
    {
        $this->transferInventoryDeliveryItems = new ArrayCollection();
    }

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

    /**
     * Get drDateAsString
     *
     * @return string
     */
    public function getDrDateAsString() {

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

    /**
     * Get transferInventoryDeliveryItemByTranInvItemId
     *
     * @param $tranInvItemId
     * @return \RxConnexionBundle\Entity\TransferInventoryDeliveryItemEntity|null
     */
    public function getTransferInventoryDeliveryItemByTranInvItemId($tranInvItemId) {

        if($this->transferInventoryDeliveryItems->count()) {
            foreach($this->transferInventoryDeliveryItems as $transferInventoryDeliveryItem) {
                if($tranInvItemId == $transferInventoryDeliveryItem->getTransferInventoryItem()->getId()) {
                    return $transferInventoryDeliveryItem;
                }
            }
        }

        return null;
    }

    /**
     * Get transferInventoryDeliveryItemByTranInvItemIdAndInvId
     *
     * @param $tranInvItemId
     * @return \RxConnexionBundle\Entity\TransferInventoryDeliveryItemEntity|null
     */
    public function getTransferInventoryDeliveryItemByTranInvItemIdAndInvId($tranInvItemId, $invId) {

        if($this->transferInventoryDeliveryItems->count()) {
            foreach($this->transferInventoryDeliveryItems as $transferInventoryDeliveryItem) {
                if($tranInvItemId == $transferInventoryDeliveryItem->getTransferInventoryItem()->getId() && $invId == $transferInventoryDeliveryItem->getInventory()->getId()) {
                    return $transferInventoryDeliveryItem;
                }
            }
        }

        return null;
    }

    /**
     * @param ArrayCollection $tiiisn
     * @return $this
     */
    public function cascadePersistRemoveTransferInventoryItemInventorySerialNo(ArrayCollection $tiiisn)
    {

        //$toRemoves = new ArrayCollection(array_diff($this->getTransferInventoryDeliveryTransInvItemInvSerialNos()->toArray(), $transferInventoryItemInventorySerialNos->toArray()));
        //$toPersists = new ArrayCollection(array_diff($transferInventoryItemInventorySerialNos->toArray(), $this->getTransferInventoryDeliveryTransInvItemInvSerialNos()->toArray()));

        $toRemoves = new ArrayCollection();
        $toPersists = new ArrayCollection();


        $savedTiiisn = new ArrayCollection();
        foreach ($this->getTransferInventoryDeliveryTransInvItemInvSerialNos() as $transferInventoryDeliveryTransInvItemInvSerialNo) {
            $savedTiiisn->add($transferInventoryDeliveryTransInvItemInvSerialNo->getTransferInventoryItemInventorySerialNo());
        }

        foreach ($tiiisn as $transferInventoryItemInventorySerialNo) {
            if(!$savedTiiisn->contains($transferInventoryItemInventorySerialNo)){
                $toPersists->add($transferInventoryItemInventorySerialNo);
            }
        }
        foreach ($savedTiiisn as $transferInventoryDeliveryTransInvItemInvSerialNo) {
            if(!$tiiisn->contains($transferInventoryDeliveryTransInvItemInvSerialNo)){
                $toRemoves->add($transferInventoryDeliveryTransInvItemInvSerialNo);
            }
        }

        foreach ($toRemoves as $toRemove) {
            $this->removeTransferInventoryDeliveryTransInvItemInvSerialNoByTiiisn($toRemove);
        }
        foreach ($toPersists as $toPersist) {
            $new = new TransferInventoryDeliveryTransInvItemInvSerialNoEntity();
            $new->setTransferInventoryDelivery($this);
            $new->setTransferInventoryItemInventorySerialNo($toPersist);
            $this->addTransferInventoryDeliveryTransInvItemInvSerialNo($toPersist);
        }

        return $this;
    }

    /**
     * Get transferInventoryDeliveryTransInvItemInvSerialNoByTransferInventoryItem
     * @param $transferInventoryItem
     * @return ArrayCollection
     */
    public function getTransferInventoryDeliveryTransInvItemInvSerialNoByTransferInventoryItem($transferInventoryItem)
    {
        $arr = new ArrayCollection();
        /**@var TransferInventoryDeliveryTransInvItemInvSerialNoEntity $transferInventoryDeliveryTransInvItemInvSerialNo*/
        foreach ($this->getTransferInventoryDeliveryTransInvItemInvSerialNos() as $transferInventoryDeliveryTransInvItemInvSerialNo) {
            if($transferInventoryDeliveryTransInvItemInvSerialNo->getTransferInventoryItemInventorySerialNo()->getTransferInventoryItem() == $transferInventoryItem) {
                $arr->add($transferInventoryDeliveryTransInvItemInvSerialNo);
            }
        }
        return $arr;
    }

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



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

    /**
     * Set drNo
     *
     * @param integer $drNo
     *
     * @return TransferInventoryDeliveryEntity
     */
    public function setDrNo($drNo)
    {
        $this->drNo = str_replace(',', '', $drNo);

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set transferInventory
     *
     * @param \RxConnexionBundle\Entity\TransferInventoryEntity $transferInventory
     *
     * @return TransferInventoryDeliveryEntity
     */
    public function setTransferInventory(\RxConnexionBundle\Entity\TransferInventoryEntity $transferInventory = null)
    {
        $this->transferInventory = $transferInventory;

        return $this;
    }

    /**
     * Get transferInventory
     *
     * @return \RxConnexionBundle\Entity\TransferInventoryEntity
     */
    public function getTransferInventory()
    {
        return $this->transferInventory;
    }

    /**
     * Add transferInventoryDeliveryItem
     *
     * @param \RxConnexionBundle\Entity\TransferInventoryDeliveryItemEntity $transferInventoryDeliveryItem
     *
     * @return TransferInventoryDeliveryEntity
     */
    public function addTransferInventoryDeliveryItem(\RxConnexionBundle\Entity\TransferInventoryDeliveryItemEntity $transferInventoryDeliveryItem)
    {
        $this->transferInventoryDeliveryItems[] = $transferInventoryDeliveryItem;

        return $this;
    }

    /**
     * Remove transferInventoryDeliveryItem
     *
     * @param \RxConnexionBundle\Entity\TransferInventoryDeliveryItemEntity $transferInventoryDeliveryItem
     */
    public function removeTransferInventoryDeliveryItem(\RxConnexionBundle\Entity\TransferInventoryDeliveryItemEntity $transferInventoryDeliveryItem)
    {
        $this->transferInventoryDeliveryItems->removeElement($transferInventoryDeliveryItem);
    }

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