beginning of web-based data importer
authorivan <ivan>
Wed, 11 Dec 2002 00:12:22 +0000 (00:12 +0000)
committerivan <ivan>
Wed, 11 Dec 2002 00:12:22 +0000 (00:12 +0000)
httemplate/misc/meta-import.cgi [new file with mode: 0644]
httemplate/misc/process/meta-import.cgi [new file with mode: 0644]

diff --git a/httemplate/misc/meta-import.cgi b/httemplate/misc/meta-import.cgi
new file mode 100644 (file)
index 0000000..ebd2a7e
--- /dev/null
@@ -0,0 +1,52 @@
+<!-- mason kludge -->
+<%= header('Import') %>
+<FORM ACTION="process/meta-import.cgi" METHOD="post" ENCTYPE="multipart/form-data">
+Import a CSV file containing customer records (zip tar etc).<BR><BR>
+
+##no##Default file format is CSV, with the following field order: <i>cust_pkg.setup, dayphone, first, last, address1, address2, city, state, zip, comments</i><BR><BR>
+
+<%
+  #false laziness with edit/cust_main.cgi
+  my @agents = qsearch( 'agent', {} );
+  die "No agents created!" unless @agents;
+  my $agentnum = $agents[0]->agentnum; #default to first
+
+  if ( scalar(@agents) == 1 ) {
+%>
+    <INPUT TYPE="hidden" NAME="agentnum" VALUE="<%= $agentnum %>">
+<% } else { %>
+    <BR><BR>Agent <SELECT NAME="agentnum" SIZE="1">
+  <% foreach my $agent (sort { $a->agent cmp $b->agent } @agents) { %>
+    <OPTION VALUE="<%= $agent->agentnum %>" <%= " SELECTED"x($agent->agentnum==$agentnum) %>><%= $agent->agent %></OPTION>
+  <% } %>
+    </SELECT><BR><BR>
+<% } %>
+
+<%
+  my @referrals = qsearch('part_referral',{});
+  die "No advertising sources created!" unless @referrals;
+  my $refnum = $referrals[0]->refnum; #default to first
+
+  if ( scalar(@referrals) == 1 ) {
+%>
+    <INPUT TYPE="hidden" NAME="refnum" VALUE="<%= $refnum %>">
+<% } else { %>
+    <BR><BR>Advertising source <SELECT NAME="refnum" SIZE="1">
+  <% foreach my $referral ( sort { $a->referral <=> $b->referral } @referrals) { %>
+    <OPTION VALUE="<%= $referral->refnum %>" <%= " SELECTED"x($referral->refnum==$refnum) %>><%= $referral->refnum %>: <%= $referral->referral %></OPTION>
+  <% } %>
+    </SELECT><BR><BR>
+<% } %>
+
+    First package: <SELECT NAME="pkgpart"><OPTION VALUE="">(none)</OPTION>
+<% foreach my $part_pkg ( qsearch('part_pkg',{'disabled'=>'' }) ) { %>
+     <OPTION VALUE="<%= $part_pkg->pkgpart %>"><%= $part_pkg->pkg. ' - '. $part_pkg->comment %></OPTION>
+<% } %>
+</SELECT><BR><BR>
+
+    CSV (zip, tar etc) Filename: <INPUT TYPE="file" NAME="csvfile"><BR><BR>
+    <INPUT TYPE="submit" VALUE="Import">
+    </FORM>
+  </BODY>
+<HTML>
+
diff --git a/httemplate/misc/process/meta-import.cgi b/httemplate/misc/process/meta-import.cgi
new file mode 100644 (file)
index 0000000..d42d6d4
--- /dev/null
@@ -0,0 +1,145 @@
+<!-- mason kludge -->
+<%= header('Map tables') %>
+<SCRIPT>
+var gSafeOnload = new Array();
+var gSafeOnsubmit = new Array();
+window.onload = SafeOnload;
+function SafeAddOnLoad(f) {
+  gSafeOnload[gSafeOnload.length] = f;
+}
+function SafeOnload() {
+  for (var i=0;i<gSafeOnload.length;i++)
+    gSafeOnload[i]();
+}
+function SafeAddOnSubmit(f) {
+  gSafeOnsubmit[gSafeOnsubmit.length] = f;
+}
+function SafeOnsubmit() {
+  for (var i=0;i<gSafeOnsubmit.length;i++)
+    gSafeOnsubmit[i]();
+}
+</SCRIPT>
+<%
+  #one
+  unless ( $cgi->param('magic') ) {
+
+    #oops, silly
+    #my $fh = $cgi->upload('csvfile');
+    ##warn $cgi;
+    ##warn $fh;
+    #
+    #use Archive::Tar;
+    #$tar = Archive::Tar->new();
+    #$tar->create_archive($fh); #or die $tar->error;
+
+    #haha for now
+    my @files = qw(
+authserv  credtype  dunprev  invoice  pmtdet    product   taxplan
+ccdet     customer  genlog   ledger   pops      pubvars   
+cchist    discplan  glacct   origco   prodcat   recur     users
+credcode  dundet    invline  payment  prodclas  repforms  webserv
+    );
+
+    %>
+    <FORM NAME="OneTrueForm" METHOD="POST" ACTION="meta-import.cgi">
+    <INPUT TYPE="hidden" NAME="magic" VALUE="process">
+    <%= hashmaker('schema', \@files, [ grep { ! /^h_/ } dbdef->tables ] ) %>
+    <br><INPUT TYPE="submit" VALUE="done">
+    </FORM>
+    </BODY>
+    </HTML>
+    <%
+
+  } elsif ( $cgi->param('magic') eq 'process' ) {
+
+    %>
+    <FORM NAME="OneTrueForm" METHOD="POST" ACTION="meta-import.cgi">
+    <INPUT TYPE="hidden" NAME="magic" VALUE="process2">
+    <%
+
+    my $schema_string = $cgi->param('schema');
+    my %schema = map { /^\s*(\w+)\s*=>\s*(\w+)\s*$/
+                         or die "guru meditation #420: $_";
+                       ( $1 => $2 );
+                     }
+                 split( /\n/, $schema_string );
+
+    #*** should be in global.asa/handler.pl like the rest
+    eval 'use Text::CSV_XS;';
+
+    foreach my $table ( keys %schema ) {
+
+      my $csv = Text::CSV_XS->new({ 'binary'=>1 });
+      open(FILE,"</home/ivan/intergate/legacy/csvdir/$table")
+        or die "can't /home/ivan/intergate/legacy/csvdir/$table: $!";
+      my $header = lc(<FILE>);
+      close FILE;
+      $csv->parse($header) or die;
+      my @from_columns = $csv->fields;
+
+      my @fs_columns = dbdef->table($schema{$table})->columns;
+
+      %>
+      <%= hashmaker($table, \@from_columns, \@fs_columns, $table, $schema{$table} ) %>
+      <br><hr><br>
+      <%
+
+    }
+
+    %>
+    <br><INPUT TYPE="submit" VALUE="done">
+    </FORM>
+    </BODY>
+    </HTML>
+    <%
+
+  } elsif ( $cgi->param('magic') eq 'process' ) {
+
+    %>
+    print results!!
+    <%
+  }
+
+  #hashmaker widget
+  sub hashmaker {
+    my($name, $from, $to, $labelfrom, $labelto) = @_;
+    $fromsize = scalar(@$from);
+    $tosize = scalar(@$to);
+    "<TABLE><TR><TH>$labelfrom</TH><TH>$labelto</TH></TR><TR><TD>".
+        qq!<SELECT NAME="${name}_from" SIZE=$fromsize>\n!.
+        join("\n", map { qq!<OPTION VALUE="$_">$_</OPTION>! } sort { $a cmp $b } @$from ).
+        "</SELECT>\n".
+      '</TD><TD>'.
+        qq!<SELECT NAME="${name}_to" SIZE=$tosize>\n!.
+        join("\n", map { qq!<OPTION VALUE="$_">$_</OPTION>! } sort { $a cmp $b } @$to ).
+        "</SELECT>\n".
+      '</TD></TR>'.
+      '<TR><TD COLSPAN=2>'.
+        qq!<INPUT TYPE="button" VALUE="map" onClick="toke_$name(this.form)">!.
+      '</TD></TR><TR><TD COLSPAN=2>'.
+      qq!<TEXTAREA NAME="$name" COLS=80 ROWS=8></TEXTAREA>!.
+      '</TD></TR></TABLE>'.
+      "<script>
+            function toke_$name() {
+              fromObject = document.OneTrueForm.${name}_from;
+              for (var i=fromObject.options.length-1;i>-1;i--) {
+                if (fromObject.options[i].selected)
+                  fromname = deleteOption_$name(fromObject,i);
+              }
+              toObject = document.OneTrueForm.${name}_to;
+              for (var i=toObject.options.length-1;i>-1;i--) {
+                if (toObject.options[i].selected)
+                  toname = deleteOption_$name(toObject,i);
+              }
+              document.OneTrueForm.$name.value = document.OneTrueForm.$name.value + fromname + ' => ' + toname + '\\n';
+            }
+            function deleteOption_$name(object,index) {
+              value = object.options[index].value;
+              object.options[index] = null;
+              return value;
+            }
+      </script>".
+      '';
+  }
+
+%>