<commit_before>require "spec_helper"

feature %q{
    As an administrator
    I want numbers, all the numbers!
} do
  include AuthenticationWorkflow
  include WebHelper

  context "Permissions for different reports" do
    context "As an enterprise user" do
      let(:user) do
        create_enterprise_user(enterprises: [
          create(:distributor_enterprise)
        ])
      end
      it "does not show super admin only reports" do
        login_to_admin_as user
        click_link "Reports"
        page.should_not have_content "Sales Total"
        page.should_not have_content "Users & Enterprises"
      end
    end
    context "As an admin user" do
      it "shows the super admin only reports" do
        login_to_admin_section
        click_link "Reports"
        page.should have_content "Sales Total"
        page.should have_content "Users & Enterprises"
      end
    end
  end

  describe "Customers report" do
    before do
      login_to_admin_section
      click_link "Reports"
    end
    scenario "customers report" do
      click_link "Mailing List"
      expect(page).to have_select('report_type', selected: 'Mailing List')

      rows = find("table#listing_customers").all("thead tr")
      table = rows.map { |r| r.all("th").map { |c| c.text.strip } }
      table.sort.should == [
        ["Email", "First Name", "Last Name", "Suburb"]
      ].sort
    end

    scenario "customers report" do
      click_link "Addresses"
      expect(page).to have_select('report_type', selected: 'Addresses')

      rows = find("table#listing_customers").all("thead tr")
      table = rows.map { |r| r.all("th").map { |c| c.text.strip } }
      table.sort.should == [
        ["First Name", "Last Name", "Billing Address", "Email", "Phone", "Hub", "Hub Address", "Shipping Method"]
      ].sort
    end
  end

  describe "Order cycle management report" do
    before do
      login_to_admin_section
      click_link "Reports"
    end

    scenario "order payment method report" do
      click_link "Order Cycle Management"
      rows = find("table#listing_order_payment_methods").all("thead tr")
      table = rows.map { |r| r.all("th").map { |c| c.text.strip } }
      table.sort.should == [
        ["<NAME>", "<NAME>", "Email", "Phone", "Hub", "Shipping Method", "Payment Method", "Amount"]
      ].sort
    end
  end

  scenario "orders and distributors report" do
    login_to_admin_section
    click_link 'Reports'
    click_link 'Orders And Distributors'

    page.should have_content 'Order date'
  end

  scenario "bulk co-op report" do
    login_to_admin_section
    click_link 'Reports'
    click_link 'Bulk Co-Op'

    page.should have_content 'Supplier'
  end

  scenario "payments reports" do
    login_to_admin_section
    click_link 'Reports'
    click_link 'Payment Reports'

    page.should have_content 'Payment State'
  end
  
  describe "Sales Tax report" do
    let(:user1) do
      create_enterprise_user(enterprises: [create(:distributor_enterprise)])
    end
    let(:user2) do
      create_enterprise_user(enterprises: [create(:distributor_enterprise)])
    end
    let(:tax_category1) { create(:tax_category) }
    let(:tax_category2) { create(:tax_category) }
    let(:country) { create(:country, :name => "Spec Republic") }
    let(:state) { create(:state, :country => country, :name => "Specville") }
    let(:zone) { create(:zone, :name => "Test Tax Zone") }
    let!(:tax_rate1) { create(:tax_rate, :amount => 0.0, :calculator => Spree::Calculator::DefaultTax.new, :tax_category => tax_category1, :zone => zone) }
    let!(:tax_rate2) { create(:tax_rate, :amount => 0.2, :calculator => Spree::Calculator::DefaultTax.new, :tax_category => tax_category2, :zone => zone) }

    let(:product1) { create(:product, :tax_category => tax_category1) }
    let(:product2) { create(:product, :tax_category => tax_category2) }
    let(:variant1) { create(:variant, price: 12.54, :product => product1) }
    let(:variant2) { create(:variant, price: 500.15, :product => product2) }

    let(:address) { create(:address, :state => state, :country => country) }
    let(:shipping_method) { create(:shipping_method, :name => "Shipping", :description => "Expensive", :calculator => Spree::Calculator::FlatRate.new(preferred_amount: 100.55)) }
    let(:order1) { create(:order, :distributor => user1.enterprises.first, :shipping_method => shipping_method, :bill_address => address) }
    let!(:line_item1) { create(:line_item, :variant => variant1, :price => 12.54, :quantity => 1, :order => order1) }
    let!(:line_item2) { create(:line_item, :variant => variant2, :price => 500.15, :quantity => 3, :order => order1) }
    let!(:adjustment) { create(:adjustment, :adjustable => order1, :label => "Shipping", :amount => 100.55) }

    before do
      Spree::Config.shipment_inc_vat = true
      Spree::Config.shipping_tax_rate = 0.2
      order1.finalize!

      login_to_admin_as user1
      click_link "Reports"
      click_link "Sales Tax"
    end
  
    it "displays the report" do
      page.should     have_select 'q_distributor_id_eq', with_options: [user1.enterprises.first.name]
      page.should_not have_select 'q_distributor_id_eq', with_options: [user2.enterprises.first.name]

      select user1.enterprises.first.name, from: 'q_distributor_id_eq'
      click_button 'Search'
      
      page.should have_content "#{order1.number}"
    end

    it "calculates sales tax on orders" do
      select user1.enterprises.first.name, from: 'q_distributor_id_eq'
      click_button 'Search'
      
      page.should have_content "1512.99" #items total
      page.should have_content "1500.45" #taxable items total
      page.should have_content "300.09" #sales tax<commit_msg>Remove unneeded objects from spec setup<commit_after>require "spec_helper"

feature %q{
    As an administrator
    I want numbers, all the numbers!
} do
  include AuthenticationWorkflow
  include WebHelper

  context "Permissions for different reports" do
    context "As an enterprise user" do
      let(:user) do
        create_enterprise_user(enterprises: [
          create(:distributor_enterprise)
        ])
      end
      it "does not show super admin only reports" do
        login_to_admin_as user
        click_link "Reports"
        page.should_not have_content "Sales Total"
        page.should_not have_content "Users & Enterprises"
      end
    end
    context "As an admin user" do
      it "shows the super admin only reports" do
        login_to_admin_section
        click_link "Reports"
        page.should have_content "Sales Total"
        page.should have_content "Users & Enterprises"
      end
    end
  end

  describe "Customers report" do
    before do
      login_to_admin_section
      click_link "Reports"
    end
    scenario "customers report" do
      click_link "Mailing List"
      expect(page).to have_select('report_type', selected: 'Mailing List')

      rows = find("table#listing_customers").all("thead tr")
      table = rows.map { |r| r.all("th").map { |c| c.text.strip } }
      table.sort.should == [
        ["Email", "First Name", "Last Name", "Suburb"]
      ].sort
    end

    scenario "customers report" do
      click_link "Addresses"
      expect(page).to have_select('report_type', selected: 'Addresses')

      rows = find("table#listing_customers").all("thead tr")
      table = rows.map { |r| r.all("th").map { |c| c.text.strip } }
      table.sort.should == [
        ["First Name", "Last Name", "Billing Address", "Email", "Phone", "Hub", "Hub Address", "Shipping Method"]
      ].sort
    end
  end

  describe "Order cycle management report" do
    before do
      login_to_admin_section
      click_link "Reports"
    end

    scenario "order payment method report" do
      click_link "Order Cycle Management"
      rows = find("table#listing_order_payment_methods").all("thead tr")
      table = rows.map { |r| r.all("th").map { |c| c.text.strip } }
      table.sort.should == [
        ["First Name", "Last Name", "Email", "Phone", "Hub", "Shipping Method", "Payment Method", "Amount"]
      ].sort
    end
  end

  scenario "orders and distributors report" do
    login_to_admin_section
    click_link 'Reports'
    click_link 'Orders And Distributors'

    page.should have_content 'Order date'
  end

  scenario "bulk co-op report" do
    login_to_admin_section
    click_link 'Reports'
    click_link 'Bulk Co-Op'

    page.should have_content 'Supplier'
  end

  scenario "payments reports" do
    login_to_admin_section
    click_link 'Reports'
    click_link 'Payment Reports'

    page.should have_content 'Payment State'
  end
  
  describe "Sales Tax report" do
    let(:user1) do
      create_enterprise_user(enterprises: [create(:distributor_enterprise)])
    end
    let(:user2) do
      create_enterprise_user(enterprises: [create(:distributor_enterprise)])
    end
    let(:tax_category1) { create(:tax_category) }
    let(:tax_category2) { create(:tax_category) }
    let!(:tax_rate1) { create(:tax_rate, :amount => 0.0, :calculator => Spree::Calculator::DefaultTax.new, :tax_category => tax_category1) }
    let!(:tax_rate2) { create(:tax_rate, :amount => 0.2, :calculator => Spree::Calculator::DefaultTax.new, :tax_category => tax_category2) }

    let(:product1) { create(:product, price: 12.54,  :tax_category => tax_category1) }
    let(:product2) { create(:product, price: 500.15, :tax_category => tax_category2) }

    let(:shipping_method) { create(:shipping_method, :name => "Shipping", :description => "Expensive", :calculator => Spree::Calculator::FlatRate.new(preferred_amount: 100.55)) }
    let(:order1) { create(:order, :distributor => user1.enterprises.first, :shipping_method => shipping_method, :bill_address => create(:address)) }
    let!(:line_item1) { create(:line_item, :variant => product1.master, :price => 12.54, :quantity => 1, :order => order1) }
    let!(:line_item2) { create(:line_item, :variant => product2.master, :price => 500.15, :quantity => 3, :order => order1) }
    let!(:adjustment) { create(:adjustment, :adjustable => order1, :label => "Shipping", :amount => 100.55) }

    before do
      Spree::Config.shipment_inc_vat = true
      Spree::Config.shipping_tax_rate = 0.2
      order1.finalize!

      login_to_admin_as user1
      click_link "Reports"
      click_link "Sales Tax"
    end
  
    it "displays the report" do
      page.should     have_select 'q_distributor_id_eq', with_options: [user1.enterprises.first.name]
      page.should_not have_select 'q_distributor_id_eq', with_options: [user2.enterprises.first.name]

      select user1.enterprises.first.name, from: 'q_distributor_id_eq'
      click_button 'Search'
      
      page.should have_content "#{order1.number}"
    end

    it "calculates sales tax on orders" do
      select user1.enterprises.first.name, from: 'q_distributor_id_eq'
      click_button 'Search'
      
      page.should have_content "1512.99" #items total
      page.should have_content "1500.45" #taxable items total
      page.should have_content "300.09" #sales tax
{'fraction_non_alphanumeric': 0.1224877783813145, 'fraction_numerical': 0.015028064457722253, 'mean_word_length': 3.362954186413902, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, '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': '2959191', 'n_tokens_mistral': 3645, 'n_tokens_neox': 3471, 'n_words': 873}