diff --git a/.rubocop.yml b/.rubocop.yml deleted file mode 100644 index 0ce1e0d..0000000 --- a/.rubocop.yml +++ /dev/null @@ -1,101 +0,0 @@ -AllCops: - Exclude: - - '**/*.gemspec' - - '**/db/**/*' - - '**/Rakefile' - - '**/rails' - - 'bin/*' - - '**/spec/**/*' - -Layout/MultilineOperationIndentation: - EnforcedStyle: indented - -AlignParameters: - Enabled: false - -ClassLength: - CountComments: false - Max: 150 - -ModuleLength: - CountComments: false - Max: 250 - -Documentation: - Enabled: false - -LineLength: - Max: 150 - -MethodLength: - CountComments: false - Max: 50 - -BlockLength: - CountComments: false - Max: 50 - Exclude: - - '**/spec/**/*' - - '**/*.rake' - -Metrics/AbcSize: - Max: 45 - -Style/StringLiterals: - EnforcedStyle: single_quotes - -Layout/DotPosition: - EnforcedStyle: trailing - Enabled: true - -Style/FrozenStringLiteralComment: - Enabled: false - -Style/RegexpLiteral: - Enabled: false - -Style/WordArray: - Enabled: false - -Style/SymbolArray: - Enabled: false - -Style/GuardClause: - Enabled: false - -Style/TrailingCommaInLiteral: - Enabled: false - -Style/BarePercentLiterals: - Enabled: false - -Style/MutableConstant: - Enabled: false - -Style/PercentLiteralDelimiters: - Enabled: false - -Naming/VariableNumber: - Enabled: false - -Performance/RegexpMatch: - Enabled: false - -Style/UnneededPercentQ: - Enabled: false - -Lint/ParenthesesAsGroupedExpression: - Enabled: false - -Metrics/PerceivedComplexity: - Max: 10 - -Metrics/CyclomaticComplexity: - Max: 10 - -Style/ClassAndModuleChildren: - Enabled: false - -Style/AndOr: - Exclude: - - '**/*controller.rb' diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..d7edb56 --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +ruby-2.6.5 diff --git a/Gemfile b/Gemfile index 0c5f8e5..a5c820b 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,16 @@ +# frozen_string_literal: true + source 'https://rubygems.org' -gem 'spree', github: 'spree/spree', branch: '3-3-stable' + +git_source(:github) do |repo_name| + repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?('/') + "https://github.com/#{repo_name}.git" +end + +gem 'rails-controller-testing' +gem 'rubocop-rails', '~> 2.14', '>= 2.14.2' +gem 'spree', '~> 4.8' + group :test do gem 'timecop', '~> 0.8.1' end diff --git a/README.md b/README.md index 4c57bb4..2b57749 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,8 @@ SpreeGiftCard is an extension and one stop solution to integrate gift card funct * Recipient can then redeem the gift card by entering the unique gift card `Code` during checkout on payment step. +* Removed deface dependency. + ## Installation 1. Just add this line to your `Gemfile`: diff --git a/Rakefile b/Rakefile index 280d6d3..0bdbe25 100644 --- a/Rakefile +++ b/Rakefile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'bundler' Bundler::GemHelper.install_tasks @@ -6,7 +8,7 @@ require 'spree/testing_support/common_rake' RSpec::Core::RakeTask.new -task :default => [:spec] +task default: [:spec] desc 'Generates a dummy app for testing' task :test_app do diff --git a/Versionfile b/Versionfile deleted file mode 100644 index 6a19dd6..0000000 --- a/Versionfile +++ /dev/null @@ -1,5 +0,0 @@ -"2.1.x" => { :branch => "master" } -"2.0.x" => { :branch => "2-0-stable" } -"1.3.x" => { :branch => "1-3-stable" } -"1.2.x" => { :branch => "1-2-stable" } -"1.1.x" => { :branch => "1-1-stable" } diff --git a/app/controllers/spree/admin/gift_cards_controller.rb b/app/controllers/spree/admin/gift_cards_controller.rb index 8bcb944..29dd386 100644 --- a/app/controllers/spree/admin/gift_cards_controller.rb +++ b/app/controllers/spree/admin/gift_cards_controller.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true module Spree module Admin class GiftCardsController < Spree::Admin::ResourceController before_action :set_gift_card, :find_gift_card_variant, except: :destroy + before_action :find_gift_card, only: %i[edit show update resend] def create @object.assign_attributes(gift_card_params) @@ -11,18 +13,41 @@ def create flash[:success] = Spree.t(:successfully_created_gift_card) redirect_to admin_gift_cards_path else - render :new + redirect_to new_gift_card_path end end + def show; end + + def edit; end + + def update + flash[:success] = Spree.t(:successfully_updated_gift_card) if @gift_card.update(gift_card_params) + redirect_to admin_gift_cards_path + end + + def resend + order = @gift_card.order + + if order.present? + order.update_columns(gift_card_notified: false) # Reset notified flag. + order.gift_card_notification + flash[:success] = Spree.t(:gift_card_email_resent) + else + flash[:error] = Spree.t(:gift_card_order_not_found) + end + + redirect_back fallback_location: spree.edit_admin_gift_card_url(@gift_card) + end + private def collection - super.order(created_at: :desc).page(params[:page]).per(Spree::Config[:admin_orders_per_page]) + super.order(created_at: :desc).page(params[:page]).per(Spree::Backend::Config[:admin_orders_per_page]) end def set_gift_card - @is_e_gift_card = request.path.include?('new-digital') || (params[:gift_card] && params[:gift_card][:e_gift_card] == 'true') + @is_e_gift_card = request.path.include?('new-digital') || (params[:gift_card].present? && params[:gift_card][:e_gift_card] == 'true') end def find_gift_card_variant @@ -32,7 +57,7 @@ def find_gift_card_variant else products.not_e_gift_cards end - @gift_card_variant_id = products.first.master.id + @gift_card_variant_id = products&.first&.master&.id end def gift_card_params @@ -43,9 +68,14 @@ def gift_card_params :original_value, :sender_name, :sender_email, - :delivery_on + :delivery_on, + :state ) end + + def find_gift_card + @gift_card = Spree::GiftCard.find_by(id: params[:id]) + end end end end diff --git a/app/controllers/spree/checkout_controller_decorator.rb b/app/controllers/spree/checkout_controller_decorator.rb deleted file mode 100644 index 4cb18b7..0000000 --- a/app/controllers/spree/checkout_controller_decorator.rb +++ /dev/null @@ -1,54 +0,0 @@ -Spree::CheckoutController.class_eval do - before_action :load_gift_card, only: [:update], if: :payment_via_gift_card? - before_action :add_gift_card_payments, only: [:update], if: :payment_via_gift_card? - - private - - def add_gift_card_payments - if spree_current_user.present? && !spree_current_user.gift_cards.include?(@gift_card) - spree_current_user.gift_cards << @gift_card - end - @order.add_gift_card_payments(@gift_card) - - # Remove other payment method parameters. - params[:order].delete(:payments_attributes) - params.delete(:payment_source) - - # Return to the Payments page if additional payment is needed. - if @order.payments.valid.sum(:amount) < @order.total - redirect_to checkout_state_path(@order.state) - flash[:success] = Spree.t('gift_card_added_partial') - return - else - flash[:success] = Spree.t('gift_card_added') - end - end - - def payment_via_gift_card? - params[:state] == 'payment' && params[:order].present? && - params[:order].fetch(:payments_attributes, {}).present? && - params[:order][:payments_attributes].select { |payments_attribute| gift_card_payment_method.try(:id).to_s == payments_attribute[:payment_method_id] }.present? - end - - def load_gift_card - @gift_card = Spree::GiftCard.find_by(code: params[:payment_source][gift_card_payment_method.try(:id).to_s][:code]) - if @gift_card.nil? - @gift_card = import_integrated_gift_card - else - sync_integrated_gift_card(@gift_card) - end - - return if @gift_card - redirect_to checkout_state_path(@order.state), flash: { error: Spree.t('gift_code_not_found') } and return - end - - def gift_card_payment_method - @gift_card_payment_method ||= Spree::PaymentMethod.gift_card.available.first - end - - def import_integrated_gift_card; end - - def sync_integrated_gift_card(_gift_card) - true - end -end diff --git a/app/controllers/spree/gift_cards_controller.rb b/app/controllers/spree/gift_cards_controller.rb index 7d3fc01..f826ab4 100644 --- a/app/controllers/spree/gift_cards_controller.rb +++ b/app/controllers/spree/gift_cards_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Spree class GiftCardsController < Spree::StoreController before_action :load_master_variant, only: :new @@ -23,32 +25,31 @@ def new end def create - begin - # Wrap the transaction script in a transaction so it is an atomic operation - Spree::GiftCard.transaction do - @gift_card = GiftCard.new(gift_card_params) - @gift_card.save! - # Create line item - line_item = LineItem.new(quantity: 1) - line_item.gift_card = @gift_card - line_item.variant = @gift_card.variant - line_item.price = @gift_card.variant.price - # Add to order - order = current_order(create_order_if_necessary: true) - order.line_items << line_item - line_item.order = order - order.update_totals - order.updater.update_item_count - order.save! - # Save gift card - @gift_card.line_item = line_item - @gift_card.save! - end - redirect_to cart_path - rescue ActiveRecord::RecordInvalid - find_gift_card_variants - render :new + # Wrap the transaction script in a transaction so it is an atomic operation + Spree::GiftCard.transaction do + @gift_card = GiftCard.new(gift_card_params) + @gift_card.save! + # Create line item + line_item = LineItem.new(quantity: 1) + line_item.gift_card = @gift_card + line_item.variant = @gift_card.variant + line_item.price = @gift_card.variant.price + # Add to order + order = current_order(create_order_if_necessary: true) + order.line_items << line_item + line_item.order = order + order.update_totals + order.updater.update_item_count + order.save! + # Save gift card + @gift_card.line_item = line_item + @gift_card.save! end + redirect_to cart_path + rescue StandardError => e + flash[:error] = e.message + find_gift_card_variants + redirect_to new_gift_card_path end private @@ -59,13 +60,11 @@ def redirect_after_redeem def load_gift_card @gift_card = Spree::GiftCard.where(code: params[:id]).last - unless @gift_card - redirect_to root_path, flash: { error: Spree.t('gift_code_not_found') } - end + redirect_to root_path, flash: { error: ::Spree.t('gift_code_not_found') } unless @gift_card end def find_gift_card_variants - products = Product.not_deleted.gift_cards + products = Spree::Product.not_deleted.gift_cards products = if @is_e_gift_card products.e_gift_cards else @@ -73,7 +72,17 @@ def find_gift_card_variants end @gift_card_product = products.first gift_card_product_ids = products.pluck(:id) - @gift_card_variants = Variant.joins(:prices).where(["amount > 0 AND product_id IN (?)", gift_card_product_ids]).order("amount") + + @gift_card_variants = [] + + products.each do |product| + @gift_card_variants += if product.variants.present? + product.variants.select(&:non_zero_price?) + else + (product.master.price.positive? ? [product.master] : []) + end + end + @gift_card_variants = @gift_card_variants.sort_by(&:price) end def gift_card_params diff --git a/app/controllers/spree/products_controller_decorator.rb b/app/controllers/spree/products_controller_decorator.rb deleted file mode 100644 index 70dd402..0000000 --- a/app/controllers/spree/products_controller_decorator.rb +++ /dev/null @@ -1,9 +0,0 @@ -Spree::ProductsController.class_eval do - before_action :redirect_gift_card, only: :show - - private - - def redirect_gift_card - redirect_to new_gift_card_path(product_id: @product) && return if @product.try :is_gift_card? - end -end diff --git a/app/controllers/spree/user_gift_cards_controller.rb b/app/controllers/spree/user_gift_cards_controller.rb index dff0b61..b9d5577 100644 --- a/app/controllers/spree/user_gift_cards_controller.rb +++ b/app/controllers/spree/user_gift_cards_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Spree class UserGiftCardsController < Spree::StoreController before_action :authorize diff --git a/app/controllers/spree_gift_card/spree/admin/stores_controller_decorator.rb b/app/controllers/spree_gift_card/spree/admin/stores_controller_decorator.rb new file mode 100644 index 0000000..03a3bd5 --- /dev/null +++ b/app/controllers/spree_gift_card/spree/admin/stores_controller_decorator.rb @@ -0,0 +1,20 @@ +module SpreeGiftCard + module Spree + module Admin + module StoresControllerDecorator + def self.prepended(base) + base.after_action :update_preferences, only: [:update] + end + + private + def update_preferences + return unless @store.save + + ::Spree::Config.set(allow_gift_card_redeem: params[:allow_gift_card_redeem] == '1') + end + end + end + end +end + +::Spree::Admin::StoresController.prepend SpreeGiftCard::Spree::Admin::StoresControllerDecorator diff --git a/app/controllers/spree_gift_card/spree/checkout_controller_decorator.rb b/app/controllers/spree_gift_card/spree/checkout_controller_decorator.rb new file mode 100644 index 0000000..38ef2d2 --- /dev/null +++ b/app/controllers/spree_gift_card/spree/checkout_controller_decorator.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +module SpreeGiftCard + module Spree + module CheckoutControllerDecorator + def self.prepended(base) + base.before_action :load_gift_card, only: [:update], if: :payment_via_gift_card? + base.before_action :add_gift_card_payments, only: [:update], if: :payment_via_gift_card? + end + + private + + def add_gift_card_payments + if spree_current_user.present? && !spree_current_user.gift_cards.include?(@gift_card) + spree_current_user.gift_cards << @gift_card + end + @order.add_gift_card_payments(@gift_card) + + # Remove other payment method parameters. + params[:order].delete(:payments_attributes) + params.delete(:payment_source) + + # Return to the Payments page if additional payment is needed. + if @order.payments.valid.sum(:amount) < @order.total + redirect_to checkout_state_path(@order.state) + flash[:success] = ::Spree.t('gift_card_added_partial') + nil + else + flash[:success] = ::Spree.t('gift_card_added') + end + end + + def payment_via_gift_card? + params[:state] == 'payment' && params[:order].present? && + params[:order].fetch(:payments_attributes, {}).present? && + params[:order][:payments_attributes].select do |payments_attribute| + gift_card_payment_method.try(:id).to_s == payments_attribute[:payment_method_id] + end.present? + end + + def load_gift_card + @gift_card = ::Spree::GiftCard.find_by(code: params[:payment_source][gift_card_payment_method.try(:id).to_s][:code]) + if @gift_card.nil? + @gift_card = import_integrated_gift_card + else + sync_integrated_gift_card(@gift_card) + end + + return if @gift_card + redirect_to checkout_state_path(@order.state), flash: { error: ::Spree.t('gift_code_not_found') } and return + end + + def gift_card_payment_method + @gift_card_payment_method ||= ::Spree::PaymentMethod.gift_card.available.first + end + + def import_integrated_gift_card; end + + def sync_integrated_gift_card(_gift_card) + true + end + end + end +end + +::Spree::CheckoutController.prepend SpreeGiftCard::Spree::CheckoutControllerDecorator diff --git a/app/controllers/spree_gift_card/spree/products_controller_decorator.rb b/app/controllers/spree_gift_card/spree/products_controller_decorator.rb new file mode 100644 index 0000000..bd5d123 --- /dev/null +++ b/app/controllers/spree_gift_card/spree/products_controller_decorator.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module SpreeGiftCard + module Spree + module ProductsControllerDecorator + def self.prepended(base) + base.before_action :redirect_gift_card, only: :show + end + + private + + def redirect_gift_card + redirect_to new_gift_card_path(product_id: @product) && return if @product.try :is_gift_card? + end + end + end +end +::Spree::ProductsController.prepend SpreeGiftCard::Spree::ProductsControllerDecorator diff --git a/app/helpers/spree/gift_cards_helper.rb b/app/helpers/spree/gift_cards_helper.rb index 55bf3c8..1729b17 100644 --- a/app/helpers/spree/gift_cards_helper.rb +++ b/app/helpers/spree/gift_cards_helper.rb @@ -1,5 +1,9 @@ -module Spree::GiftCardsHelper - def variants_values(gift_card_variants) - gift_card_variants.map { |variant| ["#{variant.display_price} - #{variant.product.name}", variant.id] } +# frozen_string_literal: true + +module Spree + module GiftCardsHelper + def variants_values(gift_card_variants) + gift_card_variants.map { |variant| [variant.product.name.to_s.downcase, variant.id] } + end end end diff --git a/app/jobs/application_job.rb b/app/jobs/application_job.rb index d394c3d..bef3959 100644 --- a/app/jobs/application_job.rb +++ b/app/jobs/application_job.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class ApplicationJob < ActiveJob::Base # Automatically retry jobs that encountered a deadlock # retry_on ActiveRecord::Deadlocked diff --git a/app/jobs/spree_gift_card/send_email_job.rb b/app/jobs/spree_gift_card/send_email_job.rb index 9a857e8..7b0b9f9 100644 --- a/app/jobs/spree_gift_card/send_email_job.rb +++ b/app/jobs/spree_gift_card/send_email_job.rb @@ -1,24 +1,18 @@ +# frozen_string_literal: true + module SpreeGiftCard class SendEmailJob < ApplicationJob - queue_as :default + queue_as :gift_card - def perform(*_args) - e_gift_card_product = Spree::Product.find_by_slug('e-gift-card') - return unless e_gift_card_product + def perform(gift_card_id) + gift_card = ::Spree::GiftCard.find_by(id: gift_card_id) - Spree::GiftCard. - deliverable. - where(variant: e_gift_card_product.master). - where.not(line_item: nil). - each do |gift_card| - next unless gift_card_shipped(gift_card) - order = gift_card.line_item.order - Spree::OrderMailer.gift_card_email(gift_card.id, order).deliver_later - end - end + if gift_card.present? + order_id = gift_card.line_item.order.id - def gift_card_shipped(gift_card) - gift_card.line_item.inventory_units.all?(&:shipped?) + ::Spree::OrderMailer.gift_card_receiver(gift_card.id, order_id).deliver_later + ::Spree::OrderMailer.gift_card_sender(gift_card.id, order_id).deliver_later + end end end end diff --git a/app/mailers/spree/order_mailer_decorator.rb b/app/mailers/spree/order_mailer_decorator.rb deleted file mode 100644 index f3946e0..0000000 --- a/app/mailers/spree/order_mailer_decorator.rb +++ /dev/null @@ -1,15 +0,0 @@ -Spree::OrderMailer.class_eval do - def gift_card_email(card_id, order_id) - @gift_card = Spree::GiftCard.find_by(id: card_id) - @order = Spree::Order.find_by(id: order_id) - subject = "#{Spree::Store.current.name} #{Spree.t('gift_card_email.subject')}" - @gift_card.update_attribute(:sent_at, Time.now) - - mail( - to: @gift_card.email, - from: "#{@gift_card.sender_name} <#{from_address}>", - reply_to: "#{@gift_card.sender_name} <#{@gift_card.sender_email}>", - subject: subject - ) - end -end diff --git a/app/mailers/spree_gift_card/spree/order_mailer_decorator.rb b/app/mailers/spree_gift_card/spree/order_mailer_decorator.rb new file mode 100644 index 0000000..4b1d80a --- /dev/null +++ b/app/mailers/spree_gift_card/spree/order_mailer_decorator.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +module SpreeGiftCard + module Spree + module OrderMailerDecorator + def gift_card_receiver(card_id, order_id) + @gift_card = ::Spree::GiftCard.find_by(id: card_id) + @order = ::Spree::Order.find_by(id: order_id) + subject = "#{::Spree::Store.current.name} #{::Spree.t('gift_card_email.subject')}" + @gift_card.update_columns(sent_at: Time.now, gift_card_notified: true) + + mail( + to: @gift_card.email, + from: from_address, + subject: subject + ) + end + + def gift_card_sender(card_id, order_id) + @gift_card = ::Spree::GiftCard.find_by(id: card_id) + return unless @gift_card.sender_email + + @order = ::Spree::Order.find_by(id: order_id) + subject = "#{::Spree::Store.current.name} Notification! Gift Card delivered!" + mail( + to: @gift_card.sender_email, + from: from_address, + subject: subject + ) + end + end + end +end + +::Spree::OrderMailer.prepend SpreeGiftCard::Spree::OrderMailerDecorator diff --git a/app/models/concerns/spree/order/gift_card.rb b/app/models/concerns/spree/order/gift_card.rb index cc2e1b3..b5b1748 100644 --- a/app/models/concerns/spree/order/gift_card.rb +++ b/app/models/concerns/spree/order/gift_card.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Spree class Order module GiftCard @@ -6,8 +8,10 @@ module GiftCard def add_gift_card_payments(gift_card) payments.gift_cards.checkout.map(&:invalidate!) return unless gift_card.present? - payment_method = Spree::PaymentMethod::GiftCard.available.first + + payment_method = ::Spree::PaymentMethod::GiftCard.available.first raise 'Gift Card payment method could not be found' unless payment_method + amount_to_take = gift_card_amount(gift_card, outstanding_balance_after_applied_store_credit) create_gift_card_payment(payment_method, gift_card, amount_to_take) end @@ -17,7 +21,7 @@ def total_applied_gift_card end def using_gift_card? - total_applied_gift_card > 0 + total_applied_gift_card.positive? end def display_total_applied_gift_card @@ -35,12 +39,15 @@ def order_total_after_store_credit_with_gift_card def add_store_credit_payments payments.store_credits.where(state: 'checkout').map(&:invalidate!) remaining_total = outstanding_balance - total_applied_gift_card - return unless user && user.store_credits.any? + return unless user&.store_credits&.any? + payment_method = Spree::PaymentMethod::StoreCredit.available.first raise 'Store credit payment method could not be found' unless payment_method + user.store_credits.order_by_priority.each do |credit| break if remaining_total.zero? next if credit.amount_remaining.zero? + amount_to_take = store_credit_amount(credit, remaining_total) create_store_credit_payment(payment_method, credit, amount_to_take) remaining_total -= amount_to_take diff --git a/app/models/concerns/spree/quantifier_can_supply.rb b/app/models/concerns/spree/quantifier_can_supply.rb index 5f92add..ca57211 100644 --- a/app/models/concerns/spree/quantifier_can_supply.rb +++ b/app/models/concerns/spree/quantifier_can_supply.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + module Spree module QuantifierCanSupply def can_supply?(required = 1) - product = Spree::Variant.find(@variant).product + product = @variant.product if product.is_e_gift_card? true else diff --git a/app/models/spree.rb b/app/models/spree.rb index 11e0699..73f18be 100644 --- a/app/models/spree.rb +++ b/app/models/spree.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Spree def self.table_name_prefix 'spree_' diff --git a/app/models/spree/adjustment_decorator.rb b/app/models/spree/adjustment_decorator.rb deleted file mode 100644 index 5454e52..0000000 --- a/app/models/spree/adjustment_decorator.rb +++ /dev/null @@ -1,3 +0,0 @@ -Spree::Adjustment.class_eval do - scope :gift_card, -> { where(source_type: 'Spree::GiftCard') } -end diff --git a/app/models/spree/app_configuration_decorator.rb b/app/models/spree/app_configuration_decorator.rb deleted file mode 100644 index 8643623..0000000 --- a/app/models/spree/app_configuration_decorator.rb +++ /dev/null @@ -1,3 +0,0 @@ -Spree::AppConfiguration.class_eval do - preference :allow_gift_card_redeem, :boolean, default: true -end diff --git a/app/models/spree/calculator/gift_card_calculator.rb b/app/models/spree/calculator/gift_card_calculator.rb index 0fb3326..a96ea40 100644 --- a/app/models/spree/calculator/gift_card_calculator.rb +++ b/app/models/spree/calculator/gift_card_calculator.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require_dependency 'spree/calculator' module Spree diff --git a/app/models/spree/gift_card.rb b/app/models/spree/gift_card.rb index 66bd78d..41c9d75 100644 --- a/app/models/spree/gift_card.rb +++ b/app/models/spree/gift_card.rb @@ -1,20 +1,22 @@ -require 'spree/core/validators/email' +# frozen_string_literal: true module Spree - class GiftCard < ActiveRecord::Base - include CalculatedAdjustments + class GiftCard < Spree::Base + include Spree::CalculatedAdjustments include Spree::GiftCard::Users UNACTIVATABLE_ORDER_STATES = %w[complete awaiting_return returned].freeze - AUTHORIZE_ACTION = 'authorize'.freeze - CAPTURE_ACTION = 'capture'.freeze - VOID_ACTION = 'void'.freeze - CREDIT_ACTION = 'credit'.freeze + AUTHORIZE_ACTION = 'authorize' + CAPTURE_ACTION = 'capture' + VOID_ACTION = 'void' + CREDIT_ACTION = 'credit' VALUES = [50, 100, 150, 200, 250, 300, 500, 1000].freeze belongs_to :variant belongs_to :line_item + delegate :order, to: :line_item + has_many :transactions, class_name: 'Spree::GiftCardTransaction' validates :current_value, :original_value, :code, presence: true @@ -25,9 +27,8 @@ class GiftCard < ActiveRecord::Base validates :email, email: true end - validates :email, :name, :sender_name, :sender_email, :note, presence: true, if: :e_gift_card? - validates :sender_email, email: true, if: :e_gift_card? - + validates :email, :name, :sender_name, :sender_email, :note, presence: true + validates :sender_email, email: true validate :amount_remaining_is_positive, if: :current_value before_validation :generate_code, on: :create @@ -35,7 +36,15 @@ class GiftCard < ActiveRecord::Base scope :active, -> { where(active: true) } scope :inactive, -> { where(active: false) } - scope :deliverable, -> { active.where('sent_at IS NULL AND (delivery_on IS NULL OR delivery_on <= ?)', Time.now) } + scope :deliverable, -> { where('sent_at IS NULL AND (delivery_on IS NULL OR delivery_on <= ?)', Time.now) } + scope :pending, -> { where(state: 'pending')} + scope :completed, -> { where(state: 'completed')} + + state_machine :state, initial: :pending do + event :complete do + transition to: :completed, from: :pending + end + end def e_gift_card? variant.product.is_e_gift_card? @@ -61,6 +70,7 @@ def authorize(amount, options = {}) authorization_code = options[:action_authorization_code] if authorization_code return true if transactions.find_by(action: AUTHORIZE_ACTION, authorization_code: authorization_code) + errors.add(:base, Spree.t('gift_card_payment_method.unable_to_capture', auth_code: authorization_code)) return false else @@ -68,7 +78,8 @@ def authorize(amount, options = {}) end if valid_authorization?(amount) - transaction = transactions.build(action: AUTHORIZE_ACTION, amount: amount, authorization_code: authorization_code) + transaction = transactions.build(action: AUTHORIZE_ACTION, amount: amount, + authorization_code: authorization_code) transaction.order = Spree::Order.find_by(number: options[:order_number]) if options[:order_number] self.authorized_amount = authorized_amount + amount save! @@ -89,6 +100,7 @@ def valid_authorization?(amount) def capture(amount, authorization_code, options = {}) return false unless authorize(amount, action_authorization_code: authorization_code) + if amount <= authorized_amount transaction = transactions.build(action: CAPTURE_ACTION, amount: amount, authorization_code: authorization_code) transaction.order = Spree::Order.find_by(number: options[:order_number]) if options[:order_number] @@ -137,7 +149,10 @@ def compute_amount(calculable) end def debit(amount, order = nil) - raise 'Cannot debit gift card by amount greater than current value.' if (amount_remaining - amount.to_f.abs) < 0 + if (amount_remaining - amount.to_f.abs).negative? + raise 'Cannot debit gift card by amount greater than current value.' + end + transaction = transactions.build transaction.amount = amount transaction.order = order if order @@ -152,7 +167,7 @@ def price def order_activatable?(order) order && created_at < order.created_at && - current_value > 0 && + current_value.positive? && !UNACTIVATABLE_ORDER_STATES.include?(order.state) end @@ -161,7 +176,7 @@ def calculator end def actions - %i[capture void] + %w{capture void} end def generate_authorization_code @@ -176,6 +191,10 @@ def can_capture?(payment) %w[checkout pending].include?(payment.state) end + def display_gift_card_price(gc_price) + Spree::Money.new(gc_price, currency: self&.variant&.currency.present? ? self.variant.currency : 'GBP') + end + private def redeem(user) @@ -212,8 +231,8 @@ def generate_code end def set_values - self.current_value ||= line_item.try(:price) - self.original_value ||= line_item.try(:price) + self.current_value ||= variant.try(:price) + self.original_value ||= variant.try(:price) end def amount_remaining_is_positive diff --git a/app/models/spree/gift_card/users.rb b/app/models/spree/gift_card/users.rb index 0153efa..844bded 100644 --- a/app/models/spree/gift_card/users.rb +++ b/app/models/spree/gift_card/users.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + module Spree # GiftCard class enhancement - class GiftCard < ActiveRecord::Base + class GiftCard < Spree::Base has_many :user_gift_cards has_many :users, through: :user_gift_cards diff --git a/app/models/spree/gift_card_transaction.rb b/app/models/spree/gift_card_transaction.rb index 00fe68d..68f738f 100644 --- a/app/models/spree/gift_card_transaction.rb +++ b/app/models/spree/gift_card_transaction.rb @@ -1,6 +1,10 @@ -class Spree::GiftCardTransaction < ActiveRecord::Base - belongs_to :gift_card - belongs_to :order +# frozen_string_literal: true - validates :amount, :gift_card, presence: true +module Spree + class GiftCardTransaction < Spree::Base + belongs_to :gift_card + belongs_to :order + + validates :amount, :gift_card, presence: true + end end diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb deleted file mode 100644 index 489d6f8..0000000 --- a/app/models/spree/line_item_decorator.rb +++ /dev/null @@ -1,13 +0,0 @@ -Spree::LineItem.class_eval do - has_one :gift_card, dependent: :destroy - - MAXIMUM_GIFT_CARD_LIMIT ||= 1 - - with_options if: :is_gift_card? do - validates :gift_card, presence: true - validates :quantity, numericality: { less_than_or_equal_to: MAXIMUM_GIFT_CARD_LIMIT }, allow_nil: true - end - - delegate :is_gift_card?, to: :product - delegate :is_e_gift_card?, to: :product -end diff --git a/app/models/spree/order_contents_decorator.rb b/app/models/spree/order_contents_decorator.rb deleted file mode 100644 index 6e44f64..0000000 --- a/app/models/spree/order_contents_decorator.rb +++ /dev/null @@ -1,13 +0,0 @@ -Spree::OrderContents.class_eval do - def grab_line_item_by_variant(variant, raise_error = false, options = {}) - return if variant.product.is_gift_card? - - line_item = order.find_line_item_by_variant(variant, options) - - if !line_item.present? && raise_error - raise ActiveRecord::RecordNotFound, "Line item not found for variant #{variant.sku}" - end - - line_item - end -end diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb deleted file mode 100644 index 8afc046..0000000 --- a/app/models/spree/order_decorator.rb +++ /dev/null @@ -1,3 +0,0 @@ -Spree::Order.class_eval do - include Spree::Order::GiftCard -end diff --git a/app/models/spree/payment_decorator.rb b/app/models/spree/payment_decorator.rb deleted file mode 100644 index 8aa8587..0000000 --- a/app/models/spree/payment_decorator.rb +++ /dev/null @@ -1,18 +0,0 @@ -Spree::Payment.class_eval do - scope :gift_cards, -> { where(source_type: Spree::GiftCard.to_s) } - - delegate :gift_card?, to: :payment_method, allow_nil: true - - def store_credit_or_gift_card? - store_credit? || gift_card? - end - - private - - def invalidate_old_payments - return if store_credit_or_gift_card? - order.payments.with_state('checkout').where("id != ?", id).each do |payment| - payment.invalidate! unless payment.store_credit_or_gift_card? - end - end -end diff --git a/app/models/spree/payment_method/gift_card.rb b/app/models/spree/payment_method/gift_card.rb index 8c081fd..f5e9fc2 100644 --- a/app/models/spree/payment_method/gift_card.rb +++ b/app/models/spree/payment_method/gift_card.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Spree class PaymentMethod::GiftCard < PaymentMethod def payment_source_class @@ -20,7 +22,7 @@ def authorize(amount_in_cents, gift_card, gateway_options = {}) if gift_card.nil? ActiveMerchant::Billing::Response.new(false, Spree.t('gift_card_payment_method.unable_to_find'), {}, {}) else - action = -> (gift_card) do + action = lambda do |gift_card| sync_integrated_gift_card(gift_card) gift_card.authorize(amount_in_cents / 100.0.to_d, order_number: get_order_number(gateway_options)) end @@ -29,7 +31,7 @@ def authorize(amount_in_cents, gift_card, gateway_options = {}) end def capture(amount_in_cents, auth_code, gateway_options = {}) - action = -> (gift_card) do + action = lambda do |gift_card| sync_integrated_gift_card(gift_card) gift_card.capture(amount_in_cents / 100.0.to_d, auth_code, order_number: get_order_number(gateway_options)) end @@ -37,7 +39,7 @@ def capture(amount_in_cents, auth_code, gateway_options = {}) end def void(auth_code, gateway_options = {}) - action = -> (gift_card) do + action = lambda do |gift_card| gift_card.void(auth_code, order_number: get_order_number(gateway_options)) end handle_action(action, :void, auth_code) @@ -47,16 +49,18 @@ def purchase(amount_in_cents, gift_card, gateway_options = {}) if gift_card.nil? ActiveMerchant::Billing::Response.new(false, Spree.t('gift_card_payment_method.unable_to_find'), {}, {}) else - action = -> (gift_card) do + action = lambda do |gift_card| purchase_amount = amount_in_cents / 100.0.to_d - (authorize_code = gift_card.authorize(purchase_amount, order_number: get_order_number(gateway_options))) && gift_card.capture(purchase_amount, authorize_code, order_number: get_order_number(gateway_options)) + (authorize_code = gift_card.authorize(purchase_amount, + order_number: get_order_number(gateway_options))) && gift_card.capture(purchase_amount, authorize_code, + order_number: get_order_number(gateway_options)) end handle_action_call(gift_card, action, :authorize) end end def credit(amount_in_cents, auth_code, gateway_options = {}) - action = -> (gift_card) do + action = lambda do |gift_card| sync_integrated_gift_card(gift_card) gift_card.credit(amount_in_cents / 100.0.to_d, auth_code, order_number: get_order_number(gateway_options)) end @@ -106,7 +110,7 @@ def handle_action(action, action_name, auth_code) end def get_order_number(gateway_options) - gateway_options[:order_id].split('-').first if gateway_options[:order_id] + gateway_options[:order_id]&.split('-')&.first end end end diff --git a/app/models/spree/payment_method_decorator.rb b/app/models/spree/payment_method_decorator.rb deleted file mode 100644 index d1eee7d..0000000 --- a/app/models/spree/payment_method_decorator.rb +++ /dev/null @@ -1,7 +0,0 @@ -Spree::PaymentMethod.class_eval do - scope :gift_card, -> { where(type: 'Spree::PaymentMethod::GiftCard') } - - def gift_card? - self.class == Spree::PaymentMethod::GiftCard - end -end diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb deleted file mode 100644 index 7be0745..0000000 --- a/app/models/spree/product_decorator.rb +++ /dev/null @@ -1,6 +0,0 @@ -Spree::Product.class_eval do - scope :gift_cards, -> { where(is_gift_card: true) } - scope :not_gift_cards, -> { where(is_gift_card: false) } - scope :e_gift_cards, -> { where(is_e_gift_card: true) } - scope :not_e_gift_cards, -> { where(is_e_gift_card: false) } -end diff --git a/app/models/spree/shipment_decorator.rb b/app/models/spree/shipment_decorator.rb deleted file mode 100644 index 5649a2c..0000000 --- a/app/models/spree/shipment_decorator.rb +++ /dev/null @@ -1,18 +0,0 @@ -module Spree - Shipment.class_eval do - # state_machine do - # after_transition to: :shipped, do: :deliver_e_gift_cards - # end - - def deliver_e_gift_cards - e_gift_card_ids = line_items. - select(&:is_e_gift_card?). - map(&:gift_card). - map(&:id) - return if e_gift_card_ids.none? - Spree::GiftCard.deliverable.where(id: e_gift_card_ids).each do |gift_card| - Spree::OrderMailer.gift_card_email(gift_card.id, order).deliver_later - end - end - end -end diff --git a/app/models/spree/stock/quantifier_decorator.rb b/app/models/spree/stock/quantifier_decorator.rb deleted file mode 100644 index a17f30e..0000000 --- a/app/models/spree/stock/quantifier_decorator.rb +++ /dev/null @@ -1,7 +0,0 @@ -module Spree - module Stock - Quantifier.class_eval do - include Spree::QuantifierCanSupply - end - end -end diff --git a/app/models/spree/store_credit_category_decorator.rb b/app/models/spree/store_credit_category_decorator.rb deleted file mode 100644 index 6b57ac8..0000000 --- a/app/models/spree/store_credit_category_decorator.rb +++ /dev/null @@ -1,5 +0,0 @@ -module Spree - StoreCreditCategory.class_eval do - scope :gift_card, -> { where(name: 'Gift Card') } - end -end diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb deleted file mode 100644 index 2aee5ef..0000000 --- a/app/models/spree/user_decorator.rb +++ /dev/null @@ -1,4 +0,0 @@ -Spree::User.class_eval do - has_many :user_gift_cards - has_many :gift_cards, through: :user_gift_cards -end diff --git a/app/models/spree/user_gift_card.rb b/app/models/spree/user_gift_card.rb index ba1705b..ca529c3 100644 --- a/app/models/spree/user_gift_card.rb +++ b/app/models/spree/user_gift_card.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + module Spree - class UserGiftCard < ActiveRecord::Base + class UserGiftCard < Spree::Base belongs_to :gift_card belongs_to :user end diff --git a/app/models/spree_gift_card/adjustment_decorator.rb b/app/models/spree_gift_card/adjustment_decorator.rb new file mode 100644 index 0000000..c58000b --- /dev/null +++ b/app/models/spree_gift_card/adjustment_decorator.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module SpreeGiftCard + module Spree + module AdjustmentDecorator + def self.prepended(base) + base.scope :gift_card, -> { where(source_type: 'Spree::GiftCard') } + end + end + end +end + +::Spree::Adjustment.prepend SpreeGiftCard::Spree::AdjustmentDecorator diff --git a/app/models/spree_gift_card/line_item_decorator.rb b/app/models/spree_gift_card/line_item_decorator.rb new file mode 100644 index 0000000..9f25773 --- /dev/null +++ b/app/models/spree_gift_card/line_item_decorator.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module SpreeGiftCard + module Spree + module LineItemDecorator + MAXIMUM_GIFT_CARD_LIMIT ||= 1 + + def self.prepended(base) + base.has_one :gift_card, dependent: :destroy + + base.with_options if: :is_gift_card? do + validates :gift_card, presence: true + validates :quantity, numericality: { less_than_or_equal_to: MAXIMUM_GIFT_CARD_LIMIT }, allow_nil: true + end + + base.delegate :is_gift_card?, to: :product + base.delegate :is_e_gift_card?, to: :product + end + end + end +end + +::Spree::LineItem.prepend SpreeGiftCard::Spree::LineItemDecorator diff --git a/app/models/spree_gift_card/order_contents_decorator.rb b/app/models/spree_gift_card/order_contents_decorator.rb new file mode 100644 index 0000000..4e80ddc --- /dev/null +++ b/app/models/spree_gift_card/order_contents_decorator.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module SpreeGiftCard + module Spree + module OrderContentsDecorator + def grab_line_item_by_variant(variant, raise_error = false, options = {}) + return if variant.product.is_gift_card? + + line_item = order.find_line_item_by_variant(variant, options) + + if !line_item.present? && raise_error + raise ActiveRecord::RecordNotFound, "Line item not found for variant #{variant.sku}" + end + + line_item + end + end + end +end + +::Spree::OrderContents.prepend SpreeGiftCard::Spree::OrderContentsDecorator if Spree.version.to_f < 3.7 diff --git a/app/models/spree_gift_card/order_decorator.rb b/app/models/spree_gift_card/order_decorator.rb new file mode 100644 index 0000000..3a72ba1 --- /dev/null +++ b/app/models/spree_gift_card/order_decorator.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +module SpreeGiftCard + module Spree + module OrderDecorator + def self.prepended(base) + base.include ::Spree::Order::GiftCard + + base.after_update :gift_card_notification, if: :complete? + + base.after_update :giftcard_state_completed, if: :complete? + end + + def gift_card_notification + gift_cards = line_items.map(&:gift_card).compact + + if gift_cards.present? && !gift_card_notified + gift_cards.each do |gift_card| + ::SpreeGiftCard::SendEmailJob.set(wait_until: gift_card&.delivery_on).perform_later(gift_card.id) + end + end + + update_columns(gift_card_notified: true) + end + + def giftcard_state_completed + gift_cards = line_items.map(&:gift_card).compact + + gift_cards.each do |gift_card| + gift_card.complete + end + end + end + end +end + +::Spree::Order.prepend SpreeGiftCard::Spree::OrderDecorator diff --git a/app/models/spree_gift_card/payment_decorator.rb b/app/models/spree_gift_card/payment_decorator.rb new file mode 100644 index 0000000..57de9a7 --- /dev/null +++ b/app/models/spree_gift_card/payment_decorator.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module SpreeGiftCard + module Spree + module PaymentDecorator + def self.prepended(base) + base.scope :gift_cards, -> { where(source_type: ::Spree::GiftCard.to_s) } + + base.delegate :gift_card?, to: :payment_method, allow_nil: true + end + + def store_credit_or_gift_card? + store_credit? || gift_card? + end + + private + + def invalidate_old_payments + return if store_credit_or_gift_card? + + order.payments.with_state('checkout').where('id != ?', id).each do |payment| + payment.invalidate! unless payment.store_credit_or_gift_card? + end + end + end + end +end + +::Spree::Payment.prepend SpreeGiftCard::Spree::PaymentDecorator diff --git a/app/models/spree_gift_card/payment_method_decorator.rb b/app/models/spree_gift_card/payment_method_decorator.rb new file mode 100644 index 0000000..c1997ec --- /dev/null +++ b/app/models/spree_gift_card/payment_method_decorator.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module SpreeGiftCard + module Spree + module PaymentMethodDecorator + def self.prepended(base) + base.scope :gift_card, -> { where(type: 'Spree::PaymentMethod::GiftCard') } + end + + def gift_card? + instance_of?(::Spree::PaymentMethod::GiftCard) + end + end + end +end + +::Spree::PaymentMethod.prepend SpreeGiftCard::Spree::PaymentMethodDecorator diff --git a/app/models/spree_gift_card/product_decorator.rb b/app/models/spree_gift_card/product_decorator.rb new file mode 100644 index 0000000..5c5bf3b --- /dev/null +++ b/app/models/spree_gift_card/product_decorator.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module SpreeGiftCard + module Spree + module ProductDecorator + def self.prepended(base) + base.scope :gift_cards, -> { where(is_gift_card: true) } + base.scope :not_gift_cards, -> { where(is_gift_card: false) } + base.scope :e_gift_cards, -> { where(is_e_gift_card: true) } + base.scope :not_e_gift_cards, -> { where(is_e_gift_card: false) } + end + end + end +end + +::Spree::Product.prepend SpreeGiftCard::Spree::ProductDecorator diff --git a/app/models/spree_gift_card/shipment_decorator.rb b/app/models/spree_gift_card/shipment_decorator.rb new file mode 100644 index 0000000..505f945 --- /dev/null +++ b/app/models/spree_gift_card/shipment_decorator.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +module SpreeGiftCard + module Spree + module ShipmentDecorator + # state_machine do + # after_transition to: :shipped, do: :deliver_e_gift_cards + # end + + def deliver_e_gift_cards + e_gift_card_ids = line_items + .select(&:is_e_gift_card?) + .map(&:gift_card) + .map(&:id) + return if e_gift_card_ids.none? + + Spree::GiftCard.deliverable.where(id: e_gift_card_ids).each do |gift_card| + Spree::OrderMailer.gift_card_email(gift_card.id, order).deliver_later + end + end + end + end +end + +::Spree::Shipment.prepend SpreeGiftCard::Spree::ShipmentDecorator diff --git a/app/models/spree_gift_card/spree/stock/quantifier_decorator.rb b/app/models/spree_gift_card/spree/stock/quantifier_decorator.rb new file mode 100644 index 0000000..187777a --- /dev/null +++ b/app/models/spree_gift_card/spree/stock/quantifier_decorator.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true +module SpreeGiftCard + module Spree + module Stock + module QuantifierDecorator + include ::Spree::QuantifierCanSupply + end + end + end +end + +::Spree::Stock::Quantifier.prepend SpreeGiftCard::Spree::Stock::QuantifierDecorator diff --git a/app/models/spree_gift_card/store_credit_category_decorator.rb b/app/models/spree_gift_card/store_credit_category_decorator.rb new file mode 100644 index 0000000..bdd27bf --- /dev/null +++ b/app/models/spree_gift_card/store_credit_category_decorator.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true +module SpreeGiftCard + module Spree + module StoreCreditCategoryDecorator + def self.prepended(base) + base.scope :gift_card, -> { where(name: 'Gift Card') } + end + end + end +end + +::Spree::StoreCreditCategory.prepend SpreeGiftCard::Spree::StoreCreditCategoryDecorator diff --git a/app/models/spree_gift_card/user_decorator.rb b/app/models/spree_gift_card/user_decorator.rb new file mode 100644 index 0000000..06677cc --- /dev/null +++ b/app/models/spree_gift_card/user_decorator.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module SpreeGiftCard + module Spree + module UserDecorator + def self.prepended(base) + base.has_many :user_gift_cards + base.has_many :gift_cards, through: :user_gift_cards + end + end + end +end + +::Spree::User.prepend SpreeGiftCard::Spree::UserDecorator diff --git a/app/overrides/spree/admin/general_settings/edit/_gift_card_settings.html.erb.deface b/app/overrides/spree/admin/general_settings/edit/_gift_card_settings.html.erb.deface deleted file mode 100644 index 5f2ed2c..0000000 --- a/app/overrides/spree/admin/general_settings/edit/_gift_card_settings.html.erb.deface +++ /dev/null @@ -1,22 +0,0 @@ - - -
- <%= f.check_box(:is_gift_card) %> <%= f.label :is_gift_card, Spree.t("is_gift_card")%> -
-- <%= f.check_box(:is_e_gift_card) %> <%= f.label :is_e_gift_card, Spree.t("is_e_gift_card")%> -
diff --git a/app/overrides/spree/checkout/_summary/_gift_card_details.html.erb.deface b/app/overrides/spree/checkout/_summary/_gift_card_details.html.erb.deface deleted file mode 100644 index 0f5944b..0000000 --- a/app/overrides/spree/checkout/_summary/_gift_card_details.html.erb.deface +++ /dev/null @@ -1,7 +0,0 @@ - -<% if order.using_gift_card? %> -<%= Spree.t(:order)%> | +<%= I18n.t(:completed_at, scope: 'activerecord.attributes.spree/order') %> | <%= Spree.t(:code) %> | <%= Spree.t(:original_value) %> | <%= Spree.t(:current_value) %> | -<%= Spree.t(:is_e_gift_card) %> | <%= Spree.t(:email) %> | <%= Spree.t(:name) %> | <%= Spree.t(:sender_email) %> | @@ -27,18 +24,28 @@||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
<%= link_to card.line_item.order.number, edit_admin_order_path(card.line_item.order) %> | + <% else %> +<%= Spree.t(:admin_user) %> | + <% end %> + <% if order_completed %> +<%= card.line_item.order.completed_at %> | + <% else %> +<%= Spree.t(:admin_user) %> | + <% end %><%= card.code %> | -<%= number_to_currency card.original_value %> | -<%= number_to_currency card.current_value %> | -<%= card.variant.product.is_e_gift_card %> | +<%= card.display_gift_card_price(card.original_value) %> | +<%= card.display_gift_card_price(card.current_value) %> | <%= card.email %> | <%= card.name %> | -<%= card.sender_email %> | -<%= card.sender_name %> | +<%= card&.sender_email %> | +<%= card&.sender_name %> | <%= card.note %> | -+ | <%= link_to_edit card, no_text: true if can?(:edit, card) %> <%= link_to_delete card, no_text: true if can?(:delete, card) %> @@ -48,4 +55,4 @@ |
+ <%= f.check_box :is_gift_card %> <%= f.label :is_gift_card, Spree.t("is_gift_card") %> +
+<%= image_tag @gift_card.variant.images.first.attachment.url(:product), style: 'width: 240px; height: auto' %> | ||
Gift Card Amount: | -<%= number_to_currency @gift_card.price %> | +<%= @gift_card.display_gift_card_price(@gift_card.price) %> |
Gift Code: | <%= @gift_card.code %> | |
Claim Code: | -<%= link_to 'Click here', redeem_gift_card_url(@gift_card.code) %> | -|
Senders Note: | <%= @gift_card.note %> | |
Sent By: | -<%= @order.email %> | +<%= @gift_card&.sender_email %> |
Hi <%= @gift_card&.sender_name %>,
++ It is to notify you that your this Gift Card to <%= "#{@gift_card.name}" %> has been delivered. +
+ +Best Regards
+<%= Spree::Store.current.name %>
diff --git a/app/views/spree/orders/_line_item.html.erb b/app/views/spree/orders/_line_item.html.erb new file mode 100644 index 0000000..03679b2 --- /dev/null +++ b/app/views/spree/orders/_line_item.html.erb @@ -0,0 +1,31 @@ +<% variant = line_item.variant %> + +<%= order_form.fields_for :line_items, line_item do |item_form| %> +