<?php

namespace RxConnexionBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="RxConnexionBundle\Repository\ItemUnitMeasurementRepository")
 * @ORM\Table(name="item_unit_measurement")
 * @ORM\HasLifecycleCallbacks()
 */
 
class ItemUnitMeasurementEntity
{
    /**
     * @ORM\Column(type="bigint")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
	protected $id;

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

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

    /**
     * @ORM\Column(name="is_smallest", type="boolean", nullable=true)
     */
    protected $isSmallest;
	
    /**
     * @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="ItemEntity", inversedBy="itemUnitMeasurements")
     * @ORM\JoinColumn(name="item_id", referencedColumnName="id")
     */
    protected $item;

    /**
     * @ORM\OneToMany(targetEntity="MaterialRequisitionItemUnitMeasurementEntity", mappedBy="itemUnitMeasurement",cascade={"remove"})
     */
    protected $materialRequisitionItemUnitMeasurements;

    /**
     * @ORM\OneToMany(targetEntity="CanvassItemUnitMeasurementEntity", mappedBy="itemUnitMeasurement",cascade={"remove"})
     */
    protected $canvassItemUnitMeasurements;

    /**
     * @ORM\OneToMany(targetEntity="ItemUnitMeasurementSupplierEntity", mappedBy="itemUnitMeasurement")
     */
    protected $itemUnitMeasurementSuppliers;

    /**
     * @ORM\OneToMany(targetEntity="AddInventoryItemUnitMeasurementEntity", mappedBy="itemUnitMeasurement")
     */
    protected $addInventoryItemUnitMeasurements;


    /**
     * @ORM\OneToMany(targetEntity="MaterialRequisitionItemEntity", mappedBy="itemUnitMeasurement")
     */
    protected $materialRequisitionItems;

    /**
     * @ORM\OneToMany(targetEntity="CanvassItemEntity", mappedBy="itemUnitMeasurement")
     */
    protected $canvassItems;

    /**
     * @ORM\OneToMany(targetEntity="CanvassItemSupplierEntity", mappedBy="itemUnitMeasurement")
     */
    protected $canvassItemSuppliers;

    /**
     * @ORM\OneToMany(targetEntity="PurchaseOrderItemEntity", mappedBy="itemUnitMeasurement")
     */
    protected $purchaseOrderItems;

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

    /**
     * @ORM\OneToMany(targetEntity="PurchaseOrderBreakdownItemEntity", mappedBy="itemUnitMeasurement")
     */
    protected $purchaseOrderBreakdownItems;

    /**
     * @ORM\OneToMany(targetEntity="PurchaseOrderBreakdownItemDeliveryEntity", mappedBy="itemUnitMeasurement")
     */
    protected $purchaseOrderBreakdownItemDeliveries;

    /** 
     * 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->code = $data->getCode();
            $this->description = $data->getDescription();
            $this->unitCost = $data->getUnitCost();
            $this->isSmallest = $data->getIsSmallest();
            $this->item = $data->getItem();
        }

        $this->addInventoryItemUnitMeasurements =  new ArrayCollection();
        $this->materialRequisitionItemUnitMeasurements = new ArrayCollection();
        $this->itemUnitMeasurementSuppliers = new ArrayCollection();
        $this->materialRequisitionItems =  new ArrayCollection();
//        $this->canvassItems =  new ArrayCollection();
//        $this->canvassItemSuppliers = new ArrayCollection();
        $this->purchaseOrderItems = new ArrayCollection();
        $this->purchaseOrderBreakdownItems =  new ArrayCollection();
//        $this->purchaseOrderBreakdownDeliveryItems =  new ArrayCollection();
        $this->addInventoryItems =  new ArrayCollection();
    }

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

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

        return base64_encode($this->id);
    }

    /**
     * Get codeAndDescription
     *
     * @return string
     */
    public function getCodeAndDescription() {

        return $this->code . ' - ' . $this->description;
    }

    public function setSmallestUnit($isSmallestUnit){

        $isSmallest = false;
        $hasSmallest = false;

        foreach ($this->item->getItemUnitMeasurements() as $measurement){
            if($measurement->getIsSmallest()){
                $measurement->setIsSmallest(false);
               $hasSmallest = true;
            }
        }

        if($isSmallestUnit){
            $isSmallest = true;
        }

        if(!$isSmallestUnit and !$hasSmallest ){
            $isSmallest = true;
        }

        $this->isSmallest = $isSmallest;

        return $this;
    }

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


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

    /**
     * Set code
     *
     * @param string $code
     * @return ItemUnitMeasurementEntity
     */
    public function setCode($code)
    {
        $this->code = $code;
    
        return $this;
    }

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

    /**
     * Set description
     *
     * @param string $description
     * @return ItemUnitMeasurementEntity
     */
    public function setDescription($description)
    {
        $this->description = $description;
    
        return $this;
    }

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

    /**
     * Set unitCost
     *
     * @param string $unitCost
     * @return ItemUnitMeasurementEntity
     */
    public function setUnitCost($unitCost)
    {
        $this->unitCost = $unitCost;
    
        return $this;
    }

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

    /**
     * Set isSmallest
     *
     * @param boolean $isSmallest
     * @return ItemUnitMeasurementEntity
     */
    public function setIsSmallest($isSmallest)
    {
        $this->isSmallest = $isSmallest;
    
        return $this;
    }

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

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

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

    /**
     * Set item
     *
     * @param \RxConnexionBundle\Entity\ItemEntity $item
     * @return ItemUnitMeasurementEntity
     */
    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;
    }

    /**
     * Add materialRequisitionItemUnitMeasurements
     *
     * @param \RxConnexionBundle\Entity\MaterialRequisitionItemUnitMeasurementEntity $materialRequisitionItemUnitMeasurements
     * @return ItemUnitMeasurementEntity
     */
    public function addMaterialRequisitionItemUnitMeasurement(\RxConnexionBundle\Entity\MaterialRequisitionItemUnitMeasurementEntity $materialRequisitionItemUnitMeasurements)
    {
        $this->materialRequisitionItemUnitMeasurements[] = $materialRequisitionItemUnitMeasurements;
    
        return $this;
    }

    /**
     * Remove materialRequisitionItemUnitMeasurements
     *
     * @param \RxConnexionBundle\Entity\MaterialRequisitionItemUnitMeasurementEntity $materialRequisitionItemUnitMeasurements
     */
    public function removeMaterialRequisitionItemUnitMeasurement(\RxConnexionBundle\Entity\MaterialRequisitionItemUnitMeasurementEntity $materialRequisitionItemUnitMeasurements)
    {
        $this->materialRequisitionItemUnitMeasurements->removeElement($materialRequisitionItemUnitMeasurements);
    }

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

    /**
     * Add canvassItemUnitMeasurements
     *
     * @param \RxConnexionBundle\Entity\CanvassItemUnitMeasurementEntity $canvassItemUnitMeasurements
     * @return ItemUnitMeasurementEntity
     */
    public function addCanvassItemUnitMeasurement(\RxConnexionBundle\Entity\CanvassItemUnitMeasurementEntity $canvassItemUnitMeasurements)
    {
        $this->canvassItemUnitMeasurements[] = $canvassItemUnitMeasurements;
    
        return $this;
    }

    /**
     * Remove canvassItemUnitMeasurements
     *
     * @param \RxConnexionBundle\Entity\CanvassItemUnitMeasurementEntity $canvassItemUnitMeasurements
     */
    public function removeCanvassItemUnitMeasurement(\RxConnexionBundle\Entity\CanvassItemUnitMeasurementEntity $canvassItemUnitMeasurements)
    {
        $this->canvassItemUnitMeasurements->removeElement($canvassItemUnitMeasurements);
    }

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

    /**
     * Add itemUnitMeasurementSuppliers
     *
     * @param \RxConnexionBundle\Entity\ItemUnitMeasurementSupplierEntity $itemUnitMeasurementSuppliers
     * @return ItemUnitMeasurementEntity
     */
    public function addItemUnitMeasurementSupplier(\RxConnexionBundle\Entity\ItemUnitMeasurementSupplierEntity $itemUnitMeasurementSuppliers)
    {
        $this->itemUnitMeasurementSuppliers[] = $itemUnitMeasurementSuppliers;
    
        return $this;
    }

    /**
     * Remove itemUnitMeasurementSuppliers
     *
     * @param \RxConnexionBundle\Entity\ItemUnitMeasurementSupplierEntity $itemUnitMeasurementSuppliers
     */
    public function removeItemUnitMeasurementSupplier(\RxConnexionBundle\Entity\ItemUnitMeasurementSupplierEntity $itemUnitMeasurementSuppliers)
    {
        $this->itemUnitMeasurementSuppliers->removeElement($itemUnitMeasurementSuppliers);
    }

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

    /**
     * Add materialRequisitionItem
     *
     * @param \RxConnexionBundle\Entity\MaterialRequisitionItemEntity $materialRequisitionItem
     * @return ItemUnitMeasurementEntity
     */
    public function addMaterialRequisitionItem(\RxConnexionBundle\Entity\MaterialRequisitionItemEntity $materialRequisitionItem)
    {
        $this->materialRequisitionItem[] = $materialRequisitionItem;
    
        return $this;
    }

    /**
     * Remove materialRequisitionItem
     *
     * @param \RxConnexionBundle\Entity\MaterialRequisitionItemEntity $materialRequisitionItem
     */
    public function removeMaterialRequisitionItem(\RxConnexionBundle\Entity\MaterialRequisitionItemEntity $materialRequisitionItem)
    {
        $this->materialRequisitionItem->removeElement($materialRequisitionItem);
    }

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

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

    /**
     * Add canvassItems
     *
     * @param \RxConnexionBundle\Entity\CanvassItemEntity $canvassItems
     * @return ItemUnitMeasurementEntity
     */
    public function addCanvassItem(\RxConnexionBundle\Entity\CanvassItemEntity $canvassItems)
    {
        $this->canvassItems[] = $canvassItems;
    
        return $this;
    }

    /**
     * Remove canvassItems
     *
     * @param \RxConnexionBundle\Entity\CanvassItemEntity $canvassItems
     */
    public function removeCanvassItem(\RxConnexionBundle\Entity\CanvassItemEntity $canvassItems)
    {
        $this->canvassItems->removeElement($canvassItems);
    }

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


    /**
     * Add canvassItemSuppliers
     *
     * @param \RxConnexionBundle\Entity\CanvassItemSupplierEntity $canvassItemSuppliers
     * @return ItemUnitMeasurementEntity
     */
    public function addCanvassItemSupplier(\RxConnexionBundle\Entity\CanvassItemSupplierEntity $canvassItemSuppliers)
    {
        $this->canvassItemSuppliers[] = $canvassItemSuppliers;
    
        return $this;
    }

    /**
     * Remove canvassItemSuppliers
     *
     * @param \RxConnexionBundle\Entity\CanvassItemSupplierEntity $canvassItemSuppliers
     */
    public function removeCanvassItemSupplier(\RxConnexionBundle\Entity\CanvassItemSupplierEntity $canvassItemSuppliers)
    {
        $this->canvassItemSuppliers->removeElement($canvassItemSuppliers);
    }

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

    /**
     * Add purchaseOrderItems
     *
     * @param \RxConnexionBundle\Entity\PurchaseOrderItemEntity $purchaseOrderItems
     * @return ItemUnitMeasurementEntity
     */
    public function addPurchaseOrderItem(\RxConnexionBundle\Entity\PurchaseOrderItemEntity $purchaseOrderItems)
    {
        $this->purchaseOrderItems[] = $purchaseOrderItems;
    
        return $this;
    }

    /**
     * Remove purchaseOrderItems
     *
     * @param \RxConnexionBundle\Entity\PurchaseOrderItemEntity $purchaseOrderItems
     */
    public function removePurchaseOrderItem(\RxConnexionBundle\Entity\PurchaseOrderItemEntity $purchaseOrderItems)
    {
        $this->purchaseOrderItems->removeElement($purchaseOrderItems);
    }

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

    /**
     * Add purchaseOrderBreakdownItems
     *
     * @param \RxConnexionBundle\Entity\PurchaseOrderBreakdownItemEntity $purchaseOrderBreakdownItems
     * @return ItemUnitMeasurementEntity
     */
    public function addPurchaseOrderBreakdownItem(\RxConnexionBundle\Entity\PurchaseOrderBreakdownItemEntity $purchaseOrderBreakdownItems)
    {
        $this->purchaseOrderBreakdownItems[] = $purchaseOrderBreakdownItems;
    
        return $this;
    }

    /**
     * Remove purchaseOrderBreakdownItems
     *
     * @param \RxConnexionBundle\Entity\PurchaseOrderBreakdownItemEntity $purchaseOrderBreakdownItems
     */
    public function removePurchaseOrderBreakdownItem(\RxConnexionBundle\Entity\PurchaseOrderBreakdownItemEntity $purchaseOrderBreakdownItems)
    {
        $this->purchaseOrderBreakdownItems->removeElement($purchaseOrderBreakdownItems);
    }

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

    /**
     * Add purchaseOrderBreakdownDeliveryItems
     *
     * @param \RxConnexionBundle\Entity\PurchaseOrderBreakdownItemDeliveryEntity $purchaseOrderBreakdownDeliveryItems
     * @return ItemUnitMeasurementEntity
     */
    public function addPurchaseOrderBreakdownDeliveryItem(\RxConnexionBundle\Entity\PurchaseOrderBreakdownItemDeliveryEntity $purchaseOrderBreakdownDeliveryItems)
    {
        $this->purchaseOrderBreakdownDeliveryItems[] = $purchaseOrderBreakdownDeliveryItems;
    
        return $this;
    }

    /**
     * Remove purchaseOrderBreakdownDeliveryItems
     *
     * @param \RxConnexionBundle\Entity\PurchaseOrderBreakdownItemDeliveryEntity $purchaseOrderBreakdownDeliveryItems
     */
    public function removePurchaseOrderBreakdownDeliveryItem(\RxConnexionBundle\Entity\PurchaseOrderBreakdownItemDeliveryEntity $purchaseOrderBreakdownDeliveryItems)
    {
        $this->purchaseOrderBreakdownDeliveryItems->removeElement($purchaseOrderBreakdownDeliveryItems);
    }

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

    /**
     * Add addInventoryItemUnitMeasurements
     *
     * @param \RxConnexionBundle\Entity\AddInventoryItemUnitMeasurementEntity $addInventoryItemUnitMeasurements
     * @return ItemUnitMeasurementEntity
     */
    public function addAddInventoryItemUnitMeasurement(\RxConnexionBundle\Entity\AddInventoryItemUnitMeasurementEntity $addInventoryItemUnitMeasurements)
    {
        $this->addInventoryItemUnitMeasurements[] = $addInventoryItemUnitMeasurements;
    
        return $this;
    }

    /**
     * Remove addInventoryItemUnitMeasurements
     *
     * @param \RxConnexionBundle\Entity\AddInventoryItemUnitMeasurementEntity $addInventoryItemUnitMeasurements
     */
    public function removeAddInventoryItemUnitMeasurement(\RxConnexionBundle\Entity\AddInventoryItemUnitMeasurementEntity $addInventoryItemUnitMeasurements)
    {
        $this->addInventoryItemUnitMeasurements->removeElement($addInventoryItemUnitMeasurements);
    }

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

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

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

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

    /**
     * Add purchaseOrderBreakdownItemDeliveries
     *
     * @param \RxConnexionBundle\Entity\PurchaseOrderBreakdownItemDeliveryEntity $purchaseOrderBreakdownItemDeliveries
     * @return ItemUnitMeasurementEntity
     */
    public function addPurchaseOrderBreakdownItemDelivery(\RxConnexionBundle\Entity\PurchaseOrderBreakdownItemDeliveryEntity $purchaseOrderBreakdownItemDeliveries)
    {
        $this->purchaseOrderBreakdownItemDeliveries[] = $purchaseOrderBreakdownItemDeliveries;

        return $this;
    }

    /**
     * Remove purchaseOrderBreakdownItemDeliveries
     *
     * @param \RxConnexionBundle\Entity\PurchaseOrderBreakdownItemDeliveryEntity $purchaseOrderBreakdownItemDeliveries
     */
    public function removePurchaseOrderBreakdownItemDelivery(\RxConnexionBundle\Entity\PurchaseOrderBreakdownItemDeliveryEntity $purchaseOrderBreakdownItemDeliveries)
    {
        $this->purchaseOrderBreakdownItemDeliveries->removeElement($purchaseOrderBreakdownItemDeliveries);
    }

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