<?php
/**
* Copyright (c) 2019, MND Next GmbH - www.mndnext.de
*/
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Intl\Intl;
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
* @ORM\Table(name="companies")
*/
class Company
{
public const API_OBJECT_NAME = 'company';
const MANDATORY_PROPS = ['name', 'street', 'streetnr', 'postcode', 'city', 'country'];
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
* @ORM\Column(name="name", type="string", length=255, nullable=true)
* @Assert\NotBlank(message="Bitte geben Sie einen Firmennamen ein.", groups={"Registration", "Profile"})
*/
private $name;
/**
* @var string
* @ORM\Column(name="department", type="string", length=255, nullable=true)
*/
private $department;
/**
* @var string
* @ORM\Column(name="street", type="string", length=255, nullable=false)
* @Assert\NotBlank(message="Bitte geben Sie eine Straße ein.", groups={"Registration", "Profile"})
*/
private $street;
/**
* @var string
* @ORM\Column(name="streetnumber", type="string", length=20, nullable=false)
* @Assert\NotBlank(message="Bitte geben Sie eine Hausnummer ein.", groups={"Registration", "Profile"})
*/
private $streetNr;
/**
* @var string
* @ORM\Column(name="postcode", type="string", length=10, nullable=false)
* @Assert\NotBlank(message="Bitte geben Sie eine PLZ ein.", groups={"Registration", "Profile"})
*/
private $postcode;
/**
* @var string
* @ORM\Column(name="city", type="string", length=255, nullable=false)
* @Assert\NotBlank(message="Bitte geben Sie einen Ort ein.", groups={"Registration", "Profile"})
*/
private $city;
/**
* @var string
* @ORM\Column(name="country", type="string", length=255, nullable=false)
* @Assert\NotBlank(message="Bitte wählen Sie ein Land.", groups={"Registration", "Profile"})
*/
private $country;
/**
* @var string
* @ORM\Column(name="phone", type="string", length=255, nullable=true)
* @Assert\NotBlank(message="Bitte geben Sie eine Telefonnummer ein.", groups={"Registration", "Profile"})
*/
private $phone;
/**
* @var \DateTime
*
* @ORM\Column(name="created", type="datetime", nullable=false)
*/
private $created;
/**
* @var string
* @ORM\Column(name="notes", type="text", nullable=true)
*/
private $notes;
/**
* @var string
* @ORM\Column(name="manufacturer", type="boolean", nullable=false, options={"default" : false})
*/
private $manufacturer = false;
/**
* @var string
* @ORM\Column(name="directory", type="string", length=10, nullable=true)
*/
private $fileDir;
/**
* @ORM\OneToMany(targetEntity="Employee", mappedBy="company")
*/
private $employees;
/**
* @ORM\OneToMany(targetEntity="Machine", mappedBy="company")
*/
private $machines;
/**
* @ORM\OneToMany(targetEntity="CompanyFile", mappedBy="company", cascade={"remove"})
*/
private $files;
/**
* Constructor
*/
public function __construct()
{
$this->employees = new \Doctrine\Common\Collections\ArrayCollection();
$this->files = new \Doctrine\Common\Collections\ArrayCollection();
$this->created = new \DateTime();
}
public function getId()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
}
public function setDepartment($clientnr)
{
$this->department = $clientnr;
return $this;
}
public function getDepartment()
{
return $this->department;
}
public function setStreet($street)
{
$this->street = $street;
return $this;
}
public function getStreet()
{
return $this->street;
}
public function setStreetNr($streetnr)
{
$this->streetNr = $streetnr;
return $this;
}
public function getStreetNr()
{
return $this->streetNr;
}
public function setPostcode($postcode)
{
$this->postcode = $postcode;
return $this;
}
public function getPostcode()
{
return $this->postcode;
}
public function setCity($city)
{
$this->city = $city;
return $this;
}
public function getCity()
{
return $this->city;
}
public function setCountry($country)
{
$this->country = $country;
return $this;
}
public function getCountry()
{
return $this->country;
}
public function getCountryName() {
return Intl::getRegionBundle()->getCountryName($this->getCountry(), 'de_DE');
}
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
public function getPhone()
{
return $this->phone;
}
public function setCreated($created)
{
$this->created = $created;
return $this;
}
public function getCreated()
{
return $this->created;
}
public function setNotes($notes)
{
$this->notes = $notes;
return $this;
}
public function getNotes()
{
return $this->notes;
}
public function getFileDir()
{
return $this->fileDir;
}
public function setFileDir($fileDir)
{
if (!$this->fileDir) {
$this->fileDir = $fileDir;
}
return $this;
}
public function setManufacturer($state)
{
$this->manufacturer = $state;
return $this;
}
public function isManufacturer()
{
return $this->manufacturer;
}
/**
* @return Collection|Employee[]
*/
public function getEmployees() : Collection
{
return $this->employees;
}
/**
* @return Collection|Employee[]
*/
public function getContactPersons() : Collection
{
$contacts = new ArrayCollection();
/* @var Employee $employee */
foreach ($this->employees as $employee) {
if ($employee->isContractor()) {
$contacts->add($employee);
}
}
return $contacts;
}
/**
* @return Collection|Machine[]
*/
public function getMachines() : Collection
{
return $this->machines;
}
/**
* @return Collection|CompanyFile[]
*/
public function getEmployeeFiles() : Collection
{
$files = new ArrayCollection();
/* @var CompanyFile $file */
foreach ($this->files as $file) {
if ($file->isVisible() && !$file->isSensitive()) {
$files->add($file);
}
}
return $files;
}
/**
* @return Collection|CompanyFile[]
*/
public function getContactFiles() : Collection
{
$files = new ArrayCollection($this->files->toArray());
/* @var CompanyFile $file */
foreach ($this->files as $file) {
if (!$file->isVisible()) {
$files->removeElement($file);
}
}
return $files;
}
/**
* @return Collection|CompanyFile[]
*/
public function getFiles() : Collection
{
return $this->files;
}
public function __toString()
{
return $this->getName();
}
public function getAddress()
{
$address = $this->getStreet() . ' ' . $this->getStreetNr();
$address .= "\n";
$address .= $this->getPostcode() . ' ' . $this->getCity();
return $address;
}
private static function getOptProp($data, $prop)
{
if (array_key_exists($prop, $data)) {
if (!is_string($data[$prop])) {
throw new BadRequestHttpException(sprintf('The key "%s" must be must be a string in "company".', $prop));
}
return $data[$prop];
}
}
public static function createFromData($data)
{
foreach (self::MANDATORY_PROPS as $prop) {
if (!array_key_exists($prop, $data)) {
throw new BadRequestHttpException(sprintf('The key "%s" must be provided in "company".', $prop));
}
}
$object = new Company();
$object->setName(self::getOptProp($data, 'name'));
$object->setStreet(self::getOptProp($data, 'street'));
$object->setStreetNr(self::getOptProp($data, 'streetnr'));
$object->setPostcode(self::getOptProp($data, 'postcode'));
$object->setCity(self::getOptProp($data, 'city'));
$object->setCountry(self::getOptProp($data, 'country'));
$object->setPhone(self::getOptProp($data, 'phone'));
return $object;
}
}