partLots; } /** * Adds the given part lot, to the list of part lots. * The part lot is assigned to this part. * @param PartLot $lot * @return $this */ public function addPartLot(PartLot $lot): self { $lot->setPart($this); $this->partLots->add($lot); return $this; } /** * Removes the given part lot from the list of part lots. * * @param PartLot $lot the part lot that should be deleted * @return $this */ public function removePartLot(PartLot $lot): self { $this->partLots->removeElement($lot); return $this; } /** * Gets the measurement unit in which the part's amount should be measured. * Returns null if no specific unit was that. That means the parts are measured simply in quantity numbers. */ public function getPartUnit(): ?MeasurementUnit { return $this->partUnit; } /** * Sets the measurement unit in which the part's amount should be measured. * Set to null, if the part should be measured in quantities. * @param MeasurementUnit|null $partUnit * @return $this */ public function setPartUnit(?MeasurementUnit $partUnit): self { $this->partUnit = $partUnit; return $this; } /** * Get the count of parts which must be in stock at least. * If a integer-based part unit is selected, the value will be rounded to integers. * * @return float count of parts which must be in stock at least */ public function getMinAmount(): float { if ($this->useFloatAmount()) { return $this->minamount; } return round($this->minamount); } /** * Checks if this part uses the float amount . * This setting is based on the part unit (see MeasurementUnit->isInteger()). * * @return bool True if the float amount field should be used. False if the integer instock field should be used. */ public function useFloatAmount(): bool { if ($this->partUnit instanceof MeasurementUnit) { return ! $this->partUnit->isInteger(); } //When no part unit is set, treat it as part count, and so use the integer value. return false; } /** * Returns the summed amount of this part (over all part lots) * Part Lots that have unknown value or are expired, are not used for this value. * * @return float The amount of parts given in partUnit * */ public function getAmountSum(): float { //TODO: Find a method to do this natively in SQL, the current method could be a bit slow $sum = 0; foreach ($this->getPartLots() as $lot) { //Dont use the instock value, if it is unkown if ($lot->isInstockUnknown() || $lot->isExpired() ?? false) { continue; } $sum += $lot->getAmount(); } if ($this->useFloatAmount()) { return $sum; } return round($sum); } /** * Set the minimum amount of parts that have to be instock. * See getPartUnit() for the associated unit. * * @param float $new_minamount the new count of parts which should be in stock at least * @return $this */ public function setMinAmount(float $new_minamount): self { $this->minamount = $new_minamount; return $this; } }