<?php

namespace VGM\WOBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * WorkOrder
 */
class WorkOrder
{
    /**
     * @var integer
     */
    private $id;
    /**
     * @var string
     */
    private $number;
    /**
     * @var integer
     */
    private $stock_quantity;
    /**
     * @var \DateTime
     */
    private $created_at;
    /**
     * @var \DateTime
     */
    private $started_at;
    /**
     * @var boolean
     */
    private $ready;
    /**
     * @var \DateTime
     */
    private $ready_at;
    /**
     * @var float
     */
    private $price;
    /**
     * @var integer
     */
    private $x_price = '0';
    /**
     * @var integer
     */
    private $status;
    /**
     * @var \DateTime
     */
    private $delivery_date;
    /**
     * @var string
     */
    private $delivery_address;
    /**
     * @var string
     */
    private $delivery_city;
    /**
     * @var string
     */
    private $delivery_state;
    /**
     * @var string
     */
    private $delivery_zipcode;
    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $images;
    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $events;
    /**
     * @var \VGM\WOBundle\Entity\SaleType
     */
    private $saletype;
    /**
     * @var \VGM\WOBundle\Entity\WorkType
     */
    private $worktype;
    /**
     * @var \VGM\WOBundle\Entity\Client
     */
    private $client;
    /**
     * @var \VGM\WOBundle\Entity\Equipment
     */
    private $equipment;
    /**
     * @var \VGM\WOBundle\Entity\Worker
     */
    private $supervisor;
    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $stocks_full;
    /**
     * @var \VGM\WOBundle\Entity\Worker
     */
    private $created_by;
    /**
     * @var string
     */
    private $origin;
    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $shifts;
    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $tasks;
    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $obs;
    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $invoice_refs;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->images = new \Doctrine\Common\Collections\ArrayCollection();
        $this->events = new \Doctrine\Common\Collections\ArrayCollection();
    }
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Set number
     *
     * @param string $number
     * @return WorkOrder
     */
    public function setNumber($number)
    {
        $this->number = $number;

        return $this;
    }
    /**
     * Get number
     *
     * @return string 
     */
    public function getNumber()
    {
        return $this->number;
    }
    /**
     * Set stock_quantity
     *
     * @param integer $stockQuantity
     * @return WorkOrder
     */
    public function setStockQuantity($stockQuantity)
    {
        if($stockQuantity > count($this->getStocksFull()))
            $this->stock_quantity = $stockQuantity;
        else
            $this->stock_quantity = count($this->getStocksFull());

        return $this;
    }
    /**
     * Get stock_quantity
     *
     * @return integer 
     */
    public function getStockQuantity()
    {
        if($this->stock_quantity == 0){
            $this->setStockQuantity(count($this->getStocksFull()));
        }
        return $this->stock_quantity;
    }
    /**
     * Set created_at
     *
     * @param \DateTime $createdAt
     * @return WorkOrder
     */
    public function setCreatedAt($createdAt)
    {
        $this->created_at = $createdAt;

        return $this;
    }
    /**
     * Get created_at
     *
     * @return \DateTime 
     */
    public function getCreatedAt()
    {
        return $this->created_at;
    }
    /**
     * Set started_at
     *
     * @param \DateTime $startedAt
     * @return WorkOrder
     */
    public function setStartedAt($startedAt)
    {
        $this->started_at = $startedAt;

        return $this;
    }
    /**
     * Get started_at
     *
     * @return \DateTime 
     */
    public function getStartedAt()
    {
        return $this->started_at;
    }
    /**
     * Set ready
     *
     * @param boolean $ready
     * @return WorkOrder
     */
    public function setReady($ready)
    {
        $this->ready = $ready;

        return $this;
    }
    /**
     * Get ready
     *
     * @return boolean 
     */
    public function getReady()
    {
        return $this->ready;
    }
    /**
     * Set ready_at
     *
     * @param \DateTime $readyAt
     * @return WorkOrder
     */
    public function setReadyAt($readyAt)
    {
        $this->ready_at = $readyAt;

        return $this;
    }
    /**
     * Get ready_at
     *
     * @return \DateTime 
     */
    public function getReadyAt()
    {
        return $this->ready_at;
    }
    /**
     * Set price
     *
     * @param float $price
     * @return WorkOrder
     */
    public function setPrice($price)
    {
        $this->price = $price;

        return $this;
    }
    /**
     * Get price
     *
     * @return float 
     */
    public function getPrice()
    {
        return $this->price;
    }
    /**
     * Set status
     *
     * @param integer $status
     * @return WorkOrder
     */
    public function setStatus($status)
    {
        $final_status = "Current";
        if(count($this->stocks_full) > 0){
            $status = $this->getStatusFromStocks();
            if($status)
                $final_status = $status;
            $this->ready_at = $this->getReadyAtFromStocks();
        }
        else{
            if($status != null){
                if(($status != "Current" || $status != "Hold"))
                    if(($this->status == "Current" || $this->status == "Hold")){
                        $this->ready_at = new \DateTime();
                }
                else
                    $this->ready_at = null;
                $final_status = $status;
            }
        }
        $this->status = $final_status;
        return $this;
    }
    /**
     * Get status
     *
     * @return integer 
     */
    public function getStatus()
    {
        return $this->status;
    }
    /**
     * Set delivery_date
     *
     * @param \DateTime $deliveryDate
     * @return WorkOrder
     */
    public function setDeliveryDate($deliveryDate)
    {
        $this->delivery_date = $deliveryDate;

        return $this;
    }
    /**
     * Get delivery_date
     *
     * @return \DateTime 
     */
    public function getDeliveryDate()
    {
        return $this->delivery_date;
    }
    /**
     * Set delivery_address
     *
     * @param string $deliveryAddress
     * @return WorkOrder
     */
    public function setDeliveryAddress($deliveryAddress)
    {
        $this->delivery_address = $deliveryAddress;

        return $this;
    }
    /**
     * Get delivery_address
     *
     * @return string 
     */
    public function getDeliveryAddress()
    {
        return $this->delivery_address;
    }
    /**
     * Set delivery_city
     *
     * @param string $deliveryCity
     * @return WorkOrder
     */
    public function setDeliveryCity($deliveryCity)
    {
        $this->delivery_city = $deliveryCity;

        return $this;
    }
    /**
     * Get delivery_city
     *
     * @return string 
     */
    public function getDeliveryCity()
    {
        return $this->delivery_city;
    }
    /**
     * Set delivery_state
     *
     * @param string $deliveryState
     * @return WorkOrder
     */
    public function setDeliveryState($deliveryState)
    {
        $this->delivery_state = $deliveryState;

        return $this;
    }
    /**
     * Get delivery_state
     *
     * @return string 
     */
    public function getDeliveryState()
    {
        return $this->delivery_state;
    }
    /**
     * Set delivery_zipcode
     *
     * @param string $deliveryZipcode
     * @return WorkOrder
     */
    public function setDeliveryZipcode($deliveryZipcode)
    {
        $this->delivery_zipcode = $deliveryZipcode;

        return $this;
    }
    /**
     * Get delivery_zipcode
     *
     * @return string 
     */
    public function getDeliveryZipcode()
    {
        return $this->delivery_zipcode;
    }
    /**
     * Add images
     *
     * @param \VGM\WOBundle\Entity\Image $images
     * @return WorkOrder
     */
    public function addImage(\VGM\WOBundle\Entity\Image $images)
    {
        $this->images[] = $images;

        return $this;
    }
    /**
     * Remove images
     *
     * @param \VGM\WOBundle\Entity\Image $images
     */
    public function removeImage(\VGM\WOBundle\Entity\Image $images)
    {
        $this->images->removeElement($images);
    }
    /**
     * Get images
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getImages()
    {
        return $this->images;
    }
    /**
     * Add events
     *
     * @param \VGM\WOBundle\Entity\WOEvent $events
     * @return WorkOrder
     */
    public function addEvent(\VGM\WOBundle\Entity\WOEvent $events)
    {
        $this->events[] = $events;

        return $this;
    }
    /**
     * Remove events
     *
     * @param \VGM\WOBundle\Entity\WOEvent $events
     */
    public function removeEvent(\VGM\WOBundle\Entity\WOEvent $events)
    {
        $this->events->removeElement($events);
    }
    /**
     * Get events
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getEvents()
    {
        return $this->events;
    }
    /**
     * Set saletype
     *
     * @param \VGM\WOBundle\Entity\SaleType $saletype
     * @return WorkOrder
     */
    public function setSaletype(\VGM\WOBundle\Entity\SaleType $saletype = null)
    {
        $this->saletype = $saletype;

        return $this;
    }
    /**
     * Get saletype
     *
     * @return \VGM\WOBundle\Entity\SaleType 
     */
    public function getSaletype()
    {
        return $this->saletype;
    }
    /**
     * Set worktype
     *
     * @param \VGM\WOBundle\Entity\WorkType $worktype
     * @return WorkOrder
     */
    public function setWorktype(\VGM\WOBundle\Entity\WorkType $worktype = null)
    {
        $this->worktype = $worktype;

        return $this;
    }
    /**
     * Get worktype
     *
     * @return \VGM\WOBundle\Entity\WorkType 
     */
    public function getWorktype()
    {
        return $this->worktype;
    }
    /**
     * Set client
     *
     * @param \VGM\WOBundle\Entity\Client $client
     * @return WorkOrder
     */
    public function setClient(\VGM\WOBundle\Entity\Client $client = null)
    {
        $this->client = $client;

        return $this;
    }
    /**
     * Get client
     *
     * @return \VGM\WOBundle\Entity\Client 
     */
    public function getClient()
    {
        return $this->client;
    }
    /**
     * Set equipment
     *
     * @param \VGM\WOBundle\Entity\Equipment $equipment
     * @return WorkOrder
     */
    public function setEquipment(\VGM\WOBundle\Entity\Equipment $equipment = null)
    {
        $this->equipment = $equipment;

        return $this;
    }
    /**
     * Get equipment
     *
     * @return \VGM\WOBundle\Entity\Equipment 
     */
    public function getEquipment()
    {
        return $this->equipment;
    }
    /**
     * Set supervisor
     *
     * @param \VGM\WOBundle\Entity\Worker $supervisor
     * @return WorkOrder
     */
    public function setSupervisor(\VGM\WOBundle\Entity\Worker $supervisor = null)
    {
        $this->supervisor = $supervisor;

        return $this;
    }
    /**
     * Get supervisor
     *
     * @return \VGM\WOBundle\Entity\Worker 
     */
    public function getSupervisor()
    {
        return $this->supervisor;
    }
    /**
     * Add stocks_full
     *
     * @param \VGM\WOBundle\Entity\WorkOrderStockAssociation $stocksFull
     * @return WorkOrder
     */
    public function addStocksFull(\VGM\WOBundle\Entity\WorkOrderStockAssociation $stocksFull)
    {
        $this->stocks_full[] = $stocksFull;

        return $this;
    }
    /**
     * Remove stocks_full
     *
     * @param \VGM\WOBundle\Entity\WorkOrderStockAssociation $stocksFull
     */
    public function removeStocksFull(\VGM\WOBundle\Entity\WorkOrderStockAssociation $stocksFull)
    {
        $this->stocks_full->removeElement($stocksFull);
    }
    /**
     * Get stocks_full
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getStocksFull()
    {
        return $this->stocks_full;
    }
    /**
     * Set createdBy
     *
     * @param \VGM\WOBundle\Entity\Worker $createdBy
     *
     * @return WorkOrder
     */
    public function setCreatedBy(\VGM\WOBundle\Entity\Worker $createdBy = null)
    {
        $this->created_by = $createdBy;

        return $this;
    }
    /**
     * Get createdBy
     *
     * @return \VGM\WOBundle\Entity\Worker
     */
    public function getCreatedBy()
    {
        return $this->created_by;
    }
    /**
     * Add task
     *
     * @param \VGM\WOBundle\Entity\PlanningTask $task
     *
     * @return WorkOrder
     */
    public function addTask(\VGM\WOBundle\Entity\PlanningTask $task)
    {
        $this->tasks[] = $task;

        return $this;
    }
    /**
     * Remove task
     *
     * @param \VGM\WOBundle\Entity\PlanningTask $task
     */
    public function removeTask(\VGM\WOBundle\Entity\PlanningTask $task)
    {
        $this->tasks->removeElement($task);
    }
    /**
     * Get tasks
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getTasks()
    {
        return $this->tasks;
    }
    /**
     * Add shift
     *
     * @param \VGM\WOBundle\Entity\LaborLoop $shift
     *
     * @return WorkOrder
     */
    public function addShift(\VGM\WOBundle\Entity\LaborLoop $shift)
    {
        $this->shifts[] = $shift;

        return $this;
    }
    /**
     * Remove shift
     *
     * @param \VGM\WOBundle\Entity\LaborLoop $shift
     */
    public function removeShift(\VGM\WOBundle\Entity\LaborLoop $shift)
    {
        $this->shifts->removeElement($shift);
    }
    /**
     * Get shifts
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getShifts()
    {
        return $this->shifts;
    }
    /**
     * Set origin
     *
     * @param string $origin
     *
     * @return WorkOrder
     */
    public function setOrigin($origin)
    {
        $this->origin = $origin;

        return $this;
    }
    /**
     * Get origin
     *
     * @return string
     */
    public function getOrigin()
    {
        return $this->origin;
    }
    /**
     * Add ob
     *
     * @param \VGM\WOBundle\Entity\WorkOrderObs $ob
     *
     * @return WorkOrder
     */
    public function addOb(\VGM\WOBundle\Entity\WorkOrderObs $ob)
    {
        $this->obs[] = $ob;

        return $this;
    }
    /**
     * Remove ob
     *
     * @param \VGM\WOBundle\Entity\WorkOrderObs $ob
     */
    public function removeOb(\VGM\WOBundle\Entity\WorkOrderObs $ob)
    {
        $this->obs->removeElement($ob);
    }
    /**
     * Get obs
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getObs()
    {
        return $this->obs;
    }
    /**
     * Set xPrice
     *
     * @param integer $xPrice
     *
     * @return WorkOrder
     */
    public function setXPrice($xPrice)
    {
        $this->x_price = $xPrice;

        return $this;
    }
    /**
     * Get xPrice
     *
     * @return integer
     */
    public function getXPrice()
    {
        return $this->x_price;
    }
    /**
     * Add invoiceRef
     *
     * @param \VGM\WOBundle\Entity\InvoiceReference $invoiceRef
     *
     * @return WorkOrder
     */
    public function addInvoiceRef(\VGM\WOBundle\Entity\InvoiceReference $invoiceRef)
    {
        $this->invoice_refs[] = $invoiceRef;

        return $this;
    }
    /**
     * Remove invoiceRef
     *
     * @param \VGM\WOBundle\Entity\InvoiceReference $invoiceRef
     */
    public function removeInvoiceRef(\VGM\WOBundle\Entity\InvoiceReference $invoiceRef)
    {
        $this->invoice_refs->removeElement($invoiceRef);
    }
    /**
     * Get invoiceRefs
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getInvoiceRefs()
    {
        return $this->invoice_refs;
    }

    /**
     * @ORM\PrePersist
     */
    public function setCreatedAtValue()
    {
        $this->setCreatedAt(new \DateTime());
        $this->setStartedAtValue();
    }
    /**
     * @ORM\PreUpdate
     */
    public function setStartedAtValue(){
        /*if(count($this->stocks_full) > 0){
            $this->setStatusFromStocks();
            $status = $this->getStatusFromStocks();
            if($status)
                $this->status = $status;
            $this->ready_at = $this->getReadyAtFromStocks();
        }
        else{
            if($this->status != null)
                if($this->status == "Current" || $this->status == "Hold")
                    $this->ready_at = null;
                elseif($this->ready_at == null)
                    $this->ready_at = new \DateTime();
        }*/
    }

    /*
     * Own functions
     */
    public function getStatusFromStocks(){
        $status_from_stock = null;
        if(count($this->stocks_full) > 0){
            foreach($this->stocks_full as $sf){
                switch($sf->getStatus()){
                    case "Current":
                        return "Current";
                        break;
                    case "Hold":
                        if($status_from_stock != "Current")
                            $status_from_stock = "Hold";
                        break;
                    case "Rented":
                        switch($status_from_stock){
                            case "Hold":
                                $status_from_stock = "Hold";
                                break;
                            case "Sold":
                                $status_from_stock = "Out";
                                break;
                            default:
                                $status_from_stock = $status_from_stock != "Out" ? "Rented" : "Out";
                                break;
                        }
                        break;
                    case "Sold":
                        switch($status_from_stock){
                            case "Hold":
                                $status_from_stock = "Hold";
                                break;
                            case "Rented":
                                $status_from_stock = "Out";
                                break;
                            default:
                                $status_from_stock = $status_from_stock != "Out" ? "Sold" : "Out";
                                break;
                        }
                        break;
                    default:
                        return null;
                        break;
                }
            }
        }
        return $status_from_stock;
    }
    public function setStatusFromStocks(){
        $status_from_stock = null;
        if(count($this->stocks_full) > 0){
            foreach($this->stocks_full as $sf){
                $status = $sf->getStatus();
                if($status != null){
                    if($status == "Current"){
                        $this->status = $status;
                        return $this;
                    }
                    else{
                        if($status == "Hold")
                            $status_from_stock = "Hold";
                        elseif($status_from_stock != "Hold"){
                            if($status_from_stock != null && $status_from_stock != "Out"
                                && $status != $status_from_stock)
                                $status_from_stock = "Out";
                            else
                                $status_from_stock = $status;

                        }
                    }
                }
                else
                    return $this;
            }
            $this->status = $status_from_stock;
        }
        return $this;
    }
    public function getReadyAtFromStocks(){
        $ready_at_from_stock = null;
        if(count($this->stocks_full) > 0){
            foreach($this->stocks_full as $sf){
                if($sf->getDeliveryDate() == null)
                    return null;
                if($ready_at_from_stock == null)
                    $ready_at_from_stock = $sf->getDeliveryDate();
                elseif($sf->getDeliveryDate() > $ready_at_from_stock)
                    $ready_at_from_stock = $sf->getDeliveryDate();
            }
        }
        return $ready_at_from_stock;
    }
    public function setReadyAtFromStocks(){
        $ready_at_from_stock = null;
        if(count($this->stocks_full) > 0){
            foreach($this->stocks_full as $sf){
                if($sf->getDeliveryDate() == null){
                    $this->ready_at = null;
                    return $this;
                }
                else{
                    if($ready_at_from_stock == null)
                        $ready_at_from_stock = $sf->getDeliveryDate();
                    elseif($sf->getDeliveryDate() > $ready_at_from_stock)
                        $ready_at_from_stock = $sf->getDeliveryDate();
                }
            }
            if($this->ready_at != null && $ready_at_from_stock != null && $this->ready_at < $ready_at_from_stock)
                $this->ready_at = $ready_at_from_stock;
        }
        return $this;
    }
    /**
     * Image of first stock
     */
    public function getImageUrl()
    {
        if(count($this->stocks_full) > 0)
            return 'http://www.aeroservicios.com/system/images/'.(string)$this->stocks_full[0]->getStock()->getAsdbId().'_0.jpg';
        else
            return 'http://www.aeroservicios.com/system/images/no_image_0.jpg';
    }

    /**
     * @return mixed
     */
    public function __toString(){
        return $this->getNumber();
    }
}
