From f40ae1e1f55528259e6995eaa019f8cbe8186265 Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Mon, 27 Aug 2018 14:58:45 +0200 Subject: [PATCH] start save expenses --- .../views/logged/previsionnel.html.twig | 10 +- assets/js/parts/main.js | 5 + .../Controller/DefaultController.php | 97 +++++++++++++++---- src/AppBundle/Entity/ExpenseKind.php | 22 +++++ src/AppBundle/Entity/SerieFestival.php | 21 ++++ src/AppBundle/Entity/User.php | 80 +++++++++++++-- 6 files changed, 201 insertions(+), 34 deletions(-) diff --git a/app/Resources/views/logged/previsionnel.html.twig b/app/Resources/views/logged/previsionnel.html.twig index 0ece30df..a783e7b6 100755 --- a/app/Resources/views/logged/previsionnel.html.twig +++ b/app/Resources/views/logged/previsionnel.html.twig @@ -100,20 +100,20 @@ - + - + - + {{ e.repeat * e.amount }} - + @@ -123,7 +123,7 @@ € - + diff --git a/assets/js/parts/main.js b/assets/js/parts/main.js index 0985260d..503b89ac 100755 --- a/assets/js/parts/main.js +++ b/assets/js/parts/main.js @@ -308,7 +308,12 @@ angular $scope.manageError) }; // save TODO + $scope.save = () => { + if($scope.config.loading ){ + console.log('already saving'); + return; + } console.log('update expenses...'); $scope.config.loading = true; $http.post('save-my-expenses', { diff --git a/src/AppBundle/Controller/DefaultController.php b/src/AppBundle/Controller/DefaultController.php index 9796700d..61172560 100755 --- a/src/AppBundle/Controller/DefaultController.php +++ b/src/AppBundle/Controller/DefaultController.php @@ -2,6 +2,7 @@ namespace AppBundle\Controller; +use AppBundle\Entity\ExpenseKind; use AppBundle\Entity\Festival; use AppBundle\Entity\Product; use AppBundle\Entity\ProductCategory; @@ -18,7 +19,7 @@ use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; class DefaultController extends Controller { - private $ownerService; + private $ownerService; private $tokenManager; public function __construct(CsrfTokenManagerInterface $tokenManager = null, OwnerService $ownerService) @@ -120,7 +121,7 @@ class DefaultController extends Controller } $ownerService = $this->ownerService; - $ownerService->setupNewFestival($currentUser); + $ownerService->setupNewFestival($currentUser); $activeFestival = $currentUser->getActiveFestival(); $categRepo = $m->getRepository('AppBundle:ProductCategory'); @@ -138,8 +139,9 @@ class DefaultController extends Controller 'lastFestival' => $activeFestival->makeArray(), ]); } + /** - * get user products + * get user expenses * @return JsonResponse */ public function getMyExpensesAction() @@ -155,7 +157,7 @@ class DefaultController extends Controller } $ownerService = $this->ownerService; - $ownerService->setupNewFestival($currentUser); + $ownerService->setupNewFestival($currentUser); $expensesOfUser = $ownerService->serializeExpensesOfUser($currentUser); return new JsonResponse([ @@ -163,6 +165,64 @@ class DefaultController extends Controller ]); } + /** + * recieve the json containing the expanse config of a user + * @param Request $request + * @return JsonResponse the list of expanses + */ + public function saveExpensesAction(Request $request) + { + + $json = json_decode($request->getContent(), true); + $currentUser = $this->getUser(); + $m = $this->getDoctrine()->getManager(); + $myExpenses = $currentUser->getExpenses(); + $categoriesByID = []; + + foreach ($myExpenses as $expense) { + $categoriesByID[$expense->getId()] = $expense; + } + // loop on the json config for expanse + // save the user configuration + foreach ($json['expenses'] as $expens) { + + if($expens['id']){ + $foundExpense = $categoriesByID[$expens['id']]; + if($foundExpense){ + $foundExpense->setName($expens['name']); + $foundExpense->setAmount($expens['amount']); + $foundExpense->setRepeatitions($expens['repeat']); + $foundExpense->setEnabled($expens['enabled']); + $m->persist($foundExpense); + }else{ + // create new expense for user + $newExpense = new ExpenseKind(); + $newExpense->setUser($currentUser); + $newExpense->setName($expens['name']); + $newExpense->setAmount($expens['amount']); + $newExpense->setRepeatitions($expens['repeat']); + $newExpense->setEnabled($expens['enabled']); + $m->persist($newExpense); + + } + } + } + foreach ($json['config'] as $conf) { + + } + $nexExpense = new ExpenseKind(); + + $m->persist($nexExpense); + + $m->persist($currentUser); + $m->flush(); + $expensesOfUser = $currentUser->getExpenses(); + + return new JsonResponse([ + 'expenses' => $expensesOfUser, + ]); + } + /** * @param Request $request * add a selling record corresponding to one client @@ -177,7 +237,7 @@ class DefaultController extends Controller $m = $this->getDoctrine()->getManager(); $newSellRecord = new SellRecord(); // sort user categories - $myCategories = $m->getRepository('AppBundle:ProductCategory')->findAll(); + $myCategories = $currentUser->getCategories(); $categoriesByID = []; foreach ($myCategories as $my_category) { $categoriesByID[$my_category->getId()] = $my_category; @@ -258,20 +318,20 @@ class DefaultController extends Controller $m = $this->getDoctrine()->getManager(); $sellingRepo = $m->getRepository('AppBundle:SellRecord'); $allSellingList = $sellingRepo->findByUser($currentUser->getId()); - $mySellings = array_splice($allSellingList, 0 , 15); + $mySellings = array_splice($allSellingList, 0, 15); $chiffreAffaires = 0; $myFestivals = $currentUser->getFestivals(); $statisticsFestivals = [ - ]; + ]; foreach ($myFestivals as $festival) { - $statisticsFestivals[]=[ - 'date'=>$festival->getDateCreation(), - 'name'=>$festival->getName(), + $statisticsFestivals[] = [ + 'date' => $festival->getDateCreation(), + 'name' => $festival->getName(), 'clients_count' => count($festival->getSellRecords()), - 'chiffreAffaire'=>$festival->getChiffreAffaire(), + 'chiffreAffaire' => $festival->getChiffreAffaire(), ]; } $statisticsSoldProducts = [ @@ -279,13 +339,13 @@ class DefaultController extends Controller ]; $statsForFestival = [ - ['name' => 'festoche bidule', 'clients_count' => 125, 'chiffreAffaire' => 236, 'date'=> new \DateTime()], + ['name' => 'festoche bidule', 'clients_count' => 125, 'chiffreAffaire' => 236, 'date' => new \DateTime()], ]; - foreach ($allSellingList as $client) { - foreach ($client->getProductsSold() as $product) { - $chiffreAffaires += $product->getPrice(); - } - } + foreach ($allSellingList as $client) { + foreach ($client->getProductsSold() as $product) { + $chiffreAffaires += $product->getPrice(); + } + } foreach ($mySellings as $client) { foreach ($client->getProductsSold() as $product) { $chiffreAffaires += $product->getPrice(); @@ -526,8 +586,7 @@ class DefaultController extends Controller ->setName($productName) ->setStockCount(500) ->setUser($currentUser) - ->setPrice($price) - ; + ->setPrice($price); $currentUser->addProduct($newProduct); $m->persist($newProduct); }// look for existing products diff --git a/src/AppBundle/Entity/ExpenseKind.php b/src/AppBundle/Entity/ExpenseKind.php index 74be4c53..51831bb2 100755 --- a/src/AppBundle/Entity/ExpenseKind.php +++ b/src/AppBundle/Entity/ExpenseKind.php @@ -34,6 +34,13 @@ class ExpenseKind * @ORM\Column(name="delay", type="integer", nullable=true) */ private $delay; + /** + * line enabled to calculate on + * + * @ORM\Column(name="enabled", type="boolean", nullable=true) + */ + private $enabled; + /** * @var int|null @@ -54,6 +61,21 @@ class ExpenseKind */ private $user; + /** + * @return int|null + */ + public function getEnabled() + { + return $this->enabled; + } + + /** + * @param int|null $enabled + */ + public function setEnabled($enabled) + { + $this->enabled = $enabled; + } /** * Get id. diff --git a/src/AppBundle/Entity/SerieFestival.php b/src/AppBundle/Entity/SerieFestival.php index 0e71ba7f..d6bbbff0 100755 --- a/src/AppBundle/Entity/SerieFestival.php +++ b/src/AppBundle/Entity/SerieFestival.php @@ -43,6 +43,27 @@ class SerieFestival { */ private $dateCreation; + /** + * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="festivals") + */ + private $user; + + /** + * @return mixed + */ + public function getUser() + { + return $this->user; + } + + /** + * @param mixed $user + */ + public function setUser($user) + { + $this->user = $user; + } + /** * @return int */ diff --git a/src/AppBundle/Entity/User.php b/src/AppBundle/Entity/User.php index 027433c2..a81ef9d7 100755 --- a/src/AppBundle/Entity/User.php +++ b/src/AppBundle/Entity/User.php @@ -37,7 +37,10 @@ class User extends BaseUser { private $googleId; private $googleAccessToken; - + /** + * @ORM\ManyToMany(targetEntity="AppBundle\Entity\ProductCategory", inversedBy="users") + */ + private $categories; /** * templates products * @ORM\OneToMany(targetEntity="AppBundle\Entity\Product", mappedBy="user") @@ -54,23 +57,80 @@ class User extends BaseUser { * @ORM\OneToMany(targetEntity="AppBundle\Entity\Festival", mappedBy="user") */ private $festivals; + /** + * series of festivals + * @ORM\OneToMany(targetEntity="AppBundle\Entity\SerieFestival", mappedBy="user") + */ + private $seriesFestivals; + + /** + * current festival we are recording sellings for + * @ORM\OneToOne(targetEntity="AppBundle\Entity\Festival") + */ + private $activeFestival; + + //expenses previsionnel configs + /** + * @ORM\Column(name="averageSpend", type="float", nullable=true) + */ + private $averageSpend; + /** + * @ORM\Column(name="disponibility", type="float", nullable=true) + */ + private $disponibility; /** * variabilised products sold * @ORM\OneToMany(targetEntity="AppBundle\Entity\ExpenseKind", mappedBy="user") */ private $expenses; - /** - * current festival we are recording sellings for - * @ORM\OneToOne(targetEntity="AppBundle\Entity\Festival") - */ - private $activeFestival; - /** - * @ORM\ManyToMany(targetEntity="AppBundle\Entity\ProductCategory", inversedBy="users") - */ - private $categories; + /** + * @return mixed + */ + public function getAverageSpend() + { + return $this->averageSpend; + } + /** + * @param mixed $averageSpend + */ + public function setAverageSpend($averageSpend) + { + $this->averageSpend = $averageSpend; + } + /** + * @return mixed + */ + public function getDisponibility() + { + return $this->disponibility; + } + + /** + * @param mixed $disponibility + */ + public function setDisponibility($disponibility) + { + $this->disponibility = $disponibility; + } + + /** + * @return mixed + */ + public function getSeriesFestivals() + { + return $this->seriesFestivals; + } + + /** + * @param mixed $seriesFestivals + */ + public function setSeriesFestivals($seriesFestivals) + { + $this->seriesFestivals = $seriesFestivals; + } /** * @return mixed */