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

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

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

    /**
     * @ORM\Column(name="received_date", type="datetime", nullable=true)
     */
    protected $receivedDate;


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

    /**
     * @ORM\Column(name="invoice_date", type="datetime", nullable=true)
     */
    protected $invoiceDate;

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

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

    /**
     * @ORM\Column(name="dr_date", type="datetime", nullable=true)
     */
    protected $drDate;

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

    /**
     * @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="PurchaseOrderBreakdownInvoiceEntity", inversedBy="purchaseOrderBreakdownItemDeliveries")
     * @ORM\JoinColumn(name="purchase_order_breakdown_invoice_id", referencedColumnName="id")
     */
    protected $purchaseOrderBreakdownInvoice;

    /**
     * @ORM\ManyToOne(targetEntity="PurchaseOrderBreakdownItemEntity", inversedBy="purchaseOrderBreakdownItemDeliveries")
     * @ORM\JoinColumn(name="purchase_order_breakdown_item_id", referencedColumnName="id")
     */
    protected $purchaseOrderBreakdownItem;

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

    /**
     * @ORM\ManyToOne(targetEntity="BankEntity", inversedBy="purchaseOrderBreakdownItemDeliveries")
     * @ORM\JoinColumn(name="bank_id", referencedColumnName="id")
     */
    protected $bank;

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

    /**
     * @ORM\OneToMany(targetEntity="InventoryMovementEntity", mappedBy="purchaseOrderBreakdownItemDelivery", cascade={"remove"})
     */
    protected $inventoryMovements;

    /**
     * @ORM\OneToMany(targetEntity="InventoryPriceEntity", mappedBy="purchaseOrderBreakdownItemDelivery", cascade={"persist", "remove"})
     */
    protected $inventoryPrices;

    /**
     * @ORM\OneToMany(targetEntity="RxConnexionBundle\Entity\PurchaseOrderBreakdownItemDeliverySerialNoEntity", mappedBy="purchaseOrderBreakdownItemDelivery", cascade={"persist", "remove"})
     */
    protected $purchaseOrderBreakdownItemDeliverySerialNos;

    /**
     * @ORM\OneToMany(targetEntity="RxConnexionBundle\Entity\InventoryEntity", mappedBy="purchaseOrderBreakdownItemDelivery", cascade={"persist", "remove"})
     */
    protected $inventories;

    /**
     * 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->quantity = $data->getQuantity();
            $this->invoiceDate = $data->getInvoiceDateAsString();
            $this->invoiceNo = $data->getInvoiceNo();
            $this->drNo = $data->getDrNo();
            $this->drDate = $data->getDrDateAsString();
            $this->paymentMethod = $data->getPaymentMethod();
            $this->location = $data->getLocation();
            $this->bank = $data->getBank();
            $this->purchaseOrderBreakdownInvoice = $data->getPurchaseOrderBreakdownInvoice();
            $this->purchaseOrderBreakdownItem = $data->getPurchaseOrderBreakdownItem();
            $this->itemUnitMeasurement = $data->getItemUnitMeasurement();
        }

        $this->inventoryMovements = new ArrayCollection();
        $this->inventoryPrices = new ArrayCollection();
        $this->purchaseOrderBreakdownItemDeliverySerialNos = new ArrayCollection();
    }

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

    /**
     * Get invoiceDateAsString
     *
     * @return string
     */
    public function getInvoiceDateAsString()
    {
        $dttst = new DateTimeToStringTransformer();
        return !empty($this->invoiceDate) ? date('m/d/Y', strtotime($dttst->transform($this->invoiceDate))) : '';
    }

    /**
     * Get drDateAsString
     *
     * @return string
     */
    public function getDrDateAsString()
    {
        $dttst = new DateTimeToStringTransformer();
        return !empty($this->drDate) ? date('m/d/Y', strtotime($dttst->transform($this->drDate))) : '';
    }

    /**
     * Get receivedDateAsString
     *
     * @return string
     */
    public function getReceivedDateAsString()
    {
        $dttst = new DateTimeToStringTransformer();
        return !empty($this->receivedDate) ? date('m/d/Y', strtotime($dttst->transform($this->receivedDate))) : '';
    }

    /**
     * Get quantityConversion
     *
     * @return string
     */
    public function getQuantityConversion() {

        if($this->purchaseOrderBreakdownItem->getItemUnitMeasurement()->getId() != $this->itemUnitMeasurement->getId()){
            $convertedQuantity = round(($this->quantity * $this->itemUnitMeasurement->getUnitCost()) / $this->purchaseOrderBreakdownItem->getItemUnitMeasurement()->getUnitCost(),2);
        } else {
            $convertedQuantity = $this->quantity ;
        }

        return $convertedQuantity;
    }

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

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

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

        return $this;
    }

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

    /**
     * Set invoiceDate
     *
     * @param \DateTime $invoiceDate
     * @return PurchaseOrderBreakdownItemDeliveryEntity
     */
    public function setInvoiceDate($invoiceDate)
    {
        $this->invoiceDate = empty($invoiceDate) ? NULL : new \DateTime($invoiceDate);

        return $this;
    }

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

    /**
     * Set invoiceNo
     *
     * @param string $invoiceNo
     * @return PurchaseOrderBreakdownItemDeliveryEntity
     */
    public function setInvoiceNo($invoiceNo)
    {
        $this->invoiceNo = $invoiceNo;

        return $this;
    }

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

    /**
     * Set drNo
     *
     * @param string $drNo
     * @return PurchaseOrderBreakdownItemDeliveryEntity
     */
    public function setDrNo($drNo)
    {
        $this->drNo = $drNo;

        return $this;
    }

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

    /**
     * Set paymentMethod
     *
     * @param string $paymentMethod
     * @return PurchaseOrderBreakdownItemDeliveryEntity
     */
    public function setPaymentMethod($paymentMethod)
    {
        $this->paymentMethod = $paymentMethod;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set purchaseOrderBreakdownItem
     *
     * @param \RxConnexionBundle\Entity\PurchaseOrderBreakdownItemEntity $purchaseOrderBreakdownItem
     * @return PurchaseOrderBreakdownItemDeliveryEntity
     */
    public function setPurchaseOrderBreakdownItem(\RxConnexionBundle\Entity\PurchaseOrderBreakdownItemEntity $purchaseOrderBreakdownItem = null)
    {
        $this->purchaseOrderBreakdownItem = $purchaseOrderBreakdownItem;

        return $this;
    }

    /**
     * Get purchaseOrderBreakdownItem
     *
     * @return \RxConnexionBundle\Entity\PurchaseOrderBreakdownItemEntity
     */
    public function getPurchaseOrderBreakdownItem()
    {
        return $this->purchaseOrderBreakdownItem;
    }

    /**
     * Set bank
     *
     * @param \RxConnexionBundle\Entity\BankEntity $bank
     * @return PurchaseOrderBreakdownItemDeliveryEntity
     */
    public function setBank(\RxConnexionBundle\Entity\BankEntity $bank = null)
    {
        $this->bank = $bank;

        return $this;
    }

    /**
     * Get bank
     *
     * @return \RxConnexionBundle\Entity\BankEntity
     */
    public function getBank()
    {
        return $this->bank;
    }

    /**
     * Add inventoryMovements
     *
     * @param \RxConnexionBundle\Entity\InventoryMovementEntity $inventoryMovements
     * @return PurchaseOrderBreakdownItemDeliveryEntity
     */
    public function addInventoryMovement(\RxConnexionBundle\Entity\InventoryMovementEntity $inventoryMovements)
    {
        $this->inventoryMovements[] = $inventoryMovements;

        return $this;
    }

    /**
     * Remove inventoryMovements
     *
     * @param \RxConnexionBundle\Entity\InventoryMovementEntity $inventoryMovements
     */
    public function removeInventoryMovement(\RxConnexionBundle\Entity\InventoryMovementEntity $inventoryMovements)
    {
        $this->inventoryMovements->removeElement($inventoryMovements);
    }

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

    /**
     * Add inventoryPrices
     *
     * @param \RxConnexionBundle\Entity\InventoryPriceEntity $inventoryPrices
     * @return PurchaseOrderBreakdownItemDeliveryEntity
     */
    public function addInventoryPrice(\RxConnexionBundle\Entity\InventoryPriceEntity $inventoryPrices)
    {
        $this->inventoryPrices[] = $inventoryPrices;

        return $this;
    }

    /**
     * Remove inventoryPrices
     *
     * @param \RxConnexionBundle\Entity\InventoryPriceEntity $inventoryPrices
     */
    public function removeInventoryPrice(\RxConnexionBundle\Entity\InventoryPriceEntity $inventoryPrices)
    {
        $this->inventoryPrices->removeElement($inventoryPrices);
    }

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

    /**
     * Set location
     *
     * @param \RxConnexionBundle\Entity\LocationEntity $location
     * @return PurchaseOrderBreakdownItemDeliveryEntity
     */
    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 purchaseOrderBreakdownInvoice
     *
     * @param \RxConnexionBundle\Entity\PurchaseOrderBreakdownInvoiceEntity $purchaseOrderBreakdownInvoice
     * @return PurchaseOrderBreakdownItemDeliveryEntity
     */
    public function setPurchaseOrderBreakdownInvoice(\RxConnexionBundle\Entity\PurchaseOrderBreakdownInvoiceEntity $purchaseOrderBreakdownInvoice = null)
    {
        $this->purchaseOrderBreakdownInvoice = $purchaseOrderBreakdownInvoice;
    
        return $this;
    }

    /**
     * Get purchaseOrderBreakdownInvoice
     *
     * @return \RxConnexionBundle\Entity\PurchaseOrderBreakdownInvoiceEntity
     */
    public function getPurchaseOrderBreakdownInvoice()
    {
        return $this->purchaseOrderBreakdownInvoice;
    }

    /**
     * Set doc_no
     *
     * @param string $docNo
     * @return PurchaseOrderBreakdownItemDeliveryEntity
     */
    public function setDocNo($docNo)
    {
        $this->doc_no = $docNo;
    
        return $this;
    }

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

    /**
     * Set itemUnitMeasurement
     *
     * @param \RxConnexionBundle\Entity\ItemUnitMeasurementEntity $itemUnitMeasurement
     * @return PurchaseOrderBreakdownItemDeliveryEntity
     */
    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 drDate
     *
     * @param \DateTime $drDate
     * @return PurchaseOrderBreakdownItemDeliveryEntity
     */
    public function setDrDate($drDate)
    {
        $this->drDate = empty($drDate) ? NULL : new \DateTime($drDate);

        return $this;
    }

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

    /**
     * Set receivedDate
     *
     * @param \DateTime $receivedDate
     * @return PurchaseOrderBreakdownItemDeliveryEntity
     */
    public function setReceivedDate($receivedDate)
    {
        $this->receivedDate = empty($receivedDate) ? NULL : new \DateTime($receivedDate);

        return $this;
    }

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

    /**
     * Add purchaseOrderBreakdownItemDeliverySerialNo
     *
     * @param \RxConnexionBundle\Entity\PurchaseOrderBreakdownItemDeliverySerialNoEntity $purchaseOrderBreakdownItemDeliverySerialNo
     *
     * @return PurchaseOrderBreakdownItemDeliveryEntity
     */
    public function addPurchaseOrderBreakdownItemDeliverySerialNo(\RxConnexionBundle\Entity\PurchaseOrderBreakdownItemDeliverySerialNoEntity $purchaseOrderBreakdownItemDeliverySerialNo)
    {
        $this->purchaseOrderBreakdownItemDeliverySerialNos[] = $purchaseOrderBreakdownItemDeliverySerialNo;

        return $this;
    }

    /**
     * Remove purchaseOrderBreakdownItemDeliverySerialNo
     *
     * @param \RxConnexionBundle\Entity\PurchaseOrderBreakdownItemDeliverySerialNoEntity $purchaseOrderBreakdownItemDeliverySerialNo
     */
    public function removePurchaseOrderBreakdownItemDeliverySerialNo(\RxConnexionBundle\Entity\PurchaseOrderBreakdownItemDeliverySerialNoEntity $purchaseOrderBreakdownItemDeliverySerialNo)
    {
        $this->purchaseOrderBreakdownItemDeliverySerialNos->removeElement($purchaseOrderBreakdownItemDeliverySerialNo);
    }

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

    /**
     * Add inventory
     *
     * @param \RxConnexionBundle\Entity\InventoryEntity $inventory
     *
     * @return PurchaseOrderBreakdownItemDeliveryEntity
     */
    public function addInventory(\RxConnexionBundle\Entity\InventoryEntity $inventory)
    {
        $this->inventories[] = $inventory;

        return $this;
    }

    /**
     * Remove inventory
     *
     * @param \RxConnexionBundle\Entity\InventoryEntity $inventory
     */
    public function removeInventory(\RxConnexionBundle\Entity\InventoryEntity $inventory)
    {
        $this->inventories->removeElement($inventory);
    }

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