Class: SolidusTiny::Order

Inherits:
Object
  • Object
show all
Defined in:
app/models/solidus_tiny/order.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spree_order) ⇒ Order

Returns a new instance of Order.



5
6
7
# File 'app/models/solidus_tiny/order.rb', line 5

def initialize spree_order
  @order = spree_order
end

Instance Attribute Details

#orderObject (readonly)

Returns the value of attribute order.



4
5
6
# File 'app/models/solidus_tiny/order.rb', line 4

def order
  @order
end

Instance Method Details

#build_cupons(cupons) ⇒ Object



135
136
137
138
139
140
141
# File 'app/models/solidus_tiny/order.rb', line 135

def build_cupons cupons
  string_cupons = ""
  if !cupons.empty?
    string_cupons = "Cupons utilizados: #{cupons.pluck(:label).join(", ")}"
  end
  string_cupons
end

#build_customer_infosObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/models/solidus_tiny/order.rb', line 147

def build_customer_infos
  ship_address = @order.ship_address
  {
    email: @order.email,
    celular: ship_address.phone,
    endereco: ship_address.address1,
    endereco_numero: ship_address.number,
    endereco_complemento: ship_address.address2,
    endereco_bairro: ship_address.district,
    endereco_cep: ship_address.zipcode,
    endereco_municipio: ship_address.city,
    endereco_uf: ship_address.state.abbr,
    endereco_pais: "Brasil",
  }
end

#build_observacoesObject

Sobreescreva esse método para customizar as observações internas do pedido no Tiny



164
165
166
# File 'app/models/solidus_tiny/order.rb', line 164

def build_observacoes
  "Pedido realizado no e-commerce #{build_order_url}"
end

#build_order_urlObject



143
144
145
# File 'app/models/solidus_tiny/order.rb', line 143

def build_order_url
  "Link para o pedido: #{@order.store.url}/pedidos/#{@order.number}"
end

#build_tiny_addressObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/solidus_tiny/order.rb', line 79

def build_tiny_address
  ship_address = @order.ship_address
  TinyErpApi::Address.new(
    endereco: ship_address.address1,
    endereco_numero: ship_address.number,
    complemento: ship_address.address2,
    bairro: ship_address.district,
    municipio: ship_address.city,
    cep: ship_address.zipcode,
    uf: ship_address.state.abbr,
    fone: ship_address.phone,
    nome_destinatario: ship_address.name,
    cpf_cnpj: @order.tax_id,
    tipo_pessoa: @order.tax_id.length > 14 ? "J" : "F"
  )
end

#build_tiny_installmentsObject



112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/solidus_tiny/order.rb', line 112

def build_tiny_installments
  parcelas = @order.payments.last.source.try(:installments) || 1
  valor_parcela = @order.total.to_f / parcelas
  1.upto(parcelas).map do |parcela|
    TinyErpApi::Installment.new(
      data: (@order.completed_at + parcela.months).strftime("%Y-%m-%d"),
      valor: valor_parcela,
      id_forma_recebimento: id_forma_recebimento
    )
  end
end

#build_tiny_itemsObject



62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/solidus_tiny/order.rb', line 62

def build_tiny_items
  @order.line_items.map do |item|
    api_product = @client.get_product_by_sku(item.sku).itens
    raise "Produto com SKU #{item.sku} não encontrado no Tiny" if api_product.blank?
    TinyErpApi::Item.new(
      id_produto: api_product[0].id,
      tipo: "P",
      quantidade: item.quantity,
      valor_unitario: item.price
    )
  end
end

#build_tiny_shipmentObject



96
97
98
99
100
101
102
103
104
105
# File 'app/models/solidus_tiny/order.rb', line 96

def build_tiny_shipment
  selected_shipping_method = @account
    .shipping_methods
    .find_by(spree_shipping_method_id: @order.shipments.last.shipping_method.id)
  TinyErpApi::Shipment.new(
    id_transportador: selected_shipping_method.id_transportador,
    id_forma_envio: selected_shipping_method.id_forma_envio,
    id_forma_frete: selected_shipping_method.id_forma_frete
  )
end

#id_forma_recebimentoObject



107
108
109
110
# File 'app/models/solidus_tiny/order.rb', line 107

def id_forma_recebimento
  spree_payment = @order.payments.valid.last
  @account.payment_methods.find_by(spree_payment_method_id: spree_payment.payment_method_id).alias
end

#observacoes_internasObject



124
125
126
127
128
129
130
131
132
133
# File 'app/models/solidus_tiny/order.rb', line 124

def observacoes_internas
  parcelas = @order.payments.last.source.try(:installments) || 1 
  "Pagamento: #{parcelas}x R$ #{(@order.total.to_f / parcelas).round(2)}" \
    "\n" \
    "Forma de envio: #{@order.shipments.last.selected_shipping_rate.name}" \
    "\n" +
    build_cupons(@order.adjustments.where(source_type: "Spree::PromotionAction", eligible: true)).to_s +
    "\n" +
    build_order_url.to_s
end

#seller_external_idObject



75
76
77
# File 'app/models/solidus_tiny/order.rb', line 75

def seller_external_id
  @order.solidus_tiny_seller_id ? @account.sellers.find(@order.solidus_tiny_seller_id).external_id : SolidusTiny::Seller.default.external_id
end

#sendObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/solidus_tiny/order.rb', line 9

def send
  @account = SolidusTiny::Account.first
  @client = SolidusTiny::TinyClient.(@account)      

  contato = upsert_contact
  itens = build_tiny_items
  id_vendedor = seller_external_id
  endereco_entrega = build_tiny_address
  transportador = build_tiny_shipment
  parcelas = build_tiny_installments

  order_response = @client.create_order(
    data: @order.completed_at.strftime("%Y-%m-%d"),
    id_contato: contato.id,
    itens: itens,
    valor_desconto: @order.adjustment_total.abs,
    valor_frete: @order.shipment_total.to_f,
    id_natureza_operacao: @account.operation_id,
    id_vendedor: id_vendedor,
    endereco_entrega: endereco_entrega,
    numero_pedido_ecommerce: @order.number,
    transportador: transportador,
    id_deposito: @account.stock_location,
    parcelas: parcelas,
    observacoes: build_observacoes,
    observacoes_internas: observacoes_internas
  )
  @order.update(tiny_order_id: order_response.id)
end

#upsert_contactObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/solidus_tiny/order.rb', line 39

def upsert_contact
  search_contato = @client.find_contact_by_document(@order.tax_id).itens
  contact_id = if search_contato.any?
    @client.update_contact(
      id: search_contato[0].id,
      nome: @order.ship_address.name,
      tipo_pessoa: @order.tax_id.length > 14 ? "J" : "F",
      cpf_cnpj: @order.tax_id,
      **build_customer_infos
    )
    search_contato[0].id
  else
    response = @client.create_contact(
      nome: @order.ship_address.name,
      tipo_pessoa: @order.tax_id.length > 14 ? "J" : "F",
      cpf_cnpj: @order.tax_id,
      **build_customer_infos
    )
    response.id
  end
  @client.get_contact(contact_id)
end