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

class PurchaseOrderBreakdownItemEntity
{
    /**
     * @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="quantity_received", type="decimal", precision=12, scale=2, nullable=true)
     */
    protected $quantityReceived;

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

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

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

    /**
     * @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="PurchaseOrderBreakdownEntity", inversedBy="purchaseOrderBreakdownItems")
     * @ORM\JoinColumn(name="purchase_order_breakdown_id", referencedColumnName="id")
     */
    protected $purchaseOrderBreakdown;

    /**
     * @ORM\ManyToOne(targetEntity="PurchaseOrderItemEntity", inversedBy="purchaseOrderBreakdownItems")
     * @ORM\JoinColumn(name="purchase_order_item_id", referencedColumnName="id")
     */
    protected $purchaseOrderItem;

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

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

//    /**
//     * @ORM\OneToMany(targetEntity="TransmittalItemEntity", mappedBy="purchaseOrderBreakdownItem")
//     */
//    protected $transmittalItems;

    /**
     * @ORM\Column(name="void_notes", type="string", length=50, nullable=true)
     */
    protected $voidNotes;

    /**
     * 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->quantityTransmittal = 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 updateQuantityReceived() {

        // $quantityReceived += $purchaseOrderBreakdownItemDelivery->getQuantity();
        $quantityReceived = 0.00;

        foreach($this->purchaseOrderBreakdownItemDeliveries as $purchaseOrderBreakdownItemDelivery) {
//            if($this->itemUnitMeasurement->getId() != $purchaseOrderBreakdownItemDelivery->getItemUnitMeasurement->getId()){
//                $quantityReceived += round(($purchaseOrderBreakdownItemDelivery->getQuantity() * $purchaseOrderBreakdownItemDelivery->getItemUnitMeasurement()->getUnitCost()) / $this->itemUnitMeasurement->getUnitCost(),2);
//            } else {
//                $quantityReceived += $purchaseOrderBreakdownItemDelivery->getQuantity();
//            }

            $quantityReceived += $purchaseOrderBreakdownItemDelivery->getQuantity();
        }

       $this->quantityReceived = $quantityReceived;

        return $this;
    }

    public function updateQuantityTransmittal() {

        $quantityTransmittal = 0.00;

        foreach($this->transmittalItems as $transmittalItem) {
            $quantityTransmittal += $transmittalItem->getQuantity();
        }

        $this->quantityTransmittal = $quantityTransmittal;

        return $this;
    }

    public function __construct($data=null)
    {
        if(!is_null($data)) {
            $this->id = $data->getId();
            $this->quantity = $data->getQuantity();
            $this->quantityReceived = $data->getQuantityReceived();
            $this->quantityVoided = $data->getQuantityVoided();
            $this->quantityTransmittal = $data->getQuantityTransmittal();
            $this->purchaseOrderBreakdown = $data->getPurchaseOrderBreakdown();
            $this->purchaseOrderItem = $data->getPurchaseOrderItem();
//            $this->itemUnitMeasurement = $data->getItemUnitMeasurement();
        }

        $this->purchaseOrderBreakdownItemDeliveries = new ArrayCollection();
//        $this->transmittalItems = new ArrayCollection();
    }

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


    /**
     * Get quantityForReceiving
     *
     * @return string
     */
    public function getQuantityForReceiving() {

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

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

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

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

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

    /**
     * Get totalPrice
     *
     * @return string
     */
    public function getTotalPrice() {

        return ($this->quantity - $this->quantityVoided + $this->quantityAdded) * $this->purchaseOrderItem->getPrice();
    }

    /**
     * Get requiredQuantity
     *
     * @return string
     */
    public function getRequiredQuantity() {

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

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

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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set quantityTransmittal
     *
     * @param string $quantityTransmittal
     * @return PurchaseOrderBreakdownItemEntity
     */
    public function setQuantityTransmittal($quantityTransmittal)
    {
        $this->quantityTransmittal = str_replace(',', '', $quantityTransmittal);

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set purchaseOrderBreakdown
     *
     * @param \RxConnexionBundle\Entity\PurchaseOrderBreakdownEntity $purchaseOrderBreakdown
     * @return PurchaseOrderBreakdownItemEntity
     */
    public function setPurchaseOrderBreakdown(\RxConnexionBundle\Entity\PurchaseOrderBreakdownEntity $purchaseOrderBreakdown = null)
    {
        $this->purchaseOrderBreakdown = $purchaseOrderBreakdown;

        return $this;
    }

    /**
     * Get purchaseOrderBreakdown
     *
     * @return \RxConnexionBundle\Entity\PurchaseOrderBreakdownEntity
     */
    public function getPurchaseOrderBreakdown()
    {
        return $this->purchaseOrderBreakdown;
    }

    /**
     * Set purchaseOrderItem
     *
     * @param \RxConnexionBundle\Entity\PurchaseOrderItemEntity $purchaseOrderItem
     * @return PurchaseOrderBreakdownItemEntity
     */
    public function setPurchaseOrderItem(\RxConnexionBundle\Entity\PurchaseOrderItemEntity $purchaseOrderItem = null)
    {
        $this->purchaseOrderItem = $purchaseOrderItem;

        return $this;
    }

    /**
     * Get purchaseOrderItem
     *
     * @return \RxConnexionBundle\Entity\PurchaseOrderItemEntity
     */
    public function getPurchaseOrderItem()
    {
        return $this->purchaseOrderItem;
    }

    /**
     * Add purchaseOrderBreakdownItemDeliveries
     *
     * @param \RxConnexionBundle\Entity\PurchaseOrderBreakdownItemDeliveryEntity $purchaseOrderBreakdownItemDeliveries
     * @return PurchaseOrderBreakdownItemEntity
     */
    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;
    }

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

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

    /**
     * Set itemUnitMeasurement
     *
     * @param \RxConnexionBundle\Entity\ItemUnitMeasurementEntity $itemUnitMeasurement
     * @return PurchaseOrderBreakdownItemEntity
     */
    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 voidNotes
     *
     * @param string $voidNotes
     *
     * @return PurchaseOrderBreakdownItemEntity
     */
    public function setVoidNotes($voidNotes)
    {
        $this->voidNotes = $voidNotes;

        return $this;
    }

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