6625e00296f1a7901a1518ffdc4079994d103bad
[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
44       my $cust_main = qsearchs('cust_main',
45                                { 'custnum' => $param->{"custnum$row"} }
46                               );
47       if ($cust_main) {
48         $cust_main->comments
49           ? $cust_main->comments($cust_main->comments. " ". $param->{"note$row"})
50           : $cust_main->comments($param->{"note$row"});
51         $error = $cust_main->replace;
52       }else{
53         $error = "Can't find customer " . $param->{"custnum$row"};
54       }
55     } else {
56       my $cust_main_note = new FS::cust_main_note {
57                                             'custnum'  => $param->{"custnum$row"},
58                                             '_date'    => $date,
59                                             'otaker'   => $otaker,
60                                             'comments' => $param->{"note$row"},
61                                                   };
62       $error = $cust_main_note->insert unless ($op eq "Preview");
63     }
64     my $result = { 'custnum' => $param->{"custnum$row"},
65                    'last'    => $param->{"last$row"},
66                    'first'   => $param->{"first$row"},
67                    'note'    => $param->{"note$row"},
68                    'name'    => $param->{"name$row"},
69                    'error'   => $error,
70                  };
71     if ($error) {
72       push @uninserted, $result;
73     }else{
74       push @inserted, $result;
75     }
76   }else{
77     push @uninserted, { 'custnum' => '',
78                         'last'    => $param->{"last$row"},
79                         'first'   => $param->{"first$row"},
80                         'note'    => $param->{"note$row"},
81                         'error'   => '',
82                       };
83   }
84 }
85 </%init>