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

class PurchaseOrderItemEntity
{
    /**
     * @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_with_po", type="decimal", precision=12, scale=2)
     */
    protected $quantityWithPo;

    /**
     * @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="price", type="decimal", precision=12, scale=2)
     */
    protected $price;

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

    /**
     * @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="PurchaseOrderEntity", inversedBy="purchaseOrderItems")
     * @ORM\JoinColumn(name="purchase_order_id", referencedColumnName="id")
     */
    protected $purchaseOrder;

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

    /**
     * @ORM\ManyToOne(targetEntity="CanvassItemSupplierEntity", inversedBy="purchaseOrderItems")
     * @ORM\JoinColumn(name="canvass_item_supplier_id", referencedColumnName="id")
     */
    protected $canvassItemSupplier;

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

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

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

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

    public function updateQuantityWithPo() {

        $quantityWithPo = 0.00;

        foreach($this->purchaseOrderBreakdownItems as $purchaseOrderBreakdownItem) {
            $quantityWithPo += $purchaseOrderBreakdownItem->getQuantity();
        }

        $this->quantityWithPo = $quantityWithPo;

        return $this;
    }

    public function __construct($data=null)
    {
        if(!is_null($data)) {
            $this->id = $data->getId();
            $this->quantity = $data->getQuantity();
            $this->quantityWithPo = $data->getQuantityWithPo();
            $this->quantityVoided = $data->getQuantityVoided();
            $this->quantityAdded = $data->getQuantityAdded();
            $this->price = $data->getPrice();
            $this->downPayment = $data->getDownPayment();
            $this->purchaseOrder = $data->getPurchaseOrder();
//            $this->materialRequisitionItem = $data->getMaterialRequisitionItem();
//            $this->canvassItemSupplier = $data->getCanvassItemSupplier();
//            $this->itemUnitMeasurement = $data->getItemUnitMeasurement();
        }

        $this->purchaseOrderBreakdownItems = new ArrayCollection();
    }

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

    /**
     * Get quantityForPo
     *
     * @return string
     */
    public function getQuantityForPo() {
        return $this->quantity - $this->quantityVoided + $this->getQuantityAdded();
    }

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

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

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

        $quantityVoided = $this->quantityVoided;

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

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

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

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

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

        return $this;
    }

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

    /**
     * Set quantityWithPo
     *
     * @param string $quantityWithPo
     * @return PurchaseOrderItemEntity
     */
    public function setQuantityWithPo($quantityWithPo)
    {
        $this->quantityWithPo = str_replace(',', '', $quantityWithPo);

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set downPayment
     *
     * @param string $downPayment
     * @return PurchaseOrderItemEntity
     */
    public function setDownPayment($downPayment)
    {
        $this->downPayment = floatval(str_replace(',', '', $downPayment));

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set purchaseOrder
     *
     * @param \RxConnexionBundle\Entity\PurchaseOrderEntity $purchaseOrder
     * @return PurchaseOrderItemEntity
     */
    public function setPurchaseOrder(\RxConnexionBundle\Entity\PurchaseOrderEntity $purchaseOrder = null)
    {
        $this->purchaseOrder = $purchaseOrder;

        return $this;
    }

    /**
     * Get purchaseOrder
     *
     * @return \RxConnexionBundle\Entity\PurchaseOrderEntity
     */
    public function getPurchaseOrder()
    {
        return $this->purchaseOrder;
    }

    /**
     * Set materialRequisitionItem
     *
     * @param \RxConnexionBundle\Entity\MaterialRequisitionItemEntity $materialRequisitionItem
     * @return PurchaseOrderItemEntity
     */
    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;
    }

    /**
     * Set canvassItemSupplier
     *
     * @param \RxConnexionBundle\Entity\CanvassItemSupplierEntity $canvassItemSupplier
     * @return PurchaseOrderItemEntity
     */
    public function setCanvassItemSupplier(\RxConnexionBundle\Entity\CanvassItemSupplierEntity $canvassItemSupplier = null)
    {
        $this->canvassItemSupplier = $canvassItemSupplier;

        return $this;
    }

    /**
     * Get canvassItemSupplier
     *
     * @return \RxConnexionBundle\Entity\CanvassItemSupplierEntity
     */
    public function getCanvassItemSupplier()
    {
        return $this->canvassItemSupplier;
    }

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

    /**
     * Set quantityAdded
     *
     * @param string $quantityAdded
     * @return PurchaseOrderItemEntity
     */
    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 PurchaseOrderItemEntity
     */
    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;
    }
}
