Class: SpreeNpAtobarai::OrderCSVService

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree_np_atobarai/order_csv_service.rb

Constant Summary collapse

CSV_HEADER =
[
  "加盟店取引ID", "加盟店取引受注日", "企業名", "部署名", "担当者名", "購入企業ID",
  "郵便番号", "住所", "電話番号1", "電話番号2",
  "メールアドレス(To)", "メールアドレス(Cc)1", "メールアドレス(Cc)2", "メールアドレス(Cc)3", "メールアドレス(Cc)4",
  "配送先企業名", "配送先部署名", "配送先担当者名", "配送先担当者名(カナ)",
  "配送先郵便番号", "配送先住所", "配送先電話番号",
  "決済方法", "FAX番号", "請求書送付方法", "コンビニ払い設定",
  "取引金額", "明細", "元加盟店取引受注日", "単価", "数量", "金額", "NP購入企業ID", "請求書税区分"
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(order_ids) ⇒ OrderCSVService

Returns a new instance of OrderCSVService.



13
14
15
16
# File 'app/services/spree_np_atobarai/order_csv_service.rb', line 13

def initialize(order_ids)
  @order_ids = order_ids.map(&:to_i)
  @orders = Spree::Order.where(id: order_ids).includes(:line_items, :adjustments, :ship_address, :bill_address, payments: :payment_method)
end

Instance Method Details

#to_csvObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/spree_np_atobarai/order_csv_service.rb', line 18

def to_csv
  options = {
    encoding: "Shift_JIS",
    undef: :replace,  # 未定義文字を置換
    replace: '?',     # 未定義文字の置換文字
    invalid: :replace # 不正なバイトシーケンスを置換
  }
  CSV.generate do |csv|
    csv << CSV_HEADER

    @order_ids.each do |id|
      order = @orders.find { |o| o.id == id }
      next unless order

      rows = build_order_rows(order)
      rows.each { |row| csv << row }
    end
  end.encode("Shift_JIS", **options)
end