Skip to content

Commit

Permalink
Fixes article form issues
Browse files Browse the repository at this point in the history
  • Loading branch information
lentschi committed Jul 1, 2022
1 parent a2f085b commit 1bb36a4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
30 changes: 10 additions & 20 deletions app/assets/javascripts/article-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ class ArticleForm {
const grossPrice = (price + deposit) * (tax / 100 + 1);
const fcPrice = grossPrice * (this.priceMarkup / 100 + 1);
const priceUnitLabel = this.getUnitLabel(this.priceUnit$.val());
let unitSuffix = priceUnitLabel.trim() === '' ? '' : ` x ${priceUnitLabel}`;
this.fcPrice$.text(isNaN(fcPrice) ? '?' : `${I18n.l('currency', fcPrice)}${unitSuffix}`);
this.fcPrice$.find('.price_value').text(isNaN(fcPrice) ? '?' : I18n.l('currency', fcPrice));
this.fcPrice$.find('.price_per_text').toggle(priceUnitLabel.trim() !== '');
this.fcPrice$.find('.price_unit').text(priceUnitLabel);
});

this.price$.trigger('change');
Expand Down Expand Up @@ -329,18 +330,6 @@ class ArticleForm {
$('tbody tr', this.unitRatiosTable$).remove();
}

const firstUnitRatioSet = !!firstUnitRatioQuantity$.val() && !!firstUnitRatioUnit$.val();
const billingUnitAndGroupOrderUnitVisible = unitRationsVisible && firstUnitRatioSet;

mergeJQueryObjects([
this.billingUnit$,
this.groupOrderUnit$
]).parents('.fold-line').toggle(billingUnitAndGroupOrderUnitVisible);

if (!billingUnitAndGroupOrderUnitVisible) {
mergeJQueryObjects([this.billingUnit$, this.groupOrderUnit$]).val('');
}

mergeJQueryObjects([
this.unit$,
this.supplierUnitSelect$,
Expand All @@ -360,9 +349,9 @@ class ArticleForm {
const numberOfRatios = $(`input[name^="${this.unitFieldsPrefix}[article_unit_ratios_attributes]"][name$="[quantity]"]`).length;

for (let i = numberOfRatios; i > 1; i--) {
const currentField$ = $(`input[name="${ratioQuantityFieldNameByIndex(i)}"]`, this.articleForm$);
const currentField$ = $(`input[name="${this.ratioQuantityFieldNameByIndex(i)}"]`, this.articleForm$);
const currentValue = currentField$.val();
const previousValue = $(`input[name="${ratioQuantityFieldNameByIndex(i - 1)}"]:last`, this.articleForm$).val();
const previousValue = $(`input[name="${this.ratioQuantityFieldNameByIndex(i - 1)}"]:last`, this.articleForm$).val();
currentField$.val(currentValue / previousValue);
}
}
Expand Down Expand Up @@ -443,20 +432,21 @@ class ArticleForm {
const ratioNameRegex = new RegExp(`${this.unitFieldsPrefix}\\[article_unit_ratios_attributes\\]\\[([0-9]+)\\]`);
const index = name.match(ratioNameRegex)[1];
quantity = quantity * previousValue;
currentField$ = $(`<input type="hidden" name="${ratioQuantityFieldNameByIndex(index)}" value="${quantity}" />`);
currentField$ = $(`<input type="hidden" name="${this.ratioQuantityFieldNameByIndex(index)}" value="${quantity}" />`);
td$.append(currentField$);
}

previousValue = quantity;
});
}
}

function ratioQuantityFieldNameByIndex(i) {
return `${this.unitFieldsPrefix}[article_unit_ratios_attributes][${i}][quantity]`;
ratioQuantityFieldNameByIndex(i) {
return `${this.unitFieldsPrefix}[article_unit_ratios_attributes][${i}][quantity]`;
}
}



function mergeJQueryObjects(array_of_jquery_objects) {
return $($.map(array_of_jquery_objects, function (el) {
return el.get();
Expand Down
7 changes: 5 additions & 2 deletions app/views/shared/_article_fields_price.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
.input-prepend
%span.add-on= t 'number.currency.format.unit'
= f.input_field :price, class: 'input-mini'
= f.input :price_unit, as: :select, collection: [], input_html: {'data-initial-value': article.price_unit, class: 'input-medium'}, label: 'per'.html_safe, include_blank: false
= f.input :price_unit, as: :select, collection: [], input_html: {'data-initial-value': article.price_unit, class: 'input-medium'}, label: t('articles.form.per'), include_blank: false
.fold-line
= f.input :tax do
.input-append
Expand All @@ -17,5 +17,8 @@
.control-group
%label.control-label{for: 'article_fc_price'}
= Article.human_attribute_name(:fc_price)
.controls.control-text#article_fc_price{:style => 'width:8em'}
.controls.control-text#article_fc_price
%span.price_value
%span.price_per_text=t('articles.form.per')
%span.price_unit

1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ en:
form:
title_edit: Edit article
title_new: Add new article
per: per
import_search_results:
action_import: import
already_imported: imported
Expand Down

0 comments on commit 1bb36a4

Please sign in to comment.