Skip to content

Instantly share code, notes, and snippets.

@welingtonsampaio
Last active April 30, 2021 21:21
Show Gist options
  • Save welingtonsampaio/897dea37d356aeb06c863d6a20fe699a to your computer and use it in GitHub Desktop.
Save welingtonsampaio/897dea37d356aeb06c863d6a20fe699a to your computer and use it in GitHub Desktop.
class CheckNotazzQty
# attr_reader :orders
attr_reader :order
def initialize(order)
# @orders = Spree::Order.s_complete.where.not(notazz_id: nil)
@order = order #s.order("RAND()").first
@l = Logger.new(Rails.root.join("log/nf.log"))
end
def validate
products.each do |k, v|
if products[k] != notazz_products[k]
raise order
end
end
rescue
@l.error "#{order.number} | #{order.id}"
end
def webhook
@_w ||= order.webhooks.where('integration LIKE "%notazz%"').last.payload
@_w
end
def products
return @_p if @_p
@_p = {}
details = {}
order.line_items.each do |l|
product = l.variant.product
if product.assembly?
product.assemblies_parts.each do |ap|
prod = ap.part.product
details[prod.sku] ||= 0
details[prod.sku] += l.quantity * ap.count
end
else
details[product.sku] ||= 0
details[product.sku] += l.quantity
end
end
# details.keys.map { |i| " - #{details[i][:qty]}x #{details[i][:name]}" }.join("\n") << "\n"
@_p = details
end
def notazz_products
return @_n if @_n
@_n = {}
details = {}
webhook[:DOCUMENT_PRODUCT].each do |k, v|
sku = webhook[:DOCUMENT_PRODUCT][k][:DOCUMENT_PRODUCT_COD]
details[sku] = webhook[:DOCUMENT_PRODUCT][k][:DOCUMENT_PRODUCT_QTD]
end
@_n = details
end
class << self
def start
Rails.logger.silence do
Spree::Order.s_complete.where("id < 112915").where.not(notazz_id: nil).order("id DESC").each do |order|
instance = self.new(order)
instance.validate
print '.'
sleep 0.1
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment