solidus_tiny 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +53 -0
  3. data/.gem_release.yml +5 -0
  4. data/.github/stale.yml +1 -0
  5. data/.github_changelog_generator +2 -0
  6. data/.gitignore +21 -0
  7. data/.rspec +2 -0
  8. data/.rubocop.yml +5 -0
  9. data/CHANGELOG.md +3 -0
  10. data/Gemfile +48 -0
  11. data/LICENSE +26 -0
  12. data/README.md +73 -0
  13. data/Rakefile +7 -0
  14. data/app/controllers/spree/admin/orders/solidus_tiny/sellers_controller.rb +38 -0
  15. data/app/controllers/spree/admin/solidus_tiny/accounts_controller.rb +82 -0
  16. data/app/controllers/spree/admin/solidus_tiny/orders_controller.rb +20 -0
  17. data/app/jobs/solidus_tiny/application_job.rb +9 -0
  18. data/app/jobs/solidus_tiny/order_job.rb +10 -0
  19. data/app/jobs/solidus_tiny/upsert_sellers_job.rb +9 -0
  20. data/app/models/solidus_tiny/account.rb +8 -0
  21. data/app/models/solidus_tiny/event.rb +4 -0
  22. data/app/models/solidus_tiny/order.rb +129 -0
  23. data/app/models/solidus_tiny/payment_method.rb +4 -0
  24. data/app/models/solidus_tiny/seller.rb +44 -0
  25. data/app/models/solidus_tiny/shipping_method.rb +10 -0
  26. data/app/models/spree/permission_sets/tiny_seller_update.rb +9 -0
  27. data/app/subscribers/solidus_tiny/subscriber.rb +12 -0
  28. data/app/views/spree/admin/orders/solidus_tiny/sellers/edit.html.erb +19 -0
  29. data/app/views/spree/admin/shared/_order_submenu.html.erb +67 -0
  30. data/app/views/spree/admin/shared/_order_summary.html.erb +75 -0
  31. data/app/views/spree/admin/solidus_tiny/accounts/_form.html.erb +110 -0
  32. data/app/views/spree/admin/solidus_tiny/accounts/edit.html.erb +3 -0
  33. data/app/views/spree/admin/solidus_tiny/accounts/index.html.erb +54 -0
  34. data/app/views/spree/admin/solidus_tiny/accounts/new.html.erb +3 -0
  35. data/bin/console +17 -0
  36. data/bin/rails +7 -0
  37. data/bin/rails-engine +13 -0
  38. data/bin/rails-sandbox +16 -0
  39. data/bin/rake +7 -0
  40. data/bin/sandbox +76 -0
  41. data/bin/setup +8 -0
  42. data/config/locales/en.yml +5 -0
  43. data/config/locales/pt-BR.yml +5 -0
  44. data/config/routes.rb +17 -0
  45. data/db/migrate/20240506185512_create_solidus_tiny_accounts.rb +11 -0
  46. data/db/migrate/20240507111938_create_solidus_tiny_sellers.rb +15 -0
  47. data/db/migrate/20240507112822_add_solidus_tiny_seller_id_to_orders.rb +5 -0
  48. data/db/migrate/20240507114823_create_solidus_tiny_shipping_methods.rb +15 -0
  49. data/db/migrate/20240507121424_create_solidus_tiny_payment_methods.rb +13 -0
  50. data/db/migrate/20240507181649_add_tiny_order_id_to_spree_orders.rb +5 -0
  51. data/db/migrate/20240507190528_create_solidus_tiny_events.rb +13 -0
  52. data/lib/generators/solidus_tiny/install/install_generator.rb +31 -0
  53. data/lib/generators/solidus_tiny/install/templates/initializer.rb +4 -0
  54. data/lib/solidus_tiny/configuration.rb +18 -0
  55. data/lib/solidus_tiny/engine.rb +38 -0
  56. data/lib/solidus_tiny/testing_support/factories.rb +4 -0
  57. data/lib/solidus_tiny/version.rb +5 -0
  58. data/lib/solidus_tiny.rb +7 -0
  59. data/solidus_tiny.gemspec +36 -0
  60. metadata +182 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 95bdd85245ceb9a48ae2b693ee328c70d4cdb468c01a037878d55d7c5f05bf86
4
+ data.tar.gz: a1fe9fee1a6b7da3a8630680eda1bf7d91dde4689c195b99228d3c4295be626d
5
+ SHA512:
6
+ metadata.gz: e6806b1e721f7ff3dc66bac5309f9ab07405082a373080f2e2971a63e84ee0f84306f32ca17798cd8a0cfe018f6ca58eea81aae0aad48c510eaf99060916a5c0
7
+ data.tar.gz: 631a622ab9b62c5c1aa04093369f609c755ad06531ce5895a767b0ab515faa59324d64adf4a5af4b974a681417a1be124340eb8ae442b5d70a9dd861c6ee1932
@@ -0,0 +1,53 @@
1
+ version: 2.1
2
+
3
+ orbs:
4
+ # Required for feature specs.
5
+ browser-tools: circleci/browser-tools@1.1
6
+
7
+ # Always take the latest version of the orb, this allows us to
8
+ # run specs against Solidus supported versions only without the need
9
+ # to change this configuration every time a Solidus version is released
10
+ # or goes EOL.
11
+ solidusio_extensions: solidusio/extensions@volatile
12
+
13
+ jobs:
14
+ run-specs-with-sqlite:
15
+ executor: solidusio_extensions/sqlite
16
+ steps:
17
+ - browser-tools/install-chrome
18
+ - solidusio_extensions/run-tests
19
+ run-specs-with-postgres:
20
+ executor: solidusio_extensions/postgres
21
+ steps:
22
+ - browser-tools/install-chrome
23
+ - solidusio_extensions/run-tests
24
+ run-specs-with-mysql:
25
+ executor: solidusio_extensions/mysql
26
+ steps:
27
+ - browser-tools/install-chrome
28
+ - solidusio_extensions/run-tests
29
+ lint-code:
30
+ executor: solidusio_extensions/sqlite-memory
31
+ steps:
32
+ - solidusio_extensions/lint-code
33
+
34
+ workflows:
35
+ "Run specs on supported Solidus versions":
36
+ jobs:
37
+ - run-specs-with-sqlite
38
+ - run-specs-with-postgres
39
+ - run-specs-with-mysql
40
+ - lint-code
41
+
42
+ "Weekly run specs against main":
43
+ triggers:
44
+ - schedule:
45
+ cron: "0 0 * * 4" # every Thursday
46
+ filters:
47
+ branches:
48
+ only:
49
+ - main
50
+ jobs:
51
+ - run-specs-with-sqlite
52
+ - run-specs-with-postgres
53
+ - run-specs-with-mysql
data/.gem_release.yml ADDED
@@ -0,0 +1,5 @@
1
+ bump:
2
+ recurse: false
3
+ file: 'lib/solidus_tiny/version.rb'
4
+ message: Bump SolidusTiny to %{version}
5
+ tag: true
data/.github/stale.yml ADDED
@@ -0,0 +1 @@
1
+ _extends: .github
@@ -0,0 +1,2 @@
1
+ issues=false
2
+ exclude-labels=infrastructure
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ \#*
3
+ *~
4
+ .#*
5
+ .DS_Store
6
+ .idea
7
+ .project
8
+ .sass-cache
9
+ coverage
10
+ Gemfile.lock
11
+ Gemfile-local
12
+ tmp
13
+ nbproject
14
+ pkg
15
+ *.swp
16
+ spec/dummy
17
+ spec/examples.txt
18
+ /sandbox
19
+ .rvmrc
20
+ .ruby-version
21
+ .ruby-gemset
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,5 @@
1
+ require:
2
+ - solidus_dev_support/rubocop
3
+
4
+ AllCops:
5
+ NewCops: disable
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # Changelog
2
+
3
+ See https://github.com/solidusio-contrib/solidus_tiny/releases or OLD_CHANGELOG.md for older versions.
data/Gemfile ADDED
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
5
+
6
+ branch = ENV.fetch('SOLIDUS_BRANCH', 'main')
7
+ gem 'solidus', github: 'solidusio/solidus', branch: branch
8
+
9
+ # The solidus_frontend gem has been pulled out since v3.2
10
+ if branch >= 'v3.2'
11
+ gem 'solidus_frontend'
12
+ elsif branch == 'main'
13
+ gem 'solidus_frontend', github: 'solidusio/solidus_frontend'
14
+ else
15
+ gem 'solidus_frontend', github: 'solidusio/solidus', branch: branch
16
+ end
17
+
18
+ # Needed to help Bundler figure out how to resolve dependencies,
19
+ # otherwise it takes forever to resolve them.
20
+ # See https://github.com/bundler/bundler/issues/6677
21
+ gem 'rails', '>0.a'
22
+
23
+
24
+ # Provides basic authentication functionality for testing parts of your engine
25
+ gem 'solidus_auth_devise'
26
+
27
+ case ENV.fetch('DB', nil)
28
+ when 'mysql'
29
+ gem 'mysql2'
30
+ when 'postgresql'
31
+ gem 'pg'
32
+ else
33
+ gem 'sqlite3'
34
+ end
35
+
36
+ # While we still support Ruby < 3 we need to workaround a limitation in
37
+ # the 'async' gem that relies on the latest ruby, since RubyGems doesn't
38
+ # resolve gems based on the required ruby version.
39
+ gem 'async', '< 3' if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3')
40
+
41
+ gemspec
42
+
43
+ # Use a local Gemfile to include development dependencies that might not be
44
+ # relevant for the project or for other contributors, e.g. pry-byebug.
45
+ #
46
+ # We use `send` instead of calling `eval_gemfile` to work around an issue with
47
+ # how Dependabot parses projects: https://github.com/dependabot/dependabot-core/issues/1658.
48
+ send(:eval_gemfile, 'Gemfile-local') if File.exist? 'Gemfile-local'
data/LICENSE ADDED
@@ -0,0 +1,26 @@
1
+ Copyright (c) 2024 ulysses
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification,
5
+ are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ * Neither the name Solidus nor the names of its contributors may be used to
13
+ endorse or promote products derived from this software without specific
14
+ prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # Solidus Tiny
2
+
3
+ [![CircleCI](https://circleci.com/gh/solidusio-contrib/solidus_tiny.svg?style=shield)](https://circleci.com/gh/solidusio-contrib/solidus_tiny)
4
+ [![codecov](https://codecov.io/gh/solidusio-contrib/solidus_tiny/branch/main/graph/badge.svg)](https://codecov.io/gh/solidusio-contrib/solidus_tiny)
5
+
6
+ <!-- Explain what your extension does. -->
7
+
8
+ ## Installation
9
+
10
+ Add solidus_tiny to your Gemfile:
11
+
12
+ ```ruby
13
+ gem 'solidus_tiny'
14
+ ```
15
+
16
+ Bundle your dependencies and run the installation generator:
17
+
18
+ ```shell
19
+ bin/rails generate solidus_tiny:install
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ <!-- Explain how to use your extension once it's been installed. -->
25
+
26
+ ## Development
27
+
28
+ ### Testing the extension
29
+
30
+ First bundle your dependencies, then run `bin/rake`. `bin/rake` will default to building the dummy
31
+ app if it does not exist, then it will run specs. The dummy app can be regenerated by using
32
+ `bin/rake extension:test_app`.
33
+
34
+ ```shell
35
+ bin/rake
36
+ ```
37
+
38
+ To run [Rubocop](https://github.com/bbatsov/rubocop) static code analysis run
39
+
40
+ ```shell
41
+ bundle exec rubocop
42
+ ```
43
+
44
+ When testing your application's integration with this extension you may use its factories.
45
+ You can load Solidus core factories along with this extension's factories using this statement:
46
+
47
+ ```ruby
48
+ SolidusDevSupport::TestingSupport::Factories.load_for(SolidusTiny::Engine)
49
+ ```
50
+
51
+ ### Running the sandbox
52
+
53
+ To run this extension in a sandboxed Solidus application, you can run `bin/sandbox`. The path for
54
+ the sandbox app is `./sandbox` and `bin/rails` will forward any Rails commands to
55
+ `sandbox/bin/rails`.
56
+
57
+ Here's an example:
58
+
59
+ ```
60
+ $ bin/rails server
61
+ => Booting Puma
62
+ => Rails 6.0.2.1 application starting in development
63
+ * Listening on tcp://127.0.0.1:3000
64
+ Use Ctrl-C to stop
65
+ ```
66
+
67
+ ### Releasing new versions
68
+
69
+ Please refer to the [dedicated page](https://github.com/solidusio/solidus/wiki/How-to-release-extensions) in the Solidus wiki.
70
+
71
+ ## License
72
+
73
+ Copyright (c) 2024 ulysses, released under the New BSD License.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require 'solidus_dev_support/rake_tasks'
5
+ SolidusDevSupport::RakeTasks.install
6
+
7
+ task default: 'extension:specs'
@@ -0,0 +1,38 @@
1
+ module Spree
2
+ module Admin
3
+ module Orders
4
+ module SolidusTiny
5
+ class SellersController < BaseController
6
+ before_action :set_order, only: [:edit, :update]
7
+
8
+ def edit
9
+ @sellers = ::SolidusTiny::Seller.actives.order(:name)
10
+ end
11
+
12
+ def update
13
+ if @order.update(solidus_tiny_seller_id: params[:order][:seller_id])
14
+ flash[:success] = "Vendedor atualizado com sucesso."
15
+ else
16
+ flash[:error] = "Erro ao atualizar vendedor."
17
+ end
18
+ redirect_to admin_orders_solidus_tiny_seller_path(@order)
19
+ end
20
+
21
+ def model_class
22
+ ::SolidusTiny::Seller
23
+ end
24
+
25
+ private
26
+
27
+ def order_params
28
+ params.require(:order).permit(:seller_id)
29
+ end
30
+
31
+ def set_order
32
+ @order = Spree::Order.find_by!(number: params[:order_number])
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,82 @@
1
+ module Spree
2
+ module Admin
3
+ module SolidusTiny
4
+ class AccountsController < BaseController
5
+ before_action :set_account, only: [:show, :edit, :update, :destroy]
6
+
7
+ def index
8
+ @accounts = ::SolidusTiny::Account.all
9
+ end
10
+
11
+ def show
12
+ redirect_to edit_admin_solidus_tiny_account_path(@tiny_account.id)
13
+ end
14
+
15
+ def new
16
+ if ::SolidusTiny::Account.any?
17
+ flash[:error] = "Você só pode ter uma conta Tiny cadastrada"
18
+ redirect_to admin_solidus_tiny_accounts_path
19
+ end
20
+ @tiny_account = ::SolidusTiny::Account.new
21
+ @payment_methods = ::Spree::PaymentMethod.all.pluck(:name, :id)
22
+ @shipping_methods = ::Spree::ShippingMethod.all.pluck(:name, :id)
23
+ @tiny_account.payment_methods.build
24
+ @tiny_account.shipping_methods.build
25
+ @tiny_account.sellers.build
26
+ end
27
+
28
+ def create
29
+ tiny_account = ::SolidusTiny::Account.new account_params
30
+ if tiny_account.save
31
+ flash[:success] = "Conta criada com sucesso"
32
+ redirect_to admin_solidus_tiny_accounts_path
33
+ else
34
+ flash[:error] = tiny_account.errors.full_messages.join("\n")
35
+ redirect_to new_admin_solidus_tiny_account_path
36
+ end
37
+ end
38
+
39
+ def edit
40
+ @payment_methods = ::Spree::PaymentMethod.all.pluck(:name, :id)
41
+ @shipping_methods = ::Spree::ShippingMethod.all.pluck(:name, :id)
42
+ @sellers = @tiny_account.sellers.pluck(:name, :id)
43
+ end
44
+
45
+ def update
46
+ @tiny_account.sellers.find_by(seller_params[:default_seller]).update(default: true)
47
+ @tiny_account.update account_params
48
+ redirect_to edit_admin_solidus_tiny_account_path(@tiny_account.id)
49
+ end
50
+
51
+ def destroy
52
+ @tiny_account.delete
53
+ redirect_to admin_solidus_tiny_accounts_path
54
+ end
55
+
56
+ def upsert_sellers
57
+ ::SolidusTiny::UpsertSellersJob.perform_later
58
+ flash[:success] = "Vendedores atualizados"
59
+ redirect_to admin_solidus_tiny_accounts_path
60
+ end
61
+
62
+ private
63
+
64
+ def set_account
65
+ @tiny_account = ::SolidusTiny::Account.find(params[:id])
66
+ end
67
+
68
+ def account_params
69
+ params.require(:account).permit(
70
+ :api_key, :operation_id, :stock_location,
71
+ payment_methods_attributes: [:id, :spree_payment_method_id, :alias],
72
+ shipping_methods_attributes: [:id, :spree_shipping_method_id, :company, :name, :service]
73
+ )
74
+ end
75
+
76
+ def seller_params
77
+ params.require(:account).permit(default_seller: [:id])
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,20 @@
1
+ module Spree
2
+ module Admin
3
+ module SolidusTiny
4
+ class OrdersController < BaseController
5
+
6
+ def update
7
+ order = Spree::Order.find(params[:id])
8
+ if order.tiny_order_id
9
+ flash[:error] = "Pedido já enviado"
10
+ else
11
+ ::SolidusTiny::OrderJob.perform_later(event: { order: order })
12
+ flash[:success] = "O pedido está sendo enviado"
13
+ end
14
+ redirect_to edit_admin_order_customer_path(order)
15
+ end
16
+
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ module SolidusTiny
2
+ class ApplicationJob < ActiveJob::Base
3
+ # Automatically retry jobs that encountered a deadlock
4
+ # retry_on ActiveRecord::Deadlocked
5
+
6
+ # Most jobs are safe to ignore if the underlying records are no longer available
7
+ # discard_on ActiveJob::DeserializationError
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ module SolidusTiny
2
+ class OrderJob < ApplicationJob
3
+ queue_as :default
4
+
5
+ def perform(event:)
6
+ order = event[:order]
7
+ SolidusTiny::Order.new(order).send
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ module SolidusTiny
2
+ class UpsertSellersJob < ApplicationJob
3
+ queue_as :default
4
+
5
+ def perform
6
+ SolidusTiny::Seller.upsert_sellers SolidusTiny::Account.first
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ module SolidusTiny
2
+ class Account < ApplicationRecord
3
+ has_many :payment_methods, dependent: :destroy
4
+ has_many :shipping_methods, dependent: :destroy
5
+ has_many :sellers, dependent: :destroy
6
+ accepts_nested_attributes_for :payment_methods, :shipping_methods, :sellers
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ module SolidusTiny
2
+ class Event < ApplicationRecord
3
+ end
4
+ end
@@ -0,0 +1,129 @@
1
+ module SolidusTiny
2
+ class Order
3
+ def initialize spree_order, account=SolidusTiny::Account.first
4
+ @order = spree_order
5
+ @account = account
6
+ end
7
+
8
+ def send
9
+ tiny_event = SolidusTiny::Event.new(internal_id: @order.id)
10
+ begin
11
+ TinyErpApi.configure { |config| config.access_token = @account.api_key }
12
+ tiny_shipping_method = transportador
13
+ order_body = {
14
+ data_pedido: @order.completed_at.strftime("%d/%m/%Y"),
15
+ transportador: tiny_shipping_method.company,
16
+ forma_pagamento: forma_pagamento,
17
+ valor_frete: @order.shipment_total,
18
+ valor_desconto: @order.adjustment_total.abs,
19
+ numero_pedido: @order.number,
20
+ observacoes: "Número do pedido loja: #{@order.number}",
21
+ parcelas: parcelas,
22
+ observacoes_internas: observacoes_internas,
23
+ forma_envio: tiny_shipping_method.name,
24
+ forma_frete: tiny_shipping_method.service,
25
+ cliente: customer,
26
+ itens: products,
27
+ id_vendedor: seller_external_id,
28
+ deposito: @account.stock_location,
29
+ id_natureza_operacao: @account.operation_id
30
+ }
31
+ tiny_event.body = order_body
32
+
33
+ tiny_order = TinyErpApi::Order.new(**order_body).create
34
+
35
+ if tiny_order
36
+ @order.update(tiny_order_id: tiny_order.id)
37
+ tiny_event.status = "success"
38
+ tiny_event.message = "Pedido enviado"
39
+ end
40
+ rescue => e
41
+ tiny_event.status = "error"
42
+ tiny_event.message = e.to_json
43
+ ensure
44
+ tiny_event.save!
45
+ end
46
+ end
47
+
48
+ def customer
49
+ ship_address = @order.ship_address
50
+ {
51
+ nome: ship_address.name,
52
+ tipo_pessoa: @order.tax_id.length > 14 ? "J" : "F",
53
+ cpf_cnpj: @order.tax_id,
54
+ endereco: ship_address.address1,
55
+ numero: ship_address.number,
56
+ complemento: ship_address.address2,
57
+ bairro: ship_address.district,
58
+ cep: ship_address.zipcode,
59
+ cidade: ship_address.city,
60
+ uf: ship_address.state.abbr,
61
+ fone: ship_address.phone,
62
+ email: @order.email
63
+ }
64
+ end
65
+
66
+ def products
67
+ @order.line_items.map do |item|
68
+ {
69
+ item: {
70
+ codigo: item.sku,
71
+ quantidade: item.quantity,
72
+ valor_unitario: item.price
73
+ }
74
+ }
75
+ end
76
+ end
77
+
78
+ def seller_external_id
79
+ @order.solidus_tiny_seller_id ? @account.sellers.find(@order.solidus_tiny_seller_id).external_id : SolidusTiny::Seller.default.external_id
80
+ end
81
+
82
+ def transportador
83
+ @account.shipping_methods.find_by(spree_shipping_method_id: @order.shipments.last.shipping_method.id)
84
+ end
85
+
86
+ def forma_pagamento
87
+ spree_payment = @order.payments.valid.last
88
+ payment_method = @account.payment_methods.find_by(spree_payment_method_id: spree_payment.payment_method_id).alias
89
+ end
90
+
91
+ def parcelas
92
+ parcelas = @order.payments.last.source.try(:installments) || 1
93
+ valor_parcela = @order.total.to_f / parcelas
94
+ 1.upto(parcelas).map do |parcela|
95
+ {
96
+ parcela: {
97
+ data: (@order.completed_at + parcela.months).strftime("%d/%m/%Y"),
98
+ valor: valor_parcela,
99
+ forma_pagamento: forma_pagamento
100
+ }
101
+ }
102
+ end
103
+ end
104
+
105
+ def observacoes_internas
106
+ parcelas = @order.payments.last.source.try(:installments) || 1
107
+ "Pagamento: #{parcelas}x R$ #{(@order.total.to_f / parcelas).round(2)}" \
108
+ "\n" \
109
+ "Forma de envio: #{@order.shipments.last.selected_shipping_rate.name}" \
110
+ "\n" +
111
+ build_cupons(@order.adjustments.where(source_type: "Spree::PromotionAction", eligible: true)).to_s +
112
+ "\n" +
113
+ build_order_url.to_s
114
+ end
115
+
116
+ def build_cupons cupons
117
+ string_cupons = ""
118
+ if !cupons.empty?
119
+ string_cupons = "Cupons utilizados: #{cupons.pluck(:label).join(", ")}"
120
+ end
121
+ string_cupons
122
+ end
123
+
124
+ def build_order_url
125
+ "Link para o pedido: #{@order.store.url}/pedidos/#{@order.number}"
126
+ end
127
+
128
+ end
129
+ end
@@ -0,0 +1,4 @@
1
+ module SolidusTiny
2
+ class PaymentMethod < ApplicationRecord
3
+ end
4
+ end
@@ -0,0 +1,44 @@
1
+ module SolidusTiny
2
+ class Seller < ApplicationRecord
3
+ validates :name, presence: true
4
+ validates :external_id, presence: true
5
+
6
+ belongs_to :account
7
+
8
+ after_save :falsify_all_others
9
+
10
+ def self.upsert_sellers account
11
+ TinyErpApi.configure { |config| config.access_token = account.api_key }
12
+ sellers = TinyErpApi::Seller.get_all
13
+ upsert_sellers = sellers.map do |seller|
14
+ situacao = seller[:situacao] == "Ativo" ? true : false
15
+ {
16
+ name: seller[:nome],
17
+ external_id: seller[:id],
18
+ account_id: account.id,
19
+ active: situacao
20
+ }
21
+ end
22
+ SolidusTiny::Seller.upsert_all(upsert_sellers, unique_by: :external_id)
23
+ inactivate_sellers(sellers)
24
+ end
25
+
26
+ def self.default
27
+ find_by(default: true)
28
+ end
29
+
30
+ def self.actives
31
+ where(active: true)
32
+ end
33
+
34
+ def self.inactivate_sellers sellers_tiny
35
+ where.not(external_id: sellers_tiny.pluck(:id)).update_all(active: false)
36
+ end
37
+
38
+ def falsify_all_others
39
+ if default
40
+ self.class.where('id != ?', self.id).update_all(default: false)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,10 @@
1
+ module SolidusTiny
2
+ class ShippingMethod < ApplicationRecord
3
+ validates :service, presence: true
4
+ validates :name, presence: true
5
+ validates :company, presence: true
6
+
7
+ belongs_to :account
8
+ belongs_to :spree_shipping_method, class_name: "Spree::ShippingMethod"
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ module Spree
2
+ module PermissionSets
3
+ class TinySellerUpdate < Spree::PermissionSets::Base
4
+ def activate!
5
+ can :manage, ::SolidusTiny::Seller
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ module SolidusTiny
2
+ class Subscriber
3
+ include Omnes::Subscriber
4
+
5
+ handle :order_finalized,
6
+ with: :send_order_to_Tiny
7
+
8
+ def send_order_to_Tiny event, debug_info = {}
9
+ SolidusTiny::OrderJob.perform_later(event: event.payload)
10
+ end
11
+ end
12
+ end