quotations, RT#16996
[freeside.git] / FS / FS / quotation_pkg.pm
1 package FS::quotation_pkg;
2
3 use strict;
4 use base qw( FS::Record );
5 use FS::Record qw( qsearchs ); #qsearch
6 use FS::part_pkg;
7 use FS::cust_location;
8
9 =head1 NAME
10
11 FS::quotation_pkg - Object methods for quotation_pkg records
12
13 =head1 SYNOPSIS
14
15   use FS::quotation_pkg;
16
17   $record = new FS::quotation_pkg \%hash;
18   $record = new FS::quotation_pkg { '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::quotation_pkg object represents a quotation package.
31 FS::quotation_pkg inherits from FS::Record.  The following fields are currently
32 supported:
33
34 =over 4
35
36 =item quotationpkgnum
37
38 primary key
39
40 =item pkgpart
41
42 pkgpart
43
44 =item locationnum
45
46 locationnum
47
48 =item start_date
49
50 start_date
51
52 =item contract_end
53
54 contract_end
55
56 =item quantity
57
58 quantity
59
60 =item waive_setup
61
62 waive_setup
63
64
65 =back
66
67 =head1 METHODS
68
69 =over 4
70
71 =item new HASHREF
72
73 Creates a new quotation package.  To add the quotation package to the database,
74 see L<"insert">.
75
76 Note that this stores the hash reference, not a distinct copy of the hash it
77 points to.  You can ask the object for a copy with the I<hash> method.
78
79 =cut
80
81 sub table { 'quotation_pkg'; }
82
83 =item insert
84
85 Adds this record to the database.  If there is an error, returns the error,
86 otherwise returns false.
87
88 =item delete
89
90 Delete this record from the database.
91
92 =item replace OLD_RECORD
93
94 Replaces the OLD_RECORD with this one in the database.  If there is an error,
95 returns the error, otherwise returns false.
96
97 =item check
98
99 Checks all fields to make sure this is a valid quotation package.  If there is
100 an error, returns the error, otherwise returns false.  Called by the insert
101 and replace methods.
102
103 =cut
104
105 sub check {
106   my $self = shift;
107
108   my $error = 
109     $self->ut_numbern('quotationpkgnum')
110     || $self->ut_foreign_key('pkgpart', 'part_pkg', 'pkgpart' )
111     || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum' )
112     || $self->ut_numbern('start_date')
113     || $self->ut_numbern('contract_end')
114     || $self->ut_numbern('quantity')
115     || $self->ut_enum('waive_setup', [ '', 'Y'] )
116   ;
117   return $error if $error;
118
119   $self->SUPER::check;
120 }
121
122 sub part_pkg {
123   my $self = shift;
124   qsearchs('part_pkg', { 'pkgpart' => $self->pkgpart } );
125 }
126
127 sub desc {
128   my $self = shift;
129   $self->part_pkg->pkg;
130 }
131
132 sub setup {
133   my $self = shift;
134   return '0.00' if $self->waive_setup eq 'Y';
135   my $part_pkg = $self->part_pkg;
136   #my $setup = $part_pkg->can('base_setup') ? $part_pkg->base_setup
137   #                                         : $part_pkg->option('setup_fee');
138   my $setup = $part_pkg->option('setup_fee');
139   #XXX discounts
140   $setup *= $self->quantity if $self->quantity;
141   sprintf('%.2f', $setup);
142
143 }
144
145 sub recur {
146   my $self = shift;
147   my $part_pkg = $self->part_pkg;
148   my $recur = $part_pkg->can('base_recur') ? $part_pkg->base_recur
149                                            : $part_pkg->option('recur_fee');
150   #XXX discounts
151   $recur *= $self->quantity if $self->quantity;
152   sprintf('%.2f', $recur);
153 }
154
155 =back
156
157 =head1 BUGS
158
159 =head1 SEE ALSO
160
161 L<FS::Record>, schema.html from the base documentation.
162
163 =cut
164
165 1;
166