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));
},
'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));
},
])

->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();
}

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