Skip to content

Commit

Permalink
Merge branch '12859-use-VINE-voucher' of github.com:rioug/openfoodnet…
Browse files Browse the repository at this point in the history
…work into 12859-use-VINE-voucher
  • Loading branch information
rioug committed Dec 2, 2024
2 parents 063ff57 + f5b9ca3 commit 167a10b
Show file tree
Hide file tree
Showing 103 changed files with 6,268 additions and 279 deletions.
2 changes: 1 addition & 1 deletion .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ STRIPE_PUBLIC_TEST_API_KEY="bogus_stripe_publishable_key"
SITE_URL="test.host"

OPENID_APP_ID="test-provider"
OPENID_APP_SECRET="12345"
OPENID_APP_SECRET="dummy-openid-app-secret-token"
OPENID_REFRESH_TOKEN="dummy-refresh-token"

ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY="test_primary_key"
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ gem "active_model_serializers", "0.8.4"
gem 'activerecord-session_store'
gem 'acts-as-taggable-on'
gem 'angularjs-file-upload-rails', '~> 2.4.1'
gem 'bigdecimal', '3.0.2'
gem 'bigdecimal'
gem 'bootsnap', require: false
gem 'geocoder'
gem 'gmaps4rails'
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ GEM
base64 (0.2.0)
bcp47_spec (0.2.1)
bcrypt (3.1.20)
bigdecimal (3.0.2)
bigdecimal (3.1.8)
bindata (2.5.0)
bindex (0.8.1)
bootsnap (1.18.3)
Expand Down Expand Up @@ -865,7 +865,7 @@ DEPENDENCIES
angularjs-rails (= 1.8.0)
arel-helpers (~> 2.12)
aws-sdk-s3
bigdecimal (= 3.0.2)
bigdecimal
bootsnap
bugsnag
bullet
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/order_completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def order_invalid_for_checkout?

def order_invalid!
Bugsnag.notify("Notice: invalid order loaded during checkout") do |payload|
payload.add_metadata :order, @order
payload.add_metadata :order, :order, @order
end

flash[:error] = t('checkout.order_not_loaded')
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/order_stock_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def check_order_cycle_expiry
return unless current_order_cycle&.closed?

Bugsnag.notify("Notice: order cycle closed during checkout completion") do |payload|
payload.add_metadata :order, current_order
payload.add_metadata :order, :order, current_order
end
current_order.empty!
current_order.set_order_cycle! nil
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/errors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def not_found
Bugsnag.notify("404") do |event|
event.severity = "info"

event.add_metadata(:request, request.env)
event.add_metadata(:request, :env, request.env)
end
render status: :not_found, formats: :html
end
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/spree/admin/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,10 @@ def set_stock_levels(product, on_hand, on_demand)

def notify_bugsnag(error, product, variant)
Bugsnag.notify(error) do |report|
report.add_metadata(:product, product.attributes)
report.add_metadata(:product_error, product.errors.first) unless product.valid?
report.add_metadata(:variant, variant.attributes)
report.add_metadata(:variant_error, variant.errors.first) unless variant.valid?
report.add_metadata(:product,
{ product: product.attributes, variant: variant.attributes })
report.add_metadata(:product, :product_error, product.errors.first) unless product.valid?
report.add_metadata(:product, :variant_error, variant.errors.first) unless variant.valid?
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/jobs/amend_backorder_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def amend_backorder(order)
urls = FdcUrlBuilder.new(reference_link)
orderer = FdcBackorderer.new(user, urls)

backorder = orderer.find_open_order
backorder = orderer.find_open_order(order)

variants = order_cycle.variants_distributed_by(distributor)
adjust_quantities(order_cycle, user, backorder, urls, variants)
Expand Down
6 changes: 4 additions & 2 deletions app/jobs/backorder_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ class BackorderJob < ApplicationJob
sidekiq_options retry: 0

def self.check_stock(order)
links = SemanticLink.where(variant_id: order.line_items.select(:variant_id))
links = SemanticLink.where(subject: order.variants)

perform_later(order) if links.exists?
rescue StandardError => e
# Errors here shouldn't affect the checkout. So let's report them
# separately:
Bugsnag.notify(e) do |payload|
payload.add_metadata(:order, order)
payload.add_metadata(:order, :order, order)
end
end

Expand Down Expand Up @@ -133,5 +133,7 @@ def place_order(user, order, orderer, backorder)
.perform_later(
user, order.distributor, order.order_cycle, placed_order.semanticId
)

order.exchange.semantic_links.create!(semantic_id: placed_order.semanticId)
end
end
6 changes: 6 additions & 0 deletions app/jobs/complete_backorder_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ class CompleteBackorderJob < ApplicationJob
# someone else's order.
def perform(user, distributor, order_cycle, order_id)
order = FdcBackorderer.new(user, nil).find_order(order_id)

return if order&.lines.blank?

urls = FdcUrlBuilder.new(order.lines[0].offer.offeredItem.semanticId)

variants = order_cycle.variants_distributed_by(distributor)
adjust_quantities(order_cycle, user, order, urls, variants)

FdcBackorderer.new(user, urls).complete_order(order)

exchange = order_cycle.exchanges.outgoing.find_by(receiver: distributor)
exchange.semantic_links.find_by(semantic_id: order_id)&.destroy!
rescue StandardError
BackorderMailer.backorder_incomplete(user, distributor, order_cycle, order_id).deliver_later

Expand Down
6 changes: 3 additions & 3 deletions app/jobs/stock_sync_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.sync_linked_catalogs(order)
# Errors here shouldn't affect the shopping. So let's report them
# separately:
Bugsnag.notify(e) do |payload|
payload.add_metadata(:order, order)
payload.add_metadata(:order, :order, order)
end
end

Expand All @@ -30,13 +30,13 @@ def self.sync_linked_catalogs_now(order)
# Errors here shouldn't affect the shopping. So let's report them
# separately:
Bugsnag.notify(e) do |payload|
payload.add_metadata(:order, order)
payload.add_metadata(:order, :order, order)
end
end

def self.catalog_ids(order)
stock_controlled_variants = order.variants.reject(&:on_demand)
links = SemanticLink.where(variant_id: stock_controlled_variants.map(&:id))
links = SemanticLink.where(subject: stock_controlled_variants)
semantic_ids = links.pluck(:semantic_id)
semantic_ids.map do |product_id|
FdcUrlBuilder.new(product_id).catalog_url
Expand Down
5 changes: 2 additions & 3 deletions app/jobs/subscription_confirm_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def confirm_order!(order)
send_failed_payment_email(order)
else
Bugsnag.notify(e) do |payload|
payload.add_metadata :order, order
payload.add_metadata :order, :order, order
end
send_failed_payment_email(order, e.message)
end
Expand Down Expand Up @@ -109,8 +109,7 @@ def send_failed_payment_email(order, error_message = nil)
SubscriptionMailer.failed_payment_email(order).deliver_now
rescue StandardError => e
Bugsnag.notify(e) do |payload|
payload.add_metadata :order, order
payload.add_metadata :error_message, error_message
payload.add_metadata :subscription_data, { order:, error_message: }
end
end
end
4 changes: 4 additions & 0 deletions app/models/exchange.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class Exchange < ApplicationRecord
has_many :exchange_fees, dependent: :destroy
has_many :enterprise_fees, through: :exchange_fees

# Links to open backorders of a distributor (outgoing exchanges only)
# Don't allow removal of distributor from OC while we have an open backorder.
has_many :semantic_links, as: :subject, dependent: :restrict_with_error

validates :sender_id, uniqueness: { scope: [:order_cycle_id, :receiver_id, :incoming] }

before_destroy :delete_related_exchange_variants, prepend: true
Expand Down
4 changes: 3 additions & 1 deletion app/models/semantic_link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

# Link a Spree::Variant to an external DFC SuppliedProduct.
class SemanticLink < ApplicationRecord
belongs_to :variant, class_name: "Spree::Variant"
self.ignored_columns += [:variant_id]

belongs_to :subject, polymorphic: true

validates :semantic_id, presence: true
end
2 changes: 1 addition & 1 deletion app/models/spree/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def add_product_management_abilities(user)
can [:admin, :index, :show, :create], ::Admin::ReportsController
can [:admin, :show, :create, :customers, :orders_and_distributors, :group_buys, :payments,
:orders_and_fulfillment, :products_and_inventory, :order_cycle_management,
:packing, :enterprise_fee_summary, :bulk_coop], :report
:packing, :enterprise_fee_summary, :bulk_coop, :suppliers], :report
end

def add_order_cycle_management_abilities(user)
Expand Down
6 changes: 5 additions & 1 deletion app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ def states
class_name: 'Spree::Adjustment',
dependent: :destroy
has_many :invoices, dependent: :restrict_with_exception

belongs_to :order_cycle, optional: true
has_one :exchange, ->(order) {
outgoing.to_enterprise(order.distributor)
}, through: :order_cycle, source: :exchanges
has_many :semantic_links, through: :exchange

belongs_to :distributor, class_name: 'Enterprise', optional: true
belongs_to :customer, optional: true
has_one :proxy_order, dependent: :destroy
Expand Down
4 changes: 0 additions & 4 deletions app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,6 @@ def set_property(property_name, property_value)
end
end

def total_on_hand
stock_items.sum(&:count_on_hand)
end

def properties_including_inherited
# Product properties override producer properties
ps = product_properties.all
Expand Down
2 changes: 1 addition & 1 deletion app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Variant < ApplicationRecord
has_many :exchanges, through: :exchange_variants
has_many :variant_overrides, dependent: :destroy
has_many :inventory_items, dependent: :destroy
has_many :semantic_links, dependent: :delete_all
has_many :semantic_links, as: :subject, dependent: :delete_all
has_many :supplier_properties, through: :supplier, source: :properties

localize_number :price, :weight
Expand Down
34 changes: 32 additions & 2 deletions app/services/fdc_backorderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(user, urls)
end

def find_or_build_order(ofn_order)
find_open_order || build_new_order(ofn_order)
find_open_order(ofn_order) || build_new_order(ofn_order)
end

def build_new_order(ofn_order)
Expand All @@ -19,7 +19,37 @@ def build_new_order(ofn_order)
end
end

def find_open_order
# Try the new method and fall back to old method.
def find_open_order(ofn_order)
lookup_open_order(ofn_order) || find_last_open_order
end

def lookup_open_order(ofn_order)
# There should be only one link at the moment but we may support
# ordering from multiple suppliers one day.
semantic_ids = ofn_order.semantic_links.pluck(:semantic_id)

semantic_ids.lazy
# Make sure we select an order from the right supplier:
.select { |id| id.starts_with?(urls.orders_url) }
# Fetch the order from the remote DFC server, lazily:
.map { |id| find_order(id) }
.compact
# Just in case someone completed the order without updating our database:
.select { |o| o.orderStatus[:path] == "Held" }
.first
# The DFC Connector doesn't recognise status values properly yet.
# So we are overriding the value with something that can be exported.
&.tap { |o| o.orderStatus = "dfc-v:Held" }
end

# DEPRECATED
#
# We now store links to orders we placed. So we don't need to search
# through all orders and pick a random open one.
# But for compatibility with currently open order cycles that don't have
# a stored link yet, we keep this method as well.
def find_last_open_order
graph = import(urls.orders_url)
open_orders = graph&.select do |o|
o.semanticType == "dfc-b:Order" && o.orderStatus[:path] == "Held"
Expand Down
5 changes: 2 additions & 3 deletions app/services/place_proxy_order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def call
rescue StandardError => e
summarizer.record_and_log_error(:processing, order, e.message)
Bugsnag.notify(e) do |payload|
payload.add_metadata :order, order
payload.add_metadata :order, :order, order
end
end

Expand Down Expand Up @@ -57,8 +57,7 @@ def initialise_order
true
rescue StandardError => e
Bugsnag.notify(e) do |payload|
payload.add_metadata :subscription, subscription
payload.add_metadata :proxy_order, proxy_order
payload.add_metadata(:proxy_order, { subscription:, proxy_order: })
end
false
end
Expand Down
10 changes: 5 additions & 5 deletions app/services/sets/product_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ def count_result(saved)

def notify_bugsnag(error, product, variant, variant_attributes)
Bugsnag.notify(error) do |report|
report.add_metadata(:product, product.attributes)
report.add_metadata(:product_error, product.errors.first) unless product.valid?
report.add_metadata(:variant_attributes, variant_attributes)
report.add_metadata(:variant, variant.attributes)
report.add_metadata(:variant_error, variant.errors.first) unless variant.valid?
report.add_metadata( :product_set,
{ product: product.attributes, variant_attributes:,
variant: variant.attributes } )
report.add_metadata(:product_set, :product_error, product.errors.first) if !product.valid?
report.add_metadata(:product_set, :variant_error, variant.errors.first) if !variant.valid?
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/variant_units/option_value_namer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def option_value_value_unit
def option_value_value_unit_scaled
unit_scale, unit_name = scale_for_unit_value

value = (@nameable.unit_value / unit_scale).to_d.truncate(2)
value = (@nameable.unit_value.to_d / unit_scale).round(2)

[value, unit_name]
end
Expand Down
14 changes: 14 additions & 0 deletions app/views/admin/reports/filters/_suppliers.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
= render 'admin/reports/date_range_form', f: f

.row
.alpha.two.columns= label_tag nil, t(:report_hubs)
.omega.fourteen.columns= f.collection_select(:distributor_id_in, @data.orders_distributors, :id, :name, {}, {class: "select2 fullwidth", multiple: true})

.row
.alpha.two.columns= label_tag nil, t(:report_producers)
.omega.fourteen.columns= select_tag(:supplier_id_in, options_from_collection_for_select(@data.orders_suppliers, :id, :name, params[:supplier_id_in]), {class: "select2 fullwidth", multiple: true})

.row
.alpha.two.columns= label_tag nil, t(:report_customers_cycle)
.omega.fourteen.columns
= f.select(:order_cycle_id_in, report_order_cycle_options(@data.order_cycles), {selected: params.dig(:q, :order_cycle_id_in)}, {class: "select2 fullwidth", multiple: true})
3 changes: 3 additions & 0 deletions app/webpacker/controllers/search_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export default class extends Controller {
}

changePage(event) {
const productsForm = document.querySelector("#products-form");
if (productsForm) productsForm.scrollIntoView({ behavior: "smooth" });

this.page.value = event.target.dataset.page;
this.submitSearch();
this.page.value = 1;
Expand Down
1 change: 1 addition & 0 deletions config/locales/ar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2804,6 +2804,7 @@ ar:
report_header_quantity: الكمية
report_header_max_quantity: اعلى كمية
report_header_variant: النوع
report_header_variant_unit_name: اسم وحدة النوع
report_header_variant_value: قيمة النوع
report_header_variant_unit: وحدة النوع
report_header_total_available: القيمة الكلية متاحة
Expand Down
1 change: 1 addition & 0 deletions config/locales/ca.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2791,6 +2791,7 @@ ca:
report_header_quantity: Quantitat
report_header_max_quantity: Quantitat màxima
report_header_variant: Variant
report_header_variant_unit_name: Nom de la unitat de la variant
report_header_variant_value: Valor de la variant
report_header_variant_unit: Unitat de la variant
report_header_total_available: Total disponible
Expand Down
1 change: 1 addition & 0 deletions config/locales/cy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2949,6 +2949,7 @@ cy:
report_header_quantity: Nifer
report_header_max_quantity: Uchafswm nifer
report_header_variant: Amrywiolyn
report_header_variant_unit_name: Enw Uned Amrywiolyn
report_header_variant_value: Gwerth Amrywiolyn
report_header_variant_unit: Uned Amrywiolyn
report_header_total_available: Cyfanswm ar gael
Expand Down
1 change: 1 addition & 0 deletions config/locales/de_CH.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2712,6 +2712,7 @@ de_CH:
report_header_quantity: Menge
report_header_max_quantity: Max Menge
report_header_variant: Produktvariante
report_header_variant_unit_name: Name der Varianteneinheit
report_header_variant_value: Wert der Produktvarianten
report_header_variant_unit: Varianteneinheit
report_header_total_available: Insgesamt verfügbar
Expand Down
1 change: 1 addition & 0 deletions config/locales/de_DE.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2929,6 +2929,7 @@ de_DE:
report_header_quantity: Menge
report_header_max_quantity: Max Menge
report_header_variant: Produktvariante
report_header_variant_unit_name: Name der Varianteneinheit
report_header_variant_value: Wert der Produktvarianten
report_header_variant_unit: Varianteneinheit
report_header_total_available: Insgesamt verfügbar
Expand Down
Loading

0 comments on commit 167a10b

Please sign in to comment.