123 lines
4.2 KiB
PHP
123 lines
4.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Reports\Domain\ValueObjects;
|
|
|
|
/**
|
|
* The profitability of a job or a group of jobs in a period, without VAT,
|
|
* amounts in cents and hours in quarters:
|
|
*
|
|
* - billed for the work (hours, fixed price, monthly fees) and for expenses;
|
|
* - the purchase cost of the expenses and the cost of the hours worked at
|
|
* the company's one internal hourly cost (null when it is not set);
|
|
* - the cost of the stock used on the job (material, spec 19.8), which is
|
|
* not billed separately;
|
|
* - the margin: everything billed less those costs (null without the hourly cost).
|
|
*/
|
|
final readonly class Profit
|
|
{
|
|
public function __construct(
|
|
public int $quarters = 0,
|
|
public int $billedQuarters = 0,
|
|
public int $workCents = 0,
|
|
public int $expenseRevenueCents = 0,
|
|
public int $expenseCostCents = 0,
|
|
public ?int $hourCostCents = 0,
|
|
public int $materialCostCents = 0,
|
|
) {}
|
|
|
|
/**
|
|
* @param int|null $hourlyCostCents The internal cost of an hour, null when not set.
|
|
*/
|
|
public static function of(int $quarters, int $billedQuarters, int $workCents, int $expenseRevenueCents, int $expenseCostCents, ?int $hourlyCostCents, int $materialCostCents = 0): self
|
|
{
|
|
return new self(
|
|
$quarters,
|
|
$billedQuarters,
|
|
$workCents,
|
|
$expenseRevenueCents,
|
|
$expenseCostCents,
|
|
// Rounded half up to a cent.
|
|
$hourlyCostCents === null ? null : intdiv($quarters * $hourlyCostCents * 2 + 4, 8),
|
|
$materialCostCents,
|
|
);
|
|
}
|
|
|
|
public function revenueCents(): int
|
|
{
|
|
return $this->workCents + $this->expenseRevenueCents;
|
|
}
|
|
|
|
public function marginCents(): ?int
|
|
{
|
|
return $this->hourCostCents === null ? null : $this->revenueCents() - $this->expenseCostCents - $this->materialCostCents - $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,
|
|
$this->materialCostCents + $other->materialCostCents,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 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<self>
|
|
*/
|
|
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),
|
|
self::share($this->materialCostCents, $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);
|
|
}
|
|
}
|