agent-virtualize credit card surcharge percentage, RT#72961
[freeside.git] / FS / FS / cust_pkg_usage.pm
1 package FS::cust_pkg_usage;
2 use base qw( FS::Record );
3
4 use strict;
5 use FS::Record qw( qsearch ); #qsearchs );
6
7 =head1 NAME
8
9 FS::cust_pkg_usage - Object methods for cust_pkg_usage records
10
11 =head1 SYNOPSIS
12
13   use FS::cust_pkg_usage;
14
15   $record = new FS::cust_pkg_usage \%hash;
16   $record = new FS::cust_pkg_usage { 'column' => 'value' };
17
18   $error = $record->insert;
19
20   $error = $new_record->replace($old_record);
21
22   $error = $record->delete;
23
24   $error = $record->check;
25
26 =head1 DESCRIPTION
27
28 An FS::cust_pkg_usage object represents a counter of remaining included
29 minutes on a voice-call package.  FS::cust_pkg_usage inherits from
30 FS::Record.  The following fields are currently supported:
31
32 =over 4
33
34 =item pkgusagenum - primary key
35
36 =item pkgnum - the package (L<FS::cust_pkg>) containing the usage
37
38 =item pkgusagepart - the usage stock definition (L<FS::part_pkg_usage>).
39 This record in turn links to the call usage classes that are eligible to 
40 use these minutes.
41
42 =item minutes - the remaining minutes
43
44 =back
45
46 =head1 METHODS
47
48 =over 4
49
50 =item new HASHREF
51
52 # the new method can be inherited from FS::Record, if a table method is defined
53
54 =cut
55
56 sub table { 'cust_pkg_usage'; }
57
58 =item insert
59
60 Adds this record to the database.  If there is an error, returns the error,
61 otherwise returns false.
62
63 =cut
64
65 # the insert method can be inherited from FS::Record
66
67 =item delete
68
69 Delete this record from the database.
70
71 =cut
72
73 sub delete {
74   my $self = shift;
75   my $error = $self->reset || $self->SUPER::delete;
76 }
77
78 =item reset
79
80 Remove all allocations of this usage to CDRs.
81
82 =cut
83
84 sub reset {
85   my $self = shift;
86   my $error = '';
87   foreach (qsearch('cdr_cust_pkg_usage', { pkgusagenum => $self->pkgusagenum }))
88   {
89     $error ||= $_->delete;
90   }
91   $error;
92 }
93
94 =item replace OLD_RECORD
95
96 Replaces the OLD_RECORD with this one in the database.  If there is an error,
97 returns the error, otherwise returns false.
98
99 =cut
100
101 # the replace method can be inherited from FS::Record
102
103 =item check
104
105 Checks all fields to make sure this is a valid example.  If there is
106 an error, returns the error, otherwise returns false.  Called by the insert
107 and replace methods.
108
109 =cut
110
111 # the check method should currently be supplied - FS::Record contains some
112 # data checking routines
113
114 sub check {
115   my $self = shift;
116
117   my $error = 
118     $self->ut_numbern('pkgusagenum')
119     || $self->ut_foreign_key('pkgnum', 'cust_pkg', 'pkgnum')
120     || $self->ut_floatn('minutes')
121     || $self->ut_foreign_key('pkgusagepart', 'part_pkg_usage', 'pkgusagepart')
122   ;
123   return $error if $error;
124
125   if ( $self->minutes eq '' ) {
126     $self->set(minutes => $self->part_pkg_usage->minutes);
127   }
128
129   $self->SUPER::check;
130 }
131
132 =item cust_pkg
133
134 Return the L<FS::cust_pkg> linked to this record.
135
136 =item part_pkg_usage
137
138 Return the L<FS::part_pkg_usage> linked to this record.
139
140 =back
141
142 =head1 SEE ALSO
143
144 L<FS::Record>, schema.html from the base documentation.
145
146 =cut
147
148 1;
149