notes import with class, RT#73455
[freeside.git] / httemplate / misc / process / cust_main_note-import.cgi
1 <% include("/elements/header.html", "Batch Customer Note Import $op") %>
2
3 The following items <% $op eq 'Preview' ? 'would not be' : 'were not' %> imported.  (See below for imported items)
4 <PRE>
5 %  foreach my $row (@uninserted) {
6 %    $csv->combine( (map{ $row->{$_} } qw(last first note) ),
7 %                   $row->{error} ? ('#!', $row->{error}) : (),
8 %                 );
9 <% $csv->string %>
10 %  }
11 </PRE>
12
13 The following items <% $op eq 'Preview' ? 'would be' : 'were' %> imported.  (See above for unimported items)
14
15 <PRE>
16 %  foreach my $row (@inserted) {
17 %    $csv->combine( (map{ $row->{$_} } qw(custnum last first note) ),
18 %                   ('#!', $row->{name}),
19 %                 );
20 <% $csv->string %>
21 %  }
22 </PRE>
23   
24 <%init>
25
26 die "access denied"
27   unless $FS::CurrentUser::CurrentUser->access_right('Import');
28
29 $FS::cust_main::import=1;  # the customer records are already in the database
30 my $date = time;
31 my $otaker = $FS::CurrentUser::CurrentUser->username;
32 my $csv = new Text::CSV_XS;
33
34 my $param = $cgi->Vars;
35
36 my $op = $param->{preview} ? "Preview" : "Results";
37
38 my @inserted = ();
39 my @uninserted = ();
40 for ( my $row = 0; exists($param->{"custnum$row"}); $row++ ) {
41   if ( $param->{"custnum$row"} ) {
42     my $error = '';
43     if ( $param->{use_comments} ) { # why? notes are sexier (i think this can
44                                     # come out now?  UI doesn't let you set it
45       my $cust_main = qsearchs('cust_main',
46                                { 'custnum' => $param->{"custnum$row"} }
47                               );
48       if ($cust_main) {
49         $cust_main->comments
50           ? $cust_main->comments($cust_main->comments. " ". $param->{"note$row"})
51           : $cust_main->comments($param->{"note$row"});
52         $error = $cust_main->replace;
53       }else{
54         $error = "Can't find customer " . $param->{"custnum$row"};
55       }
56     } else {
57
58       my $comments = $param->{"note$row"};
59       my $classnum = '';
60       if ( $comments =~ /^\s*(\d+)\s*\|\s*(.+)$/ ) {
61         $classnum = $1;
62         $comments = $2;
63       }
64
65       my $cust_main_note = new FS::cust_main_note {
66                              'custnum'  => $param->{"custnum$row"},
67                              '_date'    => $date,
68                              'otaker'   => $otaker,
69                              'comments' => $comments,
70                              'classnum' => $classnum,
71                            };
72       $error = $cust_main_note->insert unless ($op eq "Preview");
73     }
74     my $result = { 'custnum' => $param->{"custnum$row"},
75                    'last'    => $param->{"last$row"},
76                    'first'   => $param->{"first$row"},
77                    'note'    => $param->{"note$row"},
78                    'name'    => $param->{"name$row"},
79                    'error'   => $error,
80                  };
81     if ($error) {
82       push @uninserted, $result;
83     }else{
84       push @inserted, $result;
85     }
86   }else{
87     push @uninserted, { 'custnum' => '',
88                         'last'    => $param->{"last$row"},
89                         'first'   => $param->{"first$row"},
90                         'note'    => $param->{"note$row"},
91                         'error'   => '',
92                       };
93   }
94 }
95 </%init>