Class: SolidusTiny::Order
- Inherits:
-
Object
- Object
- SolidusTiny::Order
- Defined in:
- app/models/solidus_tiny/order.rb
Instance Attribute Summary collapse
-
#order ⇒ Object
readonly
Returns the value of attribute order.
Instance Method Summary collapse
- #build_cupons(cupons) ⇒ Object
- #build_customer_infos ⇒ Object
-
#build_observacoes ⇒ Object
Sobreescreva esse método para customizar as observações internas do pedido no Tiny.
- #build_order_url ⇒ Object
- #build_tiny_address ⇒ Object
- #build_tiny_installments ⇒ Object
- #build_tiny_items ⇒ Object
- #build_tiny_shipment ⇒ Object
- #id_forma_recebimento ⇒ Object
-
#initialize(spree_order) ⇒ Order
constructor
A new instance of Order.
- #observacoes_internas ⇒ Object
- #seller_external_id ⇒ Object
- #send ⇒ Object
- #upsert_contact ⇒ Object
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
#order ⇒ Object (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
136 137 138 139 140 141 142 |
# File 'app/models/solidus_tiny/order.rb', line 136 def build_cupons cupons string_cupons = "" if !cupons.empty? string_cupons = "Cupons utilizados: #{cupons.pluck(:label).join(", ")}" end string_cupons end |
#build_customer_infos ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'app/models/solidus_tiny/order.rb', line 148 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_observacoes ⇒ Object
Sobreescreva esse método para customizar as observações internas do pedido no Tiny
165 166 167 |
# File 'app/models/solidus_tiny/order.rb', line 165 def build_observacoes "Pedido realizado no e-commerce #{build_order_url}" end |
#build_order_url ⇒ Object
144 145 146 |
# File 'app/models/solidus_tiny/order.rb', line 144 def build_order_url "Link para o pedido: #{@order.store.url}/pedidos/#{@order.number}" end |
#build_tiny_address ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'app/models/solidus_tiny/order.rb', line 80 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_installments ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/solidus_tiny/order.rb', line 113 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_items ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/solidus_tiny/order.rb', line 63 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_shipment ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'app/models/solidus_tiny/order.rb', line 97 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_recebimento ⇒ Object
108 109 110 111 |
# File 'app/models/solidus_tiny/order.rb', line 108 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_internas ⇒ Object
125 126 127 128 129 130 131 132 133 134 |
# File 'app/models/solidus_tiny/order.rb', line 125 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_id ⇒ Object
76 77 78 |
# File 'app/models/solidus_tiny/order.rb', line 76 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 |
#send ⇒ Object
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 38 |
# File 'app/models/solidus_tiny/order.rb', line 9 def send @account = SolidusTiny::Account.first @client = SolidusTiny::TinyClient.for_account(@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) @client.(order_response.id, SolidusTiny.config.) if SolidusTiny.config..any? end |
#upsert_contact ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/models/solidus_tiny/order.rb', line 40 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 |