Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision ef17e41a

Von Moritz Bunkus vor fast 18 Jahren hinzugefügt

  • ID ef17e41a8364c6c97566a054768f573659dbec79
  • Vorgänger 1514a9d0
  • Nachfolger 209f6291

Umstellung der Form.pm auf die Verwendung parametrisierter Queries zur Vermeidung von SQL injection. Zusätzlich etwas Kosmetik (trailing whitespace, TABs entfernt).

Unterschiede anzeigen:

SL/Form.pm
170 170
sub escape {
171 171
  $main::lxdebug->enter_sub(2);
172 172

  
173
  my ($self, $str, $beenthere) = @_;
174

  
175
  # for Apache 2 we escape strings twice
176
  #if (($ENV{SERVER_SOFTWARE} =~ /Apache\/2/) && !$beenthere) {
177
  #  $str = $self->escape($str, 1);
178
  #}
173
  my ($self, $str) = @_;
179 174

  
180 175
  $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
181 176

  
......
244 239
  my $self = shift;
245 240

  
246 241
  if (@_) {
247
    for (@_) {
248
      print qq|<input type=hidden name="$_" value="|
249
        . $self->quote($self->{$_})
250
        . qq|">\n|;
251
    }
242
    map({ print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
252 243
  } else {
253
    delete $self->{header};
254 244
    for (sort keys %$self) {
255
      print qq|<input type=hidden name="$_" value="|
256
        . $self->quote($self->{$_})
257
        . qq|">\n|;
245
      next if (($_ eq "header") || (ref($self->{$_}) ne ""));
246
      print($main::cgi->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
258 247
    }
259 248
  }
260 249

  
......
430 419
  <meta name="robots" content="noindex,nofollow" />
431 420
  <script type="text/javascript" src="js/highlight_input.js"></script>
432 421
  <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
433
  
422

  
434 423
  <script type="text/javascript" src="js/tabcontent.js">
435
  
424

  
436 425
  /***********************************************
437 426
  * Tab Content script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
438 427
  * This notice MUST stay intact for legal use
439 428
  * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
440 429
  ***********************************************/
441
  
430

  
442 431
  </script>
443 432

  
444 433
  $extra_code
......
597 586
      {
598 587
      inputField : "| . (shift) . qq|",
599 588
      ifFormat :"$ifFormat",
600
      align : "| .  (shift) . qq|", 
589
      align : "| .  (shift) . qq|",
601 590
      button : "| . (shift) . qq|"
602 591
      }
603 592
      );
......
648 637
  $main::lxdebug->enter_sub(2);
649 638

  
650 639
  my ($self, $myconfig, $amount, $places, $dash) = @_;
651
  
640

  
652 641
  if ($amount eq "") {
653 642
    $amount = 0;
654 643
  }
......
680 669
    ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
681 670
                        ($neg ? "-$amount"   : "$amount" )    ;
682 671
  };
683
    
672

  
684 673

  
685 674
  $main::lxdebug->leave_sub(2);
686 675
  return $amount;
......
750 739
             (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
751 740
    $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
752 741
  } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
753
    $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);  
742
    $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
754 743
  } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
755 744
    $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
756 745
  } elsif ( defined $self->{'format'}) {
......
758 747
  } elsif ( $self->{'format'} eq '' ) {
759 748
    $self->error("No Outputformat given: $self->{'format'}");
760 749
  } else { #Catch the rest
761
    $self->error("Outputformat not defined: $self->{'format'}");  
750
    $self->error("Outputformat not defined: $self->{'format'}");
762 751
  }
763 752

  
764 753
  # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
......
1004 993
sub update_balance {
1005 994
  $main::lxdebug->enter_sub();
1006 995

  
1007
  my ($self, $dbh, $table, $field, $where, $value) = @_;
996
  my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
1008 997

  
1009 998
  # if we have a value, go do it
1010 999
  if ($value != 0) {
1011 1000

  
1012 1001
    # retrieve balance from table
1013 1002
    my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1014
    my $sth   = $dbh->prepare($query);
1015

  
1016
    $sth->execute || $self->dberror($query);
1003
    my $sth = prepare_execute_query($self, $dbh, $query, @values);
1017 1004
    my ($balance) = $sth->fetchrow_array;
1018 1005
    $sth->finish;
1019 1006

  
......
1021 1008

  
1022 1009
    # update balance
1023 1010
    $query = "UPDATE $table SET $field = $balance WHERE $where";
1024
    $dbh->do($query) || $self->dberror($query);
1011
    do_query($self, $dbh, $query, @values);
1025 1012
  }
1026 1013
  $main::lxdebug->leave_sub();
1027 1014
}
......
1038 1025
  }
1039 1026

  
1040 1027
  my $query = qq|SELECT e.curr FROM exchangerate e
1041
                 WHERE e.curr = '$curr'
1042
	         AND e.transdate = '$transdate'
1043
		 FOR UPDATE|;
1044
  my $sth = $dbh->prepare($query);
1045
  $sth->execute || $self->dberror($query);
1028
                 WHERE e.curr = ? AND e.transdate = ?
1029
                 FOR UPDATE|;
1030
  my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
1046 1031

  
1047 1032
  my $set;
1048 1033
  if ($buy != 0 && $sell != 0) {
......
1056 1041
  if ($sth->fetchrow_array) {
1057 1042
    $query = qq|UPDATE exchangerate
1058 1043
                SET $set
1059
		WHERE curr = '$curr'
1060
		AND transdate = '$transdate'|;
1044
                WHERE curr = ?
1045
                AND transdate = ?|;
1061 1046
  } else {
1062 1047
    $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1063
                VALUES ('$curr', $buy, $sell, '$transdate')|;
1048
                VALUES (?, $buy, $sell, ?)|;
1064 1049
  }
1065 1050
  $sth->finish;
1066
  $dbh->do($query) || $self->dberror($query);
1051
  do_query($self, $dbh, $query, $curr, $transdate);
1067 1052

  
1068 1053
  $main::lxdebug->leave_sub();
1069 1054
}
......
1097 1082
  }
1098 1083

  
1099 1084
  my $query = qq|SELECT e.$fld FROM exchangerate e
1100
                 WHERE e.curr = '$curr'
1101
		 AND e.transdate = '$transdate'|;
1102
  my $sth = $dbh->prepare($query);
1103
  $sth->execute || $self->dberror($query);
1104

  
1105
  my ($exchangerate) = $sth->fetchrow_array;
1106
  $sth->finish;
1085
                 WHERE e.curr = ? AND e.transdate = ?|;
1086
  my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
1107 1087

  
1108 1088
  if (!$exchangerate) {
1109 1089
    $exchangerate = 1;
......
1114 1094
  return $exchangerate;
1115 1095
}
1116 1096

  
1097
sub check_exchangerate {
1098
  $main::lxdebug->enter_sub();
1099

  
1100
  my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1101

  
1102
  unless ($transdate) {
1103
    $main::lxdebug->leave_sub();
1104
    return "";
1105
  }
1106

  
1107
  my $dbh = $self->dbconnect($myconfig);
1108

  
1109
  my $query = qq|SELECT e.$fld FROM exchangerate e
1110
                 WHERE e.curr = ? AND e.transdate = ?|;
1111
  my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1112
  $dbh->disconnect;
1113

  
1114
  $main::lxdebug->leave_sub();
1115

  
1116
  return $exchangerate;
1117
}
1118

  
1117 1119
sub set_payment_options {
1118 1120
  $main::lxdebug->enter_sub();
1119 1121

  
......
1124 1126
    my $dbh = $self->dbconnect($myconfig);
1125 1127

  
1126 1128
    my $query =
1127
      qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, | .
1128
      qq|p.description_long | .
1129
      qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long | .
1129 1130
      qq|FROM payment_terms p | .
1130 1131
      qq|WHERE p.id = ?|;
1131 1132

  
......
1142 1143
    }
1143 1144

  
1144 1145
    $query =
1145
      qq|SELECT date '$transdate' + $self->{terms_netto} AS netto_date, | .
1146
      qq|date '$transdate' + $self->{terms_skonto} AS skonto_date | .
1147
      qq|FROM payment_terms LIMIT 1|;
1146
      qq|SELECT ?::date + ?::integer AS netto_date, ?::date + ?::integer AS skonto_date | .
1147
      qq|FROM payment_terms|;
1148 1148
    ($self->{netto_date}, $self->{skonto_date}) =
1149
      selectrow_query($self, $dbh, $query);
1149
      selectrow_query($self, $dbh, $query, $transdate, $self->{terms_netto}, $transdate, $self->{terms_skonto});
1150 1150

  
1151 1151
    my $total = ($self->{invtotal}) ? $self->{invtotal} : $self->{ordtotal};
1152 1152
    my $skonto_amount = $self->parse_amount($myconfig, $total) *
......
1157 1157

  
1158 1158
    if ($self->{"language_id"}) {
1159 1159
      $query =
1160
        qq|SELECT t.description_long, | .
1161
        qq|l.output_numberformat, l.output_dateformat, l.output_longdates | .
1160
        qq|SELECT t.description_long, l.output_numberformat, l.output_dateformat, l.output_longdates | .
1162 1161
        qq|FROM translation_payment_terms t | .
1163 1162
        qq|LEFT JOIN language l ON t.language_id = l.id | .
1164 1163
        qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
......
1206 1205

  
1207 1206
}
1208 1207

  
1209
sub check_exchangerate {
1210
  $main::lxdebug->enter_sub();
1211

  
1212
  my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1213

  
1214
  unless ($transdate) {
1215
    $main::lxdebug->leave_sub();
1216
    return "";
1217
  }
1218

  
1219
  my $dbh = $self->dbconnect($myconfig);
1220

  
1221
  my $query = qq|SELECT e.$fld FROM exchangerate e
1222
                 WHERE e.curr = '$currency'
1223
		 AND e.transdate = '$transdate'|;
1224
  my $sth = $dbh->prepare($query);
1225
  $sth->execute || $self->dberror($query);
1226

  
1227
  my ($exchangerate) = $sth->fetchrow_array;
1228
  $sth->finish;
1229
  $dbh->disconnect;
1230

  
1231
  $main::lxdebug->leave_sub();
1232

  
1233
  return $exchangerate;
1234
}
1235

  
1236 1208
sub get_template_language {
1237 1209
  $main::lxdebug->enter_sub();
1238 1210

  
......
1241 1213
  my $template_code = "";
1242 1214

  
1243 1215
  if ($self->{language_id}) {
1244

  
1245 1216
    my $dbh = $self->dbconnect($myconfig);
1246

  
1247

  
1248
    my $query = qq|SELECT l.template_code FROM language l
1249
                  WHERE l.id = $self->{language_id}|;
1250
    my $sth = $dbh->prepare($query);
1251
    $sth->execute || $self->dberror($query);
1252
  
1253
    ($template_code) = $sth->fetchrow_array;
1254
    $sth->finish;
1217
    my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1218
    ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1255 1219
    $dbh->disconnect;
1256 1220
  }
1257 1221

  
......
1268 1232
  my $template_code = "";
1269 1233

  
1270 1234
  if ($self->{printer_id}) {
1271

  
1272 1235
    my $dbh = $self->dbconnect($myconfig);
1273

  
1274

  
1275
    my $query = qq|SELECT p.template_code,p.printer_command FROM printers p
1276
                  WHERE p.id = $self->{printer_id}|;
1277
    my $sth = $dbh->prepare($query);
1278
    $sth->execute || $self->dberror($query);
1279
  
1280
    ($template_code, $self->{printer_command}) = $sth->fetchrow_array;
1281
    $sth->finish;
1236
    my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1237
    ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1282 1238
    $dbh->disconnect;
1283 1239
  }
1284 1240

  
......
1295 1251
  my $template_code = "";
1296 1252

  
1297 1253
  if ($self->{shipto_id}) {
1298

  
1299 1254
    my $dbh = $self->dbconnect($myconfig);
1300

  
1301

  
1302
    my $query = qq|SELECT s.* FROM shipto s
1303
                  WHERE s.shipto_id = $self->{shipto_id}|;
1304
    my $sth = $dbh->prepare($query);
1305
    $sth->execute || $self->dberror($query);
1306
    $ref = $sth->fetchrow_hashref(NAME_lc);
1307
    map { $self->{$_} = $ref->{$_} } keys %$ref;
1308
    $sth->finish;  
1255
    my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1256
    my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1257
    map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1309 1258
    $dbh->disconnect;
1310 1259
  }
1311 1260

  
1312 1261
  $main::lxdebug->leave_sub();
1313

  
1314 1262
}
1315 1263

  
1316 1264
sub add_shipto {
1317 1265
  $main::lxdebug->enter_sub();
1318 1266

  
1319 1267
  my ($self, $dbh, $id, $module) = @_;
1320
##LINET
1268

  
1321 1269
  my $shipto;
1322
  foreach my $item (
1323
    qw(name department_1 department_2 street zipcode city country contact phone fax email)
1324
    ) {
1270
  my @values;
1271
  foreach my $item (qw(name department_1 department_2 street zipcode city country
1272
                       contact phone fax email)) {
1325 1273
    if ($self->{"shipto$item"}) {
1326 1274
      $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1327 1275
    }
1328
    $self->{"shipto$item"} =~ s/\'/\'\'/g;
1276
    push(@values, $self->{"shipto${item}"});
1329 1277
  }
1330 1278
  if ($shipto) {
1331 1279
    if ($self->{shipto_id}) {
1332
      my $query = qq| UPDATE shipto set
1333
                      shiptoname = '$self->{shiptoname}',
1334
                      shiptodepartment_1 = '$self->{shiptodepartment_1}',
1335
                      shiptodepartment_2 = '$self->{shiptodepartment_2}',
1336
                      shiptostreet = '$self->{shiptostreet}',
1337
                      shiptozipcode = '$self->{shiptozipcode}',
1338
                      shiptocity = '$self->{shiptocity}',
1339
                      shiptocountry = '$self->{shiptocountry}',
1340
                      shiptocontact = '$self->{shiptocontact}',
1341
                      shiptophone = '$self->{shiptophone}',
1342
                      shiptofax = '$self->{shiptofax}',
1343
                      shiptoemail = '$self->{shiptoemail}'
1344
                      WHERE shipto_id = $self->{shipto_id}|;
1345
      $dbh->do($query) || $self->dberror($query);
1280
      my $query = qq|UPDATE shipto set
1281
                       shiptoname = ?,
1282
                       shiptodepartment_1 = ?,
1283
                       shiptodepartment_2 = ?,
1284
                       shiptostreet = ?,
1285
                       shiptozipcode = ?,
1286
                       shiptocity = ?,
1287
                       shiptocountry = ?,
1288
                       shiptocontact = ?,
1289
                       shiptophone = ?,
1290
                       shiptofax = ?,
1291
                       shiptoemail = ?
1292
                     WHERE shipto_id = ?|;
1293
      do_query($self, $dbh, $query, @values, $self->{shipto_id});
1346 1294
    } else {
1347 1295
      my $query = qq|SELECT * FROM shipto
1348
                                 WHERE shiptoname = '$self->{shiptoname}' AND
1349
                      shiptodepartment_1 = '$self->{shiptodepartment_1}' AND
1350
                      shiptodepartment_2 = '$self->{shiptodepartment_2}' AND
1351
                      shiptostreet = '$self->{shiptostreet}' AND
1352
                      shiptozipcode = '$self->{shiptozipcode}' AND
1353
                      shiptocity = '$self->{shiptocity}' AND
1354
                      shiptocountry = '$self->{shiptocountry}' AND
1355
                      shiptocontact = '$self->{shiptocontact}' AND
1356
                      shiptophone = '$self->{shiptophone}' AND
1357
                      shiptofax = '$self->{shiptofax}' AND
1358
                      shiptoemail = '$self->{shiptoemail}'
1359
                      | ;
1360
      my $sth = $dbh->prepare($query);
1361
      $sth->execute() || $self->dberror($query);
1362
      my $insert_check = $sth->fetch();
1363
      $sth->finish();
1296
                     WHERE shiptoname = ? AND
1297
                       shiptodepartment_1 = ? AND
1298
                       shiptodepartment_2 = ? AND
1299
                       shiptostreet = ? AND
1300
                       shiptozipcode = ? AND
1301
                       shiptocity = ? AND
1302
                       shiptocountry = ? AND
1303
                       shiptocontact = ? AND
1304
                       shiptophone = ? AND
1305
                       shiptofax = ? AND
1306
                       shiptoemail = ?|;
1307
      my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values);
1364 1308
      if(!$insert_check){
1365 1309
        $query =
1366
        qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1,
1367
                   shiptodepartment_2, shiptostreet,
1368
                   shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1369
		   shiptophone, shiptofax, shiptoemail, module) VALUES ($id,
1370
		   '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1371
		   '$self->{shiptozipcode}', '$self->{shiptocity}',
1372
		   '$self->{shiptocountry}', '$self->{shiptocontact}',
1373
		   '$self->{shiptophone}', '$self->{shiptofax}',
1374
                   '$self->{shiptoemail}', '$module')|;              
1375
        $dbh->do($query) || $self->dberror($query);
1310
          qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
1311
                                 shiptostreet, shiptozipcode, shiptocity, shiptocountry,
1312
                                 shiptocontact, shiptophone, shiptofax, shiptoemail, module)
1313
             VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1314
        do_query($self, $dbh, $query, $id, @values, $module);
1376 1315
     }
1377 1316
    }
1378 1317
  }
1379
##/LINET
1318

  
1380 1319
  $main::lxdebug->leave_sub();
1381 1320
}
1382 1321

  
......
1385 1324

  
1386 1325
  my ($self, $dbh) = @_;
1387 1326

  
1388
  my $query = qq|SELECT e.id, e.name FROM employee e
1389
                 WHERE e.login = '$self->{login}'|;
1390
  my $sth = $dbh->prepare($query);
1391
  $sth->execute || $self->dberror($query);
1392

  
1393
  ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1327
  my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
1328
  ($self->{employee_id}, $self->{employee}) = selectrow_query($self, $dbh, $query, $self->{login});
1394 1329
  $self->{employee_id} *= 1;
1395 1330

  
1396
  $sth->finish;
1397

  
1398 1331
  $main::lxdebug->leave_sub();
1399 1332
}
1400 1333

  
......
1435 1368
  my ($self, $myconfig) = @_;
1436 1369

  
1437 1370
  my $dbh = $self->dbconnect($myconfig);
1438
  my $query = qq|SELECT current_date+terms_netto FROM payment_terms
1439
                 WHERE id = '$self->{payment_id}'|;
1440
  my $sth = $dbh->prepare($query);
1441
  $sth->execute || $self->dberror($query);
1442

  
1443
  ($self->{duedate}) = $sth->fetchrow_array;
1444

  
1445
  $sth->finish;
1371
  my $query = qq|SELECT current_date + terms_netto FROM payment_terms WHERE id = ?|;
1372
  ($self->{duedate}) = selectrow_query($self, $dbh, $query, $self->{payment_id});
1373
  $dbh->disconnect();
1446 1374

  
1447 1375
  $main::lxdebug->leave_sub();
1448 1376
}
1449 1377

  
1450
# get contacts for id, if no contact return {"","","","",""}
1451 1378
sub _get_contacts {
1452 1379
  $main::lxdebug->enter_sub();
1453 1380

  
1454 1381
  my ($self, $dbh, $id, $key) = @_;
1455 1382

  
1456 1383
  $key = "all_contacts" unless ($key);
1457
  $self->{$key} = [];
1458 1384

  
1459 1385
  my $query =
1460
    qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname, c.cp_abteilung | .
1461
    qq|FROM contacts c | .
1386
    qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
1387
    qq|FROM contacts | .
1462 1388
    qq|WHERE cp_cv_id = ? | .
1463
    qq|ORDER BY lower(c.cp_name)|;
1464
  my $sth = $dbh->prepare($query);
1465
  $sth->execute($id) || $self->dberror($query . " ($id)");
1389
    qq|ORDER BY lower(cp_name)|;
1466 1390

  
1467
  my $i = 0;
1468
  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1469
    push @{ $self->{$key} }, $ref;
1470
    $i++;
1471
  }
1391
  $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
1472 1392

  
1473
  if ($i == 0) {
1474
    push @{ $self->{$key} }, { { "", "", "", "", "", "" } };
1475
  }
1476
  $sth->finish;
1477 1393
  $main::lxdebug->leave_sub();
1478 1394
}
1479 1395

  
......
1521 1437
    qq|FROM project | .
1522 1438
    $where .
1523 1439
    qq|ORDER BY lower(projectnumber)|;
1524
  my $sth = $dbh->prepare($query);
1525
  $sth->execute(@values) ||
1526
    $self->dberror($query . " (" . join(", ", @values) . ")");
1527 1440

  
1528
  $self->{$key} = [];
1529
  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1530
    push(@{ $self->{$key} }, $ref);
1531
  }
1441
  $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
1532 1442

  
1533
  $sth->finish;
1534 1443
  $main::lxdebug->leave_sub();
1535 1444
}
1536 1445

  
......
1540 1449
  my ($self, $dbh, $vc_id, $key) = @_;
1541 1450

  
1542 1451
  $key = "all_shipto" unless ($key);
1543
  $self->{$key} = [];
1544 1452

  
1545 1453
  # get shipping addresses
1546 1454
  my $query =
1547
    qq|SELECT s.shipto_id,s.shiptoname,s.shiptodepartment_1 | .
1548
    qq|FROM shipto s | .
1549
    qq|WHERE s.trans_id = ?|;
1550
  my $sth = $dbh->prepare($query);
1551
  $sth->execute($vc_id) || $self->dberror($query . " ($vc_id)");
1455
    qq|SELECT shipto_id, shiptoname, shiptodepartment_1 | .
1456
    qq|FROM shipto | .
1457
    qq|WHERE trans_id = ?|;
1552 1458

  
1553
  while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1554
    push(@{ $self->{$key} }, $ref);
1555
  }
1556
  $sth->finish;
1459
  $self->{$key} = selectall_hashref_query($self, $dbh, $query, $vc_id);
1557 1460

  
1558 1461
  $main::lxdebug->leave_sub();
1559 1462
}
......
1564 1467
  my ($self, $dbh, $key) = @_;
1565 1468

  
1566 1469
  $key = "all_printers" unless ($key);
1567
  $self->{$key} = [];
1568 1470

  
1569 1471
  my $query = qq|SELECT id, printer_description, printer_command FROM printers|;
1570
  my $sth = $dbh->prepare($query);
1571
  $sth->execute() || $self->dberror($query);
1572 1472

  
1573
  while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1574
    push(@{ $self->{$key} }, $ref);
1575
  }
1576
  $sth->finish;
1473
  $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1577 1474

  
1578 1475
  $main::lxdebug->leave_sub();
1579 1476
}
......
1585 1482

  
1586 1483
  $key = $params->{key};
1587 1484
  $key = "all_charts" unless ($key);
1588
  $self->{$key} = [];
1589 1485

  
1590 1486
  my $transdate = quote_db_date($params->{transdate});
1591 1487

  
......
1598 1494
    qq|          ORDER BY startdate DESC LIMIT 1)) | .
1599 1495
    qq|ORDER BY c.accno|;
1600 1496

  
1601
  my $sth = $dbh->prepare($query);
1602
  $sth->execute() || $self->dberror($query);
1603

  
1604
  while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1605
    push(@{ $self->{$key} }, $ref);
1606
  }
1607
  $sth->finish;
1497
  $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1608 1498

  
1609 1499
  $main::lxdebug->leave_sub();
1610 1500
}
......
1615 1505
  my ($self, $dbh, $key) = @_;
1616 1506

  
1617 1507
  $key = "all_taxcharts" unless ($key);
1618
  $self->{$key} = [];
1619 1508

  
1620 1509
  my $query = qq|SELECT * FROM tax ORDER BY taxkey|;
1621 1510

  
1622
  my $sth = $dbh->prepare($query);
1623
  $sth->execute() || $self->dberror($query);
1624

  
1625
  while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1626
    push(@{ $self->{$key} }, $ref);
1627
  }
1628
  $sth->finish;
1511
  $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1629 1512

  
1630 1513
  $main::lxdebug->leave_sub();
1631 1514
}
......
1714 1597
  # connect to database
1715 1598
  my $dbh = $self->dbconnect($myconfig);
1716 1599

  
1717
  my $name           = $self->like(lc $self->{$table});
1718
  my $customernumber = $self->like(lc $self->{customernumber});
1600
  $table = $table eq "customer" ? "customer" : "vendor";
1601
  my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
1719 1602

  
1720
  if ($self->{customernumber} ne "") {
1721
    $query = qq~SELECT c.id, c.name,
1722
                  c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1723
                  FROM $table c
1724
                  WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1725
                  ORDER BY c.name~;
1726
  } else {
1727
    $query = qq~SELECT c.id, c.name,
1728
                 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1729
                 FROM $table c
1730
		 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1731
		 ORDER BY c.name~;
1732
  }
1603
  my ($query, @values);
1733 1604

  
1734
  if ($self->{openinvoices}) {
1735
    $query = qq~SELECT DISTINCT c.id, c.name,
1736
                c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1737
		FROM $self->{arap} a
1738
		JOIN $table c ON (a.${table}_id = c.id)
1739
		WHERE NOT a.amount = a.paid
1740
		AND lower(c.name) LIKE '$name'
1741
		ORDER BY c.name~;
1742
  }
1743
  my $sth = $dbh->prepare($query);
1744

  
1745
  $sth->execute || $self->dberror($query);
1605
  if (!$self->{openinvoices}) {
1606
    my $where;
1607
    if ($self->{customernumber} ne "") {
1608
      $where = qq|(vc.customernumber ILIKE ?)|;
1609
      push(@values, '%' . $self->{customernumber} . '%');
1610
    } else {
1611
      $where = qq|(vc.name ILIKE ?)|;
1612
      push(@values, '%' . $self->{$table} . '%');
1613
    }
1746 1614

  
1747
  my $i = 0;
1748
  while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1749
    push(@{ $self->{name_list} }, $ref);
1750
    $i++;
1615
    $query =
1616
      qq~SELECT vc.id, vc.name,
1617
           vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
1618
         FROM $table vc
1619
         WHERE $where AND (NOT vc.obsolete)
1620
         ORDER BY vc.name~;
1621
  } else {
1622
    $query =
1623
      qq~SELECT DISTINCT vc.id, vc.name,
1624
           vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
1625
         FROM $arap a
1626
         JOIN $table vc ON (a.${table}_id = vc.id)
1627
         WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
1628
         ORDER BY vc.name~;
1629
    push(@values, '%' . $self->{$table} . '%');
1751 1630
  }
1752
  $sth->finish;
1753
  $dbh->disconnect;
1631

  
1632
  $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
1754 1633

  
1755 1634
  $main::lxdebug->leave_sub();
1756 1635

  
1757
  return $i;
1636
  return scalar(@{ $self->{name_list} });
1758 1637
}
1759 1638

  
1760 1639
# the selection sub is used in the AR, AP, IS, IR and OE module
......
1767 1646
  my $ref;
1768 1647
  my $dbh = $self->dbconnect($myconfig);
1769 1648

  
1649
  $table = $table eq "customer" ? "customer" : "vendor";
1650

  
1770 1651
  my $query = qq|SELECT count(*) FROM $table|;
1771
  my $sth   = $dbh->prepare($query);
1772
  $sth->execute || $self->dberror($query);
1773
  my ($count) = $sth->fetchrow_array;
1774
  $sth->finish;
1652
  my ($count) = selectrow_query($self, $dbh, $query);
1775 1653

  
1776 1654
  # build selection list
1777 1655
  if ($count < $myconfig->{vclimit}) {
1778 1656
    $query = qq|SELECT id, name, salesman_id
1779
		FROM $table WHERE not obsolete
1780
		ORDER BY name|;
1781
    $sth = $dbh->prepare($query);
1782
    $sth->execute || $self->dberror($query);
1783

  
1784
    while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1785
      push @{ $self->{"all_$table"} }, $ref;
1786
    }
1787

  
1788
    $sth->finish;
1789

  
1657
                FROM $table WHERE NOT obsolete
1658
                ORDER BY name|;
1659
    $self->{"all_$table"} = selectall_hashref_query($self, $dbh, $query);
1790 1660
  }
1791 1661

  
1792 1662
  # get self
......
1794 1664

  
1795 1665
  # setup sales contacts
1796 1666
  $query = qq|SELECT e.id, e.name
1797
	      FROM employee e
1798
	      WHERE e.sales = '1'
1799
	      AND NOT e.id = $self->{employee_id}|;
1800
  $sth = $dbh->prepare($query);
1801
  $sth->execute || $self->dberror($query);
1802

  
1803
  while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1804
    push @{ $self->{all_employees} }, $ref;
1805
  }
1806
  $sth->finish;
1667
              FROM employee e
1668
              WHERE (e.sales = '1') AND (NOT e.id = ?)|;
1669
  $self->{all_employees} = selectall_hashref_query($self, $dbh, $query, $self->{employee_id});
1807 1670

  
1808 1671
  # this is for self
1809
  push @{ $self->{all_employees} },
1810
    { id   => $self->{employee_id},
1811
      name => $self->{employee} };
1672
  push(@{ $self->{all_employees} },
1673
       { id   => $self->{employee_id},
1674
         name => $self->{employee} });
1812 1675

  
1813 1676
  # sort the whole thing
1814 1677
  @{ $self->{all_employees} } =
......
1817 1680
  if ($module eq 'AR') {
1818 1681

  
1819 1682
    # prepare query for departments
1820
    $query = qq|SELECT d.id, d.description
1821
		FROM department d
1822
		WHERE d.role = 'P'
1823
		ORDER BY 2|;
1683
    $query = qq|SELECT id, description
1684
                FROM department
1685
                WHERE role = 'P'
1686
                ORDER BY description|;
1824 1687

  
1825 1688
  } else {
1826
    $query = qq|SELECT d.id, d.description
1827
		FROM department d
1828
		ORDER BY 2|;
1689
    $query = qq|SELECT id, description
1690
                FROM department
1691
                ORDER BY description|;
1829 1692
  }
1830 1693

  
1831
  $sth = $dbh->prepare($query);
1832
  $sth->execute || $self->dberror($query);
1833

  
1834
  while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1835
    push @{ $self->{all_departments} }, $ref;
1836
  }
1837
  $sth->finish;
1694
  $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
1838 1695

  
1839 1696
  # get languages
1840 1697
  $query = qq|SELECT id, description
1841 1698
              FROM language
1842
	      ORDER BY 1|;
1843
  $sth = $dbh->prepare($query);
1844
  $sth->execute || $self->dberror($query);
1699
              ORDER BY id|;
1845 1700

  
1846
  while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1847
    push @{ $self->{languages} }, $ref;
1848
  }
1849
  $sth->finish;
1701
  $self->{languages} = selectall_hashref_query($self, $dbh, $query);
1850 1702

  
1851 1703
  # get printer
1852 1704
  $query = qq|SELECT printer_description, id
1853 1705
              FROM printers
1854
	      ORDER BY 1|;
1855
  $sth = $dbh->prepare($query);
1856
  $sth->execute || $self->dberror($query);
1857

  
1858
  while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1859
    push @{ $self->{printers} }, $ref;
1860
  }
1861
  $sth->finish;
1706
              ORDER BY printer_description|;
1862 1707

  
1708
  $self->{printers} = selectall_hashref_query($self, $dbh, $query);
1863 1709

  
1864 1710
  # get payment terms
1865 1711
  $query = qq|SELECT id, description
1866 1712
              FROM payment_terms
1867 1713
              ORDER BY sortkey|;
1868
  $sth = $dbh->prepare($query);
1869
  $sth->execute || $self->dberror($query);
1870 1714

  
1871
  while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1872
    push @{ $self->{payment_terms} }, $ref;
1873
  }
1874
  $sth->finish;
1715
  $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
1716

  
1875 1717
  $dbh->disconnect;
1718

  
1876 1719
  $main::lxdebug->leave_sub();
1877 1720
}
1878 1721

  
......
1880 1723
  $main::lxdebug->enter_sub();
1881 1724

  
1882 1725
  my ($self, $myconfig) = @_;
1883
  undef $self->{languages};
1884
  undef $self->{payment_terms};
1885
  undef $self->{printers};
1886 1726

  
1887
  my $ref;
1888 1727
  my $dbh = $self->dbconnect($myconfig);
1889 1728
  # get languages
1890 1729
  my $query = qq|SELECT id, description
1891
              FROM language
1892
	      ORDER BY 1|;
1893
  my $sth = $dbh->prepare($query);
1894
  $sth->execute || $self->dberror($query);
1730
                 FROM language
1731
                 ORDER BY id|;
1895 1732

  
1896
  while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1897
    push @{ $self->{languages} }, $ref;
1898
  }
1899
  $sth->finish;
1733
  $self->{languages} = selectall_hashref_query($self, $dbh, $query);
1900 1734

  
1901 1735
  # get printer
1902 1736
  $query = qq|SELECT printer_description, id
1903 1737
              FROM printers
1904
	      ORDER BY 1|;
1905
  $sth = $dbh->prepare($query);
1906
  $sth->execute || $self->dberror($query);
1738
              ORDER BY printer_description|;
1907 1739

  
1908
  while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1909
    push @{ $self->{printers} }, $ref;
1910
  }
1911
  $sth->finish;
1740
  $self->{printers} = selectall_hashref_query($self, $dbh, $query);
1912 1741

  
1913 1742
  # get payment terms
1914 1743
  $query = qq|SELECT id, description
1915 1744
              FROM payment_terms
1916 1745
              ORDER BY sortkey|;
1917
  $sth = $dbh->prepare($query);
1918
  $sth->execute || $self->dberror($query);
1919 1746

  
1920
  while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1921
    push @{ $self->{payment_terms} }, $ref;
1922
  }
1923
  $sth->finish;
1747
  $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query);
1924 1748

  
1925 1749
  # get buchungsgruppen
1926 1750
  $query = qq|SELECT id, description
1927 1751
              FROM buchungsgruppen|;
1928
  $sth = $dbh->prepare($query);
1929
  $sth->execute || $self->dberror($query);
1930 1752

  
1931
  $self->{BUCHUNGSGRUPPEN} = [];
1932
  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1933
    push @{ $self->{BUCHUNGSGRUPPEN} }, $ref;
1934
  }
1935
  $sth->finish;
1753
  $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
1936 1754

  
1937 1755
  $dbh->disconnect;
1938 1756
  $main::lxdebug->leave_sub();
......
1944 1762

  
1945 1763
  my ($self, $myconfig, $table) = @_;
1946 1764

  
1947
  my $dbh   = $self->dbconnect($myconfig);
1948
  my $where = "1 = 1";
1765
  my $dbh = $self->dbconnect($myconfig);
1766
  my $where;
1949 1767

  
1950
  if (defined $table) {
1951
    if ($table eq 'customer') {
1952
      $where = " d.role = 'P'";
1953
    }
1768
  if ($table eq 'customer') {
1769
    $where = "WHERE role = 'P' ";
1954 1770
  }
1955 1771

  
1956
  my $query = qq|SELECT d.id, d.description
1957
                 FROM department d
1958
	         WHERE $where
1959
	         ORDER BY 2|;
1960
  my $sth = $dbh->prepare($query);
1961
  $sth->execute || $self->dberror($query);
1772
  my $query = qq|SELECT id, description
1773
                 FROM department
1774
                 $where
1775
                 ORDER BY description|;
1776
  $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
1962 1777

  
1963
  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1964
    push @{ $self->{all_departments} }, $ref;
1965
  }
1966
  $sth->finish;
1778
  delete($self->{all_departments}) unless (@{ $self->{all_departments} });
1967 1779

  
1968 1780
  $dbh->disconnect;
1969 1781

  
......
1975 1787

  
1976 1788
  my ($self, $module, $myconfig, $table) = @_;
1977 1789

  
1790
  my ($fld, $arap);
1791
  if ($table eq "customer") {
1792
    $fld = "buy";
1793
    $arap = "ar";
1794
  } else {
1795
    $table = "vendor";
1796
    $fld = "sell";
1797
    $arap = "ap";
1798
  }
1799

  
1978 1800
  $self->all_vc($myconfig, $table, $module);
1979 1801

  
1980 1802
  # get last customers or vendors
1981
  my ($query, $sth);
1803
  my ($query, $sth, $ref);
1982 1804

  
1983 1805
  my $dbh = $self->dbconnect($myconfig);
1984 1806
  my %xkeyref = ();
......
1987 1809

  
1988 1810
    my $transdate = "current_date";
1989 1811
    if ($self->{transdate}) {
1990
      $transdate = qq|'$self->{transdate}'|;
1812
      $transdate = $dbh->quote($self->{transdate});
1991 1813
    }
1992
  
1814

  
1993 1815
    # now get the account numbers
1994 1816
    $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
1995 1817
                FROM chart c, taxkeys tk
1996
                WHERE c.link LIKE '%$module%' AND c.id=tk.chart_id AND tk.id = 
1997
                      (SELECT id FROM taxkeys where taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate desc LIMIT 1)
1818
                WHERE (c.link LIKE ?) AND (c.id = tk.chart_id) AND tk.id =
1819
                  (SELECT id FROM taxkeys WHERE (taxkeys.chart_id = c.id) AND (startdate <= $transdate) ORDER BY startdate DESC LIMIT 1)
1998 1820
                ORDER BY c.accno|;
1999
  
1821

  
2000 1822
    $sth = $dbh->prepare($query);
2001 1823

  
2002
    do_statement($form, $sth, $query);
1824
    do_statement($form, $sth, $query, '%' . $module . '%');
2003 1825

  
2004 1826
    $self->{accounts} = "";
2005
    while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2006
  
1827
    while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1828

  
2007 1829
      foreach my $key (split(/:/, $ref->{link})) {
2008 1830
        if ($key =~ /$module/) {
2009
  
1831

  
2010 1832
          # cross reference for keys
2011 1833
          $xkeyref{ $ref->{accno} } = $key;
2012
  
1834

  
2013 1835
          push @{ $self->{"${module}_links"}{$key} },
2014 1836
            { accno       => $ref->{accno},
2015 1837
              description => $ref->{description},
2016 1838
              taxkey      => $ref->{taxkey_id},
2017 1839
              tax_id      => $ref->{tax_id} };
2018
  
1840

  
2019 1841
          $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2020 1842
        }
2021 1843
      }
......
2032 1854

  
2033 1855
  if (($module eq "AP") || ($module eq "AR")) {
2034 1856
    # get tax rates and description
2035
    $query = qq| SELECT * FROM tax t|;
1857
    $query = qq|SELECT * FROM tax|;
2036 1858
    $self->{TAX} = selectall_hashref_query($form, $dbh, $query);
2037 1859
  }
2038 1860

  
2039 1861
  if ($self->{id}) {
2040
    my $arap = ($table eq 'customer') ? 'ar' : 'ap';
2041

  
2042
    $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
2043
                a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
2044
		a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
2045
		c.name AS $table, a.department_id, d.description AS department,
2046
		a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
2047
		a.employee_id, e.name AS employee, a.gldate, a.type
2048
		FROM $arap a
2049
		JOIN $table c ON (a.${table}_id = c.id)
2050
		LEFT JOIN employee e ON (e.id = a.employee_id)
2051
		LEFT JOIN department d ON (d.id = a.department_id)
2052
		WHERE a.id = $self->{id}|;
2053
    $sth = $dbh->prepare($query);
2054
    do_statement($form, $sth, $query);
1862
    $query =
1863
      qq|SELECT
1864
           a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid,
1865
           a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes,
1866
           a.intnotes, a.department_id, a.amount AS oldinvtotal,
1867
           a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
1868
           c.name AS $table,
1869
           d.description AS department,
1870
           e.name AS employee
1871
         FROM $arap a
1872
         JOIN $table c ON (a.${table}_id = c.id)
1873
         LEFT JOIN employee e ON (e.id = a.employee_id)
1874
         LEFT JOIN department d ON (d.id = a.department_id)
1875
         WHERE a.id = ?|;
1876
    $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
2055 1877

  
2056
    $ref = $sth->fetchrow_hashref(NAME_lc);
2057 1878
    foreach $key (keys %$ref) {
2058 1879
      $self->{$key} = $ref->{$key};
2059 1880
    }
2060
    $sth->finish;
2061

  
2062 1881

  
2063 1882
    my $transdate = "current_date";
2064 1883
    if ($self->{transdate}) {
2065
      $transdate = qq|'$self->{transdate}'|;
1884
      $transdate = $dbh->quote($self->{transdate});
2066 1885
    }
2067
  
1886

  
2068 1887
    # now get the account numbers
2069 1888
    $query = qq|
2070
        SELECT 
2071
          c.accno, 
2072
          c.description, 
2073
          c.link, 
2074
          c.taxkey_id, 
2075
          tk.tax_id 
2076
        FROM chart c
2077
        LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
2078
        WHERE 
2079
          c.link LIKE ?
2080
          AND 
2081
                  (tk.chart_id = c.id AND NOT c.link like '%_tax%')
2082
          OR (NOT tk.chart_id = c.id AND     c.link like '%_tax%')
2083
          AND 
2084
            tk.id = (  SELECT id from taxkeys 
2085
                       WHERE taxkeys.chart_id = c.id 
2086
                       AND startdate <= ? 
2087
                       ORDER BY startdate desc LIMIT 1
2088
                    ) 
2089
        ORDER BY c.accno|;
2090
    
1889
      SELECT
1890
        c.accno, c.description, c.link, c.taxkey_id,
1891
        tk.tax_id
1892
      FROM chart c
1893
      LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
1894
      WHERE (c.link LIKE ?) AND (tk.chart_id = c.id) AND NOT (c.link LIKE '%_tax%')
1895
      ORDER BY c.accno|;
1896

  
2091 1897
    $sth = $dbh->prepare($query);
2092
    do_statement($form, $sth, $query, "%$module%", $transdate);
2093
  
1898
    do_statement($form, $sth, $query, "%" . $module . "%");
1899

  
2094 1900
    $self->{accounts} = "";
2095
    while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2096
  
1901
    while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1902

  
2097 1903
      foreach my $key (split(/:/, $ref->{link})) {
2098 1904
        if ($key =~ /$module/) {
2099
  
1905

  
2100 1906
          # cross reference for keys
2101 1907
          $xkeyref{ $ref->{accno} } = $key;
2102
  
1908

  
2103 1909
          push @{ $self->{"${module}_links"}{$key} },
2104 1910
            { accno       => $ref->{accno},
2105 1911
              description => $ref->{description},
2106 1912
              taxkey      => $ref->{taxkey_id},
2107 1913
              tax_id      => $ref->{tax_id} };
2108
  
1914

  
2109 1915
          $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2110 1916
        }
2111 1917
      }
......
2113 1919

  
2114 1920

  
2115 1921
    # get amounts from individual entries
2116
    $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
2117
                a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate, t.id
2118
		FROM acc_trans a
2119
		JOIN chart c ON (c.id = a.chart_id)
2120
		LEFT JOIN project p ON (p.id = a.project_id)
2121
                LEFT JOIN tax t ON (t.id=(SELECT tk.tax_id from taxkeys tk WHERE (tk.taxkey_id=a.taxkey) AND ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id=a.taxkey) THEN tk.chart_id=a.chart_id ELSE 1=1 END) OR (c.link='%tax%')) AND startdate <=a.transdate ORDER BY startdate DESC LIMIT 1)) 
2122
                WHERE a.trans_id = $self->{id}
2123
		AND a.fx_transaction = '0'
2124
		ORDER BY a.oid,a.transdate|;
1922
    $query =
1923
      qq|SELECT
1924
           c.accno, c.description,
1925
           a.source, a.amount, a.memo, a.transdate, a.cleared, a.project_id, a.taxkey,
1926
           p.projectnumber,
1927
           t.rate, t.id
1928
         FROM acc_trans a
1929
         LEFT JOIN chart c ON (c.id = a.chart_id)
1930
         LEFT JOIN project p ON (p.id = a.project_id)
1931
         LEFT JOIN tax t ON (t.id= (SELECT tk.tax_id FROM taxkeys tk
1932
                                    WHERE (tk.taxkey_id=a.taxkey) AND
1933
                                      ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
1934
                                        THEN tk.chart_id = a.chart_id
1935
                                        ELSE 1 = 1
1936
                                        END)
1937
                                       OR (c.link='%tax%')) AND
1938
                                      (startdate <= a.transdate) ORDER BY startdate DESC LIMIT 1))
1939
         WHERE a.trans_id = ?
1940
         AND a.fx_transaction = '0'
1941
         ORDER BY a.oid, a.transdate|;
2125 1942
    $sth = $dbh->prepare($query);
2126
    do_statement($form, $sth, $query);
2127

  
2128
    my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1943
    do_statement($form, $sth, $query, $self->{id});
2129 1944

  
2130 1945
    # get exchangerate for currency
2131 1946
    $self->{exchangerate} =
2132
      $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
2133
                              $fld);
1947
      $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2134 1948
    my $index = 0;
2135 1949

  
2136 1950
    # store amounts in {acc_trans}{$key} for multiple accounts
2137 1951
    while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2138 1952
      $ref->{exchangerate} =
2139
        $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
2140
                                $fld);
1953
        $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
2141 1954
      if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
2142 1955
        $index++;
2143 1956
      }
......
2150 1963
    }
2151 1964

  
2152 1965
    $sth->finish;
2153
    $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
2154
                  (SELECT c.accno FROM chart c
2155
		   WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2156
                  (SELECT c.accno FROM chart c
2157
		   WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2158
		FROM defaults d|;
2159
    $sth = $dbh->prepare($query);
2160
    do_statement($form, $sth, $query);
2161

  
2162
    $ref = $sth->fetchrow_hashref(NAME_lc);
1966
    $query =
1967
      qq|SELECT
1968
           d.curr AS currencies, d.closedto, d.revtrans,
1969
           (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1970
           (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1971
         FROM defaults d|;
1972
    $ref = selectfirst_hashref_query($self, $dbh, $query);
2163 1973
    map { $self->{$_} = $ref->{$_} } keys %$ref;
2164
    $sth->finish;
2165 1974

  
2166 1975
  } else {
2167 1976

  
2168 1977
    # get date
2169
    $query = qq|SELECT current_date AS transdate,
2170
                d.curr AS currencies, d.closedto, d.revtrans,
2171
                  (SELECT c.accno FROM chart c
2172
		   WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2173
                  (SELECT c.accno FROM chart c
2174
		   WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
2175
		FROM defaults d|;
2176
    $sth = $dbh->prepare($query);
2177
    do_statement($form, $sth, $query);
2178

  
2179
    $ref = $sth->fetchrow_hashref(NAME_lc);
1978
    $query =
1979
       qq|SELECT
1980
            current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans,
1981
            (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1982
            (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1983
          FROM defaults d|;
1984
    $ref = selectfirst_hashref_query($self, $dbh, $query);
2180 1985
    map { $self->{$_} = $ref->{$_} } keys %$ref;
2181
    $sth->finish;
2182 1986

  
2183 1987
    if ($self->{"$self->{vc}_id"}) {
2184 1988

  
......
2189 1993

  
2190 1994
      $self->lastname_used($dbh, $myconfig, $table, $module);
2191 1995

  
2192
      my $fld = ($table eq 'customer') ? 'buy' : 'sell';
2193

  
2194 1996
      # get exchangerate for currency
2195 1997
      $self->{exchangerate} =
2196
        $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
2197
                                $fld);
1998
        $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2198 1999

  
2199 2000
    }
2200 2001

  
2201 2002
  }
2202 2003

  
2203
  $sth->finish;
2204

  
2205 2004
  $dbh->disconnect;
2206 2005

  
2207 2006
  $main::lxdebug->leave_sub();
......
2213 2012
  my ($self, $dbh, $myconfig, $table, $module) = @_;
2214 2013

  
2215 2014
  my $arap  = ($table eq 'customer') ? "ar" : "ap";
2015
  $table = $table eq "customer" ? "customer" : "vendor";
2216 2016
  my $where = "1 = 1";
2217 2017

  
2218 2018
  if ($self->{type} =~ /_order/) {
......
2225 2025
  }
2226 2026

  
2227 2027
  my $query = qq|SELECT MAX(id) FROM $arap
2228
		              WHERE $where
2229
			      AND ${table}_id > 0|;
2230
  my $sth = $dbh->prepare($query);
2231
  $sth->execute || $self->dberror($query);
2232

  
2233
  my ($trans_id) = $sth->fetchrow_array;
2234
  $sth->finish;
2028
                 WHERE $where AND ${table}_id > 0|;
2029
  my ($trans_id) = selectrow_query($self, $dbh, $query);
2235 2030

  
2236 2031
  $trans_id *= 1;
2237
  $query = qq|SELECT ct.name, a.curr, a.${table}_id,
2238
              current_date + ct.terms AS duedate, a.department_id,
2239
	      d.description AS department
2240
	      FROM $arap a
2241
	      JOIN $table ct ON (a.${table}_id = ct.id)
2242
	      LEFT JOIN department d ON (a.department_id = d.id)
2243
	      WHERE a.id = $trans_id|;
2244
  $sth = $dbh->prepare($query);
2245
  $sth->execute || $self->dberror($query);
2246

  
2247
  ($self->{$table},  $self->{currency},      $self->{"${table}_id"},
2248
   $self->{duedate}, $self->{department_id}, $self->{department})
2249
    = $sth->fetchrow_array;
2250
  $sth->finish;
2032
  $query =
2033
    qq|SELECT
2034
         a.curr, a.${table}_id, a.department_id,
2035
         d.description AS department,
2036
         ct.name, current_date + ct.terms AS duedate
2037
       FROM $arap a
2038
       LEFT JOIN $table ct ON (a.${table}_id = ct.id)
2039
       LEFT JOIN department d ON (a.department_id = d.id)
2040
       WHERE a.id = ?|;
2041
  ($self->{currency},   $self->{"${table}_id"}, $self->{department_id},
2042
   $self->{department}, $self->{$table},        $self->{duedate})
2043
    = selectrow_query($self, $dbh, $query, $trans_id);
2251 2044

  
2252 2045
  $main::lxdebug->leave_sub();
2253 2046
}
......
2258 2051
  my ($self, $myconfig, $thisdate, $days) = @_;
2259 2052

  
2260 2053
  my $dbh = $self->dbconnect($myconfig);
2261
  my ($sth, $query);
2054
  my $query;
2262 2055

  
2263 2056
  $days *= 1;
2264 2057
  if ($thisdate) {
2265 2058
    my $dateformat = $myconfig->{dateformat};
2266 2059
    $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2267

  
2268
    $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
2269
                FROM defaults|;
2270
    $sth = $dbh->prepare($query);
2271
    $sth->execute || $self->dberror($query);
2060
    $thisdate = $dbh->quote($thisdate);
2061
    $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
2272 2062
  } else {
2273
    $query = qq|SELECT current_date AS thisdate
2274
                FROM defaults|;
2275
    $sth = $dbh->prepare($query);
2276
    $sth->execute || $self->dberror($query);
2063
    $query = qq|SELECT current_date AS thisdate|;
2277 2064
  }
2278 2065

  
2279
  ($thisdate) = $sth->fetchrow_array;
2280
  $sth->finish;
2066
  ($thisdate) = selectrow_query($self, $dbh, $query);
2281 2067

  
2282 2068
  $dbh->disconnect;
2283 2069

  
......
2339 2125
  my $dbh = $self->dbconnect_noauto($myconfig);
2340 2126

  
2341 2127
  my $query = qq|DELETE FROM status
2342
                 WHERE formname = '$self->{formname}'
2343
		 AND trans_id = ?|;
2344
  my $sth = $dbh->prepare($query) || $self->dberror($query);
2128
                 WHERE (formname = ?) AND (trans_id = ?)|;
2129
  my $sth = prepare_query($self, $dbh, $query);
2345 2130

  
2346 2131
  if ($self->{formname} =~ /(check|receipt)/) {
2347 2132
    for $i (1 .. $self->{rowcount}) {
2348
      $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
2349
      $sth->finish;
2133
      do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
2350 2134
    }
2351 2135
  } else {
2352
    $sth->execute($self->{id}) || $self->dberror($query);
2353
    $sth->finish;
2136
    do_statement($self, $sth, $query, $self->{formname}, $self->{id});
2354 2137
  }
2138
  $sth->finish();
2355 2139

  
2356 2140
  my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2357 2141
  my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2358 2142

  
2359 2143
  my %queued = split / /, $self->{queued};
2144
  my @values;
2360 2145

  
2361 2146
  if ($self->{formname} =~ /(check|receipt)/) {
2362 2147

  
2363 2148
    # this is a check or receipt, add one entry for each lineitem
2364 2149
    my ($accno) = split /--/, $self->{account};
2365
    $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
2366
		chart_id) VALUES (?, '$printed',
2367
		'$queued{$self->{formname}}', '$self->{prinform}',
2368
		(SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
2369
    $sth = $dbh->prepare($query) || $self->dberror($query);
2150
    $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
2151
                VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
2152
    @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
2153
    $sth = prepare_query($self, $dbh, $query);
2370 2154

  
2371 2155
    for $i (1 .. $self->{rowcount}) {
2372 2156
      if ($self->{"checked_$i"}) {
2373
        $sth->execute($self->{"id_$i"}) || $self->dberror($query);
2374
        $sth->finish;
2157
        do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
2375 2158
      }
2376 2159
    }
2160
    $sth->finish();
2161

  
2377 2162
  } else {
2378
    $query = qq|INSERT INTO status (trans_id, printed, emailed,
2379
		spoolfile, formname)
2380
		VALUES ($self->{id}, '$printed', '$emailed',
2381
		'$queued{$self->{formname}}', '$self->{formname}')|;
2382
    $dbh->do($query) || $self->dberror($query);
2163
    $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2164
                VALUES (?, ?, ?, ?, ?)|;
2165
    do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
2166
             $queued{$self->{formname}}, $self->{formname});
2383 2167
  }
2384 2168

  
2385 2169
  $dbh->commit;
......
2388 2172
  $main::lxdebug->leave_sub();
2389 2173
}
2390 2174

  
2391
#--- 4 locale ---#
2392
# $main::locale->text('SAVED') 
2393
# $main::locale->text('DELETED') 
2394
# $main::locale->text('ADDED')
2395
# $main::locale->text('PAYMENT POSTED')
2396
# $main::locale->text('POSTED')
2397
# $main::locale->text('POSTED AS NEW')
2398
# $main::locale->text('ELSE')
2399
# $main::locale->text('SAVED FOR DUNNING')
2400
# $main::locale->text('DUNNING STARTED')
2401
# $main::locale->text('PRINTED')
2402
# $main::locale->text('MAILED')
2403
# $main::locale->text('SCREENED')
2404
# $main::locale->text('invoice')
2405
# $main::locale->text('proforma')
2406
# $main::locale->text('sales_order')
2407
# $main::locale->text('packing_list')
2408
# $main::locale->text('pick_list')
2409
# $main::locale->text('purchase_order')
2410
# $main::locale->text('bin_list')
2411
# $main::locale->text('sales_quotation')
2412
# $main::locale->text('request_quotation')
2413

  
2414
sub save_history {
2415
	$main::lxdebug->enter_sub();
2416
	
2417
	my $self = shift();
2418
	my $dbh = shift();
2419
	
2420
	if(!exists $self->{employee_id}) {
2421
		&get_employee($self, $dbh);
2422
	}
2423
	
2424
	my $query =
2425
    qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done) | .
2426
    qq|VALUES (?, ?, ?, ?)|;
2427
  my @values = (conv_i($self->{id}), conv_i($self->{employee_id}),
2428
                $self->{addition}, $self->{what_done});
2429
  do_query($self, $dbh, $query, @values);
2430
	
2431
	$main::lxdebug->leave_sub();
2432
}
2433

  
2434
sub get_history {
2435
	$main::lxdebug->enter_sub();
2436
	
2437
	my $self = shift();
2438
	my $dbh = shift();
2439
	my $trans_id = shift();
2440
	my $restriction = shift();
2441
	my @tempArray;
2442
	my $i = 0;
2443
	if ($trans_id ne "") {
2444
		my $query =
2445
      qq|SELECT h.employee_id, h.itime::timestamp(0) AS itime, h.addition, h.what_done, emp.name | .
2446
      qq|FROM history_erp h | .
2447
      qq|LEFT JOIN employee emp | .
2448
      qq|ON emp.id = h.employee_id | .
2449
      qq|WHERE trans_id = ? |
2450
      . $restriction;
2451
	
2452
		my $sth = $dbh->prepare($query) || $self->dberror($query);
2453
	
2454
		$sth->execute($trans_id) || $self->dberror("$query ($trans_id)");
2455

  
2456
		while(my $hash_ref = $sth->fetchrow_hashref()) {
2457
			$hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
2458
			$hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
2459
			$tempArray[$i++] = $hash_ref; 
2460
		}
2461
    $main::lxdebug->leave_sub() and return \@tempArray
2462
      if ($i > 0 && $tempArray[0] ne "");
2463
	}
2464
	$main::lxdebug->leave_sub();
2465
	return 0;
2466
}
2467

  
2468 2175
sub save_status {
2469 2176
  $main::lxdebug->enter_sub();
2470 2177

  
......
2475 2182
  my $formnames  = $self->{printed};
2476 2183
  my $emailforms = $self->{emailed};
2477 2184

  
2478
  $query = qq|DELETE FROM status
2479
              WHERE formname = '$self->{formname}'
2480
		      AND trans_id = $self->{id}|;
2481
  $dbh->do($query) || $self->dberror($query);
2185
  my $query = qq|DELETE FROM status
... Dieser Diff wurde abgeschnitten, weil er die maximale Anzahl anzuzeigender Zeilen überschreitet.

Auch abrufbar als: Unified diff