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

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

    /**
     * @ORM\Column(name="status", type="string")
     */
    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\OneToOne(targetEntity="PurchaseOrderBreakdownEntity", inversedBy="purchaseInvoice")
     * @ORM\JoinColumn(name="purchase_order_breakdown_id", referencedColumnName="id")
     */
    protected $purchaseOrderBreakdown;

    /**
     * @ORM\OneToMany(targetEntity="PurchaseInvoicePaymentEntity", mappedBy="purchaseInvoice")
     */
    protected $purchaseInvoicePayments;

    /**
     * @ORM\OneToMany(targetEntity="PurchaseInvoiceCreditPaymentEntity", mappedBy="purchaseInvoice")
     */
    protected $purchaseInvoiceCreditPayments;

    /**
     * @ORM\OneToMany(targetEntity="RxConnexionBundle\Entity\PurchaseInvoiceOtherCostEntity", mappedBy="purchaseInvoice")
     */
    protected $purchaseInvoiceOtherCosts;

    /**
     * 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 = 'Pending';
    }

    /**
     * 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->status = $data->getStatus();
            $this->purchaseOrderBreakdown = $data->getPurchaseOrderBreakdown();
        }

        $this->purchaseInvoicePayments = new ArrayCollection();
        $this->purchaseInvoiceCreditPayments = new ArrayCollection();

    }

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

	/**
	 * Get totalOtherCost
	 * @return float
	*/
	public function getTotalOtherCost()
	{
		$total = 0;
		foreach ($this->getPurchaseInvoiceOtherCosts() as $invoiceOtherCost) {
			$total += $invoiceOtherCost->getAmount();
		}
		return $total;
	}

    /**
     * Get hasCredit
     *
     * @return boolean
     */
    public function hasCredit() {

        $hasCredit = false;

        foreach ($this->getPurchaseInvoicePayments() as $invoicePayment){
            if($invoicePayment->getPaymentMethod() == 'Credit'){
                $hasCredit = true;
            }
        }

        return $hasCredit;
    }

    /**
     * Get idEncoded
     *
     * @return string
     */
    public function getIdEncoded() {

        return base64_encode($this->id);
    }

    /**
     * Get totalAmount
     *
     * @return string
     */
    public function getTotalAmount() {

        return $this->purchaseOrderBreakdown->getTotalAmount();
    }

    /**
     * Get totalAmountWhTax
     *
     * @return string
     */
    public function getTotalAmountWhTax() {

        $purchaseOrder = $this->purchaseOrderBreakdown->getPurchaseOrder();

        $totalAmt = $this->purchaseOrderBreakdown->getTotalAmount();

        return ($totalAmt / (1 + ($purchaseOrder->getVatRate() / 100))) * ($purchaseOrder->getWhTaxRate() / 100);
    }

    /**
     * Get totalAmountReceived
     *
     * @return float
     */
    public function getTotalAmountReceived() {

        return $this->purchaseOrderBreakdown->getTotalAmountReceived();
    }

    /**
     * Get totalAmountReceivedWhTax
     *
     * @return string
     */
    public function getTotalAmountReceivedWhTax() {

        $purchaseOrder = $this->purchaseOrderBreakdown->getPurchaseOrder();

        $totalAmt = $this->purchaseOrderBreakdown->getTotalAmountReceived();

        return round(($totalAmt / (1 + ($purchaseOrder->getVatRate() / 100))) * ($purchaseOrder->getWhTaxRate() / 100), 2);
    }

    /**
     * Get totalPayment
     *
     * @return string
     */
    public function getTotalPayment() {

        $totalPayment = 0;

        foreach($this->purchaseInvoicePayments as $purchaseInvoicePayment) {
            $totalPayment +=  $purchaseInvoicePayment->getAmount();
        }

        return $totalPayment;
    }

    /**
     * Get balanceAmount
     *
     * @return string
     */
    public function getBalanceAmount() {

        return (($this->getTotalAmountReceived() + $this->getTotalOtherCost()) - $this->getTotalAmountReceivedWhTax()) - $this->getTotalPayment();
    }

    /**
     * Get totalPaymentBelowAndEqualToDate
     *
     * @param date
     * @return string
     */
    public function getTotalPaymentBelowAndEqualToDate($date) {

        $dttst = new DateTimeToStringTransformer();
        $refDT = strtotime($date);
        $amt = 0;

        foreach($this->purchaseInvoicePayments as $purchaseInvoicePayment) {
            if(strtotime($dttst->transform($purchaseInvoicePayment->getPaymentDate())) <= $refDT) {
                $amt += $purchaseInvoicePayment->getAmount();
            }
        }

        return $amt;
    }

    /**
     * Get totalCredit
     *
     * @return string
     */
    public function getTotalCredit() {

        $totalCredit = 0;

        foreach($this->purchaseInvoicePayments as $purchaseInvoicePayment) {
            if($purchaseInvoicePayment->getPaymentMethod() == 'Credit'){
                $totalCredit +=  $purchaseInvoicePayment->getAmount();
            }
        }

        return $totalCredit;
    }

    /**
     * Get totalCreditPayment
     *
     * @return string
     */
    public function getTotalCreditPayment() {

        $totalCreditPayment = 0;

        foreach($this->purchaseInvoiceCreditPayments as $purchaseInvoiceCreditPayment) {
            $totalCreditPayment +=  $purchaseInvoiceCreditPayment->getAmount();
        }

        return $totalCreditPayment;
    }

    /**
     * Get totalCreditPaymentBalance
     *
     * @return string
     */
    public function getTotalCreditPaymentBalance() {
        $totalCreditPaymentBalance = 0;

        $totalCreditPaymentBalance = $this->getTotalCredit() - $this->getTotalCreditPayment();
        return $totalCreditPaymentBalance;
    }

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


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

    /**
     * Set status
     *
     * @param string $status
     * @return PurchaseInvoiceEntity
     */
    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 PurchaseInvoiceEntity
     */
    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 PurchaseInvoiceEntity
     */
    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 PurchaseInvoiceEntity
     */
    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 PurchaseInvoiceEntity
     */
    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 PurchaseInvoiceEntity
     */
    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 purchaseInvoicePayments
     *
     * @param \RxConnexionBundle\Entity\PurchaseInvoicePaymentEntity $purchaseInvoicePayments
     * @return PurchaseInvoiceEntity
     */
    public function addPurchaseInvoicePayment(\RxConnexionBundle\Entity\PurchaseInvoicePaymentEntity $purchaseInvoicePayments)
    {
        $this->purchaseInvoicePayments[] = $purchaseInvoicePayments;
    
        return $this;
    }

    /**
     * Remove purchaseInvoicePayments
     *
     * @param \RxConnexionBundle\Entity\PurchaseInvoicePaymentEntity $purchaseInvoicePayments
     */
    public function removePurchaseInvoicePayment(\RxConnexionBundle\Entity\PurchaseInvoicePaymentEntity $purchaseInvoicePayments)
    {
        $this->purchaseInvoicePayments->removeElement($purchaseInvoicePayments);
    }

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

    /**
     * Add purchaseInvoiceCreditPayments
     *
     * @param \RxConnexionBundle\Entity\PurchaseInvoiceCreditPaymentEntity $purchaseInvoiceCreditPayments
     * @return PurchaseInvoiceEntity
     */
    public function addPurchaseInvoiceCreditPayment(\RxConnexionBundle\Entity\PurchaseInvoiceCreditPaymentEntity $purchaseInvoiceCreditPayments)
    {
        $this->purchaseInvoiceCreditPayments[] = $purchaseInvoiceCreditPayments;
    
        return $this;
    }

    /**
     * Remove purchaseInvoiceCreditPayments
     *
     * @param \RxConnexionBundle\Entity\PurchaseInvoiceCreditPaymentEntity $purchaseInvoiceCreditPayments
     */
    public function removePurchaseInvoiceCreditPayment(\RxConnexionBundle\Entity\PurchaseInvoiceCreditPaymentEntity $purchaseInvoiceCreditPayments)
    {
        $this->purchaseInvoiceCreditPayments->removeElement($purchaseInvoiceCreditPayments);
    }

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

    /**
     * Add purchaseInvoiceOtherCost
     *
     * @param \RxConnexionBundle\Entity\PurchaseInvoiceOtherCostEntity $purchaseInvoiceOtherCost
     *
     * @return PurchaseInvoiceEntity
     */
    public function addPurchaseInvoiceOtherCost(\RxConnexionBundle\Entity\PurchaseInvoiceOtherCostEntity $purchaseInvoiceOtherCost)
    {
        $this->purchaseInvoiceOtherCosts[] = $purchaseInvoiceOtherCost;

        return $this;
    }

    /**
     * Remove purchaseInvoiceOtherCost
     *
     * @param \RxConnexionBundle\Entity\PurchaseInvoiceOtherCostEntity $purchaseInvoiceOtherCost
     */
    public function removePurchaseInvoiceOtherCost(\RxConnexionBundle\Entity\PurchaseInvoiceOtherCostEntity $purchaseInvoiceOtherCost)
    {
        $this->purchaseInvoiceOtherCosts->removeElement($purchaseInvoiceOtherCost);
    }

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