workCents + $this->expenseRevenueCents; } public function marginCents(): ?int { return $this->hourCostCents === null ? null : $this->revenueCents() - $this->expenseCostCents - $this->hourCostCents; } /** * The margin as a share of what was billed, null when nothing was billed. */ public function marginRatio(): ?float { $margin = $this->marginCents(); return $margin === null || $this->revenueCents() <= 0 ? null : $margin / $this->revenueCents(); } /** * What an hour worked brought in: billed for the work per hour worked. */ public function effectiveHourlyCents(): ?int { return $this->quarters === 0 ? null : (int) round($this->workCents * 4 / $this->quarters); } public function plus(self $other): self { return new self( $this->quarters + $other->quarters, $this->billedQuarters + $other->billedQuarters, $this->workCents + $other->workCents, $this->expenseRevenueCents + $other->expenseRevenueCents, $this->expenseCostCents + $other->expenseCostCents, $this->hourCostCents === null || $other->hourCostCents === null ? null : $this->hourCostCents + $other->hourCostCents, ); } /** * Splits the figures evenly into parts, e.g. for a job of several * service units; the first parts take what does not divide evenly, so * the parts add up to the whole. * * @return non-empty-list */ public function split(int $parts): array { $parts = max(1, $parts); $result = []; for ($index = 0; $index < $parts; $index++) { $result[] = new self( self::share($this->quarters, $parts, $index), self::share($this->billedQuarters, $parts, $index), self::share($this->workCents, $parts, $index), self::share($this->expenseRevenueCents, $parts, $index), self::share($this->expenseCostCents, $parts, $index), $this->hourCostCents === null ? null : self::share($this->hourCostCents, $parts, $index), ); } return $result; } private static function share(int $value, int $parts, int $index): int { $base = intdiv($value, $parts); $rest = $value - $base * $parts; return $base + ($index < abs($rest) ? ($rest <=> 0) : 0); } }