Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

send-shipment-email-optionally #11762

Merged
Merged
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
16 changes: 0 additions & 16 deletions app/assets/javascripts/admin/spree/orders/shipments.js.erb
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
// Shipments AJAX API

$(document).ready(function() {

handle_ship_click = function(){
var link = $(this);
var shipment_number = link.data('shipment-number');
var url = Spree.url( Spree.routes.orders_api + "/" + order_number + "/shipments/" + shipment_number + "/ship.json");
$.ajax({
type: "PUT",
url: url
}).done(function( msg ) {
window.location.reload();
}).error(function( msg ) {
console.log(msg);
});
}
$('.admin-order-edit-form a.ship').click(handle_ship_click);

//handle shipping method edit click
$('a.edit-method').click(toggleMethodEdit);
$('a.cancel-method').click(toggleMethodEdit);
Expand Down
8 changes: 8 additions & 0 deletions app/components/ship_order_component.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
= render ConfirmModalComponent.new(id: dom_id(@order, :ship), confirm_reflexes: "click->Admin::OrdersReflex#ship", controller: "orders", reflex: "Admin::Orders#ship") do
%div{class: "margin-bottom-30"}
%p= t('spree.admin.orders.shipment.mark_as_shipped_message_html')
%div{class: "margin-bottom-30"}
= hidden_field_tag :id, @order.id
= label_tag do
= check_box_tag :send_shipment_email, "1", true
= t('spree.admin.orders.shipment.mark_as_shipped_label_message')
7 changes: 7 additions & 0 deletions app/components/ship_order_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class ShipOrderComponent < ViewComponent::Base
def initialize(order:)
@order = order
end
end
6 changes: 2 additions & 4 deletions app/helpers/spree/admin/orders_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,8 @@ def notify_about_required_enterprise_number

def ship_order_link
{ name: t(:ship_order),
url: spree.fire_admin_order_path(@order, e: 'ship'),
method: 'put',
icon: 'icon-truck',
confirm: t(:are_you_sure) }
url: '#',
icon: 'icon-truck' }
end

def cancel_order_link
Expand Down
10 changes: 9 additions & 1 deletion app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class Order < ApplicationRecord
go_to_state :complete
end

attr_accessor :use_billing, :checkout_processing, :save_bill_address, :save_ship_address
attr_accessor :use_billing, :checkout_processing, :save_bill_address,
:save_ship_address
attr_writer :send_shipment_email

token_resource

Expand Down Expand Up @@ -616,6 +618,12 @@ def after_delivery_state?
state.in?(["payment", "confirmation"])
end

def send_shipment_email
return true if @send_shipment_email.nil?

@send_shipment_email
end

private

def reapply_tax_on_changed_address
Expand Down
2 changes: 1 addition & 1 deletion app/models/spree/shipment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def validate_shipping_method
def after_ship
inventory_units.each(&:ship!)
fee_adjustment.finalize!
send_shipped_email
send_shipped_email if order.send_shipment_email
touch :shipped_at
update_order_shipment_state
end
Expand Down
16 changes: 13 additions & 3 deletions app/reflexes/admin/orders_reflex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@ def capture
payment_capture = OrderCaptureService.new(@order)

if payment_capture.call
morph dom_id(@order), render(partial: "spree/admin/orders/table_row",
locals: { order: @order.reload, success: true })
cable_ready.replace(selector: dom_id(@order),
html: render(partial: "spree/admin/orders/table_row",
locals: { order: @order.reload, success: true }))
morph :nothing
else
flash[:error] = payment_capture.gateway_error || I18n.t(:payment_processing_failed)
morph_admin_flashes
end
end

def ship
@order.send_shipment_email = false unless params[:send_shipment_email]
if @order.ship
return set_param_for_controller if request.url.match?('edit')

morph dom_id(@order), render(partial: "spree/admin/orders/table_row",
locals: { order: @order.reload, success: true })
else
Expand Down Expand Up @@ -83,7 +88,8 @@ def send_invoices(params)
private

def authorize_order
@order = Spree::Order.find_by(id: element.dataset[:id])
id = element.dataset[:id] || params[:id]
@order = Spree::Order.find_by(id:)
authorize! :admin, @order
end

Expand All @@ -96,5 +102,9 @@ def success(i18n_key, count)
def editable_orders
Permissions::Order.new(current_user).editable_orders
end

def set_param_for_controller
params[:id] = @order.number
end
end
end
5 changes: 4 additions & 1 deletion app/views/admin/shared/_tooltip_button.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
%div{ "data-controller": "tooltip" }
%button{class: button_class, "data-reflex": button_reflex, "data-id": reflex_data_id, "data-tooltip-target": "element" }
- if local_assigns[:shipment]
%button{class: button_class,"data-controller": "modal-link", "data-action": "click->modal-link#open", "data-modal-link-target-value": "ship_order_#{reflex_data_id}", "data-id": reflex_data_id, "data-tooltip-target": "element" }
- else
%button{class: button_class, "data-reflex": button_reflex, "data-id": reflex_data_id, "data-tooltip-target": "element" }
.tooltip-container
.tooltip{"data-tooltip-target": "tooltip"}
= sanitize tooltip_text
Expand Down
5 changes: 4 additions & 1 deletion app/views/spree/admin/orders/_shipment.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
= Spree.t("shipment_states.#{shipment.state}")
- if shipment.ready? and can? :update, shipment
= "-"
= link_to t(:ship), '#', :class => 'ship button icon-arrow-right', :data => { 'shipment-number' => shipment.number }
%button{"class": "ship button icon-arrow-right","data-controller": "modal-link",
"data-action": "click->modal-link#open", "data-modal-link-target-value": dom_id(order, :ship)}= t(:ship)
%form
= render ShipOrderComponent.new(order: order)

%table.stock-contents.index
%colgroup
Expand Down
5 changes: 4 additions & 1 deletion app/views/spree/admin/orders/_table_row.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
%i.success.icon-ok-sign{"data-controller": "ephemeral"}
= render partial: 'admin/shared/tooltip', locals: {link_class: "icon_link with-tip icon-edit no-text" ,link: edit_admin_order_path(order), link_text: "", tooltip_text: t('spree.admin.orders.index.edit')}
- if order.ready_to_ship?
= render partial: 'admin/shared/tooltip_button', locals: {button_class: "icon-road icon_link with-tip no-text", button_reflex: "click->Admin::OrdersReflex#ship", reflex_data_id: order.id.to_s, tooltip_text: t('spree.admin.orders.index.ship')}
%form
= render ShipOrderComponent.new(order: order)
= render partial: 'admin/shared/tooltip_button', locals: {button_class: "icon-road icon_link with-tip no-text", reflex_data_id: order.id.to_s, tooltip_text: t('spree.admin.orders.index.ship'), shipment: true}

- if order.payment_required? && order.pending_payments.reject(&:requires_authorization?).any?
= render partial: 'admin/shared/tooltip_button', locals: {button_class: "icon-capture icon_link no-text", button_reflex: "click->Admin::OrdersReflex#capture", reflex_data_id: order.id.to_s, tooltip_text: t('spree.admin.orders.index.capture')}
14 changes: 10 additions & 4 deletions app/views/spree/admin/shared/_order_links.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
%i{ "data-dropdown-target": "arrow", "data-expanded-class": "icon-caret-up", "data-collapsed-class": "icon-caret-down" }
%div.menu{"data-dropdown-target": "menu"}
- order_links(@order).each do |link|
%a.menu_item{ href: link[:url], target: link[:target] || "_self", data: { method: link[:method], "ujs-navigate": link[:method] ? "false" : "undefined", confirm: link[:confirm] } }
%span
%i{ class: link[:icon] }
%span=link[:name]
- if link[:name] == t(:ship_order)
%a.menu_item{ href: link[:url], target: link[:target] || "_self", data: { "modal-link-target-value": dom_id(@order, :ship), "action": "click->modal-link#open", "controller": "modal-link" } }
%span
%i{ class: link[:icon] }
%span=link[:name]
- else
%a.menu_item{ href: link[:url], target: link[:target] || "_self", data: { method: link[:method], "ujs-navigate": link[:method] ? "false" : "undefined", confirm: link[:confirm] } }
%span
%i{ class: link[:icon] }
%span=link[:name]

= render 'spree/admin/shared/custom-confirm'
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4177,6 +4177,9 @@ See the %{link} to find out more about %{sitename}'s features and to start using
add_product:
cannot_add_item_to_canceled_order: "Cannot add item to canceled order"
include_out_of_stock_variants: "Include variants with no available stock"
shipment:
mark_as_shipped_message_html: "This will mark the order as Shipped.<br />Are you sure you want to proceed?"
mark_as_shipped_label_message: "Send a shipment/pick up notification email to the customer."
index:
listing_orders: "Listing Orders"
new_order: "New Order"
Expand Down
3 changes: 2 additions & 1 deletion spec/models/spree/shipment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@
expect(shipment.shipped_at).to_not be_nil
end

it "should send a shipment email" do
it "should send a shipment email if order.send_shipment_email is true" do
shipment.order.send_shipment_email = true
mail_message = double 'Mail::Message'
shipment_id = nil
expect(Spree::ShipmentMailer).to receive(:shipped_email) { |*args|
Expand Down
80 changes: 80 additions & 0 deletions spec/system/admin/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,86 @@ def new_order_with_distribution(distributor, order_cycle)
expect(page).to have_selector 'td.tax', text: shipping_fee.included_tax_total.to_s
end

context "shipping orders" do
before do
order.finalize! # ensure order has a payment to capture
order.payments << create(:check_payment, order:, amount: order.total)
order.payments.first.capture!
visit spree.edit_admin_order_path(order)
end

it "ships the order and shipment email is sent" do
expect(order.reload.shipped?).to be false

click_button 'Ship'

within ".reveal-modal" do
expect(page).to have_checked_field('Send a shipment/pick up ' \
'notification email to the customer.')
expect {
find_button("Confirm").click
}.to enqueue_job(ActionMailer::MailDeliveryJob).exactly(:once)
end

expect(order.reload.shipped?).to be true
expect(page).to have_text 'SHIPPED'
end

it "ships the order without sending email" do
expect(order.reload.shipped?).to be false

click_button 'Ship'

within ".reveal-modal" do
uncheck 'Send a shipment/pick up notification email to the customer.'
expect {
find_button("Confirm").click
}.to_not enqueue_job(ActionMailer::MailDeliveryJob)
end

save_screenshot('~/hello.png')
expect(order.reload.shipped?).to be true
expect(page).to have_text 'SHIPPED'
end

context "ship order from dropdown" do
it "ships the order and sends email" do
expect(order.reload.shipped?).to be false

find('.ofn-drop-down').click
click_link 'Ship Order'

within ".reveal-modal" do
expect(page).to have_checked_field('Send a shipment/pick up ' \
'notification email to the customer.')
expect {
find_button("Confirm").click
}.to enqueue_job(ActionMailer::MailDeliveryJob).exactly(:once)
end

expect(order.reload.shipped?).to be true
expect(page).to have_text 'SHIPPED'
end

it "ships the order without sending email" do
expect(order.reload.shipped?).to be false

find('.ofn-drop-down').click
click_link 'Ship Order'

within ".reveal-modal" do
uncheck 'Send a shipment/pick up notification email to the customer.'
expect {
find_button("Confirm").click
}.to_not enqueue_job(ActionMailer::MailDeliveryJob)
end

expect(order.reload.shipped?).to be true
expect(page).to have_text 'SHIPPED'
end
end
end

context "when an included variant has been deleted" do
let!(:deleted_variant) do
order.line_items.first.variant.tap(&:delete)
Expand Down
25 changes: 24 additions & 1 deletion spec/system/admin/orders_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -746,13 +746,36 @@ def extract_pdf_content
expect(page).to have_current_path spree.admin_orders_path
end

it "ship order from the orders index page" do
it "ship order from the orders index page and send email" do
order.payments.first.capture!
login_as_admin
visit spree.admin_orders_path

page.find("button.icon-road").click

within ".reveal-modal" do
expect {
find_button("Confirm").click
}.to enqueue_job(ActionMailer::MailDeliveryJob).exactly(:once)
end
expect(page).to have_css "i.success"
expect(order.reload.shipments.any?(&:shipped?)).to be true
expect(order.shipment_state).to eq("shipped")
end

it "ship order from the orders index page and do not send email" do
order.payments.first.capture!
login_as_admin
visit spree.admin_orders_path

page.find("button.icon-road").click

within ".reveal-modal" do
uncheck 'Send a shipment/pick up notification email to the customer.'
expect {
find_button("Confirm").click
}.not_to enqueue_job(ActionMailer::MailDeliveryJob)
end
expect(page).to have_css "i.success"
expect(order.reload.shipments.any?(&:shipped?)).to be true
expect(order.shipment_state).to eq("shipped")
Expand Down
Loading