+ $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
+
+ $main::lxdebug->leave_sub();
+
+ return scalar(@{ $self->{name_list} });
+}
+
+sub new_lastmtime {
+
+ my ($self, $table, $provided_dbh) = @_;
+
+ my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh;
+ return unless $self->{id};
+ croak ("wrong call, no valid table defined") unless $table =~ /^(oe|ar|ap|delivery_orders|parts)$/;
+
+ my $query = "SELECT mtime, itime FROM " . $table . " WHERE id = ?";
+ my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
+ $ref->{mtime} ||= $ref->{itime};
+ $self->{lastmtime} = $ref->{mtime};
+
+}
+
+sub mtime_ischanged {
+ my ($self, $table, $option) = @_;
+
+ return unless $self->{id};
+ croak ("wrong call, no valid table defined") unless $table =~ /^(oe|ar|ap|delivery_orders|parts)$/;
+
+ my $query = "SELECT mtime, itime FROM " . $table . " WHERE id = ?";
+ my $ref = selectfirst_hashref_query($self, $self->get_standard_dbh, $query, $self->{id});
+ $ref->{mtime} ||= $ref->{itime};
+
+ if ($self->{lastmtime} && $self->{lastmtime} ne $ref->{mtime} ) {
+ $self->error(($option eq 'mail') ?
+ t8("The document has been changed by another user. No mail was sent. Please reopen it in another window and copy the changes to the new window") :
+ t8("The document has been changed by another user. Please reopen it in another window and copy the changes to the new window")
+ );
+ $::dispatcher->end_request;
+ }
+}
+
+# language_payment duplicates some of the functionality of all_vc (language,
+# printer, payment_terms), and at least in the case of sales invoices both
+# all_vc and language_payment are called when adding new invoices
+sub language_payment {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig) = @_;
+
+ my $dbh = $self->get_standard_dbh($myconfig);
+ # get languages
+ my $query = qq|SELECT id, description
+ FROM language
+ ORDER BY id|;
+
+ $self->{languages} = selectall_hashref_query($self, $dbh, $query);
+
+ # get printer
+ $query = qq|SELECT printer_description, id
+ FROM printers
+ ORDER BY printer_description|;
+
+ $self->{printers} = selectall_hashref_query($self, $dbh, $query);
+
+ # get payment terms
+ $query = qq|SELECT id, description
+ FROM payment_terms
+ WHERE ( obsolete IS FALSE OR id = ? )
+ ORDER BY sortkey |;
+ $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query, $self->{payment_id} || undef);
+
+ # get buchungsgruppen
+ $query = qq|SELECT id, description
+ FROM buchungsgruppen|;
+
+ $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
+
+ $main::lxdebug->leave_sub();
+}
+
+# this is only used for reports
+sub all_departments {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $table) = @_;
+
+ my $dbh = $self->get_standard_dbh($myconfig);
+
+ my $query = qq|SELECT id, description
+ FROM department
+ ORDER BY description|;
+ $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
+
+ delete($self->{all_departments}) unless (@{ $self->{all_departments} || [] });
+
+ $main::lxdebug->leave_sub();
+}
+
+sub create_links {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
+
+ my ($fld, $arap);
+ if ($table eq "customer") {
+ $fld = "buy";
+ $arap = "ar";
+ } else {
+ $table = "vendor";
+ $fld = "sell";
+ $arap = "ap";
+ }
+
+ # get last customers or vendors
+ my ($query, $sth, $ref);
+
+ my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
+ my %xkeyref = ();
+
+ if (!$self->{id}) {
+
+ my $transdate = "current_date";
+ if ($self->{transdate}) {
+ $transdate = $dbh->quote($self->{transdate});
+ }
+
+ # now get the account numbers
+ $query = qq|
+ SELECT c.accno, c.description, c.link, c.taxkey_id, c.id AS chart_id, tk2.tax_id
+ FROM chart c
+ -- find newest entries in taxkeys
+ INNER JOIN (
+ SELECT chart_id, MAX(startdate) AS startdate
+ FROM taxkeys
+ WHERE (startdate <= $transdate)
+ GROUP BY chart_id
+ ) tk ON (c.id = tk.chart_id)
+ -- and load all of those entries
+ INNER JOIN taxkeys tk2
+ ON (tk.chart_id = tk2.chart_id AND tk.startdate = tk2.startdate)
+ WHERE (c.link LIKE ?)
+ ORDER BY c.accno|;
+
+ $sth = $dbh->prepare($query);
+
+ do_statement($self, $sth, $query, like($module));
+
+ $self->{accounts} = "";
+ while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
+
+ foreach my $key (split(/:/, $ref->{link})) {
+ if ($key =~ /\Q$module\E/) {
+
+ # cross reference for keys
+ $xkeyref{ $ref->{accno} } = $key;
+
+ push @{ $self->{"${module}_links"}{$key} },
+ { accno => $ref->{accno},
+ chart_id => $ref->{chart_id},
+ description => $ref->{description},
+ taxkey => $ref->{taxkey_id},
+ tax_id => $ref->{tax_id} };
+
+ $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
+ }
+ }
+ }
+ }
+
+ # get taxkeys and description
+ $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
+ $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
+
+ if (($module eq "AP") || ($module eq "AR")) {
+ # get tax rates and description
+ $query = qq|SELECT * FROM tax|;
+ $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
+ }
+
+ my $extra_columns = '';
+ $extra_columns .= 'a.direct_debit, ' if ($module eq 'AR') || ($module eq 'AP');
+
+ if ($self->{id}) {
+ $query =
+ qq|SELECT
+ a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
+ a.duedate, a.ordnumber, a.taxincluded, (SELECT cu.name FROM currencies cu WHERE cu.id=a.currency_id) AS currency, a.notes,
+ a.mtime, a.itime,
+ a.intnotes, a.department_id, a.amount AS oldinvtotal,
+ a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
+ a.globalproject_id, ${extra_columns}
+ c.name AS $table,
+ d.description AS department,
+ e.name AS employee
+ FROM $arap a
+ JOIN $table c ON (a.${table}_id = c.id)
+ LEFT JOIN employee e ON (e.id = a.employee_id)
+ LEFT JOIN department d ON (d.id = a.department_id)
+ WHERE a.id = ?|;
+ $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
+
+ foreach my $key (keys %$ref) {
+ $self->{$key} = $ref->{$key};
+ }
+ $self->{mtime} ||= $self->{itime};
+ $self->{lastmtime} = $self->{mtime};