partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

Report.php (4725B)


      1 <?php
      2 
      3 namespace PartKeepr\ProjectBundle\Entity;
      4 
      5 use Doctrine\Common\Collections\ArrayCollection;
      6 use Doctrine\ORM\Mapping as ORM;
      7 use PartKeepr\CoreBundle\Entity\BaseEntity;
      8 use PartKeepr\DoctrineReflectionBundle\Annotation\TargetService;
      9 use PartKeepr\PartBundle\Entity\Part;
     10 use Symfony\Component\Serializer\Annotation\Groups;
     11 
     12 /**
     13  * Represents a project part.
     14  *
     15  * @ORM\Entity
     16  * @TargetService("/api/reports")
     17  */
     18 class Report extends BaseEntity
     19 {
     20     /**
     21      * The report name.
     22      *
     23      * @ORM\Column(type="string",nullable=true)
     24      * @Groups({"default"})
     25      *
     26      * @var string
     27      */
     28     private $name;
     29 
     30     /**
     31      * @ORM\Column(type="datetime")
     32      * @Groups({"default"})
     33      *
     34      * @var \DateTime
     35      */
     36     private $createDateTime;
     37 
     38     /**
     39      * @ORM\OneToMany(
     40      *     targetEntity="PartKeepr\ProjectBundle\Entity\ReportProject",
     41      *     mappedBy="report",
     42      *     cascade={"persist", "remove"},
     43      *     orphanRemoval=true
     44      * )
     45      * @Groups({"default"})
     46      *
     47      * @var ArrayCollection
     48      */
     49     private $reportProjects;
     50 
     51     /**
     52      * @ORM\OneToMany(
     53      *     targetEntity="PartKeepr\ProjectBundle\Entity\ReportPart",
     54      *     mappedBy="report",
     55      *     cascade={"persist", "remove"},
     56      *     orphanRemoval=true
     57      * )
     58      * @Groups({"default"})
     59      *
     60      * @var ArrayCollection
     61      */
     62     private $reportParts;
     63 
     64     public function __construct()
     65     {
     66         $this->reportProjects = new ArrayCollection();
     67         $this->reportParts = new ArrayCollection();
     68         $this->setCreateDateTime(new \DateTime());
     69     }
     70 
     71     /**
     72      * @return \DateTime
     73      */
     74     public function getCreateDateTime()
     75     {
     76         return $this->createDateTime;
     77     }
     78 
     79     /**
     80      * @param \DateTime $createDateTime
     81      *
     82      * @return Report
     83      */
     84     public function setCreateDateTime($createDateTime)
     85     {
     86         $this->createDateTime = $createDateTime;
     87 
     88         return $this;
     89     }
     90 
     91     /**
     92      * @return string
     93      */
     94     public function getName()
     95     {
     96         return $this->name;
     97     }
     98 
     99     /**
    100      * @param string $name
    101      *
    102      * @return Report
    103      */
    104     public function setName($name)
    105     {
    106         if ($name != null) {
    107             $this->name = $name;
    108         } else {
    109             $this->name = 'NewReport'; //@todo i18n
    110         }
    111 
    112         return $this;
    113     }
    114 
    115     /**
    116      * @return ArrayCollection|ReportProject[]
    117      */
    118     public function getReportProjects()
    119     {
    120         return $this->reportProjects;
    121     }
    122 
    123     /**
    124      * Adds a Report Project.
    125      *
    126      * @param ReportProject $reportProject A report project to add
    127      *
    128      * @return Report
    129      */
    130     public function addReportProject($reportProject)
    131     {
    132         $reportProject->setReport($this);
    133         $this->reportProjects->add($reportProject);
    134 
    135         return $this;
    136     }
    137 
    138     /**
    139      * Removes a Report Project.
    140      *
    141      * @param ReportProject $reportProject A report project to add
    142      *
    143      * @return Report
    144      */
    145     public function removeReportProject($reportProject)
    146     {
    147         $reportProject->setReport(null);
    148         $this->reportProjects->removeElement($reportProject);
    149 
    150         return $this;
    151     }
    152 
    153     /**
    154      * Removes a Report Part.
    155      *
    156      * @param ReportPart $reportPart A report project to add
    157      *
    158      * @return Report
    159      */
    160     public function removeReportPart($reportPart)
    161     {
    162         $reportPart->setReport(null);
    163         $this->reportProjects->removeElement($reportPart);
    164 
    165         return $this;
    166     }
    167 
    168     public function addPartQuantity(Part $part, ProjectPart $projectPart, $quantity)
    169     {
    170         $reportPart = $this->getReportPartByPart($part);
    171 
    172         if ($reportPart === null) {
    173             $reportPart = new ReportPart();
    174             $reportPart->setPart($part);
    175             $reportPart->setReport($this);
    176 
    177             $this->addReportPart($reportPart);
    178         }
    179 
    180         $reportPart->setQuantity($reportPart->getQuantity() + $quantity);
    181 
    182         $reportPart->getProjectParts()->add($projectPart);
    183     }
    184 
    185     public function getReportPartByPart(Part $part)
    186     {
    187         foreach ($this->getReportParts() as $reportPart) {
    188             if ($reportPart->getPart() === $part) {
    189                 return $reportPart;
    190             }
    191         }
    192 
    193         return null;
    194     }
    195 
    196     /**
    197      * @return ArrayCollection|ReportPart[]
    198      */
    199     public function getReportParts()
    200     {
    201         return $this->reportParts;
    202     }
    203 
    204     /**
    205      * Adds a Report Part.
    206      *
    207      * @param ReportPart $reportPart report project to add
    208      *
    209      * @return Report
    210      */
    211     public function addReportPart($reportPart)
    212     {
    213         $reportPart->setReport($this);
    214         $this->reportParts->add($reportPart);
    215 
    216         return $this;
    217     }
    218 }