diff --git a/src/Controller/InvoiceController.php b/src/Controller/InvoiceController.php index 74580ecc..f208056d 100644 --- a/src/Controller/InvoiceController.php +++ b/src/Controller/InvoiceController.php @@ -76,7 +76,7 @@ class InvoiceController extends AbstractController } /** - * @Route(path="/", name="invoice", methods={"GET"}) + * @Route(path="/", name="invoice", methods={"GET", "POST"}) * @Security("is_granted('view', 'invoice')") * * @param Request $request @@ -110,7 +110,7 @@ class InvoiceController extends AbstractController } /** - * @Route(path="/print", name="invoice_print", methods={"GET"}) + * @Route(path="/print", name="invoice_print", methods={"GET", "POST"}) * @Security("is_granted('create', 'invoice')") * * @param Request $request @@ -242,19 +242,37 @@ class InvoiceController extends AbstractController /** * @Route(path="/template/create", name="admin_invoice_template_create", methods={"GET", "POST"}) + * @Route(path="/template/create/{id}", name="admin_invoice_template_copy", methods={"GET", "POST"}) * @Security("is_granted('create', 'invoice_template')") * * @param Request $request + * @param InvoiceTemplate|null $template * @return \Symfony\Component\HttpFoundation\Response * @throws \Exception */ - public function createTemplateAction(Request $request) + public function createTemplateAction(Request $request, ?InvoiceTemplate $copyFrom) { if (!$this->invoiceRepository->hasTemplate()) { $this->flashWarning('invoice.first_template'); } - return $this->renderTemplateForm(new InvoiceTemplate(), $request); + $template = new InvoiceTemplate(); + if (null !== $copyFrom) { + $template + ->setName('Copy of ' . $copyFrom->getName()) + ->setTitle($copyFrom->getTitle()) + ->setDueDays($copyFrom->getDueDays()) + ->setCalculator($copyFrom->getCalculator()) + ->setVat($copyFrom->getVat()) + ->setRenderer($copyFrom->getRenderer()) + ->setCompany($copyFrom->getCompany()) + ->setPaymentTerms($copyFrom->getPaymentTerms()) + ->setAddress($copyFrom->getAddress()) + ->setNumberGenerator($copyFrom->getNumberGenerator()) + ; + } + + return $this->renderTemplateForm($template, $request); } /** @@ -315,7 +333,7 @@ class InvoiceController extends AbstractController { return $this->createForm(InvoiceToolbarForm::class, $query, [ 'action' => $this->generateUrl('invoice', []), - 'method' => 'GET', + 'method' => 'POST', 'attr' => [ 'id' => 'invoice-print-form' ] diff --git a/src/Twig/Extensions.php b/src/Twig/Extensions.php index 50d624b6..9be1e133 100644 --- a/src/Twig/Extensions.php +++ b/src/Twig/Extensions.php @@ -59,6 +59,7 @@ class Extensions extends \Twig_Extension 'admin' => 'fas fa-wrench', 'calendar' => 'far fa-calendar-alt', 'customer' => 'fas fa-users', + 'copy' => 'far fa-copy', 'create' => 'far fa-plus-square', 'dashboard' => 'fas fa-tachometer-alt', 'delete' => 'far fa-trash-alt', diff --git a/templates/invoice/templates.html.twig b/templates/invoice/templates.html.twig index 44931d95..a957d80d 100644 --- a/templates/invoice/templates.html.twig +++ b/templates/invoice/templates.html.twig @@ -27,6 +27,7 @@ {{ entry.vat }} {% set actionButtons = {'edit': path('admin_invoice_template_edit', {'id' : entry.id, 'page': page})} %} + {% set actionButtons = actionButtons|merge({'copy': path('admin_invoice_template_copy', {'id' : entry.id, 'page': page})}) %} {% set actionButtons = actionButtons|merge({'trash': path('admin_invoice_template_delete', {'id' : entry.id, 'page': page})}) %} {{ widgets.button_group(actionButtons) }} diff --git a/tests/Controller/InvoiceControllerTest.php b/tests/Controller/InvoiceControllerTest.php index a3deee56..8a585d3a 100644 --- a/tests/Controller/InvoiceControllerTest.php +++ b/tests/Controller/InvoiceControllerTest.php @@ -88,6 +88,34 @@ class InvoiceControllerTest extends ControllerBaseTest $this->assertHasFlashSuccess($client); } + public function testCopyTemplateAction() + { + $client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD); + $em = $client->getContainer()->get('doctrine.orm.entity_manager'); + + $fixture = new InvoiceFixtures(); + $this->importFixture($em, $fixture); + + /** @var InvoiceTemplate $template */ + $template = $em->getRepository(InvoiceTemplate::class)->find(1); + + $this->request($client, '/invoice/template/create/1'); + $this->assertTrue($client->getResponse()->isSuccessful()); + + $form = $client->getCrawler()->filter('form[name=invoice_template_form]')->form(); + $values = $form->getPhpValues()['invoice_template_form']; + $this->assertEquals('Copy of ' . $template->getName(), $values['name']); + $this->assertEquals($template->getTitle(), $values['title']); + $this->assertEquals($template->getDueDays(), $values['dueDays']); + $this->assertEquals($template->getCalculator(), $values['calculator']); + $this->assertEquals($template->getVat(), $values['vat']); + $this->assertEquals($template->getRenderer(), $values['renderer']); + $this->assertEquals($template->getCompany(), $values['company']); + $this->assertEquals($template->getAddress(), $values['address']); + $this->assertEquals($template->getPaymentTerms(), $values['paymentTerms']); + $this->assertEquals($template->getNumberGenerator(), $values['numberGenerator']); + } + public function testPrintAction() { $client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD); diff --git a/tests/Twig/ExtensionsTest.php b/tests/Twig/ExtensionsTest.php index 90c003ec..8e3e6412 100644 --- a/tests/Twig/ExtensionsTest.php +++ b/tests/Twig/ExtensionsTest.php @@ -180,7 +180,7 @@ class ExtensionsTest extends TestCase $icons = [ 'user', 'customer', 'project', 'activity', 'admin', 'invoice', 'timesheet', 'dashboard', 'logout', 'trash', 'delete', 'repeat', 'edit', 'manual', 'help', 'start', 'start-small', 'stop', 'stop-small', 'filter', - 'create', 'list', 'print', 'visibility', 'calendar', 'money', 'duration', 'download' + 'create', 'list', 'print', 'visibility', 'calendar', 'money', 'duration', 'download', 'copy' ]; // test pre-defined icons