| 1 |
/* |
| 2 |
* MoneyImport : Convert Japan Net Bank csv file to MS Money OFX file. |
| 3 |
* |
| 4 |
* Copyright (c) 2001-2003 Takuya Murakami. All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 |
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR |
| 21 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 22 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 23 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 24 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 25 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
* |
| 29 |
* $Id$ |
| 30 |
*/ |
| 31 |
//--------------------------------------------------------------------------- |
| 32 |
|
| 33 |
#include <vcl.h> |
| 34 |
#pragma hdrstop |
| 35 |
#include <Registry.hpp> |
| 36 |
|
| 37 |
#include <shellapi.h> |
| 38 |
|
| 39 |
#include <stdio.h> |
| 40 |
#include <stdlib.h> |
| 41 |
#include <string.h> |
| 42 |
|
| 43 |
#include "MainForm.h" |
| 44 |
#include "Convert.h" |
| 45 |
#include "Account.h" |
| 46 |
#include "Transaction.h" |
| 47 |
|
| 48 |
static AnsiString dateStr(DateTime *dt) |
| 49 |
{ |
| 50 |
AnsiString str; |
| 51 |
|
| 52 |
/* Y M D H M S */ |
| 53 |
str.sprintf("%4d%02d%02d%02d%02d%02d[+9:JST]", |
| 54 |
dt->year, dt->month, dt->date, |
| 55 |
dt->hour, dt->minutes, dt->seconds); |
| 56 |
return str; |
| 57 |
} |
| 58 |
|
| 59 |
static void |
| 60 |
WriteOfx(FILE *fp, TransactionList *list, Account *ac) |
| 61 |
{ |
| 62 |
unsigned long idoffset; |
| 63 |
Transaction *t, *last; |
| 64 |
|
| 65 |
last = list->Tail(); |
| 66 |
t = list->Head(); |
| 67 |
|
| 68 |
/* OFX �w�b�_ */ |
| 69 |
fprintf(fp, "OFXHEADER:100\n"); |
| 70 |
fprintf(fp, "DATA:OFXSGML\n"); |
| 71 |
fprintf(fp, "VERSION:102\n"); |
| 72 |
fprintf(fp, "SECURITY:NONE\n"); |
| 73 |
fprintf(fp, "ENCODING:UTF-8\n"); |
| 74 |
fprintf(fp, "CHARSET:CSUNICODE\n"); |
| 75 |
fprintf(fp, "COMPRESSION:NONE\n"); |
| 76 |
fprintf(fp, "OLDFILEUID:NONE\n"); |
| 77 |
fprintf(fp, "NEWFILEUID:NONE\n"); |
| 78 |
fprintf(fp, "\n"); |
| 79 |
|
| 80 |
/* ���Z�@������(�T�C���I�����X�|���X) */ |
| 81 |
fprintf(fp, "<OFX>\n"); |
| 82 |
fprintf(fp, "<SIGNONMSGSRSV1>\n"); |
| 83 |
fprintf(fp, "<SONRS>\n"); |
| 84 |
fprintf(fp, " <STATUS>\n"); |
| 85 |
fprintf(fp, " <CODE>0\n"); |
| 86 |
fprintf(fp, " <SEVERITY>INFO\n"); |
| 87 |
fprintf(fp, " </STATUS>\n"); |
| 88 |
fprintf(fp, " <DTSERVER>%s\n", dateStr(&last->date).c_str()); |
| 89 |
|
| 90 |
fprintf(fp, " <LANGUAGE>JPN\n"); |
| 91 |
fprintf(fp, " <FI>\n"); |
| 92 |
fprintf(fp, " <ORG>%s\n", ac->getIdent()); |
| 93 |
fprintf(fp, " </FI>\n"); |
| 94 |
fprintf(fp, "</SONRS>\n"); |
| 95 |
fprintf(fp, "</SIGNONMSGSRSV1>\n"); |
| 96 |
|
| 97 |
/* ��������(�o���N���b�Z�[�W���X�|���X) */ |
| 98 |
fprintf(fp, "<BANKMSGSRSV1>\n"); |
| 99 |
|
| 100 |
/* �a�������^������������ */ |
| 101 |
fprintf(fp, "<STMTTRNRS>\n"); |
| 102 |
fprintf(fp, "<TRNUID>0\n"); |
| 103 |
fprintf(fp, "<STATUS>\n"); |
| 104 |
fprintf(fp, " <CODE>0\n"); |
| 105 |
fprintf(fp, " <SEVERITY>INFO\n"); |
| 106 |
fprintf(fp, "</STATUS>\n"); |
| 107 |
|
| 108 |
fprintf(fp, "<STMTRS>\n"); |
| 109 |
fprintf(fp, " <CURDEF>JPY\n"); |
| 110 |
|
| 111 |
fprintf(fp, " <BANKACCTFROM>\n"); |
| 112 |
fprintf(fp, " <BANKID>%s\n", ac->getBankId()); |
| 113 |
fprintf(fp, " <BRANCHID>%s\n", ac->getBranchId()); |
| 114 |
fprintf(fp, " <ACCTID>%s\n", ac->getAccountId()); |
| 115 |
fprintf(fp, " <ACCTTYPE>SAVINGS\n"); |
| 116 |
fprintf(fp, " </BANKACCTFROM>\n"); |
| 117 |
|
| 118 |
/* ���������J�n(�o���N�g�����U�N�V�������X�g) */ |
| 119 |
fprintf(fp, " <BANKTRANLIST>\n"); |
| 120 |
fprintf(fp, " <DTSTART>%s\n", dateStr(&t->date).c_str()); |
| 121 |
fprintf(fp, " <DTEND>%s\n", dateStr(&last->date).c_str()); |
| 122 |
|
| 123 |
/* �g�����U�N�V���� */ |
| 124 |
do { |
| 125 |
fprintf(fp, " <STMTTRN>\n"); |
| 126 |
fprintf(fp, " <TRNTYPE>%s\n", t->GetTrnTypeStr()); |
| 127 |
fprintf(fp, " <DTPOSTED>%s\n", dateStr(&t->date).c_str()); |
| 128 |
fprintf(fp, " <TRNAMT>%d\n", t->value); |
| 129 |
|
| 130 |
/* �g�����U�N�V������ ID �����t���������������� */ |
| 131 |
fprintf(fp, " <FITID>%04d%02d%02d%07d\n", |
| 132 |
t->date.year, t->date.month, t->date.date, |
| 133 |
t->id); |
| 134 |
fprintf(fp, " <NAME>%s\n", t->desc); |
| 135 |
fprintf(fp, " </STMTTRN>\n"); |
| 136 |
} while ((t = list->Next()) != NULL); |
| 137 |
|
| 138 |
fprintf(fp, " </BANKTRANLIST>\n"); |
| 139 |
|
| 140 |
/* �c�� */ |
| 141 |
fprintf(fp, " <LEDGERBAL>\n"); |
| 142 |
fprintf(fp, " <BALAMT>%d\n", last->balance); |
| 143 |
fprintf(fp, " <DTASOF>%s\n", dateStr(&last->date).c_str()); |
| 144 |
fprintf(fp, " </LEDGERBAL>\n"); |
| 145 |
|
| 146 |
/* OFX �I�� */ |
| 147 |
fprintf(fp, " </STMTRS>\n"); |
| 148 |
fprintf(fp, "</STMTTRNRS>\n"); |
| 149 |
fprintf(fp, "</BANKMSGSRSV1>\n"); |
| 150 |
fprintf(fp, "</OFX>\n"); |
| 151 |
} |
| 152 |
|
| 153 |
void Convert(AnsiString csvfile, AnsiString ofxfile, Accounts *acs) |
| 154 |
{ |
| 155 |
TransactionList *t; |
| 156 |
|
| 157 |
FILE *fp = fopen(csvfile.c_str(), "r"); |
| 158 |
if (!fp) { |
| 159 |
Application->MessageBox("CSV�t�@�C�����J��������", "�G���[", MB_OK); |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
// CSV �t�@�C�������� |
| 164 |
Account *ac; |
| 165 |
t = acs->ReadFile(fp, &ac); |
| 166 |
fclose(fp); |
| 167 |
|
| 168 |
if (!t) return; |
| 169 |
|
| 170 |
// OFX �t�@�C���������o�� |
| 171 |
fp = fopen(ofxfile.c_str(), "wb"); |
| 172 |
if (!fp) { |
| 173 |
Application->MessageBox("OFX�t�@�C�����J��������", "�G���[", MB_OK); |
| 174 |
return; |
| 175 |
} |
| 176 |
WriteOfx(fp, t, ac); |
| 177 |
fclose(fp); |
| 178 |
|
| 179 |
// Money �N�� |
| 180 |
ShellExecute(NULL, "open", ofxfile.c_str(), |
| 181 |
NULL, NULL, SW_SHOW); |
| 182 |
} |
| 183 |
|