From 683561617064922dc9064eb73f1e1be95040d576 Mon Sep 17 00:00:00 2001 From: FGU Date: Wed, 15 Feb 2023 16:06:12 +0100 Subject: [PATCH 1/5] wip --- Gemfile | 4 +-- app/models/group_order.rb | 20 +++++++++++++++ app/views/group_orders/_form.html.haml | 2 ++ docker-compose-dev.yml | 1 + spec/models/group_order_spec.rb | 34 +++++++++++++++++++++++--- 5 files changed, 56 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index 01c2cfd7..1ec045b9 100644 --- a/Gemfile +++ b/Gemfile @@ -90,8 +90,6 @@ group :development do # Get infos when not using proper eager loading gem 'bullet' - # Display Active Record queries as tables in the console - gem 'table_print' end group :development, :test do @@ -103,6 +101,8 @@ group :development, :test do # allow to use `debugger` https://github.com/conradirwin/pry-rescue gem 'pry-rescue' gem 'pry-stack_explorer' + # Display Active Record queries as tables in the console + gem 'table_print' end group :test do diff --git a/app/models/group_order.rb b/app/models/group_order.rb index c789ef4e..f6198985 100644 --- a/app/models/group_order.rb +++ b/app/models/group_order.rb @@ -35,6 +35,7 @@ class GroupOrder < ApplicationRecord data[:account_balance] = ordergroup.nil? ? BigDecimal.new('+Infinity') : ordergroup.account_balance data[:available_funds] = ordergroup.nil? ? BigDecimal.new('+Infinity') : ordergroup.get_available_funds(self) + # load prices and other stuff.... data[:order_articles] = {} order.articles_grouped_by_category.each do |article_category, order_articles| @@ -59,9 +60,28 @@ class GroupOrder < ApplicationRecord end end + # add counts from the previous group order + if previous_group_order + previous_group_order.group_order_articles.each do |goa| + order_article_id = OrderArticle.find_by(order: order, article: goa.order_article.article)&.id + data[:order_articles][order_article_id] ||= {} + data[:order_articles][order_article_id][:previous_quantity] = goa.quantity + data[:order_articles][order_article_id][:previous_tolerance] = goa.tolerance + end + end + + puts data + data end + def previous_group_order + previous_order = ordergroup.orders.where.not(id: order.id).recent.first + return nil unless previous_order + + ordergroup.group_orders.find_by(order_id: previous_order.id) + end + def save_group_order_articles for order_article in order.order_articles # Find the group_order_article, create a new one if necessary... diff --git a/app/views/group_orders/_form.html.haml b/app/views/group_orders/_form.html.haml index 3ffd583e..bdbc2138 100644 --- a/app/views/group_orders/_form.html.haml +++ b/app/views/group_orders/_form.html.haml @@ -109,6 +109,8 @@ %td.quantity %input{id: "q_#{order_article.id}", name: "group_order[group_order_articles_attributes][#{order_article.id}][quantity]", type: "hidden", value: @ordering_data[:order_articles][order_article.id][:quantity], 'data-min' => (@ordering_data[:order_articles][order_article.id][:quantity] if @order.boxfill?), 'data-max' => (@ordering_data[:order_articles][order_article.id][:quantity]+@ordering_data[:order_articles][order_article.id][:missing_units] if @order.boxfill?)}/ + = @ordering_data[:order_articles][order_article.id][:previous_quantity] + hello %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] diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml index b0a325db..decf26f3 100644 --- a/docker-compose-dev.yml +++ b/docker-compose-dev.yml @@ -6,6 +6,7 @@ services: command: ./proc-start web ports: - "3000:3000" + platform: linux/x86_64 foodsoft_worker: build: diff --git a/spec/models/group_order_spec.rb b/spec/models/group_order_spec.rb index a2b8a2c5..0be4e3fc 100644 --- a/spec/models/group_order_spec.rb +++ b/spec/models/group_order_spec.rb @@ -1,8 +1,10 @@ require_relative '../spec_helper' describe GroupOrder do - let(:user) { create :user, groups: [create(:ordergroup)] } - let(:order) { create :order } + let(:ordergroup) { create(:ordergroup) } + let(:user) { create :user, groups: [ordergroup] } + let(:supplier) { create(:supplier, article_count: 3) } + let(:order) { create :order, supplier: supplier } # the following two tests are currently disabled - https://github.com/foodcoops/foodsoft/issues/158 @@ -15,10 +17,36 @@ describe GroupOrder do # end describe do - let(:go) { create :group_order, order: order, ordergroup: user.ordergroup } + let(:go) { create :group_order, order: order, ordergroup: ordergroup } it 'has zero price initially' do expect(go.price).to eq(0) end end + + describe "load data for javascript" do + let(:group_order) { create :group_order, order: order, ordergroup: ordergroup } + let!(:article) { order.articles.first } + let(:order_article) { OrderArticle.find_or_create_by!(order: order, article: article) } + let(:previous_order) { create :order, supplier: supplier, starts: 20.days.ago, ends: 18.days.ago } + let(:previous_group_order) { create :group_order, order: previous_order, ordergroup: ordergroup } + let(:previous_order_article) { OrderArticle.find_or_create_by!(order: previous_order, article: article) } + + it "includes data from the last order" do + create :group_order_article, + order_article: previous_order_article, + group_order: previous_group_order, + quantity: 23, + tolerance: 11 + + order.order_articles.map(&:update_results!) + order.group_orders.map(&:update_price!) + + order_article_data = group_order.load_data[:order_articles][order_article.id] + + expect(order_article_data[:previous_quantity]).to eq(23) + expect(order_article_data[:previous_tolerance]).to eq(11) + + end + end end From b808f839314f8093e71cae427776790d4582bb10 Mon Sep 17 00:00:00 2001 From: FGU Date: Wed, 15 Feb 2023 16:09:11 +0100 Subject: [PATCH 2/5] wip --- app/models/group_order.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/group_order.rb b/app/models/group_order.rb index f6198985..09db3c07 100644 --- a/app/models/group_order.rb +++ b/app/models/group_order.rb @@ -63,7 +63,7 @@ class GroupOrder < ApplicationRecord # add counts from the previous group order if previous_group_order previous_group_order.group_order_articles.each do |goa| - order_article_id = OrderArticle.find_by(order: order, article: goa.order_article.article)&.id + order_article_id = OrderArticle.find_by!(order: order, article: goa.order_article.article)&.id data[:order_articles][order_article_id] ||= {} data[:order_articles][order_article_id][:previous_quantity] = goa.quantity data[:order_articles][order_article_id][:previous_tolerance] = goa.tolerance From 32547e495f3829b9ec970c967e341dc897b812e8 Mon Sep 17 00:00:00 2001 From: FGU Date: Wed, 15 Feb 2023 16:30:45 +0100 Subject: [PATCH 3/5] wip --- app/models/group_order.rb | 7 +++++-- app/views/group_orders/_form.html.haml | 11 ++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/app/models/group_order.rb b/app/models/group_order.rb index 09db3c07..722fe816 100644 --- a/app/models/group_order.rb +++ b/app/models/group_order.rb @@ -63,7 +63,10 @@ class GroupOrder < ApplicationRecord # add counts from the previous group order if previous_group_order previous_group_order.group_order_articles.each do |goa| - order_article_id = OrderArticle.find_by!(order: order, article: goa.order_article.article)&.id + order_article_id = OrderArticle.find_by(order: order, article: goa.order_article.article)&.id + puts " order ID: #{order.id}" + puts " article ID: #{goa.order_article.article.id}" + puts "ID: #{order_article_id}" data[:order_articles][order_article_id] ||= {} data[:order_articles][order_article_id][:previous_quantity] = goa.quantity data[:order_articles][order_article_id][:previous_tolerance] = goa.tolerance @@ -76,7 +79,7 @@ class GroupOrder < ApplicationRecord end def previous_group_order - previous_order = ordergroup.orders.where.not(id: order.id).recent.first + previous_order = ordergroup.orders.where(supplier: order.supplier).where.not(id: order.id).recent.first return nil unless previous_order ordergroup.group_orders.find_by(order_id: previous_order.id) diff --git a/app/views/group_orders/_form.html.haml b/app/views/group_orders/_form.html.haml index bdbc2138..e0ee6126 100644 --- a/app/views/group_orders/_form.html.haml +++ b/app/views/group_orders/_form.html.haml @@ -95,7 +95,14 @@ %td{colspan: "9"} - order_articles.each do |order_article| %tr{class: "#{cycle('even', 'odd', name: 'articles')} order-article #{get_missing_units_css_class(@ordering_data[:order_articles][order_article.id][:missing_units])}", valign: "top"} - %td.name= order_article.article.name + %td.name + = order_article.article.name + - if @ordering_data[:order_articles][order_article.id][:previous_quantity] + %span.label + last time: + = @ordering_data[:order_articles][order_article.id][:previous_quantity] + + + = @ordering_data[:order_articles][order_article.id][:previous_tolerance] - if @order.stockit? %td= truncate order_article.article.supplier.name, length: 15 %td= h order_article.article.origin @@ -109,8 +116,6 @@ %td.quantity %input{id: "q_#{order_article.id}", name: "group_order[group_order_articles_attributes][#{order_article.id}][quantity]", type: "hidden", value: @ordering_data[:order_articles][order_article.id][:quantity], 'data-min' => (@ordering_data[:order_articles][order_article.id][:quantity] if @order.boxfill?), 'data-max' => (@ordering_data[:order_articles][order_article.id][:quantity]+@ordering_data[:order_articles][order_article.id][:missing_units] if @order.boxfill?)}/ - = @ordering_data[:order_articles][order_article.id][:previous_quantity] - hello %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] From e5cd68a4ad5a6bec67a6470765877abf8f8e8c4c Mon Sep 17 00:00:00 2001 From: FGU Date: Mon, 20 Feb 2023 16:24:46 +0100 Subject: [PATCH 4/5] wip --- app/assets/javascripts/ordering.js | 8 ++++++-- app/views/group_orders/_form.html.haml | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/ordering.js b/app/assets/javascripts/ordering.js index a3dd1050..3d4027fe 100644 --- a/app/assets/javascripts/ordering.js +++ b/app/assets/javascripts/ordering.js @@ -226,10 +226,14 @@ $(function() { $('a[data-decrease_tolerance]').on('touchclick', function() { decreaseTolerance($(this).data('decrease_tolerance')); }); - + $('a[data-reorder_previous]').on('touchclick', function() { + console.log('reorder_previous'); + update($(this).data('reorder_previous'), $(this).data('quantity'), $(this).data('tolerance')); + }); + $('a[data-confirm_switch_order]').on('touchclick', function() { return (!modified || confirm(I18n.t('js.ordering.confirm_change'))); }); updateButtons($(document)); -}); +}); \ No newline at end of file diff --git a/app/views/group_orders/_form.html.haml b/app/views/group_orders/_form.html.haml index e0ee6126..c9478de1 100644 --- a/app/views/group_orders/_form.html.haml +++ b/app/views/group_orders/_form.html.haml @@ -103,6 +103,9 @@ = @ordering_data[:order_articles][order_article.id][:previous_quantity] + = @ordering_data[:order_articles][order_article.id][:previous_tolerance] + %a.btn.btn-ordering{"data-reorder_previous": order_article.id, "data-quantity": @ordering_data[:order_articles][order_article.id][:previous_quantity], "data-tolerance": @ordering_data[:order_articles][order_article.id][:previous_tolerance]} + %i.icon-repeat + order previous - if @order.stockit? %td= truncate order_article.article.supplier.name, length: 15 %td= h order_article.article.origin From 66c56ea24df151b0f2c74fa479e48f6418709cd4 Mon Sep 17 00:00:00 2001 From: FGU Date: Thu, 23 Feb 2023 12:12:19 +0100 Subject: [PATCH 5/5] WIP on favorites --- app/assets/javascripts/ordering.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/assets/javascripts/ordering.js b/app/assets/javascripts/ordering.js index 3d4027fe..da966103 100644 --- a/app/assets/javascripts/ordering.js +++ b/app/assets/javascripts/ordering.js @@ -228,7 +228,7 @@ $(function() { }); $('a[data-reorder_previous]').on('touchclick', function() { console.log('reorder_previous'); - update($(this).data('reorder_previous'), $(this).data('quantity'), $(this).data('tolerance')); + // update($(this).data('reorder_previous'), $(this).data('quantity'), $(this).data('tolerance')); }); $('a[data-confirm_switch_order]').on('touchclick', function() {