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