<commit_before>module Quickbooks
  module Online
    class Client < Quickbooks::Base

      def status_service
        not_supported!
      end

      def build_receipt_header
        receipt_header = super
        customer_name = "#{@order["billing_address"]["firstname"]} #{@order["billing_address"]["lastname"]}"
        customer = find_customer_by_name(customer_name)
        unless customer
         customer = create_customer
        end
        receipt_header.customer_id = customer.id
        receipt_header
      end

      def sales_receipt
        receipt = create_model("SalesReceipt")
        receipt.header = build_receipt_header
        receipt.doc_number = @order["number"]

        line_items = []

        @order["line_items"].each do |line_item|
          sales_receipt_line_item = Quickeebooks::Windows::Model::SalesReceiptLineItem.new

          sku = line_item["sku"]
          desc = line_item["name"]
          price = line_item["price"]

          item = create_item(sku,desc,price)
          quantity = line_item["quantity"]
          sales_receipt_line_item.quantity = quantity
          sales_receipt_line_item.unit_price  = price
          sales_receipt_line_item.item_id  = item.id
          sales_receipt_line_item.amount = (quantity * price)
          sales_receipt_line_item.desc = line_item["name"]
          line_items << sales_receipt_line_item
        end
        adjustments = Adjustment.new(@original["adjustments"])

        adjustments.shipping.each do |adjustment|
          line = Quickeebooks::Windows::Model::SalesReceiptLineItem.new
          sku = get_config!("quickbooks.shipping_item")
          desc = adjustment["label"]
          price = adjustment["amount"]
          item = create_item(sku,desc,price)

          line.quantity = 1
          line.unit_price = price
          line.amount = price
          line.desc = desc
          line.item_id = item.id
          line_items << line
        end

        adjustments.tax.each do |adjustment|
          line = Quickeebooks::Windows::Model::SalesReceiptLineItem.new
          sku = get_config!("quickbooks.tax_item")
          desc = adjustment["label"]
          price = adjustment["amount"]
          item = create_item(sku,desc,price)

          line.quantity = 1
          line.unit_price = price
          line.amount = price
          line.desc = desc
          line.item_id = item.id
          line_items << line
        end

        adjustments.coupon.each do |adjustment|
          line = Quickeebooks::Windows::Model::SalesReceiptLineItem.new
          sku = get_config!("quickbooks.coupon_item")
          desc = adjustment["label"]
          price = adjustment["amount"]
          item = create_item(sku,desc,price)

          line.quantity = 1
          line.unit_price = price
          line.amount = price
          line.desc = desc
          line.item_id = item.id
          line_items << line
        end

        adjustments.discount.each do |adjustment|
          line = Quickeebooks::Windows::Model::SalesReceiptLineItem.new
          sku = get_config!("quickbooks.discount_item")
          desc = adjustment["label"]
          price = adjustment["amount"]
          item = create_item(sku,desc,price)

          line.quantity = 1
          line.unit_price = price
          line.amount = price
          line.desc = desc
          line.item_id = item.id
          line_items << line
        end
        receipt.line_items = line_items
        receipt
      end

      def find_account_by_name(account_name)
        list = account_service.list(["NAME :EQUALS: #{account_name}"],1,1)
        if list.count == 0
          raise Exception.new("No Account '#{account_name}' defined in Quickbooks")
        end
        list.entries.first
      end

      def find_item_by_sku(sku)
        list = item_service.list(["NAME :EQUALS: #{sku}"],1,1)
        list.entries.first
      end

      def create_item(sku, desc, price)
        account = find_account_by_name(lookup_value!(@config,"quickbooks.account_name",false,"Sales"))
        item = find_item_by_sku(sku)
        return item if item

        item = create_model("Item")
        item.name = sku
        item.desc = desc
        item.unit_price = Quickeebooks::Windows::Model::Price.new(price)
        item.account_reference = account
        item.taxable = "true"
        return item_service.create(item)
      end

      def find_customer_by_name(name)
        list = customer_service.list(["NAME :EQUALS: #{name}"]).entries
        list.entries.first
      end

      def create_customer
        customer = create_model("Customer")

        customer_name = "#{@order["billing_address"]["firstname"]} #{@order["billing_address"]["lastname"]}"

        billing_address = quickbook_address(@order["billing_address"])
        billing_address.tag = "Billing"
        customer.address = billing_address

        shipping_address = quickbook_address(@order["shipping_address"])
        shipping_address.tag = "Shipping"
        #quickeebooks will append the addresses to the internal addresses var.
        customer.address = shipping_address
        customer.name = customer_name

        customer.email_address = @order["email"]

        return customer_service.create(customer)
      end

      def persist
        begin
          receipt = receipt_service.create(sales_receipt)
          @id = receipt.id.value
          @idDomain = receipt.id.idDomain
          xref = CrossReference.new
          xref.add(@order["number"], @id, @idDomain)
        rescue Exception => exception
          return 500, {
            'message_id' => @message_id,
            'notifications' => [
              {
                "level" => "error",
                "subject" => exception.message,
                "description" => exception.backtrace
              }
            ]
          }
        end

        return 200, {
          'message_id' => @message_id,
          'notifications' => [
            {
              "level" => "info",
              "subject" => "persisted order #{@order["number"]} in Quickbooks",
              "description" => "Quickbooks SalesReceipt id = #{@id} and idDomain = #{@idDomain}"
            }
          ]
        }
      end
    end
  end
end<commit_msg>make sure the description field is a String not an Array<commit_after>module Quickbooks
  module Online
    class Client < Quickbooks::Base

      def status_service
        not_supported!
      end

      def build_receipt_header
        receipt_header = super
        customer_name = "#{@order["billing_address"]["firstname"]} #{@order["billing_address"]["lastname"]}"
        customer = find_customer_by_name(customer_name)
        unless customer
         customer = create_customer
        end
        receipt_header.customer_id = customer.id
        receipt_header
      end

      def sales_receipt
        receipt = create_model("SalesReceipt")
        receipt.header = build_receipt_header
        receipt.doc_number = @order["number"]

        line_items = []

        @order["line_items"].each do |line_item|
          sales_receipt_line_item = Quickeebooks::Windows::Model::SalesReceiptLineItem.new

          sku = line_item["sku"]
          desc = line_item["name"]
          price = line_item["price"]

          item = create_item(sku,desc,price)
          quantity = line_item["quantity"]
          sales_receipt_line_item.quantity = quantity
          sales_receipt_line_item.unit_price  = price
          sales_receipt_line_item.item_id  = item.id
          sales_receipt_line_item.amount = (quantity * price)
          sales_receipt_line_item.desc = line_item["name"]
          line_items << sales_receipt_line_item
        end
        adjustments = Adjustment.new(@original["adjustments"])

        adjustments.shipping.each do |adjustment|
          line = Quickeebooks::Windows::Model::SalesReceiptLineItem.new
          sku = get_config!("quickbooks.shipping_item")
          desc = adjustment["label"]
          price = adjustment["amount"]
          item = create_item(sku,desc,price)

          line.quantity = 1
          line.unit_price = price
          line.amount = price
          line.desc = desc
          line.item_id = item.id
          line_items << line
        end

        adjustments.tax.each do |adjustment|
          line = Quickeebooks::Windows::Model::SalesReceiptLineItem.new
          sku = get_config!("quickbooks.tax_item")
          desc = adjustment["label"]
          price = adjustment["amount"]
          item = create_item(sku,desc,price)

          line.quantity = 1
          line.unit_price = price
          line.amount = price
          line.desc = desc
          line.item_id = item.id
          line_items << line
        end

        adjustments.coupon.each do |adjustment|
          line = Quickeebooks::Windows::Model::SalesReceiptLineItem.new
          sku = get_config!("quickbooks.coupon_item")
          desc = adjustment["label"]
          price = adjustment["amount"]
          item = create_item(sku,desc,price)

          line.quantity = 1
          line.unit_price = price
          line.amount = price
          line.desc = desc
          line.item_id = item.id
          line_items << line
        end

        adjustments.discount.each do |adjustment|
          line = Quickeebooks::Windows::Model::SalesReceiptLineItem.new
          sku = get_config!("quickbooks.discount_item")
          desc = adjustment["label"]
          price = adjustment["amount"]
          item = create_item(sku,desc,price)

          line.quantity = 1
          line.unit_price = price
          line.amount = price
          line.desc = desc
          line.item_id = item.id
          line_items << line
        end
        receipt.line_items = line_items
        receipt
      end

      def find_account_by_name(account_name)
        list = account_service.list(["NAME :EQUALS: #{account_name}"],1,1)
        if list.count == 0
          raise Exception.new("No Account '#{account_name}' defined in Quickbooks")
        end
        list.entries.first
      end

      def find_item_by_sku(sku)
        list = item_service.list(["NAME :EQUALS: #{sku}"],1,1)
        list.entries.first
      end

      def create_item(sku, desc, price)
        account = find_account_by_name(lookup_value!(@config,"quickbooks.account_name",false,"Sales"))
        item = find_item_by_sku(sku)
        return item if item

        item = create_model("Item")
        item.name = sku
        item.desc = desc
        item.unit_price = Quickeebooks::Windows::Model::Price.new(price)
        item.account_reference = account
        item.taxable = "true"
        return item_service.create(item)
      end

      def find_customer_by_name(name)
        list = customer_service.list(["NAME :EQUALS: #{name}"]).entries
        list.entries.first
      end

      def create_customer
        customer = create_model("Customer")

        customer_name = "#{@order["billing_address"]["firstname"]} #{@order["billing_address"]["lastname"]}"

        billing_address = quickbook_address(@order["billing_address"])
        billing_address.tag = "Billing"
        customer.address = billing_address

        shipping_address = quickbook_address(@order["shipping_address"])
        shipping_address.tag = "Shipping"
        #quickeebooks will append the addresses to the internal addresses var.
        customer.address = shipping_address
        customer.name = customer_name

        customer.email_address = @order["email"]

        return customer_service.create(customer)
      end

      def persist
        begin
          receipt = receipt_service.create(sales_receipt)
          @id = receipt.id.value
          @idDomain = receipt.id.idDomain
          xref = CrossReference.new
          xref.add(@order["number"], @id, @idDomain)
        rescue Exception => exception
          return 500, {
            'message_id' => @message_id,
            'notifications' => [
              {
                "level" => "error",
                "subject" => exception.message,
                "description" => exception.backtrace.join("\n")
              }
            ]
          }
        end

        return 200, {
          'message_id' => @message_id,
          'notifications' => [
            {
              "level" => "info",
              "subject" => "persisted order #{@order["number"]} in Quickbooks",
              "description" => "Quickbooks SalesReceipt id = #{@id} and idDomain = #{@idDomain}"
            }
          ]
        }
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10101975780752072, 'fraction_numerical': 0.002390057361376673, 'mean_word_length': 2.086550282763708, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6791858', 'n_tokens_mistral': 3660, 'n_tokens_neox': 3519, 'n_words': 667}