partkeepr

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

ProjectPart.php (5789B)


      1 <?php
      2 
      3 namespace PartKeepr\ProjectBundle\Entity;
      4 
      5 use Doctrine\ORM\Mapping as ORM;
      6 use PartKeepr\CoreBundle\Entity\BaseEntity;
      7 use PartKeepr\DoctrineReflectionBundle\Annotation\ByReference;
      8 use PartKeepr\DoctrineReflectionBundle\Annotation\TargetService;
      9 use PartKeepr\PartBundle\Entity\Part;
     10 use Symfony\Component\Serializer\Annotation\Groups;
     11 use Symfony\Component\Validator\Constraints as Assert;
     12 
     13 /**
     14  * Represents a project part.
     15  *
     16  * @ORM\Entity
     17  * @TargetService("/api/project_parts")
     18  */
     19 class ProjectPart extends BaseEntity
     20 {
     21     const OVERAGE_TYPE_ABSOLUTE = "absolute";
     22 
     23     const OVERAGE_TYPE_PERCENT = "percent";
     24 
     25     const OVERAGE_TYPES = [self::OVERAGE_TYPE_ABSOLUTE, self::OVERAGE_TYPE_PERCENT];
     26 
     27     /**
     28      * The part this project part refers to.
     29      *
     30      * @ORM\ManyToOne(targetEntity="PartKeepr\PartBundle\Entity\Part", inversedBy="projectParts")
     31      * @Groups({"default"})
     32      * @Assert\NotNull()
     33      *
     34      * @ByReference()
     35      *
     36      * @var Part
     37      */
     38     private $part;
     39 
     40     /**
     41      * Specifies the amount of parts.
     42      *
     43      * @ORM\Column(type="integer")
     44      * @Groups({"default"})
     45      *
     46      * @var int
     47      */
     48     private $quantity;
     49 
     50     /**
     51      * Specifies the project which belongs to this project part.
     52      *
     53      * @ORM\ManyToOne(targetEntity="PartKeepr\ProjectBundle\Entity\Project", inversedBy="parts")
     54      * @Assert\NotNull()
     55      *
     56      * @var Project
     57      */
     58     private $project;
     59 
     60     /**
     61      * Specifies the remarks for this entry.
     62      *
     63      * @ORM\Column(type="string",nullable=true)
     64      * @Groups({"default"})
     65      *
     66      * @var string
     67      */
     68     private $remarks;
     69 
     70     /**
     71      * The overage type.
     72      *
     73      * @ORM\Column(type="string",options={ "default":""})
     74      * @Groups({"default"})
     75      *
     76      * @var string
     77      */
     78     private $overageType;
     79 
     80     /**
     81      * Specifies the overage, which can either be percent or an absolute value depending on overageType.
     82      *
     83      * @ORM\Column(type="integer",options={ "default":0})
     84      * @Groups({"default"})
     85      *
     86      * @var int
     87      */
     88     private $overage;
     89 
     90     /**
     91      * Specifies the lot number.
     92      *
     93      * @ORM\Column(type="text",options={ "default":""})
     94      * @Groups({"default"})
     95      *
     96      * @var string
     97      */
     98     private $lotNumber;
     99 
    100     /**
    101      * The total quantity including overage.
    102      *
    103      * @Groups({"default"})
    104      *
    105      * @var int
    106      */
    107     private $totalQuantity;
    108 
    109     /**
    110      * Retrieves the total quantity for a project part, including overage.
    111      *
    112      * @return int
    113      */
    114     public function getTotalQuantity()
    115     {
    116         switch ($this->getOverageType()) {
    117             case self::OVERAGE_TYPE_PERCENT:
    118                 return (int) $this->getQuantity() * (1 + $this->getOverage() / 100);
    119             case self::OVERAGE_TYPE_ABSOLUTE:
    120                 return $this->getQuantity() + $this->getOverage();
    121             default:
    122                 return $this->getQuantity();
    123         }
    124     }
    125 
    126     public function __construct()
    127     {
    128         $this->setOverageType(self::OVERAGE_TYPE_ABSOLUTE);
    129         $this->setOverage(0);
    130         $this->setLotNumber("");
    131     }
    132 
    133     /**
    134      * @return string
    135      */
    136     public function getLotNumber()
    137     {
    138         return $this->lotNumber;
    139     }
    140 
    141     /**
    142      * @param string $lotNumber
    143      */
    144     public function setLotNumber($lotNumber)
    145     {
    146         $this->lotNumber = $lotNumber;
    147     }
    148 
    149     /**
    150      * @return string
    151      */
    152     public function getOverageType()
    153     {
    154         if (!in_array($this->overageType, self::OVERAGE_TYPES)) {
    155             return self::OVERAGE_TYPE_ABSOLUTE;
    156         }
    157 
    158         return $this->overageType;
    159     }
    160 
    161     /**
    162      * @param string $overageType
    163      */
    164     public function setOverageType($overageType)
    165     {
    166         if (!in_array($overageType, self::OVERAGE_TYPES)) {
    167             $overageType = self::OVERAGE_TYPE_ABSOLUTE;
    168         }
    169 
    170         $this->overageType = $overageType;
    171     }
    172 
    173     /**
    174      * @return int
    175      */
    176     public function getOverage()
    177     {
    178         return $this->overage;
    179     }
    180 
    181     /**
    182      * @param int $overage
    183      */
    184     public function setOverage($overage)
    185     {
    186         $this->overage = $overage;
    187     }
    188 
    189     /**
    190      * Returns the part which belongs to this entry.
    191      *
    192      * @return Part
    193      */
    194     public function getPart()
    195     {
    196         return $this->part;
    197     }
    198 
    199     /**
    200      * Sets the part which belongs to this entry.
    201      *
    202      * @param Part $part
    203      */
    204     public function setPart(Part $part)
    205     {
    206         $this->part = $part;
    207     }
    208 
    209     /**
    210      * Returns the quantity for this project.
    211      *
    212      * @return int the amount of parts needed
    213      */
    214     public function getQuantity()
    215     {
    216         return $this->quantity;
    217     }
    218 
    219     /**
    220      * Sets the quantity for this entry.
    221      *
    222      * @param int $quantity
    223      */
    224     public function setQuantity($quantity)
    225     {
    226         $this->quantity = intval($quantity);
    227     }
    228 
    229     /**
    230      * Returns the project assigned to this entry.
    231      *
    232      * @return Project
    233      */
    234     public function getProject()
    235     {
    236         return $this->project;
    237     }
    238 
    239     /**
    240      * Sets the project assigned to this entry.
    241      *
    242      * @param Project $project
    243      */
    244     public function setProject(Project $project = null)
    245     {
    246         $this->project = $project;
    247     }
    248 
    249     /**
    250      * Returns the remarks for this entry.
    251      *
    252      * @return string
    253      */
    254     public function getRemarks()
    255     {
    256         return $this->remarks;
    257     }
    258 
    259     /**
    260      * Sets the remarks for this entry.
    261      *
    262      * @param string $remarks
    263      */
    264     public function setRemarks($remarks)
    265     {
    266         $this->remarks = $remarks;
    267     }
    268 
    269     public function __toString()
    270     {
    271         //@todo i18n
    272         return sprintf("Used in project %s", $this->getProject()->getName())." / ".parent::__toString();
    273     }
    274 }