Added an admin page for currencies.

This commit is contained in:
Jan Böhmer 2019-08-12 23:45:21 +02:00
parent 87e6f641c3
commit 2468409212
15 changed files with 374 additions and 9 deletions

View file

@ -0,0 +1,112 @@
<?php
/**
*
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 Jan Böhmer
* https://github.com/jbtronics
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
namespace App\Controller\AdminPages;
use App\Entity\Attachments\AttachmentType;
use App\Entity\Parts\Category;
use App\Entity\PriceInformations\Currency;
use App\Form\AdminPages\BaseEntityAdminForm;
use App\Form\AdminPages\CategoryAdminForm;
use App\Form\AdminPages\CurrencyAdminForm;
use App\Services\EntityExporter;
use App\Services\EntityImporter;
use App\Services\StructuralElementRecursionHelper;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;
/**
* @Route("/currency")
*
* Class CurrencyController
* @package App\Controller\AdminPages
*/
class CurrencyController extends BaseAdminController
{
protected $entity_class = Currency::class;
protected $twig_template = 'AdminPages/CurrencyAdmin.html.twig';
protected $form_class = CurrencyAdminForm::class;
protected $route_base = "currency";
/**
* @Route("/{id}/edit", requirements={"id"="\d+"}, name="currency_edit")
* @Route("/{id}/", requirements={"id"="\d+"})
*/
public function edit(Currency $entity, Request $request, EntityManagerInterface $em)
{
return $this->_edit($entity, $request, $em);
}
/**
* @Route("/new", name="currency_new")
* @Route("/")
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function new(Request $request, EntityManagerInterface $em, EntityImporter $importer)
{
return $this->_new($request, $em, $importer);
}
/**
* @Route("/{id}", name="currency_delete", methods={"DELETE"})
*/
public function delete(Request $request, Currency $entity, StructuralElementRecursionHelper $recursionHelper)
{
return $this->_delete($request, $entity, $recursionHelper);
}
/**
* @Route("/export", name="currency_export_all")
* @param Request $request
* @param SerializerInterface $serializer
* @param EntityManagerInterface $em
* @return Response
*/
public function exportAll(EntityManagerInterface $em, EntityExporter $exporter, Request $request)
{
return $this->_exportAll($em, $exporter, $request);
}
/**
* @Route("/{id}/export", name="currency_export")
* @param Request $request
* @param AttachmentType $entity
*/
public function exportEntity(Currency $entity, EntityExporter $exporter, Request $request)
{
return $this->_exportEntity($entity, $exporter, $request);
}
}

View file

@ -69,6 +69,17 @@ class MeasurementUnit extends StructuralDBElement
*/
protected $usesSIPrefixes;
/**
* @ORM\OneToMany(targetEntity="MeasurementUnit", mappedBy="parent", cascade={"persist"})
*/
protected $children;
/**
* @ORM\ManyToOne(targetEntity="MeasurementUnit", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
protected $parent;
/**
* Returns the ID as an string, defined by the element class.
* This should have a form like P000014, for a part with ID 14.

View file

@ -49,7 +49,7 @@ class Currency extends StructuralDBElement
/**
* @var string The 3 letter ISO code of the currency.
* @ORM\Column(type="string", unique=true, nullable=true)
* @ORM\Column(type="string", unique=true)
* @Assert\Currency()
*/
protected $iso_code;
@ -57,16 +57,27 @@ class Currency extends StructuralDBElement
/**
* @var float|null The exchange rate between this currency and the base currency
* (how many base units the current currency is worth)
* @ORM\Column(type="decimal", precision=11, scale=5)
* @ORM\Column(type="decimal", precision=11, scale=5, nullable=true)
* @Assert\Positive()
*/
protected $exchange_rate;
/**
* @ORM\OneToMany(targetEntity="Currency", mappedBy="parent", cascade={"persist"})
*/
protected $children;
/**
* @ORM\ManyToOne(targetEntity="Currency", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
protected $parent;
/**
* Returns the 3 letter ISO code of this currency
* @return string
*/
public function getIsoCode(): string
public function getIsoCode(): ?string
{
return $this->iso_code;
}

View file

@ -36,6 +36,8 @@ use App\Entity\Base\NamedDBElement;
use App\Entity\Base\StructuralDBElement;
use FOS\CKEditorBundle\Form\Type\CKEditorType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ResetType;
@ -48,10 +50,12 @@ class BaseEntityAdminForm extends AbstractType
{
protected $security;
protected $params;
public function __construct(Security $security)
public function __construct(Security $security, ParameterBagInterface $params)
{
$this->security = $security;
$this->params = $params;
}
@ -74,7 +78,7 @@ class BaseEntityAdminForm extends AbstractType
'label' => 'not_selectable.label', 'help' => 'not_selectable.help', 'label_attr'=> ['class' => 'checkbox-custom'],
'disabled' => !$this->security->isGranted($is_new ? 'create' : 'edit', $entity) ])
->add('comment', CKEditorType::class, ['required' => false,
->add('comment', CKEditorType::class, ['required' => false, 'empty_data' => '',
'label' => 'comment.label', 'attr' => ['rows' => 4], 'help' => 'bbcode.hint',
'disabled' => !$this->security->isGranted($is_new ? 'create' : 'edit', $entity)]);

View file

@ -0,0 +1,57 @@
<?php
/**
*
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 Jan Böhmer
* https://github.com/jbtronics
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
namespace App\Form\AdminPages;
use App\Entity\Base\NamedDBElement;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\CurrencyType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\FormBuilderInterface;
class CurrencyAdminForm extends BaseEntityAdminForm
{
protected function additionalFormElements(FormBuilderInterface $builder, array $options, NamedDBElement $entity)
{
$is_new = $entity->getID() === null;
$builder->add('iso_code', CurrencyType::class , ['required' => true,
'label' => 'currency.iso_code.label',
'preferred_choices' => ['EUR', 'USD', 'GBP', 'JPY', 'CNY'],
'attr' => ['class' => 'selectpicker', 'data-live-search' => true],
'disabled' => !$this->security->isGranted($is_new ? 'create' : 'edit', $entity)]);
$builder->add('exchange_rate', MoneyType::class, ['required' => false,
'label' => 'currency.exchange_rate.label', 'currency' => $this->params->get('default_currency'),
'disabled' => !$this->security->isGranted($is_new ? 'create' : 'edit', $entity)]);
}
}

View file

@ -24,7 +24,7 @@ final class Version20190812154222 extends AbstractMigration
$this->addSql('CREATE TABLE `measurement_units` (id INT AUTO_INCREMENT NOT NULL, unit VARCHAR(255) NOT NULL, is_integer TINYINT(1) NOT NULL, use_si_prefix TINYINT(1) NOT NULL, comment LONGTEXT NOT NULL, parent_id INT DEFAULT NULL, not_selectable TINYINT(1) NOT NULL, name VARCHAR(255) NOT NULL, last_modified DATETIME NOT NULL, datetime_added DATETIME NOT NULL, UNIQUE INDEX UNIQ_F5AF83CFDCBB0C53 (unit), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql('CREATE TABLE part_lots (id INT AUTO_INCREMENT NOT NULL, id_store_location INT DEFAULT NULL, id_part INT DEFAULT NULL, description LONGTEXT NOT NULL, comment LONGTEXT NOT NULL, expiration_date DATETIME DEFAULT NULL, instock_unknown TINYINT(1) NOT NULL, instock INT DEFAULT NULL, amount DOUBLE PRECISION DEFAULT NULL, needs_refill TINYINT(1) NOT NULL, last_modified DATETIME NOT NULL, datetime_added DATETIME NOT NULL, INDEX IDX_EBC8F9435D8F4B37 (id_store_location), INDEX IDX_EBC8F943C22F6CC4 (id_part), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql('CREATE TABLE currencies (id INT AUTO_INCREMENT NOT NULL, iso_code VARCHAR(255) DEFAULT NULL, exchange_rate NUMERIC(11, 5) NOT NULL, comment LONGTEXT NOT NULL, parent_id INT DEFAULT NULL, not_selectable TINYINT(1) NOT NULL, name VARCHAR(255) NOT NULL, last_modified DATETIME NOT NULL, datetime_added DATETIME NOT NULL, UNIQUE INDEX UNIQ_37C4469362B6A45E (iso_code), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql('CREATE TABLE currencies (id INT AUTO_INCREMENT NOT NULL, iso_code VARCHAR(255) NOT NULL, exchange_rate NUMERIC(11, 5) DEFAULT NULL, comment LONGTEXT NOT NULL, parent_id INT DEFAULT NULL, not_selectable TINYINT(1) NOT NULL, name VARCHAR(255) NOT NULL, last_modified DATETIME NOT NULL, datetime_added DATETIME NOT NULL, UNIQUE INDEX UNIQ_37C4469362B6A45E (iso_code), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql('ALTER TABLE part_lots ADD CONSTRAINT FK_EBC8F9435D8F4B37 FOREIGN KEY (id_store_location) REFERENCES `storelocations` (id)');
$this->addSql('ALTER TABLE part_lots ADD CONSTRAINT FK_EBC8F943C22F6CC4 FOREIGN KEY (id_part) REFERENCES `parts` (id)');

View file

@ -33,12 +33,13 @@ namespace App\Security\Voter;
use App\Entity\Attachments\AttachmentType;
use App\Entity\Device\Device;
use App\Entity\Devices\Device;
use App\Entity\Parts\Category;
use App\Entity\Parts\Footprint;
use App\Entity\Parts\Manufacturer;
use App\Entity\Parts\Storelocation;
use App\Entity\Parts\Supplier;
use App\Entity\PriceInformations\Currency;
use App\Entity\UserSystem\User;
class StructureVoter extends ExtendedVoter
@ -83,6 +84,9 @@ class StructureVoter extends ExtendedVoter
return 'storelocations';
case Supplier::class:
return 'suppliers';
case Currency::class:
//TODO: Implement own permission
return 'suppliers';
}
//When the class is not supported by this class return null
return null;

View file

@ -39,6 +39,7 @@ use App\Entity\Base\NamedDBElement;
use App\Entity\Parts\Part;
use App\Entity\Parts\Storelocation;
use App\Entity\Parts\Supplier;
use App\Entity\PriceInformations\Currency;
use App\Entity\UserSystem\User;
use App\Exceptions\EntityNotSupported;
use Symfony\Component\HttpKernel\HttpCache\Store;
@ -176,10 +177,14 @@ class EntityURLGenerator
return $this->urlGenerator->generate("footprint_edit", ['id' => $entity->getID()]);
}
if($entity instanceof User) {
if ($entity instanceof User) {
return $this->urlGenerator->generate('user_edit', ['id' => $entity->getID()]);
}
if ($entity instanceof Currency) {
return $this->urlGenerator->generate('currency_edit', ['id' => $entity->getID()]);
}
//Otherwise throw an error
throw new EntityNotSupported('The given entity is not supported yet!');
}
@ -229,6 +234,10 @@ class EntityURLGenerator
return $this->urlGenerator->generate('user_new');
}
if ($entity instanceof Currency) {
return $this->urlGenerator->generate('currency_new');
}
throw new EntityNotSupported('The given entity is not supported yet!');
}
@ -299,6 +308,10 @@ class EntityURLGenerator
return $this->urlGenerator->generate('user_delete', ['id' => $entity->getID()]);
}
if ($entity instanceof Currency) {
return $this->urlGenerator->generate('currency_delete', ['id' => $entity->getID()]);
}
throw new EntityNotSupported('The given entity is not supported yet!');
}

View file

@ -76,6 +76,9 @@ class ToolsTreeBuilder
$nodes[] = new TreeViewNode($this->translator->trans('tree.tools.edit.footprint'),
$this->urlGenerator->generate('footprint_new'));
$nodes[] = new TreeViewNode($this->translator->trans('tree.tools.edit.currency'),
$this->urlGenerator->generate('currency_new'));
$nodes[] = new TreeViewNode($this->translator->trans('tree.tools.edit.part'),
$this->urlGenerator->generate('part_new'));