Allow quantites in Orderdetails to be floats.

Useful for non integer based part units.
This commit is contained in:
Jan Böhmer 2019-08-31 13:08:02 +02:00
parent 40889832ca
commit b280d0c28b
7 changed files with 63 additions and 25 deletions

View file

@ -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;

View file

@ -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']);
}
}

View file

@ -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

View file

@ -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']);
}
}

View file

@ -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
}

View file

@ -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;");

View file

@ -32,11 +32,11 @@
{%- endblock choice_widget_options -%}
{% block si_unit_widget %}
<div class="input-group">
<div class="input-group {% if sm %}input-group-sm{% endif %}">
{{ form_widget(form.value) }}
<div class="input-group-append">
<div class="input-group-append {% if sm %}input-group-sm{% endif %}">
{% 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 %}
<label class="input-group-text">{{ unit }}</label>