Files
kupshop/bundles/KupShop/AdminBundle/Util/PrintCenter/PrintLabelsBase.php
2025-08-02 16:30:27 +02:00

117 lines
3.9 KiB
PHP

<?php
namespace KupShop\AdminBundle\Util\PrintCenter;
use KupShop\KupShopBundle\Context\CurrencyContext;
abstract class PrintLabelsBase
{
protected $template = 'printCenter/products_generic.tpl';
protected $label;
protected $column = 1;
protected $rows = 1;
protected $types = [
'code' => [
'next' => 'ean',
'chars_type' => 'code128',
],
'ean' => [
'next' => 'code',
'chars_type' => 'ean13',
],
];
/**
* @var CurrencyContext
*/
private $currencyContext;
public function getLabel()
{
return $this->label;
}
public function getDimensions()
{
return [$this->column, $this->rows];
}
public function getTemplate()
{
return $this->template;
}
public function getData($IDs, $vars)
{
$products = [];
foreach ($IDs as $item) {
if (empty($item['idv'])) {
$item['idv'] = null;
}
if (!isset($item['pcs']) || $item['pcs'] == '') {
$item['pcs'] = $vars['pcs'];
}
$SQL = $this->fetchData($item)->execute()->fetchAll();
foreach ($SQL as $product) {
$product['pieces'] = $item['pcs'];
$product['discount'] = toDecimal($product['discount']);
$product['productPrice'] = new \KupShop\KupShopBundle\Util\Price\ProductPrice(toDecimal($product['price']), $this->currencyContext->getDefault(), $product['vat'], $product['discount']);
if (!empty($product[$vars['barcode_by']])) {
$product['barcode'] = [
'type' => $vars['barcode_by'],
'chars' => $vars['barcode_by'] === 'ean' ? formatEAN($product[$vars['barcode_by']]) : $product[$vars['barcode_by']],
'chars_type' => $this->types[$vars['barcode_by']]['chars_type'],
];
} elseif (!empty($product[$this->types[$vars['barcode_by']]['next']])) {
$product['barcode'] = [
'type' => $this->types[$vars['barcode_by']]['next'],
'chars' => $this->types[$vars['barcode_by']]['next'] === 'ean' ? formatEAN($product[$this->types[$vars['barcode_by']]['next']]) : $product[$this->types[$vars['barcode_by']]['next']],
'chars_type' => $this->types[$this->types[$vars['barcode_by']]['next']]['chars_type'],
];
} else {
$product['barcode'] = [];
}
if (!empty($product['producer_photo'])) {
$product['producer_photo'] = @getImage($product['producer'], $product['producer_photo'], '../producer/', 7);
}
$products[] = $product;
}
}
return $products;
}
public function fetchData($item)
{
$fields = 'p.id as id_product, pv.id as id_variation, pv.title variation_title, p.title product_title, COALESCE(pv.price, p.price) as price, v.vat, COALESCE(pv.ean, p.ean) as ean, p.discount';
if (findModule(\Modules::PRODUCTS_VARIATIONS, \Modules::SUB_CODE)) {
$fields .= ', COALESCE(pv.code, p.code) as code';
} else {
$fields .= ', p.code as code';
}
$qb = sqlQueryBuilder()->select($fields)
->from('products', 'p')
->leftJoin('p', 'products_variations', 'pv', 'pv.id_product=p.id')
->leftJoin('p', 'vats', 'v', 'v.id=p.vat')
->where(\Query\Operator::equals(['p.id' => $item['idp']]))
->andWhere(\Query\Operator::equalsNullable(['pv.id' => $item['idv']]));
return $qb;
}
/**
* @required
*/
public function setCurrencyContext(CurrencyContext $currencyContext): void
{
$this->currencyContext = $currencyContext;
}
}