service for default values
This commit is contained in:
parent
3159adcdca
commit
604c5cabd1
|
@ -9,7 +9,7 @@ services:
|
||||||
# default configuration for services in *this* file
|
# default configuration for services in *this* file
|
||||||
_defaults:
|
_defaults:
|
||||||
# automatically injects dependencies in your services
|
# automatically injects dependencies in your services
|
||||||
autowire: false
|
autowire: true
|
||||||
# automatically registers your services as commands, event subscribers, etc.
|
# automatically registers your services as commands, event subscribers, etc.
|
||||||
autoconfigure: true
|
autoconfigure: true
|
||||||
# this means you cannot fetch services directly from the container via $container->get()
|
# this means you cannot fetch services directly from the container via $container->get()
|
||||||
|
@ -49,3 +49,8 @@ services:
|
||||||
- '@fos_user.user_manager'
|
- '@fos_user.user_manager'
|
||||||
- ['@fos_user.user_manager', twitter: twitter_id]
|
- ['@fos_user.user_manager', twitter: twitter_id]
|
||||||
- '@doctrine.orm.default_entity_manager'
|
- '@doctrine.orm.default_entity_manager'
|
||||||
|
tk.owner.service:
|
||||||
|
autowire: false
|
||||||
|
class: AppBundle\Service\OwnerService
|
||||||
|
arguments:
|
||||||
|
- '@doctrine.orm.default_entity_manager'
|
||||||
|
|
|
@ -7,6 +7,7 @@ use AppBundle\Entity\Product;
|
||||||
use AppBundle\Entity\ProductCategory;
|
use AppBundle\Entity\ProductCategory;
|
||||||
use AppBundle\Entity\ProductSold;
|
use AppBundle\Entity\ProductSold;
|
||||||
use AppBundle\Entity\SellRecord;
|
use AppBundle\Entity\SellRecord;
|
||||||
|
use AppBundle\Service\OwnerService;
|
||||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
|
@ -17,11 +18,13 @@ use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
|
||||||
class DefaultController extends Controller
|
class DefaultController extends Controller
|
||||||
{
|
{
|
||||||
|
|
||||||
|
private $ownerService;
|
||||||
private $tokenManager;
|
private $tokenManager;
|
||||||
|
|
||||||
public function __construct(CsrfTokenManagerInterface $tokenManager = null)
|
public function __construct(CsrfTokenManagerInterface $tokenManager = null, OwnerService $ownerService)
|
||||||
{
|
{
|
||||||
$this->tokenManager = $tokenManager;
|
$this->tokenManager = $tokenManager;
|
||||||
|
$this->ownerService = $ownerService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -111,68 +114,20 @@ class DefaultController extends Controller
|
||||||
$currentUser = $this->getUser();
|
$currentUser = $this->getUser();
|
||||||
if (!$currentUser) {
|
if (!$currentUser) {
|
||||||
return new JsonResponse([
|
return new JsonResponse([
|
||||||
'categories' => [['name' => 'demo category', 'products' => []]],
|
'categories' => [[]],
|
||||||
'recentSells' => [['']],
|
'recentSells' => [['']],
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$ownerService = $this->ownerService;
|
||||||
|
$ownerService->setupNewFestival($currentUser);
|
||||||
$activeFestival = $currentUser->getActiveFestival();
|
$activeFestival = $currentUser->getActiveFestival();
|
||||||
if (!$activeFestival) {
|
|
||||||
$activeFestival = $m->getRepository('AppBundle:Festival')
|
|
||||||
->findOneBy(['user' => $this->getUser()->getId()],
|
|
||||||
['id' => 'desc'],
|
|
||||||
0,
|
|
||||||
1);
|
|
||||||
if (!$activeFestival) {
|
|
||||||
$activeFestival = new Festival();
|
|
||||||
$activeFestival->setDateCreation(new \DateTime())
|
|
||||||
->setName('default festival')
|
|
||||||
->setChiffreAffaire(0)
|
|
||||||
->setFondDeCaisseAvant(0)
|
|
||||||
->setFondDeCaisseApres(0)
|
|
||||||
->setUser($currentUser);
|
|
||||||
}
|
|
||||||
$currentUser->setActiveFestival($activeFestival);
|
|
||||||
$m->persist($activeFestival);
|
|
||||||
$m->persist($currentUser);
|
|
||||||
$m->flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
$categRepo = $m->getRepository('AppBundle:ProductCategory');
|
$categRepo = $m->getRepository('AppBundle:ProductCategory');
|
||||||
$sellingRepo = $m->getRepository('AppBundle:SellRecord');
|
$sellingRepo = $m->getRepository('AppBundle:SellRecord');
|
||||||
$categories = $currentUser->getCategories();
|
|
||||||
if (!count($categories)) {
|
|
||||||
$categories = $defaultCategories = $categRepo->findById([1, 2]);
|
|
||||||
$currentUser->setCategories($defaultCategories);
|
|
||||||
$m->persist($currentUser);
|
|
||||||
}
|
|
||||||
$serializedCategories = [];
|
|
||||||
|
|
||||||
foreach ($categories as $category) {
|
|
||||||
$products = $category->getProducts();
|
|
||||||
if ($products) {
|
|
||||||
$listOfProductsInArray = [];
|
|
||||||
foreach ($products as $product) {
|
|
||||||
$listOfProductsInArray[] = [
|
|
||||||
'id' => $product->getId(),
|
|
||||||
'name' => $product->getName(),
|
|
||||||
'category' => $category->getId(),
|
|
||||||
'price' => 1 * $product->getPrice(),
|
|
||||||
'stockCount' => $product->getStockCount(),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
$products = $listOfProductsInArray;
|
|
||||||
}
|
|
||||||
$serializedCategories[] =
|
|
||||||
[
|
|
||||||
'id' => $category->getId(),
|
|
||||||
'name' => $category->getName(),
|
|
||||||
'products' => $products ? $products : null,
|
|
||||||
];
|
|
||||||
|
|
||||||
}
|
$categories = $ownerService->serializeCategoriesOfUser($currentUser);
|
||||||
$categories = $serializedCategories;
|
|
||||||
|
|
||||||
|
|
||||||
$recentSells = $sellingRepo->findBy(['user' => $currentUser->getId()], ['id' => 'desc'], 0, 5);
|
$recentSells = $sellingRepo->findBy(['user' => $currentUser->getId()], ['id' => 'desc'], 0, 5);
|
||||||
|
@ -180,16 +135,7 @@ class DefaultController extends Controller
|
||||||
return new JsonResponse([
|
return new JsonResponse([
|
||||||
'categories' => $categories,
|
'categories' => $categories,
|
||||||
'recentSells' => count($recentSells),
|
'recentSells' => count($recentSells),
|
||||||
'lastFestival' => [
|
'lastFestival' => $activeFestival->makeArray(),
|
||||||
'id' => $activeFestival->getId(),
|
|
||||||
'name' => $activeFestival->getName(),
|
|
||||||
'commentaire' => $activeFestival->getComment(),
|
|
||||||
'dateCreation' => $activeFestival->getDateCreation(),
|
|
||||||
'chiffreAffaire' => $activeFestival->getChiffreAffaire(),
|
|
||||||
'clientsCount' => count($activeFestival->getSellRecords()),
|
|
||||||
'fondDeCaisseAvant' => $activeFestival->getFondDeCaisseAvant(),
|
|
||||||
'fondDeCaisseApres' => $activeFestival->getFondDeCaisseApres(),
|
|
||||||
],
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -83,6 +83,23 @@ class Festival {
|
||||||
private $fraisRepas;
|
private $fraisRepas;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* array usable by js
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function makeArray(){
|
||||||
|
return [
|
||||||
|
'id' => $this->getId(),
|
||||||
|
'name' => $this->getName(),
|
||||||
|
'commentaire' => $this->getComment(),
|
||||||
|
'dateCreation' => $this->getDateCreation(),
|
||||||
|
'chiffreAffaire' => $this->getChiffreAffaire(),
|
||||||
|
'clientsCount' => count($this->getSellRecords()),
|
||||||
|
'fondDeCaisseAvant' => $this->getFondDeCaisseAvant(),
|
||||||
|
'fondDeCaisseApres' => $this->getFondDeCaisseApres(),
|
||||||
|
];
|
||||||
|
|
||||||
|
}
|
||||||
public function recalculateChiffreAffaire() {
|
public function recalculateChiffreAffaire() {
|
||||||
$sellings = $this->getSellRecords();
|
$sellings = $this->getSellRecords();
|
||||||
$newChiffreAffaire = 0;
|
$newChiffreAffaire = 0;
|
||||||
|
|
|
@ -0,0 +1,122 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace AppBundle\Service;
|
||||||
|
|
||||||
|
use AppBundle\Entity\Festival;
|
||||||
|
use AppBundle\Entity\ProductCategory;
|
||||||
|
use AppBundle\Entity\User;
|
||||||
|
use Doctrine\Common\Persistence\ObjectManager;
|
||||||
|
use Doctrine\ORM\EntityManager;
|
||||||
|
|
||||||
|
class OwnerService {
|
||||||
|
private $em;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MyFOSUBUserProvider constructor.
|
||||||
|
*
|
||||||
|
* @param EntityManager $em
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
ObjectManager $em
|
||||||
|
) {
|
||||||
|
$this->em = $em;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* add default categories for a newly created user
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function setupNewDefaultCategories( User $user ) {
|
||||||
|
|
||||||
|
$categNames = [ 'livres', 'badges', 'dessins' ];
|
||||||
|
foreach ( $categNames as $categ_name ) {
|
||||||
|
$newCateg = new ProductCategory();
|
||||||
|
$newCateg->setName( $categ_name );
|
||||||
|
$user->addCategory( $newCateg );
|
||||||
|
$newCateg->addUser( $user );
|
||||||
|
$this->em->persist( $newCateg );
|
||||||
|
$this->em->persist( $user );
|
||||||
|
}
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
|
return $user;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* setup a default festival if needed
|
||||||
|
*
|
||||||
|
* @param User $user
|
||||||
|
*
|
||||||
|
* @return User
|
||||||
|
*/
|
||||||
|
public function setupNewFestival( User $user ) {
|
||||||
|
|
||||||
|
$activeFestival = $user->getActiveFestival();
|
||||||
|
if ( ! $activeFestival ) {
|
||||||
|
$activeFestival = $this->em->getRepository( 'AppBundle:Festival' )
|
||||||
|
->findOneBy( [ 'user' => $this->getUser()->getId() ],
|
||||||
|
[ 'id' => 'desc' ],
|
||||||
|
0,
|
||||||
|
1 );
|
||||||
|
if ( ! $activeFestival ) {
|
||||||
|
$activeFestival = new Festival();
|
||||||
|
$activeFestival->setDateCreation( new \DateTime() )
|
||||||
|
->setName( 'default festival' )
|
||||||
|
->setChiffreAffaire( 0 )
|
||||||
|
->setFondDeCaisseAvant( 0 )
|
||||||
|
->setFondDeCaisseApres( 0 )
|
||||||
|
->setUser( $user );
|
||||||
|
}
|
||||||
|
$user->setActiveFestival( $activeFestival );
|
||||||
|
$this->em->persist( $activeFestival );
|
||||||
|
$this->em->persist( $user );
|
||||||
|
$this->em->flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $user;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* transform categories of a user in an array usable by js
|
||||||
|
* @param User $user
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function serializeCategoriesOfUser( User $user ) {
|
||||||
|
$categRepo = $this->em->getRepository( 'AppBundle:ProductCategory' );
|
||||||
|
$categories = $user->getCategories();
|
||||||
|
if ( ! count( $categories ) ) {
|
||||||
|
$categories = $defaultCategories = $categRepo->findById( [ 1, 2 ] );
|
||||||
|
$user->setCategories( $defaultCategories );
|
||||||
|
$this->em->persist( $user );
|
||||||
|
}
|
||||||
|
$serializedCategories = [];
|
||||||
|
|
||||||
|
foreach ( $categories as $category ) {
|
||||||
|
$products = $category->getProducts();
|
||||||
|
if ( $products ) {
|
||||||
|
$listOfProductsInArray = [];
|
||||||
|
foreach ( $products as $product ) {
|
||||||
|
$listOfProductsInArray[] = [
|
||||||
|
'id' => $product->getId(),
|
||||||
|
'name' => $product->getName(),
|
||||||
|
'category' => $category->getId(),
|
||||||
|
'price' => 1 * $product->getPrice(),
|
||||||
|
'stockCount' => $product->getStockCount(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
$products = $listOfProductsInArray;
|
||||||
|
}
|
||||||
|
$serializedCategories[] =
|
||||||
|
[
|
||||||
|
'id' => $category->getId(),
|
||||||
|
'name' => $category->getName(),
|
||||||
|
'products' => $products ? $products : null,
|
||||||
|
];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return $serializedCategories;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue