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

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

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

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

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

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

    /**
     * @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="CanvassItemEntity", inversedBy="canvassItemSuppliers")
     * @ORM\JoinColumn(name="canvass_item_id", referencedColumnName="id")
     */
    protected $canvassItem;

    /**
     * @ORM\ManyToOne(targetEntity="CanvassEntity", inversedBy="canvassItems")
     * @ORM\JoinColumn(name="canvass_id", referencedColumnName="id")
     */
    protected $canvass;

    /**
     * @ORM\ManyToOne(targetEntity="MaterialRequisitionItemEntity", inversedBy="canvassItems")
     * @ORM\JoinColumn(name="material_requisition_item_id", referencedColumnName="id")
     */
    protected $materialRequisitionItem;

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

    /**
     * @ORM\OneToMany(targetEntity="CanvassItemUnitMeasurementEntity", mappedBy="canvassItem")
     */
    protected $canvassItemUnitMeasurements;

    /**
     * @ORM\ManyToOne(targetEntity="ItemUnitMeasurementEntity", inversedBy="canvassItems")
     * @ORM\JoinColumn(name="item_unit_measurement_id", referencedColumnName="id")
     */
    protected $itemUnitMeasurement;

    /**
     * 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->quantityCanvassed = 0.00;
        $this->quantityVoided = 0.00;
        $this->quantityAdded = 0.00;
    }

    /**
     * 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->quantityCanvassed = $data->getQuantityCanvassed();
            $this->quantityVoided = $data->getQuantityVoided();
            $this->quantityAdded = $data->getQuantityAdded();
            $this->canvass = $data->getCanvass();
            $this->materialRequisitionItem = $data->getMaterialRequisitionItem();
            $this->itemUnitMeasurement = $data->getItemUnitMeasurement();
        }

        $this->canvassItemSuppliers = new ArrayCollection();
        $this->canvassItemUnitMeasurements =  new ArrayCollection();
    }

    /**
     * @return CanvassItemEntity
     */
    public function updateQuantityCanvassed() {

        $this->quantityCanvassed = 0;

        if($this->canvassItemSuppliers->count()) {
            foreach($this->canvassItemSuppliers as $canvassItemSupplier) {
                if($canvassItemSupplier->getIsSelected()) {
                    $this->quantityCanvassed += $canvassItemSupplier->getQuantity();
                }
            }
        }

        return $this;
    }

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

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

        return base64_encode($this->id);
    }

    /**
     * Get quantityForCanvassing
     *
     * @return string
     */
    public function getQuantityForCanvassing() {

        return $this->quantity - $this->quantityVoided + $this->quantityAdded;
    }

    /**
     * Get quantityForVoiding
     *
     * @return string
     */
    public function getQuantityForVoiding() {

        return $this->quantity - $this->quantityCanvassed + $this->quantityAdded;
    }

    /**
     * Get isAllQuantityVoided
     *
     * @return boolean
     */
    public function getIsAllQuantityVoided() {

        return $this->quantity + $this->quantityAdded == $this->quantityVoided ? true : false;
    }

    /**
     * Get hasSupplierById
     *
     * @param $supplier_id
     * @return boolean
     */
    public function getHasSupplierById($supplier_id) {

        if($this->canvassItemSuppliers->count()) {
            foreach($this->canvassItemSuppliers as $canvassItemSupplier) {
                if($supplier_id == $canvassItemSupplier->getSupplier()->getId()) {
                    return true;
                }
            }
        }

        return false;
    }

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

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

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

        return $this;
    }

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

    /**
     * Set quantityCanvassed
     *
     * @param string $quantityCanvassed
     * @return CanvassItemEntity
     */
    public function setQuantityCanvassed($quantityCanvassed)
    {
        $this->quantityCanvassed = str_replace(',', '', $quantityCanvassed);

        return $this;
    }

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

    /**
     * Set quantityVoided
     *
     * @param string $quantityVoided
     * @return CanvassItemEntity
     */
    public function setQuantityVoided($quantityVoided)
    {
        $this->quantityVoided = str_replace(',', '', $quantityVoided);

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set canvass
     *
     * @param \RxConnexionBundle\Entity\CanvassEntity $canvass
     * @return CanvassItemEntity
     */
    public function setCanvass(\RxConnexionBundle\Entity\CanvassEntity $canvass = null)
    {
        $this->canvass = $canvass;

        return $this;
    }

    /**
     * Get canvass
     *
     * @return \RxConnexionBundle\Entity\CanvassEntity
     */
    public function getCanvass()
    {
        return $this->canvass;
    }

    /**
     * Set materialRequisitionItem
     *
     * @param \RxConnexionBundle\Entity\MaterialRequisitionItemEntity $materialRequisitionItem
     * @return CanvassItemEntity
     */
    public function setMaterialRequisitionItem(\RxConnexionBundle\Entity\MaterialRequisitionItemEntity $materialRequisitionItem = null)
    {
        $this->materialRequisitionItem = $materialRequisitionItem;

        return $this;
    }

    /**
     * Get materialRequisitionItem
     *
     * @return \RxConnexionBundle\Entity\MaterialRequisitionItemEntity
     */
    public function getMaterialRequisitionItem()
    {
        return $this->materialRequisitionItem;
    }

    /**
     * Add canvassItemSuppliers
     *
     * @param \RxConnexionBundle\Entity\CanvassItemSupplierEntity $canvassItemSuppliers
     * @return CanvassItemEntity
     */
    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;
    }

    /**
     * Set quantityAdded
     *
     * @param string $quantityAdded
     * @return CanvassItemEntity
     */
    public function setQuantityAdded($quantityAdded)
    {
        $this->quantityAdded = str_replace(',','', $quantityAdded);
    
        return $this;
    }

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

    /**
     * Add canvassItemUnitMeasurements
     *
     * @param \RxConnexionBundle\Entity\CanvassItemUnitMeasurementEntity $canvassItemUnitMeasurements
     * @return CanvassItemEntity
     */
    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;
    }


    /**
     * Set itemUnitMeasurement
     *
     * @param \RxConnexionBundle\Entity\ItemUnitMeasurementEntity $itemUnitMeasurement
     * @return CanvassItemEntity
     */
    public function setItemUnitMeasurement(\RxConnexionBundle\Entity\ItemUnitMeasurementEntity $itemUnitMeasurement = null)
    {
        $this->itemUnitMeasurement = $itemUnitMeasurement;
    
        return $this;
    }

    /**
     * Get itemUnitMeasurement
     *
     * @return \RxConnexionBundle\Entity\ItemUnitMeasurementEntity
     */
    public function getItemUnitMeasurement()
    {
        return $this->itemUnitMeasurement;
    }

    /**
     * Set canvassItem
     *
     * @param \RxConnexionBundle\Entity\CanvassItemEntity $canvassItem
     *
     * @return CanvassItemEntity
     */
    public function setCanvassItem(\RxConnexionBundle\Entity\CanvassItemEntity $canvassItem = null)
    {
        $this->canvassItem = $canvassItem;

        return $this;
    }

    /**
     * Get canvassItem
     *
     * @return \RxConnexionBundle\Entity\CanvassItemEntity
     */
    public function getCanvassItem()
    {
        return $this->canvassItem;
    }
}
