Add the profitability report and the internal hourly cost
Profitability by job and service unit from a period's hours, expenses and the created billing materials of its months. Hours are costed at one internal hourly cost, set on the price list maintenance page; a job of several service units is shared evenly between them. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
<?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 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,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @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): self
|
||||
{
|
||||
return new self(
|
||||
$quarters,
|
||||
$billedQuarters,
|
||||
$workCents,
|
||||
$expenseRevenueCents,
|
||||
$expenseCostCents,
|
||||
// Rounded half up to a cent.
|
||||
$hourlyCostCents === null ? null : intdiv($quarters * $hourlyCostCents * 2 + 4, 8),
|
||||
);
|
||||
}
|
||||
|
||||
public function revenueCents(): int
|
||||
{
|
||||
return $this->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<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),
|
||||
);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user