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

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

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

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

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

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

    /**
     * @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\Column(name="is_consumable", type="boolean", nullable=true)
     */
    protected $isConsumable;

    /**
     * @ORM\ManyToOne(targetEntity="TransferInventoryEntity", inversedBy="transferInventoryItems")
     * @ORM\JoinColumn(name="transfer_inventory_id", referencedColumnName="id")
     */
    protected $transferInventory;

    /**
     * @ORM\ManyToOne(targetEntity="ItemEntity", inversedBy="transferInventoryItems")
     * @ORM\JoinColumn(name="item_id", referencedColumnName="id")
     */
    protected $item;

    /**
     * @ORM\ManyToOne(targetEntity="InventoryEntity", inversedBy="transferInventoryItems")
     * @ORM\JoinColumn(name="inventory_id", referencedColumnName="id")
     */
    protected $inventory;

    /**
     * @ORM\OneToMany(targetEntity="TransferInventoryDeliveryItemEntity", mappedBy="transferInventoryItem")
     */
    protected $transferInventoryDeliveryItems;

    /**
     * @ORM\OneToMany(targetEntity="TransferInventoryItemDeliveryEntity", mappedBy="transferInventoryItem")
     */
    protected $transferInventoryItemDeliveries;

    /**
     * @ORM\OneToMany(targetEntity="InventoryMovementEntity", mappedBy="transferInventoryItem")
     */
    protected $inventoryMovements;

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

    /**
     * @ORM\OneToMany(targetEntity="RxConnexionBundle\Entity\TransferInventoryItemInventorySerialNoEntity", mappedBy="transferInventoryItem", cascade={"remove"})
    */
    protected $transferInventoryItemInventorySerialNos;

    /**
     * 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->quantityRequested = $this->quantity;
    }

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

    public function updateQuantityReceived() {

        $quantityReceived = 0;

        foreach($this->transferInventoryItemDeliveries as $transferInventoryItemDelivery) {
            $quantityReceived += $transferInventoryItemDelivery->getQuantity();
        }

        $this->quantityReceived = $quantityReceived;

        return $this;
    }

    public function __construct($data=null)
    {
        if(!is_null($data)) {
            $this->id = $data->getId();
            $this->quantity = $data->getQuantity();
            $this->quantityReceived = $data->getQuantityReceived();
            $this->transferInventory = $data->getTransferInventory();
            $this->inventory = $data->getInventory();
        }

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

    /**
     * @return TransferInventoryItemEntity
     */
    public function updateQuantityDelivered() {

        $this->quantityDelivered = 0;

        if($this->transferInventoryDeliveryItems->count()) {
            foreach($this->transferInventoryDeliveryItems as $transferInventoryDeliveryItem) {
                $this->quantityDelivered += $transferInventoryDeliveryItem->getQuantity();
            }
        }

        return $this;
    }

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

	public function getDeliveredInventorySerialNosForSelectInputAsArray()
	{
		$serialNos = [];
		/**@var TransferInventoryDeliveryItemEntity $transferInventoryDeliveryItem*/
		foreach ($this->getTransferInventoryDeliveryItems() as $transferInventoryDeliveryItem) {
			/**@var TransferInventoryDeliveryItemTransferInventoryItemInvSerialNoEntity $transferInventoryDeliveryItemTransferInventoryItemInvSerialNo*/
			foreach ($transferInventoryDeliveryItem->getTransferInventoryDeliveryItemTransferInventoryItemInvSerialNos() as $transferInventoryDeliveryItemTransferInventoryItemInvSerialNo) {
				if($transferInventoryDeliveryItemTransferInventoryItemInvSerialNo->getTransferInventoryItemInventorySerialNo()->getInventorySerialNo()->getStatus() === InventorySerialNoEntity::STATUS_4) {
					$serialNos[] = implode('~', [
						$transferInventoryDeliveryItemTransferInventoryItemInvSerialNo->getId(),
						$transferInventoryDeliveryItemTransferInventoryItemInvSerialNo->getTransferInventoryItemInventorySerialNo()->getInventorySerialNo()->getSerialNo(),
						]);
				}
			}
		}
		return $serialNos;
	}

    public function getInventorySerialNoIds()
    {
        $arr = [];
        foreach ($this->getTransferInventoryItemInventorySerialNos() as $transferInventoryItemInventorySerialNo) {
            $arr[]= $transferInventoryItemInventorySerialNo->getInventorySerialNo()->getId();
        }
        return $arr;
    }

    public function getAvailableInventorySerialNosForSelectInputAsArray()
    {
        $serialNos = [];
        foreach ($this->getTransferInventoryItemInventorySerialNos() as $tiiisn) {
            //if($inventorySerialNo->getStatus() === InventorySerialNoEntity::STATUS_1) {
                $serialNos[] = implode('~', array(
                    $tiiisn->getId(),
                    $tiiisn->getInventorySerialNo()->getSerialNo(),
                ));
            //}
        }
        return $serialNos;
    }

    /**
     * Get quantityForDelivery
     *
     * @return string
     */
    public function getQuantityForDelivery() {

        return $this->quantity - $this->quantityDelivered;
    }

    public function getInventorySerialNos()
    {
        $arr = [];
        /**@var TransferInventoryItemInventorySerialNoEntity $transferInventoryItemInventorySerialNo*/
        foreach ($this->getTransferInventoryItemInventorySerialNos() as $transferInventoryItemInventorySerialNo) {
            $arr[] = [
                'id' => $transferInventoryItemInventorySerialNo->getInventorySerialNo()->getId(),
                'serialNo' => $transferInventoryItemInventorySerialNo->getInventorySerialNo()->getSerialNo(),
            ];
        }
        return $arr;
    }

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


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

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

        return $this;
    }

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

    /**
     * Set quantityRequested
     *
     * @param string $quantityRequested
     *
     * @return TransferInventoryItemEntity
     */
    public function setQuantityRequested($quantityRequested)
    {
        $this->quantityRequested = $quantityRequested;

        return $this;
    }

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

    /**
     * Set quantityDelivered
     *
     * @param string $quantityDelivered
     *
     * @return TransferInventoryItemEntity
     */
    public function setQuantityDelivered($quantityDelivered)
    {
        $this->quantityDelivered = $quantityDelivered;

        return $this;
    }

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

    /**
     * Set quantityReceived
     *
     * @param string $quantityReceived
     *
     * @return TransferInventoryItemEntity
     */
    public function setQuantityReceived($quantityReceived)
    {
        $this->quantityReceived = $quantityReceived;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set isConsumable
     *
     * @param boolean $isConsumable
     *
     * @return TransferInventoryItemEntity
     */
    public function setIsConsumable($isConsumable)
    {
        $this->isConsumable = $isConsumable;

        return $this;
    }

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

    /**
     * Set transferInventory
     *
     * @param \RxConnexionBundle\Entity\TransferInventoryEntity $transferInventory
     *
     * @return TransferInventoryItemEntity
     */
    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;
    }

    /**
     * Set item
     *
     * @param \RxConnexionBundle\Entity\ItemEntity $item
     *
     * @return TransferInventoryItemEntity
     */
    public function setItem(\RxConnexionBundle\Entity\ItemEntity $item = null)
    {
        $this->item = $item;

        return $this;
    }

    /**
     * Get item
     *
     * @return \RxConnexionBundle\Entity\ItemEntity
     */
    public function getItem()
    {
        return $this->item;
    }

    /**
     * Set inventory
     *
     * @param \RxConnexionBundle\Entity\InventoryEntity $inventory
     *
     * @return TransferInventoryItemEntity
     */
    public function setInventory(\RxConnexionBundle\Entity\InventoryEntity $inventory = null)
    {
        $this->inventory = $inventory;

        return $this;
    }

    /**
     * Get inventory
     *
     * @return \RxConnexionBundle\Entity\InventoryEntity
     */
    public function getInventory()
    {
        return $this->inventory;
    }

    /**
     * Add transferInventoryDeliveryItem
     *
     * @param \RxConnexionBundle\Entity\TransferInventoryDeliveryItemEntity $transferInventoryDeliveryItem
     *
     * @return TransferInventoryItemEntity
     */
    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;
    }

    /**
     * Add transferInventoryItemDelivery
     *
     * @param \RxConnexionBundle\Entity\TransferInventoryItemDeliveryEntity $transferInventoryItemDelivery
     *
     * @return TransferInventoryItemEntity
     */
    public function addTransferInventoryItemDelivery(\RxConnexionBundle\Entity\TransferInventoryItemDeliveryEntity $transferInventoryItemDelivery)
    {
        $this->transferInventoryItemDeliveries[] = $transferInventoryItemDelivery;

        return $this;
    }

    /**
     * Remove transferInventoryItemDelivery
     *
     * @param \RxConnexionBundle\Entity\TransferInventoryItemDeliveryEntity $transferInventoryItemDelivery
     */
    public function removeTransferInventoryItemDelivery(\RxConnexionBundle\Entity\TransferInventoryItemDeliveryEntity $transferInventoryItemDelivery)
    {
        $this->transferInventoryItemDeliveries->removeElement($transferInventoryItemDelivery);
    }

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

    /**
     * Add inventoryMovement
     *
     * @param \RxConnexionBundle\Entity\InventoryMovementEntity $inventoryMovement
     *
     * @return TransferInventoryItemEntity
     */
    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 TransferInventoryItemEntity
     */
    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 transferInventoryItemInventorySerialNo
     *
     * @param \RxConnexionBundle\Entity\TransferInventoryItemInventorySerialNoEntity $transferInventoryItemInventorySerialNo
     *
     * @return TransferInventoryItemEntity
     */
    public function addTransferInventoryItemInventorySerialNo(\RxConnexionBundle\Entity\TransferInventoryItemInventorySerialNoEntity $transferInventoryItemInventorySerialNo)
    {
        $this->transferInventoryItemInventorySerialNos[] = $transferInventoryItemInventorySerialNo;

        return $this;
    }

    /**
     * Remove transferInventoryItemInventorySerialNo
     *
     * @param \RxConnexionBundle\Entity\TransferInventoryItemInventorySerialNoEntity $transferInventoryItemInventorySerialNo
     */
    public function removeTransferInventoryItemInventorySerialNo(\RxConnexionBundle\Entity\TransferInventoryItemInventorySerialNoEntity $transferInventoryItemInventorySerialNo)
    {
        $this->transferInventoryItemInventorySerialNos->removeElement($transferInventoryItemInventorySerialNo);
    }

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