Common/View/ViewBuilder.php
private function buildSaleItemLineView(Model\SaleItemInterface $item, $level = 0)
    {
        if (!$this->options['private'] && $item->isPrivate()) {
            return null;
        }

        $lineNumber = $this->lineNumber++;

        $view = new LineView(
            'item_' . ($lineNumber - 1),
            'item_' . $item->getId(),
            $lineNumber,
            $level
        );

        $view->addClass('level-' . $level);

        $result = $item->getResult();

        $unit = $gross = $discountRates = $discountAmount = $base = $taxRates = $taxAmount = $total = null;

        if (!($item->isCompound() && !$item->hasPrivateChildren())) {
            $ati = $this->view->isAti();
            $unit = $this->formatter->currency($result->getUnit($ati));
            $gross = $this->formatter->currency($result->getGross($ati));
            $discountRates = $this->formatter->rates(...$result->getDiscountAdjustments());
            if (0 < $discount = $result->getDiscount($ati)) {
                $discountAmount = $this->formatter->currency($discount);
            }
            $base = $this->formatter->currency($result->getBase($ati));
            $taxRates = $this->formatter->rates(...$result->getTaxAdjustments());
            if (0 < $tax = $result->getTax()) {
                $taxAmount = $this->formatter->currency($tax);
            }
            $total = $this->formatter->currency($result->getTotal());
        }

        // TODO Use packaging format
        if ($item->isPrivate()) {
            $quantity = sprintf(
                '%s (x%s)',
                $this->formatter->number($item->getQuantity()),
                $this->formatter->number($item->getParentsQuantity())
            );
        } else {
            $quantity = $this->formatter->number($item->getTotalQuantity());
        }

        $view
            ->setDesignation($item->getDesignation())
            ->setDescription($item->getDescription())
            ->setReference($item->getReference())
            ->setUnit($unit)
            ->setQuantity($quantity)
            ->setGross($gross)
            ->setDiscountRates($discountRates)
            ->setDiscountAmount($discountAmount)
            ->setBase($base)
            ->setTaxRates($taxRates)
            ->setTaxAmount($taxAmount)
            ->setTotal($total);

        if ($item->isPrivate()) {
            $view->setPrivate(true)->addClass('private');
        } else {
            $view->setPrivate(false)->removeClass('private');
        }

        foreach ($this->types as $type) {
            $type->buildItemView($item, $view, $this->options);
        }

        if (!empty($view->getAvailability())) {
            $this->view->vars['show_availability'] = true;
        }

        if ($this->view->vars['show_margin'] && !($item->isCompound() && !$item->hasPrivateChildren())) {
            if (null !== $margin = $item->getMargin()) {
                $view->setMargin(
                    ($margin->isAverage() ? '~' : '') .
                    $this->formatter->percent($margin->getPercent())
                );
            }
        }

        if ($item->hasChildren()) {
            foreach ($item->getChildren() as $child) {
                if (null !== $line = $this->buildSaleItemLineView($child, $level + 1)) {
                    $view->addLine($line);
                }
            }
        }

        return $view;
    }
private function buildSaleItemLineView(Model\SaleItemInterface $item, $level = 0)
    {
        if (!$this->options['private'] && $item->isPrivate()) {
            return null;
        }

        $lineNumber = $this->lineNumber++;

        $view = new LineView(
            'item_' . ($lineNumber - 1),
            'item_' . $item->getId(),
            $lineNumber,
            $level
        );

        $view->addClass('level-' . $level);

        $result = $item->getResult();

        $unit = $gross = $discountRates = $discountAmount = $base = $taxRates = $taxAmount = $total = null;

        if (!($item->isCompound() && !$item->hasPrivateChildren())) {
            $ati = $this->view->isAti();
            $unit = $this->formatter->currency($result->getUnit($ati));
            $gross = $this->formatter->currency($result->getGross($ati));
            $discountRates = $this->formatter->rates(...$result->getDiscountAdjustments());
            if (0 < $discount = $result->getDiscount($ati)) {
                $discountAmount = $this->formatter->currency($discount);
            }
            $base = $this->formatter->currency($result->getBase($ati));
            $taxRates = $this->formatter->rates(...$result->getTaxAdjustments());
            if (0 < $tax = $result->getTax()) {
                $taxAmount = $this->formatter->currency($tax);
            }
            $total = $this->formatter->currency($result->getTotal());
        }

        // TODO Use packaging format
        if ($item->isPrivate()) {
            $quantity = sprintf(
                '%s (x%s)',
                $this->formatter->number($item->getQuantity()),
                $this->formatter->number($item->getParentsQuantity())
            );
        } else {
            $quantity = $this->formatter->number($item->getTotalQuantity());
        }

        $view
            ->setDesignation($item->getDesignation())
            ->setDescription($item->getDescription())
            ->setReference($item->getReference())
            ->setUnit($unit)
            ->setQuantity($quantity)
            ->setGross($gross)
            ->setDiscountRates($discountRates)
            ->setDiscountAmount($discountAmount)
            ->setBase($base)
            ->setTaxRates($taxRates)
            ->setTaxAmount($taxAmount)
            ->setTotal($total);

        if ($item->isPrivate()) {
            $view->setPrivate(true)->addClass('private');
        } else {
            $view->setPrivate(false)->removeClass('private');
        }

        foreach ($this->types as $type) {
            $type->buildItemView($item, $view, $this->options);
        }

        if (!empty($view->getAvailability())) {
            $this->view->vars['show_availability'] = true;
        }

        if ($this->view->vars['show_margin'] && !($item->isCompound() && !$item->hasPrivateChildren())) {
            if (null !== $margin = $item->getMargin()) {
                $view->setMargin(
                    ($margin->isAverage() ? '~' : '') .
                    $this->formatter->percent($margin->getPercent())
                );
            }
        }

        if ($item->hasChildren()) {
            foreach ($item->getChildren() as $child) {
                if (null !== $line = $this->buildSaleItemLineView($child, $level + 1)) {
                    $view->addLine($line);
                }
            }
        }

        return $view;
    }
Builds the sale line view.

@param Model\SaleItemInterface $item
@param int                     $level

@return LineView|null