<?php 
/** 
 * Created by PhpStorm. 
 * User: parcel 
 * Date: 8/24/18 
 * Time: 7:43 PM 
 */ 
 
namespace App\Entity; 
 
 
use App\Parcels\CustomConstraints\DestinationSetError; 
use Doctrine\Common\Collections\ArrayCollection; 
use Doctrine\ORM\Mapping as ORM; 
use JMS\Serializer\Annotation as Serializer; 
use Symfony\Component\Validator\Constraints as Assert; 
 
/** 
 * @ORM\Entity(repositoryClass="App\Repository\WayBillRepository") 
 * @ORM\Table(name="way_bill") 
 */ 
class WayBill { 
 
    /** 
     * @ORM\Id 
     * @ORM\GeneratedValue(strategy="IDENTITY") 
     * @ORM\Column(type="integer") 
     */ 
    private $id; 
 
 
    /** 
     * @Assert\NotBlank(message="Please fill in parcel sender") 
     * @ORM\Column(type="string") 
     */ 
    private $senderName; 
 
    /** 
     * @Assert\NotBlank(message="Please fill in sender phone number") 
     * @Assert\Length(max="12", 
     *     min="12", 
     *     maxMessage="the sender phone number must have 12 numerals", 
     *     maxMessage="the sender phone number must have at least 12 numerals" ) 
     * @ORM\Column(type="string", length=12) 
     */ 
    private $senderPhoneNumber; 
 
    /** 
     * @Assert\NotBlank(message="Please fill in receiver name") 
     * @ORM\Column(type="string") 
     */ 
    private $receiverName; 
 
    /** 
     * @Assert\NotBlank(message="Please fill in the receiver Phone number") 
     * @Assert\Length(max="12", 
     *     min="12", 
     *     maxMessage="the phone number must have 12 numerals", 
     *     maxMessage="the phone number must have at least 12 numerals" ) 
     * @ORM\Column(type="string", length=12) 
     */ 
    private $receiverPhoneNumber; 
 
    /** 
     * @var Station 
     * 
     * @ORM\ManyToOne(targetEntity="App\Entity\Station") 
     * @ORM\JoinColumns({ 
     *   @ORM\JoinColumn(name="from_station_id", referencedColumnName="id") 
     * }) 
     */ 
    private $fromStation; 
 
    /** 
     * @var Station 
     * @DestinationSetError(message2="Destination cannot be the same as Origin Station", message1="Destination has an error Please Check again", message="Please Select Destination station") 
     * @ORM\ManyToOne(targetEntity="App\Entity\Station") 
     * @ORM\JoinColumns({ 
     *   @ORM\JoinColumn(name="to_station_id", referencedColumnName="id") 
     * }) 
     */ 
    private $toStation; 
 
 
    // * @Serializer\Type("DateTime<'Y-m-d H:i:s>") 
 
    /** 
     * @Serializer\Type("DateTime<'Y-m-d H:i:s'>") 
     * @ORM\Column(type="datetime") 
     */ 
    private $createdAt; 
 
    /** 
     * @var User 
     * @ORM\ManyToOne(targetEntity="User") 
     * @ORM\JoinColumns({ 
     *   @ORM\JoinColumn(name="created_by", referencedColumnName="id") 
     * }) 
     */ 
    private $createdBy; 
 
///* @Assert\NotBlank(message="Please enter amount") 
//* @Assert\NotNull(message="Please key in an amount") 
//* @Assert\Count(min=1, max="1", minMessage="Please enter an amount", maxMessage="Please enter only one amount") 
//* @Serializer\Type("Transaction")*/ 
 
 
    /** 
     * @var Transaction $transaction 
     * @Serializer\Type("App\Entity\Transaction") 
     * @Assert\Valid() 
     * @Assert\NotNull(message="Please Enter an amount for this transaction") 
     * @ORM\OneToOne(targetEntity="Transaction", mappedBy="wayBill", cascade={"persist", "remove"}) 
     */ 
    private $transaction; 
 
 
    /** 
     * @Assert\NotBlank(message="Please enter a parcel") 
     * @Assert\NotNull(message="Please key in a parcel") 
     * @Assert\Count(min=1, minMessage="Please enter a parcel") 
     * @Serializer\Type("ArrayCollection<App\Entity\Parcel>") 
     * @ORM\OneToMany(targetEntity="App\Entity\Parcel", mappedBy="waybill", cascade={"persist", "remove"}) 
     */ 
    private $parcels; 
 
 
    /** 
     * @ORM\Column(type="boolean") 
     */ 
    private $isCollected; 
 
 
    /** 
     * @ORM\Column(type="boolean") 
     */ 
    private $isReceived; 
 
 
    /** 
     * @var User 
     * @ORM\ManyToOne(targetEntity="User") 
     * @ORM\JoinColumns({ 
     *   @ORM\JoinColumn(name="received_by", referencedColumnName="id") 
     * }) 
     */ 
    private $receivedBy; 
 
 
    /** 
     * @var Organization 
     * @ORM\ManyToOne(targetEntity="App\Entity\Organization") 
     * @ORM\JoinColumns({ 
     *   @ORM\JoinColumn(name="organization_id", referencedColumnName="id", nullable=true) 
     * }) 
     */ 
    private $organization; 
 
 
 
    /** 
     * @Serializer\Type("App\Entity\CollectedParcel") 
     * @ORM\OneToOne(targetEntity="App\Entity\CollectedParcel", mappedBy="waybill") 
     */ 
    private $collectedParcel; 
 
    /** 
     * @ORM\Column(type="datetime", nullable=true) 
     */ 
    private $receivedAt; 
 
 
 
    /** 
     * @ORM\Column(type="integer") 
     */ 
    private $percelCount; 
 
 
    /** 
     * @ORM\Column(type="float") 
     */ 
    private $longitude; 
 
    /** 
     * @ORM\Column(type="float") 
     */ 
    private $latitude; 
 
    /** 
     * @Assert\NotBlank(message="Please enter the parcel value") 
     * @ORM\Column(type="integer") 
     */ 
    private $parcelValue; 
    public function __construct(){ 
        $this->parcels = new ArrayCollection(); 
    } 
 
    /** 
     * @return mixed 
     */ 
    public function getId() 
    { 
        return $this->id; 
    } 
 
    /** 
     * @param mixed $id 
     */ 
    public function setId($id) 
    { 
        $this->id = $id; 
    } 
 
    /** 
     * @return mixed 
     */ 
    public function getSenderName() 
    { 
        return $this->senderName; 
    } 
 
    /** 
     * @param mixed $senderName 
     */ 
    public function setSenderName($senderName) 
    { 
        $this->senderName = $senderName; 
    } 
 
    /** 
     * @return mixed 
     */ 
    public function getSenderPhoneNumber() 
    { 
        return $this->senderPhoneNumber; 
    } 
 
    /** 
     * @param mixed $senderPhoneNumber 
     */ 
    public function setSenderPhoneNumber($senderPhoneNumber) 
    { 
        $this->senderPhoneNumber = $senderPhoneNumber; 
    } 
 
    /** 
     * @return mixed 
     */ 
    public function getReceiverName() 
    { 
        return $this->receiverName; 
    } 
 
    /** 
     * @param mixed $receiverName 
     */ 
    public function setReceiverName($receiverName) 
    { 
        $this->receiverName = $receiverName; 
    } 
 
    /** 
     * @return mixed 
     */ 
    public function getReceiverPhoneNumber() 
    { 
        return $this->receiverPhoneNumber; 
    } 
 
    /** 
     * @param mixed $receiverPhoneNumber 
     */ 
    public function setReceiverPhoneNumber($receiverPhoneNumber) 
    { 
        $this->receiverPhoneNumber = $receiverPhoneNumber; 
    } 
 
    /** 
     * @return Station 
     */ 
    public function getFromStation() 
    { 
        return $this->fromStation; 
    } 
 
    /** 
     * @param Station $fromStation 
     */ 
    public function setFromStation($fromStation) 
    { 
        $this->fromStation = $fromStation; 
    } 
 
    /** 
     * @return Station 
     */ 
    public function getToStation() 
    { 
        return $this->toStation; 
    } 
 
    /** 
     * @param Station $toStation 
     */ 
    public function setToStation($toStation) 
    { 
        $this->toStation = $toStation; 
    } 
 
 
 
    /** 
     * @return mixed 
     */ 
    public function getIsCollected() 
    { 
        return $this->isCollected; 
    } 
 
    /** 
     * @param mixed $isCollected 
     */ 
    public function setIsCollected($isCollected) 
    { 
        $this->isCollected = $isCollected; 
    } 
 
    /** 
     * @return mixed 
     */ 
    public function getCreatedAt() 
    { 
        return $this->createdAt; 
    } 
 
    /** 
     * @param mixed $createdAt 
     */ 
    public function setCreatedAt($createdAt) 
    { 
        $this->createdAt = $createdAt; 
    } 
 
    /** 
     * @return Organization 
     */ 
    public function getOrganization() 
    { 
        return $this->organization; 
    } 
 
    /** 
     * @param Organization $organization 
     */ 
    public function setOrganization($organization) 
    { 
        $this->organization = $organization; 
    } 
 
 
 
 
    /** 
     * @return User 
     */ 
    public function getCreatedBy() 
    { 
        return $this->createdBy; 
    } 
 
    /** 
     * @param User $createdBy 
     */ 
    public function setCreatedBy($createdBy) 
    { 
        $this->createdBy = $createdBy; 
    } 
 
    /** 
     * @return mixed 
     */ 
    public function getParcels() 
    { 
        return $this->parcels; 
    } 
 
    /** 
     * @param mixed $parcels 
     */ 
    public function setParcels($parcels) 
    { 
        $this->parcels = $parcels; 
    } 
 
    public function addParcel(Parcel $parcel) { 
 
//        $parcel->setInOfficeFrom(new \DateTime()); 
//        $parcel->setIsReceived(false); 
//        $parcel->setIsEnRoute(false); 
//        $parcel->setIsCollected(false); 
        $parcel->setCreatedAt(new \DateTime()); 
        $parcel->setWaybill($this); 
        $parcel->setIsInDelivery(false); 
        $this->parcels->add($parcel); 
    } 
 
 
    public function removeParcel(Parcel $parcel) 
    { 
        $this->parcels->removeElement($parcel); 
    } 
 
    /** 
     * @return Transaction 
     */ 
    public function getTransaction() 
    { 
        return $this->transaction; 
    } 
 
    /** 
     * @param Transaction $transaction 
     */ 
    public function setTransaction(Transaction $transaction) 
    { 
        $this->transaction = $transaction; 
    } 
 
    public function addTransaction(DailyAccount $dailyAccount) { 
 
        $this->transaction->setTaxPercentage(16); 
        $taxPercentage = $this->transaction->getTaxPercentage(); 
        $taxAmount = round((($this->transaction->getAmount()*$taxPercentage)/116), 2); 
        $balance = round(($this->transaction->getAmount() - $taxAmount), 2); 
 
        $this->transaction->setStationExpenses(0); 
        $this->transaction->setExpenses(0); 
        $this->transaction->setOrganization($this->getOrganization()); 
        $this->transaction->setCreatedBy($this->createdBy); 
        $this->transaction->setCreatedAt($this->createdAt); 
        $this->transaction->setWayBill($this); 
 
        $this->transaction->setTaxAmount($taxAmount); 
        $this->transaction->setGrossAmount($this->transaction->getAmount()); 
        $this->transaction->setBalance($this->transaction->getAmount()); 
 
        $this->transaction->setStationBalance($this->transaction->getAmount()); 
        $this->transaction->setDailyAccount($dailyAccount); 
        $this->transaction->setIsCancelled(false); 
        $this->transaction->setIsComplete(false); 
        $this->transaction->setPaidBy('CASH'); 
        $this->transaction->setIsPaid(false); 
        $this->transaction->setMpesaAmount(0); 
        $this->transaction->setCashAmount(0); 
        $this->transaction->setIsFinal(false); 
 
    } 
 
    /** 
     * @return mixed 
     */ 
    public function getPercelCount() 
    { 
        return $this->percelCount; 
    } 
 
    /** 
     * @param mixed $percelCount 
     */ 
    public function setPercelCount($percelCount) 
    { 
        $this->percelCount = $percelCount; 
    } 
 
    /** 
     * @return mixed 
     */ 
    public function getCollectedParcel() 
    { 
        return $this->collectedParcel; 
    } 
 
    /** 
     * @param mixed $collectedParcel 
     */ 
    public function setCollectedParcel($collectedParcel) 
    { 
        $this->collectedParcel = $collectedParcel; 
    } 
 
    /** 
     * @return mixed 
     */ 
    public function getIsReceived() 
    { 
        return $this->isReceived; 
    } 
 
    /** 
     * @param mixed $isReceived 
     */ 
    public function setIsReceived($isReceived) 
    { 
        $this->isReceived = $isReceived; 
    } 
 
    /** 
     * @return User 
     */ 
    public function getReceivedBy() 
    { 
        return $this->receivedBy; 
    } 
 
    /** 
     * @param User $receivedBy 
     */ 
    public function setReceivedBy($receivedBy) 
    { 
        $this->receivedBy = $receivedBy; 
    } 
 
    /** 
     * @return mixed 
     */ 
    public function getReceivedAt() 
    { 
        return $this->receivedAt; 
    } 
 
    /** 
     * @param mixed $receivedAt 
     */ 
    public function setReceivedAt($receivedAt) 
    { 
        $this->receivedAt = $receivedAt; 
    } 
 
    /** 
     * @return mixed 
     */ 
    public function getParcelValue() 
    { 
        return $this->parcelValue; 
    } 
 
    /** 
     * @param mixed $parcelValue 
     */ 
    public function setParcelValue($parcelValue): void 
    { 
        $this->parcelValue = $parcelValue; 
    } 
 
    /** 
     * @return mixed 
     */ 
    public function getLongitude() 
    { 
        return $this->longitude; 
    } 
 
    /** 
     * @param mixed $longitude 
     */ 
    public function setLongitude($longitude): void 
    { 
        $this->longitude = $longitude; 
    } 
 
    /** 
     * @return mixed 
     */ 
    public function getLatitude() 
    { 
        return $this->latitude; 
    } 
 
    /** 
     * @param mixed $latitude 
     */ 
    public function setLatitude($latitude): void 
    { 
        $this->latitude = $latitude; 
    } 
 
 
}