diff --git a/app/Resources/views/logged/previsionnel.html.twig b/app/Resources/views/logged/previsionnel.html.twig index a82cdcf1..0ece30df 100755 --- a/app/Resources/views/logged/previsionnel.html.twig +++ b/app/Resources/views/logged/previsionnel.html.twig @@ -8,6 +8,18 @@ ng-app="caisse" ng-controller="previsionnelCtrl as pCtrl" > +
+
+
+ +
+ Chargement ... +
+
+ Modifications sauvegardées +
+
+

Prévisionnel

@@ -48,7 +60,8 @@

-

Postes de dépenses mensuelles +

+ {{expenses.length}} Postes de dépenses mensuelles

diff --git a/app/config/routing.yml b/app/config/routing.yml index c10226f5..1882e91d 100755 --- a/app/config/routing.yml +++ b/app/config/routing.yml @@ -43,6 +43,12 @@ app_sell_record: get_my_products: path: /get-my-products defaults: { _controller: AppBundle:Default:getMyProducts } +get_my_expenses: + path: /get-my-expenses + defaults: { _controller: AppBundle:Default:getMyExpenses } +save_my_expenses: + path: /save-my-expenses + defaults: { _controller: AppBundle:Default:saveMyExpenses } add_selling: path: /add-selling diff --git a/assets/js/parts/main.js b/assets/js/parts/main.js index 2b48b475..0985260d 100755 --- a/assets/js/parts/main.js +++ b/assets/js/parts/main.js @@ -226,6 +226,8 @@ angular .controller('previsionnelCtrl', ['$scope', '$http', '$timeout', function ($scope, $http, $timeout) { $scope.config = { + initialLoadingDone: false, + loading: false, lines: 24, /** * expenses kind of the user @@ -289,17 +291,35 @@ angular // http related calls $scope.fetchExpenses = () => { console.log('fetch expenses...'); + $scope.config.loading = true; + $http.get('get-my-expenses').then((rep) => { - console.log('get-my-expenses', rep); - }) + console.log('get-my-expenses', rep.data.expenses); + $scope.config.loading = false; + $scope.config.initialLoadingDone = true; + // 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; + } + }, + $scope.manageError) }; - // save + // save TODO $scope.save = () => { console.log('update expenses...'); - $http.post('save-my-expenses', $scope.expenses) + $scope.config.loading = true; + $http.post('save-my-expenses', { + expenses: $scope.expenses, + config: $scope.config + }) .then((rep) => { console.log('save-my-expenses', rep); - }) + $scope.config.loading = false; + }, + $scope.manageError) }; $scope.addExpense = () => { $scope.expenses.push({ @@ -308,5 +328,14 @@ angular delay: 0, amount: 0, }) + }; + $scope.init = ()=>{ + $scope.fetchExpenses(); + }; + $scope.manageError = (error)=>{ + console.error(error); + $scope.config.loading = false; + } + $scope.init(); }]); diff --git a/src/AppBundle/Controller/DefaultController.php b/src/AppBundle/Controller/DefaultController.php index f75480c6..9796700d 100755 --- a/src/AppBundle/Controller/DefaultController.php +++ b/src/AppBundle/Controller/DefaultController.php @@ -138,6 +138,30 @@ class DefaultController extends Controller 'lastFestival' => $activeFestival->makeArray(), ]); } + /** + * get user products + * @return JsonResponse + */ + public function getMyExpensesAction() + { + $m = $this->getDoctrine()->getManager(); + + + $currentUser = $this->getUser(); + if (!$currentUser) { + return new JsonResponse([ + 'expenses' => [[]], + ]); + } + + $ownerService = $this->ownerService; + $ownerService->setupNewFestival($currentUser); + $expensesOfUser = $ownerService->serializeExpensesOfUser($currentUser); + + return new JsonResponse([ + 'expenses' => $expensesOfUser, + ]); + } /** * @param Request $request diff --git a/src/AppBundle/Entity/User.php b/src/AppBundle/Entity/User.php index 78584d7e..027433c2 100755 --- a/src/AppBundle/Entity/User.php +++ b/src/AppBundle/Entity/User.php @@ -71,6 +71,22 @@ class User extends BaseUser { */ private $categories; + /** + * @return mixed + */ + public function getExpenses() + { + return $this->expenses; + } + + /** + * @param mixed $expenses + */ + public function setExpenses($expenses) + { + $this->expenses = $expenses; + } + /** * @return mixed */ diff --git a/src/AppBundle/Service/OwnerService.php b/src/AppBundle/Service/OwnerService.php index 43b334cb..433b4988 100755 --- a/src/AppBundle/Service/OwnerService.php +++ b/src/AppBundle/Service/OwnerService.php @@ -119,4 +119,27 @@ class OwnerService { return $serializedCategories; } + + /** + * @param User $user + * @return array + * @throws \Doctrine\ORM\ORMException + */ + public function serializeExpensesOfUser( User $user ) { + $expenses = $user->getExpenses(); + $serialized = []; + + foreach ( $expenses as $exp ) { + $serialized[] = + [ + 'id' => $exp->getId(), + 'name' => $exp->getName(), + 'delay' => $exp->getDelay(), + 'repeat' => $exp->getRepeat(), + ]; + + } + + return $serialized; + } }