So, I wanted to have all the accounts set up without waiting for the
text-import feature to become available.  I figure that importing a text
file of just accounts is much quicker than a general facility.

I was debating whether to do it in scheme or Perl.  I finally chose Perl
because I program in it daily.  My only worry here is that I've heard
that it might be going away some time soon due to problems with swig.

I didn't want to do scheme even though it seems to be preferred since
the last lisp I wrote that wasn't configuring Gnus or XEmacs was over 10
years ago...

So, here's the code.  I grabbed much of it from gnc-prices and the rest
from gandering at the C code.  Of course, the create_account would come
from a file.  This is just an example and I want to get to bed.

I do have two questions about this over and above any problems with my
code.
1) Is there a way to get a list of all the entries in account_type_name
   so I don't have a static list in my file that can go out of date.
2) Do I need to call xaccFreeAccount?  Since I'm exiting immediately,
   it's not a big deal.  But I do prefer a free for every malloc.  :)

Thanks,
  Darren

    use lib qw(/usr/share/gnucash/perl /usr/lib/gnucash/perl);
    use gnucash;

    my $sess = gnucash::xaccMallocSession();
    my $grp = gnucash::xaccSessionBeginFile($sess,$ARGV[0]);

    $grp || die "failed to read file $ARGV[0], maybe its locked?";

    # real code to read and parse a chart of accounts goes here...
    my $parent = create_account("Liabilities", 4, "300", "General Liabilities", "USD", 
$grp, undef);
    create_account("Loans Payable", 4, "310", "Parent of all Loans", "USD", $grp, 
$parent);

    gnucash::xaccSessionSave($sess);
    gnucash::xaccSessionEnd($sess);
    gnucash::xaccSessionDestroy($sess);

    exit 0;

    sub create_account
    {
        my ($name, $type, $code, $desc, $currency, $grp, $parent) = @_;

        @_ == 7 || die "Invalid number of parameters to create_account.\nShould be 7, 
was " . scalar(@_) . "\n";

        my $acct = gnucash::xaccMallocAccount()  || die "Unable to create account 
structure";

        # considering it was just created there shouldn't be anything to balance
        gnucash::xaccAccountBeginEdit($acct, 1);

        gnucash::xaccAccountSetName($acct, $name);
        gnucash::xaccAccountSetType($acct, $type);
        gnucash::xaccAccountSetCode($acct, $code);
        gnucash::xaccAccountSetDescription($acct, $desc);
        gnucash::xaccAccountSetCurrency($acct, $currency);

        if ($parent)
        {
            gnucash::xaccAccountBeginEdit($parent, 0);
            gnucash::xaccInsertSubAccount($parent, $acct);
            gnucash::xaccAccountCommitEdit($parent);
        }
        else
        {
            gnucash::xaccGroupInsertAccount($grp, $acct);
        }

        gnucash::xaccAccountCommitEdit($acct);

        $acct;
    }
-- 
<[EMAIL PROTECTED]> <http://www.daft.com/~torin> <[EMAIL PROTECTED]> <[EMAIL PROTECTED]>
Darren Stalder/2608 Second Ave, @282/Seattle, WA 98121-1212/USA/+1-206-ELF-LIPZ
@ Sysadmin, webweaver, postmaster for hire. C/Perl/CGI/Pilot programmer/tutor @
@                    Make a little hot-tub in your soul.                      @

--
Gnucash Developer's List
To unsubscribe send empty email to: [EMAIL PROTECTED]


Reply via email to