Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/DataTables/ProjectBomEntriesDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,21 @@
use App\Services\ElementTypeNameGenerator;
use App\Services\EntityURLGenerator;
use App\Services\Formatters\AmountFormatter;
use App\Services\Parts\PricedetailHelper;
use Doctrine\ORM\QueryBuilder;
use Omines\DataTablesBundle\Adapter\Doctrine\ORM\SearchCriteriaProvider;
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapter;
use Omines\DataTablesBundle\Column\TextColumn;
use Omines\DataTablesBundle\DataTable;
use Omines\DataTablesBundle\DataTableTypeInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Brick\Math\BigDecimal;

class ProjectBomEntriesDataTable implements DataTableTypeInterface
{
public function __construct(protected TranslatorInterface $translator, protected PartDataTableHelper $partDataTableHelper,
protected EntityURLGenerator $entityURLGenerator, protected AmountFormatter $amountFormatter)
protected EntityURLGenerator $entityURLGenerator, protected AmountFormatter $amountFormatter,
protected PricedetailHelper $pricedetailHelper)
{
}

Expand Down Expand Up @@ -179,6 +182,25 @@ public function configure(DataTable $dataTable, array $options): void
return '';
}
])
->add('price', TextColumn::class, [
'label' => 'project.bom.price',
'render' => function ($value, ProjectBOMEntry $context) {
$price = $this->getBomEntryUnitPrice($context);

// return the price
return htmlspecialchars(number_format($price->toFloat(),2));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about rounding prices up to the full cent?
This might be especially helpful for very cheap parts:
Currently resistors / caps might be shown as 0,00€, even while not being free.

Suggested change
return htmlspecialchars(number_format($price->toFloat(),2));
return htmlspecialchars(number_format($this->roundUpDisplayedPrice($price)->toFloat(), 2));

},
'visible' => false,
])
->add('ext_price', TextColumn::class, [
'label' => 'project.bom.ext_price',
'render' => function ($value, ProjectBOMEntry $context) {
$price = $this->getBomEntryUnitPrice($context);

// return the price
return htmlspecialchars(number_format($price->toFloat() * $context->getQuantity(),2));
},
Comment on lines +201 to +202
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return htmlspecialchars(number_format($price->toFloat() * $context->getQuantity(),2));
},
return htmlspecialchars(number_format($this->roundUpDisplayedPrice($price)->toFloat(), 2));
},

])

->add('addedDate', LocaleDateTimeColumn::class, [
'label' => $this->translator->trans('part.table.addedDate'),
Expand All @@ -205,6 +227,14 @@ function (QueryBuilder $builder) use ($options): void {
],
]);
}
private function getBomEntryUnitPrice(ProjectBOMEntry $entry): BigDecimal
{
if ($entry->getPart() instanceof Part) {
return $this->pricedetailHelper->calculateAvgPrice($entry->getPart(), $entry->getQuantity()) ?? BigDecimal::zero();
}

return $entry->getPrice() ?? BigDecimal::zero();
Comment on lines +232 to +236
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We previously not correctly handled cases with amount < minimum order amount.
This should fix it!

Suggested change
if ($entry->getPart() instanceof Part) {
return $this->pricedetailHelper->calculateAvgPrice($entry->getPart(), $entry->getQuantity()) ?? BigDecimal::zero();
}
return $entry->getPrice() ?? BigDecimal::zero();
if ($entry->getPart() instanceof Part) {
$amount = $entry->getQuantity();
$minOrderAmount = $this->pricedetailHelper->getMinOrderAmount($entry->getPart());
if ($minOrderAmount !== null) {
$amount = max($amount, $minOrderAmount);
}
return $this->pricedetailHelper->calculateAvgPrice($entry->getPart(), $amount) ?? BigDecimal::zero();
}
return $entry->getPrice() ?? BigDecimal::zero();

}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just forgot to add roundUpDisplayedPrice

Suggested change
private function roundUpDisplayedPrice(BigDecimal $price): BigDecimal
{
return $price->toScale(2, RoundingMode::UP);
}

private function getQuery(QueryBuilder $builder, array $options): void
{
Expand Down