4b3bed0d7e69253a06684479a78b081e3ba218aa
[freeside.git] / FS / FS / transaction867.pm
1 package FS::transaction867;
2
3 use strict;
4 use vars qw( @ISA @EXPORT_OK );
5 use FS::Record qw( qsearch qsearchs );
6 use FS::UID qw( getotaker dbh );
7 use Exporter;
8 use Data::Dumper;
9
10 @ISA = qw(FS::Record);
11 @EXPORT_OK = qw(batch_867data_import);
12
13 =head1 NAME
14
15 FS::transaction867 - Object methods for transaction867 records
16
17 =head1 SYNOPSIS
18
19   use FS::transaction867;
20
21   $record = new FS::transaction867 \%hash;
22   $record = new FS::transaction867 { 'column' => 'value' };
23
24   $error = $record->insert;
25
26   $error = $new_record->replace($old_record);
27
28   $error = $record->delete;
29
30   $error = $record->check;
31
32 =head1 DESCRIPTION
33
34 An FS::transaction867 object represents an example.  FS::transaction867 inherits from
35 FS::Record.  The following fields are currently supported:
36
37 =over 4
38
39 =item id - primary key
40
41 =item tdsp_duns - 
42
43 =item ref_identification - 
44
45 =item esiid - 
46
47 =item trans_creation_date - 
48
49 =item meter_no - 
50
51 =item srvc_period_start_date - 
52
53 =item srvc_period_end_date - 
54
55 =item prev_read_kwatts - 
56
57 =item curr_read_kwatts - 
58
59 =item meter_multiplier - 
60
61 =item usage_kwatts - 
62
63 =item measured_demand - 
64
65 =item ack_997 - 
66
67 =item processed - 
68
69
70 =back
71
72 =head1 METHODS
73
74 =over 4
75
76 =item new HASHREF
77
78 Creates a new example.  To add the example to the database, see L<"insert">.
79
80 Note that this stores the hash reference, not a distinct copy of the hash it
81 points to.  You can ask the object for a copy with the I<hash> method.
82
83 =cut
84
85 # the new method can be inherited from FS::Record, if a table method is defined
86
87 sub table { 'transaction867'; }
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 =cut
95
96 # the insert method can be inherited from FS::Record
97
98 =item delete
99
100 Delete this record from the database.
101
102 =cut
103
104 # the delete method can be inherited from FS::Record
105
106 =item replace OLD_RECORD
107
108 Replaces the OLD_RECORD with this one in the database.  If there is an error,
109 returns the error, otherwise returns false.
110
111 =cut
112
113 # the replace method can be inherited from FS::Record
114
115 =item check
116
117 Checks all fields to make sure this is a valid example.  If there is
118 an error, returns the error, otherwise returns false.  Called by the insert
119 and replace methods.
120
121 =cut
122
123 # the check method should currently be supplied - FS::Record contains some
124 # data checking routines
125
126 sub check {
127   my $self = shift;
128
129   my $error = 
130     $self->ut_numbern('id')
131     || $self->ut_number('tdsp_duns')
132     || $self->ut_text('ref_identification')
133     || $self->ut_text('esiid')
134     || $self->ut_number('trans_creation_date')
135     || $self->ut_text('meter_no')
136     || $self->ut_number('srvc_period_start_date')
137     || $self->ut_number('srvc_period_end_date')
138     || $self->ut_float('prev_read_kwatts')
139     || $self->ut_float('curr_read_kwatts')
140     || $self->ut_float('meter_multiplier')
141     || $self->ut_float('usage_kwatts')
142     || $self->ut_floatn('measured_demand')
143     || $self->ut_number('ack_997')
144     || $self->ut_number('processed')
145   ;
146   return $error if $error;
147
148   $self->SUPER::check;
149 }
150
151
152
153
154 =item batch_867data_import
155
156
157 Importing a CVS file with the following column:
158   867_usage esiid date meter srvc_from_date srvc_to_date previous_read_kwatts 
159   current_read_kwatts mult usage_kwatts measure_demand 997_ack
160
161 =cut
162
163 #@EXPORT_OK=qw(batch_867data_import);
164 sub batch_867data_import {
165   #my $param = shift;
166   my ($fh,$format) = @_;
167
168 #  print "\n\n****************** the cvs file\n\n";
169 #  print (<$fh>);
170 #  print ("\n$format\n");
171 #  return "done\n";
172
173   #my $fh = $param->{filehandle};
174   #my $format = $param->{'format'};
175   my $error;
176   my $debug = 0;
177   
178   my @fields;
179   if ( $format eq 'extended' ) {
180     @fields = qw( 
181                   tdsp_duns ref_identification esiid trans_creation_date
182                   meter_no srvc_period_start_date srvc_period_end_date
183                   prev_read_kwatts curr_read_kwatts meter_multiplier
184                   usage_kwatts measured_demand ack_997 processed
185                 );
186   } else {
187     die "unknown format $format";
188   }
189
190   eval "use Text::CSV_XS;";
191   die $@ if $@;
192
193   my $csv = new Text::CSV_XS;
194   #warn $csv;
195   #warn $fh;
196
197   my $imported = 0;
198   #my $columns;
199
200   local $SIG{HUP} = 'IGNORE';
201   local $SIG{INT} = 'IGNORE';
202   local $SIG{QUIT} = 'IGNORE';
203   local $SIG{TERM} = 'IGNORE';
204   local $SIG{TSTP} = 'IGNORE';
205   local $SIG{PIPE} = 'IGNORE';
206
207   my $oldAutoCommit = $FS::UID::AutoCommit;
208   local $FS::UID::AutoCommit = 0;
209   my $dbh = dbh;
210   
211   #while ( $columns = $csv->getline($fh) ) {
212   my $line;
213   while ( defined($line=<$fh>) ) {
214
215     $csv->parse($line) or do {
216       $dbh->rollback if $oldAutoCommit;
217       return "can't parse: ". $csv->error_input();
218     };
219
220     my @columns = $csv->fields();
221     #warn join('-',@columns);
222
223     # this hash will hold each CVS line
224     my %transaction867_data;
225
226     my $billtime = time;
227 #    my %cust_pkg = ( pkgpart => $pkgpart );
228     my %svc_acct = ();
229     foreach my $field ( @fields ) {
230         # -cal  this section is ignored by the 867 import 
231         $transaction867_data{$field} = shift @columns;
232     }
233
234     # make sure to set the 'ack_997' column
235     $transaction867_data{'ack_997'} = 0;
236
237     # make sure to set the 'processed' column
238     $transaction867_data{'processed'} = 0;
239
240     print Dumper(\%transaction867_data) if $debug;
241
242     ### Check to see if the invoice is already in transaction810 table
243     # if so then print a warning
244
245     my $ref_identification = $transaction867_data{'ref_identification'};
246     my $search_res = qsearchs ( 'transaction867',
247                                 {'ref_identification' => $ref_identification}
248                               );
249
250     if ($search_res) {
251
252       ### 
253       # place some code here to fix the problme of trying to insert
254       # data that have the same references identification number
255
256       print "$line\n";
257       print "OOps! a transaction with the same references identification"
258            ." number $ref_identification\n"
259            ."\tis in the transaction867 table already!!\n";
260
261     }
262     else {
263
264       my $transaction867_obj = new FS::transaction867( \%transaction867_data );
265       $error = $transaction867_obj->insert;
266       if ( $error ) {
267         $dbh->rollback if $oldAutoCommit;
268         return "can't bill customer for $line: $error";
269       }
270
271       $imported++;
272
273     }
274
275   } #end while
276
277   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
278
279   return "Empty file!" unless $imported;
280
281   ''; #no error
282
283 }
284
285
286
287 =back
288
289 =head1 BUGS
290
291 The author forgot to customize this manpage.
292
293 =head1 SEE ALSO
294
295 L<FS::Record>, schema.html from the base documentation.
296
297 =cut
298
299 1;
300