Initial revision
[freeside.git] / site_perl / type_pkgs.pm
1 package FS::type_pkgs;
2
3 use strict;
4 use vars qw(@ISA @EXPORT_OK);
5 use Exporter;
6 use FS::Record qw(fields qsearchs);
7
8 @ISA = qw(FS::Record Exporter);
9 @EXPORT_OK = qw(fields);
10
11 =head1 NAME
12
13 FS::type_pkgs - Object methods for type_pkgs records
14
15 =head1 SYNOPSIS
16
17   use FS::type_pkgs;
18
19   $record = create FS::type_pkgs \%hash;
20   $record = create FS::type_pkgs { 'column' => 'value' };
21
22   $error = $record->insert;
23
24   $error = $new_record->replace($old_record);
25
26   $error = $record->delete;
27
28   $error = $record->check;
29
30 =head1 DESCRIPTION
31
32 An FS::type_pkgs record links an agent type (see L<FS::agent_type>) to a
33 billing item definition (see L<FS::part_pkg>).  FS::type_pkgs inherits from
34 FS::Record.  The following fields are currently supported:
35
36 =over 4
37
38 =item typenum - Agent type, see L<FS::agent_type>
39
40 =item pkgpart - Billing item definition, see L<FS::part_pkg>
41
42 =back
43
44 =head1 METHODS
45
46 =over 4
47
48 =item create HASHREF
49
50 Create a new record.  To add the record to the database, see L<"insert">.
51
52 =cut
53
54 sub create {
55   my($proto,$hashref)=@_;
56
57   #now in FS::Record::new
58   #my($field);
59   #foreach $field (fields('type_pkgs')) {
60   #  $hashref->{$field}='' unless defined $hashref->{$field};
61   #}
62
63   $proto->new('type_pkgs',$hashref);
64
65 }
66
67 =item insert
68
69 Adds this record to the database.  If there is an error, returns the error,
70 otherwise returns false.
71
72 =cut
73
74 sub insert {
75   my($self)=@_;
76
77   $self->check or
78   $self->add;
79 }
80
81 =item delete
82
83 Deletes this record from the database.  If there is an error, returns the
84 error, otherwise returns false.
85
86 =cut
87
88 sub delete {
89   my($self)=@_;
90
91   $self->del;
92 }
93
94 =item replace OLD_RECORD
95
96 Replaces 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 sub replace {
102   my($new,$old)=@_;
103   return "(Old) Not a type_pkgs record!" unless $old->table eq "type_pkgs";
104
105   $new->check or
106   $new->rep($old);
107 }
108
109 =item check
110
111 Checks all fields to make sure this is a valid record.  If there is an error,
112 returns the error, otherwise returns false.  Called by the insert and replace
113 methods.
114
115 =cut
116
117 sub check {
118   my($self)=@_;
119   return "Not a type_pkgs record!" unless $self->table eq "type_pkgs";
120   my($recref) = $self->hashref;
121
122   $recref->{typenum} =~ /^(\d+)$/ or return "Illegal typenum";
123   $recref->{typenum} = $1;
124   return "Unknown typenum"
125     unless qsearchs('agent_type',{'typenum'=>$recref->{typenum}});
126
127   $recref->{pkgpart} =~ /^(\d+)$/ or return "Illegal pkgpart";
128   $recref->{pkgpart} = $1;
129   return "Unknown pkgpart"
130     unless qsearchs('part_pkg',{'pkgpart'=>$recref->{pkgpart}});
131
132   ''; #no error
133 }
134
135 =back
136
137 =head1 HISTORY
138
139 Defines the relation between agent types and pkgparts
140 (Which pkgparts can the different [types of] agents sell?)
141
142 ivan@sisd.com 97-nov-13
143
144 change to ut_ FS::Record, fixed bugs
145 ivan@sisd.com 97-dec-10
146
147 =cut
148
149 1;
150