diff --git a/Gemfile.lock b/Gemfile.lock index c58093b..a9219cc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - syspro-ruby (1.0.0.alpha.1) + syspro-ruby (1.0.0.alpha.2) faraday (~> 0.10) nokogiri (~> 1.8.2) @@ -14,7 +14,7 @@ GEM coderay (1.1.2) crack (0.4.3) safe_yaml (~> 1.0.0) - faraday (0.14.0) + faraday (0.15.4) multipart-post (>= 1.2, < 3) hashdiff (0.3.7) method_source (0.9.0) @@ -27,7 +27,7 @@ GEM minitest (>= 4.7.5) vcr (>= 2.9) multipart-post (2.0.0) - nokogiri (1.8.2) + nokogiri (1.8.5) mini_portile2 (~> 2.3.0) parallel (1.12.1) parser (2.5.0.5) @@ -69,4 +69,4 @@ DEPENDENCIES webmock (~> 3.3.0) BUNDLED WITH - 1.16.1 + 1.16.4 diff --git a/lib/syspro/business_objects/models/sor_detail.rb b/lib/syspro/business_objects/models/sor_detail.rb index d4d059f..52f3e2f 100644 --- a/lib/syspro/business_objects/models/sor_detail.rb +++ b/lib/syspro/business_objects/models/sor_detail.rb @@ -20,9 +20,8 @@ module Syspro :jobs_exist_flag, :alternate_key, :hierarchy_flag, :deposit_flag, :edi_source, :mult_ship_code, :company_tax_no, :last_operator, :operator, :state, :county_zip, :extended_tax_code, :web_created, :quote, :dispatches_made, :live_disp_exist, :num_dispatches, :include_in_mrp, :header_text, - :header_notes, :commissions + :header_notes, :commissions, :sales_order_lines end end end end - diff --git a/lib/syspro/business_objects/parsers/sorqry_parser.rb b/lib/syspro/business_objects/parsers/sorqry_parser.rb index 6ef6f0b..34082ae 100644 --- a/lib/syspro/business_objects/parsers/sorqry_parser.rb +++ b/lib/syspro/business_objects/parsers/sorqry_parser.rb @@ -121,20 +121,181 @@ module Syspro sor.header_text = doc.first_element_child.xpath("HeaderText").text sor.header_notes = doc.first_element_child.xpath("HeaderNotes").text + # Inner Nested Structure Parsing + sor.commissions = parse_commissions(doc) + sor.sales_order_lines = parse_sales_order_lines(doc) + + sor + end + + def parse_commissions(doc) commissions = doc.first_element_child.xpath("Commissions") - commissions_obj = commissions.children.map do |el| + commissions_obj = parse_children_elements(commissions) + + commissions_obj + end + + + def parse_sales_order_lines(doc) + sales_order_lines = doc.first_element_child.xpath("SalesOrderLine") + sales_order_lines_obj = {} + + sales_order_lines.children.each do |el| + next if el.name == "text" + + serial_obj = {} + bin_obj = {} + attached_items_obj = {} + lot_obj = {} + + if el.name == "MiscCharge" + unless sales_order_lines_obj[:misc_charge] + sales_order_lines_obj[:misc_charge] = [] + end + + misc_charge_arr = parse_children_elements(el) + sales_order_lines_obj[:misc_charge].push(misc_charge_arr) + end + + if el.name == "Freight" + unless sales_order_lines_obj[:freight] + sales_order_lines_obj[:freight] = [] + end + + freight_arr = parse_children_elements(el) + sales_order_lines_obj[:freight].push(freight_arr) + end + + if el.name == "CommentLine" + unless sales_order_lines_obj[:comment_line] + sales_order_lines_obj[:comment_line] = [] + end + + comment_line_arr = parse_children_elements(el) + sales_order_lines_obj[:comment_line].push(comment_line_arr) + end + + if el.name == "Merchandise" + unless sales_order_lines_obj[:merchandise] + sales_order_lines_obj[:merchandise] = [] + end + + merchandise_arr = el.children.map do |el_child| + next if el_child.name == "text" + + # NOTE: These first three in the following + # conditionals are "Merchandise" elements with + # thier own nested structure that need parsed + if el_child.name == "Serial" + unless serial_obj[:serial] + serial_obj[:serial] = [] + end + + serial_arr = parse_children_elements(el_child) + serial_obj[:serial].push(serial_arr) + serial_obj + + elsif el_child.name == "Bin" + unless bin_obj[:bin] + bin_obj[:bin] = [] + end + + bin_arr = parse_children_elements(el_child) + bin_obj[:bin].push(bin_arr) + bin_obj + + elsif el_child.name == "Lot" + unless lot_obj[:lot] + lot_obj[:lot] = [] + end + + lot_arr = parse_children_elements(el_child) + lot_obj[:lot].push(lot_arr) + lot_obj + + elsif el_child.name == "AttachedItems" + # NOTE: Like "Merchandise", "AttachedItems" is + # an element within "Merchandise" that contains + # elements with thier own nested structure that + # need parsed + + sct_item_obj = {} + requisition_item_obj = {} + purchase_order_obj = {} + + unless attached_items_obj[:attached_items] + attached_items_obj[:attached_items] = [] + end + + attached_items_arr = el_child.children.map do |attached_items_child| + next if attached_items_child.name == "text" + + if attached_items_child.name == "SctItem" + unless sct_item_obj[:sct_item] + sct_item_obj[:sct_item] = [] + end + + sct_item_arr = parse_children_elements(attached_items_child) + sct_item_obj[:sct_item].push(sct_item_arr) + sct_item_obj + + elsif attached_items_child.name == "RequisitionItem" + unless requisition_item_obj[:requisition_item] + requisition_item_obj[:requisition_item] = [] + end + + requisition_item_arr = parse_children_elements(attached_items_child) + requisition_item_obj[:requisition_item].push(requisition_item_arr) + requisition_item_obj + + elsif attached_items_child.name == "PurchaseOrder" + unless purchase_order_obj[:purchase_order] + purchase_order_obj[:purchase_order] = [] + end + + purchase_order_arr = parse_children_elements(attached_items_child) + purchase_order_obj[:purchase_order].push(purchase_order_arr) + purchase_order_obj + + else + { + name: attached_items_child.name, + text: attached_items_child.text + } + end + end.compact + + attached_items_obj[:attached_items].push(attached_items_arr) + attached_items_obj + + else + { + name: el_child.name, + text: el_child.text + } + end + end.compact + + sales_order_lines_obj[:merchandise].push(merchandise_arr) + end + end + + sales_order_lines_obj + end + + def parse_children_elements(el_child) + obj_array = el_child.children.map do |el| next if el.name == "text" { name: el.name, text: el.text } end.compact - sor.commissions = commissions_obj - sor + obj_array end + end end end end - diff --git a/lib/syspro/business_objects/sorqry.rb b/lib/syspro/business_objects/sorqry.rb index 976b409..9f2f6d7 100644 --- a/lib/syspro/business_objects/sorqry.rb +++ b/lib/syspro/business_objects/sorqry.rb @@ -34,4 +34,3 @@ module Syspro end end end - diff --git a/lib/syspro/syspro_object.rb b/lib/syspro/syspro_object.rb index 6a88859..7e70ec4 100644 --- a/lib/syspro/syspro_object.rb +++ b/lib/syspro/syspro_object.rb @@ -44,7 +44,7 @@ module Syspro def to_hash # rubocop:disable Metrics/MethodLength maybe_to_hash = lambda do |value| - value&.respond_to?(:to_hash) ? value.to_hash : value + value.respond_to?(:to_hash) ? value.to_hash : value end @values.each_with_object({}) do |(key, value), acc| diff --git a/syspro-ruby-1.0.0.alpha.2.gem b/syspro-ruby-1.0.0.alpha.2.gem new file mode 100644 index 0000000..e4f89f0 Binary files /dev/null and b/syspro-ruby-1.0.0.alpha.2.gem differ -- libgit2 0.21.4