local kludge
[freeside.git] / httemplate / graph / money_time-graph.cgi
1 <%
2
3 #find first month
4 my $syear = 2001;
5 my $smonth = 8;
6
7 #find last month
8 my $eyear = 2002;
9 my $emonth = 11;
10
11 my @labels;
12 my %data;
13
14 while ( $syear < $eyear || ( $syear == $eyear && $smonth < $emonth ) ) {
15   push @labels, "$smonth/$syear";
16
17   my $speriod = timelocal(0,0,0,1,$smonth-1,$syear);
18   if ( ++$smonth == 13 ) { $syear++; $smonth=1; }
19   my $eperiod = timelocal(0,0,0,1,$smonth-1,$syear);
20
21   my $where = "WHERE _date >= $speriod AND _date < $eperiod";
22
23   # Invoiced
24   my $charged_sql = "SELECT SUM(charged) FROM cust_bill $where";
25   my $charged_sth = dbh->prepare($charged_sql) or die dbh->errstr;
26   $charged_sth->execute or die $charged_sth->errstr;
27   my $charged = $charged_sth->fetchrow_arrayref->[0] || 0;
28
29   push @{$data{charged}}, $charged;
30
31   #accounts receivable
32 #  my $ar_sql2 = "SELECT SUM(amount) FROM cust_credit $where";
33   my $credited_sql = "SELECT SUM(cust_credit_bill.amount) FROM cust_credit_bill, cust_bill WHERE cust_bill.invnum = cust_credit_bill.invnum AND cust_bill._date >= $speriod AND cust_bill._date < $eperiod";
34   my $credited_sth = dbh->prepare($credited_sql) or die dbh->errstr;
35   $credited_sth->execute or die $credited_sth->errstr;
36   my $credited = $credited_sth->fetchrow_arrayref->[0] || 0;
37
38     #horrible local kludge
39     my $expenses_sql = "SELECT SUM(cust_bill_pkg.setup) FROM cust_bill_pkg, cust_bill, cust_pkg, part_pkg WHERE cust_bill.invnum = cust_bill_pkg.invnum AND cust_bill._date >= $speriod AND cust_bill._date < $eperiod AND cust_pkg.pkgnum = cust_bill_pkg.pkgnum AND cust_pkg.pkgpart = part_pkg.pkgpart AND LOWER(part_pkg.pkg) LIKE 'expense _%'";
40     my $expenses_sth = dbh->prepare($expenses_sql) or die dbh->errstr;
41     $expenses_sth->execute or die $expenses_sth->errstr;
42     my $expenses = $expenses_sth->fetchrow_arrayref->[0] || 0;
43
44   push @{$data{ar}}, $charged-$credited-$expenses;
45
46   #deferred revenue
47 #  push @{$data{defer}}, '0';
48
49   #cashflow
50   my $paid_sql = "SELECT SUM(paid) FROM cust_pay $where";
51   my $paid_sth = dbh->prepare($paid_sql) or die dbh->errstr;
52   $paid_sth->execute or die $paid_sth->errstr;
53   my $paid = $paid_sth->fetchrow_arrayref->[0] || 0;
54
55   my $refunded_sql = "SELECT SUM(refund) FROM cust_refund $where";
56   my $refunded_sth = dbh->prepare($refunded_sql) or die dbh->errstr;
57   $refunded_sth->execute or die $refunded_sth->errstr;
58   my $refunded = $refunded_sth->fetchrow_arrayref->[0] || 0;
59
60     #horrible local kludge that doesn't even really work right
61     my $expenses_sql = "SELECT SUM(cust_bill_pay.amount) FROM cust_bill_pay, cust_bill WHERE cust_bill_pay.invnum = cust_bill.invnum AND cust_bill_pay._date >= $speriod AND cust_bill_pay._date < $eperiod AND 0 < ( select count(*) from cust_bill_pkg, cust_pkg, part_pkg WHERE cust_bill.invnum = cust_bill_pkg.invnum AND cust_pkg.pkgnum = cust_bill_pkg.pkgnum AND cust_pkg.pkgpart = part_pkg.pkgpart AND LOWER(part_pkg.pkg) LIKE 'expense _%' )";
62
63 #    my $expenses_sql = "SELECT SUM(cust_bill_pay.amount) FROM cust_bill_pay, cust_bill_pkg, cust_bill, cust_pkg, part_pkg WHERE cust_bill_pay.invnum = cust_bill.invnum AND cust_bill.invnum = cust_bill_pkg.invnum AND cust_bill_pay._date >= $speriod AND cust_bill_pay._date < $eperiod AND cust_pkg.pkgnum = cust_bill_pkg.pkgnum AND cust_pkg.pkgpart = part_pkg.pkgpart AND LOWER(part_pkg.pkg) LIKE 'expense _%'";
64     my $expenses_sth = dbh->prepare($expenses_sql) or die dbh->errstr;
65     $expenses_sth->execute or die $expenses_sth->errstr;
66     my $expenses = $expenses_sth->fetchrow_arrayref->[0] || 0;
67
68   push @{$data{cash}}, $paid-$refunded-$expenses;
69
70 }
71
72 #my $chart = Chart::LinesPoints->new(1024,480);
73 my $chart = Chart::LinesPoints->new(768,480);
74
75 $chart->set(
76   #'min_val' => 0,
77   'legend' => 'bottom',
78   'legend_labels' => [ #'Invoiced (cust_bill)',
79                        'Accounts receivable (invoices - applied credits)',
80                        #'Deferred revenue',
81                        'Actual cashflow (payments - refunds)' ],
82 );
83
84 my @data = ( \@labels,
85              #map $data{$_}, qw( ar defer cash )
86              #map $data{$_}, qw( charged ar cash )
87              map $data{$_}, qw( ar cash )
88            );
89
90 #my $gd = $chart->plot(\@data);
91 #open (IMG, ">i_r_c.png");
92 #print IMG $gd->png;
93 #close IMG;
94
95 #$chart->png("i_r_c.png", \@data);
96
97 #$chart->cgi_png(\@data);
98
99 http_header('Content-Type' => 'image/png' );
100 $Response->{ContentType} = 'image/png';
101
102 $chart->_set_colors();
103
104 %><%= $chart->scalar_png(\@data) %>