diff --git a/app/models/order.rb b/app/models/order.rb index 7910dfc3..f4545d02 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -2,6 +2,8 @@ # class Order < ActiveRecord::Base + attr_accessor :ignore_warnings + # Associations has_many :order_articles, :dependent => :destroy has_many :articles, :through => :order_articles @@ -17,6 +19,7 @@ class Order < ActiveRecord::Base # Validations validates_presence_of :starts validate :starts_before_ends, :include_articles + validate :keep_ordered_articles # Callbacks after_save :save_order_articles, :update_price_of_group_orders @@ -55,7 +58,12 @@ class Order < ActiveRecord::Base end def article_ids - @article_ids ||= order_articles.map(&:article_id) + @article_ids ||= order_articles.map { |a| a.article_id.to_s } + end + + # Returns an array of article ids that lead to a validation error. + def erroneous_article_ids + @erroneous_article_ids ||= [] end def open? @@ -209,24 +217,24 @@ class Order < ActiveRecord::Base protected def starts_before_ends - errors.add(:ends, I18n.t('articles.model.error_starts_before_ends')) if (ends && starts && ends <= starts) + errors.add(:ends, I18n.t('orders.model.error_starts_before_ends')) if (ends && starts && ends <= starts) end def include_articles - errors.add(:articles, I18n.t('articles.model.error_nosel')) if article_ids.empty? + errors.add(:articles, I18n.t('orders.model.error_nosel')) if article_ids.empty? + end + + def keep_ordered_articles + chosen_order_articles = order_articles.find_all_by_article_id(article_ids) + to_be_removed = order_articles - chosen_order_articles + to_be_removed_but_ordered = to_be_removed.select { |a| a.quantity > 0 or a.tolerance > 0 } + unless to_be_removed_but_ordered.empty? or ignore_warnings + errors.add(:articles, I18n.t(stockit? ? 'orders.model.warning_ordered_stock' : 'orders.model.warning_ordered')) + @erroneous_article_ids = to_be_removed_but_ordered.map { |a| a.article_id } + end end def save_order_articles - #self.articles = Article.find(article_ids) # This doesn't deletes the group_order_articles, belonging to order_articles, - # # see http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many - # - ## Ensure to delete also the group_order_articles, belonging to order_articles - ## This case is relevant, when removing articles from a running order - #goa_ids = GroupOrderArticle.where(group_order_id: group_order_ids).includes(:order_article). - # select { |goa| goa.order_article.nil? }.map(&:id) - #GroupOrderArticle.delete_all(id: goa_ids) unless goa_ids.empty? - - # fetch selected articles articles_list = Article.find(article_ids) # create new order_articles diff --git a/app/views/orders/_form.html.haml b/app/views/orders/_form.html.haml index 78cd3ca8..86d24106 100644 --- a/app/views/orders/_form.html.haml +++ b/app/views/orders/_form.html.haml @@ -27,10 +27,14 @@ = category_name %i.icon-tag - for article in articles - / check if the article is selected - - included = @order.article_ids.include?(article.id) - - included_class = included ? ' selected' : '' - %tr{:class => included_class, :id => article.id.to_s } + / check if the article is selected or has an error + - included = @order.article_ids.include?(article.id.to_s) + - row_class = '' + - if included + - row_class = 'selected' + - elsif @order.erroneous_article_ids.include?(article.id) + - row_class = 'error' + %tr{class: row_class, id: article.id} %td= check_box_tag "order[article_ids][]", article.id, included, :id => "checkbox_#{article.id}" %td.click-me{'data-check-this' => "#checkbox_#{article.id}"}= article.name %td=h truncate article.note, :length => 25 @@ -52,3 +56,7 @@ .form-actions = f.submit class: 'btn' = link_to t('ui.or_cancel'), orders_path + - unless @order.erroneous_article_ids.empty? +   + = check_box_tag 'order[ignore_warnings]' + = t '.ignore_warnings' diff --git a/config/locales/de.yml b/config/locales/de.yml index c77f4dcb..abee707b 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -1302,6 +1302,7 @@ de: finish: notice: Die Bestellung wurde beendet. form: + ignore_warnings: Warnungen ignorieren name: Name note: Notiz origin: Herkunft @@ -1329,6 +1330,8 @@ de: error_starts_before_ends: muss nach dem Bestellstart liegen (oder leer bleiben) notice_close: ! 'Bestellung: %{name}, bis %{ends}' stock: Lager + warning_ordered: 'Warnung: Die rot markierten Artikel wurden in der laufenden Bestellung bereits bestellt. Wenn Du sie hier abwählst, werden alle bestehenden Bestellungen dieses Artikels gelöscht.' + warning_ordered_stock: 'Warnung: Die rot markierten Artikel wurden in der laufenden Lagerbestellung bereits bestellt bzw. gekauft. Wenn Du sie hier abwählst, werden alle bestehenden Bestellungen bzw. Käufe dieses Artikels gelöscht und nicht abgerechnet!' new: title: Neue Bestellung anlegen orders: diff --git a/config/locales/en.yml b/config/locales/en.yml index 3618f4bc..5608b458 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1306,6 +1306,7 @@ en: finish: notice: The order has been closed. form: + ignore_warnings: Ignore warnings name: Name note: Note origin: Origin @@ -1333,6 +1334,8 @@ en: error_starts_before_ends: must be after the start date (or remain empty) notice_close: ! 'Order: %{name}, until %{ends}' stock: Stock + warning_ordered: 'Warning: Articles marked red have already been ordered within this open order. If you uncheck them here, all existing orders of these articles will be deleted.' + warning_ordered_stock: 'Warning: Articles marked red have already been ordered/ purchased within this open stock order. If you uncheck them here, all existing orders/ purchases of these articles will be deleted and it will not be accounted for them.' new: title: Create new order orders: