Skip to content

Commit c17cf5e

Browse files
MayNiklasjbtronics
andauthored
Add price columns to project BOM table and build price summary (#1345)
* Add unit price and extended price columns to project BOM table Adds two optional columns to the project BOM datatable (hidden by default, toggleable via column visibility): - **Price**: unit price for the BOM entry in the base currency, looked up via PricedetailHelper. For parts whose BOM quantity falls below the minimum order amount the minimum order amount is used for the price tier lookup so that a price is always returned. - **Extended Price**: unit price multiplied by the BOM quantity. Prices are rendered via MoneyFormatter (locale-aware, with currency symbol). Both columns round up to 2 decimal places to avoid displaying 0.00 for very small prices. * Add translation key for project.bom.ext_price Adds the English translation "Extended Price" for the new BOM extended price column. Other languages are marked needs-translation and will be picked up by Crowdin. * Add build price summary to project info tab Displays the total BOM price for N builds on the project info page, using the existing price-tier logic from PricedetailHelper. The user can adjust the number of builds via a small form; the unit price is also shown when N > 1. New backend: - ProjectBuildHelper gains calculateTotalBuildPrice(), calculateUnitBuildPrice(), roundedTotalBuildPrice(), and roundedUnitBuildPrice() — bulk-order quantities are factored in so that price tiers apply correctly across N builds. - ProjectController::info() now reads ?n= and passes number_of_builds to the template. Template (_info.html.twig): - Adds price badge (hidden when no pricing data is available). - Adds number-of-builds form that reloads the info page. * Add tests for build price calculation in ProjectBuildHelper Covers calculateTotalBuildPrice(), calculateUnitBuildPrice(), roundedTotalBuildPrice(), and the private getBomEntryUnitPrice() helper. Scenarios tested: empty project, no pricing data, non-part BOM entries with manual prices, part entries with pricedetails, mixed entries, rounding-up of sub-cent prices, and minimum order amount floor for price tier lookup. * Deduplicate BOM entry price logic into ProjectBuildHelper The private getBomEntryUnitPrice() in ProjectBomEntriesDataTable was identical to the one in ProjectBuildHelper. Replaced it with a new public getEntryUnitPrice() on ProjectBuildHelper (returns BigDecimal, never null) and delegate to it from the DataTable. This eliminates the duplicate code and brings the DataTable lines under the existing ProjectBuildHelper test coverage. Added three tests for getEntryUnitPrice() covering the no-pricing, non-part, and part cases. * Added type hint to service --------- Co-authored-by: Jan Böhmer <mail@jan-boehmer.de>
1 parent 5b86d6f commit c17cf5e

16 files changed

Lines changed: 398 additions & 13 deletions

src/Controller/ProjectController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,13 @@ public function info(Project $project, Request $request, ProjectBuildHelper $bui
6969
return $table->getResponse();
7070
}
7171

72+
$number_of_builds = max(1, $request->query->getInt('n', 1));
73+
7274
return $this->render('projects/info/info.html.twig', [
7375
'buildHelper' => $buildHelper,
7476
'datatable' => $table,
7577
'project' => $project,
78+
'number_of_builds' => $number_of_builds,
7679
]);
7780
}
7881

src/DataTables/ProjectBomEntriesDataTable.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@
2929
use App\DataTables\Column\MarkdownColumn;
3030
use App\DataTables\Helpers\PartDataTableHelper;
3131
use App\Doctrine\Helpers\FieldHelper;
32-
use App\Entity\Parts\Part;
3332
use App\Entity\Parts\ManufacturingStatus;
33+
use App\Entity\Parts\Part;
3434
use App\Entity\ProjectSystem\ProjectBOMEntry;
3535
use App\Services\ElementTypeNameGenerator;
3636
use App\Services\EntityURLGenerator;
3737
use App\Services\Formatters\AmountFormatter;
38+
use App\Services\Formatters\MoneyFormatter;
39+
use App\Services\ProjectSystem\ProjectBuildHelper;
40+
use Brick\Math\RoundingMode;
3841
use Doctrine\ORM\AbstractQuery;
3942
use Doctrine\ORM\Query;
4043
use Doctrine\ORM\QueryBuilder;
@@ -50,7 +53,9 @@ public function __construct(
5053
protected EntityURLGenerator $entityURLGenerator,
5154
protected TranslatorInterface $translator,
5255
protected AmountFormatter $amountFormatter,
53-
protected PartDataTableHelper $partDataTableHelper
56+
protected PartDataTableHelper $partDataTableHelper,
57+
protected ProjectBuildHelper $projectBuildHelper,
58+
protected MoneyFormatter $moneyFormatter,
5459
) {
5560
}
5661

@@ -202,6 +207,27 @@ public function configure(DataTable $dataTable, array $options): void
202207
return '';
203208
}
204209
])
210+
->add('price', TextColumn::class, [
211+
'label' => 'project.bom.price',
212+
'visible' => false,
213+
'render' => function ($value, ProjectBOMEntry $context) {
214+
$price = $this->projectBuildHelper->getEntryUnitPrice($context);
215+
return $this->moneyFormatter->format($price->toScale(2, RoundingMode::UP)->toFloat(), null, 2, true);
216+
},
217+
])
218+
->add('ext_price', TextColumn::class, [
219+
'label' => 'project.bom.ext_price',
220+
'visible' => false,
221+
'render' => function ($value, ProjectBOMEntry $context) {
222+
$price = $this->projectBuildHelper->getEntryUnitPrice($context);
223+
return $this->moneyFormatter->format(
224+
$price->multipliedBy($context->getQuantity())->toScale(2, RoundingMode::UP)->toFloat(),
225+
null,
226+
2,
227+
true
228+
);
229+
},
230+
])
205231

206232
->add('addedDate', LocaleDateTimeColumn::class, [
207233
'label' => $this->translator->trans('part.table.addedDate'),

src/Services/ProjectSystem/ProjectBuildHelper.php

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,22 @@
2525
use App\Entity\Parts\Part;
2626
use App\Entity\ProjectSystem\Project;
2727
use App\Entity\ProjectSystem\ProjectBOMEntry;
28+
use App\Entity\PriceInformations\Currency;
2829
use App\Helpers\Projects\ProjectBuildRequest;
2930
use App\Services\Parts\PartLotWithdrawAddHelper;
31+
use App\Services\Parts\PricedetailHelper;
32+
use Brick\Math\BigDecimal;
33+
use Brick\Math\RoundingMode;
3034

3135
/**
3236
* @see \App\Tests\Services\ProjectSystem\ProjectBuildHelperTest
3337
*/
3438
final readonly class ProjectBuildHelper
3539
{
36-
public function __construct(private PartLotWithdrawAddHelper $withdraw_add_helper)
37-
{
40+
public function __construct(
41+
private PartLotWithdrawAddHelper $withdraw_add_helper,
42+
private PricedetailHelper $pricedetailHelper,
43+
) {
3844
}
3945

4046
/**
@@ -168,4 +174,81 @@ public function doBuild(ProjectBuildRequest $buildRequest): void
168174
$this->withdraw_add_helper->add($buildRequest->getBuildsPartLot(), $buildRequest->getNumberOfBuilds(), $message);
169175
}
170176
}
177+
178+
/**
179+
* Calculates the total price to build the given project N times, taking bulk pricing into account.
180+
* Returns null if no BOM entry has any pricing information.
181+
*/
182+
public function calculateTotalBuildPrice(Project $project, int $number_of_builds = 1, ?Currency $currency = null): ?BigDecimal
183+
{
184+
$total = BigDecimal::zero();
185+
$has_price = false;
186+
187+
foreach ($project->getBomEntries() as $entry) {
188+
$unit_price = $this->getBomEntryUnitPrice($entry, $number_of_builds, $currency);
189+
if ($unit_price === null) {
190+
continue;
191+
}
192+
$has_price = true;
193+
$total = $total->plus($unit_price->multipliedBy($entry->getQuantity())->multipliedBy($number_of_builds));
194+
}
195+
196+
return $has_price ? $total : null;
197+
}
198+
199+
/**
200+
* Calculates the price to build one unit of the given project when ordering for N builds in total.
201+
* Returns null if no BOM entry has any pricing information.
202+
*/
203+
public function calculateUnitBuildPrice(Project $project, int $number_of_builds = 1, ?Currency $currency = null): ?BigDecimal
204+
{
205+
$total = $this->calculateTotalBuildPrice($project, $number_of_builds, $currency);
206+
if ($total === null) {
207+
return null;
208+
}
209+
return $total->dividedBy($number_of_builds, 10, RoundingMode::HALF_UP);
210+
}
211+
212+
/**
213+
* Returns the total build price rounded up to 2 decimal places, ready for display.
214+
*/
215+
public function roundedTotalBuildPrice(Project $project, int $number_of_builds = 1, ?Currency $currency = null): ?BigDecimal
216+
{
217+
return $this->calculateTotalBuildPrice($project, $number_of_builds, $currency)
218+
?->toScale(2, RoundingMode::UP);
219+
}
220+
221+
/**
222+
* Returns the unit build price rounded up to 2 decimal places, ready for display.
223+
*/
224+
public function roundedUnitBuildPrice(Project $project, int $number_of_builds = 1, ?Currency $currency = null): ?BigDecimal
225+
{
226+
return $this->calculateUnitBuildPrice($project, $number_of_builds, $currency)
227+
?->toScale(2, RoundingMode::UP);
228+
}
229+
230+
/**
231+
* Returns the effective unit price for a single piece of the given BOM entry,
232+
* taking bulk pricing and minimum order amounts into account for N builds.
233+
* Returns BigDecimal::zero() when no pricing data is available.
234+
*/
235+
public function getEntryUnitPrice(ProjectBOMEntry $entry, int $number_of_builds = 1, ?Currency $currency = null): BigDecimal
236+
{
237+
return $this->getBomEntryUnitPrice($entry, $number_of_builds, $currency) ?? BigDecimal::zero();
238+
}
239+
240+
/**
241+
* Returns the effective unit price for a single piece of the given BOM entry,
242+
* taking bulk pricing into account for N builds.
243+
*/
244+
private function getBomEntryUnitPrice(ProjectBOMEntry $entry, int $number_of_builds, ?Currency $currency): ?BigDecimal
245+
{
246+
if ($entry->getPart() instanceof Part) {
247+
$total_qty = $entry->getQuantity() * $number_of_builds;
248+
$min_order = $this->pricedetailHelper->getMinOrderAmount($entry->getPart());
249+
$effective_qty = ($min_order !== null) ? max($total_qty, $min_order) : $total_qty;
250+
return $this->pricedetailHelper->calculateAvgPrice($entry->getPart(), $effective_qty, $currency);
251+
}
252+
return $entry->getPrice();
253+
}
171254
}

templates/projects/info/_info.html.twig

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,32 @@
5555
</span>
5656
</h6>
5757
</div>
58+
{% set n = number_of_builds ?? 1 %}
59+
{% set total_build_price = buildHelper.roundedTotalBuildPrice(project, n, app.user.currency ?? null) %}
60+
{% set unit_build_price = buildHelper.roundedUnitBuildPrice(project, n, app.user.currency ?? null) %}
61+
{% if total_build_price is not null %}
62+
<div class="mt-1">
63+
<h6>
64+
<span class="badge badge-primary bg-success">
65+
<i class="fa-solid fa-money-bill-wave fa-fw"></i>
66+
{% trans %}project.info.total_build_price{% endtrans %}:
67+
{{ total_build_price | format_money(app.user.currency ?? null, 2) }}
68+
{% if n > 1 and unit_build_price is not null %}
69+
<span class="ms-1">
70+
({% trans %}project.info.per_unit_price{% endtrans %}: {{ unit_build_price | format_money(app.user.currency ?? null, 2) }})
71+
</span>
72+
{% endif %}
73+
</span>
74+
</h6>
75+
</div>
76+
{% endif %}
77+
<form method="get" action="{{ path('project_info', {'id': project.id}) }}" class="mt-2">
78+
<div class="input-group input-group-sm">
79+
<span class="input-group-text">{% trans %}project.builds.number_of_builds{% endtrans %}</span>
80+
<input type="number" min="1" class="form-control" name="n" required value="{{ n }}">
81+
<button class="btn btn-outline-secondary" type="submit">{% trans %}project.build.btn_build{% endtrans %}</button>
82+
</div>
83+
</form>
5884
{% if project.children is not empty %}
5985
<div class="mt-1">
6086
<h6>
@@ -69,9 +95,9 @@
6995
</div>
7096

7197
{% if project.comment is not empty %}
72-
<p>
73-
<h5>{% trans %}comment.label{% endtrans %}:</h5>
74-
{{ project.comment|format_markdown }}
75-
</p>
98+
<div class="col-12 mt-2">
99+
<h5>{% trans %}comment.label{% endtrans %}:</h5>
100+
{{ project.comment|format_markdown }}
101+
</div>
76102
{% endif %}
77-
</div>
103+
</div>

0 commit comments

Comments
 (0)