mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-09 03:20:21 +00:00
Merge pull request #8766 from jibees/8477-cancel-order-and-send-confirmation-email-on-last-item-deletion
Edit order: on last item deletion, cancel the order and conditionally send cancellation email to consumer
This commit is contained in:
@@ -82,10 +82,23 @@ adjustItems = function(shipment_number, variant_id, quantity){
|
||||
var inventory_units = _.where(shipment.inventory_units, {variant_id: variant_id});
|
||||
|
||||
if (quantity == 0 && inventory_units.length == shipment.inventory_units.length) {
|
||||
ofnAlert(t("js.admin.orders.cannot_remove_last_item"));
|
||||
ofnCancelOrderAlert((confirm, sendEmailCancellation) => {
|
||||
if (confirm) {
|
||||
doAdjustItems(shipment_number, variant_id, quantity, inventory_units, () => {
|
||||
var redirectTo = new URL(Spree.routes.cancel_order.toString());
|
||||
redirectTo.searchParams.append("send_cancellation_email", sendEmailCancellation);
|
||||
window.location.href = redirectTo.toString();
|
||||
});
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
doAdjustItems(shipment_number, variant_id, quantity, inventory_units, () => {
|
||||
window.location.reload();
|
||||
});
|
||||
}
|
||||
|
||||
doAdjustItems = function(shipment_number, variant_id, quantity, inventory_units, callback) {
|
||||
var url = Spree.routes.orders_api + "/" + order_number + "/shipments/" + shipment_number;
|
||||
|
||||
var new_quantity = 0;
|
||||
@@ -106,7 +119,7 @@ adjustItems = function(shipment_number, variant_id, quantity){
|
||||
url: Spree.url(url),
|
||||
data: { variant_id: variant_id, quantity: new_quantity }
|
||||
}).done(function( msg ) {
|
||||
window.location.reload();
|
||||
callback();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -181,6 +194,24 @@ ofnAlert = function(message) {
|
||||
$('#custom-alert').show();
|
||||
}
|
||||
|
||||
ofnCancelOrderAlert = function(callback) {
|
||||
$('#custom-confirm .message').html(
|
||||
` ${t("js.admin.orders.cancel_the_order_html")}
|
||||
<div class="form">
|
||||
<input type="checkbox" name="send_cancellation_email" value="1" id="send_cancellation_email" />
|
||||
<label for="send_cancellation_email">${t("js.admin.orders.cancel_the_order_send_cancelation_email")}</label>
|
||||
</div>`);
|
||||
$('#custom-confirm button.confirm').unbind( "click" ).click(() => {
|
||||
$('#custom-confirm').hide();
|
||||
callback(true, $('#send_cancellation_email').is(':checked'));
|
||||
});
|
||||
$('#custom-confirm button.cancel').click(() => {
|
||||
$('#custom-confirm').hide();
|
||||
callback(false)
|
||||
});
|
||||
$('#custom-confirm').show();
|
||||
}
|
||||
|
||||
ofnConfirm = function(callback) {
|
||||
$('#custom-confirm').data($(event.target).data());
|
||||
$('#custom-confirm button.confirm').click(callback);
|
||||
|
||||
@@ -65,6 +65,7 @@ module Spree
|
||||
|
||||
def fire
|
||||
event = params[:e]
|
||||
@order.send_cancellation_email = params[:send_cancellation_email] == "true"
|
||||
if @order.public_send(event.to_s)
|
||||
flash[:success] = Spree.t(:order_updated)
|
||||
else
|
||||
|
||||
@@ -105,6 +105,7 @@ module Spree
|
||||
|
||||
after_save_commit DefaultAddressUpdater
|
||||
|
||||
attribute :send_cancellation_email, type: :boolean, default: true
|
||||
# -- Scopes
|
||||
scope :not_empty, -> {
|
||||
left_outer_joins(:line_items).where.not(spree_line_items: { id: nil })
|
||||
@@ -659,7 +660,7 @@ module Spree
|
||||
def after_cancel
|
||||
shipments.each(&:cancel!)
|
||||
|
||||
OrderMailer.cancel_email(id).deliver_later
|
||||
OrderMailer.cancel_email(id).deliver_later if send_cancellation_email
|
||||
self.payment_state = 'credit_owed' unless shipped?
|
||||
end
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
:variants_search => spree.admin_search_variants_url(:format => 'json'),
|
||||
:taxons_search => main_app.api_v0_taxons_url(:format => 'json'),
|
||||
:orders_api => main_app.api_v0_orders_url,
|
||||
:states_search => main_app.api_v0_states_url(:format => 'json')
|
||||
:states_search => main_app.api_v0_states_url(:format => 'json'),
|
||||
:cancel_order => spree.fire_admin_order_url(id: @order ? @order.number : "", e: 'cancel')
|
||||
}.to_json %>;
|
||||
</script>
|
||||
|
||||
@@ -148,5 +148,9 @@ table.index td.actions {
|
||||
.message {
|
||||
font-size: $h5-size;
|
||||
padding: 1.2em 0;
|
||||
|
||||
.form {
|
||||
padding: 1.2em 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3031,7 +3031,8 @@ See the %{link} to find out more about %{sitename}'s features and to start using
|
||||
invalid: "invalid"
|
||||
quantity_adjusted: "Insufficient stock available. Line item updated to maximum available quantity."
|
||||
quantity_unchanged: "Quantity unchanged from previous amount."
|
||||
cannot_remove_last_item: "Cannot remove last item from order. Please cancel order instead."
|
||||
cancel_the_order_html: "This will cancel the current order.<br />Are you sure you want to proceed?"
|
||||
cancel_the_order_send_cancelation_email: "Send a cancellation email to the customer"
|
||||
resend_user_email_confirmation:
|
||||
resend: "Resend"
|
||||
sending: "Resend..."
|
||||
@@ -3416,6 +3417,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using
|
||||
tracking: "Tracking"
|
||||
tracking_number: "Tracking Number"
|
||||
order_total: "Order Total"
|
||||
order_updated: "Order updated"
|
||||
customer_details: "Customer Details"
|
||||
customer_details_updated: "Customer Details updated"
|
||||
customer_search: "Customer Search"
|
||||
|
||||
@@ -126,6 +126,88 @@ describe '
|
||||
expect(page).not_to have_select2 "add_variant_id", with_options: [product.name]
|
||||
end
|
||||
|
||||
context "deleting item of an order" do
|
||||
context "when there a more than one items in the order" do
|
||||
let(:line_item) { create(:line_item) }
|
||||
|
||||
before do
|
||||
order.line_items << line_item
|
||||
login_as_admin_and_visit spree.edit_admin_order_path(order)
|
||||
find("a.delete-item").click
|
||||
expect(page).to have_content "Are you sure?"
|
||||
end
|
||||
|
||||
it "show a modal 'Are you sure?' that the user can close and then nothing change" do
|
||||
within(".modal", visible: true) do
|
||||
expect do
|
||||
click_on("Cancel")
|
||||
expect(page).not_to have_content "Are you sure?"
|
||||
end.not_to change { order.line_items.length }
|
||||
end
|
||||
end
|
||||
|
||||
it "show a modal 'Are you sure?' that the user confirm and then the item is deleted" do
|
||||
expect(order.line_items.length).to eq(2)
|
||||
within(".modal", visible: true) do
|
||||
expect do
|
||||
click_on("OK")
|
||||
expect(page).not_to have_css(".modal", visible: true)
|
||||
end.to change { order.reload.line_items.length }.by(-1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "when it is the last item of an order" do
|
||||
before do
|
||||
# specify that order has only one line item
|
||||
order.line_items = [order.line_items.first]
|
||||
login_as_admin_and_visit spree.edit_admin_order_path(order)
|
||||
find("a.delete-item").click
|
||||
within(".modal", visible: true) do
|
||||
# ignore first modal by confirming it
|
||||
click_on("OK")
|
||||
end
|
||||
end
|
||||
|
||||
context "it shows a second modal about last item deletion and therefore about order cancellation" do
|
||||
it "that the user can close and then nothing change" do
|
||||
expect(page).to have_content "This will cancel the current order."
|
||||
within(".modal", visible: true) do
|
||||
click_on("Cancel")
|
||||
end
|
||||
|
||||
expect(order.reload.line_items.length).to eq(1)
|
||||
expect(page).to have_selector('tr.stock-item', count: 1)
|
||||
end
|
||||
|
||||
context "that the user can confirm" do
|
||||
it "and then the order is cancelled and no email is sent by default" do
|
||||
expect do
|
||||
within(".modal", visible: true) do
|
||||
click_on("OK")
|
||||
end
|
||||
expect(page).to have_content "Cannot add item to canceled order"
|
||||
expect(order.reload.line_items.length).to eq(0)
|
||||
expect(order.reload.state).to eq("canceled")
|
||||
end.to_not have_enqueued_mail(Spree::OrderMailer, :cancel_email)
|
||||
end
|
||||
|
||||
it "and check the checkbox to send an email to the customer about its order cancellation" do
|
||||
expect do
|
||||
within(".modal", visible: true) do
|
||||
check("send_cancellation_email")
|
||||
click_on("OK")
|
||||
end
|
||||
expect(page).to have_content "Cannot add item to canceled order"
|
||||
expect(order.reload.line_items.length).to eq(0)
|
||||
expect(order.reload.state).to eq("canceled")
|
||||
end.to have_enqueued_mail(Spree::OrderMailer, :cancel_email)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "can't add more items than are available" do
|
||||
# Move the order back to the cart state
|
||||
order.state = 'cart'
|
||||
|
||||
Reference in New Issue
Block a user