This file is indexed.

/usr/share/perl5/Finance/QIF.pm is in libfinance-qif-perl 3.02-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
package Finance::QIF;

use 5.006;
use strict;
use warnings;
use Carp;
use IO::File;

our $VERSION = '3.02';
$VERSION = eval $VERSION;

my %noninvestment = (
    "D" => "date",
    "T" => "transaction",
    "U" => "total",         #Quicken 2005 added this which is usually the same
                            #as T but can sometimes be higher.
    "C" => "status",
    "N" => "number",
    "P" => "payee",
    "M" => "memo",
    "A" => "address",
    "L" => "category",
    "S" => "splits"
);

my %split = (
    "S" => "category",
    "E" => "memo",
    '$' => "amount"
);

my %investment = (
    "D" => "date",
    "N" => "action",
    "Y" => "security",
    "I" => "price",
    "Q" => "quantity",
    "T" => "transaction",
    "U" => "total",         #Quicken 2005 added this which is usually the same
                            #as T but can sometimes be higher.
    "C" => "status",
    "P" => "text",
    "M" => "memo",
    "O" => "commission",
    "L" => "account",
    '$' => "amount"
);

my %account = (
    "N" => "name",
    "D" => "description",
    "L" => "limit",
    "X" => "tax",
    "A" => "note",
    "T" => "type",
    "B" => "balance"
);

my %category = (
    "N" => "name",
    "D" => "description",
    "B" => "budget",
    "E" => "expense",
    "I" => "income",
    "T" => "tax",
    "R" => "schedule"
);

my %class = (
    "N" => "name",
    "D" => "description"
);

my %memorized = (
    "K" => "type",
    "T" => "transaction",
    "U" => "total",        #Quicken 2005 added this which is usually the same as
                           #as T but can sometimes be higher.
    "C" => "status",
    "P" => "payee",
    "M" => "memo",
    "A" => "address",
    "L" => "category",
    "S" => "splits",
    "N" => "action",       #Quicken 2006 added N, Y, I, Q, $ for investment
    "Y" => "security",
    "I" => "price",
    "Q" => "quantity",
    '$' => "amount",
    "1" => "first",
    "2" => "years",
    "3" => "made",
    "4" => "periods",
    "5" => "interest",
    "6" => "balance",
    "7" => "loan"
);

my %security = (
    "N" => "security",
    "S" => "symbol",
    "T" => "type",
    "G" => "goal",
);

my %budget = (
    "N" => "name",
    "D" => "description",
    "E" => "expense",
    "I" => "income",
    "T" => "tax",
    "R" => "schedule",
    "B" => "budget"
);

my %payee = (
    "P" => "name",
    "A" => "address",
    "C" => "city",
    "S" => "state",
    "Z" => "zip",
    "Y" => "country",
    "N" => "phone",
    "#" => "account"
);

my %prices = (
    "S" => "symbol",
    "P" => "price"
);

my %price = (
    "C" => "close",
    "D" => "date",
    "X" => "max",
    "I" => "min",
    "V" => "volume"
);

my %nofields = ();

my %header = (
    "Type:Bank"         => \%noninvestment,
    "Type:Cash"         => \%noninvestment,
    "Type:CCard"        => \%noninvestment,
    "Type:Invst"        => \%investment,
    "Type:Oth A"        => \%noninvestment,
    "Type:Oth L"        => \%noninvestment,
    "Account"           => \%account,
    "Type:Cat"          => \%category,
    "Type:Class"        => \%class,
    "Type:Memorized"    => \%memorized,
    "Type:Security"     => \%security,
    "Type:Budget"       => \%budget,
    "Type:Payee"        => \%payee,
    "Type:Prices"       => \%prices,
    "Option:AutoSwitch" => \%nofields,
    "Option:AllXfr"     => \%nofields,
    "Clear:AutoSwitch"  => \%nofields
);

sub new {
    my $class = shift;
    my %opt   = @_;
    my $self  = {};

    $self->{debug}            = $opt{debug}            || 0;
    $self->{autodetect}       = $opt{autodetect}       || 0;
    $self->{trim_white_space} = $opt{trim_white_space} || 0;
    $self->{record_separator} = $opt{record_separator} || $/;

    bless( $self, $class );

    if ( $opt{file} ) {
        $self->file( $opt{file} );
        $self->open;
    }
    return $self;
}

sub file {
    my $self = shift;
    if (@_) {
        my @file = ( ref( $_[0] ) eq "ARRAY" ? @{ shift @_ } : (), @_ );
        $self->{file} = [@file];
    }
    if ( $self->{file} ) {
        return wantarray ? @{ $self->{file} } : $self->{file}->[0];
    }
    else {
        return undef;
    }
}

sub record_separator {
    my $self = shift;
    return $self->{record_separator};
}

sub _filehandle {
    my $self = shift;
    if (@_) {
        my @args = @_;
        $self->{_filehandle} = IO::File->new(@args)
          or croak("Failed to open file '$args[0]': $!");
        binmode( $self->{_filehandle} );
        $self->{_linecount} = 0;
    }
    if ( !$self->{_filehandle} ) {
        croak("No filehandle available");
    }
    return $self->{_filehandle};
}

sub open {
    my $self = shift;
    if (@_) {
        $self->file(@_);
    }
    if ( $self->file ) {
        $self->_filehandle( $self->file );
        if ( $self->{autodetect} ) {
            if ( $self->_filehandle->seek( -2, 2 ) ) {
                my $buffer = "";
                $self->_filehandle->read( $buffer, 2 );
                if ( $buffer eq "\015\012" ) {
                    $self->{record_separator} = "\015\012";
                }
                elsif ( $buffer =~ /\012$/ ) {
                    $self->{record_separator} = "\012";
                }
                elsif ( $buffer =~ /\015$/ ) {
                    $self->{record_separator} = "\015";
                }
            }
        }
        $self->reset();
    }
    else {
        croak("No file specified");
    }
}

sub next {
    my $self = shift;
    my %object;
    my $continue = 1;
    my $csplit;    # Need to keep track of current split for adding split values
    if ( $self->_filehandle->eof ) {
        return undef;
    }
    if ( exists( $self->{header} ) ) {
        $object{header} = $self->{header};
    }
    while ( !$self->_filehandle->eof && $continue ) {
        my $line = $self->_getline;
        next if ( $line =~ /^\s*$/ );
        my ( $field, $value ) = $self->_parseline($line);
        if ( $field eq '!' ) {
            $value =~ s/\s+$//;    # Headers sometimes have trailing white space
            $self->{header} = $value;
            $object{header} = $value;
            if ( !exists( $header{$value} ) ) {
                $self->_warning("Unknown header format '$value'");
            }
        }
        else {
            if ( $field eq '^' ) {
                $continue = 0;
            }
            else {
                if (
                    !exists( $header{ $object{header} } )
                    && !(
                        exists( $header{"split"} )
                        && (   $object{header} eq "noninvestment"
                            || $object{header} eq "memorized" )
                    )
                  )
                {
                    $self->_warning(
                        "Unknown header '$object{header}' can't process line");
                }
                elsif ( $object{header} eq "Type:Prices" ) {
                    $object{"symbol"} = $field;
                    push( @{ $object{"prices"} }, $value );
                }
                elsif ($field eq 'A'
                    && $header{ $object{header} }{$field} eq "address" )
                {
                    if ( $self->{header} eq "Type:Payee" ) {

                        # The address fields are numbered for this record type
                        if ( length($value) == 0 ) {
                            $self->_warning( 'Improper address record for '
                                  . 'this record type' );
                        }
                        else {
                            $value = substr( $value, 1 );
                        }
                    }
                    if ( exists( $object{ $header{ $object{header} }{$field} } )
                        && $object{ $header{ $object{header} }{$field} } ne "" )
                    {
                        $object{ $header{ $object{header} }{$field} } .= "\n";
                    }
                    $object{ $header{ $object{header} }{$field} } .= $value;
                }
                elsif ($field eq 'S'
                    && $header{ $object{header} }{$field} eq "splits" )
                {
                    my %mysplit;    # We assume "S" always appears first
                    $mysplit{ $split{$field} } = $value;
                    push( @{ $object{splits} }, \%mysplit );
                    $csplit = \%mysplit;
                }
                elsif ( ( $field eq 'E' || $field eq '$' ) && $csplit ) {

                    # this currently assumes the "S" was found first
                    $csplit->{ $split{$field} } = $value;
                }
                elsif ($field eq 'B'
                    && $header{ $object{header} }{$field} eq "budget" )
                {
                    push( @{ $object{budget} }, $value );
                }
                elsif ( exists( $header{ $object{header} }{$field} ) ) {
                    $object{ $header{ $object{header} }{$field} } = $value;
                }
                else {
                    $self->_warning("Unknown field '$field'");
                }
            }
        }
    }

    # Must check that we have a valid record to return
    if ( scalar( keys %object ) > 1 ) {
        return \%object;
    }
    else {
        return undef;
    }
}

sub _parseline {
    my $self = shift;
    my $line = shift;
    my @result;
    if (   $line !~ /^!/
        && exists( $self->{header} )
        && $self->{header} eq "Type:Prices" )
    {
        my %price;
        $line =~ s/\"//g;
        my @data = split( ",", $line );
        $result[0]      = $data[0];
        $price{"close"} = $data[1];
        $price{"date"}  = $data[2];
        if ( scalar(@data) > 3 ) {
            $price{"max"}    = $data[3];
            $price{"min"}    = $data[4];
            $price{"volume"} = $data[5];
        }
        $result[1] = \%price;
    }
    else {
        $result[0] = substr( $line, 0, 1 );
        $result[1] = substr( $line, 1 );
        if ( $self->{trim_white_space} ) {
            $result[1] =~ s/^\s*(.*?)\s*$/$1/;
        }
    }
    return @result;
}

sub _getline {
    my $self = shift;
    local $/ = $self->record_separator;
    my $line = $self->_filehandle->getline;
    chomp($line);
    $self->{_linecount}++;
    return $line;
}

sub _warning {
    my $self    = shift;
    my $message = shift;
    carp(   $message
          . " in file '"
          . $self->file
          . "' line "
          . $self->{_linecount} );
}

sub header {
    my $self   = shift;
    my $header = shift;
    my $fh     = $self->_filehandle;
    local $\ = $self->{record_separator};
    print( $fh "!", $header );

    # used during write to validate passed record is appropriate for
    # current header also generate reverse lookup for mapping record
    # values to file key identifier.
    $self->{currentheader} = $header;
    foreach my $key ( keys %{ $header{$header} } ) {
        $self->{reversemap}{ $header{$header}{$key} } = $key;
    }
    if ( exists( $header{$header}{S} ) && $header{$header}{S} eq "splits" ) {
        foreach my $key ( keys %split ) {
            $self->{reversesplitsmap}{ $split{$key} } = $key;
        }
    }

    $self->{_linecount}++;
    if ( !exists( $header{$header} ) ) {
        $self->_warning("Unsupported header '$header' written to file");
    }
}

sub write {
    my $self   = shift;
    my $record = shift;
    if ( $record->{header} eq $self->{currentheader} ) {
        if ( $record->{header} eq "Type:Prices" ) {
            if ( exists( $record->{symbol} ) && exists( $record->{prices} ) ) {
                foreach my $price ( @{ $record->{prices} } ) {
                    if (   exists( $price->{close} )
                        && exists( $price->{date} )
                        && exists( $price->{max} )
                        && exists( $price->{min} )
                        && exists( $price->{volume} ) )
                    {
                        $self->_writeline(
                            join( ",",
                                '"' . $record->{symbol} . '"',
                                $price->{close},
                                '"' . $price->{date} . '"',
                                $price->{max},
                                $price->{min},
                                $price->{volume} )
                        );
                    }
                    elsif (exists( $price->{close} )
                        && exists( $price->{date} ) )
                    {
                        $self->_writeline(
                            join( ",",
                                '"' . $record->{symbol} . '"',
                                $price->{close},
                                '"' . $price->{date} . '"' )
                        );
                    }
                    else {
                        $self->_warning("Prices missing a required field");
                    }
                }
                $self->_writeline("^");
            }
            else {
                $self->_warning("Record missing 'symbol' or 'prices'");
            }
        }
        else {
            foreach my $value ( keys %{$record} ) {
                next
                  if (
                       $value eq "header"
                    || $value eq "splits"
                    || (   $self->{currentheader} eq "Type:Memorized"
                        && $value eq "transaction" )
                  );
                if ( exists( $self->{reversemap}{$value} ) ) {
                    if ( $value eq "address" ) {
                        my @lines = split( "\n", $record->{$value} );
                        if ( $self->{currentheader} eq "Type:Payee" ) {

                          # The address fields are numbered for this record type
                            for ( my $count = 0 ; $count < 3 ; $count++ ) {
                                if ( $count <= $#lines ) {
                                    $self->_writeline( "A", $count,
                                        $lines[$count] );
                                }
                                else {
                                    $self->_writeline( "A", $count );
                                }
                            }
                        }
                        else {
                            for ( my $count = 0 ; $count < 6 ; $count++ ) {
                                if ( $count <= $#lines ) {
                                    $self->_writeline( "A", $lines[$count] );
                                }
                                else {
                                    $self->_writeline("A");
                                }
                            }
                        }
                    }
                    elsif ( $value eq "budget" ) {
                        foreach my $amount ( @{ $record->{$value} } ) {
                            $self->_writeline( $self->{reversemap}{$value},
                                $amount );
                        }
                    }
                    else {
                        $self->_writeline( $self->{reversemap}{$value},
                            $record->{$value} );
                    }
                }
                else {
                    $self->_warning( "Unsupported field '$value'"
                          . " found in record ignored" );
                }
            }
            if ( exists( $record->{splits} ) ) {
                foreach my $s ( @{ $record->{splits} } ) {
                    foreach my $key ( 'category', 'memo', 'amount' ) {
                        if ( exists( $s->{$key} ) ) {
                            $self->_writeline( $self->{reversesplitsmap}{$key},
                                $s->{$key} );
                        }
                        else {
                            $self->_writeline(
                                $self->{reversesplitsmap}{$key} );
                        }
                    }
                }
            }
            if ( $self->{currentheader} eq "Type:Memorized"
                && exists( $record->{transaction} ) )
            {
                $self->_writeline( $self->{reversemap}{"transaction"},
                    $record->{"transaction"} );
            }
            $self->_writeline("^");
        }
    }
    else {
        $self->_warning( "Record header type '"
              . $record->{header}
              . "' does not match current output header type "
              . $self->{currentheader}
              . "." );
    }
}

sub _writeline {
    my $self = shift;
    my $fh   = $self->_filehandle;
    local $\ = $self->{record_separator};
    print( $fh @_ );
    $self->{_linecount}++;
}

sub reset {
    my $self = shift;
    map( $self->{$_} = undef,    # initialize internally used variables
        qw(_linecount header currentheader reversemap reversesplitsmap) );
    $self->_filehandle->seek( 0, 0 );
}

sub close {
    my $self = shift;
    $self->_filehandle->close;
}

1;

__END__

=head1 NAME

Finance::QIF - Parse and create Quicken Interchange Format files

=head1 SYNOPSIS

  use Finance::QIF;
  
  my $qif = Finance::QIF->new( file => "test.qif" );
  
  while ( my $record = $qif->next ) {
      print( "Header: ", $record->{header}, "\n" );
      foreach my $key ( keys %{$record} ) {
          next
            if ( $key eq "header"
              || $key eq "splits"
              || $key eq "budget"
              || $key eq "prices" );
          print( "     ", $key, ": ", $record->{$key}, "\n" );
      }
      if ( exists( $record->{splits} ) ) {
          foreach my $split ( @{ $record->{splits} } ) {
              foreach my $key ( keys %{$split} ) {
                  print( "     Split: ", $key, ": ", $split->{$key}, "\n" );
              }
          }
      }
      if ( exists( $record->{budget} ) ) {
          print("     Budget: ");
          foreach my $amount ( @{ $record->{budget} } ) {
              print( " ", $amount );
          }
          print("\n");
      }
      if ( exists( $record->{prices} ) ) {
          print("     Date     Close   Max     Min     Volume\n");
          $format = "     %8s %7.2f %7.2f %7.2f %-8d\n";
          foreach my $price ( @{ $record->{prices} } ) {
              printf( $format,
                  $price->{"date"}, $price->{"close"}, $price->{"max"},
                  $price->{"min"},  $price->{"volume"} );
          }
      }
  }

=head1 DESCRIPTION

Finance::QIF is a module for working with QIF (Quicken Interchange
Format) files in Perl.  This module reads QIF data records from a file
passing each successive record to the caller for processing.  This
module also has the capability of writing QIF records to a file.

The QIF file format typically consists of a header containing a record
or transaction type, followed by associated data records.  Within a
file there may be multiple headers.  Headers are usually followed by
data records, however data is not required to always follow a header.

A hash reference is returned for each record read from a file.  The
hash will have a "header" value which contains the header type that
was read along with all supported values found for that record.  If a
value is not specified in the data file, the value will not exist in
this hash.

No processing or validation is done on values found in files or data
structures to try and convert them into appropriate types and formats.
It is expected that users of this module or extensions to this module
will do any additional processing or validation as required.

=head2 RECORD TYPES & VALUES

The following record types are currently supported by this module:

=over

=item Type:Bank, Type:Cash, Type:CCard, Type:Oth A, Type:Oth L

These are non investment ledger transactions.  All of these record
types support the following values.

=over

=item date

Date of transaction.

=item transaction

Dollar amount of transaction.

=item total

Dollar amount of transaction. This is generally the same as transaction but
in some cases can be higher. (Introduced in Quicken 2005 for windows)

=item status

Reconciliation status of transaction.

=item number

Check number of transaction.

=item payee

Who the transaction was made to.

=item memo

Additional text describing the transaction.

=item address

Address of payee.

=item category

Category the transaction is assigned to.

=item splits

If the transaction contains splits this will be defined and consist of
an array of hash references.  With each split potentially having the
following values.

=over

=item category

Category the split is assigned to.

=item memo

Additional text describing the split.

=item amount

Dollar amount of split.

=back

=back

=item Type:Invst

This is for Investment ledger transactions.  The following values are
supported for this record type.

=over

=item date

Date of transaction.

=item action

Type of transaction like buy, sell, ...

=item security

Security name of transaction.

=item price

Price of security at time of transaction.

=item quantity

Number of shares purchased.

=item transaction

Cost of shares in transaction.

=item status

Reconciliation status of transaction.

=item text

Text for non security specific transaction.

=item memo

Additional text describing transaction.

=item commission

Commission fees related to transaction.

=item account

Account related to security specific transaction.

=item amount

Dollar amount of transaction.

=item total

Dollar amount of transaction. This is generally the same as amount but
in some cases can be higher. (Introduced in Quicken 2005 for windows)

=back

=item Account

This is a list of accounts.  In cases where it is used in a file by
first providing one account record followed by a investment or
non-investment record type and its transactions, it means that that
set of transactions is related to the specified account.  In other
cases it can just be a sequence of Account records.

Each account record supports the following values.

=over

=item name

Account name.

=item description

Account description.

=item limit

Account limit usually for credit card accounts that have some upper
limit over credit.

=item tax

Defined if the account is tax related.

=item note

Additional information about the account.

=item type

Type of account.

=item balance

Current balance of account.

=back

=item Type:Cat

This is a list of categories.  The following values are supported for
category records.

=over

=item name

Name of category.

=item description

Description of category.

=item budget

An array of 12 values Jan-Dec to represent the budget amount for each
month.

=item expense

Usually exists if the category is an expense account however this is
often a default assumed value and doesn't show up in files.

=item income

Exists if the category is an income account.

=item tax

Exists if this category is tax related.

=item schedule

If this category is tax related this specifies what tax schedule it is
related if defined.

=back

=item Type:Class

This is a list of classes.  The following values are supported for
class records.

=over

=item name

Name of class.

=item description

Description of class.

=back

=item Type:Memorized

This is a list of memorized transactions.  The following values are
supported for memorized transaction records.

=over

=item type

Type of memorized transaction "C" for check, "D" for deposit, "P" for
payment, "I" for investment, and "E" for electronic payee.

=item transaction

Dollar amount of transaction.

=item total

Dollar amount of transaction. This is generally the same as amount but
in some cases can be higher. (Introduced in Quicken 2005 for windows)

=item status

Reconciliation status of transaction.

=item payee

Who the transaction was made to.

=item memo

Additional text describing the transaction.

=item address

Address of payee.

=item category

Category the transaction is assigned to.

=item action

Type of investment transaction like buty, sell, ... (Inroduced in Quicken
2006 for windows)

=item security

Security name of transaction. (Inroduced in Quicken 2006 for windows)

=item price

Price of security. (Inroduced in Quicken 2006 for windows)

=item quantity

Quantity of security. (Inroduced in Quicken 2006 for windows)

=item amount

Dollar amount of transaction. (Introduced in Quicken 2006 for windows)

=item splits

If the transaction contains splits this will be defined and consist of
an array of hashes.  With each split potentially having the following
values.

=over

=item category

Category the split is assigned to.

=item memo

Additional text describing the split.

=item amount

Dollar amount of split.

=back

=item first

First payment date.

=item years

Total years for loan.

=item made

Number of payments already made.

=item periods

Number of periods per year.

=item interest

Interest rate of loan.

=item balance

Current loan balance.

=item loan

Original loan amount.

=back

=item Type:Security

This is a list of securities.  The following values are supported for
security records.

=over

=item security

Security name.

=item symbol

Security symbol.

=item type

Security type.

=item goal

Security goal.

=back

=item Type:Budget

This is a list of budget values for categories.  The following values
are supported for budget records.

=over

=item name

Category name of budgeted item.

=item description

Category Description of budgeted item.

=item expense

Usually exists if the category is an expense account however this is
often a default assumed value and doesn't show up in files.

=item income

Exists if the category is an income account.

=item tax

Exists if this category is tax related.

=item schedule

If this category is tax related this specifies what tax schedule it is
related if defined.

=item budget

An array of 12 values Jan-Dec to represent the budget amount for each
month.

=back

=item Type:Payee

This is a list online payee accounts.  The following values are
supported for online payee account records.

=over

=item name

Name of payees.

=item address

Address of payee.

=item city

City of payee.

=item state

State of payee

=item zip

Zipcode of payee.

=item country

Country of payee.

=item phone

Phone number of payee.

=item account

Account number for payee transaction.

=back

=item Type:Prices

This is a list of prices for a security.  The following values are
supported for security prices records.

=over

=item symbol

Security Symbol.

=item prices

An array of hashes.  With each hash having the following values.

=over

=item date

Date of security values.

=item close

Close value of security for the date.

=item max

Max value of security for the date.

=item min

Min value of security for the date.

=item volume

Number of shares of security exchanged for the date.

=back

=back

=item Option:AllXfr, Option:AutoSwitch, Clear:AutoSwitch

These record types aren't related to transactions but instead provided
ways to control how Quicken processes the QIF file.  They have no
impact on how this software operates and are ignored when found.

=back

Note: If this software finds unsupported record types or values in a
data file a warning will be generated containing information on what
unexpected value was found.

=head1 METHODS

=head2 new()

Creates a new instance of Finance::QIF.  Supports the following
initializing values.

  my $qif = Finance::QIF->new( file => "myfile", debug => 1 );

If the file is specified it will be opened on new.

=over

=item file

Specifies file to use for processing.  See L</file()> for details.

  my $in = Finance::QIF->new( file => "myfile" );
OR
  my $in = Finance::QIF->new( file => [ "myfile", "<:crlf" ] );

For output files, be sure to open the file in write mode.  For example:

  my $out = Finance::QIF->new( file => ">myfile" );

=item record_separator

Can be used to redefine the QIF record separator.  Default is $/.

  my $in = Finance::QIF->new( record_separator => "\012" );

Note: For MacOS X it will most likely be necessary to change this to
"\015".  Quicken on MacOS X generates files with "\015" as the separator
which is typical of Mac however the native perl in MacOS X is unix
based and uses the default unix separator which is "\012".  See
L</autodetect> for another option.

=item autodetect

Enable auto detection of the record separator based on the file
contents.  Default is "0".

  my $in = Finance::QIF->new( autodetect => 1 );

Perl uses $/ to define line separators for text files.  Perl sets this
value according to the OS perl is running on:

  Windows="\015\012"
  Mac="\015"
  Unix="\012"

In many cases you may find yourself with text files that do not match
the OS.  In these cases Finance::QIF by default will not process that
QIF file correctly. This feature is an attempt to help with the most
common cases of having the wrong text file for the OS Finance::QIF is
running on.

This feature depends on being able to seek to the end of the file and
reading the last 2 characters to determine the proper separator. If a
seek can not be performed or the last 2 characters are not a proper
separator the record_separator will default to $/ or the value passed
in. If a valid record_separator is found then it will be set according
to what was in the file.

This code requires a file use a consistent line separator. If you find
your self dealing with unusual files containing mixed separators you
need to first Normalize the file to a consistent separator.

Normalizing a text file to have a consistent line separator is done in
modules like  File::LocalizeNewlines or Template::Parser::LocalizeNewlines
so if you are having issues with trying to process poorly formated text
files look at preprocessing them with something like those before passing
on to Finance::QIF.

=item trim_white_space

Can be used to remove leading and trailing white space from values
returned. Default is "0".

  my $qif = Finance::QIF->new( trim_white_space => 1 );

=item debug

Can be used to output debug information.  Default is "0".

  my $qif = Finance::QIF->new( debug => 1 );

=back

=head2 file()

Specify file name and optionally additional parameters that will be
used to obtain a filehandle.  The argument can be a filename (SCALAR),
an ARRAY reference, or an ARRAY whose values must be valid arguments
for passing to IO::File->new.

  $qif->file( "myfile" );
 OR
  $qif->file( [ "myfile", "<:crlf" ] );
 OR
  $qif->file( "myfile", "<:crlf" );

For output files, be sure to open the file in write mode.

=head2 record_separator()

Returns the currently used record_separator.  This is used primarly in
situations where you open a QIF file with autodetect and then want to
write out a QIF file in the same format.

  my $in  = Finance::QIF->new( file => "input.qif", autodetect => 1 );
  my $out = Finance::QIF->new( file => ">write.qif",
                               record_separator => $in->record_separator );

=head2 open()

Open already specified file.

  $qif->open();

Open also supports the same arguments as L</file()>.

=head2 next()

For input files return the next record in the QIF file.

  my $record = $in->next();

Returns undef if no more records are available.

=head2 header()

For output files use to output the passed header for records that will
then be written with write.

  $out->header( "Type:Bank" );

See L<RECORD TYPES & VALUES> for list of possible record types that
can be passed.

=head2 write()

For output files use to output the passed record to the file.

  $out->write( $record );

=head2 reset()

Resets the filehandle so the records can be read again from the
beginning of the file.

  $qif->reset();

=head2 close()

Closes the open file.

  $qif->close();

=head1 EXAMPLES

Read an existing QIF file then write out to new QIF file.

  my $in  = Finance::QIF->new( file => "input.qif" );
  my $out = Finance::QIF->new( file => ">write.qif" );
  
  my $header = "";
  while ( my $record = $in->next() ) {
      if ( $header ne $record->{header} ) {
          $out->header( $record->{header} );
          $header = $record->{header};
      }
      $out->write($record);
  }
  
  $in->close();
  $out->close();

=head1 SEE ALSO

L<Carp>, L<IO::File>

Quicken Interchange Format (QIF) specification
L<http://web.intuit.com/support/quicken/docs/d_qif.html>

=head1 ACKNOWLEDGEMENTS

Simon Cozens C<simon@cpan.org>, Author of original Finance::QIF

Nathan McFarland C<nmcfarl@cpan.org>, Maintainer of original Finance::QIF

=head1 AUTHORS

Matthew McGillis E<lt>matthew@mcgillis.orgE<gt> L<http://www.mcgillis.org/>

Phil Lobbes E<lt>phil at perkpartners dot comE<gt>

Project maintained at L<http://sourceforge.net/projects/finance-qif>

=head1 COPYRIGHT

Copyright (C) 2006-2008 by Matthew McGillis.  All rights reserved.

=head1 LICENSE

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut