RT# 79284 Updated discounts UI for Change Package screen
[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   'select'    => 'part_pkg.*, setup_option.optionvalue AS _setup_fee,
27                               recur_option.optionvalue AS _recur_fee',
28   'table'     => 'part_pkg',
29   'addl_from' => FS::part_pkg->join_options_sql,
30   'hashref'   => \%hash,
31   'extra_sql' =>
32     ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql( 'null'=>1 ).
33     ' AND '. FS::part_pkg->agent_pkgs_sql( $agent ),
34   'order_by'  => 'ORDER BY pkg',
35 });
36
37 my $conf = new FS::Conf;
38
39 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
40
41 my $default_start_date = $conf->exists('order_pkg-no_start_date')
42                            ? ''
43                            : $cust_main ? $cust_main->next_bill_date
44                                         : '';
45
46 #num_billing_pkgs may be slightly better (will allow you to fill in a start
47 # date in the weird edge case where you're using sync_next_bill and
48 # prorate_defer_bill in flat.pm and there's one-time charges hanging around
49 # for this customer but no active ones)
50 #but we don't have an easy method for that, and definitely don't want to pull
51 # all package objects
52 my $num_ncancelled_pkgs = $cust_main ? $cust_main->num_ncancelled_pkgs : 0;
53
54 my @return = map  {
55                     my $start_date = $_->delay_start_date
56                                    || $default_start_date;
57                     $start_date = time2str($date_format, $start_date)
58                       if $start_date;
59                     ( $_->pkgpart,
60                       $_->pkg_comment,
61                       $_->can_discount,
62                       ($_->start_on_hold ? 1 : 0),
63                       $_->can_start_date(
64                         num_ncancelled_pkgs => $num_ncancelled_pkgs,
65                       ),
66                       $start_date,
67                     )
68                   }
69                   #sort { $a->pkg_comment cmp $b->pkg_comment }
70                   @part_pkg;
71
72 </%init>