Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / FS / FS / part_event / Action / Mixin / fee.pm
1 package FS::part_event::Action::Mixin::fee;
2
3 use strict;
4 use base qw( FS::part_event::Action );
5 use FS::Record qw( qsearch );
6
7 sub event_stage { 'pre-bill'; }
8
9 sub option_fields {
10   (
11     'feepart'  => { label     => 'Fee definition',
12                     type      => 'select-table', #select-part_fee XXX
13                     table     => 'part_fee',
14                     hashref   => { disabled => '' },
15                     name_col  => 'itemdesc',
16                     value_col => 'feepart',
17                     disable_empty => 1,
18                   },
19   ),
20
21 }
22
23 sub default_weight { 10; }
24
25 sub hold_until_bill { 1 }
26
27 sub do_action {
28   my( $self, $cust_object, $cust_event ) = @_;
29
30   my $feepart = $self->option('feepart')
31     or die "no fee definition selected for event '".$self->event."'\n";
32   my $tablenum = $cust_object->get($cust_object->primary_key);
33
34   # see if there's already a pending fee for this customer/invoice
35   my @existing = qsearch({
36       table     => 'cust_event_fee',
37       addl_from => 'JOIN cust_event USING (eventnum)',
38       hashref   => { feepart    => $feepart,
39                      billpkgnum => '' },
40       extra_sql => " AND tablenum = $tablenum",
41   });
42   if (scalar @existing > 0) {
43     warn $self->event." event, object $tablenum: already scheduled\n"
44       if $FS::part_fee::DEBUG;
45     return;
46   }
47
48   # mark the event so that the fee will be charged
49   # the logic for calculating the fee amount is in FS::part_fee
50   # the logic for attaching it to the base invoice/line items is in 
51   # FS::cust_bill_pkg
52   my $cust_event_fee = FS::cust_event_fee->new({
53       'eventnum'    => $cust_event->eventnum,
54       'feepart'     => $feepart,
55       'billpkgnum'  => '',
56       'nextbill'    => $self->hold_until_bill ? 'Y' : '',
57   });
58
59   my $error = $cust_event_fee->insert;
60   die $error if $error;
61
62   '';
63 }
64
65 1;