mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
331 lines
6.9 KiB
PHP
331 lines
6.9 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Chill is a software for social workers
|
|
*
|
|
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
|
|
* <http://www.champs-libres.coop>, <info@champs-libres.coop>
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
* License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
namespace Chill\PersonBundle\Entity;
|
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
|
use Chill\MainBundle\Entity\User;
|
|
|
|
/**
|
|
* AccompanyingPeriod
|
|
*
|
|
* @ORM\Entity()
|
|
* @ORM\Table(name="chill_person_accompanying_period")
|
|
*/
|
|
class AccompanyingPeriod
|
|
{
|
|
/**
|
|
* @var integer
|
|
*
|
|
* @ORM\Id
|
|
* @ORM\Column(name="id", type="integer")
|
|
* @ORM\GeneratedValue(strategy="AUTO")
|
|
*/
|
|
private $id;
|
|
|
|
/**
|
|
* @var \DateTime
|
|
*
|
|
* @ORM\Column(type="date")
|
|
*/
|
|
private $openingDate;
|
|
|
|
/**
|
|
* @var \DateTime
|
|
*
|
|
* @ORM\Column(type="date", nullable=true)
|
|
*/
|
|
private $closingDate = null;
|
|
|
|
/**
|
|
* @var string
|
|
*
|
|
* @ORM\Column(type="text")
|
|
*/
|
|
private $remark = '';
|
|
|
|
/**
|
|
* @var Person
|
|
*
|
|
* @ORM\ManyToOne(
|
|
* targetEntity="Chill\PersonBundle\Entity\Person",
|
|
* inversedBy="accompanyingPeriods",
|
|
* cascade={"refresh"})
|
|
*/
|
|
private $person;
|
|
|
|
/**
|
|
* @var AccompanyingPeriod\ClosingMotive
|
|
*
|
|
* @ORM\ManyToOne(
|
|
* targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive")
|
|
* @ORM\JoinColumn(nullable=true)
|
|
*/
|
|
private $closingMotive = null;
|
|
|
|
/**
|
|
* The user making the accompanying
|
|
* @var User
|
|
*
|
|
* @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\User")
|
|
* @ORM\JoinColumn(nullable=true)
|
|
*/
|
|
private $user;
|
|
|
|
|
|
/**
|
|
* AccompanyingPeriod constructor.
|
|
*
|
|
* @param \DateTime $dateOpening
|
|
* @uses AccompanyingPeriod::setClosingDate()
|
|
*/
|
|
public function __construct(\DateTime $dateOpening) {
|
|
$this->setOpeningDate($dateOpening);
|
|
}
|
|
|
|
/**
|
|
* Get id
|
|
*
|
|
* @return integer
|
|
*/
|
|
public function getId()
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
/**
|
|
* Set openingDate
|
|
*
|
|
* @param \DateTime $dateOpening
|
|
* @return AccompanyingPeriod
|
|
*/
|
|
public function setOpeningDate($openingDate)
|
|
{
|
|
$this->openingDate = $openingDate;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get openingDate
|
|
*
|
|
* @return \DateTime
|
|
*/
|
|
public function getOpeningDate()
|
|
{
|
|
return $this->openingDate;
|
|
}
|
|
|
|
/**
|
|
* Set closingDate
|
|
*
|
|
* For closing a Person file, you should use Person::setClosed instead.
|
|
*
|
|
* @param \DateTime $dateClosing
|
|
* @return AccompanyingPeriod
|
|
*
|
|
*/
|
|
public function setClosingDate($closingDate)
|
|
{
|
|
$this->closingDate = $closingDate;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get closingDate
|
|
*
|
|
* @return \DateTime
|
|
*/
|
|
public function getClosingDate()
|
|
{
|
|
return $this->closingDate;
|
|
}
|
|
|
|
/**
|
|
* @return boolean
|
|
*/
|
|
public function isOpen(): bool
|
|
{
|
|
if ($this->getOpeningDate() > new \DateTime('now')) {
|
|
return false;
|
|
}
|
|
|
|
if ($this->getClosingDate() === null) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set remark
|
|
*
|
|
* @param string $remark
|
|
* @return AccompanyingPeriod
|
|
*/
|
|
public function setRemark($remark)
|
|
{
|
|
if ($remark === null) {
|
|
$remark = '';
|
|
}
|
|
|
|
$this->remark = $remark;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get remark
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getRemark()
|
|
{
|
|
return $this->remark;
|
|
}
|
|
|
|
/**
|
|
* Set person.
|
|
*
|
|
* For consistency, you should use Person::addAccompanyingPeriod instead.
|
|
*
|
|
* @param Person $person
|
|
* @return AccompanyingPeriod
|
|
* @see Person::addAccompanyingPeriod
|
|
*/
|
|
public function setPerson(Person $person = null)
|
|
{
|
|
$this->person = $person;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get person
|
|
*
|
|
* @return Person
|
|
*/
|
|
public function getPerson()
|
|
{
|
|
return $this->person;
|
|
}
|
|
|
|
/**
|
|
* @return AccompanyingPeriod\ClosingMotive
|
|
*/
|
|
public function getClosingMotive()
|
|
{
|
|
return $this->closingMotive;
|
|
}
|
|
|
|
/**
|
|
* @param AccompanyingPeriod\ClosingMotive|null $closingMotive
|
|
* @return $this
|
|
*/
|
|
public function setClosingMotive(AccompanyingPeriod\ClosingMotive $closingMotive = null)
|
|
{
|
|
$this->closingMotive = $closingMotive;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* If the period can be reopened.
|
|
*
|
|
* This function test if the period is closed and if the period is the last
|
|
* for the associated person
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function canBeReOpened()
|
|
{
|
|
if ($this->isOpen() === true) {
|
|
return false;
|
|
}
|
|
|
|
$periods = $this->getPerson()->getAccompanyingPeriodsOrdered();
|
|
|
|
return end($periods) === $this;
|
|
}
|
|
|
|
/**
|
|
*/
|
|
public function reOpen()
|
|
{
|
|
$this->setClosingDate(null);
|
|
$this->setClosingMotive(null);
|
|
}
|
|
|
|
/**
|
|
* Validation function
|
|
*/
|
|
public function isDateConsistent(ExecutionContextInterface $context)
|
|
{
|
|
if ($this->isOpen()) {
|
|
return;
|
|
}
|
|
|
|
if (! $this->isClosingAfterOpening()) {
|
|
$context->buildViolation('The date of closing is before the date of opening')
|
|
->atPath('dateClosing')
|
|
->addViolation();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns true if the closing date is after the opening date.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function isClosingAfterOpening()
|
|
{
|
|
$diff = $this->getOpeningDate()->diff($this->getClosingDate());
|
|
|
|
if ($diff->invert === 0) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return User|null
|
|
*/
|
|
function getUser(): ?User
|
|
{
|
|
return $this->user;
|
|
}
|
|
|
|
/**
|
|
* @param User $user
|
|
* @return AccompanyingPeriod
|
|
*/
|
|
function setUser(User $user): self
|
|
{
|
|
$this->user = $user;
|
|
|
|
return $this;
|
|
}
|
|
|
|
}
|