spree_matkahuolto 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.DS_Store +0 -0
  3. data/.gitignore +22 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE +22 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +70 -0
  8. data/Rakefile +4 -0
  9. data/app/.DS_Store +0 -0
  10. data/app/models/.DS_Store +0 -0
  11. data/app/models/spree/.DS_Store +0 -0
  12. data/app/models/spree/matkahuolto_shipment.rb +9 -0
  13. data/app/models/spree_matkahuolto_custom_shipment.rb +5 -0
  14. data/app/workers/labels_cleanup_worker.rb +8 -0
  15. data/config/environment.rb +0 -0
  16. data/config/routes.rb +3 -0
  17. data/db/migrate/20150415100000_create_table_spree_matkahuolto_shipments.rb +9 -0
  18. data/db/migrate/20150415100500_create_table_spree_matkahuolto_custom_shipments.rb +8 -0
  19. data/db/migrate/20150416100000_add_label_to_spree_matkahuolto_custom_shipments.rb +5 -0
  20. data/db/migrate/20150416100500_remove_has_matkahuolto_from_spree_matkahuolto_custom_shipments.rb +5 -0
  21. data/db/migrate/20150416101000_add_timestamps_to_spree_matkahuolto_custom_shipments.rb +6 -0
  22. data/lib/.DS_Store +0 -0
  23. data/lib/assets/javascripts/shipping_method_matkahuolto.js.coffee +35 -0
  24. data/lib/assets/stylesheets/.DS_Store +0 -0
  25. data/lib/assets/stylesheets/shipping_method_matkahuolto.scss +19 -0
  26. data/lib/controllers/spree/admin/checkout_controller_decorator.rb +34 -0
  27. data/lib/controllers/spree/admin/orders_controller_decorator.rb +74 -0
  28. data/lib/generators/spree_matkahuolto/install/install_generator.rb +18 -0
  29. data/lib/overrides/.DS_Store +0 -0
  30. data/lib/overrides/backend/admin_print_matkahuolto_shipping_labels.rb +4 -0
  31. data/lib/overrides/frontend/display_shipping_method_matkahuolto.rb +4 -0
  32. data/lib/spree_matkahuolto.rb +5 -0
  33. data/lib/spree_matkahuolto/api/office_finder.rb +100 -0
  34. data/lib/spree_matkahuolto/api/shipping_label.rb +209 -0
  35. data/lib/spree_matkahuolto/engine.rb +50 -0
  36. data/lib/spree_matkahuolto/version.rb +3 -0
  37. data/lib/tasks/labels.rake +10 -0
  38. data/lib/views/spree/overrides/.DS_Store +0 -0
  39. data/lib/views/spree/overrides/admin/orders/_print_matkahuolto_shipping_labels.erb +3 -0
  40. data/lib/views/spree/overrides/frontend/_shipping_method_matkahuolto.erb +33 -0
  41. data/spree_matkahuolto.gemspec +31 -0
  42. metadata +196 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b7509ca1f2926755a1ba70ac472c4ed85a155421
4
+ data.tar.gz: 8c3c6165fd9fc6176f00e6ebf6e430c9689f055b
5
+ SHA512:
6
+ metadata.gz: e348cabbe09a6495c4192d3bd48557c291337e2909f8e2c4b941fa7fc85a3506e57f928a149a5e309b1ad2539eb4de48c8a8a5534b8540d6c25f3d0327f44545
7
+ data.tar.gz: 2cb3d044071e520ebeea0f5b954a9dc0cebd201b9e59f42055ebe8fbfa68f9d0da954b0aea620790ea378cf076241da16f6b4d272fab3a29bab02f4d02ca6c59
Binary file
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in spree_matkahuolto.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Agency Leroy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Silvain
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,70 @@
1
+ # SpreeMatkahuolto
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'spree_matkahuolto'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install spree_matkahuolto
18
+
19
+ Then, to copy and execute the migrations, run:
20
+
21
+ $ rails g spree_matkahuolto:install
22
+
23
+ Then, add this requirement in your application.js.coffee
24
+
25
+ #= require shipping_method_matkahuolto
26
+
27
+ And add this requirement to your application.scss
28
+
29
+ *= require shipping_method_matkahuolto
30
+
31
+ ## Usage
32
+
33
+ Give Rails access to your Matkahuolto credentials (username, password and test_mode) as environment variables:
34
+
35
+ ENV["#{Rails.env.upcase}_MATKAHUOLTO_USERNAME"]
36
+
37
+ ENV["#{Rails.env.upcase}_MATKAHUOLTO_PASSWORD"]
38
+
39
+ ENV["#{Rails.env.upcase}_MATKAHUOLTO_TEST_MODE"]
40
+
41
+ In the Spree backend, shipping methods which internal names contains the following will be automatically linked to Matkahuolto backend:
42
+
43
+ matkahuolto_lahella
44
+
45
+ matkahuolto_jako
46
+
47
+ In the Spree checkout process, on the delivery method selection page, Matkahuolto Lahella will display a selection of available pickup places
48
+
49
+ In the Spree backend, any order that used Matkahuolto Lahella or Matkahuolto Jako as a delivery method will now provide a "print labels" link enabling to automatically download the package label PDF generated by Matkahuolto.
50
+
51
+ The Labels are saved to the file system. This rake tasks cleans up all labels older than 3 weeks:
52
+
53
+ rake labels:cleanup
54
+
55
+ You can setup a 'whenever' scheduled job by adding the following lines to your config/schedule.rb
56
+
57
+ every :sunday, :at => '12pm' do
58
+ rake "labels:cleanup"
59
+ end
60
+
61
+ Don't forget to use the 'whenever' capistrano script to deploy your scheduled job.
62
+
63
+
64
+ ## Contributing
65
+
66
+ 1. Fork it ( https://github.com/[my-github-username]/spree_matkahuolto/fork )
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create a new Pull Request
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ import "./lib/tasks/labels.rake"
4
+
Binary file
Binary file
@@ -0,0 +1,9 @@
1
+ module Spree
2
+ class MatkahuoltoShipment < Spree::Base
3
+ self.table_name = 'spree_matkahuolto_shipments'
4
+
5
+ belongs_to :order, class_name: 'Spree::Order'
6
+
7
+
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ class SpreeMatkahuoltoCustomShipment < ActiveRecord::Base
2
+ belongs_to :shipment, class_name: 'Spree::Shipment'
3
+ has_attached_file :label
4
+ validates_attachment_content_type :label, content_type: ['application/pdf']
5
+ end
@@ -0,0 +1,8 @@
1
+ require 'sidekiq'
2
+
3
+ class LabelsCleanupWorker
4
+ include Sidekiq::Worker
5
+ def perform
6
+ SpreeMatkahuoltoCustomShipment.where('updated_at <= ?', 3.weeks.ago).destroy_all
7
+ end
8
+ end
File without changes
@@ -0,0 +1,3 @@
1
+ Spree::Core::Engine.add_routes do
2
+ get 'admin/orders/:id/matkahuolto_labels' => 'admin/orders#matkahuolto_labels', as: :get_matkahuolto_labels_admin_order
3
+ end
@@ -0,0 +1,9 @@
1
+ class CreateTableSpreeMatkahuoltoShipments < ActiveRecord::Migration
2
+ def change
3
+ create_table :spree_matkahuolto_shipments do |t|
4
+ t.references :order, index: true
5
+ t.integer :destination_code
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ class CreateTableSpreeMatkahuoltoCustomShipments < ActiveRecord::Migration
2
+ def change
3
+ create_table :spree_matkahuolto_custom_shipments do |t|
4
+ t.integer :shipment_id
5
+ t.boolean :has_matkahuolto, default: false
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ class AddLabelToSpreeMatkahuoltoCustomShipments < ActiveRecord::Migration
2
+ def change
3
+ add_attachment :spree_matkahuolto_custom_shipments, :label
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class RemoveHasMatkahuoltoFromSpreeMatkahuoltoCustomShipments < ActiveRecord::Migration
2
+ def change
3
+ remove_column :spree_matkahuolto_custom_shipments, :has_matkahuolto, :boolean, default: false
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ class AddTimestampsToSpreeMatkahuoltoCustomShipments < ActiveRecord::Migration
2
+ def change
3
+ add_column :spree_matkahuolto_custom_shipments, :created_at, :datetime
4
+ add_column :spree_matkahuolto_custom_shipments, :updated_at, :datetime
5
+ end
6
+ end
Binary file
@@ -0,0 +1,35 @@
1
+ $(document).ready ->
2
+ list = $('.matkahuolto-destination-list')
3
+ return unless list.length > 0
4
+
5
+ radio = $('input[name="order[shipments_attributes][0][selected_shipping_rate_id]"]')
6
+ shippingMethodId = parseInt(list.data('shipping_method_id'))
7
+
8
+ onToggleChecked = ->
9
+ checked = radio.filter(':checked')
10
+ selectShippingMethodId = parseInt(checked.val())
11
+
12
+ if shippingMethodId == selectShippingMethodId
13
+ list.removeClass('hidden')
14
+ $('#matkahuolto_selected_destination_id').prop("disabled", false)
15
+ unless $('.matkahuolto-destination.active').length > 0
16
+ $('input[name="commit"]').prop("disabled", true)
17
+ else
18
+ list.addClass('hidden')
19
+ $('input[name="commit"]').prop("disabled", false)
20
+ $('#matkahuolto_selected_destination_id').prop("disabled", true)
21
+
22
+ onDestinationClick = ->
23
+ $(@).siblings('li').removeClass('active')
24
+ $(@).addClass("active")
25
+ $('input[name="commit"]').prop("disabled", false)
26
+ destinationId = $(@).data('id')
27
+ $('#matkahuolto_selected_destination_id').val(destinationId)
28
+
29
+ # Destination is selected
30
+ $('.matkahuolto-destination').click onDestinationClick
31
+ onToggleChecked()
32
+ radio.change(onToggleChecked)
33
+
34
+ activePreset = $('.matkahuolto-destination[data-id="'+$('#matkahuolto_selected_destination_id').val()+'"]')
35
+ activePreset.click() if activePreset.length > 0
@@ -0,0 +1,19 @@
1
+ $light-gray: #f1f1f1;
2
+ $black: #000;
3
+
4
+ .matkahuolto-destination-list {
5
+
6
+ &.hidden {
7
+ display: none;
8
+ }
9
+
10
+ .matkahuolto-destination {
11
+ padding: 0.5em 0.5em;
12
+ font-size: 14px;
13
+ &.active, &:hover {
14
+ background-color: $light-gray;
15
+ cursor: pointer;
16
+ color: $black;
17
+ }
18
+ }
19
+ }
@@ -0,0 +1,34 @@
1
+ Spree::CheckoutController.class_eval do
2
+ before_filter :load_matkahuolto_delivery
3
+ before_filter :save_matkahuolto_destination_shipment
4
+
5
+ def load_matkahuolto_delivery
6
+ return unless ["delivery"].include? @order.state
7
+
8
+ api = SpreeMatkahuolto::API::OfficeFinder.new(Rails.configuration.x.matkahuolto_username)
9
+ @matkahuolto_destinations = api.find(@order.ship_address.address1, @order.ship_address.zipcode, @order.ship_address.city)
10
+ @matkahuolto_selected_destination_code = Spree::MatkahuoltoShipment.where(order_id: @order.id).first.try(:destination_code)
11
+ end
12
+
13
+ def save_matkahuolto_destination_shipment
14
+ return unless "update" == params[:action] && "delivery" == params[:state]
15
+
16
+ destination_code = params[:matkahuolto_selected_destination_id]
17
+
18
+ matkahuolto_shipment = Spree::MatkahuoltoShipment.where(order_id: @order.id).first
19
+ # Delete record for order if not using matkahuolto lahella
20
+ if matkahuolto_shipment && !destination_code
21
+ matkahuolto_shipment.destroy
22
+ return
23
+ end
24
+
25
+ unless matkahuolto_shipment
26
+ matkahuolto_shipment = Spree::MatkahuoltoShipment.new
27
+ end
28
+
29
+ matkahuolto_shipment.destination_code = destination_code
30
+ matkahuolto_shipment.order = @order
31
+ matkahuolto_shipment.save
32
+ end
33
+
34
+ end
@@ -0,0 +1,74 @@
1
+ Spree::Admin::OrdersController.class_eval do
2
+
3
+ def matkahuolto_labels
4
+ @order = Spree::Order.find_by(number:params[:id])
5
+ return unless @order
6
+
7
+ label_api = SpreeMatkahuolto::API::ShippingLabel.new(
8
+ Rails.configuration.x.matkahuolto_username,
9
+ Rails.configuration.x.matkahuolto_password,
10
+ Rails.configuration.x.matkahuolto_test_mode,
11
+ # ENV["#{Rails.env.upcase}_MATKAHUOLTO_USERNAME"],
12
+ # ENV["#{Rails.env.upcase}_MATKAHUOLTO_PASSWORD"],
13
+ # ENV["#{Rails.env.upcase}_MATKAHUOLTO_TEST_MODE"]
14
+ )
15
+
16
+ shipments = []
17
+
18
+ @order.shipments.each do |s|
19
+ # TODO: Change API Login
20
+ m_shipment = SpreeMatkahuolto::Shipment.new(Rails.configuration.x.matkahuolto_username)
21
+
22
+ if s.shipping_method.admin_name.include? "matkahuolto_lahella"
23
+ destination_code = Spree::MatkahuoltoShipment.where(order_id: @order.id).first.try(:destination_code)
24
+
25
+ if destination_code
26
+ m_shipment.destination_place_code = destination_code
27
+ m_shipment.product_code = "80"
28
+ end
29
+ end
30
+
31
+ m_shipment.sender_reference = "Order: #{@order.number}"
32
+ m_shipment.receiver_name = "#{s.address.first_name} #{s.address.last_name}"
33
+ m_shipment.receiver_address = "#{s.address.address1} #{s.address.address2}"
34
+ m_shipment.receiver_postal = "#{s.address.zipcode}"
35
+ m_shipment.receiver_city = "#{s.address.city}"
36
+ m_shipment.receiver_contact_name = "#{s.address.first_name} #{s.address.last_name}"
37
+ m_shipment.receiver_contact_number = "#{s.address.phone}"
38
+ m_shipment.receiver_email = "#{@order.email}"
39
+ total_weight = 0.0
40
+ s.line_items.each do |item|
41
+ total_weight += item.try(:product).try(:weight)
42
+ end
43
+ if total_weight == 0.0
44
+ total_weight = 1.0
45
+ end
46
+ m_shipment.weight = total_weight.to_s
47
+
48
+
49
+ custom_s = SpreeMatkahuoltoCustomShipment.find_by(shipment_id:s.id)
50
+ if custom_s and custom_s.label_file_name
51
+ # m_shipment.message_type = "C"
52
+ return send_file custom_s.label.path, type: "application/pdf"
53
+ end
54
+
55
+ shipments.push m_shipment
56
+ end
57
+
58
+ label = label_api.get_labels(shipments)
59
+
60
+ @order.shipments.each do |s|
61
+ custom_s = SpreeMatkahuoltoCustomShipment.find_by(shipment_id:s.id)
62
+ if not custom_s
63
+ custom_s = SpreeMatkahuoltoCustomShipment.create(shipment_id:s.id)
64
+ end
65
+ label_path = label[:path]
66
+ label_file = File.open(label_path, 'r')
67
+ custom_s.label = label_file
68
+ custom_s.save
69
+ end
70
+
71
+ send_file label[:path], filename: label[:filename], type: "application/pdf"
72
+ end
73
+
74
+ end
@@ -0,0 +1,18 @@
1
+ module SpreeMatkahuolto
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ def add_migrations
5
+ run 'bundle exec rake railties:install:migrations FROM=spree_matkahuolto'
6
+ end
7
+
8
+ def run_migrations
9
+ res = ask "Would you like to run the migrations now? [Y/n]"
10
+ if res == "" || res.downcase == "y"
11
+ run 'bundle exec rake db:migrate'
12
+ else
13
+ puts "Skiping rake db:migrate, don't forget to run it!"
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
Binary file
@@ -0,0 +1,4 @@
1
+ Deface::Override.new(:virtual_path => "spree/admin/orders/_shipment",
2
+ :insert_bottom => "tr.show-method.total>td[colspan='4']",
3
+ :partial => "overrides/admin/orders/print_matkahuolto_shipping_labels",
4
+ :name => "admin_print_matkahuolto_shipping_labels")
@@ -0,0 +1,4 @@
1
+ Deface::Override.new(:virtual_path => "spree/checkout/_delivery",
2
+ :replace => "ul.field.radios.shipping-methods",
3
+ :partial => "overrides/frontend/shipping_method_matkahuolto",
4
+ :name => "shipping_method_matkahuolto")
@@ -0,0 +1,5 @@
1
+ require 'spree_core'
2
+ require 'spree_matkahuolto/engine'
3
+ require "spree_matkahuolto/version"
4
+ require 'spree_matkahuolto/api/office_finder'
5
+ require 'spree_matkahuolto/api/shipping_label'
@@ -0,0 +1,100 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
4
+ module SpreeMatkahuolto
5
+ class Office
6
+
7
+ attr_accessor :seq_number, :id, :type, :name, :street_address, :postal_code,
8
+ :city, :country, :distance
9
+
10
+ end
11
+ end
12
+
13
+ module SpreeMatkahuolto
14
+ module API
15
+ class OfficeFinder
16
+
17
+ def initialize(login)
18
+ #@login = login
19
+ # TODO: Only works with this test credentials
20
+ @login = "1234567"
21
+ end
22
+
23
+ def find(street_address='', postal_code='', city='', country='FI')
24
+ fields = default_options.merge({
25
+ 'StreetAddress' => street_address,
26
+ 'PostalCode'=> postal_code,
27
+ 'City' => city,
28
+ 'Country' => country
29
+ })
30
+
31
+ request = build_request(fields)
32
+
33
+ begin
34
+ response = RestClient.post "http://map.matkahuolto.fi/map24mh/searchoffices",
35
+ request, :content_type => 'text/xml', :accept => :xml
36
+
37
+ rescue # if for some reason the request doesn't work, immediately give up
38
+ return []
39
+ else
40
+ response = clean_response(response)
41
+ extract_offices_from_request(response)
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def default_options
48
+ {}
49
+ end
50
+
51
+ # the response is html encoded and with ISO-8859-1 and some other weird
52
+ # encodings nokogiri doesnt like
53
+ def clean_response(response)
54
+ response = URI.unescape(response)
55
+ .force_encoding(Encoding::ISO_8859_1)
56
+ .encode('UTF-8')
57
+ .gsub(/\<\?xml(.*)\?>/,'')
58
+ .gsub(/\+/, ' ')
59
+ end
60
+
61
+ def extract_offices_from_request(response)
62
+ parser = Nori.new
63
+ parsed_data = parser.parse(response)
64
+ if parsed_data['MHSearchOfficesReply'].has_key?("ErrorNbr")
65
+ return []
66
+ end
67
+ offices = []
68
+ return offices unless parsed_data['MHSearchOfficesReply'].has_key?("Office")
69
+ parsed_data['MHSearchOfficesReply']['Office'].each do |office|
70
+ o = Office.new
71
+ o.seq_number = office['SeqNumber']
72
+ o.id = office['Id']
73
+ o.type = office['Type']
74
+ o.name = office['Name']
75
+ o.street_address = office['StreetAddress']
76
+ o.postal_code = office['PostalCode']
77
+ o.city = office['City']
78
+ o.country = office['Country']
79
+ o.distance = office['Distance']
80
+ offices.push o
81
+ end
82
+ offices
83
+ end
84
+
85
+ def build_request(fields)
86
+ data = {
87
+ 'Login' => @login,
88
+ 'Version' => '1.9',
89
+ 'ResponseType' => 'XML',
90
+ 'MaxResults' => '5',
91
+ 'Tuko' => 'Y',
92
+ 'RussianPoint' => 'N'
93
+ }
94
+ data = data.merge(fields)
95
+ Gyoku.xml({ MH_search_offices_request: data }, { :key_converter => :none })
96
+ end
97
+
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,209 @@
1
+ require 'fileutils'
2
+ require 'Gyoku'
3
+ require 'Nori'
4
+ require 'RestClient'
5
+
6
+
7
+ # Tuotetietokentissä käytettävät arvot ovat seuraavat;
8
+ #
9
+ # ProductCode ProductName
10
+ #
11
+ # 10 Bussipaketti
12
+ # 30 Jakopaketti
13
+ # 40 Rahtipussi
14
+ # 42 Rahtipussin jakopaketti
15
+ # 43 Dokumenttikuori
16
+ # 44 Dokumenttikuoren jakopaketti
17
+ # 57 Lavarahti
18
+ # 70 Ulkomaan lentoasiakirja
19
+ # 71 Ulkomaan lentopaketti
20
+ # 72 Ulkomaan paketti
21
+ # 80 Verkkopaketti
22
+ #
23
+ # Erikoiskäsittelykoodit
24
+ #
25
+ # SpecialHandling Kuvaus
26
+ # E01 Vaarallinen aine (VAK-luokka kenttään VAKCode)
27
+ # K02 Ympärysmitta
28
+ # K03 Tankomainen
29
+ # K04 Varoen käsiteltävä
30
+
31
+
32
+ module SpreeMatkahuolto
33
+ class Shipment
34
+
35
+ # Currently only required fields are supported
36
+
37
+ attr_accessor :shipment_type, # Required
38
+ :message_type, # Required
39
+ :shipment_number,
40
+ :shipment_data,
41
+ :weight, # Required
42
+ :volume,
43
+ :packages, # Required
44
+ :sender_id, # Required
45
+ :sender_name,
46
+ :sender_name2,
47
+ :sender_address,
48
+ :sender_postal,
49
+ :sender_city,
50
+ :sender_contact_name,
51
+ :sender_contact_number,
52
+ :sender_email,
53
+ :sender_reference,
54
+ :departure_place_code,
55
+ :departure_place_name,
56
+ :receiver_name, # Required
57
+ :receiver_name2,
58
+ :receiver_address,
59
+ :receiver_postal, # Required
60
+ :receiver_city, # Required
61
+ :receiver_contact_name,
62
+ :receiver_contact_number,
63
+ :receiver_email,
64
+ :receiver_reference,
65
+ :destination_place_code,
66
+ :destination_place_name,
67
+ :payer_code,
68
+ :payer_id,
69
+ :payer_name,
70
+ :payer_name2,
71
+ :payer_address,
72
+ :payer_postal,
73
+ :payer_city,
74
+ :payer_contact_name,
75
+ :receier_contact_number,
76
+ :payer_email,
77
+ :payer_reference,
78
+ :remarks,
79
+ :product_code, # Required
80
+ :product_name,
81
+ :pickup,
82
+ :pickup_payer,
83
+ :pickup_remarks,
84
+ :delivery,
85
+ :delivery_payer,
86
+ :delivery_remarks,
87
+ :cod_sum,
88
+ :cod_currency,
89
+ :cod_account,
90
+ :cod_bic,
91
+ :cod_reference,
92
+ :goods,
93
+ :special_handling,
94
+ :vak_code,
95
+ :vak_description,
96
+ :document_type,
97
+ :shipment_row,
98
+ :package_id,
99
+ :weight,
100
+ :volume
101
+
102
+
103
+ def initialize(sender_id)
104
+ @sender_id = sender_id
105
+ @product_code = "30"
106
+ @shipment_type = "N"
107
+ @message_type = "N"
108
+ @packages = 1
109
+ @weight = 1
110
+ @receiver_name = ""
111
+ @receiver_address = ""
112
+ @receiver_city = ""
113
+ @receiver_email =""
114
+ @receiver_postal = ""
115
+ @receiver_contact_name = ""
116
+ @receiver_contact_number = ""
117
+ @destination_place_code = ""
118
+ end
119
+
120
+ end
121
+ end
122
+
123
+ module SpreeMatkahuolto
124
+ module API
125
+
126
+ class ShippingLabel
127
+
128
+ attr_accessor :test_mode
129
+
130
+ TEST_ENDPOINT = "https://extservicestest.matkahuolto.fi/mpaketti/mhshipmentxml"
131
+ PRODUCTION_ENDPOINT = "https://extservices.matkahuolto.fi/mpaketti/mhshipmentxml"
132
+
133
+ def initialize(login, password, test_mode = false)
134
+ @login = login
135
+ @password = password
136
+ @test_mode = test_mode
137
+ end
138
+
139
+ def get_labels(shipments)
140
+ return nil unless shipments
141
+ #shipments = [shipments] unless shipments.is_a?(Array)
142
+
143
+ if @test_mode
144
+ endpoint = TEST_ENDPOINT
145
+ else
146
+ endpoint = PRODUCTION_ENDPOINT
147
+ end
148
+
149
+ request = build_request(shipments)
150
+ response = RestClient.post endpoint, request, :content_type => 'text/xml', :accept => :xml
151
+
152
+ parser = Nori.new
153
+ parsed_data = parser.parse(response)
154
+
155
+ filename = parsed_data['MHShipmentReply']['PdfName']
156
+ path = Rails.root.join("tmp/shipping_labels/#{filename}")
157
+ dirname = File.dirname(path)
158
+
159
+ FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
160
+
161
+ File.open(path, 'wb') do |f|
162
+ f.write(Base64.decode64(parsed_data['MHShipmentReply']['ShipmentPdf']))
163
+ end
164
+
165
+ return { filename: "#{filename}", path: "#{path}" }
166
+ end
167
+
168
+ private
169
+
170
+ def build_request(shipments)
171
+
172
+ request = {
173
+ 'MHShipmentRequest' => {
174
+ 'UserId' => @login,
175
+ 'Password' => @password,
176
+ 'Version' => '2.0',
177
+ 'Shipment' => []
178
+ }
179
+ }
180
+
181
+ shipments.each do |shipment|
182
+ s = {
183
+ 'ShipmentType' => shipment.shipment_type,
184
+ 'MessageType' => shipment.message_type,
185
+ 'Weight' => shipment.weight,
186
+ 'Packages' => shipment.packages,
187
+ 'SenderId' => shipment.sender_id,
188
+ 'SenderReference' => shipment.sender_reference,
189
+ 'ReceiverName1' => shipment.receiver_name,
190
+ 'ReceiverAddress' => shipment.receiver_address,
191
+ 'ReceiverPostal' => shipment.receiver_postal,
192
+ 'ReceiverCity' => shipment.receiver_city,
193
+ 'ReceiverEmail' => shipment.receiver_email,
194
+ 'ReceiverContactName' => shipment.receiver_contact_name,
195
+ 'ReceiverContactNumber' => shipment.receiver_contact_number,
196
+ 'DestinationPlaceCode' => shipment.destination_place_code,
197
+ 'ProductCode' => shipment.product_code
198
+ }
199
+
200
+ request['MHShipmentRequest']['Shipment'].push s
201
+ end
202
+
203
+ Gyoku.xml(request, { :key_converter => :none })
204
+ end
205
+
206
+ end
207
+
208
+ end
209
+ end
@@ -0,0 +1,50 @@
1
+ module SpreeMatkaHuolto
2
+ class Engine < Rails::Engine
3
+ engine_name 'spree_matkahuolto'
4
+
5
+ config.autoload_paths += %W(#{config.root}/lib)
6
+
7
+ initializer "spree.matkahuolto.preferences", :after => "spree.register.payment_methods" do |app|
8
+ app.config.x.matkahuolto_username = ENV["#{Rails.env.upcase}_MATKAHUOLTO_USERNAME"]
9
+ app.config.x.matkahuolto_password = ENV["#{Rails.env.upcase}_MATKAHUOLTO_PASSWORD"]
10
+ app.config.x.matkahuolto_test_mode = ENV["#{Rails.env.upcase}_MATKAHUOLTO_TEST_MODE"]
11
+ # app.config.spree.payment_methods << Spree::Gateway::Worldpay
12
+ end
13
+
14
+ def self.activate
15
+ if SpreeMatkaHuolto::Engine.frontend_available?
16
+ Rails.application.config.assets.precompile += [
17
+ 'lib/assets/javascripts/shipping_method_matkahuolto.js.coffee',
18
+ 'lib/assets/stylesheets/shipping_method_matkahuolto.scss',
19
+ ]
20
+ Dir.glob(File.join(File.dirname(__FILE__), "../controllers/spree/admin/*_decorator*.rb")) do |c|
21
+ Rails.configuration.cache_classes ? require(c) : load(c)
22
+ end
23
+ end
24
+ end
25
+
26
+ def self.backend_available?
27
+ @@backend_available ||= ::Rails::Engine.subclasses.map(&:instance).map{ |e| e.class.to_s }.include?('Spree::Backend::Engine')
28
+ end
29
+
30
+ def self.frontend_available?
31
+ @@frontend_available ||= ::Rails::Engine.subclasses.map(&:instance).map{ |e| e.class.to_s }.include?('Spree::Frontend::Engine')
32
+ end
33
+
34
+ paths["app/overrides"] ||= []
35
+
36
+ if self.backend_available?
37
+ paths["app/controllers"] << "lib/controllers/spree/admin"
38
+ paths["app/overrides"] << "lib/overrides/backend"
39
+ paths["app/models"] << "lib/models/decorators"
40
+ paths["app/views"] << "lib/views/spree"
41
+ end
42
+
43
+ if self.frontend_available?
44
+ paths["app/overrides"] << "lib/overrides/frontend"
45
+ paths["app/views"] << "lib/views/spree"
46
+ end
47
+
48
+ config.to_prepare &method(:activate).to_proc
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module SpreeMatkahuolto
2
+ VERSION = "1.0.8"
3
+ end
@@ -0,0 +1,10 @@
1
+ require File.expand_path '../../../app/workers/labels_cleanup_worker.rb', __FILE__
2
+
3
+ begin
4
+ namespace :labels do
5
+ task :cleanup do
6
+ LabelsCleanupWorker.perform_async()
7
+ puts "Cleaned up old labels"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ <% if shipment.shipping_method and ( shipment.shipping_method.admin_name.include?'matkahuolto_lahella' or shipment.shipping_method.admin_name.include?'matkahuolto_jako' ) %>
2
+ <%= link_to 'Print Labels', get_matkahuolto_labels_admin_order_path(@order), target: '_blank' %>
3
+ <% end %>
@@ -0,0 +1,33 @@
1
+ <ul class="field radios shipping-methods unstyled">
2
+ <% ship_form.object.shipping_rates.includes(:shipping_method).reorder('spree_shipping_methods.admin_name ASC').each do |rate| %>
3
+ <li class="small-12 large-offset-1 large-11 columns shipping-method">
4
+
5
+ <%= ship_form.radio_button :selected_shipping_rate_id, rate.id, :checked => false %>
6
+ <%= ship_form.label "selected_shipping_rate_id_#{rate.id}", "<span class='rate-name'>#{rate.name}</span>
7
+ <span class='rate-coss'>#{shipping_rate_display_price(rate)}</span>".html_safe, :class => "shipping-method-radio input-helper input-helper--radio" %>
8
+
9
+ <% if rate.shipping_method.admin_name.include? "matkahuolto_lahella" %>
10
+ <ul class="matkahuolto-destination-list unstyled" id="matkahuolto-destination-list" data-shipping_method_id="<%= rate.id %>">
11
+ <li><input type="hidden" id="matkahuolto_selected_destination_id" name="matkahuolto_selected_destination_id" value="<%= @matkahuolto_selected_destination_code %>"></li>
12
+ <% @matkahuolto_destinations.each do |destination| %>
13
+
14
+ <% @active_class = '' %>
15
+
16
+ <% if @matkahuolto_selected_destination_code == destination.id.to_i %>
17
+ <% @active_class = 'class="active"'.html_safe %>
18
+ <% end %>
19
+ <% destination.distance = "0" unless destination.distance %>
20
+ <li class="matkahuolto-destination" data-id="<%= destination.id %>" <%= @active_class %> >
21
+ <%= destination.name + ", " + destination.street_address + ", " + destination.postal_code + " " + destination.city + ". " + Spree.t(:distance) + ": " + destination.distance + " km" %>
22
+ </li>
23
+ <% end %>
24
+
25
+ <% if @matkahuolto_destinations.length < 1 %>
26
+ <li class="no-destinations"><%= Spree.t(:matkahuolto_destination_not_found) %></li>
27
+ <% end %>
28
+
29
+ </ul>
30
+ <% end %>
31
+ </li>
32
+ <% end %>
33
+ </ul>
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'spree_matkahuolto/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "spree_matkahuolto"
8
+ spec.version = SpreeMatkahuolto::VERSION
9
+ spec.authors = ["Silvain"]
10
+ spec.email = ["silvain@agencyleroy.com"]
11
+ spec.summary = "Spree extension for integration with the delivery service Matkahuolto"
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/agencyleroy/spree_matkahuolto"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_dependency "gyoku", "~> 1.0"
25
+ spec.add_dependency "nori"
26
+ spec.add_dependency "whenever"
27
+ spec.add_dependency "sidekiq"
28
+ spec.add_dependency "rest-client"
29
+ spec.add_dependency "htmlentities"
30
+
31
+ end
metadata ADDED
@@ -0,0 +1,196 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spree_matkahuolto
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.8
5
+ platform: ruby
6
+ authors:
7
+ - Silvain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: gyoku
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nori
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: whenever
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sidekiq
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rest-client
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: htmlentities
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Spree extension for integration with the delivery service Matkahuolto
126
+ email:
127
+ - silvain@agencyleroy.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".DS_Store"
133
+ - ".gitignore"
134
+ - Gemfile
135
+ - LICENSE
136
+ - LICENSE.txt
137
+ - README.md
138
+ - Rakefile
139
+ - app/.DS_Store
140
+ - app/models/.DS_Store
141
+ - app/models/spree/.DS_Store
142
+ - app/models/spree/matkahuolto_shipment.rb
143
+ - app/models/spree_matkahuolto_custom_shipment.rb
144
+ - app/workers/labels_cleanup_worker.rb
145
+ - config/environment.rb
146
+ - config/routes.rb
147
+ - db/migrate/20150415100000_create_table_spree_matkahuolto_shipments.rb
148
+ - db/migrate/20150415100500_create_table_spree_matkahuolto_custom_shipments.rb
149
+ - db/migrate/20150416100000_add_label_to_spree_matkahuolto_custom_shipments.rb
150
+ - db/migrate/20150416100500_remove_has_matkahuolto_from_spree_matkahuolto_custom_shipments.rb
151
+ - db/migrate/20150416101000_add_timestamps_to_spree_matkahuolto_custom_shipments.rb
152
+ - lib/.DS_Store
153
+ - lib/assets/javascripts/shipping_method_matkahuolto.js.coffee
154
+ - lib/assets/stylesheets/.DS_Store
155
+ - lib/assets/stylesheets/shipping_method_matkahuolto.scss
156
+ - lib/controllers/spree/admin/checkout_controller_decorator.rb
157
+ - lib/controllers/spree/admin/orders_controller_decorator.rb
158
+ - lib/generators/spree_matkahuolto/install/install_generator.rb
159
+ - lib/overrides/.DS_Store
160
+ - lib/overrides/backend/admin_print_matkahuolto_shipping_labels.rb
161
+ - lib/overrides/frontend/display_shipping_method_matkahuolto.rb
162
+ - lib/spree_matkahuolto.rb
163
+ - lib/spree_matkahuolto/api/office_finder.rb
164
+ - lib/spree_matkahuolto/api/shipping_label.rb
165
+ - lib/spree_matkahuolto/engine.rb
166
+ - lib/spree_matkahuolto/version.rb
167
+ - lib/tasks/labels.rake
168
+ - lib/views/spree/overrides/.DS_Store
169
+ - lib/views/spree/overrides/admin/orders/_print_matkahuolto_shipping_labels.erb
170
+ - lib/views/spree/overrides/frontend/_shipping_method_matkahuolto.erb
171
+ - spree_matkahuolto.gemspec
172
+ homepage: https://github.com/agencyleroy/spree_matkahuolto
173
+ licenses:
174
+ - MIT
175
+ metadata: {}
176
+ post_install_message:
177
+ rdoc_options: []
178
+ require_paths:
179
+ - lib
180
+ required_ruby_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ required_rubygems_version: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - ">="
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ requirements: []
191
+ rubyforge_project:
192
+ rubygems_version: 2.2.2
193
+ signing_key:
194
+ specification_version: 4
195
+ summary: Spree extension for integration with the delivery service Matkahuolto
196
+ test_files: []