svc_hardware: better error messages for bad hw_addr when not validating as a MAC...
[freeside.git] / FS / FS / part_pkg_usage.pm
1 package FS::part_pkg_usage;
2 use base qw( FS::m2m_Common FS::Record );
3
4 use strict;
5 use Scalar::Util qw(blessed);
6
7 =head1 NAME
8
9 FS::part_pkg_usage - Object methods for part_pkg_usage records
10
11 =head1 SYNOPSIS
12
13   use FS::part_pkg_usage;
14
15   $record = new FS::part_pkg_usage \%hash;
16   $record = new FS::part_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::part_pkg_usage object represents a stock of usage minutes (generally
29 for voice services) included in a package definition.  FS::part_pkg_usage 
30 inherits from FS::Record.  The following fields are currently supported:
31
32 =over 4
33
34 =item pkgusagepart - primary key
35
36 =item pkgpart - the package definition (L<FS::part_pkg>)
37
38 =item minutes - the number of minutes included per billing cycle
39
40 =item priority - the relative order in which to use this stock of minutes.
41
42 =item shared - 'Y' to allow these minutes to be shared with other packages
43 belonging to the same customer.  Otherwise, only usage allocated to this
44 package will use this stock of minutes.
45
46 =item rollover - 'Y' to allow unused minutes to carry over between billing
47 cycles.  Otherwise, the available minutes will reset to the value of the 
48 "minutes" field upon billing.
49
50 =item description - a text description of this stock of minutes
51
52 =back
53
54 =head1 METHODS
55
56 =over 4
57
58 =item new HASHREF
59
60 =item insert CLASSES
61
62 =item replace CLASSES
63
64 CLASSES can be an array or hash of usage classnums (see L<FS::usage_class>)
65 to link to this record.
66
67 =item delete
68
69 =cut
70
71 sub table { 'part_pkg_usage'; }
72
73 sub insert {
74   my $self = shift;
75   my $opt = ref($_[0]) eq 'HASH' ? shift : { @_ };
76
77   $self->SUPER::insert
78   || $self->process_m2m( 'link_table'   => 'part_pkg_usage_class',
79                          'target_table' => 'usage_class',
80                          'params'       => $opt,
81   );
82 }
83
84 sub replace {
85   my $self = shift;
86   my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
87               ? shift
88               : $self->replace_old;
89   my $opt = ref($_[0]) eq 'HASH' ? $_[0] : { @_ };
90   $self->SUPER::replace($old)
91   || $self->process_m2m( 'link_table'   => 'part_pkg_usage_class',
92                          'target_table' => 'usage_class',
93                          'params'       => $opt,
94   );
95 }
96
97 sub delete {
98   my $self = shift;
99   $self->process_m2m( 'link_table'   => 'part_pkg_usage_class',
100                       'target_table' => 'usage_class',
101                       'params'       => {},
102   ) || $self->SUPER::delete;
103 }
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 sub check {
114   my $self = shift;
115
116   my $error = 
117     $self->ut_numbern('pkgusagepart')
118     || $self->ut_foreign_key('pkgpart', 'part_pkg', 'pkgpart')
119     || $self->ut_float('minutes')
120     || $self->ut_numbern('priority')
121     || $self->ut_flag('shared')
122     || $self->ut_flag('rollover')
123     || $self->ut_textn('description')
124   ;
125   return $error if $error;
126
127   $self->SUPER::check;
128 }
129
130 =item classnums
131
132 Returns the usage class numbers that are allowed to use minutes from this
133 pool.
134
135 =cut
136
137 sub classnums {
138   my $self = shift;
139   if (!$self->get('classnums')) {
140     my $classnums = [ map { $_->classnum } $self->part_pkg_usage_class ];
141     $self->set('classnums', $classnums);
142   }
143   @{ $self->get('classnums') };
144 }
145
146 =back
147
148 =head1 SEE ALSO
149
150 L<FS::Record>, schema.html from the base documentation.
151
152 =cut
153
154 1;
155