/* * FeliCa2Money * * Copyright (C) 2001-2008 Takuya Murakami * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /* OFX ファイル生成 ファイルバージョンは 1.0.2 (SGML) */ using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Xml; namespace FeliCa2Money { class OfxFile { protected string ofxFilePath; public OfxFile() { } public void SetOfxFilePath(String path) { ofxFilePath = path; } protected string dateStr(DateTime d) { string s = String.Format("{0}{1:00}{2:00}", d.Year, d.Month, d.Day); s += String.Format("{0:00}{1:00}{2:00}", d.Hour, d.Minute, d.Second); s += "[+9:JST]"; return s; } protected string transId(Transaction t) { /* トランザクションの ID は日付と取引番号で生成 */ string longId = String.Format("{0:0000}{1:00}{2:00}", t.date.Year, t.date.Month, t.date.Day); longId += String.Format("{0:0000000}", t.id); return longId; } protected string quoteString(string s) { s = s.Replace("&", "&"); s = s.Replace("<", "<"); s = s.Replace(">", ">"); //s = s.Replace("'", "'"); //s = s.Replace("\"", """); return s; } public virtual void WriteFile(Card card, List transactions) { Transaction first = transactions[0]; Transaction last = transactions[transactions.Count - 1]; StreamWriter w = new StreamWriter(ofxFilePath, false); //, Encoding.UTF8); w.NewLine = "\n"; w.WriteLine("OFXHEADER:100"); w.WriteLine("DATA:OFXSGML"); w.WriteLine("VERSION:102"); w.WriteLine("SECURITY:NONE"); w.WriteLine("ENCODING:UTF-8"); w.WriteLine("CHARSET:CSUNICODE"); w.WriteLine("COMPRESSION:NONE"); w.WriteLine("OLDFILEUID:NONE"); w.WriteLine("NEWFILEUID:NONE"); w.WriteLine(""); /* 金融機関情報(サインオンレスポンス) */ w.WriteLine(""); w.WriteLine(""); w.WriteLine(""); w.WriteLine(" "); w.WriteLine(" 0"); w.WriteLine(" INFO"); w.WriteLine(" "); w.WriteLine(" {0}", dateStr(last.date)); w.WriteLine(" JPN"); w.WriteLine(" "); w.WriteLine(" {0}", card.Ident); w.WriteLine(" "); w.WriteLine(""); w.WriteLine(""); /* 口座情報(バンクメッセージレスポンス) */ w.WriteLine(""); /* 預金口座型明細情報作成 */ w.WriteLine(""); w.WriteLine("0"); w.WriteLine(""); w.WriteLine(" 0"); w.WriteLine(" INFO"); w.WriteLine(""); w.WriteLine(""); w.WriteLine(" JPY"); w.WriteLine(" "); w.WriteLine(" {0}", card.BankId); w.WriteLine(" {0}", card.BranchId); w.WriteLine(" {0}", card.AccountId); w.WriteLine(" SAVINGS"); w.WriteLine(" "); /* 明細情報開始(バンクトランザクションリスト) */ w.WriteLine(" "); w.WriteLine(" {0}", dateStr(first.date)); w.WriteLine(" {0}", dateStr(last.date)); /* トランザクション */ foreach (Transaction t in transactions) { w.WriteLine(" "); w.WriteLine(" {0}", t.GetTransString()); w.WriteLine(" {0}", dateStr(t.date)); w.WriteLine(" {0}", t.value); /* トランザクションの ID は日付と取引番号で生成 */ w.WriteLine(" {0}", transId(t)); w.WriteLine(" {0}", quoteString(t.desc)); if (t.memo != null) { w.WriteLine(" {0}", quoteString(t.memo)); } w.WriteLine(" "); } w.WriteLine(" "); /* 残高 */ w.WriteLine(" "); w.WriteLine(" {0}", last.balance); w.WriteLine(" {0}", dateStr(last.date)); w.WriteLine(" "); /* OFX 終了 */ w.WriteLine(" "); w.WriteLine(""); w.WriteLine(""); w.WriteLine(""); w.Close(); } public void Execute() { System.Diagnostics.Process.Start(ofxFilePath); } } }