~fuzzgun/fin/trunk

« back to all changes in this revision

Viewing changes to src/summary.c

  • Committer: Bob Mottram
  • Date: 2012-10-06 23:03:24 UTC
  • Revision ID: fuzzgun@gmail.com-20121006230324-fzi3u51j1v153boz
Adjustments

Show diffs side-by-side

added added

removed removed

Lines of Context:
302
302
                        }
303
303
 
304
304
#ifdef USE_COLOURS
305
 
                        if (column_separator==' ') {
306
 
                                if (atof((char*)sqlite3_column_text(stmt,2))>0) {
307
 
                                        printf("%s",COLOUR_POSITIVE);
 
305
                        if ((column_separator==' ') && (cols>3)) {
 
306
                                if ((char*)sqlite3_column_text(stmt,2)!=NULL) {
 
307
                                        if (atof((char*)sqlite3_column_text(stmt,2))>0) {
 
308
                                                printf("%s",COLOUR_POSITIVE);
 
309
                                        }
308
310
                                }
309
 
                                if (atof((char*)sqlite3_column_text(stmt,3))>0) {
310
 
                                        printf("%s",COLOUR_NEGATIVE);
 
311
                                if ((char*)sqlite3_column_text(stmt,3)!=NULL) {
 
312
                                        if (atof((char*)sqlite3_column_text(stmt,3))>0) {
 
313
                                                printf("%s",COLOUR_NEGATIVE);
 
314
                                        }
311
315
                                }
312
316
                        }
313
317
#endif
481
485
        }
482
486
        return retval;
483
487
}
 
488
 
 
489
int show_recent_adjustments(char * account, int no_of_transactions,
 
490
                                                        char column_separator)
 
491
{
 
492
        int retval,col,cols,row,i;
 
493
        char database_filename[STRING_BLOCK];
 
494
        char receive_str[STRING_BLOCK];
 
495
        char spend_str[STRING_BLOCK], date_str[STRING_BLOCK];
 
496
        char query[] = "select transaction_number, date, receive, spend, " \
 
497
                "status, description, supplier, " \
 
498
                "adjustment_date, adjustment_type" \
 
499
                " from adjustments " \
 
500
                "order by adjustment_date desc;";
 
501
        char * date_format = settings_get_date_format();
 
502
        int start_column=43;
 
503
        sqlite3_stmt *stmt;
 
504
        sqlite3 *handle;
 
505
        char * str;
 
506
 
 
507
        if (account_exists(account)==0) {
 
508
                return 0;
 
509
        }
 
510
 
 
511
        /* if no account is specified use the default one */
 
512
        if (strlen(account)==0) {
 
513
                settings_set_account(get_text_from_identifier(SETTINGS_DEFAULT_CURRENT_ACCOUNT));
 
514
                if (strlen(get_text_from_identifier(SETTINGS_DEFAULT_CURRENT_ACCOUNT))==0) {
 
515
                        printf("Unable to get default account name\n");
 
516
                        return 0;
 
517
                }
 
518
                account = settings_get_account();
 
519
        }
 
520
 
 
521
        if (strlen(account)==0) {
 
522
                printf("No account specified\n");
 
523
                return 0;
 
524
        }
 
525
        database_name(account, (char*)database_filename);
 
526
        retval = sqlite3_open_v2((char*)database_filename,
 
527
                                                         &handle,SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE,
 
528
                                                         NULL);
 
529
        /* If connection failed, handle returns NULL */
 
530
        if (retval) {
 
531
                printf("Sqlite3 database connection failed\n");
 
532
                sqlite3_close(handle);
 
533
                return 0;
 
534
        }
 
535
 
 
536
        retval = sqlite3_prepare_v2(handle,query,-1,&stmt,0);
 
537
        if(retval) {
 
538
                printf("Selecting recent adjustments from %s account failed\n",
 
539
                           account);
 
540
                sqlite3_close(handle);
 
541
                return 0;
 
542
        }
 
543
    
 
544
        cols = sqlite3_column_count(stmt);
 
545
 
 
546
        for (i=0;i<MAX_ROW_LENGTH;i++) {
 
547
                printf("-");
 
548
        }
 
549
        printf("\n");
 
550
 
 
551
        /* title */
 
552
        if (column_separator!=' ') {
 
553
                printf("%c",column_separator);
 
554
        }
 
555
        printf(get_text_from_identifier(TITLE_ADJUSTMENTS),
 
556
                   column_separator,column_separator,column_separator,
 
557
                   column_separator,column_separator,column_separator,
 
558
                   column_separator);
 
559
        printf("\n");
 
560
        if (column_separator!=' ') {
 
561
                printf("%c",column_separator);
 
562
        }
 
563
        for (i=0;i<MAX_ROW_LENGTH;i++) {
 
564
                if ((column_separator==' ') ||
 
565
                        ((i!=1) && (i!=13) && (i!=23) && (i!=33) && (i!=start_column))) {
 
566
                        printf("-");
 
567
                }
 
568
                else {
 
569
                        printf("+");
 
570
                }
 
571
        }
 
572
        if (column_separator!=' ') {
 
573
                printf("%c",column_separator);
 
574
        }
 
575
        printf("\n");
 
576
 
 
577
        row=0;
 
578
        while(1) {
 
579
                retval = sqlite3_step(stmt);
 
580
        
 
581
                if(retval == SQLITE_ROW) {
 
582
                        if (column_separator!=' ') {
 
583
                                printf("%c",column_separator);
 
584
                        }
 
585
 
 
586
                        if ((char*)sqlite3_column_text(stmt,8)!=NULL) {
 
587
                                printf("%s%c",
 
588
                                           (char*)sqlite3_column_text(stmt,8),
 
589
                                           column_separator);
 
590
                        }
 
591
                        else {
 
592
                                printf("-%c",column_separator);
 
593
                        }
 
594
 
 
595
#ifdef USE_COLOURS
 
596
                        if (column_separator==' ') {
 
597
                                str = (char*)sqlite3_column_text(stmt,8);
 
598
                                if (str!=NULL) {
 
599
                                        if (str[0]==ADJUSTMENT_TYPE_BEFORE) {
 
600
                                                printf("%s",COLOUR_BEFORE);
 
601
                                        }
 
602
                                        if (str[0]==ADJUSTMENT_TYPE_AFTER) {
 
603
                                                printf("%s",COLOUR_AFTER);
 
604
                                        }
 
605
                                        if (str[0]==ADJUSTMENT_TYPE_DELETE) {
 
606
                                                printf("%s",COLOUR_DELETE);
 
607
                                        }
 
608
                                }
 
609
                        }
 
610
#endif
 
611
 
 
612
                        for(col=1;col<cols-2;col++) {
 
613
                                char * val = (char*)sqlite3_column_text(stmt,col);
 
614
                                if (col==2) {
 
615
                                        pad_value(val, (char *)receive_str,
 
616
                                                          LEADING_SPACES, TRAILING_ZEROS);
 
617
                                        printf("%s %c",receive_str,column_separator);
 
618
                                }
 
619
                                else {
 
620
                                        if (col==3) {
 
621
                                                pad_value(val, (char *)spend_str,
 
622
                                                                  LEADING_SPACES, TRAILING_ZEROS);
 
623
                                                printf("%s %c",spend_str,column_separator);
 
624
                                        }
 
625
                                        else {
 
626
                                                if (col==4) {
 
627
                                                        /* status */
 
628
                                                        pad_text(val,8);
 
629
                                                }
 
630
                                                if (col<cols-2) {
 
631
                                                        if (col==1) {
 
632
                                                                convert_standard_to_date(val,
 
633
                                                                                                                 date_format,
 
634
                                                                                                                 date_str);
 
635
                                                                printf("%s %c",date_str,column_separator);
 
636
                                                        }
 
637
                                                        else {
 
638
                                                                printf("%s %c",val,column_separator);
 
639
                                                        }
 
640
                                                }
 
641
                                                else {
 
642
                                                        if (col < cols-1) {
 
643
                                                                description_multiline(val,
 
644
                                                                                                          column_separator,
 
645
                                                                                                          start_column);
 
646
                                                        }
 
647
                                                        else {   
 
648
                                                                if (strlen(val)>0) {
 
649
                                                                        printf("(%s)%c",val,column_separator);
 
650
                                                                }
 
651
                                                        }
 
652
 
 
653
                                                }
 
654
                                        }
 
655
                                }  
 
656
                        }
 
657
 
 
658
#ifdef USE_COLOURS
 
659
                        if (column_separator==' ') {
 
660
                                printf("%s",NORMAL);
 
661
                        }
 
662
#endif
 
663
                        printf("\n");
 
664
                        row++;
 
665
                        if (row>=no_of_transactions) break;
 
666
                }
 
667
                else if(retval == SQLITE_DONE) {
 
668
                        break;
 
669
                }
 
670
                else {
 
671
                        printf("Some error encountered while running " \
 
672
                                   "adjustments summary query\n");
 
673
                        sqlite3_close(handle);
 
674
                        return 0;
 
675
                }
 
676
        }
 
677
 
 
678
        sqlite3_close(handle);
 
679
        return 1;
 
680
}