Class: Gemgento::Adapter::Shopify::Order
- Inherits:
-
Object
- Object
- Gemgento::Adapter::Shopify::Order
- Defined in:
- app/models/gemgento/adapter/shopify/order.rb
Class Method Summary collapse
-
.export ⇒ Void
Export all shopify orders to a CSV.
-
.order_export_headers ⇒ Array(String)
Generate CSV headers for an order export.
-
.order_row(order) ⇒ Array(String)
Create a row for the given order.
-
.order_status(order) ⇒ String
Determine the Magento order status from a Shopify order.
-
.paid_amount(order) ⇒ Float
Calculate the amount paid for an order.
-
.products_ordered(line_items, store) ⇒ Object
Generate a string of line item details for the order.
-
.quantity_ordered(order) ⇒ Float
Calculate total quantity of ordered items.
-
.refund_amount(order) ⇒ Float
Calculate the total amount refunded.
-
.shipping_amount(order) ⇒ Float
Calculate total shipping cost for shopify order.
-
.store(order) ⇒ Gemgento::Store
Retrieve the store related to the order.
-
.street(address) ⇒ String
Get a formatted street attribute from an address.
-
.totals(order) ⇒ Hash(Double)
Calculate the various totals for an order.
-
.user_group(user) ⇒ String
Get the user group from the provided user.
Class Method Details
.export ⇒ Void
Export all shopify orders to a CSV.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'app/models/gemgento/adapter/shopify/order.rb', line 10 def self.export ShopifyAPI::Base.site = Gemgento::Adapter::ShopifyAdapter.api_url page = 1 shopify_orders = ShopifyAPI::Order.where(limit: 250, page: page) CSV.open('orders.csv', 'wb') do |csv| csv << order_export_headers while shopify_orders.any? shopify_orders.each do |shopify_order| csv << order_row(shopify_order) end page = page + 1 shopify_orders = ShopifyAPI::Order.where(limit: 250, page: page) end end end |
.order_export_headers ⇒ Array(String)
Generate CSV headers for an order export.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/models/gemgento/adapter/shopify/order.rb', line 32 def self.order_export_headers %w[ order_id website email password group_id store_id payment_method shipping_method billing_prefix billing_firstname billing_middlename billing_lastname billing_suffix billing_street_full billing_city billing_region billing_country billing_postcode billing_telephone billing_company billing_fax shipping_prefix shipping_firstname shipping_middlename shipping_lastname shipping_suffix shipping_street_full shipping_city shipping_region shipping_country shipping_postcode shipping_telephone shipping_company shipping_fax created_in is_subscribed customer_id created_at updated_at tax_amount shipping_amount discount_amount subtotal grand_total total_paid total_refunded total_qty_ordered total_canceled total_invoiced total_online_refunded total_offline_refunded base_tax_amount base_shipping_amount base_discount_amount base_subtotal base_grand_total base_total_paid base_total_refunded base_total_qty_ordered base_total_canceled base_total_invoiced base_total_online_refunded base_total_offline_refunded subtotal_refunded subtotal_canceled discount_refunded discount_invoiced tax_refunded tax_canceled shipping_refunded shipping_canceled base_subtotal_refunded base_subtotal_canceled base_discount_refunded base_discount_canceled base_discount_invoiced base_tax_refunded base_tax_canceled base_shipping_refunded base_shipping_canceled subtotal_invoiced tax_invoiced shipping_invoiced base_subtotal_invoiced base_tax_invoiced base_shipping_invoiced shipping_tax_amount base_shipping_tax_amount shipping_tax_refunded base_shipping_tax_refunded products_ordered order_status ] end |
.order_row(order) ⇒ Array(String)
Create a row for the given order.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'app/models/gemgento/adapter/shopify/order.rb', line 59 def self.order_row(order) user = Gemgento::User.find_by(email: order.email) store = store(order) begin shipping_address = order.shipping_address rescue shipping_address = nil end begin billing_address = order.billing_address rescue billing_address = nil end totals = totals(order) line_items = order.line_items [ order.order_number, # order_id store.code, # website order.email, # email '', # password user_group(user), # group_id store.magento_id, # store_id 'payment_cryozonic_stripe', # payment_method 'flatrate_flatrate', # shipping_method '', # billing_prefix (billing_address ? billing_address.first_name : ''), # billing_firstname '', # billing_middlename (billing_address ? billing_address.last_name : ''), # billing_lastname '', # billing_suffix (billing_address ? street(billing_address) : ''), # billing_street_full (billing_address ? billing_address.city : ''), # billing_city (billing_address ? billing_address.province : ''), # billing_region (billing_address ? billing_address.country : ''), # billing_country (billing_address ? billing_address.zip : ''), # billing_postcode (billing_address ? billing_address.phone : ''), # billing_telephone (billing_address ? billing_address.company : ''), # billing_company '', # billing_fax '', # shipping_prefix (shipping_address ? shipping_address.first_name : ''), # shipping_firstname '', # shipping_middlename (shipping_address ? shipping_address.last_name : ''), # shipping_lastname '', # shipping_suffix (shipping_address ? street(shipping_address) : ''), # shipping_street_full (shipping_address ? shipping_address.city : ''), # shipping_city (shipping_address ? shipping_address.province : ''), # shipping_region (shipping_address ? shipping_address.country : ''), # shipping_country (shipping_address ? shipping_address.zip : ''), # shipping_postcode (shipping_address ? shipping_address.phone : ''), # shipping_telephone (shipping_address ? shipping_address.company : ''), # shipping_company '', # shipping_fax store.name, # created_in (order.buyer_accepts_marketing ? 1 : 0), # is_subscribed (user.nil? ? nil : user.magento_id), # customer_id order.created_at, # created_at order.updated_at, # updated_at totals[:tax], # tax_amount totals[:shipping], # shipping_amount totals[:discount], # discount_amount totals[:subtotal], # subtotal totals[:grand], # grand_total totals[:paid], # total_paid totals[:refunded], # total_refunded quantity_ordered(order), # total_qty_ordered totals[:canceled], # total_canceled totals[:grand], # total_invoiced totals[:refunded], # total_online_refunded 0, # total_offline_refunded totals[:tax], # base_tax_amount totals[:shipping], # base_shipping_amount totals[:discount], # base_discount_amount totals[:subtotal], # base_subtotal totals[:grand], # base_grand_total totals[:paid], # base_total_paid totals[:refunded], # base_total_refunded quantity_ordered(order), # base_total_qty_ordered totals[:canceled], # base_total_canceled totals[:grand], # base_total_invoiced totals[:refunded], # base_total_online_refunded 0, # base_total_offline_refunded totals[:refunded], # subtotal_refunded totals[:canceled], # subtotal_canceled 0, # discount_refunded 0, # discount_invoiced 0, # tax_refunded 0, # tax_canceled 0, # shipping_refunded 0, # shipping_canceled totals[:refunded], # base_subtotal_refunded totals[:canceled], # base_subtotal_canceled 0, # base_discount_refunded 0, # base_discount_canceled 0, # base_discount_invoiced 0, # base_tax_refunded 0, # base_tax_canceled 0, # base_shipping_refunded 0, # base_shipping_canceled totals[:subtotal], # subtotal_invoiced totals[:tax], # tax_invoiced totals[:shipping], # shipping_invoiced totals[:subtotal], # base_subtotal_invoiced totals[:tax], # base_tax_invoiced totals[:shipping], # base_shipping_invoiced 0, # shipping_tax_amount 0, # base_shipping_tax_amount 0, # shipping_tax_refunded 0, # base_shipping_tax_refunded products_ordered(line_items, store), # products_ordered order_status(order) # order_status ] end |
.order_status(order) ⇒ String
Determine the Magento order status from a Shopify order.
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'app/models/gemgento/adapter/shopify/order.rb', line 315 def self.order_status(order) if %w[paid refunded partially_refunded].include? order.financial_status if order.fulfillment_status == 'shipped' return 'complete' elsif %w[refunded partially_refunded].include? order.financial_status return 'complete' else return 'pending' end elsif order.financial_status == 'pending' return 'pending' elsif order.financial_status == 'voided' return 'canceled' else return 'complete' end end |
.paid_amount(order) ⇒ Float
Calculate the amount paid for an order.
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'app/models/gemgento/adapter/shopify/order.rb', line 227 def self.paid_amount(order) return order.total_price if order.financial_status == 'paid' begin total = 0.0 order.transactions.each do |transaction| total = total + transaction.amount.to_f if (transaction.kind == 'capture' && transaction.status == 'success') end return total rescue return 0.0 end end |
.products_ordered(line_items, store) ⇒ Object
Generate a string of line item details for the order.
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'app/models/gemgento/adapter/shopify/order.rb', line 291 def self.products_ordered(line_items, store) ordered = [] line_items.each do |line_item| if shopify_adapter = Gemgento::Adapter::ShopifyAdapter.find_by(shopify_model_type: 'ShopifyAPI::Variant', shopify_model_id: line_item.variant_id) product = shopify_adapter.gemgento_model else product = Gemgento::Product.find_by(sku: Gemgento::Adapter::Shopify::Product.normalize_sku(line_item.sku)) end if product ordered << "#{product.sku}:#{line_item.quantity}:#{line_item.price}" else ordered << "#{Gemgento::Adapter::Shopify::Product.normalize_sku(line_item.sku)}:#{line_item.quantity}:#{line_item.price}" end end return ordered.join('|') end |
.quantity_ordered(order) ⇒ Float
Calculate total quantity of ordered items.
277 278 279 280 281 282 283 284 285 |
# File 'app/models/gemgento/adapter/shopify/order.rb', line 277 def self.quantity_ordered(order) total = 0.0 order.line_items.each do |line_item| total = total + line_item.quantity.to_f end return total end |
.refund_amount(order) ⇒ Float
Calculate the total amount refunded.
261 262 263 264 265 266 267 268 269 270 271 |
# File 'app/models/gemgento/adapter/shopify/order.rb', line 261 def self.refund_amount(order) total = 0.0 order.refunds.each do |refund| refund.transactions.each do |transaction| total = total + transaction.amount.to_f if transaction.kind = 'refund' end end return total end |
.shipping_amount(order) ⇒ Float
Calculate total shipping cost for shopify order.
247 248 249 250 251 252 253 254 255 |
# File 'app/models/gemgento/adapter/shopify/order.rb', line 247 def self.shipping_amount(order) total = 0.0 order.shipping_lines.each do |shipping_line| total = total + shipping_line.price.to_f end return total end |
.store(order) ⇒ Gemgento::Store
Retrieve the store related to the order.
178 179 180 |
# File 'app/models/gemgento/adapter/shopify/order.rb', line 178 def self.store(order) return Gemgento::Store.current end |
.street(address) ⇒ String
Get a formatted street attribute from an address.
216 217 218 219 220 221 |
# File 'app/models/gemgento/adapter/shopify/order.rb', line 216 def self.street(address) street = address.address1 street = "#{street}\n#{address.address2}" unless address.address2.blank? return street end |
.totals(order) ⇒ Hash(Double)
Calculate the various totals for an order.
186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'app/models/gemgento/adapter/shopify/order.rb', line 186 def self.totals(order) totals = {} totals[:subtotal] = order.subtotal_price totals[:tax] = order.total_tax totals[:shipping] = shipping_amount(order) totals[:discount] = order.total_discounts totals[:grand] = order.total_price totals[:refunded] = refund_amount(order) totals[:paid] = paid_amount(order) totals[:canceled] = (order.financial_status == 'voided') ? totals[:paid] : 0.0 return totals end |
.user_group(user) ⇒ String
Get the user group from the provided user. Default to NOT LOGGED IN.
204 205 206 207 208 209 210 |
# File 'app/models/gemgento/adapter/shopify/order.rb', line 204 def self.user_group(user) if user.nil? return Gemgento::UserGroup.find_by(magento_id: 0).code else return user.user_group.code end end |