Skip to content

Instantly share code, notes, and snippets.

@glennfu
Created May 29, 2016 16:31
Show Gist options
  • Save glennfu/56f0bab456d0bccafe0b6aab5cb7f8a5 to your computer and use it in GitHub Desktop.
Save glennfu/56f0bab456d0bccafe0b6aab5cb7f8a5 to your computer and use it in GitHub Desktop.
class InventoryItem < ActiveRecord::Base
def self.update_kit_ids
search = NetSuite::Records::InventoryItem.search({
criteria: {
basic: [
{
field: 'type',
operator: 'anyOf',
type: 'SearchEnumMultiSelectField',
value: [
# https://system.netsuite.com/help/helpcenter/en_US/srbrowser/Browser2014_1/schema/enum/itemtype.html
'_kit'
]
}
]
}
}); kits = []; search.results_in_batches { |batch| kits += batch }; kits.length
kits.each do |kit|
# DO THINGS HERE
end
end
end
# Some of this is for convenience, some helped with issues
module NetSuite
module Records
class KitItem
actions :search
record_refs :member_list
def items
inventory_items = self.member_list.attributes[:item_member]
inventory_items = [inventory_items] unless inventory_items.kind_of?(Array)
inventory_items.map do |hash|
{
quantity: hash[:quantity].to_i,
name: hash[:member_descr],
tattly_id: hash[:item][:name],
net_suite_id: hash[:item][:@internal_id].to_i,
member_unit: hash[:member_unit]
}
end
end
end
class AssemblyItem
record_refs :member_list
def items
inventory_items = self.member_list.attributes[:item_member]
inventory_items = [inventory_items] unless inventory_items.kind_of?(Array)
inventory_items.map do |hash|
{
quantity: hash[:quantity].to_i,
name: hash[:member_descr],
tattly_id: hash[:item][:name],
net_suite_id: hash[:item][:@internal_id].to_i,
member_unit: hash[:member_unit]
}
end
end
end
class Invoice
actions :search
end
class InventoryItem
actions :search
def search_class_name
"Item"
end
end
class Partner
include Support::Fields
include Support::RecordRefs
include Support::Records
include Support::Actions
include Namespaces::ListRel
attr_accessor :search_joins
actions :get, :search, :get_list, :update
fields :first_name, :last_name, :entity_id, :partner_code, :email, :phone, :company_name,
:address, :address1, :address2, :address_label, :city, :country, :email, :zip_code
field :addressbook_list, CustomerAddressbookList
field :custom_field_list, CustomFieldList
# record_refs :address_book_list
attr_reader :internal_id
attr_accessor :external_id
attr_accessor :search_joins
def initialize(attributes = {})
@internal_id = attributes.delete(:internal_id) || attributes.delete(:@internal_id)
@external_id = attributes.delete(:external_id) || attributes.delete(:@external_id)
initialize_from_attributes_hash(attributes)
end
def to_record
rec = super
if rec["#{record_namespace}:customFieldList"]
rec["#{record_namespace}:customFieldList!"] = rec.delete("#{record_namespace}:customFieldList")
end
rec
end
end
# Convenience methods so that what I'm accessing isn't so messy and easier to spec
class Transaction
attr_accessor :invoice
def department_name
department.name rescue nil
end
def items
@invoice ||= NetSuite::Records::Invoice.get({
internal_id: internal_id,
columns: {
'tranSales:basic' => [
'platformCommon:internalId/' => {}
],
'tranSales:itemList' => [
'platformCommon:internalId/' => {},
'platformCommon:units/' => {}
]
},
})
@invoice.item_list.items
end
end
class InvoiceItem
def tattly_id
item.name
end
def member_unit_guess
units.name rescue "SET"
end
def quantity_with_typecast
quantity_without_typecast.to_i
end
alias_method_chain :quantity, :typecast
def internal_id
item.internal_id.to_i
end
end
end
module Actions
class Search
def search_friendly_class_name
class_name = @klass.to_s.split("::").last
if class_name == "InventoryItem"
"Item"
else
class_name
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment