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

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

    /**
     * @ORM\Column(name="convert_date", type="datetime")
     */
    protected $convertDate;

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

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

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

    /**
     * @ORM\Column(name="approval_batch_no", type="integer")
     */
    protected $approvalBatchNo;

    /**
     * @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="HealthFacilityEntity", inversedBy="convertInventories")
     * @ORM\JoinColumn(name="health_facility_id", referencedColumnName="id")
     */
    protected $healthFacility;

    /**
     * @ORM\OneToMany(targetEntity="ConvertInventoryItemEntity", mappedBy="convertInventory")
     */
    protected $convertInventoryItems;

    /**
     * @ORM\OneToMany(targetEntity="ConvertInventoryItemBiEntity", mappedBy="convertInventory")
     */
    protected $convertInventoryItemBis;

    /**
     * @ORM\OneToMany(targetEntity="ConvertInventoryStatusEntity", mappedBy="convertInventory")
     */
    protected $convertInventoryStatuses;

//    /**
//     * @ORM\OneToMany(targetEntity="SalesOrderConvertInvEntity", mappedBy="convertInventory")
//     */
//    protected $salesOrderConvertInvs;

    /**
     * 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();

        //*Forced Approved*/
        $this->status = 'New';
//        $this->status = 'Approved';
        $this->approvalBatchNo = 0;
    }

    /**
     * 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->convertDate = $data->getConvertDateAsString();
            $this->note = $data->getNote();
            $this->status = $data->getStatus();
            $this->totalCost = $data->getTotalCost();
            $this->healthFacility = $data->getHealthFacility();
        }

        $this->convertInventoryItems = new ArrayCollection();
//        $this->convertInventoryItemBis = new ArrayCollection();
        $this->convertInventoryStatuses = new ArrayCollection();
//        $this->salesOrderConvertInvs = new ArrayCollection();
    }

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

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

        return base64_encode($this->id);
    }

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

    /**
     * Get convertInventoryNoParsed
     *
     * @return string
     */
    public function getConvertInventoryNoParsed() {

        return sprintf('%012d', $this->id);
    }

    /**
     * Get itemCtr
     *
     * @return string
     */
    public function getItemCtr() {

        return count($this->convertInventoryItems);
    }

    /**
     * Get itemBiCtr
     *
     * @return string
     */
    public function getItemBiCtr() {

        return count($this->convertInventoryItemBis);
    }

    /**
     * Get itemBiTotalCost
     *
     * @return string
     */
    public function getItemBiTotalCost() {

        $totalCost = 0;

        foreach($this->convertInventoryItemBis as $convertInventoryItemBi) {
            $totalCost += $convertInventoryItemBi->getTotalCost();
        }

        return $totalCost;
    }

    /**
     * @param array $userData
     *
     * @return boolean
     */
    public function canApproveByUser($userData) {

        $canApproveByUser = false;
        $isAlreadyApproved = false;

        switch($this->status) {
            case 'Pending Admin Approval':

                if(in_array($userData['type'], array('Super Admin', 'Admin'))) {

                    foreach($this->convertInventoryStatuses as $convertInventoryStatus) {
                        if($this->approvalBatchNo === $convertInventoryStatus->getBatchNo() && $convertInventoryStatus->getStatus() === 'Pending Admin Approval' && $convertInventoryStatus->getUser() && $convertInventoryStatus->getUser()->getId() === $userData['id'])
                            $isAlreadyApproved = true;
                    }

                    if(!$isAlreadyApproved) $canApproveByUser = true;
                }

                break;
        }

        return $canApproveByUser;
    }

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

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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Add convertInventoryItems
     *
     * @param \RxConnexionBundle\Entity\ConvertInventoryItemEntity $convertInventoryItems
     * @return ConvertInventoryEntity
     */
    public function addConvertInventoryItem(\RxConnexionBundle\Entity\ConvertInventoryItemEntity $convertInventoryItems)
    {
        $this->convertInventoryItems[] = $convertInventoryItems;

        return $this;
    }

    /**
     * Remove convertInventoryItems
     *
     * @param \RxConnexionBundle\Entity\ConvertInventoryItemEntity $convertInventoryItems
     */
    public function removeConvertInventoryItem(\RxConnexionBundle\Entity\ConvertInventoryItemEntity $convertInventoryItems)
    {
        $this->convertInventoryItems->removeElement($convertInventoryItems);
    }

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

    /**
     * Add convertInventoryItemBis
     *
     * @param \RxConnexionBundle\Entity\ConvertInventoryItemBiEntity $convertInventoryItemBis
     * @return ConvertInventoryEntity
     */
    public function addConvertInventoryItemBi(\RxConnexionBundle\Entity\ConvertInventoryItemBiEntity $convertInventoryItemBis)
    {
        $this->convertInventoryItemBis[] = $convertInventoryItemBis;
    
        return $this;
    }

    /**
     * Remove convertInventoryItemBis
     *
     * @param \RxConnexionBundle\Entity\ConvertInventoryItemBiEntity $convertInventoryItemBis
     */
    public function removeConvertInventoryItemBi(\RxConnexionBundle\Entity\ConvertInventoryItemBiEntity $convertInventoryItemBis)
    {
        $this->convertInventoryItemBis->removeElement($convertInventoryItemBis);
    }

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

    /**
     * Set totalCost
     *
     * @param string $totalCost
     * @return ConvertInventoryEntity
     */
    public function setTotalCost($totalCost)
    {
        $this->totalCost = str_replace(',' , '', $totalCost);
    
        return $this;
    }

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

    /**
     * Set approvalBatchNo
     *
     * @param integer $approvalBatchNo
     * @return ConvertInventoryEntity
     */
    public function setApprovalBatchNo($approvalBatchNo)
    {
        $this->approvalBatchNo = $approvalBatchNo;
    
        return $this;
    }

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

    /**
     * Add convertInventoryStatuses
     *
     * @param \RxConnexionBundle\Entity\ConvertInventoryStatusEntity $convertInventoryStatuses
     * @return ConvertInventoryEntity
     */
    public function addConvertInventoryStatus(\RxConnexionBundle\Entity\ConvertInventoryStatusEntity $convertInventoryStatuses)
    {
        $this->convertInventoryStatuses[] = $convertInventoryStatuses;
    
        return $this;
    }

    /**
     * Remove convertInventoryStatuses
     *
     * @param \RxConnexionBundle\Entity\ConvertInventoryStatusEntity $convertInventoryStatuses
     */
    public function removeConvertInventoryStatus(\RxConnexionBundle\Entity\ConvertInventoryStatusEntity $convertInventoryStatuses)
    {
        $this->convertInventoryStatuses->removeElement($convertInventoryStatuses);
    }

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

    /**
     * Set healthFacility
     *
     * @param \RxConnexionBundle\Entity\HealthFacilityEntity $healthFacility
     *
     * @return ConvertInventoryEntity
     */
    public function setHealthFacility(\RxConnexionBundle\Entity\HealthFacilityEntity $healthFacility = null)
    {
        $this->healthFacility = $healthFacility;

        return $this;
    }

    /**
     * Get healthFacility
     *
     * @return \RxConnexionBundle\Entity\HealthFacilityEntity
     */
    public function getHealthFacility()
    {
        return $this->healthFacility;
    }
}
