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

class TransferInventoryEntity
{
    const STATUS_NEW = 'New';
    const STATUS_PENDING_APPROVAL = 'Pending Approval';
    const STATUS_APPROVED = 'Approved';
    const STATUS_REJECTED = 'Rejected';
    const STATUS_FOR_RECEIVING = 'For Receiving';
    const STATUS_COMPLETED = 'Completed';

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

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

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

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

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

    /**
     * @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="RxConnexionBundle\Entity\HealthFacilityEntity", inversedBy="transferInventoriesFrom")
     * @ORM\JoinColumn(name="branch_id_from", referencedColumnName="id")
     */
    protected $branchFrom;

    /**
     * @ORM\ManyToOne(targetEntity="LocationEntity", inversedBy="transferInventoryLocationsFrom")
     * @ORM\JoinColumn(name="location_id_from", referencedColumnName="id")
     */
    protected $locationFrom;

    /**
     * @ORM\ManyToOne(targetEntity="RxConnexionBundle\Entity\HealthFacilityEntity", inversedBy="transferInventoriesTo")
     * @ORM\JoinColumn(name="branch_id_to", referencedColumnName="id")
     */
    protected $branchTo;

    /**
     * @ORM\ManyToOne(targetEntity="LocationEntity", inversedBy="transferInventoryLocationsTo")
     * @ORM\JoinColumn(name="location_id_to", referencedColumnName="id")
     */
    protected $locationTo;

//    /**
//     * @ORM\ManyToOne(targetEntity="DepartmentEntity", inversedBy="transferInventories")
//     * @ORM\JoinColumn(name="department_id", referencedColumnName="id")
//     */
//    protected $department;

    /**
     * @ORM\ManyToOne(targetEntity="RxConnexionBundle\Entity\AccountUserEntity", inversedBy="transferInventories")
     * @ORM\JoinColumn(name="user_to", referencedColumnName="id")
     */
    protected $accountUser;

    /**
     * @ORM\OneToMany(targetEntity="TransferInventoryItemEntity", mappedBy="transferInventory", cascade={"remove"})
     */
    protected $transferInventoryItems;

    /**
     * @ORM\OneToMany(targetEntity="TransferInventoryDeliveryEntity", mappedBy="transferInventory")
     */
    protected $transferInventoryDeliveries;

    /**
     * @ORM\OneToMany(targetEntity="RxConnexionBundle\Entity\TransferInventoryStatusEntity", mappedBy="transferInventory")
     */
    protected $transferInventoryStatuses;

    /**
     * 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 = 'New';
        $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->transferDate = $data->getTransferDateAsString();
            $this->note = $data->getNote();
            $this->status = $data->getStatus();
            $this->branchFrom = $data->getBranchFrom();
            $this->branchTo = $data->getBranchTo();
            $this->locationFrom = $data->getLocationFrom();
            $this->locationTo = $data->getLocationTo();
            $this->accountUser = $data->getAccountUser();
        }

//        $this->transferInventoryMatReqs = new ArrayCollection();
//        $this->transferInventoryStatuses = new ArrayCollection();
        $this->transferInventoryItems = new ArrayCollection();
        $this->transferInventoryDeliveries = new ArrayCollection();
//        $this->projectManagementTransferInventoryEgresses = new ArrayCollection();
//        $this->projectManagementTransferInventoryIngresses = new ArrayCollection();
    }

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

//    /**
//     * Get relatedProject
//     *
//     * @return ProjectManagementEntity|null
//    */
//    public function getRelatedProject(){
//        $pm = null;
//        foreach($this->projectManagementTransferInventoryEgresses as $egress){
//            return $egress->getProjectManagement();
//        }
//        foreach($this->projectManagementTransferInventoryIngresses as $ingress){
//            return $ingress->getProjectManagement();
//        }
//        return $pm;
//    }


    /**
     * Get distinctDeliveredItems
     *
     * @return array
     */
    public function getDistinctDeliveredItems(){
        /**
         * @var TransferInventoryDeliveryItemEntity $transferInventoryDeliveryItem
         * @var TransferInventoryDeliveryEntity $transferInventoryDelivery
         */
        $items = array();
        $ids = array();
        foreach($this->transferInventoryDeliveries as $transferInventoryDelivery){
            foreach($transferInventoryDelivery->getTransferInventoryDeliveryItems() as $transferInventoryDeliveryItem){
                $item = $transferInventoryDeliveryItem->getTransferInventoryItem()->getItem();
                if(!in_array($item->getId(), $ids)){
                    $ids[] = $item->getId();
                    $items[] = $item;
                }
            }
        }
        return array(
            'ids' => $ids,
            'obj' => $items
        );
    }


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

        return base64_encode($this->id);
    }

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

    /**
     * Get transferInventoryNoParsed
     *
     * @return string
     */
    public function getTransferInventoryNoParsed() {

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

    /**
     * Get totalQuantity
     *
     * @return string
     */
    public function getTotalQuantity() {

        $totalQuantity = 0;
        foreach($this->transferInventoryItems as $transferInventoryItem) {
            if($transferInventoryItem->getIsConsumable() == false && $this->getBranchFrom()->getDescription() == BranchEntity::DESCRIPTION_PROJECT && $this->getBranchTo()->getDescription() == BranchEntity::DESCRIPTION_RCC ){
                $totalQuantity += $transferInventoryItem->getQuantity();
            }
        }

        return $totalQuantity;
    }

    /**
     * Get totalQuantityReceived
     *
     * @return string
     */
    public function getTotalQuantityReceived() {

        $totalQuantityReceived = 0;

        foreach($this->transferInventoryItems as $transferInventoryItem) {
            if($transferInventoryItem->getIsConsumable() == false && $this->getBranchFrom()->getDescription() == BranchEntity::DESCRIPTION_PROJECT && $this->getBranchTo()->getDescription() == BranchEntity::DESCRIPTION_RCC ){
                $totalQuantityReceived += $transferInventoryItem->getQuantityReceived();
            }
        }

        return $totalQuantityReceived;
    }

//    /**
//     * Get hasMatReqById
//     *
//     * @param $id
//     * @return boolean
//     */
//    public function getHasMatReqById($id) {
//
//        if($this->transferInventoryMatReqs->count()) {
//            foreach($this->transferInventoryMatReqs as $transferInventoryMatReq) {
//                if($id == $transferInventoryMatReq->getMaterialRequisition()->getId()) {
//                    return true;
//                }
//            }
//        }
//
//        return false;
//    }

//    /**
//     * Get isApprovedByStatusAndUserFullName
//     *
//     * @param $status
//     * @param $userFullName
//     * @return boolean
//     */
//    public function isApprovedByStatusAndUserFullName($status, $userFullName) {
//
//        $userFullName = strtoupper($userFullName);
//
//        if($this->transferInventoryStatuses->count()) {
//            foreach($this->transferInventoryStatuses as $transferInventoryStatus) {
//                if($status === $transferInventoryStatus->getStatus() && 'Approved' === $transferInventoryStatus->getApprovalDescription() &&
//                    $userFullName === $transferInventoryStatus->getUser()->getFullNameToUpper()) {
//                    return true;
//                }
//            }
//        }
//
//        return false;
//    }

    /**
     * Get originAsString
     *
     * @return string
     */
    public function getOriginAsString(){
        $str = $this->getBranchFrom()->getDescription();
        if($this->getBranchFrom() && $this->getLocationFrom()){
            $str = $this->getBranchFrom()->getDescription() . " (" . $this->getLocationFrom()->getDescription() . ")";
        }
        return $str;
    }

    /**
     * Get destinationAsString
     *
     * @return string
     */
    public function getDestinationAsString(){
        $str = $this->getBranchTo()->getDescription();
        if($this->getBranchTo() && $this->getLocationTo()){
            $str = $this->getBranchTo()->getDescription() . " (" . $this->getLocationTo()->getDescription() . ")";
        }
        return $str;
    }

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



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

    /**
     * Set transferNo
     *
     * @param string $transferNo
     *
     * @return TransferInventoryEntity
     */
    public function setTransferNo($transferNo)
    {
        $this->transferNo = $transferNo;

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set branchFrom
     *
     * @param \RxConnexionBundle\Entity\HealthFacilityEntity $branchFrom
     *
     * @return TransferInventoryEntity
     */
    public function setBranchFrom(\RxConnexionBundle\Entity\HealthFacilityEntity $branchFrom = null)
    {
        $this->branchFrom = $branchFrom;

        return $this;
    }

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

    /**
     * Set locationFrom
     *
     * @param \RxConnexionBundle\Entity\LocationEntity $locationFrom
     *
     * @return TransferInventoryEntity
     */
    public function setLocationFrom(\RxConnexionBundle\Entity\LocationEntity $locationFrom = null)
    {
        $this->locationFrom = $locationFrom;

        return $this;
    }

    /**
     * Get locationFrom
     *
     * @return \RxConnexionBundle\Entity\LocationEntity
     */
    public function getLocationFrom()
    {
        return $this->locationFrom;
    }

    /**
     * Set branchTo
     *
     * @param \RxConnexionBundle\Entity\HealthFacilityEntity $branchTo
     *
     * @return TransferInventoryEntity
     */
    public function setBranchTo(\RxConnexionBundle\Entity\HealthFacilityEntity $branchTo = null)
    {
        $this->branchTo = $branchTo;

        return $this;
    }

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

    /**
     * Set locationTo
     *
     * @param \RxConnexionBundle\Entity\LocationEntity $locationTo
     *
     * @return TransferInventoryEntity
     */
    public function setLocationTo(\RxConnexionBundle\Entity\LocationEntity $locationTo = null)
    {
        $this->locationTo = $locationTo;

        return $this;
    }

    /**
     * Get locationTo
     *
     * @return \RxConnexionBundle\Entity\LocationEntity
     */
    public function getLocationTo()
    {
        return $this->locationTo;
    }

    /**
     * Set user
     *
     * @param \RxConnexionBundle\Entity\UserEntity $user
     *
     * @return TransferInventoryEntity
     */
    public function setUser(\RxConnexionBundle\Entity\UserEntity $user = null)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return \RxConnexionBundle\Entity\UserEntity
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * Add transferInventoryItem
     *
     * @param \RxConnexionBundle\Entity\TransferInventoryItemEntity $transferInventoryItem
     *
     * @return TransferInventoryEntity
     */
    public function addTransferInventoryItem(\RxConnexionBundle\Entity\TransferInventoryItemEntity $transferInventoryItem)
    {
        $this->transferInventoryItems[] = $transferInventoryItem;

        return $this;
    }

    /**
     * Remove transferInventoryItem
     *
     * @param \RxConnexionBundle\Entity\TransferInventoryItemEntity $transferInventoryItem
     */
    public function removeTransferInventoryItem(\RxConnexionBundle\Entity\TransferInventoryItemEntity $transferInventoryItem)
    {
        $this->transferInventoryItems->removeElement($transferInventoryItem);
    }

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

    /**
     * Add transferInventoryDelivery
     *
     * @param \RxConnexionBundle\Entity\TransferInventoryDeliveryEntity $transferInventoryDelivery
     *
     * @return TransferInventoryEntity
     */
    public function addTransferInventoryDelivery(\RxConnexionBundle\Entity\TransferInventoryDeliveryEntity $transferInventoryDelivery)
    {
        $this->transferInventoryDeliveries[] = $transferInventoryDelivery;

        return $this;
    }

    /**
     * Remove transferInventoryDelivery
     *
     * @param \RxConnexionBundle\Entity\TransferInventoryDeliveryEntity $transferInventoryDelivery
     */
    public function removeTransferInventoryDelivery(\RxConnexionBundle\Entity\TransferInventoryDeliveryEntity $transferInventoryDelivery)
    {
        $this->transferInventoryDeliveries->removeElement($transferInventoryDelivery);
    }

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

    /**
     * Add transferInventoryStatus
     *
     * @param \RxConnexionBundle\Entity\TransferInventoryStatusEntity $transferInventoryStatus
     *
     * @return TransferInventoryEntity
     */
    public function addTransferInventoryStatus(\RxConnexionBundle\Entity\TransferInventoryStatusEntity $transferInventoryStatus)
    {
        $this->transferInventoryStatuses[] = $transferInventoryStatus;

        return $this;
    }

    /**
     * Remove transferInventoryStatus
     *
     * @param \RxConnexionBundle\Entity\TransferInventoryStatusEntity $transferInventoryStatus
     */
    public function removeTransferInventoryStatus(\RxConnexionBundle\Entity\TransferInventoryStatusEntity $transferInventoryStatus)
    {
        $this->transferInventoryStatuses->removeElement($transferInventoryStatus);
    }

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

    /**
     * Set accountUser
     *
     * @param \RxConnexionBundle\Entity\AccountUserEntity $accountUser
     *
     * @return TransferInventoryEntity
     */
    public function setAccountUser(\RxConnexionBundle\Entity\AccountUserEntity $accountUser = null)
    {
        $this->accountUser = $accountUser;

        return $this;
    }

    /**
     * Get accountUser
     *
     * @return \RxConnexionBundle\Entity\AccountUserEntity
     */
    public function getAccountUser()
    {
        return $this->accountUser;
    }
}
