637a3bf9425f11b8eeedeb635e2e46bcb8d4f568
[freeside.git] / httemplate / graph / money_time-graph.cgi
1 <%
2
3 #my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
4 my ($curmon,$curyear) = (localtime(time))[4,5];
5
6 #find first month
7 my $syear = $cgi->param('syear') || 1899+$curyear;
8 my $smonth = $cgi->param('smonth') || $curmon+1;
9
10 #find last month
11 my $eyear = $cgi->param('eyear') || 1900+$curyear;
12 my $emonth = $cgi->param('emonth') || $curmon+1;
13 #if ( $emonth++>12 ) { $emonth-=12; $eyear++; }
14
15 # XXX or virtual
16 my $agentnum = '';
17 if ( $cgi->param('agentnum') =~ /^(\d*)$/ ) {
18   $agentnum = $1;
19 }
20
21 #my @labels;
22 #my %data;
23
24 my @items = qw( invoiced netsales credits payments receipts );
25 if ( $cgi->param('12mo') == 1 ) {
26   @items = map $_.'_12mo', @items;
27 }
28
29 my %label = (
30  'invoiced' => 'Gross Sales (invoiced)', 
31  'netsales' => 'Net Sales (invoiced - applied credits)',
32  'credits'  => 'Credits',
33  'payments' => 'Gross Receipts (payments)',
34  'receipts' => 'Net Receipts/Cashflow (payments - refunds)',
35 );
36 $label{$_.'_12mo'} = $label{$_}. " (previous 12 months)"
37   foreach keys %label;
38
39 my %color = (
40   'invoiced' => [ 153, 153, 255 ], #light blue
41   'netsales' => [   0,   0, 204 ], #blue
42   'credits'  => [ 204,   0,   0 ], #red
43   'payments' => [ 153, 204, 153 ], #light green
44   'receipts' => [   0, 204,   0 ], #green
45 );
46 $color{$_.'_12mo'} = $color{$_}
47   foreach keys %color;
48
49 my $report = new FS::Report::Table::Monthly (
50   'items' => \@items,
51   'start_month' => $smonth,
52   'start_year'  => $syear,
53   'end_month'   => $emonth,
54   'end_year'    => $eyear,
55   'agentnum'    => $agentnum,
56 );
57 my %data = %{$report->data};
58
59 #my $chart = Chart::LinesPoints->new(1024,480);
60 #my $chart = Chart::LinesPoints->new(768,480);
61 my $chart = Chart::LinesPoints->new(976,384);
62
63 my $d = 0;
64 $chart->set(
65   #'min_val' => 0,
66   'legend' => 'bottom',
67   'colors' => { ( map { 'dataset'.$d++ => $color{$_} } @items ),
68                 #'grey_background' => [ 211, 211, 211 ],
69                 'grey_background' => 'white',
70                 'background' => [ 0xe8, 0xe8, 0xe8 ], #grey
71               },
72   #'grey_background' => 'false',
73   'legend_labels' => [ map { $label{$_} } @items ],
74   'brush_size' => 4,
75   #'pt_size' => 12,
76 );
77
78 my @data = map { $data{$_} } ( 'label', @items );
79
80 http_header('Content-Type' => 'image/png' );
81
82 $chart->_set_colors();
83
84 %><%= $chart->scalar_png(\@data) %>