+ ($form->{orphaned}) = $sth->fetchrow_array;
+ $form->{orphaned} = !$form->{orphaned};
+ $sth->finish;
+
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+}
+
+sub save_warehouse {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form) = @_;
+
+ # connect to database
+ my $dbh = $form->dbconnect($myconfig);
+
+ $form->{description} =~ s/\'/\'\'/g;
+
+ if ($form->{id}) {
+ $query = qq|UPDATE warehouse SET
+ description = '$form->{description}'
+ WHERE id = $form->{id}|;
+ } else {
+ $query = qq|INSERT INTO warehouse
+ (description)
+ VALUES ('$form->{description}')|;
+ }
+ $dbh->do($query) || $form->dberror($query);
+
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+}
+
+sub delete_warehouse {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form) = @_;
+
+ # connect to database
+ my $dbh = $form->dbconnect($myconfig);
+
+ $query = qq|DELETE FROM warehouse
+ WHERE id = $form->{id}|;
+ $dbh->do($query) || $form->dberror($query);
+
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+}
+
+sub departments {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form) = @_;
+
+ # connect to database
+ my $dbh = $form->dbconnect($myconfig);
+
+ my $query = qq|SELECT d.id, d.description, d.role
+ FROM department d
+ ORDER BY 2|;
+
+ $sth = $dbh->prepare($query);
+ $sth->execute || $form->dberror($query);
+
+ while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+ push @{ $form->{ALL} }, $ref;
+ }
+
+ $sth->finish;
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+}
+
+sub get_department {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form) = @_;
+
+ # connect to database
+ my $dbh = $form->dbconnect($myconfig);
+
+ my $query = qq|SELECT d.description, d.role
+ FROM department d
+ WHERE d.id = $form->{id}|;
+ my $sth = $dbh->prepare($query);
+ $sth->execute || $form->dberror($query);
+
+ my $ref = $sth->fetchrow_hashref(NAME_lc);
+
+ map { $form->{$_} = $ref->{$_} } keys %$ref;
+
+ $sth->finish;
+
+ # see if it is in use
+ $query = qq|SELECT count(*) FROM dpt_trans d
+ WHERE d.department_id = $form->{id}|;
+ $sth = $dbh->prepare($query);
+ $sth->execute || $form->dberror($query);
+
+ ($form->{orphaned}) = $sth->fetchrow_array;
+ $form->{orphaned} = !$form->{orphaned};
+ $sth->finish;
+
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+}
+
+sub save_department {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form) = @_;
+
+ # connect to database
+ my $dbh = $form->dbconnect($myconfig);
+
+ $form->{description} =~ s/\'/\'\'/g;
+
+ if ($form->{id}) {
+ $query = qq|UPDATE department SET
+ description = '$form->{description}',
+ role = '$form->{role}'
+ WHERE id = $form->{id}|;
+ } else {
+ $query = qq|INSERT INTO department
+ (description, role)
+ VALUES ('$form->{description}', '$form->{role}')|;
+ }
+ $dbh->do($query) || $form->dberror($query);
+
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+}
+
+sub delete_department {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form) = @_;
+
+ # connect to database
+ my $dbh = $form->dbconnect($myconfig);
+
+ $query = qq|DELETE FROM department
+ WHERE id = $form->{id}|;
+ $dbh->do($query) || $form->dberror($query);
+
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+}
+
+sub lead {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form) = @_;
+
+ # connect to database
+ my $dbh = $form->dbconnect($myconfig);
+
+ my $query = qq|SELECT id, lead
+ FROM leads
+ ORDER BY 2|;
+
+ $sth = $dbh->prepare($query);
+ $sth->execute || $form->dberror($query);
+
+ while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+ push @{ $form->{ALL} }, $ref;
+ }
+
+ $sth->finish;
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+}
+
+sub get_lead {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form) = @_;
+
+ # connect to database
+ my $dbh = $form->dbconnect($myconfig);
+
+ my $query =
+ qq|SELECT l.id, l.lead
+ FROM leads l
+ WHERE l.id = $form->{id}|;
+ my $sth = $dbh->prepare($query);
+ $sth->execute || $form->dberror($query);
+
+ my $ref = $sth->fetchrow_hashref(NAME_lc);
+
+ map { $form->{$_} = $ref->{$_} } keys %$ref;
+
+ $sth->finish;
+
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+}
+
+sub save_lead {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form) = @_;
+
+ # connect to database
+ my $dbh = $form->dbconnect($myconfig);
+
+ $form->{lead} =~ s/\'/\'\'/g;
+
+ # id is the old record
+ if ($form->{id}) {
+ $query = qq|UPDATE leads SET
+ lead = '$form->{description}'
+ WHERE id = $form->{id}|;
+ } else {
+ $query = qq|INSERT INTO leads
+ (lead)
+ VALUES ('$form->{description}')|;
+ }
+ $dbh->do($query) || $form->dberror($query);
+
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+}
+
+sub delete_lead {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form) = @_;
+
+ # connect to database
+ my $dbh = $form->dbconnect($myconfig);
+
+ $query = qq|DELETE FROM leads
+ WHERE id = $form->{id}|;
+ $dbh->do($query) || $form->dberror($query);
+
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+}
+
+sub business {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form) = @_;
+
+ # connect to database
+ my $dbh = $form->dbconnect($myconfig);
+
+ my $query = qq|SELECT id, description, discount, customernumberinit, salesman
+ FROM business
+ ORDER BY 2|;
+
+ $sth = $dbh->prepare($query);
+ $sth->execute || $form->dberror($query);
+
+ while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+ push @{ $form->{ALL} }, $ref;
+ }
+
+ $sth->finish;
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+}
+
+sub get_business {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form) = @_;
+
+ # connect to database
+ my $dbh = $form->dbconnect($myconfig);
+
+ my $query =
+ qq|SELECT b.description, b.discount, b.customernumberinit, b.salesman
+ FROM business b
+ WHERE b.id = $form->{id}|;
+ my $sth = $dbh->prepare($query);
+ $sth->execute || $form->dberror($query);
+
+ my $ref = $sth->fetchrow_hashref(NAME_lc);
+
+ map { $form->{$_} = $ref->{$_} } keys %$ref;
+
+ $sth->finish;
+
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+}
+
+sub save_business {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form) = @_;
+
+ # connect to database
+ my $dbh = $form->dbconnect($myconfig);
+
+ $form->{description} =~ s/\'/\'\'/g;
+ $form->{discount} /= 100;
+ $form->{salesman} *= 1;
+
+ # id is the old record
+ if ($form->{id}) {
+ $query = qq|UPDATE business SET
+ description = '$form->{description}',
+ discount = $form->{discount},
+ customernumberinit = '$form->{customernumberinit}',
+ salesman = '$form->{salesman}'
+ WHERE id = $form->{id}|;
+ } else {
+ $query = qq|INSERT INTO business
+ (description, discount, customernumberinit, salesman)
+ VALUES ('$form->{description}', $form->{discount}, '$form->{customernumberinit}', '$form->{salesman}')|;
+ }
+ $dbh->do($query) || $form->dberror($query);
+
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+}
+
+sub delete_business {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form) = @_;
+
+ # connect to database
+ my $dbh = $form->dbconnect($myconfig);
+
+ $query = qq|DELETE FROM business
+ WHERE id = $form->{id}|;
+ $dbh->do($query) || $form->dberror($query);
+
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+}
+
+
+sub language {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form, $return_list) = @_;
+
+ # connect to database
+ my $dbh = $form->dbconnect($myconfig);
+
+ my $query =
+ "SELECT id, description, template_code, article_code, " .
+ " output_numberformat, output_dateformat, output_longdates " .
+ "FROM language ORDER BY description";
+
+ $sth = $dbh->prepare($query);
+ $sth->execute || $form->dberror($query);
+
+ my $ary = [];
+
+ while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+ push(@{ $ary }, $ref);
+ }
+
+ $sth->finish;
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+
+ if ($return_list) {
+ return @{$ary};
+ } else {
+ $form->{ALL} = $ary;
+ }
+}
+
+sub get_language {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form) = @_;
+
+ # connect to database
+ my $dbh = $form->dbconnect($myconfig);
+
+ my $query =
+ "SELECT description, template_code, article_code, " .
+ " output_numberformat, output_dateformat, output_longdates " .
+ "FROM language WHERE id = ?";
+ my $sth = $dbh->prepare($query);
+ $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
+
+ my $ref = $sth->fetchrow_hashref(NAME_lc);
+
+ map { $form->{$_} = $ref->{$_} } keys %$ref;
+
+ $sth->finish;
+
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+}
+
+sub get_language_details {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form, $id) = @_;
+
+ # connect to database
+ my $dbh = $form->dbconnect($myconfig);
+
+ my $query =
+ "SELECT template_code, " .
+ " output_numberformat, output_dateformat, output_longdates " .
+ "FROM language WHERE id = ?";
+ my @res = $dbh->selectrow_array($query, undef, $id);
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+
+ return @res;
+}
+
+sub save_language {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form) = @_;
+
+ # connect to database
+ my $dbh = $form->dbconnect($myconfig);
+ my (@values, $query);
+
+ map({ push(@values, $form->{$_}); }
+ qw(description template_code article_code
+ output_numberformat output_dateformat output_longdates));
+
+ # id is the old record
+ if ($form->{id}) {
+ $query =
+ "UPDATE language SET " .
+ " description = ?, template_code = ?, article_code = ?, " .
+ " output_numberformat = ?, output_dateformat = ?, " .
+ " output_longdates = ? " .
+ "WHERE id = ?";
+ push(@values, $form->{id});
+ } else {
+ $query =
+ "INSERT INTO language (" .
+ " description, template_code, article_code, " .
+ " output_numberformat, output_dateformat, output_longdates" .
+ ") VALUES (?, ?, ?, ?, ?, ?)";
+ }
+ $dbh->do($query, undef, @values) ||
+ $form->dberror($query . " (" . join(", ", @values) . ")");
+
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+}
+
+sub delete_language {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form) = @_;
+
+ # connect to database
+ my $dbh = $form->dbconnect_noauto($myconfig);
+
+ foreach my $table (qw(translation_payment_terms units_language)) {
+ my $query = qq|DELETE FROM $table WHERE language_id = ?|;
+ do_query($form, $dbh, $query, $form->{"id"});
+ }
+
+ $query = "DELETE FROM language WHERE id = ?";
+ do_query($form, $dbh, $query, $form->{"id"});
+
+ $dbh->commit();
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+}
+
+
+sub buchungsgruppe {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form) = @_;
+
+ # connect to database
+ my $dbh = $form->dbconnect($myconfig);
+
+ my $query = qq|SELECT id, description,
+ inventory_accno_id,
+ (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
+ income_accno_id_0,
+ (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
+ expense_accno_id_0,
+ (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
+ income_accno_id_1,
+ (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
+ expense_accno_id_1,
+ (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
+ income_accno_id_2,
+ (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
+ expense_accno_id_2,
+ (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
+ income_accno_id_3,
+ (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
+ expense_accno_id_3,
+ (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
+ FROM buchungsgruppen
+ ORDER BY sortkey|;
+
+ $sth = $dbh->prepare($query);
+ $sth->execute || $form->dberror($query);
+
+ $form->{ALL} = [];
+ while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+ push @{ $form->{ALL} }, $ref;
+ }
+
+ $sth->finish;
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+}
+
+sub get_buchungsgruppe {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form) = @_;
+
+ # connect to database
+ my $dbh = $form->dbconnect($myconfig);
+
+ if ($form->{id}) {
+ my $query =
+ qq|SELECT description, inventory_accno_id,
+ (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
+ income_accno_id_0,
+ (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
+ expense_accno_id_0,
+ (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
+ income_accno_id_1,
+ (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
+ expense_accno_id_1,
+ (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
+ income_accno_id_2,
+ (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
+ expense_accno_id_2,
+ (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
+ income_accno_id_3,
+ (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
+ expense_accno_id_3,
+ (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
+ FROM buchungsgruppen
+ WHERE id = ?|;
+ my $sth = $dbh->prepare($query);
+ $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
+
+ my $ref = $sth->fetchrow_hashref(NAME_lc);
+
+ map { $form->{$_} = $ref->{$_} } keys %$ref;
+
+ $sth->finish;
+
+ my $query =
+ qq|SELECT count(id) = 0 AS orphaned
+ FROM parts
+ WHERE buchungsgruppen_id = ?|;
+ ($form->{orphaned}) = $dbh->selectrow_array($query, undef, $form->{id});
+ $form->dberror($query . " ($form->{id})") if ($dbh->err);
+ }
+
+ $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
+ "FROM defaults";
+ ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
+ $form->{"std_expense_accno_id"}) = $dbh->selectrow_array($query);
+
+ my $module = "IC";
+ $query = qq|SELECT c.accno, c.description, c.link, c.id,
+ d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
+ FROM chart c, defaults d
+ WHERE c.link LIKE '%$module%'
+ ORDER BY c.accno|;
+
+
+ my $sth = $dbh->prepare($query);
+ $sth->execute || $form->dberror($query);
+ while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
+ foreach my $key (split(/:/, $ref->{link})) {
+ if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
+ $form->{"std_inventory_accno_id"} = $ref->{"id"};
+ }
+ if ($key =~ /$module/) {
+ if ( ($ref->{id} eq $ref->{inventory_accno_id})
+ || ($ref->{id} eq $ref->{income_accno_id})
+ || ($ref->{id} eq $ref->{expense_accno_id})) {
+ push @{ $form->{"${module}_links"}{$key} },
+ { accno => $ref->{accno},
+ description => $ref->{description},
+ selected => "selected",
+ id => $ref->{id} };
+ } else {
+ push @{ $form->{"${module}_links"}{$key} },
+ { accno => $ref->{accno},
+ description => $ref->{description},
+ selected => "",
+ id => $ref->{id} };
+ }
+ }
+ }
+ }