1 <% include( 'elements/search.html',
5 'query' => { 'select' => $select,
7 'addl_from' => $addl_from,
9 'extra_sql' => $extra_sql,
10 'order_by' => "ORDER BY $order_by",
12 'count_query' => $count_query,
20 #this is about reports about packages definitions (starting w/commission ones)
21 # while browse/part_pkg.cgi is config->package definitions
23 my $curuser = $FS::CurrentUser::CurrentUser;
25 unless $curuser->access_right('Financial reports');
27 my $conf = new FS::Conf;
28 my $money_char = $conf->config('money_char') || '$';
30 my $title = 'Package definition report';
31 my $name = 'package definition';
44 if (1) { #commission reports
46 if (1) { #employee commission reports
48 $group_by = join(', ',
49 ( map "access_user.$_", qw( usernum username ) ),
50 ( map "part_pkg.$_", qw( pkgpart pkg comment plan ) ),
55 $addl_from .= ' CROSS JOIN access_user ';
57 $match = 'cust_pkg.usernum = access_user.usernum';
59 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
61 #XXX in this context, agent virt for employees, not package defs
62 my $access_user = qsearchs('access_user', { 'usernum' => $1, })
63 or die "unknown usernum";
65 $title = $access_user->name;
69 push @header, 'Employee';
70 push @fields, sub { shift->get('username'); }; #access_user->name
71 push @links, ''; #link to employee edit w/ACL?
74 push @order_by, 'usernum'; #join to username? we're mostly interested in grouping rather than order
80 } elsif (0) { #agent commission reports
82 $match = 'cust_main.agentnum = agent.agentnum'; #XXX need to join to this
84 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
87 my $agent = qsearchs('agent', { 'agentnum' => $1 })
88 or die "unknown agentnum";
90 $title = $agent->agent;
92 push @header, 'Agent';
93 push @fields, sub { 'XXXagent' };
94 push @links, ''; #link to agent edit w/ACL?
97 push @order_by, 'agentnum'; #join to agent? we're mostly interested in grouping rather than order
106 LEFT JOIN cust_pkg ON ( part_pkg.pkgpart = cust_pkg.pkgpart
108 LEFT JOIN cust_bill_pkg USING ( pkgnum )
109 LEFT JOIN cust_bill USING ( invnum )
112 $title .= ' commission report';
113 $name = "commissionable $name";
118 push @header, 'Package definition';
119 push @fields, 'pkg_comment';
120 push @links, ''; #link to pkg definition edit w/ACL?
123 if (1) { #commission reports
125 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
127 push @where, " cust_bill_pkg.pkgnum > 0 ",
128 " cust_bill._date >= $beginning ",
129 " cust_bill._date <= $ending ";
131 push @header, '#'; # of sales';
132 push @links, ''; #link to detail report
134 push @fields, 'num_cust_pkg';
135 $select .= ", COUNT(DISTINCT cust_pkg.pkgnum) AS num_cust_pkg";
137 push @header, 'Sales';
138 push @links, ''; #link to detail report
140 # push @fields, sub { $money_char. sprintf('%.2f', shift->get('pkg_sales')); };
142 # ", SUM( SELECT setup+recur $from_cust_bill_pkg_where ) AS pkg_sales";
144 my $part_pkg = shift;
145 my $sql = "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur)".
146 # " FROM cust_bill_pkg $addl_from $where AND pkgpart = ? AND ";
148 LEFT JOIN cust_pkg USING ( pkgnum )
149 LEFT JOIN cust_bill USING ( invnum )
150 WHERE ". join(' AND ', @where).
151 " AND pkgpart = ? AND ";
152 my @arg = ($part_pkg->pkgpart);
153 if (1) { #employee commission reports
154 $sql .= 'usernum = ?';
155 push @arg, $part_pkg->get('usernum');
156 } elsif (0) { #agent commission reports
157 $match = 'cust_main.agentnum = agent.agentnum';
159 my $sth = dbh->prepare($sql) or die dbh->errstr;
160 $sth->execute(@arg) or die $sth->errstr;
161 $money_char. sprintf('%.2f', $sth->fetchrow_arrayref->[0] );
164 push @header, 'Commission';
165 push @links, ''; #link to detail report
167 #push @fields, sub { $money_char. sprintf('%.2f', shift->get('pkg_commission')); };
169 my $part_pkg = shift;
170 my $sql = "SELECT SUM(amount) FROM cust_credit
171 LEFT JOIN cust_event USING ( eventnum )
172 LEFT JOIN part_event USING ( eventpart )
173 LEFT JOIN cust_pkg ON ( cust_event.tablenum = cust_pkg.pkgnum )
174 WHERE eventnum IS NOT NULL
175 AND action IN ( 'pkg_employee_credit',
176 'pkg_employee_credit_pkg'
178 AND cust_credit._date >= $beginning
179 AND cust_credit._date <= $ending
181 AND cust_credit.custnum = ?
183 my @arg = ($part_pkg->pkgpart);
184 if (1) { #employee commission reports
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";
190 return 0 unless $access_user->user_custnum;
191 push @arg, $access_user->user_custnum;
193 } elsif (0) { #agent commission reports
194 push @arg, 'XXXagent_custnum'; #$agent->agent_custnum
196 my $sth = dbh->prepare($sql) or die dbh->errstr;
197 $sth->execute(@arg) or die $sth->errstr;
198 $money_char. sprintf('%.2f', $sth->fetchrow_arrayref->[0] );
204 push @order_by, 'pkgpart'; #pkg?
206 $select ||= 'part_pkg.*';
207 my $extra_sql = scalar(@where) ? 'WHERE ' . join(' AND ', @where) : '';
208 $extra_sql .= "GROUP BY $group_by" if $group_by;
209 my $order_by = join(', ', @order_by);
212 my $count_query = "SELECT COUNT(*) FROM ( SELECT 1 FROM part_pkg $addl_from $extra_sql ) AS num";