diff --git a/src/DataTables/ProjectBomEntriesDataTable.php b/src/DataTables/ProjectBomEntriesDataTable.php index 433f6f78b..7bfa8a03d 100644 --- a/src/DataTables/ProjectBomEntriesDataTable.php +++ b/src/DataTables/ProjectBomEntriesDataTable.php @@ -32,6 +32,7 @@ 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; @@ -39,11 +40,13 @@ 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) { } @@ -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'), @@ -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 {