Class: Spree::Admin::BoxnowController

Inherits:
BaseController
  • Object
show all
Includes:
OrdersFiltersHelper
Defined in:
app/controllers/spree/admin/boxnow_controller.rb

Instance Method Summary collapse

Instance Method Details

#cancelObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/spree/admin/boxnow_controller.rb', line 77

def cancel
  load_order

  @order.shipments.each do |shipment|
    next unless shipment.can_cancel_boxnow_voucher?

    SpreeBoxnow::CancelVoucher.new(shipment).call
  end

  flash[:success] = Spree.t('admin.integrations.boxnow.voucher_successfully_cancelled')
rescue ActiveRecord::RecordNotFound
  order_not_found
rescue StandardError => e
  Rails.logger.error "Boxnow Error: #{e.message}"

  flash[:error] = "#{Spree.t('admin.integrations.boxnow.voucher_cancellation_failed')}: #{e.message}"
end

#createObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/spree/admin/boxnow_controller.rb', line 8

def create
  begin
    load_order

    @order.shipments.each do |shipment|
      next unless shipment.can_create_boxnow_voucher?

      result = SpreeBoxnow::CreateVoucher.new(shipment).call

      shipment.tracking = result[:parcel_id]
      shipment.['boxnow.vg_child'] = result[:child_parcel_ids]
      shipment.save!
    end

    flash[:success] = Spree.t('admin.integrations.boxnow.voucher_successfully_created')
  rescue ActiveRecord::RecordNotFound
    order_not_found
  rescue StandardError => e
    Rails.logger.error "Boxnow Error: #{e.message}"

    flash[:error] = "#{Spree.t('admin.integrations.boxnow.voucher_creation_failed')}: #{e.message}"
  end
end


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/spree/admin/boxnow_controller.rb', line 32

def print
  begin
    load_order

    shipments = @order.shipments.select(&:can_print_boxnow_voucher?)

    voucher_numbers = shipments.flat_map do |shipment|
      child_ids = shipment.['boxnow.vg_child'] || []
      [shipment.tracking] + child_ids
    end

    if voucher_numbers.empty?
      raise StandardError, Spree.t('admin.integrations.boxnow.voucher_print_failed')
    end

    pdf_contents = voucher_numbers.map do |parcel_id|
      SpreeBoxnow::PrintVouchers.new(parcel_id).call
    end

    merged_bytes =
      if pdf_contents.size == 1
        pdf_contents.first
      else
        combined = CombinePDF.new
        pdf_contents.each { |bytes| combined << CombinePDF.parse(bytes) }
        combined.to_pdf
      end

    send_data merged_bytes,
      filename: "#{@order.number}.pdf",
      type: 'application/pdf',
      disposition: 'inline'
  rescue ActiveRecord::RecordNotFound
    render json: {
      error: flash_message_for(Spree::Order.new, :not_found)
    }, status: 404
  rescue StandardError => e
    Rails.logger.error "Boxnow Error: #{e.message}"

    render json: {
      error: Spree.t('admin.integrations.boxnow.voucher_print_failed')
    }, status: 400
  end
end

#select_lockerObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/controllers/spree/admin/boxnow_controller.rb', line 95

def select_locker
  load_order
  shipment = @order.shipments.find { |s| s.shipping_method&.boxnow? }

  if shipment.nil? || params[:locker_id].blank?
    render json: { error: 'Invalid request' }, status: :unprocessable_entity and return
  end

  shipment.['boxnow.destination_location_id'] = params[:locker_id]
  shipment.['boxnow.locker_name']             = params[:locker_name]
  shipment.['boxnow.locker_address']          = params[:locker_address]
  shipment.save!

  render json: { success: true }
rescue ActiveRecord::RecordNotFound
  order_not_found
end