<commit_before>class Spree::ShippingLabel
  attr_accessor :user_id,:shipment_id,:shipping_method,:tax_note,:user_email,
                :to_name,:to_company,:to_telephone,:to_address1,:to_address2,:to_city,
                :to_state,:to_zip,:to_country,:to_residential,:origin_name,:origin_company,
                :origin_telephone,:origin_address,:origin_state,:origin_city,:origin_zip,:origin_country

  def initialize shipment
    @shipment = Spree::Shipment.find_by_number(shipment)
    @stock_location = @shipment.stock_location
    @order = @shipment.order
    if @order.user.blank?
      self.user_id = rand(10000)
      self.tax_note = ""
      self.user_email = ""
    else
      self.user_id = @order.user.id
      self.tax_note = @order.user.tax_note
      self.user_email = @order.user.email
    end

    self.shipment_id = @shipment.id
    self.shipping_method = @shipment.shipping_method.api_name

    self.to_name = "#{@order.ship_address.firstname} #{@order.ship_address.lastname}"
    self.to_company = @order.ship_address.company || ""
    self.to_telephone = clean_phone_number(@order.ship_address.phone == "(not given)" ? @order.bill_address.phone : @order.ship_address.phone)
    self.to_address1 = @order.ship_address.address1
    self.to_address2 = @order.ship_address.address2 || ""
    self.to_city = @order.ship_address.city
    self.to_state = @order.ship_address.state_name || (@order.ship_address.state.nil? ? "" : @order.ship_address.state.abbr)

    self.to_zip = @order.ship_address.zipcode.gsub(/\-|\s/, '')
    self.to_country = Spree::Country.find(@order.ship_address.country_id).iso
    country = Spree::Country.find(@order.ship_address.country_id)
    self.to_residential = @order.ship_address.company.blank? ? "true" : "false"

    self.origin_name = Spree::PrintShippingLabel::Config[:origin_name]
    self.origin_company = Spree::PrintShippingLabel::Config[:origin_company]
    self.origin_telephone = clean_phone_number(Spree::PrintShippingLabel::Config[:origin_telephone])

    self.origin_address = Spree::PrintShippingLabel::Config[:origin_address]
    self.origin_country = @stock_location.country.iso
    self.origin_state = @stock_location.state.nil? ? @stock_location.state_name : @stock_location.state.name
    self.origin_city = @stock_location.city
    self.origin_zip = @stock_location.zipcode

    @path = "public/shipments/"
    @file = "#{@order.number}_#{@order.shipments.size || 1}.pdf"
    @tmp_file = "tmp_#{@file}"

    @weight = @shipment.line_items.map{|i| i.variant.weight || 0.00625}.sum

    case @shipment.shipping_method.name
      when /USPS.*/i
        usps
      when /FedEx.*/i
        fedex
    end
  end

  private

  def international?
   self.to_country != "US"
  end

  def clean_phone_number number
    number.gsub(/\+|\.|\-|\(|\)|\s/, '')
  end

  def usps
    senders_customs_ref   = ""
    importers_customs_ref = ""
    xml = []
    xml << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
    test_attributes = "Test='Yes'"
    intl_attibutes = "LabelType='International' LabelSubtype='Integrated'"
    xml << "<LabelRequest #{(Spree::ActiveShipping::Config[:test_mode]) ? test_attributes : ""} #{(!international?) ? "LabelType='Default'" : intl_attibutes} LabelSize='4x6' ImageFormat='PDF'>"
    xml << "<LabelSize>4x6</LabelSize>"
    xml << "<ImageFormat>PDF</ImageFormat>"
    xml << "<Test>N</Test>" unless Spree::ActiveShipping::Config[:test_mode]
    if international?
      # Form 2976 (short form: use this form if the number of items is 5 or fewer.)
      # Form 2976-A (long form: use this form if number of items is greater than 5.)
      # Required when the label subtype is Integrated.
      # Page 41

      # Values
        #Form2976
          #MaxItems: 5
          #FirstClassMailInternational
          #PriorityMailInternational (when used with FlatRateEnvelope, FlatRateLegalEnvelope, FlatRatePaddedEnvelope or SmallFlatRateBox)
        #Form2976A
          #Max Items: 999
          #PriorityMailInternational (when used with Parcel, MediumFlatRateBox or LargeFlatRateBox);
          #ExpressMailInternational (when used with FlatRateEnvelope, FlatRateLegalEnvelope or Parcel)
      # Page 151
      # Since we only use Parcel we will always choose Form2976A
      xml << "<IntegratedFormType>FORM2976A</IntegratedFormType>"
    end
    xml << "<RequesterID>#{Spree::PrintShippingLabel::Config[:endicia_requester_id]}</RequesterID>"
    xml << "<AccountID>#{Spree::PrintShippingLabel::Config[:endicia_account_id]}</AccountID>"
    xml << "<PassPhrase>#{Spree::PrintShippingLabel::Config[:endicia_password]}</PassPhrase>"
    xml << "<MailClass>#{self.shipping_method}</MailClass>"
    xml << "<DateAdvance>0</DateAdvance>"
    xml << "<WeightOz>#{(@weight * 16).round(1)}</WeightOz>"
    xml << "<Stealth>FALSE</Stealth>"
    xml << "<Services InsuredMail='OFF' SignatureConfirmation='OFF' />"
    # has to be greater than 0
    xml << "<Value>#{@shipment.item_cost.to_f}</Value>"
    xml << "<Description>Order ##{@order.number} / Shipment #{@shipment.number}</Description>"
    xml << "<PartnerCustomerID>#{self.user_id}</PartnerCustomerID>"
    xml << "<PartnerTransactionID>#{self.shipment_id}</PartnerTransactionID>"
    xml << "<ToName>#{self.to_name}</ToName>"
    xml << "<ToAddress1>#{self.to_address1}</ToAddress1>"
    xml << "<ToCity>#{self.to_city}</ToCity>"
    xml << "<ToState>#{self.to_state}</ToState>"
    xml << "<ToCountry>#{Spree::Country.find(@order.ship_address.country_id).iso_name}</ToCountry>"
    xml << "<ToCountryCode>#{self.to_country}</ToCountryCode>"
    xml << "<ToPostalCode>#{self.to_zip}</ToPostalCode>"
    xml << "<ToDeliveryPoint>00</ToDeliveryPoint>"
    # remove any signs from the number
    xml << "<ToPhone>#{self.to_telephone}</ToPhone>"
    xml << "<FromName>#{self.origin_name}</FromName>"
    xml << "<FromCompany>#{self.origin_company}</FromCompany>"
    xml << "<ReturnAddress1>#{self.origin_address}</ReturnAddress1>"
    xml << "<FromCity>#{self.origin_city}</FromCity>"
    xml << "<FromState>#{self.origin_state}</FromState>"
    xml << "<FromPostalCode>#{self.origin_zip}</FromPostalCode>"
    xml << "<FromPhone>#{self.origin_telephone}</FromPhone>"<commit_msg>minimum total weight post to weightoz elements there where cases where the total WeightOz value was a value lower than 0.1 but bigger than 0 in which cases round(1) was rounding to 0 (e.g.:) 0.01.round(1) == 0.0<commit_after>class Spree::ShippingLabel
  attr_accessor :user_id,:shipment_id,:shipping_method,:tax_note,:user_email,
                :to_name,:to_company,:to_telephone,:to_address1,:to_address2,:to_city,
                :to_state,:to_zip,:to_country,:to_residential,:origin_name,:origin_company,
                :origin_telephone,:origin_address,:origin_state,:origin_city,:origin_zip,:origin_country

  def initialize shipment
    @shipment = Spree::Shipment.find_by_number(shipment)
    @stock_location = @shipment.stock_location
    @order = @shipment.order
    if @order.user.blank?
      self.user_id = rand(10000)
      self.tax_note = ""
      self.user_email = ""
    else
      self.user_id = @order.user.id
      self.tax_note = @order.user.tax_note
      self.user_email = @order.user.email
    end

    self.shipment_id = @shipment.id
    self.shipping_method = @shipment.shipping_method.api_name

    self.to_name = "#{@order.ship_address.firstname} #{@order.ship_address.lastname}"
    self.to_company = @order.ship_address.company || ""
    self.to_telephone = clean_phone_number(@order.ship_address.phone == "(not given)" ? @order.bill_address.phone : @order.ship_address.phone)
    self.to_address1 = @order.ship_address.address1
    self.to_address2 = @order.ship_address.address2 || ""
    self.to_city = @order.ship_address.city
    self.to_state = @order.ship_address.state_name || (@order.ship_address.state.nil? ? "" : @order.ship_address.state.abbr)

    self.to_zip = @order.ship_address.zipcode.gsub(/\-|\s/, '')
    self.to_country = Spree::Country.find(@order.ship_address.country_id).iso
    country = Spree::Country.find(@order.ship_address.country_id)
    self.to_residential = @order.ship_address.company.blank? ? "true" : "false"

    self.origin_name = Spree::PrintShippingLabel::Config[:origin_name]
    self.origin_company = Spree::PrintShippingLabel::Config[:origin_company]
    self.origin_telephone = clean_phone_number(Spree::PrintShippingLabel::Config[:origin_telephone])

    self.origin_address = Spree::PrintShippingLabel::Config[:origin_address]
    self.origin_country = @stock_location.country.iso
    self.origin_state = @stock_location.state.nil? ? @stock_location.state_name : @stock_location.state.name
    self.origin_city = @stock_location.city
    self.origin_zip = @stock_location.zipcode

    @path = "public/shipments/"
    @file = "#{@order.number}_#{@order.shipments.size || 1}.pdf"
    @tmp_file = "tmp_#{@file}"

    @weight = @shipment.line_items.map{|i| i.variant.weight || 0.00625}.sum

    case @shipment.shipping_method.name
      when /USPS.*/i
        usps
      when /FedEx.*/i
        fedex
    end
  end

  private

  def international?
   self.to_country != "US"
  end

  def clean_phone_number number
    number.gsub(/\+|\.|\-|\(|\)|\s/, '')
  end

  def usps
    senders_customs_ref   = ""
    importers_customs_ref = ""
    xml = []
    xml << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
    test_attributes = "Test='Yes'"
    intl_attibutes = "LabelType='International' LabelSubtype='Integrated'"
    xml << "<LabelRequest #{(Spree::ActiveShipping::Config[:test_mode]) ? test_attributes : ""} #{(!international?) ? "LabelType='Default'" : intl_attibutes} LabelSize='4x6' ImageFormat='PDF'>"
    xml << "<LabelSize>4x6</LabelSize>"
    xml << "<ImageFormat>PDF</ImageFormat>"
    xml << "<Test>N</Test>" unless Spree::ActiveShipping::Config[:test_mode]
    if international?
      # Form 2976 (short form: use this form if the number of items is 5 or fewer.)
      # Form 2976-A (long form: use this form if number of items is greater than 5.)
      # Required when the label subtype is Integrated.
      # Page 41

      # Values
        #Form2976
          #MaxItems: 5
          #FirstClassMailInternational
          #PriorityMailInternational (when used with FlatRateEnvelope, FlatRateLegalEnvelope, FlatRatePaddedEnvelope or SmallFlatRateBox)
        #Form2976A
          #Max Items: 999
          #PriorityMailInternational (when used with Parcel, MediumFlatRateBox or LargeFlatRateBox);
          #ExpressMailInternational (when used with FlatRateEnvelope, FlatRateLegalEnvelope or Parcel)
      # Page 151
      # Since we only use Parcel we will always choose Form2976A
      xml << "<IntegratedFormType>FORM2976A</IntegratedFormType>"
    end
    xml << "<RequesterID>#{Spree::PrintShippingLabel::Config[:endicia_requester_id]}</RequesterID>"
    xml << "<AccountID>#{Spree::PrintShippingLabel::Config[:endicia_account_id]}</AccountID>"
    xml << "<PassPhrase>#{Spree::PrintShippingLabel::Config[:endicia_password]}</PassPhrase>"
    xml << "<MailClass>#{self.shipping_method}</MailClass>"
    xml << "<DateAdvance>0</DateAdvance>"
    @weight *= 16 # lb to oz
    @weight = (@weight.round(1) == 0) ? 0.1 : @weight.round(1) # make sure we dont post 0.0 
    xml << "<WeightOz>#{@weight}</WeightOz>"
    xml << "<Stealth>FALSE</Stealth>"
    xml << "<Services InsuredMail='OFF' SignatureConfirmation='OFF' />"
    # has to be greater than 0
    xml << "<Value>#{@shipment.item_cost.to_f}</Value>"
    xml << "<Description>Order ##{@order.number} / Shipment #{@shipment.number}</Description>"
    xml << "<PartnerCustomerID>#{self.user_id}</PartnerCustomerID>"
    xml << "<PartnerTransactionID>#{self.shipment_id}</PartnerTransactionID>"
    xml << "<ToName>#{self.to_name}</ToName>"
    xml << "<ToAddress1>#{self.to_address1}</ToAddress1>"
    xml << "<ToCity>#{self.to_city}</ToCity>"
    xml << "<ToState>#{self.to_state}</ToState>"
    xml << "<ToCountry>#{Spree::Country.find(@order.ship_address.country_id).iso_name}</ToCountry>"
    xml << "<ToCountryCode>#{self.to_country}</ToCountryCode>"
    xml << "<ToPostalCode>#{self.to_zip}</ToPostalCode>"
    xml << "<ToDeliveryPoint>00</ToDeliveryPoint>"
    # remove any signs from the number
    xml << "<ToPhone>#{self.to_telephone}</ToPhone>"
    xml << "<FromName>#{self.origin_name}</FromName>"
    xml << "<FromCompany>#{self.origin_company}</FromCompany>"
    xml << "<ReturnAddress1>#{self.origin_address}</ReturnAddress1>"
    xml << "<FromCity>#{self.origin_city}</FromCity>"
    xml << "<FromState>#{self.origin_state}</FromState>"
    xml << "<FromPostalCode>#{self.origin_zip}</FromPostalCode>"
    xml << "<FromPhone>#{self.origin_telephone}</FromPhone>"
{'fraction_non_alphanumeric': 0.15290423861852434, 'fraction_numerical': 0.012637362637362638, 'mean_word_length': 4.1624797406807135, 'pattern_counts': {'":': 0, '<': 269, '<?xml version=': 2, '>': 133, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 72}, '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': '2250536', 'n_tokens_mistral': 4320, 'n_tokens_neox': 4196, 'n_words': 671}