YetiForceCompany/YetiForceCRM

View on GitHub
app/TextParser/ProductsTableShortVersion.php

Summary

Maintainability
D
1 day
Test Coverage
F
0%
<?php

namespace App\TextParser;

/**
 * Products table short version class.
 *
 * @package TextParser
 *
 * @copyright YetiForce S.A.
 * @license   YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com)
 * @author    Arkadiusz Sołek <a.solek@yetiforce.com>
 */
class ProductsTableShortVersion extends Base
{
    /** @var string Class name */
    public $name = 'LBL_PRODUCTS_TABLE_SHORT_VERSION';

    /** @var mixed Parser type */
    public $type = 'pdf';

    /**
     * Process.
     *
     * @return string
     */
    public function process()
    {
        $html = '';
        if (!$this->textParser->recordModel->getModule()->isInventory()) {
            return $html;
        }
        $inventory = \Vtiger_Inventory_Model::getInstance($this->textParser->moduleName);
        $inventoryRows = $this->textParser->recordModel->getInventoryData();
        $firstRow = current($inventoryRows);
        $baseCurrency = \Vtiger_Util_Helper::getBaseCurrency();
        if ($inventory->isField('currency')) {
            if (!empty($firstRow) && null !== $firstRow['currency']) {
                $currency = $firstRow['currency'];
            } else {
                $currency = $baseCurrency['id'];
            }
            $currencySymbol = \App\Fields\Currency::getById($currency)['currency_symbol'];
        } else {
            $currencySymbol = \App\Fields\Currency::getDefault()['currency_symbol'];
        }
        $headerStyle = 'font-size:9px;padding:0px 4px;text-align:center;background-color:#ddd;';
        $bodyStyle = 'font-size:8px;border:1px solid #ddd;padding:0px 4px;';
        $html .= '<table class="products-table-long-version" style="width:100%;font-size:8px;border-collapse:collapse;">
                <thead>
                    <tr>';
        $groupModels = [];
        foreach (['ItemNumber', 'Name',  'Quantity', 'UnitPrice', 'TotalPrice', 'GrossPrice']  as $fieldType) {
            foreach ($inventory->getFieldsByType($fieldType) as $fieldModel) {
                $columnName = $fieldModel->getColumnName();
                if (!$fieldModel->isVisible()) {
                    continue;
                }
                $html .= "<th style=\"{$headerStyle}\">" . \App\Language::translate($fieldModel->get('label'), $this->textParser->moduleName) . '</th>';
                $groupModels[$columnName] = $fieldModel;
            }
        }
        $html .= '</tr></thead>';
        if (!empty($groupModels)) {
            $html .= '<tbody>';
            $counter = 0;
            foreach ($inventoryRows as $inventoryRow) {
                ++$counter;
                $html .= '<tr class="row-' . $counter . '">';
                foreach ($groupModels as $fieldModel) {
                    $columnName = $fieldModel->getColumnName();
                    $typeName = $fieldModel->getType();
                    $fieldStyle = $bodyStyle;
                    if ('ItemNumber' === $typeName) {
                        $html .= "<td style=\"{$bodyStyle}font-weight:bold;\">" . $counter++ . '</td>';
                    } elseif ('ean' === $columnName) {
                        $code = $inventoryRow[$columnName];
                        $html .= "<td class=\"col-type-barcode\" style=\"{$fieldStyle}font-weight:bold;text-align:center;\"><div data-barcode=\"EAN13\" data-code=\"{$code}\" data-size=\"1\" data-height=\"16\">{$code}</div></td>";
                    } else {
                        $itemValue = $inventoryRow[$columnName];
                        if ('Name' === $typeName) {
                            $fieldStyle = $bodyStyle . 'text-align:left;';
                            $fieldValue = '<strong>' . $fieldModel->getDisplayValue($itemValue, $inventoryRow, true) . '</strong>';
                            foreach ($inventory->getFieldsByType('Comment') as $commentField) {
                                if ($commentField->isVisible() && ($value = $inventoryRow[$commentField->getColumnName()]) && $comment = $commentField->getDisplayValue($value, $inventoryRow, true)) {
                                    $fieldValue .= '<br />' . $comment;
                                }
                            }
                        } elseif (\in_array($typeName, ['GrossPrice', 'UnitPrice', 'TotalPrice']) && !empty($currencySymbol)) {
                            $fieldValue = \CurrencyField::appendCurrencySymbol($fieldModel->getDisplayValue($itemValue, $inventoryRow), $currencySymbol);
                            $fieldStyle = $bodyStyle . 'text-align:right;white-space: nowrap;';
                        } else {
                            $fieldValue = $fieldModel->getDisplayValue($itemValue, $inventoryRow, true);
                        }
                        $html .= "<td class=\"col-type-{$typeName}\" style=\"{$fieldStyle}\">" . $fieldValue . '</td>';
                    }
                }
                $html .= '</tr>';
            }
            $html .= '</tbody><tfoot><tr>';
            foreach ($groupModels as $fieldModel) {
                $headerStyle = 'font-size:7px;padding:0px 4px;text-align:center;background-color:#ddd;';
                $html .= "<th class=\"col-type-{$typeName}\" style=\"{$headerStyle}\">";
                if ($fieldModel->isSummary()) {
                    $sum = 0;
                    foreach ($inventoryRows as $inventoryRow) {
                        $sum += $inventoryRow[$fieldModel->getColumnName()];
                    }
                    $html .= \CurrencyField::appendCurrencySymbol(\CurrencyField::convertToUserFormat($sum, null, true), $currencySymbol);
                }
                $html .= '</th>';
            }
            $html .= '</tr></tfoot></table>';
        }
        return $html;
    }
}