[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SF.net SVN: ledger-smb:[3058] addons/1.3



Revision: 3058
          http://ledger-smb.svn.sourceforge.net/ledger-smb/?rev=3058&view=rev
Author:   einhverfr
Date:     2010-09-03 17:01:39 +0000 (Fri, 03 Sep 2010)

Log Message:
-----------
Transaction import from CSV addon

Added Paths:
-----------
    addons/1.3/import_trans/
    addons/1.3/import_trans/import_trans.pl
    addons/1.3/import_trans/tags/
    addons/1.3/import_trans/trunk/
    addons/1.3/import_trans/trunk/UI/
    addons/1.3/import_trans/trunk/UI/import_trans/
    addons/1.3/import_trans/trunk/UI/import_trans/import_trans.html
    addons/1.3/import_trans/trunk/scripts/
    addons/1.3/import_trans/trunk/scripts/import_trans.pl

Copied: addons/1.3/import_trans/import_trans.pl (from rev 3031, trunk/customer.pl)
===================================================================
--- addons/1.3/import_trans/import_trans.pl	                        (rev 0)
+++ addons/1.3/import_trans/import_trans.pl	2010-09-03 17:01:39 UTC (rev 3058)
@@ -0,0 +1,3 @@
+#!/usr/bin/perl
+
+require 'lsmb-request.pl';

Added: addons/1.3/import_trans/trunk/UI/import_trans/import_trans.html
===================================================================
--- addons/1.3/import_trans/trunk/UI/import_trans/import_trans.html	                        (rev 0)
+++ addons/1.3/import_trans/trunk/UI/import_trans/import_trans.html	2010-09-03 17:01:39 UTC (rev 3058)
@@ -0,0 +1,64 @@
+<?lsmb INCLUDE 'ui-header.html' 
+      include_stylesheet = ["UI/asset/asset.css"] 
+      include_script = ["UI/asset/asset.js"] 
+?>
+<?lsmb PROCESS 'elements.html' ?>
+<body onLoad="init()">
+<div class="listtop"><?lsmb title ?></div>
+<div class="info"><?lsmb info ?></div>
+<form action="<?lsmb script ?>" method="post" enctype="multipart/form-data">
+<?lsmb IF !multiple ?>
+<div class="inputrow" id = "referencerow">
+<?lsmb PROCESS input element_data = {
+    type  = "text"
+    class = "reference"
+    name  = "reference"
+    label = text('Reference')
+    value = reference
+    size  = 15
+} ?>
+</div>
+<div class="inputrow" id = "descriptionrow">
+<?lsmb PROCESS input element_data = {
+    type  = "text"
+    class = "description"
+    name  = "description"
+    label = text('Description')
+    value = description
+} ?>
+</div>
+<div class="inputrow" id="daterow">
+<?lsmb PROCESS input element_data = {
+     type = "text"
+     class = "date"
+     label = text("Transaction Date") #" 
+     name = "transdate"
+     value = transdate
+     size = 12
+} ?>
+<?lsmb END IF # !multiple ?>
+<div class="inputrow" id="filerow">
+<?lsmb PROCESS input element_data = {
+       name = "import_file"
+       type = "file"
+       label = text('From File') #'
+       class = "file"
+} ?>
+</div>
+<div class="inputrow" id="buttonrow">
+<?lsmb PROCESS input element_data = {
+       type = "hidden"
+       name = "trans_type"
+       value = trans_type
+} ?>
+<?lsmb PROCESS button element_data = {
+	text = text("Save")
+	type = "submit"
+	class = "submit"
+	name = "action"
+	value = "run_import"
+} ?>
+</div>
+</form>
+</body>
+</html>

Added: addons/1.3/import_trans/trunk/scripts/import_trans.pl
===================================================================
--- addons/1.3/import_trans/trunk/scripts/import_trans.pl	                        (rev 0)
+++ addons/1.3/import_trans/trunk/scripts/import_trans.pl	2010-09-03 17:01:39 UTC (rev 3058)
@@ -0,0 +1,111 @@
+=pod
+
+=head1 NAME 
+
+LedgerSMB::Scripts::import_trans
+
+=head1 SYNPOSIS
+
+This is a module that demonstrates how to set up scripts for importing bulk 
+data.
+
+=cut
+
+package LedgerSMB::Scripts::import_trans;
+use LedgerSMB::Template;
+use LedgerSMB::Form;
+use strict;
+
+our $cols = {
+   gl       =>  ['accno', 'debit', 'credit', 'source', 'memo'],
+   ap_multi =>  ['vendor', 'amount', 'account', 'ap', 'description', 
+                 'invnumber', 'transdate'],
+};
+
+our $process = {
+   gl       => sub {
+                   use LedgerSMB::GL;
+                   my ($request, $entries) = @_;
+                   my $form = Form->new();
+                   $form->{reference} = $request->{reference};
+                   $form->{description} = $request->{description};
+                   $form->{transdate} = $request->{transdate};
+                   $form->{rowcount} = 0;
+                   $form->{dbh} = $request->{_dbh};
+                   for my $ref (@$entries){
+                       ++$form->{rowcount} = 0;
+                       for my $col (@{$col->{$request->{type}}}){
+                           $form->{"${col}_$form->{rowcount}"} = shift @$ref;
+                       }
+                   }
+                   LedgerSMB::GL->post($request->{_user}, $form);
+                },
+   ap_multi => sub {
+                   use LedgerSMB::AA;
+                   my ($request, $entries) = @_;
+                   for my $ref (@$entries){
+                       my $form = Form->new();
+                       $form->{dbh} = $request->{_dbh};
+                       $form->{rowcount} = 1;
+                       $form->{vendor_id} = shift @$ref;
+                       $form->{amount_1} = shift @$ref;
+                       $form->{accno_1} = shift @$ref;
+                       $form->{ARAP} = 'AP';
+                       $form->{vc} = "vendor";
+                       $form->{arap} = 'ap';
+                       $form->{AP} = shift @$ref;
+                       $form->{description} = shift @$ref;
+                       $form->{invnumber} = shift @$ref;
+                       $form->{transdate} = shift @$ref;
+                       LedgerSMB::AA->post_invoice($request->{_user}, $form);
+                   }
+               },
+};
+
+sub parse_file {
+    my $self = shift @_;
+
+    my $handle = $self->{_request}->upload('import_file');
+    my $contents = join("\n", <$handle>);
+
+    $self->{import_entries} = [];
+    for my $line (split /(\r\n|\r|\n)/, $contents){
+        next if ($line !~ /,/);
+        my @fields;
+        $line =~ s/[^"]"",/"/g;
+        while ($line ne '') {
+            if ($line =~ /^"/){
+                $line =~ s/"(.*?)"(,|$)//;
+                my $field = $1;
+                $field =~ s/\s*$//;
+                push @fields, $field;
+            } else {
+                $line =~ s/([^,]*),?//;
+                my $field = $1;
+                $field =~ s/\s*$//;
+                push @fields, $field;
+            }
+        }
+        push @{$self->{import_entries}}, ..hidden..;
+    }     
+    unshift @{$self->{import_entries}}; # get rid of header line
+    return @{$self->{import_entries}};
+}
+
+sub begin_import {
+    my ($request) = @_;
+    my $template = LedgerSMB::Template->new(
+        user =>$request->{_user}, 
+        locale => $request->{_locale},
+        path => 'UI/asset',
+        template => 'import_asset',
+        format => 'HTML'
+    );
+    $template->render($request);
+}
+
+sub run_import {
+    my $request = @_;
+    my @entries = process_file($request);
+    &$process->{$request->{type}}($request, ..hidden..) || begin_import($request);
+}


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.