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

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

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

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

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

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

    /**
     * @ORM\Column(name="quantity_po", type="decimal", precision=12, scale=2, nullable=true)
     */
    protected $quantityPo;
    /**
     * @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, nullable=true)
     */
    protected $quantityVoided;

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

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

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

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

    /**
     * @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="MaterialRequisitionEntity", inversedBy="materialRequisitionItems")
     * @ORM\JoinColumn(name="material_requisition_id", referencedColumnName="id")
     */
    protected $materialRequisition;

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

    /**
     * @ORM\ManyToOne(targetEntity="LocationEntity", inversedBy="materialRequisitionItems")
     * @ORM\JoinColumn(name="location_id", referencedColumnName="id")
     */
    protected $location;

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

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

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

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

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

    /**
     * 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->status = 'New';
        $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 updateQuantityCanvassed() {

        $quantityCanvassed = 0;

        foreach($this->canvassItems as $canvassItem) {
           if($canvassItem->getCanvass()->getStatus() !== 'Cancelled') $quantityCanvassed += $canvassItem->getQuantityCanvassed();
        }

        $this->quantityCanvassed = $quantityCanvassed;

        return $this;
    }

    public function updateQuantityPo() {
        $this->quantityPo = 0;
        if($this->purchaseOrderItems->count()) {
            foreach($this->purchaseOrderItems as $purchaseOrderItem) {
                if('Cancelled' !== $purchaseOrderItem->getPurchaseOrder()->getStatus()) {
                    $this->quantityPo += $purchaseOrderItem->getQuantityWithPo();
                }
            }
        }
        return $this;
    }

    public function updateQuantityVoided() {

        $quantityVoided = 0;

        if(count($this->canvassItems)) {

            foreach($this->canvassItems as $canvassItem) {
                if($canvassItem->getCanvass()->getStatus() !== 'Cancelled') $quantityVoided += $canvassItem->getQuantityVoided();
            }
        }

        if(count($this->purchaseOrderItems)) {

            foreach($this->purchaseOrderItems as $purchaseOrderItem) {
                $quantityVoided += $purchaseOrderItem->getQuantityVoided();
                if(count($purchaseOrderItem->getPurchaseOrderBreakdownItems())) {
                    foreach($purchaseOrderItem->getPurchaseOrderBreakdownItems() as $purchaseOrderBreakdownItem) {
                        $quantityVoided += $purchaseOrderBreakdownItem->getQuantityVoided();
                    }
                }
            }
        }

        $this->quantityVoided = $quantityVoided;

        return $this;
    }

    public function updateQuantityAdded() {

        $quantityAdded = 0;

        if(count($this->canvassItems)) {

            foreach($this->canvassItems as $canvassItem) {
                if($canvassItem->getCanvass()->getStatus() !== 'Cancelled') $quantityAdded += $canvassItem->getQuantityAdded();
            }
        }

        if(count($this->purchaseOrderItems)) {

            foreach($this->purchaseOrderItems as $purchaseOrderItem) {
                $quantityAdded += $purchaseOrderItem->getQuantityAdded();
                if(count($purchaseOrderItem->getPurchaseOrderBreakdownItems())) {
                    foreach($purchaseOrderItem->getPurchaseOrderBreakdownItems() as $purchaseOrderBreakdownItem) {
                        $quantityAdded += $purchaseOrderBreakdownItem->getQuantityAdded();
                    }
                }
            }
        }

        $this->quantityAdded = $quantityAdded;

        return $this;
    }

    public function __construct(self $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->controlNo = $data->getControlNo();
            $this->status = $data->getStatus();
            $this->materialRequisition = $data->getMaterialRequisition();
            $this->item = $data->getItem();
            $this->location = $data->getLocation();
            $this->itemUnitMeasurement = $data->getItemUnitMeasurement();
            $this->price = $data->getPrice();
            $this->initialPrice = $data->getInitialPrice();
            $this->tax = $data->getTax();
        }

        $this->canvassItems = new ArrayCollection();
        $this->purchaseOrderItems = new ArrayCollection();
//        $this->transmittalItems = new ArrayCollection();
        $this->materialRequisitionItemUnitMeasurements = new ArrayCollection();
    }

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

    /**
     * Get itemProcessStatus
     *
     * @return string
     */
    public function getItemProcessStatus() {

        $itemProcessStatus  = '';

        if(count($this->transmittalItems)){
            $itemProcessStatus = 'On Transmittal';
        } elseif(count($this->purchaseOrderItems)){
            $itemProcessStatus = 'On Purchasing';
        } elseif(count($this->canvassItems)){
            $itemProcessStatus = 'On Canvassing';
        }

        return $itemProcessStatus;
    }

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

        return ($this->quantity - $this->quantityVoided + $this->quantityAdded) - $this->quantityCanvassed;
    }

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

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

    /**
     * Get itemUnitMeasurementQuantity
     *
     * @return string
     */
    public function getItemUnitMeasurementQuantity() {

        $itemUnitMeasurementQuantity = '';
        $ctr = count($this->materialRequisitionItemUnitMeasurements);

        foreach ($this->materialRequisitionItemUnitMeasurements as $key => $itemUnitMeasurement){
            $itemUnitMeasurementQuantity .= $itemUnitMeasurement->getQuantity() .'/' . $itemUnitMeasurement->getItemUnitMeasurement()->getCode() . ((($key + 1) < $ctr ) ? ' | ' : '');
        }

        return $itemUnitMeasurementQuantity;
    }

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

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

    /**
     * Set quantity
     *
     * @param string $quantity
     * @return MaterialRequisitionItemEntity
     */
    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 MaterialRequisitionItemEntity
     */
    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 MaterialRequisitionItemEntity
     */
    public function setQuantityVoided($quantityVoided)
    {
        $this->quantityVoided = str_replace(',', '', $quantityVoided);

        return $this;
    }

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

    /**
     * Set controlNo
     *
     * @param string $controlNo
     * @return MaterialRequisitionItemEntity
     */
    public function setControlNo($controlNo)
    {
        $this->controlNo = $controlNo;

        return $this;
    }

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

    /**
     * Set status
     *
     * @param string $status
     * @return MaterialRequisitionItemEntity
     */
    public function setStatus($status)
    {
        $this->status = $status;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set materialRequisition
     *
     * @param \RxConnexionBundle\Entity\MaterialRequisitionEntity $materialRequisition
     * @return MaterialRequisitionItemEntity
     */
    public function setMaterialRequisition(\RxConnexionBundle\Entity\MaterialRequisitionEntity $materialRequisition = null)
    {
        $this->materialRequisition = $materialRequisition;

        return $this;
    }

    /**
     * Get materialRequisition
     *
     * @return \RxConnexionBundle\Entity\MaterialRequisitionEntity
     */
    public function getMaterialRequisition()
    {
        return $this->materialRequisition;
    }

    /**
     * Set item
     *
     * @param \RxConnexionBundle\Entity\ItemEntity $item
     * @return MaterialRequisitionItemEntity
     */
    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 canvassItems
     *
     * @param \RxConnexionBundle\Entity\CanvassItemEntity $canvassItems
     * @return MaterialRequisitionItemEntity
     */
    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 purchaseOrderItems
     *
     * @param \RxConnexionBundle\Entity\PurchaseOrderItemEntity $purchaseOrderItems
     * @return MaterialRequisitionItemEntity
     */
    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;
    }

    /**
     * Set location
     *
     * @param \RxConnexionBundle\Entity\LocationEntity $location
     * @return MaterialRequisitionItemEntity
     */
    public function setLocation(\RxConnexionBundle\Entity\LocationEntity $location = null)
    {
        $this->location = $location;
    
        return $this;
    }

    /**
     * Get location
     *
     * @return \RxConnexionBundle\Entity\LocationEntity
     */
    public function getLocation()
    {
        return $this->location;
    }

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

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


    /**
     * Add materialRequisitionItemUnitMeasurements
     *
     * @param \RxConnexionBundle\Entity\MaterialRequisitionItemUnitMeasurementEntity $materialRequisitionItemUnitMeasurements
     * @return MaterialRequisitionItemEntity
     */
    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;
    }

    /**
     * Set adjustedCost
     *
     * @param string $adjustedCost
     * @return MaterialRequisitionItemEntity
     */
    public function setAdjustedCost($adjustedCost)
    {
        $this->adjustedCost = $adjustedCost;
    
        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set price
     *
     * @param string $price
     *
     * @return MaterialRequisitionItemEntity
     */
    public function setPrice($price)
    {
        $this->price = str_replace(',', '', $price);

        return $this;
    }

    /**
     * Get price
     *
     * @return float
     */
    public function getPrice()
    {
        return $this->price;
    }

    /**
     * Set totalPrice
     *
     * @param string $initialPrice
     *
     * @return MaterialRequisitionItemEntity
     */
    public function setInitialPrice($initialPrice)
    {
        $this->initialPrice = str_replace(',', '', $initialPrice);

        return $this;
    }

    /**
     * Get totalPrice
     *
     * @return string
     */
    public function getInitialPrice()
    {
        return $this->initialPrice;
    }

    /**
     * Set tax
     *
     * @param string $tax
     *
     * @return MaterialRequisitionItemEntity
     */
    public function setTax($tax)
    {
        $this->tax = str_replace(',', '', $tax);

        return $this;
    }

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