start save expenses

This commit is contained in:
Baptiste Lemoine 2018-08-27 14:58:45 +02:00
parent d81aa1cbe0
commit f40ae1e1f5
6 changed files with 201 additions and 34 deletions

View File

@ -100,20 +100,20 @@
</tbody> </tbody>
<tr ng-repeat="e in expenses "> <tr ng-repeat="e in expenses ">
<td> <td>
<input type="text" ng-model="e.name"> <input type="text" ng-model="e.name" ng-change="save()">
</td> </td>
<td ng-if="config.showDelays"> <td ng-if="config.showDelays">
<input type="number" ng-model="e.delay"> <input type="number" ng-model="e.delay" ng-change="save()">
</td> </td>
<td ng-if="config.showRepeats"> <td ng-if="config.showRepeats">
<input type="number" ng-model="e.repeat"> <input type="number" ng-model="e.repeat" ng-change="save()">
</td> </td>
<td ng-if="config.showRepeats" class="text-right padded"> <td ng-if="config.showRepeats" class="text-right padded">
{{ e.repeat * e.amount }} {{ e.repeat * e.amount }}
</td> </td>
<td> <td>
<input type="number" ng-model="e.amount"> <input type="number" ng-model="e.amount" ng-change="save()">
</td> </td>
<td class="text-right padded"> <td class="text-right padded">
<strong> <strong>
@ -123,7 +123,7 @@
</td> </td>
<td class="padded"> <td class="padded">
<input type="checkbox" ng-model="e.enabled"> <input type="checkbox" ng-model="e.enabled" ng-change="save()">
</td> </td>
</tr> </tr>
</table> </table>

View File

@ -308,7 +308,12 @@ angular
$scope.manageError) $scope.manageError)
}; };
// save TODO // save TODO
$scope.save = () => { $scope.save = () => {
if($scope.config.loading ){
console.log('already saving');
return;
}
console.log('update expenses...'); console.log('update expenses...');
$scope.config.loading = true; $scope.config.loading = true;
$http.post('save-my-expenses', { $http.post('save-my-expenses', {

View File

@ -2,6 +2,7 @@
namespace AppBundle\Controller; namespace AppBundle\Controller;
use AppBundle\Entity\ExpenseKind;
use AppBundle\Entity\Festival; use AppBundle\Entity\Festival;
use AppBundle\Entity\Product; use AppBundle\Entity\Product;
use AppBundle\Entity\ProductCategory; use AppBundle\Entity\ProductCategory;
@ -138,8 +139,9 @@ class DefaultController extends Controller
'lastFestival' => $activeFestival->makeArray(), 'lastFestival' => $activeFestival->makeArray(),
]); ]);
} }
/** /**
* get user products * get user expenses
* @return JsonResponse * @return JsonResponse
*/ */
public function getMyExpensesAction() public function getMyExpensesAction()
@ -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 * @param Request $request
* add a selling record corresponding to one client * add a selling record corresponding to one client
@ -177,7 +237,7 @@ class DefaultController extends Controller
$m = $this->getDoctrine()->getManager(); $m = $this->getDoctrine()->getManager();
$newSellRecord = new SellRecord(); $newSellRecord = new SellRecord();
// sort user categories // sort user categories
$myCategories = $m->getRepository('AppBundle:ProductCategory')->findAll(); $myCategories = $currentUser->getCategories();
$categoriesByID = []; $categoriesByID = [];
foreach ($myCategories as $my_category) { foreach ($myCategories as $my_category) {
$categoriesByID[$my_category->getId()] = $my_category; $categoriesByID[$my_category->getId()] = $my_category;
@ -258,7 +318,7 @@ class DefaultController extends Controller
$m = $this->getDoctrine()->getManager(); $m = $this->getDoctrine()->getManager();
$sellingRepo = $m->getRepository('AppBundle:SellRecord'); $sellingRepo = $m->getRepository('AppBundle:SellRecord');
$allSellingList = $sellingRepo->findByUser($currentUser->getId()); $allSellingList = $sellingRepo->findByUser($currentUser->getId());
$mySellings = array_splice($allSellingList, 0 , 15); $mySellings = array_splice($allSellingList, 0, 15);
$chiffreAffaires = 0; $chiffreAffaires = 0;
$myFestivals = $currentUser->getFestivals(); $myFestivals = $currentUser->getFestivals();
@ -267,11 +327,11 @@ class DefaultController extends Controller
]; ];
foreach ($myFestivals as $festival) { foreach ($myFestivals as $festival) {
$statisticsFestivals[]=[ $statisticsFestivals[] = [
'date'=>$festival->getDateCreation(), 'date' => $festival->getDateCreation(),
'name'=>$festival->getName(), 'name' => $festival->getName(),
'clients_count' => count($festival->getSellRecords()), 'clients_count' => count($festival->getSellRecords()),
'chiffreAffaire'=>$festival->getChiffreAffaire(), 'chiffreAffaire' => $festival->getChiffreAffaire(),
]; ];
} }
$statisticsSoldProducts = [ $statisticsSoldProducts = [
@ -279,7 +339,7 @@ class DefaultController extends Controller
]; ];
$statsForFestival = [ $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 ($allSellingList as $client) {
foreach ($client->getProductsSold() as $product) { foreach ($client->getProductsSold() as $product) {
@ -526,8 +586,7 @@ class DefaultController extends Controller
->setName($productName) ->setName($productName)
->setStockCount(500) ->setStockCount(500)
->setUser($currentUser) ->setUser($currentUser)
->setPrice($price) ->setPrice($price);
;
$currentUser->addProduct($newProduct); $currentUser->addProduct($newProduct);
$m->persist($newProduct); $m->persist($newProduct);
}// look for existing products }// look for existing products

View File

@ -34,6 +34,13 @@ class ExpenseKind
* @ORM\Column(name="delay", type="integer", nullable=true) * @ORM\Column(name="delay", type="integer", nullable=true)
*/ */
private $delay; private $delay;
/**
* line enabled to calculate on
*
* @ORM\Column(name="enabled", type="boolean", nullable=true)
*/
private $enabled;
/** /**
* @var int|null * @var int|null
@ -54,6 +61,21 @@ class ExpenseKind
*/ */
private $user; private $user;
/**
* @return int|null
*/
public function getEnabled()
{
return $this->enabled;
}
/**
* @param int|null $enabled
*/
public function setEnabled($enabled)
{
$this->enabled = $enabled;
}
/** /**
* Get id. * Get id.

View File

@ -43,6 +43,27 @@ class SerieFestival {
*/ */
private $dateCreation; 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 * @return int
*/ */

View File

@ -37,7 +37,10 @@ class User extends BaseUser {
private $googleId; private $googleId;
private $googleAccessToken; private $googleAccessToken;
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\ProductCategory", inversedBy="users")
*/
private $categories;
/** /**
* templates products * templates products
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Product", mappedBy="user") * @ORM\OneToMany(targetEntity="AppBundle\Entity\Product", mappedBy="user")
@ -55,10 +58,10 @@ class User extends BaseUser {
*/ */
private $festivals; private $festivals;
/** /**
* variabilised products sold * series of festivals
* @ORM\OneToMany(targetEntity="AppBundle\Entity\ExpenseKind", mappedBy="user") * @ORM\OneToMany(targetEntity="AppBundle\Entity\SerieFestival", mappedBy="user")
*/ */
private $expenses; private $seriesFestivals;
/** /**
* current festival we are recording sellings for * current festival we are recording sellings for
@ -66,11 +69,68 @@ class User extends BaseUser {
*/ */
private $activeFestival; private $activeFestival;
//expenses previsionnel configs
/** /**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\ProductCategory", inversedBy="users") * @ORM\Column(name="averageSpend", type="float", nullable=true)
*/ */
private $categories; 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;
/**
* @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 * @return mixed
*/ */