<?php

use common\helpers\Price;
use common\logic\Order\ProductOrder\Module\ProductOrderModule;
use common\logic\Producer\Producer\Module\ProducerModule;
use common\logic\Product\Product\Model\Product;
use yii\helpers\Html;

$documentModule = $this->getDocumentModule();
$productModule = $this->getProductModule();
$productOrderModule = ProductOrderModule::getInstance();
$producerModule = ProducerModule::getInstance();

?>

<tr class="<?php if(isset($displayOrders) && $displayOrders): ?>order<?php endif; ?>">
        <td class="align-left column-product">
                <?php if($productOrder->product): ?>
                    <?= Html::encode($productOrder->product->name) ?>
                <?php endif; ?>
                <?php if($productOrder->unit == 'piece' && isset($productOrder->product->weight) && $productOrder->product->weight): ?>
                        <span class="weight"> / <?= $productOrder->product->weight ?> g</span>
                <?php endif; ?>
                <?php if($productOrder->product && strlen($productOrder->product->description) && $displayProductDescription): ?>
                        <br /><small><?= Html::encode($productOrder->product->description) ?></small>
                <?php endif; ?>
        </td>

        <?php
            $price = $productOrder->getPrice() ;
            if($documentModule->isInvoicePrice($document) && $productOrder->getInvoicePrice()) {
                    $price = $productOrder->getInvoicePrice() ;
            }
        ?>

        <?php if($displayPrices): ?>
                <td class="align-center column-unit-price">
                    <?php $displayPriceUnitReference = $producerModule->getSolver()->getConfig('option_document_display_price_unit_reference'); ?>
                    <?php $priceUnitReference = $productOrderModule->getSolver()->getPriceUnitReference($productOrder); ?>
                    <?php $priceUnitReferenceString = Price::format($priceUnitReference, $documentPriceDecimals).' / kg' ?>
                    <?php if($productOrder->unit == 'piece'): ?>
                        <?= Price::format($price, $documentPriceDecimals) ?>
                        <?php if($displayPriceUnitReference && $priceUnitReference): ?>
                            (<?= $priceUnitReferenceString ?>)
                        <?php endif; ?>
                    <?php else: ?>
                        <?= $priceUnitReferenceString ?>
                    <?php endif; ?>
                </td>
        <?php endif; ?>
        <td class="align-center column-quantity">
            <?= $productOrder->quantity *  Product::$unitsArray[$productOrder->unit]['coefficient'] ?>
            <?=  $productModule->getSolver()->strUnit($productOrder->product, 'wording') ?>
        </td>
        <?php if($displayPrices): ?>
                <?php if($producer->taxRate->value != 0): ?>
                        <td class="align-center column-tax-rate"><?= $productOrder->taxRate->value * 100 ?> %</td>
                <?php endif; ?>
                <td class="align-center column-price">
                        <?php if($documentModule->getClass($document) == ''): ?>
                                <?= Price::format($price * $productOrder->quantity) ?>
                        <?php else: ?>
                                <?= Price::format($price * $productOrder->quantity) ?>
                        <?php endif; ?>
                </td>
        <?php endif; ?>
</tr>
