<?php
//----------------------------------------------------------------------
// src/Services/Product/ProductTools.php
//----------------------------------------------------------------------
namespace App\Services\Product;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\Access;
use App\Entity\SocietyGroup;
use App\Entity\Config\Config;
use App\Entity\Product\Unit;
use App\Entity\Product\Product;
use App\Entity\Product\Tva;
use App\Services\LogTools;
use App\Services\Common\TextTools;
class ProductTools
{
// Import product : Correspond to the number of element in each line of the CSV
const IMPORT_SIZE_ROW_PRODUCT = 6;
const IMPORT_MAX_ERROR = 20;
const IMPORT_MAX_SIZE_FILE = 10000;
const IMPORT_MAX_PACKAGE_SQL = 1000;
public function __construct(ManagerRegistry $doctrine, Logtools $logTools, TextTools $textTools)
{
$this->em = $doctrine->getManager();
$this->logTools = $logTools;
$this->textTools = $textTools;
}
public function productToArray($product)
{
if ($product === null) return array();
$url = $this->em->getRepository(Config::class)
->findOneByName(Config::URL);
if ($url !== null)
{
$url = $url->getValue();
}
$unitData = array();
$questionData = array();
$templates = array();
$productUrl = null;
$tva = null;
$unit = $product->getUnit();
$question = $product->getQuestion();
$tva = $product->getTva();
if ($unit !== null)
{
$unitData = array(
"unit_id" => $unit->getId(),
"unit_value" => $unit->getValue(),
"unit_abbrv" => $unit->getAbbrv(),
);
}
if ($question !== null)
{
$questionData = array(
"question_id" => $question->getId(),
"question_content" => $question->getContent(),
);
}
if ($tva !== null)
{
$tva = $tva->getValue();
}
if ($product->getLogo() !== null && $url !== null)
{
$productUrl = $url . "/" .$product->getLogo()->getUrlForDisplay();
}
foreach ($product->getTemplates() as $template)
{
$templates[] = array(
"template_id" => $template->getId(),
"template_ref" => $template->getRef(),
"template_title" => $template->getTitle(),
"template_description" => $template->getDescription(),
);
}
$productData = array(
"id" => $product->getId(),
"ref" => $product->getRef(),
"title" => $product->getTitle(),
"description" => $product->getDescription(),
"tva" => $this->textTools->encodeForJson($tva),
"public_price" => $product->getPublicPrice(),
"internal_price" => $product->getInternalPrice(),
"free" => $this->textTools->encodeForJson($product->getFree()),
"discount" => $this->textTools->encodeForJson($product->getDiscount()),
"readonly" => $this->textTools->encodeForJson($product->getReadonly()),
"samePrice" => $this->textTools->encodeForJson($product->getSamePrice()),
"optionNoPrice" => $this->textTools->encodeForJson($product->getOptionNoPrice()),
"logo" => $productUrl,
"unit" => $unitData,
"question" => $questionData,
"logo_url" => $productUrl,
"templates" => $templates,
);
return $productData;
}
public function productDevisToArray($product)
{
if ($product === null) return array();
$unitData = array();
$questionData = array();
$unit = $product->getUnit();
$question = $product->getQuestion();
$tva = $product->getTva();
if ($unit !== null)
{
$unitData = array(
"unit_id" => $unit->getId(),
"unit_value" => $unit->getValue(),
"unit_abbrv" => $unit->getAbbrv(),
);
}
if ($question !== null)
{
$questionData = array(
"question_id" => $question->getId(),
"question_content" => $question->getContent(),
);
}
if ($tva !== null)
{
$tva = $tva->getValue();
}
$productData = array(
"id" => $product->getId(),
"ref" => $product->getRef(),
"title" => $product->getTitle(),
"description" => $product->getDescription(),
"tva" => $this->textTools->encodeForJson($tva),
"public_price" => $product->getPublicPrice(),
"internal_price" => $product->getInternalPrice(),
"quantity" => $this->textTools->encodeForJson($product->getQuantity()),
"percentage" => $this->textTools->encodeForJson($product->getPercentage()),
"invoiced" => $this->textTools->encodeForJson($product->getInvoiced()),
"invoiced_percentage" => $this->textTools->encodeForJson($product->getInvoicedPercentage()),
"discount" => $this->textTools->encodeForJson($product->getDiscount()),
"readonly" => $this->textTools->encodeForJson($product->getReadonly()),
"same_price" => $this->textTools->encodeForJson($product->getSamePrice()),
"optionNoPrice" => $this->textTools->encodeForJson($product->getOptionNoPrice()),
"unit" => $unitData,
"question" => $questionData,
"total_public_ht" => $product->getTotalPublicHT(),
"total_internal_ht" => $product->getTotalInternalHT(),
"total_public_ttc" => $product->getTotalPublicTTC(),
"total_internal_ttc" => $product->getTotalInternalTTC(),
);
return $productData;
}
}