Class: ErpIntegration::Fulfil::Resources::SalesOrder

Inherits:
ApiResource
  • Object
show all
Defined in:
lib/erp_integration/fulfil/resources/sales_order.rb

Defined Under Namespace

Classes: MissingIdempotencyKey

Constant Summary

Constants included from PaginationMethods

PaginationMethods::DEFAULT_LIMIT, PaginationMethods::DEFAULT_OFFSET, PaginationMethods::MAX_LIMIT

Instance Attribute Summary

Attributes inherited from ApiResource

#resource_klass

Attributes included from QueryMethods

#or_clauses, #selected_fields, #where_clauses

Attributes included from PaginationMethods

#limit_value, #offset_value, #page_number

Instance Method Summary collapse

Methods inherited from ApiResource

#all, client, config, #count, #each, #find_each, #initialize, model_name, model_name=

Methods included from QueryMethods

#or, #or!, #select, #select!, #where, #where!, #where_domain, #where_ilike, #where_in, #where_less_or_equal_to, #where_less_than, #where_like, #where_more_or_equal_to, #where_more_than, #where_not, #where_not_in

Methods included from Persistence

#create, #destroy, #update

Methods included from PaginationMethods

#limit, #limit!, #offset, #offset!, #page, #page!

Methods included from FinderMethods

#find, #find_by, #find_by!

Methods included from Context

#context?, #with_context

Constructor Details

This class inherits a constructor from ErpIntegration::Fulfil::ApiResource

Instance Method Details

#cancel(id) ⇒ boolean

Allows cancelling the entire sales order in Fulfil.

Parameters:

  • id (Integer|String)

    The ID of the to be cancelled order.

Returns:

  • (boolean)

    Whether the sales order was cancelled successfully or not.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/erp_integration/fulfil/resources/sales_order.rb', line 24

def cancel(id)
  client.put("model/sale.sale/#{id}/cancel")
  true

# Fulfil will return an 400 (a.k.a. "Bad Request") status code when a sales order couldn't
# be cancelled. If a sales order isn't cancellable by design, no exception should be raised.
#
# See the Fulfil's documentation for more information:
# https://developers.fulfil.io/rest_api/model/sale.sale/#cancel-a-sales-order
rescue ErpIntegration::HttpError::BadRequest
  false
# Workaround: Fulfil api does not return a json when status code is 200 (a.k.a. "Ok")
# and faraday is having an error when trying to parse it. Let's skip the parse error
# and move on.
rescue Faraday::ParsingError
  true
end

#confirm(id) ⇒ boolean

Confirm the order on Fulfil.

Parameters:

  • id (Integer|String)

    The ID of the to be confirmed order.

Returns:

  • (boolean)

    Whether the sales order was confirmed successfully or not.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/erp_integration/fulfil/resources/sales_order.rb', line 45

def confirm(id)
  client.put("model/sale.sale/#{id}/confirm")
  true

# Fulfil will return an 400 (a.k.a. "Bad Request") status code when a sales order couldn't
# be confirmed.
rescue ErpIntegration::HttpError::BadRequest
  false
# Workaround: Fulfil api does not return a json when status code is 200 (a.k.a. "Ok")
# and faraday is having an error when trying to parse it. Let's skip the parse error
# and move on.
rescue Faraday::ParsingError
  true
end

#create_for(sales_channel, attributes:) ⇒ ErpIntegration::SalesOrder

Creates the sales order in one API call in Fulfil.

Fulfil allows two ways of creating a new sales order. Through their regular/generic API endpoint just like any other resource or through the sales channel.

The benefit of using the Sales Channel route is it allows creating the sales

The sales order number is used as an idempotency key for Fulfil to prevent sales orders from being processed twice. Due to that it’s a required attribute.

Parameters:

  • sales_channel (String, Integer)

    The ID of the sales channel in Fulfil.

  • attributes (Hash)

    The attributes of the sales order

Returns:

Raises:

See Also:



78
79
80
81
82
83
84
# File 'lib/erp_integration/fulfil/resources/sales_order.rb', line 78

def create_for(sales_channel, attributes:)
  raise MissingIdempotencyKey if attributes['reference'].blank? && attributes[:reference].blank?

  ErpIntegration::SalesOrder.new(
    client.put("model/sale.channel/#{sales_channel}/create_order", [attributes])
  )
end

#duplicate(id) ⇒ Array|boolean

Allows duplicating the entire sales order in Fulfil.

Parameters:

  • id (Integer|String)

    The ID of the to be duplicated order.

Returns:

  • (Array|boolean)

    Whether the sales order was duplicated successfully or not.



89
90
91
92
93
94
95
96
97
# File 'lib/erp_integration/fulfil/resources/sales_order.rb', line 89

def duplicate(id)
  duplicated_order_id = client.put("model/sale.sale/#{id}/copy").first
  ErpIntegration::SalesOrder.new(id: duplicated_order_id)

# Fulfil will return an 400 (a.k.a. "Bad Request") status code when a sales order couldn't
# be duplicated.
rescue ErpIntegration::HttpError::BadRequest
  false
end

#process(id) ⇒ boolean

Process the order on Fulfil.

Parameters:

  • id (Integer|String)

    The ID of the to be processed order.

Returns:

  • (boolean)

    Whether the sales order was processed successfully or not.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/erp_integration/fulfil/resources/sales_order.rb', line 102

def process(id)
  client.put("model/sale.sale/#{id}/process")
  true

# Fulfil will return an 400 (a.k.a. "Bad Request") status code when a sales order couldn't
# be processed.
rescue ErpIntegration::HttpError::BadRequest
  false
# Workaround: Fulfil api does not return a json when status code is 200 (a.k.a. "Ok")
# and faraday is having an error when trying to parse it. Let's skip the parse error
# and move on.
rescue Faraday::ParsingError
  true
end

#return!(id, options) ⇒ Object

Creates a return order in Fulfil.

Parameters:

  • id (Integer, String)

    The ID of the sales order

  • options (Hash)

    The attributes needed for the return.



121
122
123
# File 'lib/erp_integration/fulfil/resources/sales_order.rb', line 121

def return!(id, options)
  client.put("model/sale.sale/#{id}/return_order", options)
end