Adapted to the changed format of the bank CSV files.
[account.git] / account.cpp
index ea071d6..171caaa 100644 (file)
@@ -58,8 +58,7 @@ std::ostream &operator<<(std::ostream &s, amount a)
       s << "           ";
    else
    {
-      //s << form("%8d.%02d",a / 100, a % 100);
-      s << std::setw(8) << a / 100 << ".";
+      s << std::setw(8) << std::setfill(' ') << a / 100 << ".";
       s << std::setw(2) << std::setfill('0') << a % 100;
       s << std::setw(0) << std::setfill(' ');
    }
@@ -91,11 +90,7 @@ std::ostream &operator<<(std::ostream &s, balance b)
 
 std::istream &operator>>(std::istream &s, Booking &b)
 {
-   char cmnt[128];
-
    s >> b.Bnumber >> b.booked;
-   //s.get(cmnt, 128);
-   //b.comment = cmnt;
    s >> b.comment;
 
    return s;
@@ -103,8 +98,6 @@ std::istream &operator>>(std::istream &s, Booking &b)
 
 std::ostream &operator<<(std::ostream &s, Booking &b)
 {
-   //s << form("#%03d ",b.Bnumber) << b.booked << " " << b.comment;
-   //s << form("%.*s", 58 - ~b.comment, "                                                              ");
    s << "#" << std::setw(4) << std::setfill('0') << b.Bnumber;
    s << std::setw(0) << std::setfill(' ');
    s << " " << b.booked << " ";
@@ -118,7 +111,6 @@ std::istream &operator>>(std::istream &s, Mutation &m)
 {
    int   nr_blanks = 0;
    char  c;
-   char  buf[128];
 
    while (s.get(c), c == ' ')
       nr_blanks++;
@@ -127,10 +119,6 @@ std::istream &operator>>(std::istream &s, Mutation &m)
    s >> m.account;
 
    amount  a;
-   //s.get(buf, 128);
-
-   //std::istringstream amnt(buf+strlen(buf)-10);
-   //amnt >> a;
 
    String amnt;
    int    p;
@@ -171,7 +159,7 @@ std::ostream &operator<<(std::ostream &s, Mutation &m)
 
 std::istream &operator>>(std::istream &s, Account &acc)
 {
-   char kind, name[128];
+   char kind;
 
    s >> acc.Anumber >> kind >> std::ws;
    if (kind == 'B')
@@ -179,9 +167,6 @@ std::istream &operator>>(std::istream &s, Account &acc)
    else
       acc.kind = COST;
 
-   //s.get(name, 128);
-   //acc.name = name;
-
    s >> acc.name;
 
    return s;
@@ -191,7 +176,6 @@ std::ostream &operator<<(std::ostream &s, Account &acc)
 {
    s << acc.Anumber << " " << std::setw(44) << std::left << acc.name;
    s << std::setw(0) << std::right << " ";
-   //s << form("%.*s", 45 - ~acc.name, "                                                   ");
    s << acc.Bal;
 
    return s;
@@ -263,115 +247,130 @@ void PostScript_balans_layout(PostScript &ps, String header)
 
 void Ledger::accounts_report(const char * filename)
 {
-   PostScript ps(filename);
-   int        i;
+   try
+   {
+      PostScript ps(filename);
+      int        i;
 
-   for (i=0; i<nr_accs; i++)
+      for (i=0; i<nr_accs; i++)
+      {
+         accs[i].Postscript_sheet(ps);
+      }
+   }
+   catch (psexception &e)
    {
-      accs[i].Postscript_sheet(ps);
+      std::cerr << "Error creating postscript output: " << e.what() << "\n";
    }
 }
 
 void Ledger::saldi_report(const char * filename, date period_begin, date period_end)
 {
-   PostScript ps(filename);
-   balance total, saldi;
-   balance EndBalance, Result;
-   int        i;
-   float      y = 572;
-   int        row = 0;
+   try
+   {
+      PostScript ps(filename);
+      balance total, saldi;
+      balance EndBalance, Result;
+      int        i;
+      float      y = 572;
+      int        row = 0;
 
-   String     header("Saldibalans");
+      String     header("Saldibalans");
 
-   header += " van " + period_begin.format("%e %B %Y") + " tot " + period_end.format("%e %B %Y");
+      header += " " + companyname;
+      header += " van " + period_begin.format("%e %B %Y") + " tot " + period_end.format("%e %B %Y");
 
-   ps.NewPage(842, 595);
-   ps.FontSize(6);
+      ps.NewPage(842, 595);
+      ps.FontSize(6);
 
 
-   for (i=0; i<nr_accs; i++)
-   {
-      balance saldo(accs[i]);
-
-      if (saldo.debit() != 0 || saldo.credit() != 0)
+      for (i=0; i<nr_accs; i++)
       {
-         if (row == 64)
-         {
-            ps.FontSize(9);
-            PostScript_balans_layout(ps, header);
-            ps.NewPage(842, 595);
-            ps.FontSize(6);
-            y = 572;
-            row = 0;
-         }
+         balance saldo(accs[i]);
 
-         if (row % 2 == 1)
+         if (saldo.debit() != 0 || saldo.credit() != 0)
          {
-            ps.Rectangle(16, y-2, 739, 7);
-         }
+            if (row == 64)
+            {
+               ps.FontSize(9);
+               PostScript_balans_layout(ps, header);
+               ps.NewPage(842, 595);
+               ps.FontSize(6);
+               y = 572;
+               row = 0;
+            }
 
-         total += accs[i];
-         saldi += ~balance(accs[i]);
+            if (row % 2 == 1)
+            {
+               ps.Rectangle(16, y-2, 739, 7);
+            }
 
-         ps.Text(20, y, String(accs[i].Number()));
-         ps.Text(50, y, accs[i].Name());
+            total += accs[i];
+            saldi += ~balance(accs[i]);
 
-         balance saldo(accs[i]);
+            ps.Text(20, y, String(accs[i].Number()));
+            ps.Text(50, y, accs[i].Name());
 
-         ps.Text(200, y, saldo.debit().to_string());
-         ps.Text(270, y, saldo.credit().to_string());
-         saldo = ~saldo;
-         ps.Text(340, y, saldo.debit().to_string());
-         ps.Text(410, y, saldo.credit().to_string());
+            balance saldo(accs[i]);
 
-         if (accs[i].BAL())
-         {
-            EndBalance += ~balance(accs[i]);
+            ps.Text(200, y, saldo.debit().to_string());
+            ps.Text(270, y, saldo.credit().to_string());
+            saldo = ~saldo;
+            ps.Text(340, y, saldo.debit().to_string());
+            ps.Text(410, y, saldo.credit().to_string());
 
-            ps.Text(620, y, saldo.debit().to_string());
-            ps.Text(690, y, saldo.credit().to_string());
-         }
-         else
-         {
-            Result += ~balance(accs[i]);
-
-            ps.Text(480, y, saldo.debit().to_string());
-            ps.Text(550, y, saldo.credit().to_string());
-         }
+            if (accs[i].BAL())
+            {
+               EndBalance += ~balance(accs[i]);
 
-         y -= 7.0;
-         row ++;
-      }
-   }
+               ps.Text(620, y, saldo.debit().to_string());
+               ps.Text(690, y, saldo.credit().to_string());
+            }
+            else
+            {
+               Result += ~balance(accs[i]);
 
-   ps.FontSize(9);
-   PostScript_balans_layout(ps, header);
+               ps.Text(480, y, saldo.debit().to_string());
+               ps.Text(550, y, saldo.credit().to_string());
+            }
 
-   balance profit;
-   profit = ~Result;
-   Result += -profit;
-   EndBalance += profit;
+            y -= 7.0;
+            row ++;
+         }
+      }
 
-   ps.LineAttributes(0.5, 1.0, 0.0);
-   ps.Line(16, 60, 755, 60);
-   y = 52;
-   ps.Text(50, y, "Resultaat");
-   ps.Text(480, y, (-profit).debit().to_string());
-   ps.Text(550, y, (-profit).credit().to_string());
-   ps.Text(620, y, profit.debit().to_string());
-   ps.Text(690, y, profit.credit().to_string());
-
-   ps.Line(16, 50, 755, 50);
-   y = 42;
-   ps.Text(50, y, "Totaal");
-   ps.Text(200, y, total.debit().to_string());
-   ps.Text(270, y, total.credit().to_string());
-   ps.Text(340, y, saldi.debit().to_string());
-   ps.Text(410, y, saldi.credit().to_string());
-   ps.Text(480, y, Result.debit().to_string());
-   ps.Text(550, y, Result.credit().to_string());
-   ps.Text(620, y, EndBalance.debit().to_string());
-   ps.Text(690, y, EndBalance.credit().to_string());
+      ps.FontSize(9);
+      PostScript_balans_layout(ps, header);
+
+      balance profit;
+      profit = ~Result;
+      Result += -profit;
+      EndBalance += profit;
+
+      ps.LineAttributes(0.5, 1.0, 0.0);
+      ps.Line(16, 60, 755, 60);
+      y = 52;
+      ps.Text(50, y, "Resultaat");
+      ps.Text(480, y, (-profit).debit().to_string());
+      ps.Text(550, y, (-profit).credit().to_string());
+      ps.Text(620, y, profit.debit().to_string());
+      ps.Text(690, y, profit.credit().to_string());
+
+      ps.Line(16, 50, 755, 50);
+      y = 42;
+      ps.Text(50, y, "Totaal");
+      ps.Text(200, y, total.debit().to_string());
+      ps.Text(270, y, total.credit().to_string());
+      ps.Text(340, y, saldi.debit().to_string());
+      ps.Text(410, y, saldi.credit().to_string());
+      ps.Text(480, y, Result.debit().to_string());
+      ps.Text(550, y, Result.credit().to_string());
+      ps.Text(620, y, EndBalance.debit().to_string());
+      ps.Text(690, y, EndBalance.credit().to_string());
+   }
+   catch (psexception &e)
+   {
+      std::cerr << "Error creating postscript output: " << e.what() << "\n";
+   }
 }
 
 void Ledger::XML_saldi(std::ostream &s, date period_begin, date period_end)
@@ -501,23 +500,11 @@ void Account::Postscript_sheet(PostScript &ps)
 
          y -= 10;
          row++;
-         //s << b[B] << "|" << balance(m[M]) << " |\n";
       }
    }
 
    /*  Print the footer with the totals  */
 
-#if 0
-   s << "                 SALDO                                      ";
-   s << "               |" << -~total << " |\n";
-   s << "----------------------------------------------------------------";
-   s << "-----------+-------------+-----------+\n";
-   total += -~total;
-   s << "                 TOTAAL                                       ";
-   s << "             |" << total << " |\n";
-   s << "----------------------------------------------------------------";
-   s << "-----------+-------------+-----------+\n";
-#endif
 
    if (page_started)
    {
@@ -586,6 +573,7 @@ void Ledger::read(char *filename)
    nr_accs = 0;
    while (in)
    {
+      line_begin = '\n';
       in.get(line_begin);
       if (isdigit(line_begin))
       {