384db7288368741e64ebfa042fede08ea27066c0
[freeside.git] / httemplate / search / part_pkg.html
1 <% include( 'elements/search.html',
2               'title'       => $title,
3               'name'        => $name,
4               'header'      => \@header,
5               'query'       => { 'select'    => $select,
6                                  'table'     => 'part_pkg',
7                                  'addl_from' => $addl_from,
8                                  'hashref'   => {},
9                                  'extra_sql' => $extra_sql,
10                                  'order_by'  => "ORDER BY $order_by",
11                                },
12               'count_query' => $count_query,
13               'fields'      => \@fields,
14               'links'       => \@links,
15               'align'       => $align,
16           )
17 %>
18 <%init>
19
20 #this is about reports about packages definitions (starting w/commission ones)
21 # while browse/part_pkg.cgi is config->package definitions
22
23 my $curuser = $FS::CurrentUser::CurrentUser;
24 die "access denied"
25   unless $curuser->access_right('Financial reports');
26
27 my $conf = new FS::Conf;
28 my $money_char = $conf->config('money_char') || '$';
29
30 my $title = 'Package definition report';
31 my $name = 'package definition';
32
33 my $select = '';
34 my $addl_from = '';
35 my @where = ();
36 my @order_by = ();
37 my @header = ();
38 my @fields = ();
39 my @links  = ();
40 my $align  = '';
41
42 if (1) { #commission reports
43
44   if (1) { #employee commission reports 
45
46     $select = 'DISTINCT usernum, username, part_pkg.*';
47
48     $addl_from .= ' CROSS JOIN access_user ';
49
50     if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
51
52       #XXX in this context, agent virt for employees, not package defs
53       my $access_user = qsearchs('access_user', { 'usernum' => $1, })
54         or die "unknown usernum";
55
56       $title = $access_user->name;
57
58     } else {
59
60       push @header, 'Employee';
61       push @fields, sub { shift->get('username'); }; #access_user->name
62       push @links, ''; #link to employee edit w/ACL?
63       $align .= 'c';
64
65       push @order_by, 'usernum'; #join to username?  we're mostly interested in grouping rather than order
66
67       $title = 'Employee';
68
69     }
70
71   } elsif (0) { #agent commission reports
72
73     if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
74
75       #agent virt
76       my $agent = qsearchs('agent', { 'agentnum' => $1 })
77         or die "unknown agentnum";
78
79       $title = $agent->agent;
80
81       push @header, 'Agent';
82       push @fields, sub { 'XXXagent' };
83       push @links, ''; #link to agent edit w/ACL?
84       $align .= 'c';
85
86       push @order_by, 'agentnum'; #join to agent?  we're mostly interested in grouping rather than order
87
88     } else {
89       $title = 'Agent';
90     }
91
92   }
93
94   $title .= ' commission report';
95   $name = "commissionable $name";
96
97
98 }
99
100 push @header, 'Package definition';
101 push @fields, 'pkg_comment';
102 push @links, ''; #link to pkg definition edit w/ACL?
103 $align .= 'l';
104
105 if (1) { #commission reports
106
107   my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
108
109   my $match = '';
110   if (1) { #employee commission reports 
111     $match = 'cust_pkg.usernum = access_user.usernum';
112   } elsif (0) { #agent commission reports
113     $match = 'cust_main.agentnum = agent.agentnum';
114   }
115
116   my $from_cust_bill_pkg_where = "FROM cust_bill_pkg
117                                       LEFT JOIN ( cust_bill ) USING ( invnum )
118                                       LEFT JOIN ( cust_pkg  ) USING ( pkgnum )
119                                     WHERE cust_bill_pkg.pkgnum > 0
120                                       AND cust_bill._date >= $beginning
121                                       AND cust_bill._date <= $ending         ";
122   my $and = "                         AND $match
123                                       AND cust_pkg.pkgpart = part_pkg.pkgpart";
124
125   push @where, "EXISTS( SELECT 1 $from_cust_bill_pkg_where $and )";
126
127   push @header, '#'; # of sales';
128   push @links, ''; #link to detail report
129   $align .= 'r';
130   push @fields, 'num_cust_pkg';
131   $select .= ", ( SELECT COUNT(DISTINCT pkgnum)
132                     $from_cust_bill_pkg_where $and )
133                 AS num_cust_pkg";
134 #  push @fields, sub {
135 #    my $part_pkg = shift;
136 #    my $sql =
137 #      #"SELECT COUNT( SELECT DISTINCT pkgnum $from_cust_bill_pkg_where )";
138 #      "SELECT COUNT(DISTINCT pkgnum) $from_cust_bill_pkg_where";
139 #    my $sth = dbh->prepare($sql) or die dbh->errstr;
140 #    $sth->execute or die $sth->errstr;
141 #    $sth->fetchrow_arrayref->[0];
142 #  };
143
144   push @header, 'Sales';
145   push @links, ''; #link to detail report
146   $align .= 'r';
147 #  push @fields, sub { $money_char. sprintf('%.2f', shift->get('pkg_sales')); };
148 #  $select .=
149 #    ", SUM( SELECT setup+recur $from_cust_bill_pkg_where ) AS pkg_sales";
150   push @fields, sub {
151     my $part_pkg = shift;
152     my $sql = "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $from_cust_bill_pkg_where AND pkgpart = ? AND ";
153     my @arg = ($part_pkg->pkgpart);
154     if (1) { #employee commission reports 
155       $sql .= 'usernum = ?';
156       push @arg, $part_pkg->get('usernum');
157     } elsif (0) { #agent commission reports
158       $match = 'cust_main.agentnum = agent.agentnum';
159     }
160     my $sth = dbh->prepare($sql) or die dbh->errstr;
161     $sth->execute(@arg) or die $sth->errstr;
162     $money_char. sprintf('%.2f', $sth->fetchrow_arrayref->[0] );
163   };
164
165   push @header, 'Commission';
166   push @links, ''; #link to detail report
167   $align .= 'r';
168   #push @fields, sub { $money_char. sprintf('%.2f', shift->get('pkg_commission')); };
169   push @fields, sub {
170     my $part_pkg = shift;
171     my $sql = "SELECT SUM(amount) FROM cust_credit
172                    INNER JOIN cust_event USING ( eventnum )
173                    LEFT  JOIN part_event USING ( eventpart )
174                    LEFT  JOIN cust_pkg ON ( cust_event.tablenum = cust_pkg.pkgnum )
175                  WHERE action IN ( 'pkg_employee_credit',
176                                    'pkg_employee_credit_pkg'
177                                  )
178                    AND cust_credit._date >= $beginning
179                    AND cust_credit._date <= $ending         
180                    AND pkgpart = ?
181                    AND cust_credit.custnum = ?
182               ";
183     my @arg = ($part_pkg->pkgpart);
184     if (1) { #employee commission reports 
185
186       #XXX in this context, agent virt for employees, not package defs
187       my $access_user = qsearchs('access_user', { 'usernum' => $part_pkg->get('usernum'), })
188         or die "unknown usernum";
189
190       push @arg, $access_user->user_custnum or return 0;
191
192     } elsif (0) { #agent commission reports
193       push @arg, 'XXXagent_custnum'; #$agent->agent_custnum
194     }
195     my $sth = dbh->prepare($sql) or die dbh->errstr;
196     $sth->execute(@arg) or die $sth->errstr;
197     $money_char. sprintf('%.2f', $sth->fetchrow_arrayref->[0] );
198      
199   };
200
201 }
202
203 push @order_by, 'pkgpart'; #pkg?
204
205 $select ||= 'part_pkg.*';
206 my $extra_sql = scalar(@where) ? 'WHERE ' . join(' AND ', @where) : ''; 
207 my $order_by = join(', ', @order_by);
208
209 my $count_query = "SELECT COUNT(*) FROM part_pkg $addl_from $extra_sql";
210
211 </%init>