foodcoop-adam/foodsoft

View on GitHub
app/views/group_orders/_form.html.haml

Summary

Maintainability
Test Coverage
- content_for :javascript do
  :javascript
    $(function() {
      #{data_to_js(@ordering_data)}
      setGroupBalance(#{@ordering_data[:available_funds]});
      setMinimumBalance(#{FoodsoftConfig[:minimum_balance] or 0});
      setToleranceBehaviour(#{FoodsoftConfig[:tolerance_is_costly]});
      setStockit(#{@order.stockit?});
      // create List for search-feature (using list.js, http://listjs.com)
      var listjsResetPlugin = ['reset', {highlightClass: 'btn-primary'}];
      var listjsDelayPlugin = ['delay', {delayedSearchTime: 500}];
      new List(document.body, {
        valueNames: ['name'],
        engine: 'unlist',
        plugins: [listjsResetPlugin, listjsDelayPlugin],
        // make large pages work too (as we don't have paging - articles may disappear!)
        page: 10000,
        indexAsync: true
      });
    });

- title t('.title'), false

.pull-right{style: 'margin: 16px 0px'}
  .form-search
    .input-append
      = text_field_tag :article, params[:article], placeholder: t('.search_article'), class: 'search-query delayed-search resettable'
      %button.add-on.btn.reset-search{:type => :button, :title => t('.reset_article_search')}
        %i.icon.icon-remove

= render :partial => 'heading'
= render :partial => 'min_quantity_notice' unless @order.stockit?

- unless @order.note.blank?
  %p
    %i= @order.note

= form_for @group_order do |f|
  = f.hidden_field :lock_version
  = f.hidden_field :order_id
  = f.hidden_field :updated_by_user_id
  = f.hidden_field :ordergroup_id
  - has_tolerance = (!@order.stockit? and !@ordering_data[:order_articles].values.index{|a| a[:unit] > 1}.nil?)
  %table.table.table-hover
    %thead
      %tr
        %th= heading_helper Article, :name
        - if @order.stockit?
          %th{style: 'width:120px'}= heading_helper StockArticle, :supplier
        %th{style: "width:13px;"}
        %th{style: "width:4.5em;"}= heading_helper Article, :unit
        %th{style: "width:4.5em;"}= t '.price'
        - if @order.stockit?
          %th(style="width:20px")= heading_helper StockArticle, :available
          %th#col_required= heading_helper GroupOrderArticle, :quantity
        - elsif has_tolerance
          %th{colspan: 2}= t '.units_all'
          %th#col_required= heading_helper GroupOrderArticle, :quantity
          %th#col_tolerance= heading_helper GroupOrderArticle, :tolerance
        - else
          %th
            %abbr{title: t('.units_all')}= t '.units_all_short'
          %th#col_required= heading_helper GroupOrderArticle, :quantity
        %th{style: "width:15px;"}= heading_helper GroupOrderArticle, :total_price
    %tbody.list
      - @order.articles_grouped_by_category.each do |category, order_articles|
        %tr.list-heading.article-category
          %td
            = category
            %i.icon-tag
          %td{colspan: "10"}
        - order_articles.each do |order_article|
          %tr{class: "#{cycle('even', 'odd', name: 'articles')} order-article", valign: "top"}
            %td.name{title: article_info_title(order_article.article)}
              = article_info_icon order_article.article
              = order_article.article.name
            - if @order.stockit?
              %td= truncate order_article.article.supplier.name, length: 15
            %td= h order_article.article.origin
            %td= order_article.article.unit
            %td= number_to_currency(@ordering_data[:order_articles][order_article.id][:price])
            %td.units_all
              %span{id: "unit_to_order_#{order_article.id}"}= order_article.units_to_order * order_article.price.unit_quantity
              %span.plus= @ordering_data[:order_articles][order_article.id][:unit] > 1 ? '+' : ' '.html_safe
            - if @order.stockit?
              %td= @ordering_data[:order_articles][order_article.id][:quantity_available]
            - elsif @ordering_data[:order_articles][order_article.id][:unit] > 1
              %td.units_pack
                - uq = @ordering_data[:order_articles][order_article.id][:unit]
                - progress_units = order_article.quantity+order_article.tolerance - order_article.units_to_order*uq
                - progress_pct = [100, 100*progress_units/uq].min.to_i
                .progress{id: "progress_#{order_article.id}"}
                  .bar{style: "width: #{progress_pct}%;"}
                    = progress_units
                  .bar{style: "width: #{100-progress_pct}%;"}
                    = [0, uq - progress_units].max
            - else
              - if has_tolerance
                %td

            %td.quantity
              -# we consider 1(used) + 2(unused) quantity display to be confusing; just show total requested
              -#%input{id: "q_#{order_article.id}", name: "group_order[group_order_articles_attributes][#{order_article.id}][quantity]", size: "2", type: "hidden", value: @ordering_data[:order_articles][order_article.id][:quantity]}/
              -#%span.used{id: "q_used_#{order_article.id}"}= @ordering_data[:order_articles][order_article.id][:used_quantity]
              -#+
              -#%span.unused{id: "q_unused_#{order_article.id}"}= @ordering_data[:order_articles][order_article.id][:quantity] - @ordering_data[:order_articles][order_article.id][:used_quantity]
              %input{id: "q_#{order_article.id}", name: "group_order[group_order_articles_attributes][#{order_article.id}][quantity]", size: "2", type: "hidden", value: @ordering_data[:order_articles][order_article.id][:quantity]}/
              %input{type: 'button', value: '-', 'data-decrease_quantity' => order_article.id}
              %span.total{id: "q_grouptotal_#{order_article.id}"}= @ordering_data[:order_articles][order_article.id][:quantity]
              %input{type: 'button', value: '+', 'data-increase_quantity' => order_article.id}

            - if has_tolerance
              %td.tolerance
                %input{id: "t_#{order_article.id}", name: "group_order[group_order_articles_attributes][#{order_article.id}][tolerance]", size: "2", type: "hidden", value: @ordering_data[:order_articles][order_article.id][:tolerance]}/
                - if (@ordering_data[:order_articles][order_article.id][:unit] > 1)
                  -#%span.used{id: "t_used_#{order_article.id}"}= @ordering_data[:order_articles][order_article.id][:used_tolerance]
                  -#+
                  -#%span.unused{id: "t_unused_#{order_article.id}"}= @ordering_data[:order_articles][order_article.id][:tolerance] - @ordering_data[:order_articles][order_article.id][:used_tolerance]
                  %input{type: 'button', value: '-', 'data-decrease_tolerance' => order_article.id}
                  %span.total{id: "t_grouptotal_#{order_article.id}"}= @ordering_data[:order_articles][order_article.id][:tolerance]
                  %input{type: 'button', value: '+', 'data-increase_tolerance' => order_article.id}

            %td{id: "td_price_#{order_article.id}", style: "text-align:right; padding-right:10px; width:4em"}
              %span{id: "price_#{order_article.id}_display"}= number_to_currency(@ordering_data[:order_articles][order_article.id][:total_price])
              -#.article-info
                .article-name= order_article.article.name
                .pull-right
                  = t('.units_full') + ':'
                  %span{id: "units_#{order_article.id}"}= order_article.units_to_order
                  %br/
                  = t('.units_total') + ':'
                  %span{id: "q_total_#{order_article.id}"}= @ordering_data[:order_articles][order_article.id][:quantity] + @ordering_data[:order_articles][order_article.id][:others_quantity]
                  %br/
                  = t('.total_tolerance') + ':'
                  %span{id: "t_total_#{order_article.id}"}= @ordering_data[:order_articles][order_article.id][:tolerance] + @ordering_data[:order_articles][order_article.id][:others_tolerance]
                  %br/
                .pull-left
                  #{heading_helper Article, :manufacturer}: #{order_article.article.manufacturer}
                  %br/
                  #{heading_helper Article, :units}: #{@order.stockit? ? order_article.article.quantity_available : @ordering_data[:order_articles][order_article.id][:unit]} * #{h order_article.article.unit}
                  %br/
                  #{heading_helper Article, :note}: #{order_article.article.note}
                  %br/
  #order-footer
    #info-box
    #total-sum
      %table
        %tr
          %td= t('.total_sum_amount') + ':'
          %td.currency
            %span#total_price= number_to_currency(@group_order.price)
        -#%tr
          %td= heading_helper(Ordergroup, :available_funds) + ':'
          %td.currency= number_to_currency(@ordering_data[:available_funds])
        %tr
          %td= t('.new_funds') + ':'
          %td.currency
            %strong
              %span#new_balance= number_to_currency(@ordering_data[:available_funds] - @group_order.price)
      #order-button
        = submit_tag( t('.action_save'), id: 'submit_button', class: 'btn btn-primary' )
        #{link_to t('ui.or_cancel'), root_path(anchor: '')}
  %input#total_balance{name: "total_balance", type: "hidden", value: @ordergroup.account_balance - @group_order.price}/
  %input{name: "version", type: "hidden", value: @version}/