added option to delete invoice template (#294)

This commit is contained in:
Kevin Papst
2018-09-02 11:36:04 +02:00
committed by GitHub
parent 6fb6a16246
commit 9f6079ad8a
21 changed files with 281 additions and 83 deletions

View File

@@ -141,3 +141,8 @@ services:
class: Doctrine\ORM\EntityRepository
factory: ['@doctrine.orm.entity_manager', getRepository]
arguments: ['App\Entity\Customer']
App\Repository\InvoiceTemplateRepository:
class: Doctrine\ORM\EntityRepository
factory: ['@doctrine.orm.entity_manager', getRepository]
arguments: ['App\Entity\InvoiceTemplate']

View File

@@ -115,7 +115,7 @@ class ActivityController extends AbstractController
$entityManager->remove($activity);
$entityManager->flush();
$this->flashSuccess('action.deleted_successfully');
$this->flashSuccess('action.delete.success');
return $this->redirectToRoute('admin_activity');
}
@@ -146,7 +146,7 @@ class ActivityController extends AbstractController
$entityManager->persist($activity);
$entityManager->flush();
$this->flashSuccess('action.updated_successfully');
$this->flashSuccess('action.update.success');
if ($editForm->has('create_more') && $editForm->get('create_more')->getData() === true) {
$newActivity = new Activity();

View File

@@ -95,7 +95,7 @@ class CustomerController extends AbstractController
$entityManager->persist($customer);
$entityManager->flush();
$this->flashSuccess('action.updated_successfully');
$this->flashSuccess('action.update.success');
return $this->redirectToRoute('admin_customer');
}
@@ -132,7 +132,7 @@ class CustomerController extends AbstractController
$entityManager->remove($customer);
$entityManager->flush();
$this->flashSuccess('action.deleted_successfully');
$this->flashSuccess('action.delete.success');
return $this->redirectToRoute('admin_customer');
}

View File

@@ -109,7 +109,7 @@ class ProjectController extends AbstractController
$entityManager->remove($project);
$entityManager->flush();
$this->flashSuccess('action.deleted_successfully');
$this->flashSuccess('action.delete.success');
return $this->redirectToRoute('admin_project');
}
@@ -137,7 +137,7 @@ class ProjectController extends AbstractController
$entityManager->persist($project);
$entityManager->flush();
$this->flashSuccess('action.updated_successfully');
$this->flashSuccess('action.update.success');
if ($editForm->has('create_more') && $editForm->get('create_more')->getData() === true) {
$newProject = new Project();

View File

@@ -132,9 +132,9 @@ class TimesheetController extends AbstractController
$entityManager->remove($entry);
$entityManager->flush();
$this->flashSuccess('action.deleted_successfully');
$this->flashSuccess('action.delete.success');
} catch (\Exception $ex) {
$this->flashError('action.deleted.error', ['%reason%' => $ex->getMessage()]);
$this->flashError('action.delete.error', ['%reason%' => $ex->getMessage()]);
}
return $this->redirectToRoute('admin_timesheet_paginated', ['page' => $request->get('page')]);

View File

@@ -102,7 +102,7 @@ class UserController extends AbstractController
$entityManager->persist($user);
$entityManager->flush();
$this->flashSuccess('action.updated_successfully');
$this->flashSuccess('action.update.success');
if ($editForm->get('create_more')->getData() !== true) {
return $this->redirectToRoute('user_profile_edit', ['username' => $user->getUsername()]);
@@ -149,7 +149,7 @@ class UserController extends AbstractController
$entityManager->remove($userToDelete);
$entityManager->flush();
$this->flashSuccess('action.deleted_successfully');
$this->flashSuccess('action.delete.success');
return $this->redirectToRoute('admin_user');
}

View File

@@ -15,6 +15,7 @@ use App\Form\InvoiceTemplateForm;
use App\Form\Toolbar\InvoiceToolbarForm;
use App\Invoice\ServiceInvoice;
use App\Model\InvoiceModel;
use App\Repository\InvoiceTemplateRepository;
use App\Repository\Query\BaseQuery;
use App\Repository\Query\InvoiceQuery;
use App\Repository\Query\TimesheetQuery;
@@ -35,13 +36,25 @@ class InvoiceController extends AbstractController
* @var ServiceInvoice
*/
protected $service;
/**
* @var InvoiceTemplateRepository
*/
protected $invoiceRepository;
/**
* @var TimesheetRepository
*/
protected $timesheetRepository;
/**
* @param ServiceInvoice $service
* @param InvoiceTemplateRepository $invoice
* @param TimesheetRepository $timesheet
*/
public function __construct(ServiceInvoice $service)
public function __construct(ServiceInvoice $service, InvoiceTemplateRepository $invoice, TimesheetRepository $timesheet)
{
$this->service = $service;
$this->invoiceRepository = $invoice;
$this->timesheetRepository = $timesheet;
}
/**
@@ -62,14 +75,6 @@ class InvoiceController extends AbstractController
return $query;
}
/**
* @return \App\Repository\InvoiceTemplateRepository
*/
protected function getRepository()
{
return $this->getDoctrine()->getRepository(InvoiceTemplate::class);
}
/**
* @Route(path="/", name="invoice", methods={"GET", "POST"})
*
@@ -79,7 +84,7 @@ class InvoiceController extends AbstractController
*/
public function indexAction(Request $request)
{
if (!$this->getRepository()->hasTemplate()) {
if (!$this->invoiceRepository->hasTemplate()) {
return $this->redirectToRoute('admin_invoice_template_create');
}
@@ -99,9 +104,7 @@ class InvoiceController extends AbstractController
$query->getBegin()->setTime(0, 0, 0);
$query->getEnd()->setTime(23, 59, 59);
/* @var TimesheetRepository $timeRepo */
$timeRepo = $this->getDoctrine()->getRepository(Timesheet::class);
$queryBuilder = $timeRepo->findByQuery($query);
$queryBuilder = $this->timesheetRepository->findByQuery($query);
$entries = $queryBuilder->getQuery()->getResult();
}
}
@@ -147,7 +150,7 @@ class InvoiceController extends AbstractController
*/
public function listTemplateAction($page)
{
$templates = $this->getRepository()->findByQuery(new BaseQuery());
$templates = $this->invoiceRepository->findByQuery(new BaseQuery());
return $this->render('invoice/templates.html.twig', [
'entries' => $templates,
@@ -156,10 +159,11 @@ class InvoiceController extends AbstractController
}
/**
* @Route(path="/{id}/edit", name="admin_invoice_template_edit", methods={"GET", "POST"})
* @Route(path="/template/{id}/edit", name="admin_invoice_template_edit", methods={"GET", "POST"})
*
* TODO permission
*
* @param InvoiceTemplate $template
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
* @throws \Exception
@@ -170,7 +174,7 @@ class InvoiceController extends AbstractController
}
/**
* @Route(path="/create", name="admin_invoice_template_create", methods={"GET", "POST"})
* @Route(path="/template/create", name="admin_invoice_template_create", methods={"GET", "POST"})
*
* TODO permission
*
@@ -180,13 +184,36 @@ class InvoiceController extends AbstractController
*/
public function createTemplateAction(Request $request)
{
if (!$this->getRepository()->hasTemplate()) {
if (!$this->invoiceRepository->hasTemplate()) {
$this->flashWarning('invoice.first_template');
}
return $this->renderTemplateForm(new InvoiceTemplate(), $request);
}
/**
* The route to delete an existing template.
*
* TODO permission
*
* @Route(path="/template/{id}/delete", name="admin_invoice_template_delete", methods={"GET", "POST"})
*
* @param InvoiceTemplate $template
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function deleteTemplate(InvoiceTemplate $template, Request $request)
{
try {
$this->invoiceRepository->removeTemplate($template);
$this->flashSuccess('action.delete.success');
} catch (\Exception $ex) {
$this->flashError('action.delete.error', ['%reason%' => $ex->getMessage()]);
}
return $this->redirectToRoute('admin_invoice_template_paginated', ['page' => $request->get('page')]);
}
/**
* @param InvoiceTemplate $template
* @param Request $request
@@ -199,13 +226,13 @@ class InvoiceController extends AbstractController
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($template);
$entityManager->flush();
$this->flashSuccess('action.updated_successfully');
return $this->redirectToRoute('admin_invoice_template');
try {
$this->invoiceRepository->saveTemplate($template);
$this->flashSuccess('action.update.success');
return $this->redirectToRoute('admin_invoice_template');
} catch (\Exception $ex) {
$this->flashError('action.update.error', ['%reason%' => $ex->getMessage()]);
}
}
return $this->render('invoice/template_edit.html.twig', [

View File

@@ -68,7 +68,7 @@ class ProfileController extends AbstractController
$entityManager->persist($profile);
$entityManager->flush();
$this->flashSuccess('action.updated_successfully');
$this->flashSuccess('action.update.success');
return $this->redirectToRoute('user_profile', ['username' => $profile->getUsername()]);
}
@@ -93,7 +93,7 @@ class ProfileController extends AbstractController
$entityManager->persist($profile);
$entityManager->flush();
$this->flashSuccess('action.updated_successfully');
$this->flashSuccess('action.update.success');
return $this->redirectToRoute('user_profile', ['username' => $profile->getUsername()]);
}
@@ -118,7 +118,7 @@ class ProfileController extends AbstractController
$entityManager->persist($profile);
$entityManager->flush();
$this->flashSuccess('action.updated_successfully');
$this->flashSuccess('action.update.success');
return $this->redirectToRoute('user_profile', ['username' => $profile->getUsername()]);
}
@@ -140,7 +140,7 @@ class ProfileController extends AbstractController
$entityManager->persist($profile);
$entityManager->flush();
$this->flashSuccess('action.updated_successfully');
$this->flashSuccess('action.update.success');
return $this->redirectToRoute('user_profile', ['username' => $profile->getUsername()]);
}
@@ -184,7 +184,7 @@ class ProfileController extends AbstractController
$entityManager->persist($profile);
$entityManager->flush();
$this->flashSuccess('action.updated_successfully');
$this->flashSuccess('action.update.success');
return $this->redirectToRoute('user_profile', ['username' => $profile->getUsername()]);
}

View File

@@ -169,9 +169,9 @@ class TimesheetController extends AbstractController
$entityManager->remove($entry);
$entityManager->flush();
$this->flashSuccess('action.deleted_successfully');
$this->flashSuccess('action.delete.success');
} catch (\Exception $ex) {
$this->flashError('action.deleted.error', ['%reason%' => $ex->getMessage()]);
$this->flashError('action.delete.error', ['%reason%' => $ex->getMessage()]);
}
return $this->redirectToRoute('timesheet_paginated', ['page' => $request->get('page')]);

View File

@@ -98,7 +98,7 @@ trait TimesheetControllerTrait
$entityManager->persist($entry);
$entityManager->flush();
$this->flashSuccess('action.updated_successfully');
$this->flashSuccess('action.update.success');
return $this->redirectToRoute($redirectRoute, ['page' => $request->get('page')]);
}
@@ -177,7 +177,7 @@ trait TimesheetControllerTrait
$entityManager->flush();
$this->flashSuccess('action.updated_successfully');
$this->flashSuccess('action.update.success');
return $this->redirectToRoute($redirectRoute);
}

View File

@@ -54,4 +54,35 @@ class InvoiceTemplateRepository extends AbstractRepository
return $this->getBaseQueryResult($qb, $query);
}
/**
* @param InvoiceTemplate $template
* @return InvoiceTemplate
* @throws RepositoryException
*/
public function saveTemplate(InvoiceTemplate $template)
{
try {
$this->getEntityManager()->persist($template);
$this->getEntityManager()->flush();
} catch (\Exception $ex) {
throw new RepositoryException('Could not save InvoiceTemplate');
}
return $template;
}
/**
* @param InvoiceTemplate $template
* @throws RepositoryException
*/
public function removeTemplate(InvoiceTemplate $template)
{
try {
$this->getEntityManager()->remove($template);
$this->getEntityManager()->flush();
} catch (\Exception $ex) {
throw new RepositoryException('Could not remove InvoiceTemplate');
}
}
}

View File

@@ -29,6 +29,7 @@
<td class="hidden-xs hidden-sm">{{ "invoice_renderer.#{entry.renderer}"|trans }}</td>
<td>
{% set actionButtons = {'edit': path('admin_invoice_template_edit', {'id' : entry.id, 'page': page})} %}
{% set actionButtons = actionButtons|merge({'trash': path('admin_invoice_template_delete', {'id' : entry.id, 'page': page})}) %}
{{ widgets.button_group(actionButtons) }}
</td>
</tr>

View File

@@ -9,7 +9,9 @@
namespace App\Tests\Controller;
use App\Entity\InvoiceTemplate;
use App\Entity\User;
use App\Tests\DataFixtures\InvoiceFixtures;
/**
* @coversDefaultClass \App\Controller\InvoiceController
@@ -23,12 +25,59 @@ class InvoiceControllerTest extends ControllerBaseTest
$this->assertUrlIsSecuredForRole(User::ROLE_USER, '/invoice/');
}
public function testIndexAction()
public function testIndexActionRedirectsToCreateTemplate()
{
$this->markTestSkipped('create invoice template before this test case');
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$this->request($client, '/invoice/');
$this->assertIsRedirect($client, '/invoice/template/create');
}
public function testIndexActionHasErrorMessageOnEmptyQuery()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$fixture = new InvoiceFixtures();
$this->importFixture($em, $fixture);
$this->request($client, '/invoice/');
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertMainContentClass($client, 'dashboard');
$node = $client->getCrawler()->filter('div.callout.callout-warning.lead');
$this->assertNotEmpty($node->text());
$this->assertContains('Before you can create an invoice, you have to select at least a customer filter.', $node->text());
}
public function testListTemplateAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$fixture = new InvoiceFixtures();
$this->importFixture($em, $fixture);
$this->request($client, '/invoice/template');
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasDataTable($client);
}
public function testDeleteTemplateAction()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$fixture = new InvoiceFixtures();
$this->importFixture($em, $fixture);
$this->request($client, '/invoice/template/1/delete?page=1');
$this->assertIsRedirect($client, '/invoice/template/page/1');
$client->followRedirect();
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasFlashSuccess($client);
$this->assertEquals(0, $em->getRepository(InvoiceTemplate::class)->count([]));
}
}

View File

@@ -14,7 +14,7 @@ use App\Entity\User;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
/**
* @coversDefaultClass \App\Controller\InvoiceController
* @coversDefaultClass \App\Controller\ProfileController
* @group integration
*/
class ProfileControllerTest extends ControllerBaseTest

View File

@@ -0,0 +1,61 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\DataFixtures;
use App\Entity\InvoiceTemplate;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Faker\Factory;
/**
* Defines the sample data to load in during controller tests.
*/
class InvoiceFixtures extends Fixture
{
/**
* {@inheritdoc}
*/
public function load(ObjectManager $manager)
{
$this->loadInvoiceTemplates($manager);
}
/**
* @param ObjectManager $manager
*/
private function loadInvoiceTemplates(ObjectManager $manager)
{
$faker = Factory::create();
$template = new InvoiceTemplate();
$template
->setName('Invoice')
->setTitle('Your company name')
->setCompany($faker->company)
->setVat(19)
->setDueDays(14)
->setPaymentTerms(
'I would like to thank you for your confidence and will gladly be there for you in the future.' .
PHP_EOL .
'Please transfer the total amount within 14 days to the given account and use the invoice number ' .
'as reference.'
)
->setAddress(
$faker->streetAddress . PHP_EOL .
$faker->city . ', ' . $faker->stateAbbr . ' ' . $faker->postcode . PHP_EOL .
'Phone: ' . $faker->phoneNumber . PHP_EOL .
'Email: ' . $faker->safeEmail
)
;
$manager->persist($template);
$manager->flush();
}
}

View File

@@ -18,16 +18,20 @@
<source>timesheet.start.error</source>
<target>Zeitmessung konnte nicht gestartet werden: %reason%</target>
</trans-unit>
<trans-unit id="action.updated_successfully">
<source>action.updated_successfully</source>
<trans-unit id="action.update.success">
<source>action.update.success</source>
<target>Änderungen erfolgreich gespeichert</target>
</trans-unit>
<trans-unit id="action.deleted_successfully">
<source>action.deleted_successfully</source>
<trans-unit id="action.update.error">
<source>action.update.error</source>
<target>Änderungen konnten nicht gespeichert werden: %reason%</target>
</trans-unit>
<trans-unit id="action.delete.success">
<source>action.delete.success</source>
<target>Eintrag wurde erfolgreich gelöscht</target>
</trans-unit>
<trans-unit id="action.deleted.error">
<source>action.deleted.error</source>
<trans-unit id="action.delete.error">
<source>action.delete.error</source>
<target>Eintrag konnte nicht gelöscht werden: %reason%</target>
</trans-unit>
<trans-unit id="invoice.first_template">

View File

@@ -18,16 +18,20 @@
<source>timesheet.start.error</source>
<target>Time-recording could not be started: %reason%</target>
</trans-unit>
<trans-unit id="action.updated_successfully">
<source>action.updated_successfully</source>
<trans-unit id="action.update.success">
<source>action.update.success</source>
<target>Saved changes successful</target>
</trans-unit>
<trans-unit id="action.deleted_successfully">
<source>action.deleted_successfully</source>
<trans-unit id="action.update.error">
<source>action.update.error</source>
<target>Changes could not be saved: %reason%</target>
</trans-unit>
<trans-unit id="action.delete.success">
<source>action.delete.success</source>
<target>Entry was deleted successful</target>
</trans-unit>
<trans-unit id="action.deleted.error">
<source>action.deleted.error</source>
<trans-unit id="action.delete.error">
<source>action.delete.error</source>
<target>Entry could not be deleted: %reason%</target>
</trans-unit>
<trans-unit id="invoice.first_template">

View File

@@ -18,16 +18,20 @@
<source>timesheet.start.error</source>
<target>No se pudo iniciar el registro de tiempo: %reason%</target>
</trans-unit>
<trans-unit id="action.updated_successfully">
<source>action.updated_successfully</source>
<trans-unit id="action.update.success">
<source>action.update.success</source>
<target>Cambios guardados!</target>
</trans-unit>
<trans-unit id="action.deleted_successfully">
<source>action.deleted_successfully</source>
<trans-unit id="action.update.error">
<source>action.update.error</source>
<target>Changes could not be saved: %reason%</target>
</trans-unit>
<trans-unit id="action.delete.success">
<source>action.delete.success</source>
<target>Registro borrado!</target>
</trans-unit>
<trans-unit id="action.deleted.error">
<source>action.deleted.error</source>
<trans-unit id="action.delete.error">
<source>action.delete.error</source>
<target>No se pudo borrar el registro: %reason%</target>
</trans-unit>
<trans-unit id="invoice.first_template">

View File

@@ -18,16 +18,20 @@
<source>timesheet.start.error</source>
<target>Le chronométrage ne peut pas être lancé : %reason%</target>
</trans-unit>
<trans-unit id="action.updated_successfully">
<source>action.updated_successfully</source>
<trans-unit id="action.update.success">
<source>action.update.success</source>
<target>Modifications enregistrées</target>
</trans-unit>
<trans-unit id="action.deleted_successfully">
<source>action.deleted_successfully</source>
<trans-unit id="action.update.error">
<source>action.update.error</source>
<target>Changes could not be saved: %reason%</target>
</trans-unit>
<trans-unit id="action.delete.success">
<source>action.delete.success</source>
<target>L'entrée a été supprimée</target>
</trans-unit>
<trans-unit id="action.deleted.error">
<source>action.deleted.error</source>
<trans-unit id="action.delete.error">
<source>action.delete.error</source>
<target>L'entrée ne peut pas être supprimée : %reason%</target>
</trans-unit>
<trans-unit id="invoice.first_template">

View File

@@ -18,16 +18,20 @@
<source>timesheet.start.error</source>
<target>Time-recording non può essere avviata: %reason%</target>
</trans-unit>
<trans-unit id="action.updated_successfully">
<source>action.updated_successfully</source>
<trans-unit id="action.update.success">
<source>action.update.success</source>
<target>Salvataggio avvenuto</target>
</trans-unit>
<trans-unit id="action.deleted_successfully">
<source>action.deleted_successfully</source>
<trans-unit id="action.update.error">
<source>action.update.error</source>
<target>Changes could not be saved: %reason%</target>
</trans-unit>
<trans-unit id="action.delete.success">
<source>action.delete.success</source>
<target>Informazioni cancellate</target>
</trans-unit>
<trans-unit id="action.deleted.error">
<source>action.deleted.error</source>
<trans-unit id="action.delete.error">
<source>action.delete.error</source>
<target>Le informazioni non possono essere cancellate: %reason%</target>
</trans-unit>
<trans-unit id="invoice.first_template">

View File

@@ -18,16 +18,20 @@
<source>timesheet.start.error</source>
<target>Ошибка - Хронометраж не был начат: %reason%</target>
</trans-unit>
<trans-unit id="action.updated_successfully">
<source>action.updated_successfully</source>
<trans-unit id="action.update.success">
<source>action.update.success</source>
<target>Изменения успешно сохранены</target>
</trans-unit>
<trans-unit id="action.deleted_successfully">
<source>action.deleted_successfully</source>
<trans-unit id="action.update.error">
<source>action.update.error</source>
<target>Changes could not be saved: %reason%</target>
</trans-unit>
<trans-unit id="action.delete.success">
<source>action.delete.success</source>
<target>Запись успешно удалена</target>
</trans-unit>
<trans-unit id="action.deleted.error">
<source>action.deleted.error</source>
<trans-unit id="action.delete.error">
<source>action.delete.error</source>
<target>Ошибка - Запись не была удалена: %reason%</target>
</trans-unit>
<trans-unit id="invoice.first_template">