backup the schema for tables we don't need the data from. RT#85959
[freeside.git] / FS / FS / cust_pkg_usageprice.pm
1 package FS::cust_pkg_usageprice;
2 use base qw( FS::Record );
3
4 use strict;
5 use FS::Record qw( dbh ); # qsearch qsearchs );
6
7 =head1 NAME
8
9 FS::cust_pkg_usageprice - Object methods for cust_pkg_usageprice records
10
11 =head1 SYNOPSIS
12
13   use FS::cust_pkg_usageprice;
14
15   $record = new FS::cust_pkg_usageprice \%hash;
16   $record = new FS::cust_pkg_usageprice { '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::cust_pkg_usageprice object represents an specific customer package usage
29 pricing add-on.  FS::cust_pkg_usageprice inherits from FS::Record.  The
30 following fields are currently supported:
31
32 =over 4
33
34 =item usagepricenum
35
36 primary key
37
38 =item pkgnum
39
40 pkgnum
41
42 =item usagepricepart
43
44 usagepricepart
45
46 =item quantity
47
48 quantity
49
50
51 =back
52
53 =head1 METHODS
54
55 =over 4
56
57 =item new HASHREF
58
59 Creates a new record.  To add the record to the database, see L<"insert">.
60
61 Note that this stores the hash reference, not a distinct copy of the hash it
62 points to.  You can ask the object for a copy with the I<hash> method.
63
64 =cut
65
66 # the new method can be inherited from FS::Record, if a table method is defined
67
68 sub table { 'cust_pkg_usageprice'; }
69
70 =item insert
71
72 Adds this record to the database.  If there is an error, returns the error,
73 otherwise returns false.
74
75 =item delete
76
77 Delete this record from the database.
78
79 =item replace OLD_RECORD
80
81 Replaces the OLD_RECORD with this one in the database.  If there is an error,
82 returns the error, otherwise returns false.
83
84 =item check
85
86 Checks all fields to make sure this is a valid record.  If there is
87 an error, returns the error, otherwise returns false.  Called by the insert
88 and replace methods.
89
90 =cut
91
92 sub check {
93   my $self = shift;
94
95   my $error = 
96     $self->ut_numbern('usagepricenum')
97     || $self->ut_number('pkgnum')
98     || $self->ut_number('usagepricepart')
99     || $self->ut_number('quantity')
100   ;
101   return $error if $error;
102
103   $self->SUPER::check;
104 }
105
106 =item price
107
108 Returns the price for this customer usage pricing add-on (quantity of this
109 record multiplied by price of the associated FS::part_pkg_usageprice record)
110
111 =cut
112
113 sub price {
114   my $self = shift;
115   sprintf('%.2f', $self->quantity * $self->part_pkg_usageprice->price);
116 }
117
118 =item apply
119
120 Applies this customer usage pricing add-on.  (Mulitplies quantity of this record
121 by part_pkg_usageprice.amount, and applies to to any services of this package
122 matching part_pkg_usageprice.target)
123
124 If there is an error, returns the error, otherwise returns false.
125
126 =cut
127
128 sub apply {
129   my $self = shift;
130
131   my $oldAutoCommit = $FS::UID::AutoCommit;
132   local $FS::UID::AutoCommit = 0;
133   my $dbh = dbh;
134
135   my $error = '';
136
137   my $part_pkg_usageprice = $self->part_pkg_usageprice;
138
139   my $amount = $self->quantity * $part_pkg_usageprice->amount;
140
141   my $target = $part_pkg_usageprice->target;
142
143   #these are ongoing counters that count down, so increment them
144   if ( $target =~ /^svc_acct.(\w+)$/ ) {
145
146     my $method = "increment_$1";
147
148     foreach my $cust_svc ( $self->cust_pkg->cust_svc(svcdb=>'svc_acct') ) {
149       $error ||= $cust_svc->svc_x->$method( $amount );
150     }
151
152   #this is a maximum number, not a counter, so we want to take our number
153   # and add it to the default for the service
154   } elsif ( $target eq 'svc_conferencing.participants' ) {
155
156     foreach my $cust_svc ($self->cust_pkg->cust_svc(svcdb=>'svc_conferencing')){
157       my $svc_conferencing = $cust_svc->svc_x;
158       my $base_amount = $cust_svc->part_svc->part_svc_column('participants')->columnvalue || 0; #assuming.. D?  F would get overridden  :/
159       $svc_conferencing->participants( $base_amount + $amount );
160       $error ||= $svc_conferencing->replace;
161     }
162
163   #this has no multiplication involved, its just a set only
164   #} elsif ( $target eq 'svc_conferencing.confqualitynum' ) {
165
166   
167   } elsif ( $target eq 'sqlradacct_hour.recur_included_total' ) {
168
169     $error = "Cannot call apply on target $target";
170
171   }
172
173   if ( $error ) {
174     $dbh->rollback if $oldAutoCommit;
175   } else {
176     $dbh->commit if $oldAutoCommit;
177   }
178   return $error;
179
180 }
181
182 =back
183
184 =head1 BUGS
185
186 =head1 SEE ALSO
187
188 L<FS::part_pkg_usageprice>, L<FS::cust_pkg>, L<FS::Record>
189
190 =cut
191
192 1;
193