<?php
namespace App\Services;
use Symfony\Component\DependencyInjection\Tests\Fixtures\StdClassDecorator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormError;
use UserBundle\Entity\User;
use App\Entity\Membership;
use App\Entity\Dic;
use App\Entity\Ba;
use Symfony\Component\HttpFoundation\RequestStack;
// Import the Twig Environment
use Twig\Environment;
class Utils
{
private $container;
private $twig;
protected $fileUploader;
/**
* Constructor
*
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container, EntityManagerInterface $entityManager, Environment $twig, FileUploader $fileUploader)
{
$this->container = $container;
$this->em = $entityManager;
$this->twig = $twig;
$this->fileUploader = $fileUploader;
}
public function translateUnit($unitOrigin, $unit, $value){
return $this->calculUnit($unitOrigin, $unit, $value);
}
public function translateUnitLong($unitOrigin, $unit, $value){
$unitTemp = "";
$tabQuantity = $this->calculUnit($unitOrigin, $unit, $value, true);
if($unit == "oz" || $unit == "cl"){
if($unit == "oz"){
if($unitOrigin == "cl"){
if($tabQuantity['typeUnit'] != null){
$unitTemp = $tabQuantity['typeUnit'];
}
else{
$unitTemp = $unit;
}
}
else{
$unitTemp = $unitOrigin;
}
}
else{
if($unitOrigin == "cl" || $unitOrigin == "oz" || $unitOrigin == "cup" || $unitOrigin == "quart" || $unitOrigin == "gallon"){
$unitTemp = $unit;
}
else{
$unitTemp = $unitOrigin;
}
}
}
else{
$unitTemp = $unitOrigin;
}
return $tabQuantity['valueReturn']." ".$unitTemp;
}
public function calculUnit($unitOrigin, $unit, $value, $all = false){
$valueReturn = "";
$typeUnit = null;
if($value == null || $value == ""){
$valueReturn = "";
}
else{
if($unitOrigin == "cl" || $unitOrigin == "oz" || $unitOrigin == "cup" || $unitOrigin == "quart" || $unitOrigin == "gallon"){
if($unit == "oz"){
if($unitOrigin == "cl"){
$valueBase = round((floatval($value) / 2.96), 2);
if($valueBase < 2){
if($valueBase > 1){
$valueNew = $this->MRound($valueBase - 1, 4);
}
else{
$valueNew = $this->MRound($valueBase, 4);
}
if($valueNew < 0.25){
$valueNew = "¼";
}
else if($valueNew == "0.25"){
$valueNew = "¼";
}
else if($valueNew == "0.50"){
$valueNew = "½";
}
else if($valueNew == "0.75"){
$valueNew = "¾";
}
else{
$valueNew = "1";
}
if($valueBase > 1){
if($valueNew == 1){
$valueNew = 2;
}
else{
$valueNew = "1 ".$valueNew;
}
}
$valueBase = $valueNew;
}
else if($valueBase >= 2 && $valueBase < 8){
//It's oz classic
$valueBase = round((floatval($valueBase)), 0);
$typeUnit = "oz";
}
else if($valueBase >= 8 && $valueBase < 32){
//It's cup
$valueBase = round((floatval($valueBase) / 8), 0);
$typeUnit = "cup";
}
else if($valueBase >= 32 && $valueBase < 128){
//It's quart
$valueBase = round((floatval($valueBase) / 32), 0);
$typeUnit = "quart";
}
else if($valueBase >= 128){
//It's gallon
$valueBase = round((floatval($valueBase) / 128), 0);
$typeUnit = "gallon";
}
$valueReturn = $valueBase;
}
else{
if($value == "0.25"){
$value = "¼";
}
if($value == "0.50"){
$value = "½";
}
if($value == "0.75"){
$value = "¾";
}
if($value == "1.25"){
$value = "1 ¼";
}
if($value == "1.50"){
$value = "1 ½";
}
if($value == "1.75"){
$value = "1 ¾";
}
if($value == "2.25"){
$value = "2 ¼";
}
if($value == "2.50"){
$value = "2 ½";
}
if($value == "2.75"){
$value = "2 ¾";
}
$valueReturn = $value;
}
}
else if($unit == "cl"){
if($unitOrigin == "oz"){
$valueReturn = round((floatval($value) * 2.96), 0);
}
else if($unitOrigin == "cup"){
$valueReturn = round((floatval($value) * 23.7), 0);
}
else if($unitOrigin == "quart"){
$valueReturn = round((floatval($value) * 94.6), 0);
}
else if($unitOrigin == "gallon"){
$valueReturn = round((floatval($value) * 378.5), 0);
}
else{
$valueReturn = $value;
}
}
else{
$valueReturn = $value;
}
}
else{
$valueReturn = $value;
}
}
if($all){
return array(
"valueReturn" => $valueReturn,
"typeUnit" => $typeUnit
);
}
else{
return $valueReturn;
}
}
function MRound($num,$parts) {
$res = $num * $parts;
$res = round($res);
return $res /$parts;
}
public function getMembershipByCustomer($customer){
$membership = $this->em
->getRepository('App:Membership')
->findOneBy(['user' => $customer], array("id" => "DESC"));
if(!$membership){
$customer = $this->em
->getRepository('App:User')
->findOneBy(['id' => $customer]);
$membership = new Membership;
$membership->setUser($customer);
$membership->setStatus("DRAFT");
$this->em->persist($membership);
$this->em->flush();
$membershipLast = $membership;
}
else{
$membershipLast = $membership;
}
if(!$membershipLast->getDic()){
$dic = new Dic;
$membershipLast->setDic($dic);
$this->em->persist($dic);
$this->em->persist($membershipLast);
$this->em->flush();
}
if(!$membershipLast->getBa()){
$ba = new Ba;
$membershipLast->setBa($ba);
$this->em->persist($ba);
$this->em->persist($membershipLast);
$this->em->flush();
}
return $membershipLast;
}
public function sendEmailEndSignin($user){
$mailNoReply = $this->container->getParameter('mail_no_reply');
$senderName = $this->container->getParameter('sender_name');
$url = $this->container->get('router')->generate('signin_end', array('token' => $user->getTokenReset()), UrlGeneratorInterface::ABSOLUTE_URL);
$bodyEmail = $this->twig->render(
'email/addUserWithToken.html.twig',
array('user' => $user, 'confirmationToken' => $url)
);
$message = (new \Swift_Message("Fin d'inscription"))
->setSubject("Fin d'inscription")
->setFrom(array($mailNoReply => $senderName))
->setTo(array($user->getEmail() => $senderName))
->setBody($bodyEmail,'text/html');
$this->mailer->send($message);
}
public function sendEmailEndSigninCustomer($user){
$mailNoReply = $this->container->getParameter('mail_no_reply');
$senderName = $this->container->getParameter('sender_name');
$url = $this->container->get('router')->generate('signin_end', array('token' => $user->getTokenReset()), UrlGeneratorInterface::ABSOLUTE_URL);
$bodyEmail = $this->twig->render(
'email/addCustomerWithToken.html.twig',
array('user' => $user, 'confirmationToken' => $url)
);
$message = (new \Swift_Message("Finalisez votre adhésion"))
->setSubject("Finalisez votre adhésion")
->setFrom(array($mailNoReply => $senderName))
->setTo(array($user->getEmail() => $senderName))
->setBody($bodyEmail,'text/html');
$this->mailer->send($message);
}
public function getStatusMembership($customer){
$status = "no-status";
$membershipCurrent = $this->getMembershipByCustomer($customer);
if($membershipCurrent->getStatus() == "DRAFT"){
$status = "Brouillon";
}
else if($membershipCurrent->getStatus() == "VALIDBYSELLER"){
$status = "En attente de validation";
}
else if($membershipCurrent->getStatus() == "VALIDBYCUSTOMER"){
$status = "En attente de signature";
}
else if($membershipCurrent->getStatus() == "SIGNBYCUSTOMER"){
$status = "En attente de paiement";
}
else if($membershipCurrent->getStatus() == "PAYBYCUSTOMER"){
$status = "Validé";
}
return $status;
}
function formatSizeUnits($bytes)
{
if ($bytes >= 1073741824)
{
$bytes = number_format($bytes / 1073741824, 2) . ' GB';
}
elseif ($bytes >= 1048576)
{
$bytes = number_format($bytes / 1048576, 2) . ' MB';
}
elseif ($bytes >= 1024)
{
$bytes = number_format($bytes / 1024, 2) . ' KB';
}
elseif ($bytes > 1)
{
$bytes = $bytes . ' bytes';
}
elseif ($bytes == 1)
{
$bytes = $bytes . ' byte';
}
else
{
$bytes = '0 bytes';
}
return $bytes;
}
}