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

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

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

//    /**
//     * @ORM\OneToOne(targetEntity="TransactionLinkEntity", mappedBy="purchaseOrderBreakdownInvoiceBeforePay")
//     */
//    protected $transactionLinkBeforePay;

//    /**
//     * @ORM\OneToOne(targetEntity="TransactionLinkEntity", mappedBy="purchaseOrderBreakdownInvoiceAfterPay")
//     */
//    protected $transactionLinkAfterPay;

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

    /**
     * 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->invoiceDate = $data->getInvoiceDateAsString();
            $this->invoiceNo = $data->getInvoiceNo();
            $this->purchaseOrderBreakdown = $data->getPurchaseOrderBreakdown();
            $this->transactionLinkAfterPay = $data->getTransactionLinkAfterPay();
            $this->transactionLinkBeforePay = $data->getTransactionLinkBeforePay();
        }

        $this->purchaseOrderBreakdownItemDeliveries = 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 totalAmountReceived
     *
     * @return string
     */
    public function getTotalAmountReceived() {

        $amt = 0;

        foreach($this->purchaseOrderBreakdownItemDeliveries as $purchaseOrderBreakdownItemDelivery) {
            $amt += ($purchaseOrderBreakdownItemDelivery->getQuantity() * $purchaseOrderBreakdownItemDelivery->getPurchaseOrderBreakdownItem()->getPurchaseOrderItem()->getPrice());
        }

        return $amt;
    }

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

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

    /**
     * Set invoiceDate
     *
     * @param \DateTime $invoiceDate
     * @return PurchaseOrderBreakdownInvoiceEntity
     */
    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 PurchaseOrderBreakdownInvoiceEntity
     */
    public function setInvoiceNo($invoiceNo)
    {
        $this->invoiceNo = $invoiceNo;

        return $this;
    }

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

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

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