lib/pack_material/services/complete_sales_order.rb
# rubocop:disable Metrics/ClassLength
# Complete a sales order and send XML representation to accounting system.
# - use the `just_show_xml` flag to check the XML output without modifying the db.
#
# @param user_name [string] the user who initiated this task.
# @param [Integer] mr_sales_order_id
# @param just_show_xml [bool] set to true to just return XML from the call without updating the db.
disable Metrics/ClassLength
Complete a sales order and send XML representation to accounting system.
use the `just_show_xml` flag to check the XML output without modifying the db.
disable Metrics / ClassLength Complete a sales order and send XML representation to accounting system . use the ` just_show_xml ` flag to check the XML output without modifying the db .
def build_xml 
      products = @repo.products_for_sales_order(@id)
      so_totals = @repo.so_sub_totals(@id, delimiter: '', no_decimals: 5)
      costs = @repo.so_costs(@id)
      total_cost_of_sales = products.sum { |r| r[:weighted_average_cost] * r[:quantity_required] }
      cost_of_sales = UtilityFunctions.delimited_number(total_cost_of_sales, delimiter: '', no_decimals: 2)
      request_xml = Nokogiri::XML::Builder.new do |xml| 
        xml.sales_invoice do 
          xml.internal_invoice_number @sales_order.sales_order_number
          xml.customer_code @sales_order.erp_customer_number
          xml.account_code '10650'
          xml.shipped_at @sales_order.shipped_at
          xml.invoice_total so_totals[:total]
          xml.subtotal so_totals[:subtotal]
          xml.vat so_totals[:vat]
          xml.object 'PGM'
          xml.customer_order_number @sales_order.client_reference_number
          xml.costs do
            costs.each do |cost|
              xml.cost do
                xml.item 'NS100107'
                xml.cost_code cost[:cost_type_code]
                xml.account_code cost[:account_code]
                xml.amount UtilityFunctions.delimited_number(cost[:amount], delimiter: '', no_decimals: 2)
                xml.object 'OTH'
              end
            end
          end
          xml.line_items do
            products.each do |line_item|
              xml.line_item do
                xml.item 'NS100057'
                xml.description line_item[:product_variant_code]
                xml.unit_price UtilityFunctions.delimited_number(line_item[:unit_price], delimiter: '', no_decimals: 5)
                xml.quantity UtilityFunctions.delimited_number(line_item[:quantity_required], delimiter: '', no_decimals: 2)
                xml.line_total UtilityFunctions.delimited_number(line_item[:line_total], delimiter: '', no_decimals: 2)
                xml.cost UtilityFunctions.delimited_number(line_item[:weighted_average_cost], delimiter: '', no_decimals: 5)
                xml.fifo UtilityFunctions.delimited_number(line_item[:weighted_average_cost], delimiter: '', no_decimals: 5)
                xml.location 'PMAT'
              end
            end
          end
          xml.profit_loss_journal do
            xml.transaction_date @sales_order.shipped_at
            xml.reference 'MBB'
            xml.items do
              xml.item do
                xml.account '10650'
                xml.object 'PGM, PML, PPM, FUT'
                xml.description 'Profit/Loss on Packing Material'
                xml.base_debit cost_of_sales
                xml.base_credit nil
              end
              xml.item do
                xml.account '77000'
                xml.object nil
                xml.description 'Inventory'
                xml.base_debit nil
                xml.base_credit cost_of_sales
              end
            end
            xml.total_base_debit
            xml.total_base_credit
          end
        end
      end
      request_xml.to_xml
    end
def build_xml products = @repo . products_for_sales_order ( @id ) so_totals = @repo . so_sub_totals ( @id , delimiter : '' , no_decimals : 5 ) costs = @repo . so_costs ( @id ) total_cost_of_sales = products . sum { | r | r [ :weighted_average_cost ] * r [ :quantity_required ] } cost_of_sales = UtilityFunctions . delimited_number ( total_cost_of_sales , delimiter : '' , no_decimals : 2 ) request_xml = Nokogiri :: XML :: Builder . new do | xml | xml . sales_invoice do xml . internal_invoice_number @sales_order . sales_order_number xml . customer_code @sales_order . erp_customer_number xml . account_code '10650' xml . shipped_at @sales_order . shipped_at xml . invoice_total so_totals [ :total ] xml . subtotal so_totals [ :subtotal ] xml . vat so_totals [ :vat ] xml . object 'PGM' xml . customer_order_number @sales_order . client_reference_number xml . costs do costs . each do | cost | xml . cost do xml . item 'NS100107' xml . cost_code cost [ :cost_type_code ] xml . account_code cost [ :account_code ] xml . amount UtilityFunctions . delimited_number ( cost [ :amount ] , delimiter : '' , no_decimals : 2 ) xml . object 'OTH' end end end xml . line_items do products . each do | line_item | xml . line_item do xml . item 'NS100057' xml . description line_item [ :product_variant_code ] xml . unit_price UtilityFunctions . delimited_number ( line_item [ :unit_price ] , delimiter : '' , no_decimals : 5 ) xml . quantity UtilityFunctions . delimited_number ( line_item [ :quantity_required ] , delimiter : '' , no_decimals : 2 ) xml . line_total UtilityFunctions . delimited_number ( line_item [ :line_total ] , delimiter : '' , no_decimals : 2 ) xml . cost UtilityFunctions . delimited_number ( line_item [ :weighted_average_cost ] , delimiter : '' , no_decimals : 5 ) xml . fifo UtilityFunctions . delimited_number ( line_item [ :weighted_average_cost ] , delimiter : '' , no_decimals : 5 ) xml . location 'PMAT' end end end xml . profit_loss_journal do xml . transaction_date @sales_order . shipped_at xml . reference 'MBB' xml . items do xml . item do xml . account '10650' xml . object 'PGM, PML, PPM, FUT' xml . description 'Profit/Loss on Packing Material' xml . base_debit cost_of_sales xml . base_credit nil end xml . item do xml . account '77000' xml . object nil xml . description 'Inventory' xml . base_debit nil xml . base_credit cost_of_sales end end xml . total_base_debit xml . total_base_credit end end end request_xml . to_xml end
rubocop:disable Metrics/ClassLength
Complete a sales order and send XML representation to accounting system.
rubocop : disable Metrics / ClassLength Complete a sales order and send XML representation to accounting system .
# rubocop:disable Metrics/AbcSize # rubocop:disable Metrics/BlockLength # rubocop:disable Metrics/BlockLength
user_name
<not_specific>
the user who initiated this task.
the user who initiated this task .
<not_specific>
None
just_show_xml
<not_specific>
set to true to just return XML from the call without updating the db.
set to true to just return XML from the call without updating the db .