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