-sub defaultaccounts {
- $main::lxdebug->enter_sub();
-
- my ($self, $myconfig, $form) = @_;
-
- # connect to database
- my $dbh = $form->dbconnect($myconfig);
-
- # get defaults from defaults table
- my $query = qq|SELECT * FROM defaults|;
- my $sth = $dbh->prepare($query);
- $sth->execute || $form->dberror($query);
-
- $form->{defaults} = $sth->fetchrow_hashref("NAME_lc");
- $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
- $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
- $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
- $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
- $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
- $form->{defaults}{AR_paid} = $form->{defaults}{ar_paid_accno_id};
-
- $form->{defaults}{weightunit} ||= 'kg';
-
- $sth->finish;
-
- $query = qq|SELECT c.id, c.accno, c.description, c.link
- FROM chart c
- WHERE c.link LIKE '%IC%'
- ORDER BY c.accno|;
- $sth = $dbh->prepare($query);
- $sth->execute || $self->dberror($query);
-
- while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
- foreach my $key (split(/:/, $ref->{link})) {
- if ($key =~ /IC/) {
- my $nkey = $key;
- if ($key =~ /cogs/) {
- $nkey = "IC_expense";
- }
- if ($key =~ /sale/) {
- $nkey = "IC_income";
- }
- %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
- id => $ref->{id},
- description => $ref->{description}
- );
- }
- }
- }
- $sth->finish;
-
- $query = qq|SELECT c.id, c.accno, c.description
- FROM chart c
- WHERE c.category = 'I'
- AND c.charttype = 'A'
- ORDER BY c.accno|;
- $sth = $dbh->prepare($query);
- $sth->execute || $self->dberror($query);
-
- while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
- %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
- id => $ref->{id},
- description => $ref->{description}
- );
- }
- $sth->finish;
-
- $query = qq|SELECT c.id, c.accno, c.description
- FROM chart c
- WHERE c.category = 'E'
- AND c.charttype = 'A'
- ORDER BY c.accno|;
- $sth = $dbh->prepare($query);
- $sth->execute || $self->dberror($query);
-
- while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
- %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
- id => $ref->{id},
- description => $ref->{description}
- );
- }
- $sth->finish;
-
- # now get the tax rates and numbers
- $query = qq|SELECT c.id, c.accno, c.description,
- t.rate * 100 AS rate, t.taxnumber
- FROM chart c, tax t
- WHERE c.id = t.chart_id|;
-
- $sth = $dbh->prepare($query);
- $sth->execute || $form->dberror($query);
-
- while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
- $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
- $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
- $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
- if $ref->{taxnumber};
- $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
- }
- # Abfrage für Standard Umlaufvermögenskonto
- $query =
- qq|SELECT id, accno, description, link | .
- qq|FROM chart | .
- qq|WHERE link LIKE ? |.
- qq|ORDER BY accno|;
- $sth = prepare_execute_query($form, $dbh, $query, '%AR%');
- $sth->execute || $form->dberror($query);#
- while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
- foreach my $item (split(/:/, $ref->{link})) {
- if ($item eq "AR_paid") {
- %{ $form->{IC}{AR_paid}{ $ref->{accno} } } = (
- id => $ref->{id},
- description => $ref->{description}
- );
- }
- }
- }
-
- $sth->finish;
-
- #Get currencies:
- $query = qq|SELECT name AS curr FROM currencies ORDER BY id|;
- $form->{CURRENCIES} = selectall_hashref_query($form, $dbh, $query);
-
- #Which of them is the default currency?
- $query = qq|SELECT name AS defaultcurrency FROM currencies WHERE id = (SELECT currency_id FROM defaults LIMIT 1);|;
- ($form->{defaultcurrency}) = selectrow_query($form, $dbh, $query);
-
- $dbh->disconnect;
-
- $main::lxdebug->leave_sub();
-}
-