summaryrefslogtreecommitdiff
path: root/httemplate/misc/process/cust_main_note-import.cgi
blob: 945689f89f384468f7ecfdbda274e319b0e48c6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<% include("/elements/header.html", "Batch Customer Note Import $op") %>

The following items <% $op eq 'Preview' ? 'would not be' : 'were not' %> imported.  (See below for imported items)
<PRE>
%  foreach my $row (@uninserted) {
%    $csv->combine( (map{ $row->{$_} } qw(last first note) ),
%                   $row->{error} ? ('#!', $row->{error}) : (),
%                 );
<% $csv->string %>
%  }
</PRE>

The following items <% $op eq 'Preview' ? 'would be' : 'were' %> imported.  (See above for unimported items)

<PRE>
%  foreach my $row (@inserted) {
%    $csv->combine( (map{ $row->{$_} } qw(custnum last first note) ),
%                   ('#!', $row->{name}),
%                 );
<% $csv->string %>
%  }
</PRE>
  
<%init>

die "access denied"
  unless $FS::CurrentUser::CurrentUser->access_right('Import');

$FS::cust_main::import=1;  # the customer records are already in the database
my $date = time;
my $otaker = $FS::CurrentUser::CurrentUser->username;
my $csv = new Text::CSV_XS;

my $param = $cgi->Vars;

my $op = $param->{preview} ? "Preview" : "Results";

my @inserted = ();
my @uninserted = ();
for ( my $row = 0; exists($param->{"custnum$row"}); $row++ ) {
  if ( $param->{"custnum$row"} ) {
    my $error = '';
    if ( $param->{use_comments} ) { # why? notes are sexier (i think this can
                                    # come out now?  UI doesn't let you set it
      my $cust_main = qsearchs('cust_main',
                               { 'custnum' => $param->{"custnum$row"} }
                              );
      if ($cust_main) {
        $cust_main->comments
          ? $cust_main->comments($cust_main->comments. " ". $param->{"note$row"})
          : $cust_main->comments($param->{"note$row"});
        $error = $cust_main->replace;
      }else{
        $error = "Can't find customer " . $param->{"custnum$row"};
      }
    } else {

      my $comments = $param->{"note$row"};
      my $classnum = '';
      if ( $comments =~ /^\s*(\d+)\s*\|\s*(.+)$/ ) {
        $classnum = $1;
        $comments = $2;
      }

      my $cust_main_note = new FS::cust_main_note {
                             'custnum'  => $param->{"custnum$row"},
                             '_date'    => $date,
                             'otaker'   => $otaker,
                             'comments' => $comments,
                             'classnum' => $classnum,
                           };
      $error = $cust_main_note->insert unless ($op eq "Preview");
    }
    my $result = { 'custnum' => $param->{"custnum$row"},
                   'last'    => $param->{"last$row"},
                   'first'   => $param->{"first$row"},
                   'note'    => $param->{"note$row"},
                   'name'    => $param->{"name$row"},
                   'error'   => $error,
                 };
    if ($error) {
      push @uninserted, $result;
    }else{
      push @inserted, $result;
    }
  }else{
    push @uninserted, { 'custnum' => '',
                        'last'    => $param->{"last$row"},
                        'first'   => $param->{"first$row"},
                        'note'    => $param->{"note$row"},
                        'error'   => '',
                      };
  }
}
</%init>