From 3fa2457cbe18c6dc5636a87fd21b3c100cdfa3fe Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Mon, 27 Aug 2018 15:20:01 +0200 Subject: [PATCH] loop save ok --- .../views/logged/previsionnel.html.twig | 4 +-- assets/js/parts/main.js | 34 +++++++++--------- .../Controller/DefaultController.php | 35 ++++++++++--------- src/AppBundle/Entity/User.php | 16 ++++----- src/AppBundle/Service/OwnerService.php | 4 ++- 5 files changed, 50 insertions(+), 43 deletions(-) diff --git a/app/Resources/views/logged/previsionnel.html.twig b/app/Resources/views/logged/previsionnel.html.twig index a783e7b6..644420c7 100755 --- a/app/Resources/views/logged/previsionnel.html.twig +++ b/app/Resources/views/logged/previsionnel.html.twig @@ -30,11 +30,11 @@

Euros disponibles au départ: - +
Gains moyen par mois: - +

diff --git a/assets/js/parts/main.js b/assets/js/parts/main.js index 503b89ac..b8c4926f 100755 --- a/assets/js/parts/main.js +++ b/assets/js/parts/main.js @@ -294,36 +294,38 @@ angular $scope.config.loading = true; $http.get('get-my-expenses').then((rep) => { - console.log('get-my-expenses', rep.data.expenses); - $scope.config.loading = false; + console.log('get-my-expenses', rep.data.expenses); + $scope.config.loading = false; $scope.config.initialLoadingDone = true; + $scope.config.disponibility = rep.data.disponibility; + $scope.config.averageMonthlyEarnings = rep.data.averageMonthlyEarnings; // default data when user has nothing saved - console.log('rep.data.expenses.length',rep.data.expenses.length) - if(!rep.data.expenses.length){ - $scope.expenses = exampleExpenses; - }else{ - $scope.expenses = rep.data.expenses; - } - }, + console.log('rep.data.expenses.length', rep.data.expenses.length) + if (!rep.data.expenses.length) { + $scope.expenses = exampleExpenses; + } else { + $scope.expenses = rep.data.expenses; + } + }, $scope.manageError) }; // save TODO $scope.save = () => { - if($scope.config.loading ){ + if ($scope.config.loading) { console.log('already saving'); return; } console.log('update expenses...'); $scope.config.loading = true; $http.post('save-my-expenses', { - expenses: $scope.expenses, + expenses: $scope.expenses, config: $scope.config }) .then((rep) => { - console.log('save-my-expenses', rep); - $scope.config.loading = false; - }, + console.log('save-my-expenses', rep); + $scope.config.loading = false; + }, $scope.manageError) }; $scope.addExpense = () => { @@ -334,10 +336,10 @@ angular amount: 0, }) }; - $scope.init = ()=>{ + $scope.init = () => { $scope.fetchExpenses(); }; - $scope.manageError = (error)=>{ + $scope.manageError = (error) => { console.error(error); $scope.config.loading = false; diff --git a/src/AppBundle/Controller/DefaultController.php b/src/AppBundle/Controller/DefaultController.php index 61172560..8da1f41b 100755 --- a/src/AppBundle/Controller/DefaultController.php +++ b/src/AppBundle/Controller/DefaultController.php @@ -162,6 +162,8 @@ class DefaultController extends Controller return new JsonResponse([ 'expenses' => $expensesOfUser, + 'disponibility' => $currentUser->getDisponibility(), + 'averageMonthlyEarnings' => $currentUser->getAverageMonthlyEarnings(), ]); } @@ -170,7 +172,7 @@ class DefaultController extends Controller * @param Request $request * @return JsonResponse the list of expanses */ - public function saveExpensesAction(Request $request) + public function saveMyExpensesAction(Request $request) { $json = json_decode($request->getContent(), true); @@ -186,33 +188,34 @@ class DefaultController extends Controller // save the user configuration foreach ($json['expenses'] as $expens) { - if($expens['id']){ + if(isset($expens['id'])){ $foundExpense = $categoriesByID[$expens['id']]; if($foundExpense){ + // update existing expenses of logged in user $foundExpense->setName($expens['name']); $foundExpense->setAmount($expens['amount']); + $foundExpense->setDelay($expens['delay']); $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); - } + }else{ + // create new expense for user + $newExpense = new ExpenseKind(); + $newExpense->setUser($currentUser); + $newExpense->setName($expens['name']); + $newExpense->setDelay($expens['delay']); + $newExpense->setAmount($expens['amount']); + $newExpense->setRepeatitions($expens['repeat']); + $newExpense->setEnabled($expens['enabled']); + $m->persist($newExpense); + } } - foreach ($json['config'] as $conf) { - } - $nexExpense = new ExpenseKind(); + $currentUser->setDisponibility($json['config']['disponibility']); + $currentUser->setAverageMonthlyEarnings($json['config']['averageMonthlyEarnings']); - $m->persist($nexExpense); $m->persist($currentUser); $m->flush(); diff --git a/src/AppBundle/Entity/User.php b/src/AppBundle/Entity/User.php index a81ef9d7..b239147d 100755 --- a/src/AppBundle/Entity/User.php +++ b/src/AppBundle/Entity/User.php @@ -71,9 +71,10 @@ class User extends BaseUser { //expenses previsionnel configs /** - * @ORM\Column(name="averageSpend", type="float", nullable=true) + * @ORM\Column(name="averageMonthlyEarnings", type="float", nullable=true) */ - private $averageSpend; + private $averageMonthlyEarnings; + /** * @ORM\Column(name="disponibility", type="float", nullable=true) */ @@ -84,21 +85,20 @@ class User extends BaseUser { */ private $expenses; - /** * @return mixed */ - public function getAverageSpend() + public function getAverageMonthlyEarnings() { - return $this->averageSpend; + return $this->averageMonthlyEarnings; } /** - * @param mixed $averageSpend + * @param mixed $averageMonthlyEarnings */ - public function setAverageSpend($averageSpend) + public function setAverageMonthlyEarnings($averageMonthlyEarnings) { - $this->averageSpend = $averageSpend; + $this->averageMonthlyEarnings = $averageMonthlyEarnings; } /** * @return mixed diff --git a/src/AppBundle/Service/OwnerService.php b/src/AppBundle/Service/OwnerService.php index 433b4988..026943aa 100755 --- a/src/AppBundle/Service/OwnerService.php +++ b/src/AppBundle/Service/OwnerService.php @@ -132,10 +132,12 @@ class OwnerService { foreach ( $expenses as $exp ) { $serialized[] = [ + 'amount' => $exp->getAmount(), 'id' => $exp->getId(), 'name' => $exp->getName(), 'delay' => $exp->getDelay(), - 'repeat' => $exp->getRepeat(), + 'repeat' => $exp->getRepeatitions(), + 'enabled' => $exp->getEnabled(), ]; }