<?php declare(strict_types=1);
namespace App\StartPlatz\Bundle\MemberBundle\Entity;
use App\StartPlatz\Bundle\LogBundle\Entity\Errorlog;
use App\StartPlatz\Bundle\MonsumBundle\Entity\Customer;
use App\StartPlatz\Bundle\SettingBundle\Entity\Space;
use DateTime;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use App\StartPlatz\Bundle\FibuBundle\Entity\Product as FibuProduct;
use App\StartPlatz\Bundle\FibuBundle\Entity\Position;
use App\StartPlatz\Bundle\MonsumBundle\Entity\Article;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
/**
* ProductRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class ProductRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $managerRegistry)
{
parent::__construct($managerRegistry, Product::class /*Product Member*/);
}
public function isPurchaseTemplateSet(Product $product)
{
$em = $this->getEntityManager();
if (!$product->getPurchaseTemplate()) {
$template = [
'stepOneTitle' => "Produkt prüfen",
'backgroundImage' => "https://res.cloudinary.com/startplatz/image/upload/v1588517648/header/STARTPLATZ_MEDIAPARK.jpg",
'stepTwoTitle' => "Checkout",
'receiptTitle' => "Quittung / Receipt",
'receiptText' => "Ihr Einkauf war erfolgreich. // Your purchase was successful",
];
$product->setPurchaseTemplate(json_encode($template));
$em->persist($product);
$em->flush();
}
return $product;
}
public function findPaginated($criteria = [], $order = [], $page = 1, $limit = 5)
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb
->select('p')
->from(Product::class /*Product Member*/, 'p')
->setFirstResult($limit * ($page - 1)) // Offset
->setMaxResults($limit) // Limit
;
$qb = $this->setCriteria($qb, $criteria);
if ($order) {
foreach ($order as $key => $value) {
$qb->orderBy('p.' . $key, $value);
}
} else {
$qb->addOrderBy('p.lastModified', 'DESC');
}
$query = $qb->getQuery();
$results = $query->getResult();
return $results;
}
private function setCriteria(QueryBuilder $qb, $criteria)
{
foreach($criteria as $field =>$value) {
switch ($field) {
case "partners":
$qb->andWhere("p.type IN (:types) ");
$qb->setParameter(':types', $value);
break;
case "accounts":
$qb->andWhere("p.account IN (:array) ");
$qb->setParameter(':array', $value);
break;
case "tags":
$qb->andWhere('p.'. $field . ' LIKE :' . $field);
$qb->setParameter($field, "%{$value}%");
break;
default:
$qb->andWhere('p.'. $field . ' = :' . $field);
$qb->setParameter($field, $value);
break;
}
}
return $qb;
}
public function countByCriteria($criteria = [])
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb
->select('COUNT(p)')
->from(Product::class /*Product Member*/, 'p')
;
$qb = $this->setCriteria($qb, $criteria);
return $qb->getQuery()->getSingleScalarResult();
}
public function deleteProduct(Product $product)
{
$em = $this->getEntityManager();
// if used in entity space set to null
$em->getRepository(Space::class)->setProductIdToNullIfUsed($product->getId());
$em->remove($product);
$em->flush();
return "Product deleted";
}
public function setField($field, $action, $id)
{
$em = $this->getEntityManager();
$field = (in_array($field, ['status']) ? $field : 'status');
if (!$entity = $this->findOneBy(['id' => $id])) {
return 'ERROR: entity not found';
}
$method = 'set' . ($field);
$entity->$method($action);
$entity->setLastModified(new DateTime());
$em->persist($entity);
$em->flush();
return 'field ' . $field . ' set to ' . $action;
}
public function updateRelations(Product $product)
{
$connection = $this->getEntityManager()->getConnection();
$sql ="UPDATE aaa_sp_services p
SET p.productType = :type,
p.productName = :name
WHERE p.productId = :productId" ;
$connection->executeQuery($sql, ['type' => $product->getType(), 'name' => $product->getName(), 'productId' => $product->getId()]);
$sql ="UPDATE sp_fibu_easybill_positions p
SET p.allmedaProductType = :type,
p.allmedaProductId = :productId
WHERE p.positionId = :easybillPositionId" ;
$connection->executeQuery($sql, ['type' => $product->getType(), 'name' => $product->getName(), 'productId' => $product->getId(), 'easybillPositionId' => $product->getEasybillPositionId()]);
$sql ="UPDATE sp_monsum_items p
SET p.allmedaProductType = :type
WHERE p.allmedaProductId = :productId" ;
$connection->executeQuery($sql, ['type' => $product->getType(), 'name' => $product->getName(), 'productId' => $product->getId(), 'easybillPositionId' => $product->getEasybillPositionId()]);
return "Services and Easybill positions updated for product {$product->getName()}, Monsum Items updated for allmedaProductId";
}
public function setDefaultValues(Product $product)
{
$now = new DateTime();
$product->setStatus('active');
$product->setBookingAccount('44010');
$product->setNoticeDays(0);
return $product;
}
public function getSettingsDefault(Product $product)
{
return [
"special-offer" => [
"price" => "",
"description" => "",
"name" => "",
],
];
}
public function setMonsumArticleId(Product $product, Article $article, $adminEmail)
{
$em = $this->getEntityManager();
$now = new DateTime();
$product->setMonsumArticleId($article->getArticleId());
$product->setLastModified($now);
$product->setLastChangeUser($adminEmail);
$em->persist($product);
$em->flush();
return $product;
}
public function createOrUpdateProductByMonsumArticle(Article $article, $adminEmail)
{
$em = $this->getEntityManager();
$now = new DateTime();
if (!$product = $this->findOneBy(['monsumArticleId' => $article->getArticleId()])) {
if (!$product = $this->findOneBy(['account' => $article->getAccount(),'agent'=>'monsum', 'productNumber' => $article->getArticleNumber() ])) {
$product = new Product();
$product->setMonsumArticleId($article->getArticleId());
$product->setAccount($article->getAccount());
$product->setAgent('monsum');
$product->setType('Other');
$product->setBookingAccount('44010');
$product->setStatus('active');
$product->setLocation($em->getRepository(Customer::class)->getLocationByAccount($article->getAccount()));
} else {
// product has been cloned
$product->setMonsumArticleId($article->getArticleId());
}
}
$product->setMonsumArticleNumber($article->getArticleNumber());
$product->setName($article->getTitle());
$product->setProductNumber($article->getArticleNumber());
$product->setIsAddon($article->getIsAddon());
$product->setMonsumSettings(json_encode($em->getRepository(Article::class /*Monsum Article*/)->getMonsumSettingsByArticle($article)));
$product->setPrice($article->getUnitPrice());
$product->setDescription($article->getDescription());
$product->setLastModified($now);
$product->setLastChangeUser($adminEmail);
$logText = "createOrUpdateProductByMonsumArticle";
$product->setHistory($this->writeHistory($product, $logText, $adminEmail));
$em->persist($product);
$em->flush();
return $product;
}
private function writeHistory(Product $product, $logText, $adminEmail){
$product->setHistory('==== ' . date("Y-m-d") . ' by ' . $product->getLastChangeUser() . ' ===' . PHP_EOL . $logText . PHP_EOL . $product->getHistory());
return $product;
}
public function createOrUpdateProductExceptionByMonsum($settings)
{
$em = $this->getEntityManager();
$now = new DateTime();
$account = $settings['account'];
$articleNumber = '99999';
if (!$product = $this->findOneBy(['monsumArticleNumber' => $articleNumber, 'account' => $account])) {
$product = new Product();
$product->setAccount($settings['account']);
$product->setAgent('monsum');
}
$product->setMonsumArticleNumber($articleNumber);
$product->setName("Exception");
$product->setProductNumber($product->getMonsumArticleNumber());
$product->setType('Exception');
$product->setStatus('active');
$product->setBookingAccount('44010');
$product->setDescription("Exception");
$product->setLastModified($now);
$product->setLastChangeUser($settings['adminEmail']);
$em->persist($product);
$em->flush();
return $product;
}
public function createOrUpdateProductByMonsumSubscriptionPlan($plan, $settings)
{
$em = $this->getEntityManager();
$now = new DateTime();
$articleId = $plan->ARTICLE_ID;
if (!$product = $this->findOneBy(['monsumArticleId' => $articleId])) {
$product = new Product();
$product->setMonsumArticleId($articleId);
$product->setAccount($settings['account']);
$product->setAgent('monsum');
$product->setType('Other');
}
$product->setMonsumArticleNumber($plan->ARTICLE_NUMBER);
$product->setName($plan->TITLE);
$product->setProductNumber($product->getMonsumArticleNumber());
$product->setStatus('active');
$product->setBookingAccount('44010');
$product->setPrice($plan->UNIT_PRICE);
$product->setDescription($plan->DESCRIPTION);
$product->setLastModified($now);
$product->setLastChangeUser($settings['adminEmail']);
$logText = "from subscription {$plan->ARTICLE_NUMBER} by createOrUpdateProductByMonsumSubscriptionPlan";
$product->setHistory($this->writeHistory($product, $logText, $settings['adminEmail']));
$em->persist($product);
$em->flush();
return $product;
}
public function createOrUpdateProductByMonsumInvoiceItem($item, $settings)
{
$em = $this->getEntityManager();
$now = new DateTime();
if (!$product = $this->findOneBy(['monsumArticleNumber' => $item->ARTICLE_NUMBER, 'account'=>$settings['account']])) {
$product = new Product();
$product->setMonsumArticleNumber($item->ARTICLE_NUMBER);
$product->setAccount($settings['account']);
$product->setAgent('monsum');
$product->setType('Other');
}
$product->setName($item->DESCRIPTION);
$product->setProductNumber($product->getMonsumArticleNumber());
$product->setStatus('active');
$product->setBookingAccount('44010');
$product->setPrice($item->UNIT_PRICE);
$product->setDescription($item->DESCRIPTION);
$product->setLastModified($now);
$product->setLastChangeUser($settings['adminEmail']);
$logText = "modified from invoiceItem {$item->INVOICE_ITEM_ID} by createOrUpdateProductByMonsumInvoiceItem";
$product->setHistory($this->writeHistory($product, $logText, $settings['adminEmail']));
$em->persist($product);
$em->flush();
return $product;
}
/******** easybill create or update starts here ***/
public function findExistingProduct( $positionId, string $account, ?string $productNumber = null): ?Product
{
// Immer erst nach PositionId UND Account suchen
$product = $this->findOneBy([
'easybillPositionId' => $positionId,
'account' => $account
]);
// Fallback-Suche nach Produktnummer UND Account
if (!$product && $productNumber) {
$product = $this->findOneBy([
'productNumber' => $productNumber,
'account' => $account
]);
}
return $product;
}
public function initializeNewProduct(
string $positionId,
string $account,
?string $productNumber = null,
string $adminEmail
): array {
$product = new Product();
$product->setEasybillPositionId($positionId);
$product->setAccount($account);
$location = match($account) {
'AI-Hub' => 'AI-Hub',
'SP-DUS' => 'DUS',
default => 'CGN'
};
$product->setLocation($location);
$product->setAgent('easybill');
$product->setType('Other');
if ($productNumber) {
$product->setProductNumber($productNumber);
$product->setPrevNumber($productNumber);
}
// Logging und Response
$activity = "SUCCESS: Allmeda Product created {$product->getName()} with type = 'Other' and easybillPositionId={$positionId}";
$this->getEntityManager()->getRepository(Errorlog::class)->logError(
$activity,
"createOrUpdateProductByEasybillProduct",
$adminEmail,
"allmedaProduct",
$product->getId()
);
return [
'product' => $product,
'response' => $activity
];
}
public function createOrUpdateProductByEasybillPosition(Position $position, $adminEmail)
{
$em = $this->getEntityManager();
$now = new DateTime();
if (!$product = $this->findOneBy(['easybillPositionId' => $position->getPositionId()])) {
$product = new Product();
$product->setEasybillPositionId($position->getPositionId());
$product->setAccount($position->getAccount());
$product->setLocation(($position->getAccount() == 'SP-DUS' ? 'DUS' : 'CGN'));
$product->setAgent('easybill');
}
$product->setStatus('active');
$product->setBookingAccount($position->getBookingAccount());
$description = mb_substr(preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $position->getDescription()), 0, 255);
$product->setName($description);
$product->setPrice($position->getSinglePriceNet());
$product->setDescription($description);
$product->setLastModified($now);
$product->setLastChangeUser($adminEmail);
$em->persist($product);
$em->flush();
return $product;
}
/**
* Aktualisiert ein bestehendes Produkt mit Daten aus einem FibuProduct
*
* @param Product $allmedaProduct Das zu aktualisierende Produkt
* @param FibuProduct $fibuProduct Das Quellprodukt mit den neuen Daten
* @param string $adminEmail E-Mail für Logging-Zwecke
* @return array Ergebnis der Aktualisierung
*/
public function updateExistingProduct(
Product $allmedaProduct,
FibuProduct $fibuProduct,
string $adminEmail
): array {
// Aktualisiere relevante Felder basierend auf $fibuProduct
// Hier müssen die konkreten Felder ergänzt werden, die aktualisiert werden sollen
// Beispiel:
// $allmedaProduct->setName($fibuProduct->getName());
// $allmedaProduct->setDescription($fibuProduct->getDescription());
$now = new DateTime();
$allmedaProduct->setStatus('active');
$allmedaProduct->setBookingAccount($fibuProduct->getBookingAccount());
$description = mb_substr(preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $fibuProduct->getDescription()), 0, 255);
$allmedaProduct->setName($description);
$allmedaProduct->setPrice($fibuProduct->getPrice());
$allmedaProduct->setVatPercent($fibuProduct->getVatPercent());
$allmedaProduct->setDescription($description);
$allmedaProduct->setLastModified($now);
$allmedaProduct->setLastChangeUser($adminEmail);
$this->getEntityManager()->persist($allmedaProduct);
$this->getEntityManager()->flush();
// Logging
$activity = "Allmeda Product updated: {$allmedaProduct->getName()} with ID={$allmedaProduct->getId()}";
$this->getEntityManager()->getRepository(Errorlog::class)->logError(
$activity,
"updateExistingProduct",
$adminEmail,
"allmedaProduct",
$allmedaProduct->getId()
);
return [
'product' => $allmedaProduct,
'response' => $activity
];
}
public function createOrUpdateProductByEasybillApiPosition($item, $account, $adminEmail)
{
$em = $this->getEntityManager();
$now = new DateTime();
$response = '';
$apiData = $this->getApiData($item);
$positionId = $item->id;
$price = $item->sale_price /100; // otherwise we get an input error with numbers > 999
if (!$product = $this->findOneBy(['easybillPositionId' => $positionId, 'account'=>$account])) {
if (!$product = $this->findOneBy(['productNumber' => $item->number, 'account'=>$account])) {
$product = new Product();
$product->setAccount($account);
// Determine the location using a switch statement
switch ($product->getAccount()) {
case 'AI-Hub':
$location = 'AI-Hub';
break;
case 'SP-DUS':
$location = 'DUS';
break;
default:
$location = 'CGN'; // Default value
break;
}
// Set the location
$product->setLocation($location);
$product->setAgent('easybill');
$product->setType('Other');
$product->setName($apiData['name']);
$product->setProductNumber($apiData['productNumber']);
$product->setPrevNumber($apiData['productNumber']);
$product->setEasybillPositionId($positionId);
$activity = "SUCCESS: created {$product->getName()}, with type = 'Other' and easybillPositionId={$product->getEasybillPositionId()}";
} else {
$product->setEasybillPositionId($positionId);
$activity = "SUCCESS: updated {$product->getName()}, with easybillPositionId={$product->getEasybillPositionId()}";
}
//$em->getRepository(Errorlog::class)->logError($activity, "createOrUpdateProductByEasybillProduct", $adminEmail, "allmedaProduct", $product->getId());
$response = $activity;
}
$allmedaData = $this->getAllmedaData($product);
$changes = array_diff_assoc($allmedaData, $apiData);
if ($changes) {
$product->setStatus('active');
$product->setBookingAccount($apiData['bookingAccount']);
$product->setPrice($price);
$product->setDescription($apiData['description']);
$product->setEasybillGroupId($apiData['groupId']);
$product->setName($apiData['name']);
if ($product->getProductNumber() < '99000' and $product->getProductNumber() != $apiData['productNumber']) {
// check if the combination of account, agent and productNumber is already in use
if ($this->countByCriteria(['account' => $product->getAccount(), 'agent' => $product->getAgent(), 'productNumber' => $apiData['productNumber']])) {
$newProductNumber = $this->findNextExceptionProductNumber();
$product->setProductNumber($newProductNumber);
$activity = "ERROR: Reset ProductNumber {$apiData['productNumber']} to {$product->getProductNumber()}";
$em->getRepository(Errorlog::class)->logError($activity, "createOrUpdateProductByEasybillProduct", $adminEmail, "allmedaProduct", $product->getId());
} else {
$product->setProductNumber($apiData['productNumber']);
}
} else {
$product->setProductNumber($apiData['productNumber']);
}
$product->setLastModified($now);
$product->setLastChangeUser($adminEmail);
$em->persist($product);
$em->flush();
$jsonChanges = json_encode($changes);
$apiData = json_encode($apiData);
$response .= " createOrUpdateProductByEasybillApiPosition: updated {$product->getId()} {$product->getName()} with type = {$product->getType()} and changes {$jsonChanges} and apiData {$apiData}";
} else {
$response .= " createOrUpdateProductByEasybillApiPosition: exists already and no update needed {$product->getId()} {$product->getName()} - Easybill position {$positionId}";
}
return ['meta' => $response, 'entity' => $product] ;
}
public function updateExistingAllmedaProductByEasybillProduct(Product $product, \App\StartPlatz\Bundle\FibuBundle\Entity\Product $position, $adminEmail)
{
$em = $this->getEntityManager();
$now = new DateTime();
$response = '';
$storedData = $this->getStoredData($product);
$fetchedData = $this->getFetchedData($position);
$changes = array_diff_assoc($fetchedData, $storedData);
if ($changes) {
$product->setAccount($position->getAccount());
$location = match($position->getAccount()) {
'AI-Hub' => 'AI-Hub',
'SP-DUS' => 'DUS',
default => 'CGN'
};
$product->setLocation($location);
$product->setStatus('active');
$product->setBookingAccount($position->getBookingAccount());
$description = $position->getDescription();
$product->setPrice($position->getSalePrice());
$product->setName($position->getName());
$product->setDescription($description);
$product->setEasybillGroupId($position->getGroupId());
$product->setEasybillPositionId($position->getPositionId());
$product->setAgent('easybill');
$product->setProductNumber($position->getNumber());
$product->setLastModified($now);
$product->setLastChangeUser($adminEmail);
$em->persist($product);
$em->flush();
$this->updateRelations($product);
$jsonChanges = json_encode($changes);
$response .= "Allmeda Product updated {$product->getId()} {$product->getName()} with type = {$product->getType()} and changes {$jsonChanges} and relations updated";
} else {
$response .= "Allmeda Product exists already and no update needed {$product->getId()} {$product->getName()} - Easybill position {$position->getPositionId()}";
}
return [
'response' => $response,
'product' => $product
];
}
private function findNextExceptionProductNumber()
{
if ($exception = $this->findOneBy(['productNumber' => '99001'])) {
$lastException = $this->findOneBy([], ['productNumber' => 'DESC']);
$nextProductNumber = $lastException->getProductNumber() + 1;
} else {
$nextProductNumber = '99001';
}
return $nextProductNumber;
}
private function getApiData($item)
{
$description = $item->description;
$data = ['description' => $description, 'name' => trim(explode(PHP_EOL, (string) $description)[0]), 'bookingAccount' => $item->export_identifier > "" ? $item->export_identifier : '99999', 'groupId' => $item->group_id, 'price' => number_format((float) $item->sale_price /100, 2), 'productNumber' => $item->number, 'vatPercent' => $item->vat_percent];
return $data;
}
private function getAllmedaData(Product $product)
{
$data = ['description' => $product->getDescription(), 'name' => $product->getName(), 'bookingAccount' => $product->getBookingAccount(), 'groupId' => $product->getEasybillGroupId(), 'price' => number_format((float) $product->getPrice(), 2), 'productNumber' => $product->getProductNumber(), 'vatPercent' => $product->getVatPercent()];
return $data;
}
private function getStoredData(Product $product)
{
$data = ['positionId' => $product->getEasybillPositionId(), 'name' => $product->getName(), 'account' => $product->getAccount(), 'productNumber' => $product->getProductNumber(), 'BookingAccount' => $product->getBookingAccount(), 'description' => $product->getDescription(), 'Price' => number_format((float) $product->getPrice(), 2), 'agent' => $product->getAgent(), 'groupId' => $product->getEasybillGroupId()];
return $data;
}
private function getFetchedData(\App\StartPlatz\Bundle\FibuBundle\Entity\Product $position)
{
$description = $position->getDescription();
$name = $position->getName();
$data = ['positionId' => $position->getPositionId() ,'name' => $position->getName(), 'account' => $position->getAccount(), 'productNumber' => $position->getNumber(), 'BookingAccount' => $position->getBookingAccount(), 'description' => $description, 'Price' => number_format((float) $position->getSalePrice(), 2), 'agent' => 'easybill', 'groupId' => $position->getGroupId()];
return $data;
}
public function getAggregateByGroup($group, $filter)
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select(['t.' . $group, 'COUNT(t.id) as number']);
$qb->from(Product::class /*Product Member*/, 't');
if ($filter) {
//$qb->where('t.month = :filter');
//$qb->setParameter(':filter', $filter);
}
$qb->groupBy('t.'. $group);
return $qb->getQuery()->getResult();
}
public function findOneBySpaceId($spaceId)
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('p')
->from(Product::class /*Product Member*/, 'p')
->from(Space::class, 's')
->where('s.productId = p.id')
->andWhere('s.id = :spaceId')
->setParameter('spaceId', $spaceId)
;
return $qb->getQuery()->getOneOrNullResult();
}
public function findByQueryString($queryString, $sortArray = [])
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select(['p']);
$qb->from(Product::class /*Product Member*/, 'p');
$qb->where('p.name LIKE :queryString OR p.id LIKE :queryString OR p.sortName LIKE :queryString OR p.description LIKE :queryString ');
$queryString = '%' . $queryString . '%';
$qb->setParameter(':queryString', $queryString);
if (isset($sortArray)) {
foreach($sortArray as $field => $order) {
$qb->addOrderBy('p.'. $field, $order);
}
} else {
$qb->orderBy('p.name', 'DESC');
}
return $qb->getQuery()->getResult();
}
public function findByLocation($location)
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select(['p']);
$qb->from(Product::class /*Product Member*/, 'p');
$qb->where('p.location = :location OR p.location = ' . "'SP'");
$qb->setParameter(':location', $location);
$qb->orderBy('p.name', 'DESC');
return $qb->getQuery()->getResult();
}
//
public function countParts($field, $criteria)
{
$qb = $this->getEntityManager()->createQueryBuilder();
$field = 'p.' . $field;
$qb->select([$field, 'COUNT(' .$field .') as total']);
$qb->from(Product::class /*Product Member*/, 'p');
if ($criteria) {
foreach ($criteria as $key => $value) {
$qb->andWhere('p.' . $key . ' = :param');
$qb->setParameter(':param', $value);
}
}
$qb->groupBy($field);
return $qb->getQuery()->getResult();
}
public function findOneByAndGetArrayResult($criteria)
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select(['p']);
$qb->from(Product::class /*Product Member*/, 'p');
$field = key($criteria);
$qb->where('p.'. $field . ' = :queryString');
$queryString = $criteria[$field];
$qb->setParameter(':queryString', $queryString);
return $qb->getQuery()->getOneOrNullResult(2);
}
public function findByAndGetArrayResult($criteria)
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select(['p']);
$qb->from(Product::class /*Product Member*/, 'p');
$field = key($criteria);
$qb->where('p.'. $field . ' = :queryString');
$queryString = $criteria[$field];
$qb->setParameter(':queryString', $queryString);
return $qb->getQuery()->getArrayResult();
}
public function getProductData(Product $product)
{
return ['name' => $product->getName(), 'price' => (float) $product->getPrice(), 'type' => $product->getType(), 'agent' => $product->getAgent(), 'account' => $product->getAccount(), 'description' => $product->getDescription(), 'imageUrl' => $product->getImageUrl(), 'faq' => $product->getFaq(), 'isBookableByMember' => $product->getIsBookableByMember(), 'isBookableByAdmin' => $product->getIsBookableByAdmin(), 'easybillPositionId' => $product->getEasybillPositionId()];
}
public function getProductList()
{
$products = $this->findBy([], ['type'=>'ASC', 'productNumber' => 'ASC', 'account' => 'ASC']);
$productIds = array_map(fn (Product $product) => $product->getId(), $products);
$productNames = array_map(fn (Product $product) => "{$product->getType()} - {$product->getProductNumber()}: {$product->getAccount()}-{$product->getName()}", $products);
return array_combine($productIds, $productNames);
}
public function find4Search()
{
$connection = $this->getEntityManager()->getConnection();
$sql ="SELECT p.id, p.name, p.status, p.location, p.price, p.type, p.easybillPositionId, p.account, p.agent, p.productNumber
FROM aaa_sp_products p
";
return $connection->fetchAllAssociative($sql, []);
}
public function getTemplateVars($request)
{
$templateVars = ['filter' => null, 'tag' => []];
$now = new DateTime();
if ($tag = $request->query->get('tag')) {
$templateVars['tag'] = trim((string) $tag);
}
if ($location = $request->query->get('location')) {
$templateVars['location'] = $location;
}
if ($status = $request->query->get('status')) {
$status = (!in_array($status, ['active', 'inactive']) ? 'active' : $status);
$templateVars['status'] = $status;
}
if ($criteria = $request->query->get('criteria')) {
$templateVars['criteria'] = $criteria;
} else {
$templateVars['criteria'] = [];
}
if ($dimensions = $request->query->get('dimension')) {
$templateVars['dimensions'] = $dimensions;
} else {
$templateVars['dimensions'] = [];
}
if ($filter = $request->query->get('filter') and !$page = $request->query->get('page')) {
$templateVars['filter'] = $filter;
if (preg_match("/:/", (string) $filter)) {
$parameters = explode(':', (string) $filter);
if (isset($templateVars['criteria']['id'])) {
unset($templateVars['criteria']['id']);
}
if (isset($templateVars['criteria'][$parameters[0]]) and $templateVars['criteria'][$parameters[0]] == $parameters[1]) {
unset($templateVars['criteria'][$parameters[0]]);
} else {
$templateVars['criteria'][$parameters[0]] = $parameters[1];
$templateVars['filter'] = null;
}
} else {
if ($filter == 'none') {
$templateVars['criteria'] = [];
$templateVars['filter'] = null;
} else {
$templateVars['filter'] = null;
}
}
}
if ($sort = $request->query->get('sort')) {
$templateVars['sort'] = $sort;
if (preg_match("/:/", (string) $sort)) {
$parameters = explode(':', (string) $sort);
$templateVars['dimensions'][$parameters[0]] = $parameters[1];
} else {
$sort = 'none';
switch ($sort) {
default:
$templateVars['dimensions'] = [];
$templateVars['sort'] = null;
break;
}
}
}
if ($fields = $request->query->get('fields')) {
$templateVars['fields'] = $fields;
}
return $templateVars;
}
}