From 57d1bee02f383dea18d4b853e35fad9818a85da8 Mon Sep 17 00:00:00 2001 From: Sokleng Houng Date: Thu, 28 Nov 2024 17:33:36 +0700 Subject: [PATCH] close #2096 filter order by vendor (#2097) --- app/models/spree_cm_commissioner/line_item_decorator.rb | 2 +- app/models/spree_cm_commissioner/order_decorator.rb | 4 ++++ .../orders/_search/vendor_search_field.html.erb.deface | 8 ++++++++ spec/models/spree/line_item_spec.rb | 2 +- 4 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 app/overrides/spree/admin/orders/_search/vendor_search_field.html.erb.deface diff --git a/app/models/spree_cm_commissioner/line_item_decorator.rb b/app/models/spree_cm_commissioner/line_item_decorator.rb index f22a6879b..4ce3971a4 100644 --- a/app/models/spree_cm_commissioner/line_item_decorator.rb +++ b/app/models/spree_cm_commissioner/line_item_decorator.rb @@ -19,7 +19,7 @@ def self.prepended(base) # rubocop:disable Metrics/MethodLength base.validate :ensure_not_exceed_max_quantity_per_order, if: -> { variant&.max_quantity_per_order.present? } base.whitelisted_ransackable_associations |= %w[guests] - base.whitelisted_ransackable_attributes |= %w[number to_date from_date] + base.whitelisted_ransackable_attributes |= %w[number to_date from_date vendor_id] base.delegate :delivery_required?, :permanent_stock?, :high_demand, to: :variant diff --git a/app/models/spree_cm_commissioner/order_decorator.rb b/app/models/spree_cm_commissioner/order_decorator.rb index 1962b4e26..a75fde52c 100644 --- a/app/models/spree_cm_commissioner/order_decorator.rb +++ b/app/models/spree_cm_commissioner/order_decorator.rb @@ -17,6 +17,10 @@ def self.prepended(base) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize ) } + base.scope :filter_by_vendor, lambda { |vendor| + joins(:line_items).where(spree_line_items: { vendor_id: vendor }).distinct + } + base.before_create :link_by_phone_number base.before_create :associate_customer diff --git a/app/overrides/spree/admin/orders/_search/vendor_search_field.html.erb.deface b/app/overrides/spree/admin/orders/_search/vendor_search_field.html.erb.deface new file mode 100644 index 000000000..06f34a0de --- /dev/null +++ b/app/overrides/spree/admin/orders/_search/vendor_search_field.html.erb.deface @@ -0,0 +1,8 @@ + + +
+
+ <%= label_tag :q_line_items_vendor_id_eq, Spree.t(:vendor) %> + <%= f.select :line_items_vendor_id_eq, Spree::Vendor.pluck(:name, :id), { include_blank: true }, class: 'select2-clear js-filterable' %> +
+
diff --git a/spec/models/spree/line_item_spec.rb b/spec/models/spree/line_item_spec.rb index cfefd33c1..c9d05588c 100644 --- a/spec/models/spree/line_item_spec.rb +++ b/spec/models/spree/line_item_spec.rb @@ -15,7 +15,7 @@ expect(described_class.whitelisted_ransackable_attributes).to match_array([ "variant_id", "order_id", "tax_category_id", "quantity", "price", "cost_price", "cost_currency", "adjustment_total", "additional_tax_total", "promo_total", "included_tax_total", "pre_tax_amount", "taxable_adjustment_total", - "non_taxable_adjustment_total", "number", "to_date", "from_date" + "non_taxable_adjustment_total", "number", "to_date", "from_date", "vendor_id" ]) end end