From b280d0c28b3e3a1c2f4d6ba29e8922f35acae7ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sat, 31 Aug 2019 13:08:02 +0200 Subject: [PATCH] Allow quantites in Orderdetails to be floats. Useful for non integer based part units. --- src/Entity/PriceInformations/Pricedetail.php | 44 ++++++++++++------- src/Form/Part/OrderdetailType.php | 9 +++- src/Form/Part/PartBaseType.php | 3 ++ src/Form/Part/PricedetailType.php | 15 +++++-- src/Form/Type/SIUnitType.php | 7 +++ src/Migrations/Version20190829104643.php | 4 ++ .../Form/extendedBootstrap4_layout.html.twig | 6 +-- 7 files changed, 63 insertions(+), 25 deletions(-) diff --git a/src/Entity/PriceInformations/Pricedetail.php b/src/Entity/PriceInformations/Pricedetail.php index 6b359a37..e03394fd 100644 --- a/src/Entity/PriceInformations/Pricedetail.php +++ b/src/Entity/PriceInformations/Pricedetail.php @@ -106,18 +106,18 @@ class Pricedetail extends DBElement protected $currency; /** - * @var int - * @ORM\Column(type="integer") + * @var float + * @ORM\Column(type="float") * @Assert\Positive() */ - protected $price_related_quantity = 1; + protected $price_related_quantity = 1.0; /** - * @var int - * @ORM\Column(type="integer") + * @var float + * @ORM\Column(type="float") * @Assert\Positive() */ - protected $min_discount_quantity = 1; + protected $min_discount_quantity = 1.0; /** * @var bool @@ -173,13 +173,20 @@ class Pricedetail extends DBElement * Get the price related quantity. * * This is the quantity, for which the price is valid. + * The amount is measured in part unit. * - * @return int the price related quantity + * @return float the price related quantity * * @see Pricedetail::setPriceRelatedQuantity() */ - public function getPriceRelatedQuantity(): int + public function getPriceRelatedQuantity(): float { + if ($this->orderdetail && $this->orderdetail->getPart()) { + if (!$this->orderdetail->getPart()->useFloatAmount()) { + $tmp = round($this->price_related_quantity); + return $tmp < 1 ? 1 : $tmp; + } + } return $this->price_related_quantity; } @@ -189,12 +196,21 @@ class Pricedetail extends DBElement * "Minimum discount quantity" means the minimum order quantity for which the price * of this orderdetails is valid. * + * The amount is measured in part unit. + * * @return int the minimum discount quantity * * @see Pricedetail::setMinDiscountQuantity() */ - public function getMinDiscountQuantity(): int + public function getMinDiscountQuantity(): float { + if ($this->orderdetail && $this->orderdetail->getPart()) { + if (!$this->orderdetail->getPart()->useFloatAmount()) { + $tmp = round($this->min_discount_quantity); + return $tmp < 1 ? 1 : $tmp; + } + } + return $this->min_discount_quantity; } @@ -270,11 +286,8 @@ class Pricedetail extends DBElement * * @return self */ - public function setPriceRelatedQuantity(int $new_price_related_quantity): self + public function setPriceRelatedQuantity(float $new_price_related_quantity): self { - //Assert::greaterThan($new_price_related_quantity, 0, - // 'The new price related quantity must be greater zero! Got %s.'); - $this->price_related_quantity = $new_price_related_quantity; return $this; @@ -299,11 +312,8 @@ class Pricedetail extends DBElement * * @return self */ - public function setMinDiscountQuantity(int $new_min_discount_quantity): self + public function setMinDiscountQuantity(float $new_min_discount_quantity): self { - //Assert::greaterThan($new_min_discount_quantity, 0, - // 'The new minimum discount quantity must be greater zero! Got %s.'); - $this->min_discount_quantity = $new_min_discount_quantity; return $this; diff --git a/src/Form/Part/OrderdetailType.php b/src/Form/Part/OrderdetailType.php index d009a935..8030667a 100644 --- a/src/Form/Part/OrderdetailType.php +++ b/src/Form/Part/OrderdetailType.php @@ -31,6 +31,7 @@ namespace App\Form\Part; +use App\Entity\Parts\MeasurementUnit; use App\Entity\Parts\Supplier; use App\Entity\PriceInformations\Orderdetail; use App\Form\Type\StructuralEntityType; @@ -74,7 +75,10 @@ class OrderdetailType extends AbstractType 'entry_type' => PricedetailType::class, 'allow_add' => true, 'allow_delete' => true, 'label' => false, - 'by_reference' => false + 'by_reference' => false, + 'entry_options' => [ + 'measurement_unit' => $options['measurement_unit'] + ] ]); } @@ -84,5 +88,8 @@ class OrderdetailType extends AbstractType 'data_class' => Orderdetail::class, 'error_bubbling' => false, ]); + + $resolver->setRequired('measurement_unit'); + $resolver->setAllowedTypes('measurement_unit', [MeasurementUnit::class, 'null']); } } \ No newline at end of file diff --git a/src/Form/Part/PartBaseType.php b/src/Form/Part/PartBaseType.php index 9aa2fa63..c2a6ba4f 100644 --- a/src/Form/Part/PartBaseType.php +++ b/src/Form/Part/PartBaseType.php @@ -150,6 +150,9 @@ class PartBaseType extends AbstractType 'allow_add' => true, 'allow_delete' => true, 'label' => false, 'by_reference' => false, + 'entry_options' => [ + 'measurement_unit' => $part->getPartUnit() + ] ]); $builder diff --git a/src/Form/Part/PricedetailType.php b/src/Form/Part/PricedetailType.php index c9f0eade..dcaf9c8d 100644 --- a/src/Form/Part/PricedetailType.php +++ b/src/Form/Part/PricedetailType.php @@ -32,10 +32,12 @@ namespace App\Form\Part; +use App\Entity\Parts\MeasurementUnit; use App\Entity\PriceInformations\Currency; use App\Entity\PriceInformations\Orderdetail; use App\Entity\PriceInformations\Pricedetail; use App\Form\Type\CurrencyEntityType; +use App\Form\Type\SIUnitType; use App\Form\Type\StructuralEntityType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\IntegerType; @@ -48,11 +50,13 @@ class PricedetailType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { //No labels needed, we define translation in templates - $builder->add("min_discount_quantity", IntegerType::class, [ - 'attr' => ['min' => 1] + $builder->add("min_discount_quantity", SIUnitType::class, [ + 'measurement_unit' => $options['measurement_unit'], + 'attr' => ['class' => 'form-control-sm'] ]); - $builder->add("price_related_quantity", IntegerType::class, [ - 'attr' => ['min' => 1] + $builder->add("price_related_quantity", SIUnitType::class, [ + 'measurement_unit' => $options['measurement_unit'], + 'attr' => ['class' => 'form-control-sm'] ]); $builder->add("price", NumberType::class); $builder->add("currency", CurrencyEntityType::class, ['required' => false]); @@ -64,5 +68,8 @@ class PricedetailType extends AbstractType 'data_class' => Pricedetail::class, 'error_bubbling' => false ]); + + $resolver->setRequired('measurement_unit'); + $resolver->setAllowedTypes('measurement_unit', [MeasurementUnit::class, 'null']); } } \ No newline at end of file diff --git a/src/Form/Type/SIUnitType.php b/src/Form/Type/SIUnitType.php index 91af5d68..6e8c1053 100644 --- a/src/Form/Type/SIUnitType.php +++ b/src/Form/Type/SIUnitType.php @@ -126,6 +126,13 @@ class SIUnitType extends AbstractType implements DataMapperInterface public function buildView(FormView $view, FormInterface $form, array $options) { + $view->vars['sm'] = false; + + //Check if we need to make this thing small + if (isset($options['attr']['class'])) { + $view->vars['sm'] = (strpos($options['attr']['class'], 'form-control-sm') !== false); + } + $view->vars['unit'] = $options['unit']; parent::buildView($view, $form, $options); // TODO: Change the autogenerated stub } diff --git a/src/Migrations/Version20190829104643.php b/src/Migrations/Version20190829104643.php index 60dcc42e..8a48eba8 100644 --- a/src/Migrations/Version20190829104643.php +++ b/src/Migrations/Version20190829104643.php @@ -46,6 +46,10 @@ final class Version20190829104643 extends AbstractMigration $this->addSql('ALTER TABLE `groups` CHANGE parent_id parent_id INT DEFAULT NULL, CHANGE datetime_added datetime_added DATETIME NOT NULL, CHANGE last_modified last_modified DATETIME NOT NULL'); $this->addSql('ALTER TABLE users CHANGE group_id group_id INT DEFAULT NULL, CHANGE password password VARCHAR(255) DEFAULT NULL, CHANGE first_name first_name VARCHAR(255) DEFAULT NULL, CHANGE last_name last_name VARCHAR(255) DEFAULT NULL, CHANGE department department VARCHAR(255) DEFAULT NULL, CHANGE email email VARCHAR(255) DEFAULT NULL, CHANGE config_language config_language VARCHAR(255) DEFAULT NULL, CHANGE config_timezone config_timezone VARCHAR(255) DEFAULT NULL, CHANGE config_theme config_theme VARCHAR(255) DEFAULT NULL, CHANGE datetime_added datetime_added DATETIME NOT NULL, CHANGE last_modified last_modified DATETIME NOT NULL'); + //Use float values for pricedetails amount, to use with non integer part units + $this->addSql('ALTER TABLE pricedetails CHANGE orderdetails_id orderdetails_id INT DEFAULT NULL, CHANGE id_currency id_currency INT DEFAULT NULL, CHANGE price_related_quantity price_related_quantity DOUBLE PRECISION NOT NULL, CHANGE min_discount_quantity min_discount_quantity DOUBLE PRECISION NOT NULL, CHANGE last_modified last_modified DATETIME NOT NULL, CHANGE datetime_added datetime_added DATETIME NOT NULL'); + + //Fix typo in attachment table names $this->addSql("ALTER TABLE attachements RENAME TO attachments;"); $this->addSql("ALTER TABLE attachement_types RENAME TO attachment_types;"); diff --git a/templates/Form/extendedBootstrap4_layout.html.twig b/templates/Form/extendedBootstrap4_layout.html.twig index 4f70adf0..a2279744 100644 --- a/templates/Form/extendedBootstrap4_layout.html.twig +++ b/templates/Form/extendedBootstrap4_layout.html.twig @@ -32,11 +32,11 @@ {%- endblock choice_widget_options -%} {% block si_unit_widget %} -
+
{{ form_widget(form.value) }} -
+
{% if form.prefix is defined %} - {{ form_widget(form.prefix, {'attr': {'class': 'custom-select btn'}}) }} + {{ form_widget(form.prefix, {'attr': {'class': 'custom-select'}}) }} {% endif %} {% if unit is not empty %}