Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/views/stock_takings/_stock_takings.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
%th= t '.date'
%th= t '.note'
%th
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't there be a table heading for this new 'balance' column? 🤔

%th
%tbody
- for stock_taking in @stock_takings
Comment thread
lentschi marked this conversation as resolved.
%tr
%td= link_to format_date(stock_taking.date), stock_taking
%td= truncate stock_taking.note
- balance = 0
- for stock_change in stock_taking.stock_changes
- balance += stock_change.quantity * stock_change.stock_article.gross_price
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, building sums like this may raise performance issues.
If there are a larger number of stock_changes it might be less resource hungry to do this with a db function. Have you considered using sum?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes a db function would be much more effective, I will try to implement it this way. I am not very familiar with this kind of programming and was glad that I could get working it this way on my local foodsoft installation to get the balances for our foodcoop...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mjavurek

I am not very familiar with this kind of programming

Admittedly what I'm suggesting here is quite complicated as we have to take article versioning into account (Something that your stock_change.stock_article automagically did, but with poor performance), so I looked into it myself - Adapting the StockTakings query in the controller's index action like this should work:

@stock_takings = StockEvent
                     .select(%(
                      `#{StockEvent.table_name}`.*,
                      SUM(
                         `#{StockChange.table_name}`.`quantity`
                         * (`#{ArticleVersion.table_name}`.`price` + (`#{ArticleVersion.table_name}`.`deposit`) * ((`#{ArticleVersion.table_name}`.`tax`) + 1))
                      ) AS balance
                     ))
                     .joins(stock_changes: { stock_article: [:article_versions] })
                     .joins(ArticleVersion.latest_outer_join_sql("#{Article.table_name}.#{Article.primary_key}"))
                     .where(later_article_versions: { id: nil })
                     .group('stock_event_id')
                     .order('date DESC')
                     .page(params[:page]).per(@per_page)

Then in the view you can use format_currency(stock_taking.balance) and remove the balance variable you introduced.

%td.numeric{style: 'width:5em'}= format_currency(balance)
%td
= link_to t('ui.edit'), edit_stock_taking_path(stock_taking), class: 'btn btn-mini'
= link_to t('ui.delete'), stock_taking, :data => {:confirm => t('.confirm_delete')}, :method => :delete,
Expand Down
16 changes: 15 additions & 1 deletion app/views/stock_takings/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,27 @@
%th= t '.article'
%th= t '.supplier'
%th= t '.unit'
%th
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't there be a table heading for this new 'balance' column? 🤔

%th= t '.amount'
%th
- balance = 0
- for stock_change in @stock_taking.stock_changes
%tr
%td= stock_change.stock_article.name
%td= stock_change.stock_article.supplier.name
%td= stock_change.stock_article.unit
%td= stock_change.quantity
%td.numeric{style: 'width:5em'}= format_currency(stock_change.stock_article.gross_price)
%td.numeric{style: 'width:5em'}= stock_change.quantity
- amount = stock_change.quantity * stock_change.stock_article.gross_price
- balance += amount
Comment thread
mjavurek marked this conversation as resolved.
%td.numeric{style: 'width:5em'}= format_currency(amount)
%tr
%th
%th
%th
%th
%th
%th.numeric{style: 'width:5em'}= format_currency(balance)

.btn-group
= link_to t('ui.edit'), edit_stock_taking_path(@stock_taking), class: 'btn'
Expand Down
Loading