Merge branch 'master' of https://github.com/jgoodman/Freeside
[freeside.git] / httemplate / misc / cust-part_pkg.cgi
1 <% encode_json( \@return ) %>\
2 <%init>
3
4 my( $custnum, $prospectnum, $classnum ) = $cgi->param('arg');
5
6 my $agent;
7 my $cust_main;
8 if ( $custnum ) {
9   $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
10     or die 'unknown custnum';
11   $agent = $cust_main->agent;
12 } else {
13   my $prospect_main = qsearchs('prospect_main', {'prospectnum'=>$prospectnum} )
14     or die 'unknown prospectnum';
15   $agent = $prospect_main->agent;
16 }
17
18 my %hash = ( 'disabled' => '' );
19 if ( $classnum > 0 ) {
20   $hash{'classnum'} = $classnum;
21 } elsif ( $classnum eq '' || $classnum == 0 ) {
22   $hash{'classnum'} = '';
23 } #else -1, all classes, so don't set classnum
24
25 my @part_pkg = qsearch({
26   'table'     => 'part_pkg',
27   'hashref'   => \%hash,
28   'extra_sql' =>
29     ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql( 'null'=>1 ).
30     ' AND '. FS::part_pkg->agent_pkgs_sql( $agent ),
31   'order_by'  => 'ORDER BY pkg',
32 });
33
34 my $conf = new FS::Conf;
35
36 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
37
38 my $default_start_date = $conf->exists('order_pkg-no_start_date')
39                            ? ''
40                            : $cust_main ? $cust_main->next_bill_date
41                                         : '';
42
43 #num_billing_pkgs may be slightly better (will allow you to fill in a start
44 # date in the weird edge case where you're using sync_next_bill and
45 # prorate_defer_bill in flat.pm and there's one-time charges hanging around
46 # for this customer but no active ones)
47 #but we don't have an easy method for that, and definitely don't want to pull
48 # all package objects
49 my $num_ncancelled_pkgs = $cust_main ? $cust_main->num_ncancelled_pkgs : 0;
50
51 my @return = map  {
52                     my $start_date = $_->delay_start_date
53                                    || $default_start_date;
54                     $start_date = time2str($date_format, $start_date)
55                       if $start_date;
56                     ( $_->pkgpart,
57                       $_->pkg_comment,
58                       $_->can_discount,
59                       $_->can_start_date(
60                         num_ncancelled_pkgs => $num_ncancelled_pkgs,
61                       ),
62                       $start_date,
63                     )
64                   }
65                   #sort { $a->pkg_comment cmp $b->pkg_comment }
66                   @part_pkg;
67
68 </%init>