v0.9.12
This commit is contained in:
commit
e50fe9b0c2
298 files changed
+20885
No files matched your search
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Modules\Costs;
|
||||
|
||||
use IriksIT\Manager\Support\Changelog;
|
||||
use IriksIT\Manager\Support\Costs\Ledger;
|
||||
|
||||
class CostsController
|
||||
{
|
||||
public function index(Ledger $ledger)
|
||||
{
|
||||
$items = $ledger->items();
|
||||
$dates = [];
|
||||
|
||||
foreach (Changelog::releases(limit: 200) as $release) {
|
||||
$dates[$release['version']] = $release['date'];
|
||||
}
|
||||
|
||||
// A group per release, newest first as in the changelog, and before them the work that has no release yet. A row without a
|
||||
// version belongs to the newest release when the site says so (on the live site everything that is deployed is released, and
|
||||
// the row only has no version because the file is filled in afterwards).
|
||||
$groups = [];
|
||||
|
||||
foreach ($dates as $version => $date) {
|
||||
$groups[$version] = ['version' => $version, 'date' => $date, 'items' => []];
|
||||
}
|
||||
|
||||
$newest = config('manager.costs.assign_open_to_newest') ? array_key_first($groups) : null;
|
||||
$open = ['version' => null, 'date' => null, 'items' => []];
|
||||
|
||||
foreach ($items as $item) {
|
||||
if ($item['version'] === null && $newest !== null) {
|
||||
$groups[$newest]['items'][] = $item;
|
||||
} elseif ($item['version'] === null) {
|
||||
$open['items'][] = $item;
|
||||
} else {
|
||||
// A row can name a release the changelog does not have (yet): it still gets its group.
|
||||
$groups[$item['version']] ??= ['version' => $item['version'], 'date' => null, 'items' => []];
|
||||
$groups[$item['version']]['items'][] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
$all = array_values(array_filter([...($open['items'] !== [] ? [$open] : []), ...array_values($groups)], fn (array $group) => $group['items'] !== []));
|
||||
|
||||
$sum = fn (array $rows, string $key): float => (float) array_sum(array_map(fn (array $item) => $item[$key] ?? 0.0, $rows));
|
||||
$hosting = $ledger->hosting();
|
||||
$discount = $ledger->discount();
|
||||
$discounted = round(array_sum(array_map(fn (array $item) => $ledger->discountOn($item), $items)), 2);
|
||||
$work = round($sum($items, 'work_eur'), 2);
|
||||
$ai = round($sum($items, 'eur'), 2);
|
||||
|
||||
return response()->json([
|
||||
'groups' => array_map(fn (array $group) => $group + [
|
||||
'eur' => round($sum($group['items'], 'eur'), 2),
|
||||
'tokens' => (int) $sum($group['items'], 'tokens'),
|
||||
'hours' => round($sum($group['items'], 'hours'), 2),
|
||||
'work_eur' => round($sum($group['items'], 'work_eur'), 2),
|
||||
], $all),
|
||||
// Three costs, kept apart: the work (hours at the hourly rate), what the AI model cost in tokens, and hosting.
|
||||
'costs' => [
|
||||
'hourly_rate' => $ledger->hourlyRate(),
|
||||
'hours' => round($sum($items, 'hours'), 2),
|
||||
'work_eur' => $work,
|
||||
'ai_eur' => $ai,
|
||||
'hosting' => $hosting,
|
||||
'total_eur' => round($work + $ai + $hosting['eur'], 2),
|
||||
'discount' => $discount !== null ? $discount + ['eur' => $discounted] : null,
|
||||
// Never a cent below zero through rounding, and never "-0".
|
||||
'due_eur' => max(0.0, round($work + $ai + $hosting['eur'] - $discounted, 2)) + 0.0,
|
||||
'without_hours' => count(array_filter($items, fn (array $item) => $item['hours'] === null)),
|
||||
],
|
||||
'totals' => [
|
||||
'tokens' => (int) $sum($items, 'tokens'),
|
||||
'steps' => (int) $sum($items, 'steps'),
|
||||
'unpriced' => count(array_filter($items, fn (array $item) => $item['eur'] === null)),
|
||||
],
|
||||
// Where the amounts come from: a price source that was asked once and kept, the fixed rates of the config, or both.
|
||||
'source' => self::source($items),
|
||||
]);
|
||||
}
|
||||
|
||||
/** @param list<array{priced_by: string|null}> $items */
|
||||
private static function source(array $items): string
|
||||
{
|
||||
$by = array_values(array_unique(array_filter(array_column($items, 'priced_by'))));
|
||||
|
||||
return match (true) {
|
||||
$by === ['service'] => 'service',
|
||||
in_array('service', $by, true) => 'mixed',
|
||||
default => 'config',
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user